Compiling on GNU∕Linux and 🟉BSD

A quick guide to get started with OpenTTD development on Linux and other *nix-like systems. For help with compiling on various platforms, join #openttd on OFTC.

Contents

Step 1. Required/recommended software and how to install it

Instructions for the various distributions should also be applicable to derivatives of them. Text that looks like this is a shell command.

Mandriva

(tested on Mandriva 2009 Spring)

urpmi gcc gcc-c++ libsdl-devel zlib1-devel git patch patchutils

Debian, Ubuntu

Tested on Debian buster/testing. Take a look at the CompileFarm's Dockerfile to see what's installed there to build on Debian: https://github.com/OpenTTD/CompileFarm/blob/master/base-linux/Dockerfile

Automatically

To install the same dependencies used by the official packages, run:

sudo apt build-dep openttd

This will not install any utility programs. For that, do:

sudo apt install git

Manually

Install:

In one command:

sudo apt install build-essential pkg-config libsdl1.2-dev git zlib1g-dev liblzo2-dev liblzma-dev libfontconfig-dev libicu-dev libfluidsynth-dev

In at least one instance the package "libsdl1.2debian-all" was required for a full installation with a PowerPC build of Debian. Without this package, only the dedicated server would compile. [1] Remark: This was not needed on a custom Debian 2.6.26-2-amd64 server when compiling r20739.

Fedora

Automatically

For the same dependencies as the official package:

yum-builddep openttd -y

NOTE: This does not include gcc and gcc-c++ (which you can install via (sudo) yum install gcc/gcc-c++)

Manually

In one command (as root): yum install gcc-c++ SDL-devel zlib-devel esound xz-devel lzo-devel -y

openSUSE

Automatically

For the same dependencies as the official package:

zypper source-install --build-deps-only openttd

Manually

Use YaST to download:

