1The Advance Guide to 2 Building the Mingw-w64 x86_64-w64-mingw32 cross-compiler 3 4== About this document == 5 6This document describes more advanced methods in building the mingw-w64 7cross compiler. It also includes details about the toolchain 8dependencies and relevant options that can be passed to the GCC 9configure. 10 11The commands are run under a MSYS/MinGW setup unless specified otherwise. 12Using Cygwin or any other POSIX compatible shell should work. 13 14== Targeted audience == 15 16This document assumes readers are familiar with the UNIX Command Line 17and building the GNU toolchain in general, but would want a more fine 18grained control over the capabilities of the cross compiler. 19 20For a basic guide on building the mingw-w64 toolchain, please refer to 21"mingw-w64-howto-build.txt". 22 23== Notes about LTO == 24 25LTO for COFF targets can be enabled without installing libelf, as of gcc-4.6.0. 26 27== Version changes == 28 29Date / Version / Author 302009-10-09 1.0 Jonathan Yong <jon_y[a]users.sourceforge.com> 312010-01-03 1.1 Jonathan Yong <jon_y[a]users.sourceforge.com> 322010-05-18 1.11 Jonathan Yong <jon_y[a]users.sourceforge.com> 332010-08-31 1.12 Jonathan Yong <jon_y[a]users.sourceforge.com> 342013-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 59You can search the keys (i.e. [BMPCLIB]) to jump to that section. 60 61== Some Basic Info about MinGW-w64 == [SBSCINF] 62 63MinGW-w64 began as a fork of mingw.org MinGW to support 64bit windows as 64a target. 65 66Canonical 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 75These libraries are used by GCC on the "host" side. It means that these 76should be built for the system that will run the newly built GCC. 77 78=== The GNU MP Bignum Library (GMP) === [BGMPLIB] 79 80Homepage: <http://gmplib.org/> 81Depends on: None 82Depended by: MPC, MPFR, PPL, CLooG 83GCC dependency type: Hard (GCC won't build without it) 84 85GMP is primarily written in C and assembly, but it comes with C++ bindings. 86The asm part is selected by the $host_cpu detected by configure. If your 87platform is not detected properly, you can use --host=none-none-none to 88disable asm optimizations. Be sure to set the correct CC, CXX, NM, RANLIB 89and AR variables. 90 91The C++ bindings are not built by default, but it is required by PPL. To fix 92this, use "--enable-cxx" and "CPPFLAGS=-fexceptions". PPL needs the 93-fexceptions part to throw exceptions properly, but it is not strictly 94required. 95 96On Windows hosts, only static or shared builds can be used at a time, but not 97both. This is reasoned by the incompatible header set used by static/shared 98builds. If you do use static builds, you should build any libs that depend on 99it as static to prevent accidentally exporting GMP symbols in other dlls. 100 101=== MPFR === [BMPFRLB] 102 103Homepage: <http://www.mpfr.org/> 104Depends on: GMP 105Depended by: None 106GCC dependency type: Hard (GCC won't build without it) 107 108MPFR isn't particularly fussy about configure options, so the usual: 109 ./configure 110 make 111 make install 112is fine. 113 114=== Multiprecision (MPC) === [BMPCLIB] 115 116Homepage: <http://www.multiprecision.org/> 117Depends on: GMP 118Depended by: None 119GCC dependency type: Hard (GCC won't build without it) 120 121MPC isn't particularly fussy about configure options, so the usual: 122 ./configure 123 make 124 make install 125is fine. 126 127=== The Parma Polyhedra Library (PPL) === [BPPLLIB] 128 129Homepage: <http://www.cs.unipr.it/ppl/> 130Depends on: GMP 131Depended by: ClooG 132GCC dependency type: None (GCC does not use it directly) 133 134PPL 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 139Homepage: <http://repo.or.cz/w/cloog-ppl.git> 140Depends on: GMP, PPL 141Depended by: None 142GCC dependency type: Soft (GCC can build without it) 143If you are planning to use CLooG with static PPL, remeber to add 144'--with-host-libstdcxx="-lstdc++ -lsupc++"' to configure. 145CLooG 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 152Target support libraries are for use with mingw-w64 itself. It should not 153be used by the host directly. Hence the target libs will need to be compiled 154with a compiler targeting mingw-w64. 155 156=== pthreads-win32 === [PTHRW32] 157Homepage: <http://sourceware.org/pthreads-win32/> 158Depends on: None 159Depended by: None 160GCC dependency type: Soft (Required for GCC libgomp support) 161 162Download instructions: 163 cvs -d :pserver:anoncvs@sourceware.org:/cvs/pthreads-win32 login 164 {enter ``anoncvs'' for the password} 165 cvs -d :pserver:anoncvs@sourceware.org:/cvs/pthreads-win32 checkout pthreads 166 167Patch: <http://sourceware.org/ml/pthreads-win32/2009/msg00030/w64sup.patch> 168 169Note: pthreads-win32 does not come with a autotools based build system. It 170 uses a Makefile "GNUmakefile" to build the pthreads dll. 171 172Note: To allow win64 support, apply the patch from the mailing list. 173 174General 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 20432bit build instructions (assuming mutilib 64bit default GCC) 205 * Edit the GNUmakefile, and change: 206======================================================================= 207AR = $(CROSS)ar 208DLLTOOL = $(CROSS)dlltool 209CC = $(CROSS)gcc 210CXX = $(CROSS)g++ 211RANLIB = $(CROSS)ranlib 212RC = $(CROSS)windres 213======================================================================= 214to 215======================================================================= 216AR = $(CROSS)ar 217DLLTOOL = $(CROSS)dlltool -m i386 218CC = $(CROSS)gcc -m32 219CXX = $(CROSS)g++ -m32 220RANLIB = $(CROSS)ranlib 221RC = $(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 230Its a lazy method because it involves building GCC with all enabled 231front ends only once, without restarting the cross GCC build. 232 233First of all, your host compiler (native system "gcc") must support ADA. 234If your platform does not have GNAT binaries, use the binaries from: 235 <https://libre.adacore.com/libre/download/> 236 237If you do not want ADA support, or confident about your host ADA support, 238you can skip the ADA bootstrap phase. 239 240=== ADA Bootstrap Phase === [ADABOOT] 241 242Bootstrapping ADA is needed if ADA is to be supported by the mingw-w64 243cross GCC. 244 245To begin bootstrap ADA, you must grab the GCC source that will be used 246for the mingw-w64 cross GCC, so that the ADA support is as close as 247possible with the target GCC version. 248 249Use the source to build a native compiler for your host machine. You must 250enable at least C, ADA, and C++ support. It is advisable to prefix 251(i.e. using --prefix=/opt/test) the entire native bootstrap toolchain 252in order to avoid overwriting the compiler provided by your host system. 253You should also build a prefixed native Binutils for use with the new 254bootstrap compiler. 255 256Remember to build GCC OUTSIDE its source directory, NOT inside it and 257certainly NOT in a sub-directory. 258 259=== MinGW-w64 cross Binutils === [CRSBINU] 260 261The official method to build the mingw-w64 toolchain is to set --prefix 262and --with-sysroot to the same directory to allow the toolchain to be 263relocatable. To make Multilib support possible, you need to use 264"--enable-targets=x86_64-w64-mingw32,i686-w64-mingw32". 265 266An 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 274When you checkout the svn trunk from the mingw-w64 developer repository, there 275should 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=none --build=<host triplet> --host=x86_64-w64-mingw32 278 make install 279 280To view the available sdks, use --help. The prefix should be similar to the 281prefix used for cross Binutils. Although the mingw-w64 cross GCC is not 282installed yet, configure will not fail it as it only checks the --host option. 283It is important to set the --host option correctly, failing to do so will 284cause the cross GCC to fail to find the expected system headers. 285 286=== Setup Symlinks === [SETSYML] 287 288Your install root for the mingw-w64 cross toolchain should contain the 289following directories. Create the directories if missing. It is easier 290to 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 303On Windows, you can use "ntfs link" <http://elsdoerfer.name/=ntfslink> 304to create "junction points" to link directories. These junction points 305are transparent to user mode applications such as GCC. 306 307Make sure the "mingw" directory mirrors "x86_64-w64-mingw32" exactly. Ditto 308for "lib64" and "lib" 309 310=== Build Core GCC === [BCORGCC] 311 312GCC can be configured in many ways, the following is an example that worked. 313Make sure to add your cross Binutils binaries to your $PATH before continuing. 314 315If you did use the ADA compiler from adacore to produce a bootstrap ADA 316compiler, make sure to add the prefix in such a way that it is found 317before your default host compiler. 318(i.e. export PATH=/opt/adaboot/bin:$PATH:/mingw64/path/bin) 319 320Some of the configure options do not sound possible with a stage-1 cross, 321but be assured that it has been tested. 322 323Remember to build GCC OUTSIDE its source directory, NOT inside it and certainly 324NOT in a sub-directory. 325 326Example: 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 335Explanation: 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 367Use "make all-gcc" to build the standalone GCC compiler without target 368support libs. Install it with "make install-gcc". 369 370Remember, this is a standalone gcc, it won't actually be able to link 371executables, but it is already suitable to compile the CRT. 372 373Do not delete the GCC build directory, we can continue later. 374 375=== Build the mingw-w64 CRT === [BMGWCRT] 376 377When you checkout the svn trunk from the mingw-w64 developer repository, there 378should be a directory called mingw-w64-crt. 379Example 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 383Explanation: 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 393Once the configure ends successfully, run "make && make install". 394 395If you used symlinks or ntfs junction points to link the "lib*" directories, 396you should be fine. If you manually copied x86_64-w64-mingw32 to mingw, you 397must 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 402Reenter the build directory used earlier in [BCORGCC], and issue 403"make all-target-libgcc", then "make install-target-libgcc". 404 405This will install libgcc. As of writing, there is a minor bug when 406--enable-version-specific-runtime-libs is used, libgcc_s.a is 407installed incorrectly. 408 409Move it from: 410<prefix>/lib/gcc/<arch>/lib32/libgcc_s.a to 411<prefix>/lib/gcc/<arch>/<version>/32/libgcc_s.a 412and 413<prefix>/lib/gcc/<arch>/lib64/libgcc_s.a to 414<prefix>/lib/gcc/<arch>/<version>/libgcc_s.a 415 416The libgcc dll is also clobbered during install, search the build 417directory for the dll and install it to separate directories as you 418see fit. Make sure 32bit apps only see the 32bit variant, same for 41964bit apps. 420 421=== Build pthreads === [BPTHR32] 422 423pthread-win32 is required for libgomp to work. Now that the CRT and 424libgcc is installed, we can proceed with building the pthread dll. 425 426Please refer to the pthreads-win32 section [PTHRW32] for instructions. 427Make sure to build for both win32 and win64 variants. 428 429=== Continue the build === [CTNTBLD] 430 431Reenter the build directory used earlier in [BCORGCC] and continue 432the build with "make". Finally, install with "make install". 433 434