Skip to content

Instantly share code, notes, and snippets.

@minnyres
Last active May 7, 2022 14:46
Show Gist options
  • Save minnyres/017aae8d11de582465427098be81828a to your computer and use it in GitHub Desktop.
Save minnyres/017aae8d11de582465427098be81828a to your computer and use it in GitHub Desktop.
Bash scripts to build MinGW-w64 cross toolchains
#!/bin/bash
set -e
help_msg="Usage: ./gcc-mingw.sh -arch=[x86|x64]"
if [ $# == 1 ]; then
if [ $1 == "-arch=x86" ]; then
arch=x86
elif [ $1 == "-arch=x64" ]; then
arch=x64
else
echo $help_msg
exit -1
fi
else
echo $help_msg
exit -1
fi
if [ $arch == "x86" ]; then
TARGET=i686-w64-mingw32
BUILDDIR=$PWD/mingw32
mingw_lib32=yes
mingw_lib64=no
else
TARGET=x86_64-w64-mingw32
BUILDDIR=$PWD/mingw64
mingw_lib32=no
mingw_lib64=yes
fi
GCC_VERSION=12.1.0
MINGW_VERSION=10.0.0
BINUTILS_VERSION=2.38
mkdir $BUILDDIR/tmp -p
cd $BUILDDIR/tmp
wget https://ftpmirror.gnu.org/gnu/gcc/gcc-$GCC_VERSION/gcc-$GCC_VERSION.tar.xz
wget https://ftpmirror.gnu.org/gnu/binutils/binutils-$BINUTILS_VERSION.tar.xz
wget https://jaist.dl.sourceforge.net/project/mingw-w64/mingw-w64/mingw-w64-release/mingw-w64-v$MINGW_VERSION.tar.bz2
# binutils
cd $BUILDDIR/tmp
tar -xf binutils-$BINUTILS_VERSION.tar.xz
cd binutils-$BINUTILS_VERSION
./configure CFLAGS="-g0 -Os" LDFLAGS="-s" --prefix=$BUILDDIR --target=$TARGET --with-sysroot=$BUILDDIR --disable-multilib
make -j$(nproc)
make install
# mingw-w64 headers
cd $BUILDDIR/tmp
tar -xf mingw-w64-v$MINGW_VERSION.tar.bz2
cd mingw-w64-v$MINGW_VERSION/mingw-w64-headers
./configure --prefix=$BUILDDIR/$TARGET --host=$TARGET --enable-sdk=all --enable-idl --enable-secure-api --with-default-msvcrt=msvcrt --with-default-win32-winnt=0x0600
make all
make install
# gcc-core
cd $BUILDDIR/tmp
tar -xf gcc-$GCC_VERSION.tar.xz
cd gcc-$GCC_VERSION
./contrib/download_prerequisites
mkdir build
cd build
../configure CFLAGS="-g0 -Os" CXXFLAGS="-g0 -Os" CFLAGS_FOR_TARGET="-g0 -Os" \
CXXFLAGS_FOR_TARGET="-g0 -Os" BOOT_CFLAGS="-g0 -Os" BOOT_CXXFLAGS="-g0 -Os" \
LDFLAGS="-s" LDFLAGS_FOR_TARGET="-s" BOOT_LDFLAGS="-s" \
--prefix=$BUILDDIR --target=$TARGET --with-sysroot=$BUILDDIR \
--disable-multilib --disable-shared --enable-languages=c,c++ \
--enable-threads=posix --disable-win32-registry --enable-version-specific-runtime-libs \
--enable-fully-dynamic-string --enable-libgomp --enable-libssp --enable-lto \
--disable-libstdcxx-pch --disable-libstdcxx-verbose
ln -s $TARGET $BUILDDIR/mingw
make -j$(nproc) all-gcc
make install-gcc
PATH=$BUILDDIR/bin:$PATH
# mingw-w64 crt
mkdir -p $BUILDDIR/$TARGET/lib
ln -s lib $BUILDDIR/$TARGET/lib64
cd $BUILDDIR/tmp/mingw-w64-v$MINGW_VERSION/mingw-w64-crt/
./configure CFLAGS="-g0 -Os" LDFLAGS="-s" --prefix=$BUILDDIR/$TARGET --host=$TARGET --enable-lib64=$mingw_lib64 --enable-lib32=$mingw_lib32 --with-default-msvcrt=msvcrt --with-default-win32-winnt=0x0600
make
make install
# winpthreads
cd $BUILDDIR/tmp/mingw-w64-v$MINGW_VERSION/mingw-w64-libraries/winpthreads/
./configure CFLAGS="-g0 -Os" LDFLAGS="-s" --prefix=$BUILDDIR/$TARGET --host=$TARGET --enable-shared=no --enable-static
make -j$(nproc)
make install
# gcc libs
cd $BUILDDIR/tmp/gcc-$GCC_VERSION/build
make -j$(nproc)
make install
# clean
cd $BUILDDIR/
rm -rf $BUILDDIR/tmp/
rm $BUILDDIR/mingw
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment