# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=gcc
_mingw_suff=mingw-w64-cross
pkgname=("${_mingw_suff}-${_realname}")
pkgver=9.3.0
pkgrel=1
pkgdesc="Cross GCC for the MinGW-w64"
arch=('i686' 'x86_64')
url="https://gcc.gnu.org"
license=('GPL' 'LGPL' 'FDL' 'custom')
groups=("${_mingw_suff}-toolchain" "${_mingw_suff}")
depends=("zlib" "mpc" "isl"
         "${_mingw_suff}-binutils"
         "${_mingw_suff}-crt"
         "${_mingw_suff}-headers"
         "${_mingw_suff}-winpthreads"
         "${_mingw_suff}-windows-default-manifest")
makedepends=("gcc" 'lndir' "gmp-devel" "mpc-devel" "zlib-devel" "isl-devel")
#checkdepends=('dejagnu')
options=('!strip' 'staticlibs' '!emptydirs' '!buildflags')
source=(https://ftp.gnu.org/gnu/gcc/gcc-${pkgver}/gcc-${pkgver}.tar.gz
        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
        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
        0022-libgomp-soname-cygwin-mingw.patch
        0028-g++-time.patch
        0030-newlib-ftm.patch
        0031-define_std-unix.patch
        0120-Handle-spaces-in-path-for-default-manifest.patch
        0130-libstdc++-in-out.patch
        0140-gcc-8.2.0-diagnostic-color.patch
        0950-9.1.0-configure-msys2.patch
        0951-9.1.0-msys2-spec.patch
        0953-9.1.0-testsuite-msys2.patch)
sha256sums=('5258a9b6afe9463c2e56b9e8355b1a4bee125ca828b8078f910303bc2ef91fa6'
            '24bdd964c6479251af5a275965db42ed867599cd2d84ecb526397395e201cda4'
            '817361b4b301ca3f578d0d24b899691273a4ca9e20a91790a74efddc91c1130e'
            '7ad412357429a2e2376ec673d0e0a0d2ab4e3a162db532818bc76d55835f35d6'
            'cf33d6ff5e32ed35146117c9bd3d78bb9bf228abad73e5c2b44f006bf8cf0fc8'
            '4922110c9bb7959a453d62e0107aee7c58c1e146540f930f55466c72f24f72a7'
            '84c8127f2e70f26a6bf0f7ff1fe06df4814096bc02f5fc19291a729c740887d7'
            '4d3030238181a6d28c1aff5e5b43db6d82517f3412309175e162f5da63f7cd00'
            '0e6a373a0911d81656d9bfd2d93085b817266538600eab14965f1bafb034bac8'
            'a277245cf7ccf6e3bd83a79945ff26ea5e41fb3ecf7fe0a473b2ec64d8c5b101'
            '00e528e198183799c00167cacc1a7b1cb9505ae93ef89e581632ffaae20080f2'
            '041ec7903f3285d28a4c2949fbda7b830e9e32c7883de3649c53bf595df037a0'
            '081457942bab05b4583718dd9bef50cb42032c43a8ded6ad633d6993e4dd05c1'
            'ebd467d7cf1997d8feecb053ed83d93e199a39a0af3f01b2e343f8e23a654630'
            'e427297a118c47d2e71cb05dd9a496e9ada497917721c16918548ee2f13ad709'
            'a526cfab91a1499be92c824c7f2185a013a38dbdfbeabe63ecbe84451e07b021'
            'e467f0ac68b349de826c79b00a45c5ad9e7c5a55d06b9b9fa7afd94c597f6376'
            'd3047e58c944746a4af9da942d5145c0b5e56b6853a9355cad2441dc51930de6'
            '259d025fec7b527a1044c798051e2d074088eca4b382eebff433d6deecabd282'
            '6f62bf968154323ef531716e086fe6132e5d3d7fc496e7159e3c59e436658bff')

_threads="win32"
_targets="i686-w64-mingw32 x86_64-w64-mingw32" #armv7-w64-mingw32 

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

