Try forcing <pre> manually.
[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
71 If `$HOME/bin` is not already part of your `$PATH`, you need to add it:
72
73         export PATH="$HOME/bin:$PATH"
74
75 We use this script to call `./configure` and `make` with the right environment
76 variables, but only when the `./configure` script doesn't support cross-compilation itself.
77 You can also run the export commands from the `mingw64` script by
78 hand instead of calling the mingw64 script for every `./configure` or `make`
79 command, or execute `$HOME/bin/mingw64 $SHELL` to get a shell with these
80 environment variables set, but in this howto we will call it explicitly every
81 time it is needed.
82
83 ### Compiling LZO
84
85 Cross-compiling LZO is easy:
86
87         cd $HOME/mingw64/lzo2-2.03
88         ./configure --host=amd64-mingw32msvc
89         make
90         DESTDIR=$HOME/mingw64 make install
91
92 If it fails with a message about not passing the "ACC" test,
93 create a symlink for the missing getopt.h file as mentioned above.
94
95 ### Compiling Zlib
96
97 Cross-compiling Zlib is also easy, but a plain `make` failed to compile the
98 tests, so we only build the static library here:
99
100         cd $HOME/mingw64/zlib-1.2.3.3.dfsg
101         mingw64 ./configure
102         mingw64 make libz.a
103         DESTDIR=$HOME/mingw64 mingw64 make install
104
105 ### Compiling OpenSSL
106
107 Although older versions will not compile, OpenSSL 1.0.0 is easy.
108 Do not use the `-j` option when compiling OpenSSL, it will break.
109
110         cd $HOME/mingw64/openssl-1.0.0
111         mingw64 ./Configure --openssldir=$HOME/mingw64/usr/local mingw64
112         mingw64 make
113         mingw64 make install
114
115 ### Compiling tinc
116
117 Now that all the dependencies have been cross-compiled, we can cross-compile
118 tinc.  Since we use a clone of the git repository here, we need to run
119 `autoreconf` first. If you want to cross-compile tinc from a released tarball,
120 this is not necessary.
121
122         cd $HOME/mingw64/tinc
123         autoreconf -fsi
124         ./configure --host=amd64-mingw32msvc --with-openssl=$HOME/mingw64/usr/local
125         make