# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Martell Malone <martellmalone@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

# toolchain build order: win32-api-headers->msys2-runtime->binutils->gcc->binutils->msys2-runtime
# NOTE: libtool requires rebuilt with each new gcc version

pkgbase=gcc
pkgname=('gcc' 'gcc-libs' 'gcc-fortran')
pkgver=7.3.0
pkgrel=3
pkgdesc="The GNU Compiler Collection"
arch=('i686' 'x86_64')
groups=('msys2-devel')
license=('GPL' 'LGPL' 'FDL' 'custom')
url="https://gcc.gnu.org/"
makedepends=('binutils' 'mpc-devel' 'gmp-devel'
             'mpfr-devel' 'isl-devel' 'zlib-devel'
             'windows-default-manifest')
STRIP_OPTION="strip"
#STRIP_OPTION="!strip"
#DEBUG_OPTION="debug"
DEBUG_OPTION="!debug"
options=('!emptydirs' "$STRIP_OPTION" "$DEBUG_OPTION")
source=(https://ftp.gnu.org/gnu/gcc/gcc-${pkgver}/gcc-${pkgver}.tar.gz
        0001-share-mingw-fset-stack-executable-with-cygwin.patch
        0007-Avoid-installing-libffi-V2.patch
        0009-Cygwin-uses-sysv-ABI-on-x86_64-V2.patch
        0010-Do-not-version-lto-plugin-on-cygwin-mingw.patch
        0011-add-dummy-pthread-tsaware-and-large-address-aware-fo.patch
        0012-handle-dllimport-properly-in-medium-model-V2.patch
        0013-skip-test-for-cygwin-mingw.patch
        0014-64bit-Cygwin-uses-SEH.patch
        0015-define-RTS_CONTROL_ENABLE-and-DTR_CONTROL_ENABLE-for.patch
        0016-fix-some-implicit-declaration-warnings.patch
        0017-__cxa-atexit-for-Cygwin.patch
        0018-prevent-modules-from-being-unloaded-before-their-dto.patch
        0020-cygwin-uses-cyg-lib-prefix-v3.patch
        0022-libgomp-soname-cygwin-mingw.patch
        0024-libitm-weak-symbols.patch
        0025-enable-libcilkrts-V2.patch
        0028-g++-time.patch
        0029-gcc-specs.patch
        0030-newlib-ftm.patch
        0031-define_std-unix.patch
        0130-libstdc++-in-out.patch
        0950-7.3.0-configure-msys2.patch
        0951-7.3.0-msys2-spec.patch
        0953-7.3.0-testsuite-msys2.patch
        0955-4.9.2-apply-hack-so-gcc_s-isnt-stripped.patch
        pr86138.patch)
