Skip to content

Instantly share code, notes, and snippets.

@jeroen
Created February 23, 2015 04:35
Show Gist options
  • Save jeroen/e2ab0f4c019b0144649b to your computer and use it in GitHub Desktop.
Save jeroen/e2ab0f4c019b0144649b to your computer and use it in GitHub Desktop.
# This uses the Debian (or Ubuntu) cross compiler to a native gcc for win32 with multilib support
#
# Based on: http://sourceforge.net/p/mingw-w64/wiki2/Native%20Win64%20compiler/
#
# Cross compiling notes:
# - The minor version of gcc must match that of our cross compiler (4.9 in this case)
# - Important parameters: http://gcc.gnu.org/onlinedocs/gccint/Configure-Terms.html
# Prepare system
sudo apt-get update -y
sudo apt-get dist-upgrade -y
# Install cross compiler
sudo apt-get install -y make gcc texinfo binutils-mingw-w64-i686 mingw-w64-i686-dev gcc-mingw-w64-i686 g++-mingw-w64-i686 gfortran-mingw-w64-i686 mingw-w64 mingw-w64-i686-dev # gcc-mingw-w64-x86-64
# Setup dir structure
BUILDROOT=~/mingw
SRC=$BUILDROOT/sources
DEST=$BUILDROOT/dest
mkdir -p $SRC
mkdir -p $DEST
# Get sources
cd $SRC
wget http://ftp.gnu.org/gnu/binutils/binutils-2.25.tar.bz2
tar xjf binutils-2.25.tar.bz2
wget http://downloads.sourceforge.net/project/mingw-w64/mingw-w64/mingw-w64-release/mingw-w64-v3.1.0.tar.bz2
tar xjf mingw-w64-v3.1.0.tar.bz2
wget http://ftp.gnu.org/gnu/gcc/gcc-4.9.2/gcc-4.9.2.tar.bz2
tar xjf gcc-4.9.2.tar.bz2
# Make binutils
mkdir -p $BUILDROOT/binutils
cd $BUILDROOT/binutils
$SRC/binutils-2.25/configure --prefix=$DEST --with-sysroot=$DEST --host=i686-w64-mingw32 --target=i686-w64-mingw32 --enable-targets=i686-w64-mingw32,x86_64-w64-mingw32 #--enable-64-bit-bfd
make
make install
# Get gcc dependencies
cd $SRC/gcc-4.9.2
./contrib/download_prerequisites
# Create the 'winsup' dir
mkdir -p $SRC/gcc-4.9.2/gcc/winsup/mingw
ln -s /usr/i686-w64-mingw32/include $SRC/gcc-4.9.2/gcc/winsup/mingw/include
# Build mingw headers
# Assumes we are building with x86_64-w64-mingw32 cross compiler!
mkdir -p $BUILDROOT/headers
cd $BUILDROOT/headers
$SRC/mingw-w64-v3.1.0/mingw-w64-headers/configure --prefix=$DEST/i686-w64-mingw32 --host=i686-w64-mingw32 --build=i686-w64-mingw32
make
make install
# Symlink for gcc
ln -s $DEST/i686-w64-mingw32 $DEST/mingw
ln -s $DEST/i686-w64-mingw32/lib $DEST/i686-w64-mingw32/lib32
# Building GCC
# Not sure about --disable-shared
mkdir -p $BUILDROOT/gcc
cd $BUILDROOT/gcc
$SRC/gcc-4.9.2/configure --host=i686-w64-mingw32 --build=x86_64-w64-mingw32 --target=i686-w64-mingw32 \
--prefix=$DEST/i686-w64-mingw32 --with-sysroot=$DEST --enable-targets=all --disable-shared --enable-languages=c,c++,fortran \
CFLAGS="-I/usr/x86_64-w64-mingw32/include"
make all-gcc
make install-gcc
# Building CRT (Mingw-w64 itself)
mkdir -p $BUILDROOT/crt
cd $BUILDROOT/crt
$SRC/mingw-w64-v3.1.0/configure --host=i686-w64-mingw32 --prefix=$DEST/i686-w64-mingw32 --with-sysroot=$DEST --enable-lib32 --enable-lib64
make
make install
# Finishing gcc
cd $BUILDROOT/gcc
make
make install
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment