#!/bin/bash

function install_1_mingw-w64-headers() {
  mkdir $BUILD_DIR/build-headers
  cd $BUILD_DIR/build-headers
  $SRC_DIR/mingw-w64-$mingw_ver/mingw-w64-headers/configure --host=$HOST --prefix=$SYS_ROOT || exit 1
  make || exit 1
  make install || exit 1
}

function install_2_binutils() {
  mkdir $BUILD_DIR/build-binutils
  cd $BUILD_DIR/build-binutils
  $SRC_DIR/binutils-$binutils_ver/configure --target=$TARGET --prefix=$SYS_DIR --with-sysroot=$SYS_DIR --enable-multilib --enable-targets=x86_64-w64-mingw32,i686-w64-mingw32 || exit 1
  make || exit 1
  make install || exit 1
}

function install_4_gcc-core() {
  mkdir $BUILD_DIR/build-gcc
  cd $BUILD_DIR/build-gcc
  $SRC_DIR/gcc-$gcc_ver/configure --target=$TARGET --prefix=$SYS_DIR --with-sysroot=$SYS_DIR --enable-languages=c,c++ --enable-multilib --enable-targets=x86_64-w64-mingw32,i686-w64-mingw32 || exit 1
  make all-gcc || exit 1
  make install-gcc || exit 1
}

install_5_mingw-w64-crt() {
  mkdir $BUILD_DIR/build-crt
  cd $BUILD_DIR/build-crt
  $SRC_DIR/mingw-w64-$mingw_ver/mingw-w64-crt/configure --host=$HOST --prefix=$SYS_ROOT --with-sysroot=$SYS_ROOT --enable-lib64 --enable-lib32 || exit 1
  make || exit 1
  make install || exit 1
}

install_6_gcc() {
  cd $BUILD_DIR/build-gcc
  make || exit 1
  make install || exit 1
}

#==============================================================

HOST=x86_64-w64-mingw32
TARGET=x86_64-w64-mingw32
BASE="$( cd "$( dirname "$0" )" && pwd)"
SOURCE_DIR=$BASE/SOURCES
SRC_DIR=$BASE/SRC
BUILD_DIR=$BASE/BUILD
SYS_DIR=/usr/local/x86_64-w64-mingw32
SYS_ROOT=$SYS_DIR/$TARGET

mingw_ver=v9.0.0
binutils_ver=2.36.1
gcc_ver=9.4.0
gmp_ver=6.1.0
mpfr_ver=3.1.4
mpc_ver=1.0.3
isl_ver=0.18

rm -rf $BUILD_DIR
rm -rf $SRC_DIR
mkdir -p $BUILD_DIR
mkdir -p $SRC_DIR

sudo mkdir -p $SYS_DIR
sudo chmod 777 $SYS_DIR

export PATH=$SYS_DIR/bin:$PATH

tar -xf $SOURCE_DIR/mingw-w64-$mingw_ver.tar.bz2 -C $SRC_DIR
tar -xf $SOURCE_DIR/binutils-$binutils_ver.tar.xz -C $SRC_DIR
tar -xf $SOURCE_DIR/gcc-$gcc_ver.tar.xz -C $SRC_DIR
cp $SOURCE_DIR/gmp-$gmp_ver.tar.bz2 $SRC_DIR/gcc-$gcc_ver
cp $SOURCE_DIR/mpfr-$mpfr_ver.tar.bz2 $SRC_DIR/gcc-$gcc_ver
cp $SOURCE_DIR/mpc-$mpc_ver.tar.gz $SRC_DIR/gcc-$gcc_ver
cp $SOURCE_DIR/isl-$isl_ver.tar.bz2 $SRC_DIR/gcc-$gcc_ver
cd $SRC_DIR/gcc-$gcc_ver
./contrib/download_prerequisites
cd $BASE

# 1.mingw-w64-headers
install_1_mingw-w64-headers

# 2.binutils
install_2_binutils

# 3.symlink
sudo ln -s $SYS_ROOT $SYS_DIR/mingw

# 4.gcc-core
install_4_gcc-core

# 5.mingw-w64-crt
install_5_mingw-w64-crt

# 6.gcc
install_6_gcc

sudo chmod 755 $SYS_DIR
sudo chown root:root $SYS_DIR -R

以上可编译成功,但使用上编译老代码会出现问题。


新方案,使用系统自带mingw-w64,再升级header、crt部分。

$ sudo apt-get install mingw-w64

$ ../mingw-w64-headers/configure --host=x86_64-w64-mingw32 --prefix=/usr/x86_64-w64-mingw32
$ make
$ sudo make install

$ ../mingw-w64-crt/configure --host=x86_64-w64-mingw32 --prefix=/usr/x86_64-w64-mingw32/ --disable-multilib
$ make
$ sudo make install