Fix the cross-compiling examples.
[wiki] / examples / cross-compiling-64-bit-windows-binary.mdwn
1 [[!meta title="cross-compiling tinc for 64-bit 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 64-bit 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 64-bit MinGW.
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 mingw-w64 git-core wget quilt
25         sudo apt-get build-dep tinc
26
27 Other Linux distributions may also have 64-bit MinGW packages, use their respective
28 package management tools to install them.  Debian installs the cross-compiler
29 in `/usr/x86_64-w64-mingw32/`. Other distributions might install it in another
30 directory however. Check in which directory
31 it is installed, and replace all occurences of `x86_64-w64-mingw32` 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 `mingw64/` in the home directory.  We use
37 apt-get and wget to get the required libraries necessary for tinc, and use
38 `git` to get the latest development version of tinc.
39
40         mkdir $HOME/mingw64
41         cd $HOME/mingw64
42         apt-get source liblzo2-dev zlib1g-dev libssl-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 `mingw64` that will set
48 up the necessary environment variables so configure scripts and Makefiles will
49 use the 64-bit MinGW version of GCC and binutils:
50
51         mkdir $HOME/bin
52         cat >$HOME/bin/mingw64 << 'EOF'
53         #!/bin/sh
54         PREFIX=x86_64-w64-mingw32
55         export CC=$PREFIX-gcc
56         export CXX=$PREFIX-g++
57         export CPP=$PREFIX-cpp
58         export RANLIB=$PREFIX-ranlib
59         export PATH="/usr/x86_64-w64-mingw32/bin:$PATH"
60         exec "$@"
61         EOF
62         chmod u+x $HOME/bin/mingw64
63
64 If `$HOME/bin` is not already part of your `$PATH`, you need to add it:
65
66         export PATH="$HOME/bin:$PATH"
67
68 We use this script to call `./configure` and `make` with the right environment
69 variables, but only when the `./configure` script doesn't support cross-compilation itself.
70 You can also run the export commands from the `mingw64` script by
71 hand instead of calling the mingw64 script for every `./configure` or `make`
72 command, or execute `$HOME/bin/mingw64 $SHELL` to get a shell with these
73 environment variables set, but in this howto we will call it explicitly every
74 time it is needed.
75
76 ### Compiling LZO
77
78 Cross-compiling LZO is easy:
79
80         cd $HOME/mingw64/lzo2-2.06
81         ./configure --host=x86_64-w64-mingw32
82         make
83         DESTDIR=$HOME/mingw64 make install
84
85 If it fails with a message about not passing the "ACC" test,
86 create a symlink for the missing getopt.h file as mentioned above.
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/mingw64/zlib-1.2.7.dfsg
94         mingw64 ./configure --static
95         mingw64 make
96         DESTDIR=$HOME/mingw64 mingw64 make install
97
98 ### Compiling OpenSSL
99
100 Although older versions will not compile, OpenSSL 1.0.0 is easy.
101 However, `apt-get source` will have applied
102 Debian-specific patches that break cross-compiling a Windows binary.
103 You need to undo those patches first.
104 Do not use the `-j` option when compiling OpenSSL, it will break.
105
106         cd $HOME/mingw64/openssl-1.0.1c
107         quilt pop -a
108         mingw64 ./Configure --openssldir=$HOME/mingw64/usr/local mingw64
109         mingw64 make
110         mingw64 make install
111
112 ### Compiling tinc
113
114 Now that all the dependencies have been cross-compiled, we can cross-compile
115 tinc.  Since we use a clone of the git repository here, we need to run
116 `autoreconf` first. If you want to cross-compile tinc from a released tarball,
117 this is not necessary.
118
119         cd $HOME/mingw64/tinc
120         autoreconf -fsi
121         ./configure --host=x86_64-w64-mingw32 --with-zlib=$HOME/mingw64/usr/local
122         make