Differences
This shows you the differences between two versions of the page.
Last revision | |||
— | blog:2017:0918_buillding_mingw64_compiler_on_linux [2020/07/10 12:11] – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Building Mingw64 compiler from sources on Linux ====== | ||
+ | |||
+ | {{tag> | ||
+ | |||
+ | Yes I know you can install a package on your distro to get the mingw64 compilers ready in a few minutes. But there is a major issue with this solution: you never quite know which version you will get! Or well, you know which version you get, but it might not be the one you would expect! | ||
+ | |||
+ | Actually this is just happening to me right now: I need to install the mingw64/32 compilers on an ubuntu machine... and this would be a build a very modern project with all fancy C++11/14 stuff: so I would really like to get the latest (or close to the latest) version of gcc and all. So I went to check the [[https:// | ||
+ | |||
+ | ====== ====== | ||
+ | |||
+ | ====== Old build instructions ====== | ||
+ | |||
+ | <note important> | ||
+ | |||
+ | Let's start with the beginning and find some decent initial build instructions. [[https:// | ||
+ | |||
+ | First we download the require source files: | ||
+ | * [[https:// | ||
+ | * [[https:// | ||
+ | |||
+ | Trying to use the src-7.2.0-release-rt_v6-rev0.tar.7z package: <sxh bash>$ src-7.2.0-release-rt_v6-rev0.tar.7z | ||
+ | $ tar xvf src-7.2.0-release-rt_v6-rev0.tar | ||
+ | $ cd src | ||
+ | </ | ||
+ | |||
+ | => This source folder contains a lot of different libraries: < | ||
+ | binutils-2.28 | ||
+ | bzip2-1.0.6 | ||
+ | expat-2.1.0 | ||
+ | |||
+ | ===== Building the binutils ===== | ||
+ | |||
+ | We create a build folder in the binutils folder, and then we build it: <sxh bash>cd binutils-2.28 | ||
+ | mkdir build | ||
+ | cd build | ||
+ | ../ | ||
+ | make | ||
+ | make install | ||
+ | |||
+ | cd ../ | ||
+ | cp -R mingw-w64-headers/ | ||
+ | cd / | ||
+ | ln -s x86_64-w64-mingw32 mingw</ | ||
+ | |||
+ | ===== Building the gcc core ===== | ||
+ | |||
+ | Building gcc core with: <sxh bash>cd / | ||
+ | mkdir build | ||
+ | cd build | ||
+ | ../ | ||
+ | </ | ||
+ | |||
+ | We get an error on this point: < | ||
+ | |||
+ | So let's try to build these first: <sxh bash>cd / | ||
+ | mkdir build | ||
+ | cd build | ||
+ | ../ | ||
+ | make | ||
+ | make install | ||
+ | |||
+ | cd ../ | ||
+ | mkdir build | ||
+ | cd build | ||
+ | export CFLAGS=-fPIC | ||
+ | export CXXFLAGS=-fPIC | ||
+ | ../ | ||
+ | make | ||
+ | make install | ||
+ | |||
+ | cd ../ | ||
+ | mkdir build | ||
+ | cd build | ||
+ | ../ | ||
+ | make | ||
+ | make install | ||
+ | </ | ||
+ | |||
+ | Getting a compilation error: < | ||
+ | / | ||
+ | |||
+ | => So now building with PIC as indicated above. But this doesn' | ||
+ | |||
+ | With those dependencies we can try to build the gcc core again: <sxh bash>cd / | ||
+ | mkdir build | ||
+ | cd build | ||
+ | ../ | ||
+ | make all-gcc | ||
+ | make install-gcc | ||
+ | </ | ||
+ | |||
+ | ===== Building the CRT ===== | ||
+ | |||
+ | Now building the C runtime library with: <sxh bash>cd / | ||
+ | ../ | ||
+ | </ | ||
+ | |||
+ | Of course we get an error when doing this: < | ||
+ | |||
+ | And running make distclean doesn' | ||
+ | Makefile: | ||
+ | </ | ||
+ | |||
+ | Instead trying to manually remove the **makefile** and **config.h** files: still **not working** | ||
+ | |||
+ | Time to check out additional mingw64 package ? <sxh bash>tar xvjf mingw-w64-v5.0.2.tar.bz2 | ||
+ | cd mingw-w64-v5.0.2/ | ||
+ | ../ | ||
+ | </ | ||
+ | |||
+ | Still the same error !!Grrr...!! Time to try a new perspective. | ||
+ | |||
+ | ====== New build instructions ====== | ||
+ | |||
+ | <note important> | ||
+ | |||
+ | Okay, so found updated instructions from this page: https:// | ||
+ | |||
+ | So let's restart everything. This time we will build from **/ | ||
+ | |||
+ | ===== Building the binutils ===== | ||
+ | |||
+ | <sxh bash>cd / | ||
+ | mkdir build | ||
+ | cd build | ||
+ | ../ | ||
+ | make | ||
+ | make install | ||
+ | cd .. | ||
+ | rm -Rf build</ | ||
+ | |||
+ | Add our new bin folder to our path (from our master **profile.sh** file): <sxh bash> | ||
+ | |||
+ | ===== Install the mingw-w64 headers ===== | ||
+ | |||
+ | **Note**: In the command below it is very important to specify the **x86_64-w64-mingw32** ending of the prefix location: | ||
+ | |||
+ | <sxh bash>cd / | ||
+ | mkdir build | ||
+ | cd build | ||
+ | ../ | ||
+ | make install | ||
+ | cd / | ||
+ | ln -s x86_64-w64-mingw32 mingw | ||
+ | cd x86_64-w64-mingw32 | ||
+ | ln -s lib lib64</ | ||
+ | |||
+ | ===== Building the gcc cross-compiler ===== | ||
+ | |||
+ | <sxh bash> | ||
+ | cd / | ||
+ | mkdir build | ||
+ | cd build | ||
+ | ../ | ||
+ | make | ||
+ | make install | ||
+ | cd .. | ||
+ | rm -Rf build | ||
+ | cd ../ | ||
+ | mkdir build | ||
+ | cd build | ||
+ | ../ | ||
+ | make | ||
+ | make install | ||
+ | cd .. | ||
+ | rm -Rf build | ||
+ | cd ../ | ||
+ | mkdir build | ||
+ | cd build | ||
+ | ../ | ||
+ | make | ||
+ | make install | ||
+ | cd .. | ||
+ | rm -Rf build | ||
+ | cd ../ | ||
+ | mkdir build | ||
+ | cd build | ||
+ | ../ | ||
+ | make all-gcc | ||
+ | make install-gcc | ||
+ | </ | ||
+ | |||
+ | At this point we keep the gcc build folder as we will come back to it later. | ||
+ | |||
+ | ===== Building the CRT ===== | ||
+ | |||
+ | <sxh bash>cd / | ||
+ | mkdir build | ||
+ | cd build | ||
+ | ../ | ||
+ | make | ||
+ | </ | ||
+ | |||
+ | Great... Now we have a segmentation fault in our new compiler freshly installed: < | ||
+ | echo " | ||
+ | make all-am | ||
+ | make[1]: Entering directory `/ | ||
+ | x86_64-w64-mingw32-gcc -DHAVE_CONFIG_H -I. -I.. -m32 -I../ | ||
+ | In file included from / | ||
+ | from / | ||
+ | from / | ||
+ | from ../ | ||
+ | / | ||
+ | / | ||
+ | __A = _mm512_mask_mov_ps (_mm512_set1_ps (1.0f), __U, __A); | ||
+ | ^~~ | ||
+ | 0xa497ef crash_signal | ||
+ | ../ | ||
+ | Please submit a full bug report, | ||
+ | with preprocessed source if appropriate. | ||
+ | Please include the complete backtrace with any bug report. | ||
+ | See < | ||
+ | make[1]: *** [intrincs/ | ||
+ | make[1]: Leaving directory `/ | ||
+ | |||
+ | => Note: we did not disable multilib in GCC core build process above, trying again with that: **same error**. Seriously ?... | ||
+ | |||
+ | ====== Third trial ====== | ||
+ | |||
+ | Now also taking into account the info from: https:// | ||
+ | # arg1: the name of the library to build | ||
+ | # arg2: the opts as a string to pass to the configure command | ||
+ | # arg3: an optional target specialization flag: if this is " | ||
+ | nv_std_build() | ||
+ | { | ||
+ | local cpus=$(grep -c processor / | ||
+ | local lname=" | ||
+ | local cfg=" | ||
+ | local tgt=" | ||
+ | local maketgt="" | ||
+ | local insttgt=" | ||
+ | if [ " | ||
+ | maketgt=" | ||
+ | insttgt=" | ||
+ | fi | ||
+ | |||
+ | cd " | ||
+ | mkdir build | ||
+ | cd build | ||
+ | ../ | ||
+ | make -j $cpus $maketgt || { echo " | ||
+ | make $insttgt || { echo " | ||
+ | cd ../ | ||
+ | if [ " | ||
+ | rm -Rf build | ||
+ | fi | ||
+ | cd ../ | ||
+ | } | ||
+ | |||
+ | # Helper method used to build a compiler from sources: | ||
+ | nv_build_mingw_compiler() | ||
+ | { | ||
+ | local arch=" | ||
+ | |||
+ | local sdir="/ | ||
+ | local bdir="/ | ||
+ | local idir="/ | ||
+ | | ||
+ | local tgt=" | ||
+ | if [ " | ||
+ | tgt=" | ||
+ | fi | ||
+ | |||
+ | local host=" | ||
+ | local build=" | ||
+ | |||
+ | local srcfile=" | ||
+ | local __binutils=" | ||
+ | local __gmp=" | ||
+ | local __headers=" | ||
+ | local __crt=" | ||
+ | local __mpfr=" | ||
+ | local __mpc=" | ||
+ | local __isl=" | ||
+ | local __gcc=" | ||
+ | |||
+ | local cpus=$(grep -c processor / | ||
+ | echo "Using $cpus CPUs" | ||
+ | |||
+ | # Remove previous stuff: | ||
+ | echo " | ||
+ | rm -Rf " | ||
+ | |||
+ | echo " | ||
+ | rm -Rf " | ||
+ | |||
+ | echo " | ||
+ | mkdir -p " | ||
+ | cd " | ||
+ | 7za x " | ||
+ | |||
+ | # Setup the directories: | ||
+ | local srcdir=" | ||
+ | | ||
+ | cd " | ||
+ | |||
+ | # Build the binutils: | ||
+ | nv_std_build $__binutils " | ||
+ | |||
+ | # Ensure our path contains our new compiler location: | ||
+ | if [[ `echo $PATH | grep $idir/bin` == "" | ||
+ | echo " | ||
+ | export PATH=" | ||
+ | fi | ||
+ | |||
+ | # Install the headers: | ||
+ | nv_std_build $__headers " | ||
+ | |||
+ | # Create the symlinks: | ||
+ | cd $idir | ||
+ | ln -s $tgt mingw | ||
+ | |||
+ | cd " | ||
+ | |||
+ | # Move dependencies into GCC folder: | ||
+ | mv $__gmp $__gcc/gmp | ||
+ | mv $__mpfr $__gcc/mpfr | ||
+ | mv $__mpc $__gcc/mpc | ||
+ | mv $__isl $__gcc/isl | ||
+ | |||
+ | # Build GCC, pass 1 | ||
+ | nv_std_build $__gcc " | ||
+ | |||
+ | # Build the crt | ||
+ | nv_std_build $__crt " | ||
+ | |||
+ | # Finish building GCC: | ||
+ | cd " | ||
+ | make || { echo " | ||
+ | make install || { echo " | ||
+ | cd .. | ||
+ | rm -Rf build | ||
+ | | ||
+ | echo "=> Compiler build SUCCESSFUL." | ||
+ | } | ||
+ | |||
+ | nv_build_mingw64_compiler() | ||
+ | { | ||
+ | nv_build_mingw_compiler 64 | ||
+ | } | ||
+ | |||
+ | nv_build_mingw32_compiler() | ||
+ | { | ||
+ | nv_build_mingw_compiler 32 | ||
+ | }</ | ||
+ | |||
+ | |||
+ | **Note**: we can figure out our current machine triplet with: <sxh bash>$ gcc -dumpmachine | ||
+ | x86_64-linux-gnu</ | ||
+ | |||
+ | Got a configuration error for MPFR 3.1.4: < | ||
+ | * According to [[https:// | ||
+ | * => Nope, C++ compiler is already installed. So checking the config.log... From there we have: < | ||
+ | In file included from / | ||
+ | from / | ||
+ | from conftest.c: | ||
+ | / | ||
+ | # | ||
+ | ^ | ||
+ | </ | ||
+ | * So we have to specify the proper target value when building MPFR ? => **No**: this will generate another error: " | ||
+ | |||
+ | Found additional indications on [[http:// | ||
+ | |||
+ | Got an error in GCC build pass 2: < | ||
+ | * Trying to build gcc without **--with-system-zlib**: | ||
+ | * Checking the config.log file: **build/ | ||
+ | * Checking this build script: https:// | ||
+ | * Using the previous script directly: **Working** !! | ||
+ | * => So we need to figure out what we are doing wrong in our script... | ||
+ | * Integrated the Zeranoe script in our library: **still working** but the outputs pipes need fixing: **fixed**. | ||
+ | |||
+ | => So, on the whole, still no clear idea why our script didn't work... but maybe we should try to download the same sources as the one used in the zeranoe script! => **And this works!** so it's really the source package provided on the mingw64 sourforge download page which was to blame here. My script above is now working when using the same sources as Zeranoe! | ||
+ | |||
+ | Also tried to build the compiler for the i686 target: **OK! build successful** :-). | ||
+ | |||
+ | ====== Conclusion ====== | ||
+ | |||
+ | Well, this experiment was a lot more painful than I anticipated... I thought that building mingw compilers would be something at least somewhat appropriately documented. But **it is not**. Seriously, I think someone from the developer team should take the time to **clean** the various wiki pages and pseudo explanations on how to build from sources, to only keep an updated version that should be easy enough to find on the web... | ||
+ | |||
+ | But anyway, in the end the only working option was the one I found on the [[https:// | ||
+ | |||
+ | ====== Selecting threading model ====== | ||
+ | |||
+ | Okay, this is all very good until you then try to build a project with those compilers where you try to use for instance the **std:: | ||
+ | * cf. https:// | ||
+ | |||
+ | < | ||
+ | |||
+ | >" | ||
+ | => As most of the time, all you get is a collection of stupid ideas, useless or unrelated discussions and partial explanations going in all directions, and then of course you are the one who spend the complete day just trying to figure out where the world is going...</ | ||
+ | |||
+ | Finally found this [[https:// | ||
+ | * Found from that project that the threading model should be passed to gcc configuration command: <sxh bash> | ||
+ | * Also reading [[https:// | ||
+ | |||
+ | => So rebuilding our compilers with **posix thread model** this time with threads and exception specifications: | ||
+ | |||
+ | But of course when trying just that you get an error: < | ||
+ | from ../ | ||
+ | ./ | ||
+ | # | ||
+ | ^~~~~~~~~~~ | ||
+ | compilation terminated. | ||
+ | make[2]: *** [unwind-sjlj.o] Error 1</ | ||
+ | |||
+ | We need to provide this pthread library somehow obviously... | ||
+ | * According to [[http:// | ||
+ | * => **OK Now** :-)! We just needed to install the winpthreads library in the appropriate folder! Now the compilers are building again. | ||
+ | * Testing with **i686** compiler: **OK** | ||
+ | * Testing build a project library using std::mutex with the new win64 compiler: **Working!** | ||
+ | * Testing build a project library using std::mutex with the new win32 compiler: **Working!** | ||
+ | |||
+ | |||
+ | ~~DISCUSSION~~ | ||