sha256sums=('fa06e455ca198ddc11ea4ddf2a394cf7cfb66aa7e0ab98cc1184189f1d405870'
            'e2583724bbc9ac16a5a3927b79a05c006d43fd70fe0e598b5d402fb66080e308'
            '25ba33a68cefd1921d35b796a2dc033bae22b33527b8b81b04dddb3e0dee5920'
            '24bdd964c6479251af5a275965db42ed867599cd2d84ecb526397395e201cda4'
            'b77b6045aabba3aaae58d578d0b2ffc56ce70907f39a280fa964c9fcbd88bdad'
            '7ad412357429a2e2376ec673d0e0a0d2ab4e3a162db532818bc76d55835f35d6'
            'cf33d6ff5e32ed35146117c9bd3d78bb9bf228abad73e5c2b44f006bf8cf0fc8'
            '79db6a8951c3c07d9df26863a2bcfc607606211f6eb813c156b4f46123ecbbdc'
            'ac7e9e08ea8b2b4b5fdb394e217ce9081352fe07746e19b9374a753ebf89a4a2'
            '84c8127f2e70f26a6bf0f7ff1fe06df4814096bc02f5fc19291a729c740887d7'
            '4d3030238181a6d28c1aff5e5b43db6d82517f3412309175e162f5da63f7cd00'
            '0e6a373a0911d81656d9bfd2d93085b817266538600eab14965f1bafb034bac8'
            'a277245cf7ccf6e3bd83a79945ff26ea5e41fb3ecf7fe0a473b2ec64d8c5b101'
            '277907dd3982c61dcd8d739231e49afebf3841cf687fc1fcf3e2ffbba419c904'
            '00e528e198183799c00167cacc1a7b1cb9505ae93ef89e581632ffaae20080f2'
            '9daf3accc664d5a2b6f93354a1e8807d19c640e627dc2977111aab17b766542e'
            '5ac69b7933d576133618c4367fa54faf89e11231228f54c9ab8ce4c5fb221a49'
            '77f1ff3c35cf54c0698e80156a0b4ee0f6f87892e5b45bdafb36096c61e87fdf'
            '5c981e03ee5f34129ab06e3e4aab0bd190bf9d454d98513ac68b39e01f0b039d'
            'e1b56aa9c8786b5491ec3f6522498f4381fe78c6117b87ae0daeb7741e111a98'
            'e767610ddf54d2f277fef89e355728f31dbe9fe920eccd082942fbadfd3c1409'
            '379e965989618e380565c19f49a10c011e2480190e4f4dfb1bf27be4cc933a87'
            'd075205468c4518d7eb5d38c6c95c4bcc07b9788521cce41467b7396f44cfcb8'
            'e41e326ecfb555f527c769599537bfe312c2a7cfb899519620bfe873c228f0cb'
            '07e82b66cc4ef6f3c711525f9d91037777cad8a9f81ccf9dcecbb56193401eba'
            '89e00457776fb1cae1b020c6f835012a8617996a0d0710a4b12e9804aeda27df'
            'c2126ca50487d84fa9aaf0a64bc03df8dbc0bccc2dd5be1b5f1e277c9ad7c0aa')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _fname in "$@"
  do
    msg2 "Applying ${_fname}"
    patch -Nbp1 -i "${srcdir}"/${_fname}
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ] || [ -d $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #

prepare() {
  cd ${srcdir}/gcc-${pkgver}
  
  # Remove files created by patches
  rm -f config/mt-cygwin \
        gcc/ada/system-cygwin-x86_64.ads \
        libgomp/config/cygwin/plugin-suffix.h \
        libgcc/config/i386/t-msys \
        gcc/config/i386/msys.h \
        gcc/config/i386/msys-w64.h \
        gcc/config/i386/t-msys-w64 > /dev/null 2>&1 | true

  # Cygwin patches
  apply_patch_with_msg \
     0001-share-mingw-fset-stack-executable-with-cygwin.patch \
     0009-Cygwin-uses-sysv-ABI-on-x86_64-V2.patch \
     0010-Do-not-version-lto-plugin-on-cygwin-mingw.patch \
     0011-add-dummy-pthread-tsaware-and-large-address-aware-fo.patch \
     0012-handle-dllimport-properly-in-medium-model-V2.patch \
     0013-skip-test-for-cygwin-mingw.patch \
     0014-64bit-Cygwin-uses-SEH.patch \
     0015-define-RTS_CONTROL_ENABLE-and-DTR_CONTROL_ENABLE-for.patch \
     0016-fix-some-implicit-declaration-warnings.patch \
     0017-__cxa-atexit-for-Cygwin.patch \
     0018-prevent-modules-from-being-unloaded-before-their-dto.patch \
     0020-cygwin-uses-cyg-lib-prefix-v3.patch \
     0022-libgomp-soname-cygwin-mingw.patch \
     0024-libitm-weak-symbols.patch \
     0025-enable-libcilkrts-V2.patch
#     0029-gcc-specs.patch

  patch -Np2 -i ${srcdir}/0028-g++-time.patch
  patch -Np2 -i ${srcdir}/0030-newlib-ftm.patch
  patch -Np2 -i ${srcdir}/0031-define_std-unix.patch
  patch -Np1 -i ${srcdir}/pr86138.patch
  # MINGW Patches
  apply_patch_with_msg 0130-libstdc++-in-out.patch
  
  # MSYS2 Patches
  apply_patch_with_msg \
    0950-7.3.0-configure-msys2.patch \
    0951-7.3.0-msys2-spec.patch \
    0953-7.3.0-testsuite-msys2.patch \
    0955-4.9.2-apply-hack-so-gcc_s-isnt-stripped.patch
  
  # Do not run fixincludes
  #sed -i 's@\./fixinc\.sh@-c true@' gcc/Makefile.in
  echo ${pkgver} > gcc/BASE-VER
  # hack! - some configure tests for header files using "$CPP $CPPFLAGS"
  sed -i "/ac_cpp=/s/\$CPPFLAGS/\$CPPFLAGS -O2/" {libiberty,gcc}/configure
  
  [[ -d ${srcdir}/build-$(arch) ]] && rm -rf ${srcdir}/build-$(arch)
  mkdir ${srcdir}/build-$(arch)
}

build() {
  cd ${srcdir}/build-$(arch)

  case ${CARCH} in
    i686)
      _arch=i686
      _arch_conf="--disable-sjlj-exceptions"
    ;;
    x86_64)
      _arch=x86-64
      _arch_conf=
    ;;
  esac

  # libgcc is built with -g0, but INHIBIT_LIBC_CFLAGS can
  # be used to undo that.
  if check_option "debug" "y"; then
    export INHIBIT_LIBC_CFLAGS="-g2"
  fi

  # using -pipe causes spurious test-suite failures
  # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=48565
  CFLAGS=${CFLAGS/-pipe/}
  CXXFLAGS=${CXXFLAGS/-pipe/}
  
  # use built-in SSP with Cygwin 2.10
  # FIXME: --disable-libssp should suffice in GCC 8
  export gcc_cv_libc_provides_ssp=yes
  # libssp: conflicts with builtin SSP

  ${srcdir}/gcc-${pkgver}/configure \
    --build=${CHOST} \
    --prefix=/usr \
    --libexecdir=/usr/lib \
    --enable-bootstrap \
    --enable-shared --enable-shared-libgcc --enable-static \
    --enable-version-specific-runtime-libs \
    --with-arch=${_arch} \
    --with-tune=generic \
    --disable-multilib \
    ${_arch_conf} \
    --enable-__cxa_atexit \
    --with-dwarf2 \
    --enable-languages=c,c++,fortran,lto \
    --enable-graphite \
    --enable-threads=posix \
    --enable-libatomic \
    --enable-libcilkrts \
    --enable-libgomp \
    --enable-libitm \
    --enable-libquadmath \
    --enable-libquadmath-support \
    --disable-libssp \
    --disable-win32-registry \
    --disable-symvers \
    --with-gnu-ld \
    --with-gnu-as \
    --disable-isl-version-check \
    --enable-checking=release \
    --without-libiconv-prefix \
    --without-libintl-prefix \
    --with-system-zlib \
    --enable-linker-build-id \
    --with-default-libstdcxx-abi=gcc4-compatible

  make

  # make documentation
  #cd $CHOST/libstdc++-v3
  #make doc-man-doxygen
}