You can use the command-line tool `zypper' as well:

zypper install gcc-c++ SDL-devel zlib-devel libpng-devel libicu-devel lzo-devel freetype2-devel xz-devel git

Gentoo

As root run:

emerge --onlydeps -av openttd && emerge git

This will install all dependencies as well as Git.

If you want to use the most current version (aka git/master), run the following:

git clone git@github.com:OpenTTD/OpenTTD.git

Arch Linux

As root run

pacman -S --needed openttd

and it will install all the OpenTTD dependencies. It will install OpenTTD and all the dependencies. Free graphics set (openttd-opengfx) and free soundset (openttd-opensfx) are also available in the official repositories.

If you just want development environment, this should do the trick:

sudo pacman -S --needed desktop-file-utils fontconfig freetype2 hicolor-icon-theme icu libpng lzo2 sdl xz zlib

openttd-bin, openttd-git, and other PKGBUILDs are available in the AUR.

FreeBSD

To fetch, compile and install all necessary dependencies, as root run:

cd /usr/ports/games/openttd && make depends clean

If you are going to compile OpenTTD from subversion then again, as root run:

cd /usr/ports/devel/subversion && make install clean

NetBSD

To fetch, compile and install all necessary dependencies run:

cd /usr/pkgsrc/games/openttd && make depends clean

If you are going to compile OpenTTD from subversion then again run:

cd /usr/pkgsrc/devel/subversion-base && make install clean

You should be asked for the root password once the code is compiled and ready for installation.

OpenBSD

As root, do (as root) pkg_add gmake openttd to install OpenTTD with its dependencies. If you only want the dependencies, run:

pkg_add gmake icu4c lzo2 png sdl xz

You may also want to pkg_add git.

Warning: Since OpenBSD 6.2, amd64 and i386 users must configure OpenTTD to use clang. A plain ./configure picks gcc, but the old gcc doesn't work with the ic4uc package. Use ./configure CC=clang CXX=clang++

Step 2. Getting the source and applying patches

Releases

You can download the .tar.gz source files from the OpenTTD website. Extracting tarball will give you a directory called openttd-<version>, where <version> is the version you downloaded. Enter this directory, and you're ready to proceed to step 3.

Most current version (Git master)

The most current version of the official code is stored in our Git repository (https://github.com/OpenTTD/OpenTTD). Git tracks various versions and shares our code. For you as a user this means you can quickly get the newest sourcecode.

Initial download

To obtain the source code from GitHub, type from command line:

git clone https://github.com/OpenTTD/OpenTTD.git openttd

This downloads the repository and makes a "working tree" in the directory openttd/. The working tree tracks the latest version of our "master" branch. All git commands given from now on assume your shell's working directory is the openttd/ directory.

You can now skip to step 3 if you just want to compile the latest version.

Updating

If you have previously downloaded the repository and a new version is available on GitHub, you can update your working tree to the latest version. Inside your tree run the following:

git pull

This downloads the updates from GitHub, and "fast-forwards" your working tree to the latest version of master, or it tells you if your tree is already up to date.

Also, if you have made any local changes to the source, these will be preserved. If your changes overlap with the pulled changes, then Git may prompt you to run git stash before git pull. This will move your changes to the stash. After the pull, you run git stash pop to merge your changes with the pulled changes.

If a conflict between the changes occurs, then git stash pop will put conflict markers in your files. You must edit the files to resolve the conflict. Use git status to files with conflicts, and git reset filename to remove the conflict status. After you resolve the conflict, run git stash drop to clean the stash.

Creating a patch file

If you have made changes to your source that you want to share or upload as a patch, you can ask git to generate a diff. The command

git diff > mypatch.diff

creates a diff file (also referred to as "patch") which you can submit to the developers to share your improvements.

Reverting local changes

You can undo local changes to a file with

git checkout filename

or to your entire working directory with:

git checkout .

Applying a patch

/File/en/Klipper.png
To Do
Update this section to use git, not svn.

To apply a patch (diff file) to the source code, update your source code to the patch file's revision. Let's say the patch file has been made on r1234. You can see this in the first lines of the patch:

Index: foo.c
===================================================================
--- foo.c (revision 1234)
+++ foo.c (working copy)

To update your source code to r1234, run

svn update -r 1234

Then you're ready to merge the patch file

patch -p0 < mydiff.diff

Now (optionally) update the source code to the newest revision with

svn update

If this produces conflicts (files marked with "C"), tough luck, the patch doesn't work for the latest revision (you can always revert and retry the patching, skipping this last step and playing an older version).

Step 3. Compiling commands, running the compiled binary

cd <directory in which the OpenTTD source code is located>

openttd >= 1.11

In openttd 1.11 cmake has replaced .configure

mkdir build
cd build
cmake ..
make
./openttd (to start the game)

openttd < 1.11

For openttd < 1.11 the following commands apply:
./configure

Your system's default compiler (usually GCC) will be used -- if you want to use a specific compiler, export CC and CXX in the environment, or pass them as configure arguments. Example for clang: $ ./configure CC=clang CXX=clang++

NOTE: Without a compiler configured, you will receive the "please define the CC/CXX environment to where it is located" message when running configure. Define (export) them, or pass them as given in the example above.

This will create a Makefile. The next step is to run make; BSD users may need to run gmake instead of make. If you have changed files or updated your source, only modified code will be recompiled. Now:

make --jobs=<#>

On multi-core systems, using a high thread number (the #) can speed up compiling greatly; it's commonly recommended to use the number of logical cores (obtainable using getconf _NPROCESSORS_ONLN on Linux at least; sysctl hw.ncpu on BSD) plus one. (The theory behind this is that if one compiling thread is currently limited by I/O speed, the others should still fully saturate the available hardware threads.) For example, on an Intel i5 with two hyper-threaded cores (i.e. four logical ones), make --jobs=5 took 1 minute and 33 seconds, and plain make three minutes and 18 seconds.

There should now be an OpenTTD executable in the bin subdirectory. To install the OpenTTD binary to a directory included in your PATH variable, so it can be run as only openttd, do a make install as root/using sudo.

Installing TiMidity and sound fonts

In Debian/Ubuntu:
sudo apt-get install timidity freepats
In Fedora:
yum install timidity++

Timidity wants to talk to ESD, so you will need to "enable software sound mixing (ESD)" (System → Preferences → Sound).

In Arch Linux:
 pacman -S timidity++ timidity-freepats
 cp /etc/timidity++/timidity-freepats.cfg /etc/timidity++/timidity.cfg
In OpenBSD:
pkg_add timidity--

Debian package

/File/en/Klipper.png
To Do
Doesn't actually work, at least not on Ubuntu 13.04.

You should be able to build a Debian package from the latest source using the supplied debian directory. You can replace step 3 by this. This Debian directory is, conforming to OpenTTD standards, put inside the os subdirectory and needs to be moved a level up first:

mv os/debian .

After that, use normal debian tools to build the package:

dpkg-buildpackage -rfakeroot -uc -us

Installing can then be done by executing (as root): dpkg -i ../openttd-<version>_<architecture>.deb

Data files (original TTD as well as replacement) will need to be manually copied to /usr/share/games/openttd/data, since they are not included in the .deb due to licensing issues.

Now, when you run openttd, you will always be starting the version you have just built and installed.