| 1 |
The Advance Guide to |
| 2 |
Building the Mingw-w64 x86_64-w64-mingw32 cross-compiler |
| 3 |
|
| 4 |
== About this document == |
| 5 |
|
| 6 |
This document describes more advanced methods in building the mingw-w64 |
| 7 |
cross compiler. It also includes details about the toolchain |
| 8 |
dependencies and relevant options that can be passed to the GCC |
| 9 |
configure. |
| 10 |
|
| 11 |
The commands are run under a MSYS/MinGW setup unless specified otherwise. |
| 12 |
Using Cygwin or any other POSIX compatible shell should work. |
| 13 |
|
| 14 |
== Targeted audience == |
| 15 |
|
| 16 |
This document assumes readers are familiar with the UNIX Command Line |
| 17 |
and building the GNU toolchain in general, but would want a more fine |
| 18 |
grained control over the capabilities of the cross compiler. |
| 19 |
|
| 20 |
For a basic guide on building the mingw-w64 toolchain, please refer to |
| 21 |
"mingw-w64-howto-build.txt". |
| 22 |
|
| 23 |
== Notes about LTO == |
| 24 |
|
| 25 |
LTO for COFF targets can be enabled without installing libelf, as of gcc-4.6.0. |
| 26 |
|
| 27 |
== Version changes == |
| 28 |
|
| 29 |
Date / Version / Author |
| 30 |
2009-10-09 1.0 Jonathan Yong <jon_y[a]users.sourceforge.com> |
| 31 |
2010-01-03 1.1 Jonathan Yong <jon_y[a]users.sourceforge.com> |
| 32 |
2010-05-18 1.11 Jonathan Yong <jon_y[a]users.sourceforge.com> |
| 33 |
2010-08-31 1.12 Jonathan Yong <jon_y[a]users.sourceforge.com> |
| 34 |
2013-01-27 1.13 Jonathan Yong <jon_y[a]users.sourceforge.com> |
| 35 |
|
| 36 |
== Table of Contents == |
| 37 |
|
| 38 |
* Some Basic Info about mingw-w64 [SBSCINF] |
| 39 |
* Host Support Libraries [HOSTLIB] |
| 40 |
* The GNU MP Bignum Library [BGMPLIB] |
| 41 |
* MPFR [BMPFRLB] |
| 42 |
* Multiprecision [BMPCLIB] |
| 43 |
* The Parma Polyhedra Library [BPPLLIB] |
| 44 |
* The Clunky Loop Generator (PPL port) [BCLGLIB] |
| 45 |
* Target Support Libraries [TGTSPLB] |
| 46 |
* pthreads-win32 [PTHRW32] |
| 47 |
* Building the Multilib Cross MinGW-w64 GCC the |
| 48 |
lazy way(With ADA support and libgomp) [LAZYW64] |
| 49 |
* ADA Bootstrap Phase [ADABOOT] |
| 50 |
* MinGW-w64 cross Binutils [CRSBINU] |
| 51 |
* Install mingw-w64 Headers [MGW64HD] |
| 52 |
* Setup Symlinks [SETSYML] |
| 53 |
* Build Core GCC [BCORGCC] |
| 54 |
* Build the mingw-w64 CRT [BMGWCRT] |
| 55 |
* Build libgcc [BLIBGCC] |
| 56 |
* Build pthreads [BPTHR32] |
| 57 |
* Continue the build [CTNTBLD] |
| 58 |
|
| 59 |
You can search the keys (i.e. [BMPCLIB]) to jump to that section. |
| 60 |
|
| 61 |
== Some Basic Info about MinGW-w64 == [SBSCINF] |
| 62 |
|
| 63 |
MinGW-w64 began as a fork of mingw.org MinGW to support 64bit windows as |
| 64 |
a target. |
| 65 |
|
| 66 |
Canonical Triplet info: |
| 67 |
mingw.org MinGW: i686-pc-mingw32 |
| 68 |
mingw-w64 64bit: x86_64-pc-mingw32 (obsolete triplet) |
| 69 |
x86_64-w64-mingw32 (preferred triplet) |
| 70 |
mingw-w64 32bit: i686-pc-mingw32 (mingw.org compatibility mode) |
| 71 |
i686-w64-mingw32 (preferred triplet) |
| 72 |
|
| 73 |
== Host Support Libraries == [HOSTLIB] |
| 74 |
|
| 75 |
These libraries are used by GCC on the "host" side. It means that these |
| 76 |
should be built for the system that will run the newly built GCC. |
| 77 |
|
| 78 |
=== The GNU MP Bignum Library (GMP) === [BGMPLIB] |
| 79 |
|
| 80 |
Homepage: <http://gmplib.org/> |
| 81 |
Depends on: None |
| 82 |
Depended by: MPC, MPFR, PPL, CLooG |
| 83 |
GCC dependency type: Hard (GCC won't build without it) |
| 84 |
|
| 85 |
GMP is primarily written in C and assembly, but it comes with C++ bindings. |
| 86 |
The asm part is selected by the $host_cpu detected by configure. If your |
| 87 |
platform is not detected properly, you can use --host=none-none-none to |
| 88 |
disable asm optimizations. Be sure to set the correct CC, CXX, NM, RANLIB |
| 89 |
and AR variables. |
| 90 |
|
| 91 |
The C++ bindings are not built by default, but it is required by PPL. To fix |
| 92 |
this, use "--enable-cxx" and "CPPFLAGS=-fexceptions". PPL needs the |
| 93 |
-fexceptions part to throw exceptions properly, but it is not strictly |
| 94 |
required. |
| 95 |
|
| 96 |
On Windows hosts, only static or shared builds can be used at a time, but not |
| 97 |
both. This is reasoned by the incompatible header set used by static/shared |
| 98 |
builds. If you do use static builds, you should build any libs that depend on |
| 99 |
it as static to prevent accidentally exporting GMP symbols in other dlls. |
| 100 |
|
| 101 |
=== MPFR === [BMPFRLB] |
| 102 |
|
| 103 |
Homepage: <http://www.mpfr.org/> |
| 104 |
Depends on: GMP |
| 105 |
Depended by: None |
| 106 |
GCC dependency type: Hard (GCC won't build without it) |
| 107 |
|
| 108 |
MPFR isn't particularly fussy about configure options, so the usual: |
| 109 |
./configure |
| 110 |
make |
| 111 |
make install |
| 112 |
is fine. |
| 113 |
|
| 114 |
=== Multiprecision (MPC) === [BMPCLIB] |
| 115 |
|
| 116 |
Homepage: <http://www.multiprecision.org/> |
| 117 |
Depends on: GMP |
| 118 |
Depended by: None |
| 119 |
GCC dependency type: Hard (GCC won't build without it) |
| 120 |
|
| 121 |
MPC isn't particularly fussy about configure options, so the usual: |
| 122 |
./configure |
| 123 |
make |
| 124 |
make install |
| 125 |
is fine. |
| 126 |
|
| 127 |
=== The Parma Polyhedra Library (PPL) === [BPPLLIB] |
| 128 |
|
| 129 |
Homepage: <http://www.cs.unipr.it/ppl/> |
| 130 |
Depends on: GMP |
| 131 |
Depended by: ClooG |
| 132 |
GCC dependency type: None (GCC does not use it directly) |
| 133 |
|
| 134 |
PPL requires that GMP be built with C++ bindings (--enable-cxx) and |
| 135 |
-fexceptions (to allow exceptions to be thrown across GMP). |
| 136 |
|
| 137 |
=== The Clunky Loop Generator (PPL port) === [BCLGLIB] |
| 138 |
|
| 139 |
Homepage: <http://repo.or.cz/w/cloog-ppl.git> |
| 140 |
Depends on: GMP, PPL |
| 141 |
Depended by: None |
| 142 |
GCC dependency type: Soft (GCC can build without it) |
| 143 |
If you are planning to use CLooG with static PPL, remeber to add |
| 144 |
'--with-host-libstdcxx="-lstdc++ -lsupc++"' to configure. |
| 145 |
CLooG needs to be told to use the PPL backend, so use: |
| 146 |
./configure --with-ppl=<PPL install prefix> |
| 147 |
make |
| 148 |
make install |
| 149 |
|
| 150 |
== Target Support Libraries == [TGTSPLB] |
| 151 |
|
| 152 |
Target support libraries are for use with mingw-w64 itself. It should not |
| 153 |
be used by the host directly. Hence the target libs will need to be compiled |
| 154 |
with a compiler targeting mingw-w64. |
| 155 |
|
| 156 |
=== pthreads-win32 === [PTHRW32] |
| 157 |
Homepage: <http://sourceware.org/pthreads-win32/> |
| 158 |
Depends on: None |
| 159 |
Depended by: None |
| 160 |
GCC dependency type: Soft (Required for GCC libgomp support) |
| 161 |
|
| 162 |
Download instructions: |
| 163 |
cvs -d :pserver:[email protected]:/cvs/pthreads-win32 login |
| 164 |
{enter ``anoncvs'' for the password} |
| 165 |
cvs -d :pserver:[email protected]:/cvs/pthreads-win32 checkout pthreads |
| 166 |
|
| 167 |
Patch: <http://sourceware.org/ml/pthreads-win32/2009/msg00030/w64sup.patch> |
| 168 |
|
| 169 |
Note: pthreads-win32 does not come with a autotools based build system. It |
| 170 |
uses a Makefile "GNUmakefile" to build the pthreads dll. |
| 171 |
|
| 172 |
Note: To allow win64 support, apply the patch from the mailing list. |
| 173 |
|
| 174 |
General Build and install instructions: |
| 175 |
Step 1) type "make clean GC CROSS=x86_64-w64-mingw32-" |
| 176 |
Note: The CROSS variable specifies your win64/win32 toolchain |
| 177 |
triplet prefix. To prevent dll clobbering by 32bit/64bit |
| 178 |
builds having the same names, you can edit GC_DLL in the |
| 179 |
GNUmakefile (line 440) to produce differently named dlls |
| 180 |
based on its "bitness" |
| 181 |
(eg. pthreadGC2-64.dll, pthreadGC2-32.dll). |
| 182 |
Step 2) copy pthreadGC2.dll (or $GC_DLL) to your path |
| 183 |
Step 3) Install the lib by copying pthreadGC2.dll as libpthread.a to |
| 184 |
your GCC libdir (eg. PREFIX/x86_64-w64-mingw32/lib) |
| 185 |
Step 4) Copy pthread.h, sched.h and semaphore.h to the GCC include dir |
| 186 |
(eg. PREFIX/x86_64-w64-mingw32/include) |
| 187 |
Step 5) Edit the pthread.h in your include dir and change the following |
| 188 |
section from: |
| 189 |
======================================================================= |
| 190 |
#if HAVE_CONFIG_H |
| 191 |
#include "config.h" |
| 192 |
#endif /* HAVE_CONFIG_H */ |
| 193 |
======================================================================= |
| 194 |
to: |
| 195 |
======================================================================= |
| 196 |
/* #if HAVE_CONFIG_H */ |
| 197 |
#include "pconfig.h" |
| 198 |
/* #endif HAVE_CONFIG_H */ |
| 199 |
======================================================================= |
| 200 |
This is needed to prevent conflicts with other autotools based packages |
| 201 |
that actually use "config.h" for options. |
| 202 |
Step 6) Copy the pthread-win32 config.h to your include dir as pconfig.h |
| 203 |
|
| 204 |
32bit build instructions (assuming mutilib 64bit default GCC) |
| 205 |
* Edit the GNUmakefile, and change: |
| 206 |
======================================================================= |
| 207 |
AR = $(CROSS)ar |
| 208 |
DLLTOOL = $(CROSS)dlltool |
| 209 |
CC = $(CROSS)gcc |
| 210 |
CXX = $(CROSS)g++ |
| 211 |
RANLIB = $(CROSS)ranlib |
| 212 |
RC = $(CROSS)windres |
| 213 |
======================================================================= |
| 214 |
to |
| 215 |
======================================================================= |
| 216 |
AR = $(CROSS)ar |
| 217 |
DLLTOOL = $(CROSS)dlltool -m i386 |
| 218 |
CC = $(CROSS)gcc -m32 |
| 219 |
CXX = $(CROSS)g++ -m32 |
| 220 |
RANLIB = $(CROSS)ranlib |
| 221 |
RC = $(CROSS)windres -F pe-i386 |
| 222 |
======================================================================= |
| 223 |
* You can also use a patch for 32bit builds such as |
| 224 |
<http://mingw-w64.pastebin.com/pastebin.php?dl=f7b38df38> |
| 225 |
* Next, follow the General Build and install instructions |
| 226 |
|
| 227 |
== Building the Multilib Cross MinGW-w64 GCC the lazy way == |
| 228 |
(With ADA support and libgomp) [LAZYW64] |
| 229 |
|
| 230 |
Its a lazy method because it involves building GCC with all enabled |
| 231 |
front ends only once, without restarting the cross GCC build. |
| 232 |
|
| 233 |
First of all, your host compiler (native system "gcc") must support ADA. |
| 234 |
If your platform does not have GNAT binaries, use the binaries from: |
| 235 |
<https://libre.adacore.com/libre/download/> |
| 236 |
|
| 237 |
If you do not want ADA support, or confident about your host ADA support, |
| 238 |
you can skip the ADA bootstrap phase. |
| 239 |
|
| 240 |
=== ADA Bootstrap Phase === [ADABOOT] |
| 241 |
|
| 242 |
Bootstrapping ADA is needed if ADA is to be supported by the mingw-w64 |
| 243 |
cross GCC. |
| 244 |
|
| 245 |
To begin bootstrap ADA, you must grab the GCC source that will be used |
| 246 |
for the mingw-w64 cross GCC, so that the ADA support is as close as |
| 247 |
possible with the target GCC version. |
| 248 |
|
| 249 |
Use the source to build a native compiler for your host machine. You must |
| 250 |
enable at least C, ADA, and C++ support. It is advisable to prefix |
| 251 |
(i.e. using --prefix=/opt/test) the entire native bootstrap toolchain |
| 252 |
in order to avoid overwriting the compiler provided by your host system. |
| 253 |
You should also build a prefixed native Binutils for use with the new |
| 254 |
bootstrap compiler. |
| 255 |
|
| 256 |
Remember to build GCC OUTSIDE its source directory, NOT inside it and |
| 257 |
certainly NOT in a sub-directory. |
| 258 |
|
| 259 |
=== MinGW-w64 cross Binutils === [CRSBINU] |
| 260 |
|
| 261 |
The official method to build the mingw-w64 toolchain is to set --prefix |
| 262 |
and --with-sysroot to the same directory to allow the toolchain to be |
| 263 |
relocatable. To make Multilib support possible, you need to use |
| 264 |
"--enable-targets=x86_64-w64-mingw32,i686-w64-mingw32". |
| 265 |
|
| 266 |
An example configure line for Binutils is: |
| 267 |
../path/to/binutils/configure --prefix=<prefix> --with-sysroot=<prefix> \ |
| 268 |
--enable-targets=x86_64-w64-mingw32,i686-w64-mingw32 \ |
| 269 |
--host=<build triplet> --build=<build triplet> \ |
| 270 |
--target=x86_64-w64-mingw32 |
| 271 |
|
| 272 |
=== Install mingw-w64 Headers === [MGW64HD] |
| 273 |
|
| 274 |
When you checkout the svn trunk from the mingw-w64 developer repository, there |
| 275 |
should be a directory called mingw-w64-headers. Install it with: |
| 276 |
../path/to/mingw-w64-headers/configure --prefix=<prefix>/x86_64-w64-mingw32 \ |
| 277 |
--enable-sdk=no --build=<host triplet> --host=x86_64-w64-mingw32 |
| 278 |
make install |
| 279 |
|
| 280 |
To view the available sdks, use --help. The prefix should be similar to the |
| 281 |
prefix used for cross Binutils. Although the mingw-w64 cross GCC is not |
| 282 |
installed yet, configure will not fail it as it only checks the --host option. |
| 283 |
It is important to set the --host option correctly, failing to do so will |
| 284 |
cause the cross GCC to fail to find the expected system headers. |
| 285 |
|
| 286 |
=== Setup Symlinks === [SETSYML] |
| 287 |
|
| 288 |
Your install root for the mingw-w64 cross toolchain should contain the |
| 289 |
following directories. Create the directories if missing. It is easier |
| 290 |
to use "ln -s" softlinks to link directories than to copy them over. |
| 291 |
|
| 292 |
<root>/x86_64-w64-mingw32 |
| 293 |
<root>/x86_64-w64-mingw32/include [headers previously installed here] |
| 294 |
<root>/x86_64-w64-mingw32/lib |
| 295 |
<root>/x86_64-w64-mingw32/lib32 |
| 296 |
<root>/x86_64-w64-mingw32/lib64 [link to neighbor lib] |
| 297 |
<root>/mingw [link to x86_64-w64-mingw32] |
| 298 |
<root>/mingw/include |
| 299 |
<root>/mingw/lib |
| 300 |
<root>/mingw/lib32 |
| 301 |
<root>/mingw/lib64 [link to neighbor lib] |
| 302 |
|
| 303 |
On Windows, you can use "ntfs link" <http://elsdoerfer.name/=ntfslink> |
| 304 |
to create "junction points" to link directories. These junction points |
| 305 |
are transparent to user mode applications such as GCC. |
| 306 |
|
| 307 |
Make sure the "mingw" directory mirrors "x86_64-w64-mingw32" exactly. Ditto |
| 308 |
for "lib64" and "lib" |
| 309 |
|
| 310 |
=== Build Core GCC === [BCORGCC] |
| 311 |
|
| 312 |
GCC can be configured in many ways, the following is an example that worked. |
| 313 |
Make sure to add your cross Binutils binaries to your $PATH before continuing. |
| 314 |
|
| 315 |
If you did use the ADA compiler from adacore to produce a bootstrap ADA |
| 316 |
compiler, make sure to add the prefix in such a way that it is found |
| 317 |
before your default host compiler. |
| 318 |
(i.e. export PATH=/opt/adaboot/bin:$PATH:/mingw64/path/bin) |
| 319 |
|
| 320 |
Some of the configure options do not sound possible with a stage-1 cross, |
| 321 |
but be assured that it has been tested. |
| 322 |
|
| 323 |
Remember to build GCC OUTSIDE its source directory, NOT inside it and certainly |
| 324 |
NOT in a sub-directory. |
| 325 |
|
| 326 |
Example: |
| 327 |
../gcc-trunk/configure --{host,build}=<build triplet> \ |
| 328 |
--target=x86_64-w64-mingw32 --enable-multilib --enable-64bit \ |
| 329 |
--{prefix,with-sysroot}=<prefix> --enable-version-specific-runtime-libs \ |
| 330 |
--enable-shared --with-dwarf --enable-fully-dynamic-string \ |
| 331 |
--enable-languages=c,ada,c++,fortran,objc,obj-c++ --enable-libgomp \ |
| 332 |
--enable-libssp --with-host-libstdcxx="-lstdc++ -lsupc++" \ |
| 333 |
--with-{gmp,mpfr,mpc,cloog,ppl}=<host dir> --enable-lto |
| 334 |
|
| 335 |
Explanation: |
| 336 |
--enable-version-specific-runtime-libs |
| 337 |
Installs libgcc/libstdc++ and other target support libraries in such a |
| 338 |
way that multiple GCC installs can coexist simultaneously. |
| 339 |
|
| 340 |
--enable-shared |
| 341 |
Builds shared support libraries |
| 342 |
|
| 343 |
--with-dwarf |
| 344 |
Use Dwarf debugging format by default |
| 345 |
|
| 346 |
--enable-fully-dynamic-string |
| 347 |
Enable dynamic std::string class to work around lazy initialization. |
| 348 |
|
| 349 |
--enable-libgomp |
| 350 |
Enable OpenMP support, it is not enabled by default on MinGW platforms. |
| 351 |
Requires pthreads-win32 (target) installed. |
| 352 |
|
| 353 |
--enable-libssp |
| 354 |
Enable Stack Smash Protection, a buffer overrun detector. |
| 355 |
|
| 356 |
--with-host-libstdcxx |
| 357 |
Lists down the C++ support libraries to link with. This is useful |
| 358 |
when using static PPL and CLooG. |
| 359 |
|
| 360 |
--with-{gmp,mpfr,mpc,cloog,ppl}=<host dir> |
| 361 |
Tells GCC where the host support libraries are installed to. |
| 362 |
(i.e. search <host dir>/include & <host dir>/lib) |
| 363 |
|
| 364 |
--enable-lto |
| 365 |
Enables Link Time Optimization support. |
| 366 |
|
| 367 |
Use "make all-gcc" to build the standalone GCC compiler without target |
| 368 |
support libs. Install it with "make install-gcc". |
| 369 |
|
| 370 |
Remember, this is a standalone gcc, it won't actually be able to link |
| 371 |
executables, but it is already suitable to compile the CRT. |
| 372 |
|
| 373 |
Do not delete the GCC build directory, we can continue later. |
| 374 |
|
| 375 |
=== Build the mingw-w64 CRT === [BMGWCRT] |
| 376 |
|
| 377 |
When you checkout the svn trunk from the mingw-w64 developer repository, there |
| 378 |
should be a directory called mingw-w64-crt. |
| 379 |
Example configure: |
| 380 |
../path/to/crt/configure --prefix=<prefix>/x86_64-w64-mingw32 --enable-lib32 \ |
| 381 |
--enable-lib64 --build=<build triplet> --host=x86_64-w64-mingw32 |
| 382 |
|
| 383 |
Explanation: |
| 384 |
--enable-lib32 |
| 385 |
If you enabled multilib support in x86_64-w64-mingw32-gcc, this will build |
| 386 |
32bit libs for use with win32 programming. For $host_cpu i686-w64-mingw32, |
| 387 |
it is enabled by default. |
| 388 |
|
| 389 |
--enable-lib64 |
| 390 |
Enable building 64bit libs for use with win64 programming. For $host_cpu |
| 391 |
x86_64-w64-mingw32, it is enabled by default. |
| 392 |
|
| 393 |
Once the configure ends successfully, run "make && make install". |
| 394 |
|
| 395 |
If you used symlinks or ntfs junction points to link the "lib*" directories, |
| 396 |
you should be fine. If you manually copied x86_64-w64-mingw32 to mingw, you |
| 397 |
must copy the "lib*" directories so that "lib64" mirrors "lib" and |
| 398 |
"lib32" in "mingw" mirrors "x86_64-w64-mingw32/lib32" |
| 399 |
|
| 400 |
=== Build libgcc === [BLIBGCC] |
| 401 |
|
| 402 |
Reenter the build directory used earlier in [BCORGCC], and issue |
| 403 |
"make all-target-libgcc", then "make install-target-libgcc". |
| 404 |
|
| 405 |
This will install libgcc. As of writing, there is a minor bug when |
| 406 |
--enable-version-specific-runtime-libs is used, libgcc_s.a is |
| 407 |
installed incorrectly. |
| 408 |
|
| 409 |
Move it from: |
| 410 |
<prefix>/lib/gcc/<arch>/lib32/libgcc_s.a to |
| 411 |
<prefix>/lib/gcc/<arch>/<version>/32/libgcc_s.a |
| 412 |
and |
| 413 |
<prefix>/lib/gcc/<arch>/lib64/libgcc_s.a to |
| 414 |
<prefix>/lib/gcc/<arch>/<version>/libgcc_s.a |
| 415 |
|
| 416 |
The libgcc dll is also clobbered during install, search the build |
| 417 |
directory for the dll and install it to separate directories as you |
| 418 |
see fit. Make sure 32bit apps only see the 32bit variant, same for |
| 419 |
64bit apps. |
| 420 |
|
| 421 |
=== Build pthreads === [BPTHR32] |
| 422 |
|
| 423 |
pthread-win32 is required for libgomp to work. Now that the CRT and |
| 424 |
libgcc is installed, we can proceed with building the pthread dll. |
| 425 |
|
| 426 |
Please refer to the pthreads-win32 section [PTHRW32] for instructions. |
| 427 |
Make sure to build for both win32 and win64 variants. |
| 428 |
|
| 429 |
=== Continue the build === [CTNTBLD] |
| 430 |
|
| 431 |
Reenter the build directory used earlier in [BCORGCC] and continue |
| 432 |
the build with "make". Finally, install with "make install". |
| 433 |
|