summaryrefslogtreecommitdiffstats
path: root/MinGW-w64/build/MinGW-w64.SlackBuild
blob: ed0e97d7f9d2a05327b536ec2dd3558a440f57cd (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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
#!/bin/sh
# $Id$
# Copyright 2020, 2021, 2023  Eric Hameleers, Eindhoven, NL
# All rights reserved.
#
#   Permission to use, copy, modify, and distribute this software for
#   any purpose with or without fee is hereby granted, provided that
#   the above copyright notice and this permission notice appear in all
#   copies.
#
#   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
#   WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
#   IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
#   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
#   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
#   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
#   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
#   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
#   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
#   SUCH DAMAGE.
# -----------------------------------------------------------------------------
#
# This script pays hommage to the work of Tk-Glitch <ti3nou at gmail dot com>
# who is the author of '(Mostly) Portable GCC/MinGW', this SlackBuild is an
# adapted version of his work, adding support for a 32bit OS.
# For more info, see the git homepage at:
#   https://github.com/Frogging-Family/mostlyportable-gcc
#
# -----------------------------------------------------------------------------
#
# Slackware SlackBuild script 
# ===========================
# By:          Eric Hameleers <alien@slackware.com>
# For:         MinGW-w64
# Descr:       Mininalist GCC to compile Windows binaries
# URL:         http://mingw-w64.org/doku.php
# Build needs: 
# Needs:       
# Changelog:   
# 7.0.0_gcc9.3.0-1:
#              21/Sep/2020 by Eric Hameleers <alien@slackware.com>
#              * Initial build.
# 8.0.0_gcc10.2.0-1:
#              10/dec/2020 by Eric Hameleers <alien@slackware.com>
#              * Update.
# 9.0.0_gcc11.2.0-1:
#              22/dec/2021 by Eric Hameleers <alien@slackware.com>
#              * Update.
# 10.0.0_gcc12.2.0-1:
#              17/apr/2023 by Eric Hameleers <alien@slackware.com>
#              * Update.
# 
# Run 'sh MinGW-w64.SlackBuild' to build a Slackware package.
# The package (.t?z) and .txt file as well as build logs are created in /tmp .
# Install the package using 'installpkg' or 'upgradepkg --install-new'.
#
# -----------------------------------------------------------------------------

PRGNAM=MinGW-w64
BUILD=${BUILD:-1}
NUMJOBS=${NUMJOBS:-" -j$(nproc) "}
TAG=${TAG:-alien}

BINUTILS_DOCS="COPYING* ChangeLog.linux MAI* README*"
GCC_DOCS="COPYING* ChangeLog* INSTALL LAST_UPDATED MAINTAINERS \
  NEWS README*"
MINGW_DOCS="AUTHORS COPYING* DISCLAIMER*"

# Core tools:
BINUTILS=2.40
GCC=12.2.0
MINGW=v10.0.0

# Package version:
VERSION=${VERSION:-${MINGW}_gcc${GCC}}

# Support tools:
GMP=6.2.1
MPFR=4.2.0
MPC=1.3.1
OSL=0.9.5
ISL=0.26
CLOOG=0.20.0

# Automatically determine the architecture we're building on:
if [ -z "$ARCH" ]; then
  case "$(uname -m)" in
    i?86) ARCH=i586 ;;
    arm*) readelf /usr/bin/file -A | egrep -q "Tag_CPU.*[4,5]" && ARCH=arm || ARCH=armv7hl ;;
    # Unless $ARCH is already set, use uname -m for all other archs:
    *) ARCH=$(uname -m) ;;
  esac
  export ARCH
fi
# Set CFLAGS/CXXFLAGS and LIBDIRSUFFIX:
case "$ARCH" in
  i?86)      SLKCFLAGS="-O2 -march=${ARCH} -mtune=i686"
             SLKLDFLAGS="-L/usr/lib"; LIBDIRSUFFIX=""
             MINGW_TARGETS="${ARCH}-w64-mingw32"
             ;;
  x86_64)    SLKCFLAGS="-O2 -fPIC"
             SLKLDFLAGS="-L/usr/lib64"; LIBDIRSUFFIX="64"
             MINGW_TARGETS="i686-w64-mingw32 x86_64-w64-mingw32"
             ;;
  *)         SLKCFLAGS=${SLKCFLAGS:-"-O2"}
             SLKLDFLAGS=${SLKLDFLAGS:-""}; LIBDIRSUFFIX=${LIBDIRSUFFIX:-""}
             MINGW_TARGETS="${ARCH}-w64-mingw32"
             ;;
