summaryrefslogtreecommitdiffstats
path: root/scripts/mingw-w64-build
blob: b585af2b326cf249b5a2eb9dc70c71e23afe7a77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
#!/bin/bash
# mingw-w64-build - download and build mingw-w64 toolchain
#
# Copyright 2018 Bradley Sepos
# Released under the MIT License. See LICENSE for details.
# https://github.com/bradleysepos/mingw-w64-build

# checks for required external tools
function check_dependencies {  # check_dependencies $DEP1 $DEP2 ...
    local DEPS ERRORS
    DEPS=("${@}");
    ERRORS=()
    for DEP in ${DEPS[@]}; do
        if echo "${DEP}" | grep '/' >/dev/null 2>&1 && [[ ! -x "${DEP}" ]]; then
            ERRORS+=("${DEP}")
        elif ! hash "${DEP}" >/dev/null 2>&1; then
            ERRORS+=("${DEP}")
        fi
    done
    if [[ "${#ERRORS[@]}" -ne 0 ]]; then
        echo "dependencies: ${DEPS[@]}"
        echo "unable to find command(s): ${ERRORS[*]}" >&2
        return 1
    fi
}

# downloads from one or more urls
function download_url {  # download_url $VERBOSE $FILE $URLS
    local VERBOSE FILE URLS I FAILED
    OPTIND=1
    VERBOSE="${1}"
    FILE="${2}"
    shift 2
    URLS=("${@}")
    if [[ "${#URLS[@]}" -eq 0 ]] || [[ "${URLS[0]:-}" == "" ]]; then
        echo "url not specified for download" >&2
        return 1
    fi
    if [[ "${FILE:-}" == "" ]]; then
        echo "output path not specified for download: ${FILE}" >&2
        return 1
    fi
    FAILED=()
    for I in "${!URLS[@]}"; do
        if ! curl --head -Lf --connect-timeout 30 "${URLS[I]}" >/dev/null 2>&1; then
            FAILED+=("${URLS[I]}")
            if [[ "$(( ${I} + 1 ))" -lt "${#URLS[@]}" ]]; then
                continue
            else
                echo "unable to download from urls: ${FAILED[@]}" >&2
                echo "unable to download to file: ${FILE}" >&2
                return 1
            fi
        fi
        if ! touch "${FILE}" >/dev/null 2>&1; then
            echo "unable to create path: ${FILE}" >&2
            return 1
        fi
        if [[ "${VERBOSE:-}" == true ]]; then
            echo "curl -Lf --connect-timeout 30 \"${URLS[I]}\" -o \"${FILE}\""
        fi
        if ! curl -Lf --connect-timeout 30 "${URLS[I]}" -o "${FILE}"; then
            FAILED+=("${URLS[I]}")
            if [[ "$(( ${I} + 1 ))" -lt "${#URLS[@]}" ]]; then
                continue
            else
                echo "unable to download from urls: ${FAILED[@]}" >&2
                echo "unable to download to file: ${FILE}" >&2
                return 1
            fi
        fi
    done
}

