git:// links no longer work, refer to the https:// one.
[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 mingw-w64 wine git-core quilt
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/i686-w64-mingw32/`. 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 `i686-w64-mingw32` 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         PREFIX=i686-w64-mingw32
58         export CC=$PREFIX-gcc
59         export CXX=$PREFIX-g++
60         export CPP=$PREFIX-cpp
61         export RANLIB=$PREFIX-ranlib
62         export PATH="/usr/$PREFIX/bin:$PATH"
63         exec "$@"
64         EOF
65         chmod u+x $HOME/bin/mingw
66
67 If `$HOME/bin` is not already part of your `$PATH`, you need to add it:
68
69         export PATH="$HOME/bin:$PATH"
70
71 We use this script to call `./configure` and `make` with the right environment
72 variables, but only when the `./configure` script doesn't support cross-compilation itself.
73 You can also run the export commands from the `mingw` script by
74 hand instead of calling the mingw script for every `./configure` or `make`
75 command, or execute `$HOME/bin/mingw $SHELL` to get a shell with these
76 environment variables set, but in this howto we will call it explicitly every
77 time it is needed.
78
79 ### Compiling LZO
80
81 Cross-compiling LZO is easy:
82
83         cd $HOME/mingw/lzo2-2.08
84         ./configure --host=i686-w64-mingw32
85         make
86         DESTDIR=$HOME/mingw make install
87
88 ### Compiling Zlib
89
90 Cross-compiling Zlib is also easy, but a plain `make` failed to compile the
91 tests, so we only build the static library here:
92
93         cd $HOME/mingw/zlib-1.2.8.dfsg
94         mingw ./configure
95         mingw make libz.a
96         DESTDIR=$HOME/mingw mingw make install
97
98 ### Compiling LibreSSL
99
100 Tinc can use either OpenSSL or LibreSSL. The latter is recommended.
101
102         cd $HOME/mingw/libressl-2.3.3
103         CC=i686-w64-mingw32-gcc ./configure --host=i686-w64-mingw32
104         make
105         DESTDIR=$HOME/mingw make install
106
107 ### Compiling tinc
108
109 Now that all the dependencies have been cross-compiled, we can cross-compile
110 tinc.  Since we use a clone of the git repository here, we need to run
111 `autoreconf` first. If you want to cross-compile tinc from a released tarball,
112 this is not necessary.
113
114         cd $HOME/mingw/tinc
115         autoreconf -fsi
116         ./configure --host=i686-w64-mingw32 --with-zlib=$HOME/mingw/usr/local
117         make
118
119 ### Testing tinc
120
121 Since Wine was installed, you can execute the resulting binary even on Linux.
122 Wine does not provide a TAP-Win32 device, but you can use the `DeviceType = dummy` option to test it without. 
123 The following command should work in any case:
124
125         $HOME/mingw/tinc/src/tincd.exe --help
126