esac
TARGET=${ARCH}-slackware-linux

# You can chose to use the system gcc instead of compiling it to save some time:
BUILD_GCC=${BUILD_GCC:-"YES"}

# Tool capabilities:
GCC_EXCEPTIONS="dwarf2"
GCC_LANGUAGES="c,c++,lto"
MINGW_LANGUAGES="c,c++,lto,objc,obj-c++"
WIN32_THREADS="false"

if [ "$ARCH" == "x86_64" ]; then
  BINUTILS_TARGETS="x86_64-pep"
  GCC_MULTI_ARCH="\
    --with-arch-32=i686 \
    --with-abi=m64 \
    --with-multilib-list=m32,m64 \
    --enable-multilib \
    --enable-multiarch \
    --enable-default-pie"
else
  BINUTILS_TARGETS="i386-efi-pe"
  GCC_MULTI_ARCH="\
    --with-arch=i586"
fi

# Arguments for the MinGW gcc compilation:
if [ "$GCC_EXCEPTIONS" == "dwarf2" ]; then
  EXCEPTIONS_ARGS="--disable-sjlj-exceptions --with-dwarf2"
else
  EXCEPTIONS_ARGS="--disable-dw2-exceptions"
fi
if [ "$WIN32_THREADS" == "true" ]; then
  WIN32THREADS_ARGS="--enable-threads=win32"
else
  WIN32THREADS_ARGS="--enable-threads=posix"
fi

# Where do we look for sources?
SRCDIR=$(cd $(dirname $0); pwd)

# Place to build (TMP) package (PKG) and output (OUTPUT) the program:
# Note: OUTPUT should *not* be a subdirectory of TMP!
TMP=${TMP:-/tmp/build}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

# Where will our bootstrap gcc be installed:
GCC_BOOTSTRAP_PATH="${TMP}/mingw_gcc_bootstrap"

# Where will MinGW be installed:
MINGW_OUTPUT_PATH="${PKG}/usr/mingw-w64-$VERSION"

# If you want automatic download to work, supply a URL for 'SRCURL' below:
SOURCE[0]="$SRCDIR/sources/mingw-w64-${MINGW}.tar.bz2"
SRCURL[0]="https://sourceforge.net/projects/mingw-w64/files/mingw-w64/mingw-w64-release/mingw-w64-${MINGW}.tar.bz2"

# Source tarballs and download locations:
mkdir -p $SRCDIR/sources

SOURCE[1]="$SRCDIR/sources/gcc-${GCC}.tar.xz"
SRCURL[1]="ftp://ftp.gnu.org/gnu/gcc/gcc-${GCC}/gcc-${GCC}.tar.xz"

SOURCE[2]="$SRCDIR/sources/binutils-${BINUTILS}.tar.gz"
SRCURL[2]="https://ftp.gnu.org/gnu/binutils/binutils-${BINUTILS}.tar.gz"

SOURCE[3]="$SRCDIR/sources/gmp-${GMP}.tar.xz"
SRCURL[3]="https://ftp.gnu.org/gnu/gmp/gmp-${GMP}.tar.xz"

SOURCE[4]="$SRCDIR/sources/mpfr-${MPFR}.tar.xz"
SRCURL[4]="https://ftp.gnu.org/gnu/mpfr/mpfr-${MPFR}.tar.xz"

SOURCE[5]="$SRCDIR/sources/mpc-${MPC}.tar.gz"
SRCURL[5]="https://ftp.gnu.org/gnu/mpc/mpc-${MPC}.tar.gz"

SOURCE[6]="$SRCDIR/sources/isl-${ISL}.tar.xz"
SRCURL[6]="https://libisl.sourceforge.io/isl-${ISL}.tar.xz"

SOURCE[7]="$SRCDIR/sources/osl-${OSL}.tar.gz"
SRCURL[7]="https://github.com/periscop/openscop/releases/download/${OSL}/osl-${OSL}.tar.gz"

SOURCE[8]="$SRCDIR/sources/cloog-${CLOOG}.tar.gz"
SRCURL[8]="https://github.com/periscop/cloog/releases/download/cloog-${CLOOG}/cloog-${CLOOG}.tar.gz"

##
## --- with a little luck, you won't have to edit below this point --- ##
##

