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

_realname=gcc
_mingw_suff=mingw-w64-cross
pkgname=("${_mingw_suff}-${_realname}")
pkgver=14.2.0
pkgrel=1
pkgdesc="Cross GCC for the MinGW-w64"
arch=('i686' 'x86_64')
url="https://gcc.gnu.org"
license=('GPL' 'LGPL' 'FDL' 'custom')
msys2_references=(
  'archlinux: gcc'
  'cpe: cpe:/a:gnu:gcc'
)
groups=("${_mingw_suff}-toolchain" "${_mingw_suff}")
depends=("zlib" "mpc" "isl" "libzstd"
         "${_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" 'autotools' 'gperf' 'libzstd-devel')
#checkdepends=('dejagnu')
options=('!strip' 'staticlibs' '!emptydirs' '!buildflags')
source=(https://ftp.gnu.org/gnu/gcc/gcc-${pkgver}/gcc-${pkgver}.tar.gz
        0001-Cygwin-use-SysV-ABI-on-x86_64.patch
        0002-Cygwin-add-dummy-pthread-tsaware-and-large-address-a.patch
        0003-Cygwin-handle-dllimport-properly-in-medium-model-V2.patch
        0004-Cygwin-MinGW-skip-test.patch
        0005-Cygwin-define-RTS_CONTROL_ENABLE-and-DTR_CONTROL_ENA.patch
        0006-Cygwin-fix-some-implicit-declaration-warnings-and-re.patch
        0007-Cygwin-__cxa-atexit.patch
        0008-Cygwin-libgomp-soname.patch
        0009-Cygwin-g-time.patch
        0010-Cygwin-newlib-ftm.patch
        0011-Cygwin-define-STD_UNIX.patch
        0101-Cygwin-enable-libgccjit-not-just-for-MingW.patch
        0102-Cygwin-testsuite-fixes-for-libgccjit.patch
        0012-Handle-spaces-in-path-for-default-manifest.patch
        0014-gcc-9-branch-clone_function_name_1-Retain-any-stdcall-suffix.patch
        0020-libgomp-Don-t-hard-code-MS-printf-attributes.patch
        0200-add-m-no-align-vector-insn-option-for-i386.patch)
sha256sums=('7d376d445f93126dc545e2c0086d0f647c3094aae081cdb78f42ce2bc25e7293'
            'bc788aa466a83184d285cc2f6c1ffc40c6ed416dd08c6999015262a53f1ab1b5'
            '704acfaeb11d24d3fe5aab34bc883c184ca93aff03d752016c9a50fdd82c5655'
            'c5676fd62d5f7f69be26062b95d42ef47f28151af83b83efa3998ecd8e939e19'
            '03ecf29419119286ebcef35864f9bd26d3bf3eaa355945856953204ed727e173'
            '06214859ae4030ce30bb68aa35aa4a9ac92d1e79e34133089dcbfeb7cc8066e8'
            'a843c038042811265d6ce5917b0c76b709e2591822e5cab53a32b7e9c806da29'
            'e27f82af1eb7a4aad955ec36d5fd7d9a4c24575845bcf718372df9bc2b099eb6'
            'de5692673731f90b888bf301a547481ab8e3cc0029713b6fd5abaabd860250f0'
            'a40e7025507130a2a5d2eb2eea8ba4b053398c307cd55b3c9421a8507bd315ed'
            '2658eb376f7829179963978b69a048ed105a41508adc55d0fc0d607c14181926'
            '83b6aea4a462ae80121fd68d42c6234d02e20865132197a10575bbf95fd33b7e'
            'c5aeab9609f90a8430c43bb50172b63c3155eb10082369f81fac58a395d2e2ee'
            '3707b0aab99b203cbd9e513be46c7d4600de06e6c8344160b7fb1779061d08da'
            'e98805ead7d78ee2a92f237894c4b2b7ddc1688e1b517d8c04f28d440202e40f'
            'fd9bdecb2bbc4796bbc9f00b708dac42ef9e3464a06d6d27e5475cee117de5be'
            'ad1f7b5e7afaaec008b7cbd14feea13a10989fa91bda7003af72d457619bb199'
            'c34f9e71b5a092be1987ad4c65891742c74c9eb8ef6560100e751cd31375f579')

_threads="win32"
_targets="x86_64-w64-mingw32ucrt i686-w64-mingw32 x86_64-w64-mingw32"

apply_patch_with_msg() {
  for _fname in "$@"
  do
    msg2 "Applying ${_fname}"
    patch -Nbp1 -i "${srcdir}"/${_fname}
  done
}

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}

  # Cygwin patches
  apply_patch_with_msg \
    0001-Cygwin-use-SysV-ABI-on-x86_64.patch \
    0002-Cygwin-add-dummy-pthread-tsaware-and-large-address-a.patch \
    0003-Cygwin-handle-dllimport-properly-in-medium-model-V2.patch \
    0004-Cygwin-MinGW-skip-test.patch \
    0005-Cygwin-define-RTS_CONTROL_ENABLE-and-DTR_CONTROL_ENA.patch \
    0006-Cygwin-fix-some-implicit-declaration-warnings-and-re.patch \
    0007-Cygwin-__cxa-atexit.patch \
    0008-Cygwin-libgomp-soname.patch \
    0009-Cygwin-g-time.patch \
    0010-Cygwin-newlib-ftm.patch \
    0011-Cygwin-define-STD_UNIX.patch \
    0101-Cygwin-enable-libgccjit-not-just-for-MingW.patch \
    0102-Cygwin-testsuite-fixes-for-libgccjit.patch

  # MinGW Patches
  apply_patch_with_msg \
    0012-Handle-spaces-in-path-for-default-manifest.patch \
    0014-gcc-9-branch-clone_function_name_1-Retain-any-stdcall-suffix.patch \
    0020-libgomp-Don-t-hard-code-MS-printf-attributes.patch \
    0200-add-m-no-align-vector-insn-option-for-i386.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=pentium4
        _langs="${_langs} fortran"
      ;;

      x86_64*)
        local _arch=nocona
        local _conf=""
        _langs="${_langs} fortran"
      ;;
    esac

    local CYGWIN_CHOST="${CHOST/-msys/-cygwin}"

    ../${_realname}-${pkgver}/configure \
      --prefix=/opt \
      --build=${CYGWIN_CHOST} \
      --host=${CYGWIN_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-filesystem-ts \
      --enable-libstdcxx-time \
      --disable-libstdcxx-pch \
      --disable-libstdcxx-debug \
      --enable-cloog-backend=isl \
      --enable-version-specific-runtime-libs \
      --disable-isl-version-check \
      --enable-lto \
      --enable-libgomp \
      --disable-libssp \
      --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" \
      CXXFLAGS="-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
}
