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