# Exit the script on errors:
set -e
trap 'echo "$0 FAILED at line ${LINENO}" | tee $OUTPUT/error-${PRGNAM}.log' ERR
# Catch unitialized variables:
set -u
P1=${1:-1}

# Save old umask and set to 0022:
_UMASK_=$(umask)
umask 0022

# Create working directories:
mkdir -p $OUTPUT           # place for the package to be saved
mkdir -p $TMP/tmp-$PRGNAM  # location to build the source
mkdir -p $PKG              # place for the package to be built
rm -rf $PKG/*              # always erase old package's contents
rm -rf $TMP/tmp-$PRGNAM/*  # remove the remnants of previous build
rm -rf $OUTPUT/{configure,make,install,error,makepkg,patch}-$PRGNAM.log
                           # remove old log files
rm -rf $GCC_BOOTSTRAP_PATH # Remove other files created outside of $PKG

# Source file availability:
for (( i = 0; i < ${#SOURCE[*]}; i++ )) ; do
  if ! [ -f ${SOURCE[$i]} ]; then
    echo "Source '$(basename ${SOURCE[$i]})' not available yet..."
    # Check if the $SRCDIR is writable at all - if not, download to $OUTPUT
    [ -w "$SRCDIR" ] || SOURCE[$i]="$OUTPUT/$(basename ${SOURCE[$i]})"
    if [ -f ${SOURCE[$i]} ]; then echo "Ah, found it!"; continue; fi
    if ! [ "x${SRCURL[$i]}" == "x" ]; then
      echo "Will download file to $(dirname $SOURCE[$i])"
      wget --no-check-certificate -nv -T 20 -O "${SOURCE[$i]}" ${SRCURL[$i]} || true
      if [ $? -ne 0 -o ! -s "${SOURCE[$i]}" ]; then
        echo "Fail to download '$(basename ${SOURCE[$i]})'. Aborting the build."
        mv -f "${SOURCE[$i]}" "${SOURCE[$i]}".FAIL
        exit 1
      fi
    fi
    if [ ! -f "${SOURCE[$i]}" -o ! -s "${SOURCE[$i]}" ]; then
      echo "File '$(basename ${SOURCE[$i]})' not available. Aborting the build."
      exit 1
    fi
  fi
done

if [ "$P1" == "--download" ]; then
  echo "Download complete."
  exit 0
fi

# --- PACKAGE BUILDING ---

echo "++"
echo "|| $PRGNAM-$VERSION"
echo "++"

echo Building ...
export LDFLAGS="$SLKLDFLAGS"
export CXXFLAGS="$SLKCFLAGS"
export CFLAGS="$SLKCFLAGS"
export CPPFLAGS="$SLKCFLAGS"

# Used in all compilations:
COMMONCONFIG="--disable-shared --enable-static"

# Build the supporting tools first, then build a custom gcc for our platform
# and then build mingw-w64 binaries using our support tools and custom gcc:

function build_support_tools() {
  MY_DESTDIR="${1}"
  MY_ARCH="${2:-""}"

  [ -z "$MY_ARCH" ] && BUILD_ARGS="" || BUILD_ARGS="--build=$MY_ARCH"

  # gmp:
  echo "-- gmp --"
  cd $TMP/tmp-$PRGNAM/gmp-${GMP}
  ./configure \
    --prefix=${MY_DESTDIR} \
    ${COMMONCONFIG} \
    ${BUILD_ARGS}
  make ${NUMJOBS} || make || exit 1
  make install || exit 1

  # mpfr:
  echo "-- mpfr --"
  cd $TMP/tmp-$PRGNAM/mpfr-${MPFR}
  ./configure \
    --prefix=${MY_DESTDIR} \
    --with-gmp=${MY_DESTDIR} \
    ${COMMONCONFIG} \
    ${BUILD_ARGS}
  make ${NUMJOBS} || make || exit 1
  make install || exit 1

  # mpc:
  echo "-- mpc --"
  cd $TMP/tmp-$PRGNAM/mpc-${MPC}
  ./configure \
    --prefix=${MY_DESTDIR} \
    --with-gmp=${MY_DESTDIR} \
    --with-mpfr=${MY_DESTDIR} \
    ${COMMONCONFIG} \
    ${BUILD_ARGS}
  make ${NUMJOBS} || make || exit 1
  make install || exit 1

  # isl:
  echo "-- isl --"
  cd $TMP/tmp-$PRGNAM/isl-${ISL}
  ./configure \
    --prefix=${MY_DESTDIR} \
    ${COMMONCONFIG} \
    ${BUILD_ARGS}
  make ${NUMJOBS} || make || exit 1
  make install || exit 1

} # end build_support_tools

function build_support_tools_mingw() {
  MY_DESTDIR=${1}

  # osl:
  echo "-- osl --"
  cd $TMP/tmp-$PRGNAM/osl-${OSL}
  ./configure \
    --prefix=${MY_DESTDIR} \
    --with-gmp=${MY_DESTDIR} \
    ${COMMONCONFIG}
  make ${NUMJOBS} || make || exit 1
  make install || exit 1

  # cloog:
  echo "-- cloog --"
  cd $TMP/tmp-$PRGNAM/cloog-${CLOOG}
  ./configure \
    --prefix=${MY_DESTDIR} \
    --with-isl=${MY_DESTDIR} \
    --with-osl=${MY_DESTDIR} \
    ${COMMONCONFIG}
  make ${NUMJOBS} || make || exit 1
  # Issues with parallism in installing docs:
  make -i install

} # end build_support_tools_mingw

function untar_and_patch() {
  cd $TMP/tmp-$PRGNAM || exit 1
  rm -rf *
  echo "[MINGW] Extracting the source archive(s) for $PRGNAM..."
  for (( i = 0; i < ${#SOURCE[*]}; i++ )) ; do
    tar -xf ${SOURCE[$i]}
  done

  # Fix errors in compilation:
  cd gcc-${GCC}
   cat $SRCDIR/patches/0020-libgomp-Don-t-hard-code-MS-printf-attributes.patch \
     | patch -p1 --verbose || exit 1
   sed -i libbacktrace/configure -e 's|\-Werror||g' || exit 1
  cd ..

  chown -R root:root *
  chmod -R u+w,go+r-w,a+rX-st *
} # end untar_and_patch

if [ "$BUILD_GCC" == "YES" ]; then
#
# --- GCC ---
#

# We extract the sources twice (and remove them inbetween),
# to build native GCC and MinGW in two separate stages:
untar_and_patch || exit 1

cd $TMP/tmp-$PRGNAM || exit 1

# Make the process use our tools as they get built:
export PATH="${GCC_BOOTSTRAP_PATH}/bin:${GCC_BOOTSTRAP_PATH}/lib:${GCC_BOOTSTRAP_PATH}/include:${PATH}"

# Build the support tools:
build_support_tools ${GCC_BOOTSTRAP_PATH} ${TARGET}

# binutils:
echo "-- binutils --"
mkdir -p $TMP/tmp-$PRGNAM/binutils-build && cd $TMP/tmp-$PRGNAM/binutils-build
$TMP/tmp-$PRGNAM/binutils-${BINUTILS}/configure \
  --prefix=${GCC_BOOTSTRAP_PATH} \
  --with-lib-path="${GCC_BOOTSTRAP_PATH}/lib" \
  --enable-deterministic-archives \
  --enable-gold \
  --enable-ld=default \
  --enable-lto \
  --enable-plugins \
  --enable-relro \
  --enable-targets=${BINUTILS_TARGETS} \
  --enable-threads \
  --disable-gdb \
  --disable-werror \
  --with-pic \
  --with-system-zlib \
  ${COMMONCONFIG}
make ${NUMJOBS} configure-host || exit 1
make ${NUMJOBS} tooldir=${GCC_BOOTSTRAP_PATH} || exit 1
make install prefix=${GCC_BOOTSTRAP_PATH} tooldir=${GCC_BOOTSTRAP_PATH} || exit 1
# Remove unwanted files:
rm -f ${GCC_BOOTSTRAP_PATH}/share/man/man1/{dlltool,nlmconv,windres,windmc}*

if [ "$ARCH" != "x86_64" ]; then
  # Use the output of '${GCC_BOOTSTRAP_PATH}/bin/ld --verbose | grep SEARCH_DIR'
  # to see that this ld looks in a not-existing location, which we'll exploit.
  ln -s /usr ${GCC_BOOTSTRAP_PATH}/i686-pc-linux-gnu
fi

# gcc:
echo "-- gcc --"
mkdir -p $TMP/tmp-$PRGNAM/gcc-build && cd $TMP/tmp-$PRGNAM/gcc-build
LDFLAGS="$LDFLAGS" \
LD="${GCC_BOOTSTRAP_PATH}/bin/ld" \
AR="${GCC_BOOTSTRAP_PATH}/bin/ar" \
$TMP/tmp-$PRGNAM/gcc-${GCC}/configure \
  ${GCC_MULTI_ARCH} \
  --prefix=${GCC_BOOTSTRAP_PATH} \
  --with-pkgversion='alienBOB' \
  --with-build-sysroot='/' \
  --enable-languages=${GCC_LANGUAGES} \
  --disable-bootstrap \
  --with-gcc-major-version-only \
  --enable-linker-build-id \
  --disable-libstdcxx-pch \
  --without-included-gettext \
  --enable-libgomp \
  --enable-lto \
  --enable-threads=posix \
  --enable-tls \
  --enable-nls \
  --enable-clocale=gnu \
  --enable-libstdcxx-time=yes \
  --with-default-libstdcxx-abi=new \
  --enable-gnu-unique-object \
  --disable-vtable-verify \
  --enable-plugin \
  --with-target-system-zlib=auto \
  --with-system-zlib \
  --disable-werror \
  --enable-checking=release \
  --with-fpmath=sse \
  --with-tune=generic \
  --without-cuda-driver \
  --with-isl=${GCC_BOOTSTRAP_PATH} \
  --with-gmp=${GCC_BOOTSTRAP_PATH} \
  --with-mpfr=${GCC_BOOTSTRAP_PATH} \
  --with-mpc=${GCC_BOOTSTRAP_PATH} \
  --enable-offload-targets=nvptx-none,amdgcn \
  --build=${TARGET} \
  --host=${TARGET} \
  --target=${TARGET} \
  || exit 1


make ${NUMJOBS} || make || exit 1
make install || exit 1
ln -s gcc ${GCC_BOOTSTRAP_PATH}/bin/cc

#libgcc:
echo "-- libgcc --"
cd $TMP/tmp-$PRGNAM/gcc-build
make -C ${TARGET}/libgcc install
make -C libcpp install
make -C gcc install-po
make -C ${TARGET}/libgcc install-shared
rm -f ${GCC_BOOTSTRAP_PATH}/lib/gcc/${TARGET}/${GCC}/libgcc_eh.a
if [ "$ARCH" == "x86_64" ]; then
  make -C ${TARGET}/32/libgcc install
  make -C ${TARGET}/32/libgcc install-shared
  rm -f ${GCC_BOOTSTRAP_PATH}/lib/gcc/${TARGET}/${GCC}/32/libgcc_eh.a
fi
for lib in \
  libatomic \
  libgomp \
  libitm \
  libquadmath \
  libsanitizer/{a,l,ub}san \
  libstdc++-v3/src \
  libvtv; do
  make -C ${TARGET}/$lib install-toolexeclibLTLIBRARIES
done
if [ "$ARCH" == "x86_64" ]; then
  make -C ${TARGET}/libsanitizer/tsan install-toolexeclibLTLIBRARIES
  for lib in \
    libatomic \
    libgomp \
    libitm \
    libquadmath \
    libsanitizer/{a,l,ub}san \
    libstdc++-v3/src \
    libvtv; do
    make -C ${TARGET}/32/$lib install-toolexeclibLTLIBRARIES
  done
fi
make -C ${TARGET}/libstdc++-v3/po install

fi # End $BUILD_GCC == YES

#
# --- MINGW ---
#

# We extract the sources twice (and remove them inbetween),
# to build native GCC and MinGW in two separate stages:
untar_and_patch || exit 1

cd $TMP/tmp-$PRGNAM || exit 1

# Make the process use our tools as they get built:
if [ "$BUILD_GCC" == "YES" ]; then
  export PATH="${GCC_BOOTSTRAP_PATH}/bin:${GCC_BOOTSTRAP_PATH}/lib:${GCC_BOOTSTRAP_PATH}/include:${MINGW_OUTPUT_PATH}/bin:${MINGW_OUTPUT_PATH}/lib:${MINGW_OUTPUT_PATH}/include:${PATH}"
else
  export PATH="${MINGW_OUTPUT_PATH}/bin:${MINGW_OUTPUT_PATH}/lib:${MINGW_OUTPUT_PATH}/include:${PATH}"
fi

# Re-build the support tools, this time targeting MinGW:
build_support_tools ${MINGW_OUTPUT_PATH} ${TARGET}
build_support_tools_mingw ${MINGW_OUTPUT_PATH}

# mingw-w64-binutils:
echo "-- mingw-w64-binutils --"
cd $TMP/tmp-$PRGNAM/binutils-${BINUTILS}
# Do not install libiberty:
sed -i 's/install_to_$(INSTALL_DEST) //' libiberty/Makefile.in
for _target in ${MINGW_TARGETS}; do
  echo -e "Building ${_target} cross binutils"
  mkdir -p $TMP/tmp-$PRGNAM/binutils-${_target} && cd $TMP/tmp-$PRGNAM/binutils-${_target}
  $TMP/tmp-$PRGNAM/binutils-${BINUTILS}/configure \
    --target="${_target}" \
    --enable-lto \
    --enable-plugins \
    --enable-deterministic-archives \
    --disable-multilib \
    --disable-nls \
    --disable-werror \
    --prefix="${MINGW_OUTPUT_PATH}" \
    ${COMMONCONFIG}
  make ${NUMJOBS} || make || exit 1
done
for _target in ${MINGW_TARGETS}; do
  echo -e "Installing ${_target} cross binutils"
  cd $TMP/tmp-$PRGNAM/binutils-${_target}
  make install || exit 1
done

# binutils docs:
echo "-- binutils-docs --"
mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION/binutils
( cd $TMP/tmp-$PRGNAM/binutils-${BINUTILS}
  cp -a $BINUTILS_DOCS $PKG/usr/doc/$PRGNAM-$VERSION/binutils/ || true
)

# mingw-w64-headers:
echo "-- mingw-w64-headers --"
for _target in ${MINGW_TARGETS}; do
  echo -e "Configuring ${_target} headers"
  mkdir -p $TMP/tmp-$PRGNAM/headers-${_target} && cd $TMP/tmp-$PRGNAM/headers-${_target}
  $TMP/tmp-$PRGNAM/mingw-w64-${MINGW}/mingw-w64-headers/configure \
    --enable-sdk=all \
    --enable-secure-api \
    --host="${_target}" \
    --prefix="${MINGW_OUTPUT_PATH}/${_target}"
  make || exit 1
done
for _target in ${MINGW_TARGETS}; do
  echo -e "Installing ${_target} headers"
  cd $TMP/tmp-$PRGNAM/headers-${_target}
  make install
  rm ${MINGW_OUTPUT_PATH}/${_target}/include/pthread_signal.h
  rm ${MINGW_OUTPUT_PATH}/${_target}/include/pthread_time.h
  rm ${MINGW_OUTPUT_PATH}/${_target}/include/pthread_unistd.h
done

# mingw-w64-headers-bootstrap:
echo "-- mingw-w64-headers-bootstrap --"
_dummystring="/* Dummy header, which gets overriden, if winpthread library gets installed.  */"
mkdir -p $TMP/tmp-$PRGNAM/dummy/ && cd $TMP/tmp-$PRGNAM//dummy
echo "${_dummystring}" > pthread_signal.h
echo "${_dummystring}" > pthread_time.h
echo "${_dummystring}" > pthread_unistd.h
for _target in ${MINGW_TARGETS}; do
  install -Dm644 $TMP/tmp-$PRGNAM/dummy/pthread_signal.h ${MINGW_OUTPUT_PATH}/${_target}/include/pthread_signal.h
  install -Dm644 $TMP/tmp-$PRGNAM/dummy/pthread_time.h ${MINGW_OUTPUT_PATH}/${_target}/include/pthread_time.h
  install -Dm644 $TMP/tmp-$PRGNAM/dummy/pthread_unistd.h ${MINGW_OUTPUT_PATH}/${_target}/include/pthread_unistd.h
