Add missing dot in version number.
[wiki] / examples / cross-compiling-windows-binary.mdwn
1 [[!meta title="cross-compiling tinc for Windows under Linux using MinGW"]]
2
3 ## Howto: cross-compiling tinc for Windows under Linux using MinGW
4
5 This howto describes how to create a Windows binary of tinc. Although it is
6 possible to compile tinc under Windows itself, cross-compiling it under Linux
7 is much faster. It is also much easier to get all the dependencies in a modern
8 distribution. Therefore, this howto deals with cross-compiling tinc with MinGW
9 under Linux on a Debian distribution.
10
11 ### Overview
12
13 The idea is simple:
14
15 * Install MinGW and Wine.
16 * Create a directory where we will perform all cross-compilations.
17 * Get all the necessary sources.
18 * Cross-compile everything.
19
20 ### Installing the prerequisites for cross-compilation
21
22 There are only a few packages that need to be installed as root to get started:
23
24 >     sudo apt-get install mingw32 wine git-core
25 >     sudo apt-get build-dep tinc
26
27 Other Linux distributions may also have MinGW packages, use their respective
28 package management tools to install them.  Debian installs the cross-compiler
29 in `/usr/i586-mingw32msvc/`. Other distributions might install it in another
30 directory however, for example `/usr/i686-pc-mingw32/`. Check in which directory
31 it is installed, and replace all occurences of `i586-mingw32msvc` in this
32 example with the correct name from your distribution.
33
34 ### Setting up the build directory and getting the sources
35
36 We will create a directory called `mingw/` in the home directory.  We use
37 apt-get to get the required libraries necessary for tinc, and use `git` to get
38 the latest development version of tinc.
39
40 >     mkdir $HOME/mingw
41 >     cd $HOME/mingw
42 >     apt-get source openssl liblzo2-dev zlib1g-dev
43 >     git clone git://tinc-vpn.org/tinc
44
45 ### Making cross-compilation easy
46
47 To make cross-compiling easy, we create a script called `mingw` that will set
48 up the necessary environment variables so configure scripts and Makefiles will
49 use the MinGW version of GCC and binutils:
50
51 >     mkdir $HOME/bin
52 >     cat >$HOME/bin/mingw << EOF
53 >     #!/bin/sh
54 >     export CC=i586-mingw32msvc-gcc
55 >     export CXX=i586-mingw32msvc-g++
56 >     export CPP=i586-mingw32msvc-cpp
57 >     export RANLIB=i586-mingw32msvc-ranlib
58 >     export PATH="/usr/i586-mingw32msvc/bin:$PATH"
59 >     exec "$@"
60 >     EOF
61
62 If `$HOME/bin` is not already part of your `$PATH`, you need to add it:
63
64 >     export PATH="$HOME/bin:$PATH"
65
66 We use this script to call `./configure` and `make` with the right environment
67 variables, but only when the `./configure` script doesn't support cross-compilation itself.
68 You can also run the export commands from the `mingw` script by
69 hand instead of calling the mingw script for every `./configure` or `make`
70 command, or execute `$HOME/bin/mingw $SHELL` to get a shell with these
71 environment variables set, but in this howto we will call it explicitly every
72 time it is needed.
73
74 ### Compiling LZO
75
76 Cross-compiling LZO is easy:
77
78 >     cd $HOME/mingw/lzo2-2.03
79 >     ./configure --host=i586-mingw32msvc
80 >     make
81 >     DESTDIR=$HOME/mingw make install
82
83 ### Compiling Zlib
84
85 Cross-compiling Zlib is also easy, but a plain `make` failed to compile the
86 tests, so we only build the static library here:
87
88 >     cd $HOME/mingw/zlib-1.2.3.3.dfsg
89 >     mingw ./configure
90 >     mingw make libz.a
91 >     DESTDIR=$HOME/mingw mingw make install
92
93 ### Compiling OpenSSL
94
95 OpenSSL is always a bit hard to compile, because they have their own
96 `Configure` script that needs some tweaking. There is also a small bug in
97 e_os2.h that breaks compilation with recent versions of GCC. First download
98 this [[openssl-cross-compilation.diff]] to your home directory, then patch
99 OpenSSL, and then compile as usual.  Do not use the `-j` option when compiling
100 OpenSSL, it will break.
101
102 >     cd $HOME/mingw/openssl-0.9.8k
103 >     patch < $HOME/openssl-cross-compilation.diff
104 >     mingw ./Configure --openssldir=$HOME/mingw/usr/local mingw
105 >     mingw make
106 >     mingw make install
107
108 ### Compiling tinc
109
110 Now that all the dependencies have been cross-compiled, we can cross-compile
111 tinc.  Since we use a clone of the git repository here, we need to run
112 `autoreconf` first. If you want to cross-compile tinc from a released tarball,
113 this is not necessary.
114
115 >     cd $HOME/mingw/tinc
116 >     autoreconf -fsi
117 >     ./configure --host=i586-mingw32msvc --with-openssl=$HOME/mingw/usr/local
118 >     make
119
120 ### Testing tinc
121
122 Since Wine was installed, you can execute the resulting binary even on Linux.
123 You cannot do much however, since tinc requires a TAP-Win32 device, which is
124 not available in Wine. Still, the following command should work:
125
126 >     $HOME/mingw/tinc/src/tincd.exe --help
127