apply_patch_p2_with_msg() {
  for _fname in "$@"
  do
    msg2 "Applying ${_fname}"
    patch -Nbp2 -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}/${_realname}-${pkgver}

  #do not install libiberty
  sed -i 's/install_to_$(INSTALL_DEST) //' libiberty/Makefile.in
  # hack! - some configure tests for header files using "$CPP $CPPFLAGS"
  #sed -i "/ac_cpp=/s/\$CPPFLAGS/\$CPPFLAGS -O2/" {libiberty,gcc}/configure

  # 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 \
    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 \
    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 \
    0022-libgomp-soname-cygwin-mingw.patch \
    0028-g++-time.patch \
    0030-newlib-ftm.patch \
    0031-define_std-unix.patch

  # MINGW patches
  apply_patch_with_msg \
    0120-Handle-spaces-in-path-for-default-manifest.patch \
    0130-libstdc++-in-out.patch \
    0140-gcc-8.2.0-diagnostic-color.patch

  # MSYS2 Patches
  apply_patch_with_msg \
    0950-9.1.0-configure-msys2.patch \
    0951-9.1.0-msys2-spec.patch \
    0953-9.1.0-testsuite-msys2.patch
}

build() {
  export lt_cv_deplibs_check_method='pass_all'
  for _target in ${_targets}; do
    mkdir -p ${srcdir}/gcc-build-${_target} && cd ${srcdir}/gcc-build-${_target}

    local _langs="c,lto,c++"
    case "${_target}" in
      i686*)
        local _conf="--disable-sjlj-exceptions --with-dwarf2"
        local _arch=i686
        _langs="${_langs} fortran"
      ;;

      x86_64*)
        local _arch=x86-64
        local _conf=""
        _langs="${_langs} fortran"
      ;;
    esac

    # use built-in SSP with Cygwin 2.10
    # FIXME: --disable-libssp should suffice in GCC 8
    export gcc_cv_libc_provides_ssp=yes
    # configure tries to test SUSv4-compliant behaviour of
    # realpath(..., NULL) via _XOPEN_VERSION
    export glibcxx_cv_realpath=yes
    ../${_realname}-${pkgver}/configure \
      --prefix=/opt \
      --build=${CHOST} \
      --host=${CHOST} \
      --target=${_target} \
      --libexecdir=/opt/lib \
      --disable-bootstrap \
      --with-arch=${_arch} \
      --with-tune=generic \
      --enable-languages=${_langs} \
      --enable-shared --enable-static \
      --enable-threads=${_threads} \
      --enable-graphite \
      --enable-fully-dynamic-string \
      --enable-libstdcxx-time=yes \
      --disable-libstdcxx-pch \
      --disable-libstdcxx-debug \
      --enable-cloog-backend=isl \
      --enable-version-specific-runtime-libs \
      --disable-isl-version-check \
      --enable-lto \
      --enable-libgomp \
      --disable-multilib \
      --enable-checking=release \
      --disable-rpath \
      --disable-win32-registry \
      --disable-werror \
      --disable-symvers \
      --with-libiconv \
      --with-system-zlib \
      --with-{gmp,mpfr,mpc,isl}=/usr \
      --with-gnu-as --with-gnu-ld \
      ${_conf} \
      CFLAGS="-O2 -pipe" \
      CXXFALGS="-O2 -pipe"
    
    make all
  done
}

package() {
  for _target in ${_targets}; do
    cd ${srcdir}/gcc-build-${_target}
    make -j1 DESTDIR=${pkgdir} install
    mv ${pkgdir}/opt/lib/gcc/${_target}/lib/libgcc_s.a ${pkgdir}/opt/lib/gcc/${_target}/${pkgver}/
    mv ${pkgdir}/opt/lib/gcc/${_target}/libgcc*.dll ${pkgdir}/opt/lib/gcc/${_target}/${pkgver}/
    for _f in ${pkgdir}/opt/bin/*.exe; do
      strip -s ${_f}
    done
    for _f in ${pkgdir}/opt/lib/gcc/${_target}/${pkgver}/*.exe; do
      strip -s ${_f}
    done
    rm -rf ${pkgdir}/opt/share
  done
}