done

# Use a separate src dir for mingw-w64-gcc-base:
cp -r $TMP/tmp-$PRGNAM/gcc-${GCC} $TMP/tmp-$PRGNAM/gcc-${GCC}-base

# mingw-w64-gcc-base:
echo "-- mingw-w64-gcc-base --"
# Do not install libiberty:
sed -i 's/install_to_$(INSTALL_DEST) //' $TMP/tmp-$PRGNAM/gcc-${GCC}-base/libiberty/Makefile.in
for _target in ${MINGW_TARGETS}; do
  echo -e "Building "${_target}" GCC C compiler"
  mkdir -p $TMP/tmp-$PRGNAM/gcc-${GCC}-base-${_target} && cd $TMP/tmp-$PRGNAM/gcc-${GCC}-base-${_target}
  $TMP/tmp-$PRGNAM/gcc-${GCC}-base/configure \
    --target="${_target}" \
    --enable-languages=c,lto \
    --with-system-zlib \
    --enable-lto \
    --disable-nls \
    --enable-version-specific-runtime-libs \
    --disable-multilib \
    --enable-checking=release \
    --with-isl="${MINGW_OUTPUT_PATH}" \
    --with-gmp="${MINGW_OUTPUT_PATH}" \
    --with-mpfr="${MINGW_OUTPUT_PATH}" \
    --with-mpc="${MINGW_OUTPUT_PATH}" \
    --prefix="${MINGW_OUTPUT_PATH}" \
    ${EXCEPTIONS_ARGS} \
    ${COMMONCONFIG} \
    || exit 1
    make ${NUMJOBS} all-gcc || make all-gcc || exit 1