# builds mingw-w64
function mingw-w64-build {  # mingw-w64-build $TARGET_PARAM $TARGET_DIR
    set -o pipefail

    # dependencies
    local DEPS
    DEPS=("bison" "bzip2" "curl" "flex" "g++" "gcc" "gunzip" "m4" "make" "pax" "yasm")
    check_dependencies "${DEPS[@]}" || return 1

    # package names
    local CONFIG_NAME BINUTILS_NAME MINGW_W64_NAME GMP_NAME MPFR_NAME MPC_NAME ISL_NAME GCC_NAME NAMES
    CONFIG_NAME="config"
    BINUTILS_NAME="binutils"
    MINGW_W64_NAME="mingw-w64"
    GMP_NAME="gmp"
    MPFR_NAME="mpfr"
    MPC_NAME="mpc"
    ISL_NAME="isl"
    GCC_NAME="gcc"
    NAMES=("${CONFIG_NAME}" "${BINUTILS_NAME}" "${MINGW_W64_NAME}" "${GMP_NAME}" "${MPFR_NAME}" "${MPC_NAME}" "${ISL_NAME}" "${GCC_NAME}")

    # versions
    local CONFIG_VER BINUTILS_VER MINGW_W64_VER GMP_VER MPFR_VER MPC_VER ISL_VER GCC_VER
    CONFIG_VER="64198f1"  # config.guess 2017-11-07
    BINUTILS_VER="2.29.1"
    MINGW_W64_VER="5.0.3"
    GMP_VER="6.1.2"
    MPFR_VER="3.1.6"
    MPC_VER="1.0.3"
    ISL_VER="0.18"
    GCC_VER="7.2.0"

    # filenames
    local CONFIG_PKG BINUTILS_PKG MINGW_W64_PKG GMP_PKG MPFR_PKG MPC_PKG ISL_PKG GCC_PKG PKGS
    CONFIG_PKG="config-${CONFIG_VER}.tar.gz"
    BINUTILS_PKG="binutils-${BINUTILS_VER}.tar.bz2"
    MINGW_W64_PKG="mingw-w64-v${MINGW_W64_VER}.tar.bz2"
    GMP_PKG="gmp-${GMP_VER}.tar.bz2"
    MPFR_PKG="mpfr-${MPFR_VER}.tar.gz"
    MPC_PKG="mpc-${MPC_VER}.tar.gz"
    ISL_PKG="isl-${ISL_VER}.tar.bz2"
    GCC_PKG="gcc-${GCC_VER}.tar.gz"
    PKGS=("${CONFIG_PKG}" "${BINUTILS_PKG}" "${MINGW_W64_PKG}" "${GMP_PKG}" "${MPFR_PKG}" "${MPC_PKG}" "${ISL_PKG}" "${GCC_PKG}")

    # urls
    local CONFIG_URLS BINUTILS_URLS MINGW_W64_URLS GMP_URLS MPFR_URLS MPC_URLS ISL_URLS GCC_URLS URLS_VARNAMES
    CONFIG_URLS=("http://git.savannah.gnu.org/gitweb/?p=config.git;a=snapshot;h=${CONFIG_VER};sf=tgz")
    BINUTILS_URLS=("https://ftp.gnu.org/gnu/binutils/binutils-${BINUTILS_VER}.tar.bz2")
    MINGW_W64_URLS=("http://downloads.sourceforge.net/project/mingw-w64/mingw-w64/mingw-w64-release/mingw-w64-v${MINGW_W64_VER}.tar.bz2")
    GMP_URLS=("https://ftp.gnu.org/gnu/gmp/gmp-${GMP_VER}.tar.bz2")
    MPFR_URLS=("https://ftp.gnu.org/gnu/mpfr/mpfr-${MPFR_VER}.tar.gz")
    MPC_URLS=("https://ftp.gnu.org/gnu/mpc/mpc-${MPC_VER}.tar.gz")
    ISL_URLS=("http://isl.gforge.inria.fr/isl-${ISL_VER}.tar.bz2")
    GCC_URLS=("https://ftp.gnu.org/gnu/gcc/gcc-${GCC_VER}/gcc-${GCC_VER}.tar.gz")
    URLS_VARNAMES=('CONFIG_URLS' 'BINUTILS_URLS' 'MINGW_W64_URLS' 'GMP_URLS' 'MPFR_URLS' 'MPC_URLS' 'ISL_URLS' 'GCC_URLS')

    # checksums
    local CONFIG_SHA256 BINUTILS_SHA256 MINGW_W64_SHA256 GMP_SHA256 MPFR_SHA256 MPC_SHA256 ISL_SHA256 GCC_SHA256 CHECKSUMS
    CONFIG_SHA256="4aed1df62df9a8263cdfbe2f05bb6a47e7fda12e4e47e4c71bc16718940c2133"
    BINUTILS_SHA256="1509dff41369fb70aed23682351b663b56db894034773e6dbf7d5d6071fc55cc"
    MINGW_W64_SHA256="2a601db99ef579b9be69c775218ad956a24a09d7dabc9ff6c5bd60da9ccc9cb4"
    GMP_SHA256="5275bb04f4863a13516b2f39392ac5e272f5e1bb8057b18aec1c9b79d73d8fb2"
    MPFR_SHA256="569ceb418aa935317a79e93b87eeb3f956cab1a97dfb2f3b5fd8ac2501011d62"
    MPC_SHA256="617decc6ea09889fb08ede330917a00b16809b8db88c29c31bfbb49cbf88ecc3"
    ISL_SHA256="6b8b0fd7f81d0a957beb3679c81bbb34ccc7568d5682844d8924424a0dadcb1b"
    GCC_SHA256="0153a003d3b433459336a91610cca2995ee0fb3d71131bd72555f2231a6efcfc"
    CHECKSUMS=("${CONFIG_SHA256}" "${BINUTILS_SHA256}" "${MINGW_W64_SHA256}" "${GMP_SHA256}" "${MPFR_SHA256}" "${MPC_SHA256}" "${ISL_SHA256}" "${GCC_SHA256}")

    # internal vars
    local NAME VERSION SELF SELF_NAME HELP
    NAME="mingw-w64-build"
    VERSION="3.1.1"
    SELF="${BASH_SOURCE[0]}"
    SELF_NAME=$(basename "${SELF}")
    HELP="\
usage: ${SELF_NAME} target [install-dir]
targets:
  i686
  i686.clean
  i686.distclean
  x86_64
  x86_64.clean
  x86_64.distclean
  pkgclean
default install-dir: ${HOME}/toolchains/mingw-w64-${MINGW_W64_VER}-gcc-${GCC_VER}"

    # args
    local TARGET_PARAM TARGET_DIR
    TARGET_PARAM="${1}"
    TARGET_DIR="${2}"
    if [[ "${TARGET_PARAM:-}" == "" ]]; then
        echo -e "${HELP}"
        echo "no target specified" >&2
        return 1
    fi
    if [[ "${TARGET_DIR:-}" == "" ]]; then
        TARGET_DIR="${HOME}/toolchains/mingw-w64-${MINGW_W64_VER}-gcc-${GCC_VER}"
    fi

    # target and prefix
    local TARGET_i686 TARGET_x86_64 PREFIX_i686 PREFIX_x86_64 TARGET PREFIX
    TARGET_i686="i686-w64-mingw32"
    TARGET_x86_64="x86_64-w64-mingw32"
    PREFIX_i686="mingw-w64-i686"
    PREFIX_x86_64="mingw-w64-x86_64"
    case "${TARGET_PARAM}" in
        i686|i686.clean|i686.distclean)
            TARGET="${TARGET_i686}"
            PREFIX="${PREFIX_i686}"
            ;;
        x86_64|x86_64.clean|x86_64.distclean)
            TARGET="${TARGET_x86_64}"
            PREFIX="${PREFIX_x86_64}"
            ;;
        pkgclean)
            TARGET="pkgclean"
            PREFIX="pkgclean"
            ;;
    esac
    if [[ "${PREFIX:-}" == "" ]]; then
        echo -e "${HELP}"
        echo "target not valid: ${TARGET_PARAM}" >&2
        return 1
    fi

    # cleaning and directory creation
    echo "path: ${TARGET_DIR}"
    local MINGW_W64_DIR PKG_DIR SOURCE_DIR BUILD_DIR
    MINGW_W64_DIR="${TARGET_DIR}/${PREFIX}"
    PKG_DIR="${TARGET_DIR}/pkg"
    SOURCE_DIR="${TARGET_DIR}/source"
    BUILD_DIR="${TARGET_DIR}/build-${PREFIX}"
    case "${TARGET_PARAM}" in
        i686.clean|x86_64.clean)
            echo "clean:"
            echo "  rm -rf \"${BUILD_DIR}\""
            echo "  rm -rf \"${SOURCE_DIR}\""
            rm -rf "${BUILD_DIR}"
            rm -rf "${SOURCE_DIR}"
            echo "complete."
            return 0
            ;;
        i686.distclean|x86_64.distclean)
            echo "distclean:"
            echo "  rm -rf \"${MINGW_W64_DIR}\""
            rm -rf "${MINGW_W64_DIR}"
            echo "complete."
            return 0
            ;;
        pkgclean)
            echo "pkgclean:"
            echo "  rm -rf \"${PKG_DIR}\""
            rm -rf "${PKG_DIR}"
            echo "complete."
            return 0
            ;;
    esac
    mkdir -p "${TARGET_DIR}"
    if [[ ! -d "${TARGET_DIR}" ]]; then
        echo "unable to create directory: ${TARGET_DIR}" >&2
        return 1
    fi
    if [[ -e "${MINGW_W64_DIR}" ]]; then
        # prefix dir should not exist
        echo "unable to create directory (exists): ${MINGW_W64_DIR}" >&2
        echo "remove with: ${SELF_NAME} ${TARGET_PARAM%%.*}.distclean${2:+ $2}" >&2
        return 1
    fi
    mkdir -p "${PKG_DIR}"
    if [[ ! -d "${PKG_DIR}" ]]; then
        echo "unable to create directory: ${PKG_DIR}" >&2
        return 1
    fi

    # verify/fetch
    echo "verify:"
    local DOWNLOAD_VERBOSE I URLS_IREF URLS CHECKSUM
    DOWNLOAD_VERBOSE=true
    for I in "${!PKGS[@]}"; do
        echo "  ${PKGS[I]}"
        URLS_IREF="${URLS_VARNAMES[I]}[@]"
        URLS="${!URLS_IREF}"
        CHECKSUM=$(shasum -a 256 "${PKG_DIR}/${PKGS[I]}" 2>/dev/null | awk '{ print $1 }')
        if [[ "${CHECKSUM}" != "${CHECKSUMS[I]}" ]] >/dev/null 2>&1; then
            download_url "${DOWNLOAD_VERBOSE}" "${PKG_DIR}/${PKGS[I]}" ${URLS[@]} || return 1
        fi
        CHECKSUM=$(shasum -a 256 "${PKG_DIR}/${PKGS[I]}" 2>/dev/null | awk '{ print $1 }')
        if [[ "${CHECKSUM}" != "${CHECKSUMS[I]}" ]]; then
            echo "checksum mismatch for package: ${PKG_DIR}/${PKGS[I]}" >&2
            echo "expected: ${CHECKSUMS[I]}" >&2
            echo "actual:   ${CHECKSUM}" >&2
            return 1
        fi
    done

    # additional directory creation
    mkdir -p "${MINGW_W64_DIR}"
    if [[ ! -d "${MINGW_W64_DIR}" ]]; then
        echo "unable to create directory: ${MINGW_W64_DIR}" >&2
        return 1
    fi
    mkdir -p "${SOURCE_DIR}"
    if [[ ! -d "${SOURCE_DIR}" ]]; then
        echo "unable to create directory: ${SOURCE_DIR}" >&2
        return 1
    fi
    if [[ -e "${BUILD_DIR}" ]]; then
        # never reuse build dir
        rm -rf "${BUILD_DIR}"
    fi
    mkdir -p "${BUILD_DIR}"
    if [[ ! -d "${BUILD_DIR}" ]]; then
        echo "unable to create directory: ${BUILD_DIR}" >&2
        return 1
    fi

    # extract
    echo "extract:"
    for I in "${!PKGS[@]}"; do
        echo "  ${PKGS[I]}"
        if [[ -e "${SOURCE_DIR}/${NAMES[I]}" ]]; then
            rm -rf "${SOURCE_DIR}/${NAMES[I]}"
        fi
        mkdir -p "${SOURCE_DIR}/${NAMES[I]}"
        if ! tar -xf "${PKG_DIR}/${PKGS[I]}" -C "${SOURCE_DIR}/${NAMES[I]}" >/dev/null 2>&1; then
            echo "unable to extract package: ${PKG_DIR}/${PKGS[I]}" >&2
            return 1
        fi
    done

    # host
    echo "host:"
    local SYS_NAME SYS_ARCH SYS_TYPE CPU_COUNT
    SYS_NAME=$(uname | awk '{ print tolower($0)}')
    SYS_ARCH=$(uname -m)
    SYS_TYPE=$("${SOURCE_DIR}/config/config-${CONFIG_VER}/config.guess")
    if [[ "${SYS_NAME}" == "darwin" ]]; then
        CPU_COUNT=$(sysctl -n hw.ncpu 2>/dev/null)
    elif [[ "${SYS_NAME}" == "linux" ]]; then
        CPU_COUNT=$(grep -c processor /proc/cpuinfo 2>/dev/null)
    fi
    CPU_COUNT="${CPU_COUNT:-1}"
    echo "  name: ${SYS_NAME}"
    echo "  arch: ${SYS_ARCH}"
    echo "  type: ${SYS_TYPE}"
    echo "  vcpu: ${CPU_COUNT}"

    # build
    echo "target: ${TARGET}"
    echo "build:"

    # binutils
    echo "  binutils ${BINUTILS_VER}"
    mkdir -pv "${BUILD_DIR}/binutils" > "${BUILD_DIR}/binutils.log" 2>&1 || return 1
    cd "${BUILD_DIR}/binutils"
    "${SOURCE_DIR}/binutils/binutils-${BINUTILS_VER}/configure" CC=gcc CXX=g++ --build="${SYS_TYPE}" --target="${TARGET}" --with-sysroot="${MINGW_W64_DIR}" --prefix="${MINGW_W64_DIR}" --disable-multilib --disable-werror --disable-shared --enable-static >> "${BUILD_DIR}/binutils.log" 2>&1 || return 1
    make -j "${CPU_COUNT}" >> "${BUILD_DIR}/binutils.log" 2>&1 || return 1
    make install-strip >> "${BUILD_DIR}/binutils.log" 2>&1 || return 1

    # update PATH
    export PATH="${MINGW_W64_DIR}/bin:${PATH}"

    # mingw-w64 headers
    echo "  mingw-w64 ${MINGW_W64_VER} headers"
    mkdir -pv "${BUILD_DIR}/mingw-w64-headers" > "${BUILD_DIR}/mingw-w64-headers.log" 2>&1 || return 1
    cd "${BUILD_DIR}/mingw-w64-headers"
    "${SOURCE_DIR}/mingw-w64/mingw-w64-v${MINGW_W64_VER}/mingw-w64-headers/configure" --build="${SYS_TYPE}" --host="${TARGET}" --prefix="${MINGW_W64_DIR}" >> "${BUILD_DIR}/mingw-w64-headers.log" 2>&1 || return 1
    make install >> "${BUILD_DIR}/mingw-w64-headers.log" 2>&1 || return 1

    # create symlinks
    cd "${MINGW_W64_DIR}"
    ln -s "${TARGET}" mingw
    if [[ ! -d "usr" ]]; then
        ln -s . usr
    fi
    if [[ ! -d "${TARGET}/include" ]]; then
        cd "${TARGET}"
        ln -s ../include include
        cd "${MINGW_W64_DIR}"
    fi
    if [[ ! -d "${TARGET}/usr" ]]; then
        cd "${TARGET}"
        ln -s . usr
        cd "${MINGW_W64_DIR}"
    fi

    # gmp
    echo "  gmp ${GMP_VER}"
    local GMP_DIR
    GMP_DIR="${BUILD_DIR}/gmp-${GMP_VER}-${SYS_ARCH}"
    mkdir -pv "${BUILD_DIR}/gmp" > "${BUILD_DIR}/gmp.log" 2>&1 || return 1
    cd "${BUILD_DIR}/gmp"
    "${SOURCE_DIR}/gmp/gmp-${GMP_VER}/configure" CC=gcc CXX=g++ CPPFLAGS=-fexceptions --build="${SYS_TYPE}" --prefix="${GMP_DIR}" --enable-cxx --disable-shared --enable-static >> "${BUILD_DIR}/gmp.log" 2>&1 || return 1
    make -j "${CPU_COUNT}" >> "${BUILD_DIR}/gmp.log" 2>&1 || return 1
    make check >> "${BUILD_DIR}/gmp.log" 2>&1 || return 1
    make install >> "${BUILD_DIR}/gmp.log" 2>&1 || return 1

    # mpfr
    echo "  mpfr ${MPFR_VER}"
    local MPFR_DIR
    MPFR_DIR="${BUILD_DIR}/mpfr-${MPFR_VER}-${SYS_ARCH}"
    mkdir -pv "${BUILD_DIR}/mpfr" > "${BUILD_DIR}/mpfr.log" 2>&1 || return 1
    cd "${BUILD_DIR}/mpfr"
    "${SOURCE_DIR}/mpfr/mpfr-${MPFR_VER}/configure" CC=gcc CXX=g++ CFLAGS="-I${GMP_DIR}/include" CPPFLAGS="-I${GMP_DIR}/include" LDFLAGS="-L${GMP_DIR}/lib" --build="${SYS_TYPE}" --prefix="${MPFR_DIR}" --with-gmp="${GMP_DIR}" --disable-shared --enable-static >> "${BUILD_DIR}/mpfr.log" 2>&1 || return 1
    make -j "${CPU_COUNT}" >> "${BUILD_DIR}/mpfr.log" 2>&1 || return 1
    make install >> "${BUILD_DIR}/mpfr.log" 2>&1 || return 1

    # mpc
    echo "  mpc ${MPC_VER}"
    local MPC_DIR
    MPC_DIR="${BUILD_DIR}/mpc-${MPC_VER}-${SYS_ARCH}"
    mkdir -pv "${BUILD_DIR}/mpc" > "${BUILD_DIR}/mpc.log" 2>&1 || return 1
    cd "${BUILD_DIR}/mpc"
    "${SOURCE_DIR}/mpc/mpc-${MPC_VER}/configure" CC=gcc CXX=g++ CFLAGS="-I${GMP_DIR}/include -I${MPFR_DIR}/include" CPPFLAGS="-I${GMP_DIR}/include -I${MPFR_DIR}/include" LDFLAGS="-L${GMP_DIR}/lib -L${MPFR_DIR}/lib" --build="${SYS_TYPE}" --prefix="${MPC_DIR}" --with-gmp="${GMP_DIR}" --with-mpfr="${MPFR_DIR}" --disable-shared --enable-static >> "${BUILD_DIR}/mpc.log" 2>&1 || return 1
    make -j "${CPU_COUNT}" >> "${BUILD_DIR}/mpc.log" 2>&1 || return 1
    make install >> "${BUILD_DIR}/mpc.log" 2>&1 || return 1

    # isl
    echo "  isl ${ISL_VER}"
    local ISL_DIR
    ISL_DIR="${BUILD_DIR}/isl-${ISL_VER}-${SYS_ARCH}"
    mkdir -pv "${BUILD_DIR}/isl" > "${BUILD_DIR}/isl.log" 2>&1 || return 1
    cd "${BUILD_DIR}/isl"
    "${SOURCE_DIR}/isl/isl-${ISL_VER}/configure" CC=gcc CXX=g++ CFLAGS="-I${GMP_DIR}/include" CPPFLAGS="-I${GMP_DIR}/include" LDFLAGS="-L${GMP_DIR}/lib" --build="${SYS_TYPE}" --prefix="${ISL_DIR}" --with-gmp="${GMP_DIR}" --disable-shared --enable-static >> "${BUILD_DIR}/isl.log" 2>&1 || return 1
    make -j "${CPU_COUNT}" >> "${BUILD_DIR}/isl.log" 2>&1 || return 1
    make install >> "${BUILD_DIR}/isl.log" 2>&1 || return 1

    # gcc compilers
    echo "  gcc ${GCC_VER} compilers"
    local GCC_CONFIG_EXTRA
    GCC_CONFIG_EXTRA=()
    if [[ "${SYS_NAME}" == "darwin" ]]; then
        GCC_CONFIG_EXTRA+=("--with-system-zlib")
    fi
    mkdir -pv "${BUILD_DIR}/gcc" > "${BUILD_DIR}/gcc.log" 2>&1 || return 1
    cd "${BUILD_DIR}/gcc"
    "${SOURCE_DIR}/gcc/gcc-${GCC_VER}/configure" CFLAGS="-I${GMP_DIR}/include -I${MPFR_DIR}/include -I${MPC_DIR}/include -I${ISL_DIR}/include" CPPFLAGS="-I${GMP_DIR}/include -I${MPFR_DIR}/include -I${MPC_DIR}/include -I${ISL_DIR}/include" LDFLAGS="-L${GMP_DIR}/lib -L${MPFR_DIR}/lib -L${MPC_DIR}/lib -L${ISL_DIR}/lib" --build="${SYS_TYPE}" --host="${SYS_TYPE}" --target="${TARGET}" --prefix="${MINGW_W64_DIR}" --with-sysroot="${MINGW_W64_DIR}" --with-gmp="${GMP_DIR}" --with-mpfr="${MPFR_DIR}" --with-mpc="${MPC_DIR}" --with-isl="${ISL_DIR}" --disable-multilib --disable-nls --disable-libstdcxx-pch --disable-win32-registry --enable-checking=release --enable-languages=c,c++ --enable-lto --disable-shared --enable-static "${GCC_CONFIG_EXTRA[@]}" >> "${BUILD_DIR}/gcc.log" 2>&1 || return 1
    make -j "${CPU_COUNT}" all-gcc >> "${BUILD_DIR}/gcc.log" 2>&1 || return 1
    make install-gcc >> "${BUILD_DIR}/gcc.log" 2>&1 || return 1

    # mingw-w64 runtime
    export CC=""
    export CXX=""
    echo "  mingw-w64 ${MINGW_W64_VER} runtime"
    local MINGW_W64_CONFIG_EXTRA
    MINGW_W64_CONFIG_EXTRA=()
    if [[ "${TARGET}" == "${TARGET_i686}" ]]; then
        MINGW_W64_CONFIG_EXTRA+=("--enable-lib32" "--disable-lib64")
    elif [[ "${TARGET}" == "${TARGET_x86_64}" ]]; then
        MINGW_W64_CONFIG_EXTRA+=("--enable-lib64" "--disable-lib32")
    fi
    mkdir -pv "${BUILD_DIR}/mingw-w64-crt" > "${BUILD_DIR}/mingw-w64-crt.log" 2>&1 || return 1
    cd "${BUILD_DIR}/mingw-w64-crt"
    "${SOURCE_DIR}/mingw-w64/mingw-w64-v${MINGW_W64_VER}/mingw-w64-crt/configure" --build="${SYS_TYPE}" --host="${TARGET}" --prefix="${MINGW_W64_DIR}" --with-sysroot="${MINGW_W64_DIR}" "${MINGW_W64_CONFIG_EXTRA[@]}" >> "${BUILD_DIR}/mingw-w64-crt.log" 2>&1 || return 1
    make -j "${CPU_COUNT}" >> "${BUILD_DIR}/mingw-w64-crt.log" 2>&1 || return 1
    make install-strip >> "${BUILD_DIR}/mingw-w64-crt.log" 2>&1 || return 1

    # relocate and symlink libs
    cd "${MINGW_W64_DIR}"
    mv "${TARGET}/lib/"* lib/
    rm -rf "${TARGET}/lib"
    cd "${TARGET}"
    ln -s ../lib lib

    # gcc libraries
    echo "  gcc ${GCC_VER} libraries"
    cd "${BUILD_DIR}/gcc"
    make -j "${CPU_COUNT}" all-target-libgcc >> "${BUILD_DIR}/gcc.log" 2>&1 || return 1
    make -j "${CPU_COUNT}" install-target-libgcc >> "${BUILD_DIR}/gcc.log" 2>&1 || return 1
    make -j "${CPU_COUNT}" >> "${BUILD_DIR}/gcc.log" 2>&1 || return 1
    make install-strip >> "${BUILD_DIR}/gcc.log" 2>&1 || return 1

    # clean up
    cd "${MINGW_W64_DIR}"
    find . -name "*.la" -type f -exec rm {} ";" >/dev/null 2>&1

    # done
    echo "bin: ${MINGW_W64_DIR}/bin"
    echo "  run the following command and add it to your shell startup script"
    echo "  (e.g., .bashrc or .bash_profile) to make persistent across sessions:"
    echo "    export PATH=\"${MINGW_W64_DIR}/bin:\${PATH}\""
    echo "complete."
    return 0

    set +o pipefail
}

mingw-w64-build "${@}"