#check() {
#  cd ${srcdir}/build-$(arch)

#  # do not abort on error as some are "expected"
#  make -k check || true
#  ${srcdir}/gcc-${pkgver}/contrib/test_summary
#}

package_gcc-libs()
{
  pkgdesc="Runtime libraries shipped by GCC"
  groups=('base')
  depends=('msys2-runtime')
  install=gcc-libs.install

  cd ${srcdir}/build-$(arch)
  make -j1 -C $CHOST/libgcc DESTDIR=${pkgdir} install-shared
  for lib in libgomp libstdc++-v3/src; do
    make -j1 -C $CHOST/$lib DESTDIR=${pkgdir} install-toolexeclibLTLIBRARIES
  done

  make -j1 -C $CHOST/libstdc++-v3/po DESTDIR=${pkgdir} install
  make -j1 -C $CHOST/libgomp DESTDIR=${pkgdir} install-info
  make -j1 -C $CHOST/libquadmath DESTDIR=${pkgdir} install-info

  make -j1 DESTDIR=${pkgdir} install-target-libquadmath
  make -j1 DESTDIR=${pkgdir} install-target-libgfortran
  #make -j1 DESTDIR=${pkgdir} install-target-libssp
  make -j1 DESTDIR=${pkgdir} install-target-libatomic
  make -j1 DESTDIR=${pkgdir} install-target-libcilkrts
  make -j1 DESTDIR=${pkgdir} install-target-libvtv

  rm -f ${pkgdir}/*.dll
  rm -rf ${pkgdir}/usr/lib/
  rm -rf ${pkgdir}/usr/share/gcc-${pkgver}

  # remove static libraries
  #find ${pkgdir} -name *.a -delete

  # Install Runtime Library Exception
  install -Dm644 ${srcdir}/gcc-${pkgver}/COPYING.RUNTIME \
    ${pkgdir}/usr/share/licenses/gcc-libs/RUNTIME.LIBRARY.EXCEPTION
}

package_gcc()
{
  pkgdesc="The GNU Compiler Collection - C and C++ frontends"
  depends=("gcc-libs=${pkgver}-${pkgrel}" 'binutils' 'gmp' 'isl' 'mpc' 'mpfr' 'msys2-runtime-devel' 'msys2-w32api-headers' 'msys2-w32api-runtime' 'windows-default-manifest')
  options=('staticlibs' "$STRIP_OPTION" "$DEBUG_OPTION")
  install=gcc.install

  cd ${srcdir}/build-$(arch)

  if check_option "strip" "n"; then
    export STRIP=true
  fi

  make -j1 DESTDIR=${pkgdir} install
  if [ "$CARCH" = "x86_64" ]
  then
    mv ${pkgdir}/usr/lib/gcc/${CHOST}/lib/libgcc_s.dll.a ${pkgdir}/usr/lib/gcc/${CHOST}/${pkgver/-*/.*}/
    rm -rf ${pkgdir}/usr/lib/gcc/${CHOST}/lib
  fi
  # install -d ${pkgdir}/usr/share/gdb/auto-load/usr/lib
  # mv $pkg{pkgdir}dir{,/usr/share/gdb/auto-load}/usr/lib/libstdc++.so.6.0.18-gdb.py

  # unfortunately it is much, much easier to install the lot and clean-up the mess...
  rm ${pkgdir}/usr/bin/{$CHOST-,}gfortran
  # remove all DLLs
  rm -f ${pkgdir}/usr/bin/*.dll

  rm -f ${pkgdir}/usr/lib/libiberty.a
  # rm -f ${pkgdir}/usr/lib/gcc/${CHOST}/${pkgver}/libatomic{.dll,}.a
  rm -f ${pkgdir}/usr/lib/gcc/${CHOST}/${pkgver}/libgfortran{.dll,}.a
  rm -f ${pkgdir}/usr/lib/gcc/${CHOST}/${pkgver}/libgfortran.spec
  rm -r ${pkgdir}/usr/lib/gcc/$CHOST/${pkgver}/finclude
  rm -f ${pkgdir}/usr/lib/gcc/$CHOST/${pkgver}/f951.exe
  rm -f ${pkgdir}/usr/lib/gcc/$CHOST/${pkgver}/{libcaf_single,libgfortranbegin}.a
  rm -f ${pkgdir}/usr/share/info/{gfortran,libgomp,libquadmath}.info
  rm -f ${pkgdir}/usr/share/locale/{de,fr}/LC_MESSAGES/libstdc++.mo
  rm -f ${pkgdir}/usr/share/man/man1/gfortran.1

  # remove static libraries - note libstdc++.a is needed for the binutils and glibc testsuite
  #rm ${pkgdir}/usr/lib/gcc/${CHOST}/${pkgver}/lib{gomp,quadmath}{.dll,}.a

  # many packages expect this symlinks
  ln -s gcc ${pkgdir}/usr/bin/cc
  cp -f ${pkgdir}/usr/bin/cpp.exe ${pkgdir}/usr/lib/cpp

  # POSIX conformance launcher scripts for c89 and c99
  cat > $pkgdir/usr/bin/c89 <<"EOF"
#!/bin/sh
fl="-std=c89"
for opt; do
  case "\$opt" in
    -ansi|-std=c89|-std=iso9899:1990) fl="";;
    -std=*) echo "`basename \$0` called with non ANSI/ISO C option \$opt" >&2
	    exit 1;;
  esac
done
exec /usr/bin/gcc $fl ${1+"$@"}
EOF

  cat > ${pkgdir}/usr/bin/c99 <<"EOF"
#!/bin/sh
fl="-std=c99"
for opt; do
  case "\$opt" in
    -std=c99|-std=iso9899:1999) fl="";;
    -std=*) echo "`basename \$0` called with non ISO C99 option \$opt" >&2
	    exit 1;;
  esac
done
exec /usr/bin/gcc $fl ${1+"$@"}
EOF

  chmod 755 ${pkgdir}/usr/bin/c{8,9}9

  # install the libstdc++ man pages
  # install -dm755 ${pkgdir}/usr/share/man/man3
  # install -m644 -t ${pkgdir}/usr/share/man/man3 \
  #  ${CHOST}/libstdc++-v3/doc/doxygen/man/man3/*.3

  # Install Runtime Library Exception
  install -Dm644 ${srcdir}/gcc-${pkgver}/COPYING.RUNTIME \
    ${pkgdir}/usr/share/licenses/gcc/RUNTIME.LIBRARY.EXCEPTION
}

package_gcc-fortran()
{
  pkgdesc="Fortran front-end for GCC"
  depends=("gcc=$pkgver-$pkgrel")
  options=('staticlibs' '!emptydirs' "$STRIP_OPTION" "$DEBUG_OPTION")
  install=gcc-fortran.install

  cd ${srcdir}/build-$(arch)

  if check_option "strip" "n"; then
    export STRIP=true
  fi

  make -j1 DESTDIR=${pkgdir} install-target-libgfortran
  make -j1 -C $CHOST/libgomp DESTDIR=${pkgdir} install-nodist_fincludeHEADERS
  make -j1 -C gcc DESTDIR=${pkgdir} fortran.install-{common,man,info}
  install -Dm755 gcc/f951 ${pkgdir}/usr/lib/gcc/$CHOST/$pkgver/f951

  ln -s gfortran ${pkgdir}/usr/bin/f95

  rm -f ${pkgdir}/usr/lib/gcc/${CHOST}/lib/libgcc_s.dll.a
  # remove files included in gcc-libs or gcc and unnneeded static lib
  rm ${pkgdir}/usr/bin/*.dll
  rm ${pkgdir}/usr/lib/gcc/$CHOST/${pkgver}/libquadmath.{dll.a,a}
  rm ${pkgdir}/usr/lib/gcc/$CHOST/${pkgver}/{*.o,libgc*}
  rm ${pkgdir}/usr/share/info/libquadmath.info
  rm -r ${pkgdir}/usr/lib/gcc/$CHOST/${pkgver}/include
  # rm ${pkgdir}/usr/lib/gcc/$CHOST/${pkgver}/libgfortran.a

  # Install Runtime Library Exception
  install -Dm644 ${srcdir}/gcc-${pkgver}/COPYING.RUNTIME \
    ${pkgdir}/usr/share/licenses/gcc-fortran/RUNTIME.LIBRARY.EXCEPTION
}