done
for _target in ${MINGW_TARGETS}; do
  echo -e "Installing ${_target} GCC C compiler"
  cd $TMP/tmp-$PRGNAM/gcc-${GCC}-base-"${_target}"
  make install-gcc
  strip ${MINGW_OUTPUT_PATH}/bin/${_target}-* || true
  strip ${MINGW_OUTPUT_PATH}/libexec/gcc/${_target}/${GCC}/{cc1,collect2,lto*} || true
done

# mingw-w64-crt:
echo "-- mingw-w64-crt --"
for _target in ${MINGW_TARGETS}; do
  echo -e "Building ${_target} CRT"
  if [ "${_target}" == "x86_64-w64-mingw32" ]; then
    _crt_configure_args="--disable-lib32 --enable-lib64"
  else
    _crt_configure_args="--disable-lib64 --enable-lib32"
  fi
  mkdir -p $TMP/tmp-$PRGNAM/crt-${_target} && cd $TMP/tmp-$PRGNAM/crt-${_target}
  $TMP/tmp-$PRGNAM/mingw-w64-${MINGW}/mingw-w64-crt/configure \
    --host="${_target}" \
    --enable-wildcard \
    ${_crt_configure_args} \
    --prefix=${MINGW_OUTPUT_PATH}/${_target}
  make ${NUMJOBS} || make || exit 1
