#!/bin/bash # # Copyright (C) 2017 Kyle Schwarz # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # v_script="4.0.1" v_binutils="2.29.1" v_gcc="7.3.0" v_gmp="6.1.2" v_mpfr="4.0.1" v_mpc="1.1.0" v_isl="0.18" show_help() { cat <. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. VERSION } error_exit() { local error_text="$1" shift if [[ -z "$error_text" ]]; then error_text="See 'build.log' for further details." fi echo "mingw-w64-build: error: $error_text" >&3 exit 1 } clean_build() { local build_dir="$1" rm -fr "$build_dir" mkdir -p "$build_dir" cd "$build_dir" || error_exit } download_sources() { cd "$src" || error_exit echo "downloading mingw-w64-git" >&3 git clone --depth 1 git://git.code.sf.net/p/mingw-w64/mingw-w64 "mingw-w64-git" || error_exit local urls=( "http://ftp.gnu.org/gnu/binutils/binutils-$v_binutils.tar.bz2" "http://ftp.gnu.org/gnu/gcc/gcc-$v_gcc/gcc-$v_gcc.tar.xz" "http://ftp.gnu.org/gnu/gmp/gmp-$v_gmp.tar.xz" "http://ftp.gnu.org/gnu/mpfr/mpfr-$v_mpfr.tar.xz" "http://ftp.gnu.org/gnu/mpc/mpc-$v_mpc.tar.gz" "http://isl.gforge.inria.fr/isl-$v_isl.tar.xz" ) for url in "${urls[@]}"; do local archive="${url##*/}" local c echo "downloading $archive" >&3 wget -nv "$url" || error_exit echo "extracting $archive" >&3 case "${archive##*.}" in gz) c=z ;; bz2) c=j ;; xz) c=J ;; esac tar -x${c}f "$archive" || error_exit done cd "$src/gcc-$v_gcc" || error_exit ln -s "../gmp-$v_gmp" "gmp" ln -s "../mpfr-$v_mpfr" "mpfr" ln -s "../mpc-$v_mpc" "mpc" ln -s "../isl-$v_isl" "isl" patch -p 1 <&3 "../../src/binutils-$v_binutils/configure" --prefix="$prefix" --disable-shared \ --enable-static --with-sysroot="$prefix" --target="$host" \ --disable-multilib --disable-nls || error_exit echo "building binutils" >&3 make -j $cpus || error_exit echo "installing binutils" >&3 make install || error_exit clean_build "$bld/mingw-w64" echo "configuring mingw-w64-headers" >&3 "../../src/mingw-w64-git/mingw-w64-headers/configure" --build="$build" \ --host="$host" --prefix="$prefix/$host" --enable-secure-api || error_exit echo "installing mingw-w64-headers" >&3 make install || error_exit cd "$prefix" || error_exit ln -s "./$host" "./mingw" || error_exit clean_build "$bld/gcc" echo "configuring gcc" >&3 "../../src/gcc-$v_gcc/configure" --target="$host" --disable-shared \ --enable-static --disable-multilib --prefix="$prefix" \ --enable-languages=c,c++ --disable-nls || error_exit echo "running 'make-gcc' for gcc" >&3 make -j $cpus all-gcc || error_exit echo "running 'install-gcc' for gcc" >&3 make install-gcc || error_exit clean_build "$bld/mingw-w64" echo "configuring mingw-w64-crt" >&3 "../../src/mingw-w64-git/mingw-w64-crt/configure" --build="$build" --host="$host" \ --prefix="$prefix/$host" --with-sysroot="$prefix/$host" echo "building mingw-w64-crt" >&3 make -j $cpus || error_exit echo "installing mingw-w64-crt" >&3 make install || error_exit cd "$bld/gcc" || error_exit echo "building gcc" >&3 make -j $cpus || error_exit echo "installing gcc" >&3 make install || error_exit return 0 } while :; do case $1 in -h|--help) show_help exit ;; --version) show_version exit ;; -?*) echo "mingw-w64-build: error: unknown option: '$1'" >&2 exit ;; *) break esac shift done if [[ "$PWD" = *" "* ]]; then echo "mingw-w64-build: error: working path contains spaces" >&2 exit 1 fi if [[ -z "$@" ]]; then echo "mingw-w64-build: error: missing ARCH option" >&2 echo "See 'mingw-w64-build --help' for build options." >&2 exit 1 fi for arch in $@; do if [[ "$arch" != "i686" ]] && [[ "$arch" != "x86_64" ]]; then echo "mingw-w64-build: error: invalid ARCH: '$arch'" >&2 exit 1 fi done progs=( "wget" "gzip" "bzip2" "xz" "git" "make" "patch" "g++" "tr" "tar" ) missing_progs="" for prog in "${progs[@]}"; do if ! command -v $prog >/dev/null; then missing_progs="$prog $missing_progs" fi done if [[ -n "$missing_progs" ]]; then echo "mingw-w64-build: missing required program(s): $missing_progs" >&2 exit 1 fi cpus=$(grep -c processor /proc/cpuinfo 2>/dev/null || sysctl -n hw.ncpu | tr -d "\n" 2>/dev/null) build=$(wget -qO - "http://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.guess;hb=HEAD" | sh) wd="$PWD" src="$wd/src" bld="$wd/bld" log="$wd/bld/build.log" rm -fr "$log" "$src" "$bld" mkdir "$bld" "$src" download_sources 3>&1 1>>"$log" 2>&1 for arch in $@; do build_toolchain "$arch" 3>&1 1>>"$log" 2>&1 done rm -fr "$bld" "$src" exit 0