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