done
for _target in ${MINGW_TARGETS}; do
  echo -e "Installing ${_target} crt"
  cd $TMP/tmp-$PRGNAM/crt-${_target}
  make install
done

# mingw-w64-winpthreads:
echo "-- mingw-w64-winpthreads --"
for _target in ${MINGW_TARGETS}; do
  echo -e "Building ${_target} winpthreads..."
  mkdir -p $TMP/tmp-$PRGNAM/winpthreads-build-${_target} && cd $TMP/tmp-$PRGNAM/winpthreads-build-${_target}
  $TMP/tmp-$PRGNAM/mingw-w64-${MINGW}/mingw-w64-libraries/winpthreads/configure \
    --host="${_target}" \
    --prefix=${MINGW_OUTPUT_PATH}/${_target} \
    ${COMMONCONFIG}
  make ${NUMJOBS} || make || exit 1
done
for _target in ${MINGW_TARGETS}; do
  cd $TMP/tmp-$PRGNAM/winpthreads-build-${_target}
  make install || exit 1
  ${_target}-strip --strip-unneeded ${MINGW_OUTPUT_PATH}/${_target}/bin/*.dll  || true
done

# mingw-w64 docs:
echo "-- mingw-w64-docs --"
mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION/mingw-w64
( cd $TMP/tmp-$PRGNAM/mingw-w64-${MINGW}
  cp -a $MINGW_DOCS $PKG/usr/doc/$PRGNAM-$VERSION/mingw-w64/ || true
)

# mingw-w64-gcc:
echo "-- mingw-w64-gcc --"
for _target in ${MINGW_TARGETS}; do
  mkdir -p $TMP/tmp-$PRGNAM/gcc-build-${_target} && cd $TMP/tmp-$PRGNAM/gcc-build-${_target}
  $TMP/tmp-$PRGNAM/gcc-${GCC}/configure \
    --with-pkgversion='alienBOB' \
    --target="${_target}" \
    --libexecdir=${MINGW_OUTPUT_PATH}/lib \
    --enable-languages=${MINGW_LANGUAGES} \
    --disable-shared \
    --enable-fully-dynamic-string \
    --enable-libstdcxx-time=yes \
    --enable-libstdcxx-filesystem-ts=yes \
    --with-system-zlib \
    --enable-cloog-backend=isl \
    --enable-lto \
    --enable-libgomp \
    --disable-multilib \
    --enable-checking=release \
    --with-isl="${MINGW_OUTPUT_PATH}" \
    --with-gmp="${MINGW_OUTPUT_PATH}" \
    --with-mpfr="${MINGW_OUTPUT_PATH}" \
    --with-mpc="${MINGW_OUTPUT_PATH}" \
    --prefix="${MINGW_OUTPUT_PATH}" \
    ${EXCEPTIONS_ARGS} \
    ${WIN32THREADS_ARGS} \
    || exit 1
  make ${NUMJOBS} || make || exit 1
done
for _target in ${MINGW_TARGETS}; do
  cd $TMP/tmp-$PRGNAM/gcc-build-${_target}
  make install || exit 1
  ${_target}-strip ${MINGW_OUTPUT_PATH}/${_target}/lib/*.dll || true
  strip ${MINGW_OUTPUT_PATH}/bin/${_target}-* || true
  strip ${MINGW_OUTPUT_PATH}/lib/gcc/${_target}/${GCC}/{cc1*,collect2,lto*} || true
  ln -s ${_target}-gcc ${MINGW_OUTPUT_PATH}/bin/${_target}-cc
  # Move dlls to the correct location:
  mkdir -p ${MINGW_OUTPUT_PATH}/${_target}/bin/
  mv ${MINGW_OUTPUT_PATH}/${_target}/lib/*.dll ${MINGW_OUTPUT_PATH}/${_target}/bin/ || true
done
for _binaries in ${MINGW_OUTPUT_PATH}/bin/*; do
  if [[ "$_binaries" != *"eu"* ]]; then
    strip $_binaries || true
  fi
done
# Remove unnecessary files:
rm -rf ${MINGW_OUTPUT_PATH}/share
rm -f ${MINGW_OUTPUT_PATH}/lib/libcc1.*
# Create lto plugin link:
mkdir -p ${MINGW_OUTPUT_PATH}/lib/bfd-plugins
ln -sf ../gcc/${ARCH}-w64-mingw32/${GCC}/liblto_plugin.so ${MINGW_OUTPUT_PATH}/lib/bfd-plugins/liblto_plugin.so

# gcc docs:
echo "-- gcc-docs --"
mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION/gcc
( cd $TMP/tmp-$PRGNAM/gcc-${GCC}
  cp -a $GCC_DOCS $PKG/usr/doc/$PRGNAM-$VERSION/gcc/ || true
)

#
# --- DONE ---
#

# Add profile scripts to modify the $PATH:
mkdir -p $PKG/etc/profile.d
cat $SRCDIR/profile.d/mingw-w64.sh | sed -e "s/@@VERSION@@/$VERSION/" \
  > $PKG/etc/profile.d/mingw-w64.sh
cat $SRCDIR/profile.d/mingw-w64.csh | sed -e "s/@@VERSION@@/$VERSION/" \
  > $PKG/etc/profile.d/mingw-w64.csh
chmod 0755 $PKG/etc/profile.d/mingw-w64.*

# Don't ship .la files:
rm -f $PKG/{,usr/}lib${LIBDIRSUFFIX}/*.la

# Add documentation:
mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cat $SRCDIR/$(basename $0) > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild
chown -R root:root $PKG/usr/doc/$PRGNAM-$VERSION
find $PKG/usr/doc -type f -exec chmod 644 {} \;

# Add a package description:
mkdir -p $PKG/install
cat $SRCDIR/slack-desc > $PKG/install/slack-desc

# Build the package:
cd $PKG
makepkg --linkadd y --chown n $OUTPUT/${PRGNAM}-${VERSION}-${ARCH}-${BUILD}${TAG}.${PKGTYPE:-txz} 2>&1 | tee $OUTPUT/makepkg-${PRGNAM}.log
cd $OUTPUT
md5sum ${PRGNAM}-${VERSION}-${ARCH}-${BUILD}${TAG}.${PKGTYPE:-txz} > ${PRGNAM}-${VERSION}-${ARCH}-${BUILD}${TAG}.${PKGTYPE:-txz}.md5
cd -
cat $PKG/install/slack-desc | grep "^${PRGNAM}" > $OUTPUT/${PRGNAM}-${VERSION}-${ARCH}-${BUILD}${TAG}.txt

# Restore the original umask:
umask ${_UMASK_}