(no commit message)
[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 gcc-mingw32 mingw64 git-core wget
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/amd64-mingw32msvc/`. Other distributions might install it in another
30 directory however. Check in which directory
31 it is installed, and replace all occurences of `amd64-mingw32msvc` in this
32 example with the correct name from your distribution.
33
34 At the time of writing, the gcc-mingw32 package contains the 64-bit compiler as
35 well, in the future this might be put into its own package. Also, a header file
36 is missing in the amd64-mingw32msvc include directory, a workaround is to
37 create a symlink to the otherwise identical 32-bit version of that header file:
38
39         ln -s ../../i586-mingw32msvc/include/getopt.h /usr/amd64-mingw32msvc/include/getopt.h
40
41 ### Setting up the build directory and getting the sources
42
43 We will create a directory called `mingw64/` in the home directory.  We use
44 apt-get and wget to get the required libraries necessary for tinc, and use
45 `git` to get the latest development version of tinc.
46
47         mkdir $HOME/mingw64
48         cd $HOME/mingw64
49         apt-get source liblzo2-dev zlib1g-dev
50         wget http://www.openssl.org/source/openssl-1.0.0.tar.gz
51         tar xzf openssl-1.0.0.tar.gz
52         git clone git://tinc-vpn.org/tinc
53
54 ### Making cross-compilation easy
55
56 To make cross-compiling easy, we create a script called `mingw64` that will set
57 up the necessary environment variables so configure scripts and Makefiles will
58 use the 64-bit MinGW version of GCC and binutils:
59
60         mkdir $HOME/bin
61         cat >$HOME/bin/mingw64 << EOF
62         #!/bin/sh
63         export CC=amd64-mingw32msvc-gcc
64         export CXX=amd64-mingw32msvc-g++
65         export CPP=amd64-mingw32msvc-cpp
66         export RANLIB=amd64-mingw32msvc-ranlib
67         export PATH="/usr/amd64-mingw32msvc/bin:$PATH"
68         exec "$@"
69         EOF
70         chmod u+x $HOME/bin/mingw64
71
72 If `$HOME/bin` is not already part of your `$PATH`, you need to add it:
73
74         export PATH="$HOME/bin:$PATH"
75
76 We use this script to call `./configure` and `make` with the right environment
77 variables, but only when the `./configure` script doesn't support cross-compilation itself.
78 You can also run the export commands from the `mingw64` script by
79 hand instead of calling the mingw64 script for every `./configure` or `make`
80 command, or execute `$HOME/bin/mingw64 $SHELL` to get a shell with these
81 environment variables set, but in this howto we will call it explicitly every
82 time it is needed.
83
84 ### Compiling LZO
85
86 Cross-compiling LZO is easy:
87
88         cd $HOME/mingw64/lzo2-2.03
89         ./configure --host=amd64-mingw32msvc
90         make
91         DESTDIR=$HOME/mingw64 make install
92
93 If it fails with a message about not passing the "ACC" test,
94 create a symlink for the missing getopt.h file as mentioned above.
95
96 ### Compiling Zlib
97
98 Cross-compiling Zlib is also easy, but a plain `make` failed to compile the
99 tests, so we only build the static library here:
100
101         cd $HOME/mingw64/zlib-1.2.3.3.dfsg
102         mingw64 ./configure
103         mingw64 make libz.a
104         DESTDIR=$HOME/mingw64 mingw64 make install
105
106 ### Compiling OpenSSL
107
108 Although older versions will not compile, OpenSSL 1.0.0 is easy.
109 However, `apt-get source` will have applied
110 Debian-specific patches that break cross-compiling a Windows binary.
111 You need to undo those patches first.
112 Do not use the `-j` option when compiling OpenSSL, it will break.
113
114         cd $HOME/mingw64/openssl-1.0.0
115         quilt pop -a
116         mingw64 ./Configure --openssldir=$HOME/mingw64/usr/local mingw64
117         mingw64 make
118         mingw64 make install
119
120 ### Compiling tinc
121
122 Now that all the dependencies have been cross-compiled, we can cross-compile
123 tinc.  Since we use a clone of the git repository here, we need to run
124 `autoreconf` first. If you want to cross-compile tinc from a released tarball,
125 this is not necessary.
126
127         cd $HOME/mingw64/tinc
128         autoreconf -fsi
129         ./configure --host=amd64-mingw32msvc --with-openssl=$HOME/mingw64/usr/local
130         make