diff options
| author | Oskari Timperi <oskari.timperi@iki.fi> | 2022-11-02 08:28:14 +0200 |
|---|---|---|
| committer | Oskari Timperi <oskari.timperi@iki.fi> | 2022-11-02 08:28:14 +0200 |
| commit | 8d8e54e42e0d152f9854ba84ba03bb6a8f960e40 (patch) | |
| tree | e3a5cd12bd854107e7e0fdfefcb7ea6bcbed1092 | |
| download | mingw-main.tar.gz mingw-main.zip | |
| -rw-r--r-- | README.md | 76 | ||||
| -rw-r--r-- | build.sh | 158 |
2 files changed, 234 insertions, 0 deletions
diff --git a/README.md b/README.md new file mode 100644 index 0000000..da1b320 --- /dev/null +++ b/README.md @@ -0,0 +1,76 @@ +# A minimal MinGW distro + +## Downloads + +The latest version is [2021-08-17]($ROOT/files/mingw-2021-08-17.7z). + +You can find all downloads from [here]($ROOT/files). + +## Components + +- GCC 11.2.0 +- mingw-w64 9.0.0 +- binutils 2.36.1 +- gdb 10.2 + +## Installation + +Extract the archive to a location of your choosing and put the `bin` directory to your `PATH` + +## Updating + +Remove the contents of the old directory and extract the new version in its place. Don't extract on top of an older version without removing the contents of the old version. + +## Installing libraries + +I recommend using [vcpkg](https://github.com/microsoft/vcpkg) with this toolchain. + +A simple example how to install zlib: + +```powershell +> git clone https://github.com/microsoft/vcpkg.git +> cd vcpkg +> .\bootstrap-vcpkg.bat +> $env:Path += ";C:\Path\To\MinGW\bin" +> vcpkg install --triplet=x64-mingw-dynamic zlib +``` + +## Build requirements + +On Debian-based systems you can install the requirements with + +```sh +$ sudo apt install autoconf unzip texinfo wget build-essential +``` + +You do not need a cross-compiler, as the script will build its own. A native compiler is enough. + +## Building + +As said above, the script will first build a cross-compiler which is then used to build the actual resulting compiler. + +You can build the compiler on Linux or you can also use Windows Subsystem for Linux (WSL2 is what I use). + +To build, just execute the build script: + +```sh +$ ./build.sh +``` + +If you want to modify where the files are located, just modify the script. The variables used: + +- `X_ROOT` the root directory under which everything happens, by default `/tmp/mingw` +- `X_DOWNLOADS` where to cache downloads, by default `$X_ROOT/downloads` +- `X_BUILD` where to extract sources and create build directories, by default `$X_ROOT/build` +- `X_DEST` where to install the resulting compiler, by default `$X_ROOT/dest` +- `X_CROSS` where to install the cross-compiler, by default `$X_ROOT/cross` + +By default `make` is instructed to use all CPUs. You can change this by editing the `X_MAKE_OPTS` variable. + +## References + +Some inspiration is taken from Stephan T. Lavavej's MinGW distro (https://nuwen.net/mingw.html). + +crosstools-ng was used as learning point (https://crosstool-ng.github.io/). + +MinGW-w64 Wiki contains instructions how to build cross compiler and native compiler (https://sourceforge.net/p/mingw-w64/wiki2/Home). diff --git a/build.sh b/build.sh new file mode 100644 index 0000000..ee1df94 --- /dev/null +++ b/build.sh @@ -0,0 +1,158 @@ +#!/usr/bin/env bash + +set -ex + +X_ROOT=/tmp/mingw +X_DOWNLOADS=$X_ROOT/downloads +X_BUILD=$X_ROOT/build +X_DEST=$X_ROOT/dest +X_CROSS=$X_ROOT/cross +# Use all processors and collect output nicely +X_MAKE_OPTS="-j$(nproc) -O" +SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" + +# A helper function to setup common directory structure for each package. +# +# Source archives are downloaded to $X_DOWNLOADS. The archives are extracted +# to $X_BUILD/PKG_NAME so that sources are usually available at +# $X_BUILD/PKG_NAME/PKG_NAME-PKG_VERSION. A build directory is created at +# $X_BUILD/PKG_NAME/bld. +function prepare { + local PKG_NAME=$1 + local PKG_VERSION=$2 + local PKG_SOURCE=$3 + local PKG_FILENAME=$4 + rm -rf $X_BUILD/$PKG_NAME + mkdir -pv $X_DOWNLOADS $X_BUILD/$PKG_NAME + cd $X_DOWNLOADS + if [ ! -f $PKG_FILENAME ]; then + wget $PKG_SOURCE + fi + cd $X_BUILD/$PKG_NAME + tar xf $X_DOWNLOADS/$PKG_FILENAME + mkdir bld +} + +### BUILD THE CROSS COMPILER + +prepare binutils 2.36.1 https://ftpmirror.gnu.org/binutils/binutils-2.36.1.tar.xz binutils-2.36.1.tar.xz +cd bld +../binutils-2.36.1/configure --target=x86_64-w64-mingw32 --prefix=$X_CROSS --with-sysroot=$X_CROSS \ + --disable-multilib --disable-nls --disable-shared +make $X_MAKE_OPTS all "CFLAGS=-O3" "LDFLAGS=-s" +make $X_MAKE_OPTS install + +export PATH=$PATH:$X_CROSS/bin + +prepare mingw-w64 9.0.0 https://sourceforge.net/projects/mingw-w64/files/mingw-w64/mingw-w64-release/mingw-w64-v9.0.0.tar.bz2 mingw-w64-v9.0.0.tar.bz2 +cd bld +# We need to install headers first, so use --without-crt here. +../mingw-w64-v9.0.0/configure --host=x86_64-w64-mingw32 --prefix=$X_CROSS/x86_64-w64-mingw32 \ + --with-sysroot=$X_CROSS/x86_64-w64-mingw32 --disable-lib32 --enable-wildcard --disable-shared --without-crt +cd mingw-w64-headers +make $X_MAKE_OPTS install + +[ -e $X_CROSS/mingw ] || ln -sv $X_CROSS/x86_64-w64-mingw32 $X_CROSS/mingw +mkdir -pv $X_CROSS/x86_64-w64-mingw32/lib +[ -e $X_CROSS/x86_64-w64-mingw32/lib64 ] || ln -sv $X_CROSS/x86_64-w64-mingw32/lib $X_CROSS/x86_64-w64-mingw32/lib64 + +prepare gcc 11.2.0 https://ftpmirror.gnu.org/gcc/gcc-11.2.0/gcc-11.2.0.tar.xz gcc-11.2.0.tar.xz +(cd gcc-11.2.0 && ./contrib/download_prerequisites --directory=$X_DOWNLOADS) +cd bld +../gcc-11.2.0/configure --enable-languages=c,c++ --target=x86_64-w64-mingw32 --disable-multilib \ + --prefix=$X_CROSS --with-sysroot=$X_CROSS --disable-libstdcxx-pch --disable-libstdcxx-verbose \ + --disable-nls --disable-shared --disable-win32-registry --enable-threads=posix --enable-libgomp +make $X_MAKE_OPTS all-gcc +make $X_MAKE_OPTS install-gcc + +# Now we can build the rest of mingw-w64 (crt + winpthreads). +cd $X_BUILD/mingw-w64/bld +../mingw-w64-v9.0.0/configure --host=x86_64-w64-mingw32 --prefix=$X_CROSS/x86_64-w64-mingw32 \ + --with-sysroot=$X_CROSS/x86_64-w64-mingw32 --disable-lib32 --enable-wildcard \ + --disable-shared --with-libraries=winpthreads +make $X_MAKE_OPTS "CFLAGS=-s -O3" +make $X_MAKE_OPTS install + +cd $X_BUILD/gcc/bld +make $X_MAKE_OPTS "CFLAGS=-g0 -O3" "CXXFLAGS=-g0 -O3" \ + "CFLAGS_FOR_TARGET=-g0 -O3" "CXXFLAGS_FOR_TARGET=-g0 -O3" \ + "BOOT_CFLAGS=-g0 -O3" "BOOT_CXXFLAGS=-g0 -O3" +make $X_MAKE_OPTS install + +# These symlinks are not needed anymore. +rm $X_CROSS/mingw +rm $X_CROSS/x86_64-w64-mingw32/lib64 + +# ### BUILD THE NATIVE COMPILER + +prepare binutils 2.36.1 https://ftpmirror.gnu.org/binutils/binutils-2.36.1.tar.xz binutils-2.36.1.tar.xz +cd bld +../binutils-2.36.1/configure --host=x86_64-w64-mingw32 --target=x86_64-w64-mingw32 --prefix=$X_DEST \ + --with-sysroot=$X_DEST --disable-multilib --disable-nls --disable-shared +make $X_MAKE_OPTS all "CFLAGS=-O3" "LDFLAGS=-s" +make $X_MAKE_OPTS install + +prepare gcc 11.2.0 https://ftpmirror.gnu.org/gcc/gcc-11.2.0/gcc-11.2.0.tar.xz gcc-11.2.0.tar.xz +( + cd gcc-11.2.0 + ./contrib/download_prerequisites --directory=$X_DOWNLOADS + mkdir -p winsup/mingw + cd winsup/mingw + # Note that this needs to point to the cross compilers include directory + [ -e include ] || ln -s $X_CROSS/x86_64-w64-mingw32/include include +) + +prepare mingw-w64 9.0.0 https://sourceforge.net/projects/mingw-w64/files/mingw-w64/mingw-w64-release/mingw-w64-v9.0.0.tar.bz2 mingw-w64-v9.0.0.tar.bz2 +cd bld +../mingw-w64-v9.0.0/configure --host=x86_64-w64-mingw32 --build=x86_64-w64-mingw32 \ + --prefix=$X_DEST/x86_64-w64-mingw32 --with-sysroot=$X_DEST/x86_64-w64-mingw32 \ + --disable-lib32 --enable-wildcard --disable-shared --without-crt +cd mingw-w64-headers +make $X_MAKE_OPTS +make $X_MAKE_OPTS install + +[ -e $X_DEST/mingw ] || ln -s $X_DEST/x86_64-w64-mingw32 $X_DEST/mingw +[ -e $X_DEST/x86_64-w64-mingw32/lib64 ] || ln -s $X_DEST/x86_64-w64-mingw32/lib $X_DEST/x86_64-w64-mingw32/lib64 + +cd $X_BUILD/gcc/bld +../gcc-11.2.0/configure --enable-languages=c,c++ --host=x86_64-w64-mingw32 --target=x86_64-w64-mingw32 \ + --disable-multilib --prefix=$X_DEST --with-sysroot=$X_DEST --disable-libstdcxx-pch \ + --disable-libstdcxx-verbose --disable-nls --disable-shared --disable-win32-registry \ + --enable-threads=posix --enable-libgomp +make $X_MAKE_OPTS all-gcc +make $X_MAKE_OPTS install-gcc + +cd $X_BUILD/mingw-w64/bld +../mingw-w64-v9.0.0/configure --host=x86_64-w64-mingw32 --build=x86_64-w64-mingw32 \ + --prefix=$X_DEST/x86_64-w64-mingw32 --with-sysroot=$X_DEST/x86_64-w64-mingw32 \ + --disable-lib32 --enable-wildcard --disable-shared --with-libraries=winpthreads \ + --with-tools=all +make $X_MAKE_OPTS "CFLAGS=-s -O3" +make $X_MAKE_OPTS install + +cd $X_BUILD/gcc/bld +make $X_MAKE_OPTS clean +make $X_MAKE_OPTS "CFLAGS=-g0 -O3" "CXXFLAGS=-g0 -O3" "CFLAGS_FOR_TARGET=-g0 -O3" "CXXFLAGS_FOR_TARGET=-g0 -O3" "BOOT_CFLAGS=-g0 -O3" "BOOT_CXXFLAGS=-g0 -O3" +make $X_MAKE_OPTS install + +# These symlinks are not needed anymore. +rm -v $X_DEST/mingw +rm -v $X_DEST/x86_64-w64-mingw32/lib64 + +# https://github.com/msys2/MINGW-packages/issues/7890 +# https://github.com/msys2/MINGW-packages/pull/8248 +rm -v $X_DEST/lib/bfd-plugins/libdep.a + +### BUILD EXTRA STUFF + +prepare gdb 10.2 https://ftpmirror.gnu.org/gnu/gdb/gdb-10.2.tar.xz gdb-10.2.tar.xz +# Hack around undefined reference to BCryptGenRandom by defining HAVE_LIB_BCRYPT to 0. +cp -v $SCRIPT_DIR/gdb-gnulib-import-m4-getrandom.m4 gdb-10.2/gnulib/import/m4/getrandom.m4 +(cd gdb-10.2/gnulib && autoreconf --force --install --verbose) +cd bld +../gdb-10.2/configure --host=x86_64-w64-mingw32 --target=x86_64-w64-mingw32 --prefix=$X_DEST --disable-nls +make $X_MAKE_OPTS "CFLAGS=-O3" "CXXFLAGS=-O3" "LDFLAGS=-s" +make $X_MAKE_OPTS install + +cd $X_DEST +7zr a $X_ROOT/mingw.7z . |
