Cross Compiling

From OpenTTD

Jump to: navigation, search

This guide will show you how to cross compile OpenTTD on Linux to be run under Windows, using the freely available GNU and MinGW tools. It is based upon the Building a cross compiler (compile on Linux, run on Windows) guide on the Gentoo Forums.

Contents

[edit] IMPORTANT

At the moment I cannot get libpng and zlib to work correctly with OpenTTD so if you want you can just ignore the "Zlib and libpng" section.

[edit] What you will need

The first things you need to get are the sources for GCC and Binutils, and the binaries for the Win32 API and MinGW runtime. You need the binaries for the Win32 API and MinGW runtime, because you need a compiler to build those in the first place; but you need those to build the compiler; and so on....

You can get the latest version of these packages from the MinGW website.

binutils-*-src.tar.gz (binutils-2.15.91-20040904-1-src.tar.gz 14mb)

  • Windows API - headers and import libraries to support the Win32 API

w32api-*.tar.gz (w32api-3.2.tar.gz)

  • MinGW runtime - Run time headers to build a compiler system to build code to be run on Windows

runtime-*.tar.gz (mingw-runtime-3.7.tar.gz)

I could not get the version of GCC from MinGW to compile, so I had to obtain a clean version from GCC. Choose a mirror near to you from the mirror list and then download ./releases/gcc-3.4.3/gcc-3.4.3.tar.gz. For example:
ftp://ftp.mirrorservice.org/sites/sources.redhat.com/pub/gcc/releases/gcc-3.4.3/gcc-3.4.3.tar.gz.

  • GCC - the GNU Compiler Collection

gcc-*.tar.gz

You optionally need zlib and libpng, if you want to be able to have compressed save games, and png screenshots.

zlib-*.tar.gz (zlib-1.2.2.tar.gz)

libpng-*-config.tar.gz (libpng-1.2.8-config.tar.gz)

[edit] Setting up the directories

First you need to decide where everything will go, you need a temporary directory to build stuff in, and a permanent one to store all the tools in. You do not need system wide access, so they will both fit nicely into your home dir, for example:

~/build - temporary directory for building (build dir)
~/cross-tools - permanent directory for all the tools (dest dir)

So make those directories, cd into them, download the files, and decompress them. GCC and Binutils need to be decompressed in the build dir while MinGW and the Win32 API need to be decompressed into the dest dir:

cd ~
mkdir -p cross-tools/i386-mingw32msvc
mkdir -p build/source
cd ~/build/source
echo "downloading ottd, please correct the VERSION NUMBER"
wget http://nightly.openttd.org/latest/OTTD-source-nightly-r3535.tar.gz
cd ~/cross-tools/i386-mingw32msvc
tar xvfz mingw-runtime-3.7.tar.g
tar xvfz w32api-3.2.tar.gz
cd ~/build
tar xvfz source/binutils-2.15.91-20040904-1-src.tar.gz
tar xvfz source/gcc-3.4.3.tar.gz

Configuring and building GCC and Binutils takes fooorrreeeevver so it is a good idea to keep your source directories clean in case something goes wrong.

mkdir ~/build/binutils-i386-mingw32msvc
mkdir ~/build/gcc-i386-mingw32msvc

[edit] Building Binutils and GCC

Configure, build and install Binutils. This will take a rather long time, so you may want to go and play your favourite game.

cd /home/luca/build/binutils-i386-mingw32msvc
/home/luca/build/binutils-2.15.91-20040904-1/configure --prefix=/home/luca/cross-tools --target=i386-mingw32msvc
make
make install

Next you need to add the path of your newly compiled tools to your PATH variable:

export PATH=/home/luca/cross-tools/bin:$PATH

Make sure you add your current PATH after the new stuff, otherwise you will have problems later on. Now more compiling, GCC this time.

cd /home/luca/build/gcc-i386-mingw32msvc
/home/luca/build/gcc-3.4.3/configure -v --prefix=/home/luca/cross-tools --target=i386-mingw32msvc --with-headers=/home/luca/cross-tools/i386-mingw32msvc/include --with-gnu-as --with-gnu-ld --without-newlib --disable-multilib
make
make install

If all went well, you should have got no errors and should now have a compiler that can compile on Linux for Windows. If you want to free up some space you should clean out the sys-includes directory, it is needed during the gcc compile, but not by anything else.

rm -rf /home/luca/cross-tools/i386-mingw32msvc/sys-include

[edit] Testing

Here is a little test to make sure your compiler is working - it is no use trying anything else unless it is.</br></br> Create the file hello.c:

#include <stdio.h>
int main(void)
{
  printf("Hello World!\n");
  exit(0);
}

And to compile:

i386-mingw32msvc-gcc hello.c -o hello.exe

To test it either run it (in a DOS prompt) under Windows:

hello.exe
Hello World!

Or under WINE on Linux:

wine hello.exe
Hello World!

[edit] Zlib and libpng

Next you need to compile zlib...

cd /home/luca/build
tar xvfz zlib-1.2.2.tar.gz
cd zlib-1.2.2/
./configure --prefix=/home/luca/cross-tools/
make CC=i386-mingw32msvc-gcc AR="i386-mingw32msvc-ar rc" RANLIB=i386-mingw32msvc-ranlib
make install prefix=/home/luca/cross-tools/

...and libpng...

cd /home/luca/build
tar xvfz libpng-1.2.8-config.tar.gz
cd libpng-1.2.8-config
cp scripts/makefile.linux Makefile
make target=i386-mingw32msvc host=i386-mingw32msvc build=i386-linux prefix=/home/luca/cross-tools/
make install prefix=/home/luca/cross-tools/

...and voila! You should now be able to compile OpenTTD.

[edit] Compiling OpenTTD

Nothing out of the ordinary needs to be done for OpenTTD, just a few options need to be set in Makefile.config. Everthing else should be left unset / set as the default:

WITH_ZLIB:=0
WITH_PNG:=0
WITH_SDL:=0

WIN32:=1
UNIX:=
MINGW:=1

CC_TARGET:=i386-mingw32msvc-gcc
CC_HOST:=cc
WINDRES:=i386-mingw32msvc-windres
Personal tools