2e850c12a40de8203db48c70a2a5fcd283e6aaf3
[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 ### Setting up the build directory and getting the sources
28
29 We will create a directory called `mingw/` in the home directory.  We use
30 apt-get to get the required libraries necessary for tinc, and use `git` to get
31 the latest development version of tinc.
32
33 >     mkdir $HOME/mingw
34 >     cd $HOME/mingw
35 >     apt-get source openssl liblzo2-dev zlib1g-dev
36 >     git clone git://tinc-vpn.org/tinc
37
38 ### Making cross-compilation easy
39
40 To make cross-compiling easy, we create a script called `mingw` that will set
41 up the necessary environment variables so configure scripts and Makefiles will
42 use the MinGW version of GCC and binutils:
43
44 >     mkdir $HOME/bin
45 >     cat >$HOME/bin/mingw << EOF
46 >     #!/bin/sh
47 >     export CC=i586-mingw32msvc-gcc
48 >     export CXX=i586-mingw32msvc-g++
49 >     export CPP=i586-mingw32msvc-cpp
50 >     export RANLIB=i586-mingw32msvc-ranlib
51 >     export PATH="/usr/i586-mingw32msvc/bin:$PATH"
52 >     exec "$@"
53 >     EOF
54
55 If `$HOME/bin` is not already part of your `$PATH`, you need to add it:
56
57 >     export PATH="$HOME/bin:$PATH"
58
59 We use this script to call `./configure` and `make` with the right environment
60 variables, but only when the `./configure` script doesn't support cross-compilation itself.
61 You can also run the export commands from the `mingw` script by
62 hand instead of calling the mingw script for every `./configure` or `make`
63 command, or execute `$HOME/bin/mingw $SHELL` to get a shell with these
64 environment variables set, but in this howto we will call it explicitly every
65 time it is needed.
66
67 ### Compiling LZO
68
69 Cross-compiling LZO is easy:
70
71 >     cd $HOME/mingw/lzo2-2.03
72 >     ./configure --host=i586-mingw32msvc
73 >     make
74 >     DESTDIR=$HOME/mingw make install
75
76 ### Compiling Zlib
77
78 Cross-compiling Zlib is also easy, but a plain `make` failed to compile the
79 tests, so we only build the static library here:
80
81 >     cd $HOME/mingw/zlib-1.2.3.3.dfsg
82 >     mingw ./configure
83 >     mingw make libz.a
84 >     DESTDIR=$HOME/mingw mingw make install
85
86 ### Compiling OpenSSL
87
88 OpenSSL is always a bit hard to compile, because they have their own
89 `Configure` script that needs some tweaking. There is also a small bug in
90 e_os2.h that breaks compilation with recent versions of GCC. First download
91 this [[openssl-cross-compilation.diff]] to your home directory, then patch
92 OpenSSL, and then compile as usual.  Do not use the `-j` option when compiling
93 OpenSSL, it will break.
94
95 >     cd $HOME/mingw/openssl-0.9.8k
96 >     patch < $HOME/openssl-cross-compilation.diff
97 >     mingw ./Configure --openssldir=$HOME/mingw/usr/local mingw
98 >     mingw make
99 >     mingw make install
100
101 ### Compiling tinc
102
103 Now that all the dependencies have been cross-compiled, we can cross-compile
104 tinc.  Since we use a clone of the git repository here, we need to run
105 `autoreconf` first. If you want to cross-compile tinc from a released tarball,
106 this is not necessary.
107
108 >     cd $HOME/mingw/tinc
109 >     autoreconf -fsi
110 >     ./configure --host=i586-mingw32msvc --with-openssl=$HOME/mingw/usr/local
111 >     make
112
113 ### Testing tinc
114
115 Since Wine was installed, you can execute the resulting binary even on Linux.
116 You cannot do much however, since tinc requires a TAP-Win32 device, which is
117 not available in Wine. Still, the following command should work:
118
119 >     $HOME/mingw/tinc/src/tincd.exe --help
120