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

_realname=gcc
_mingw_suff=mingw-w64-cross
pkgbase="${_mingw_suff}-${_realname}"
_targetpkgs=("${_mingw_suff}-ucrt64-${_realname}" "${_mingw_suff}-mingw32-${_realname}" "${_mingw_suff}-mingw64-${_realname}")
pkgname=("${_mingw_suff}-${_realname}" "${_targetpkgs[@]}")
pkgver=15.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")
makedepends=("gcc" 'lndir' "gmp-devel" "mpc-devel" "zlib-devel" "isl-devel" 'autotools' 'gperf' 'libzstd-devel'
             "${_mingw_suff}-crt" "${_mingw_suff}-headers" "${_mingw_suff}-winpthreads"
             "${_mingw_suff}-windows-default-manifest" "${_mingw_suff}-binutils")
#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=('7294d65cc1a0558cb815af0ca8c7763d86f7a31199794ede3f630c0d1b0a5723'
            'bc788aa466a83184d285cc2f6c1ffc40c6ed416dd08c6999015262a53f1ab1b5'
            '704acfaeb11d24d3fe5aab34bc883c184ca93aff03d752016c9a50fdd82c5655'
            'c5676fd62d5f7f69be26062b95d42ef47f28151af83b83efa3998ecd8e939e19'
            '03ecf29419119286ebcef35864f9bd26d3bf3eaa355945856953204ed727e173'
            '06214859ae4030ce30bb68aa35aa4a9ac92d1e79e34133089dcbfeb7cc8066e8'
            'a843c038042811265d6ce5917b0c76b709e2591822e5cab53a32b7e9c806da29'
            'e27f82af1eb7a4aad955ec36d5fd7d9a4c24575845bcf718372df9bc2b099eb6'
            'de5692673731f90b888bf301a547481ab8e3cc0029713b6fd5abaabd860250f0'
            'a40e7025507130a2a5d2eb2eea8ba4b053398c307cd55b3c9421a8507bd315ed'
            '2658eb376f7829179963978b69a048ed105a41508adc55d0fc0d607c14181926'
            '83b6aea4a462ae80121fd68d42c6234d02e20865132197a10575bbf95fd33b7e'
            'c6763a1558a8f273868a664133806cfe155ae3312eb1b6df74a883fcde711d8c'
            '3707b0aab99b203cbd9e513be46c7d4600de06e6c8344160b7fb1779061d08da'
            'e98805ead7d78ee2a92f237894c4b2b7ddc1688e1b517d8c04f28d440202e40f'
            'fd9bdecb2bbc4796bbc9f00b708dac42ef9e3464a06d6d27e5475cee117de5be'
            'ad1f7b5e7afaaec008b7cbd14feea13a10989fa91bda7003af72d457619bb199'
            '1484911163634f30324827619c873a6267b377abba0df8bbedfd128163c53ea4')

_threads="win32"

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() {
  _target=$1
  export lt_cv_deplibs_check_method='pass_all'

  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


  ../${_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-backtrace=yes \
    --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
}

_package() {
  _target=$1
  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
}

build() {
  for _pkg in "${_targetpkgs[@]}"; do
    case "$_pkg" in
      "${_mingw_suff}-ucrt64-${_realname}") _build "x86_64-w64-mingw32ucrt" ;;
      "${_mingw_suff}-mingw32-${_realname}") _build "i686-w64-mingw32" ;;
      "${_mingw_suff}-mingw64-${_realname}") _build "x86_64-w64-mingw32" ;;
    esac
  done
}

package_mingw-w64-cross-gcc() {
  pkgdesc+=" (meta package, for backward compatibility)"
  depends=("${_targetpkgs[@]}")
}

package_mingw-w64-cross-ucrt64-gcc() {
  conflicts=("${_mingw_suff}-${_realname}<=14.2.0-1")
  depends+=(
    "${_mingw_suff}-ucrt64-crt"
    "${_mingw_suff}-ucrt64-headers"
    "${_mingw_suff}-ucrt64-winpthreads"
    "${_mingw_suff}-ucrt64-windows-default-manifest"
    "${_mingw_suff}-ucrt64-binutils"
  )
  _package "x86_64-w64-mingw32ucrt"
}

package_mingw-w64-cross-mingw32-gcc() {
  conflicts=("${_mingw_suff}-${_realname}<=14.2.0-1")
  depends+=(
    "${_mingw_suff}-mingw32-crt"
    "${_mingw_suff}-mingw32-headers"
    "${_mingw_suff}-mingw32-winpthreads"
    "${_mingw_suff}-mingw32-windows-default-manifest"
    "${_mingw_suff}-mingw32-binutils"
  )
  _package "i686-w64-mingw32"
}

package_mingw-w64-cross-mingw64-gcc() {
  conflicts=("${_mingw_suff}-${_realname}<=14.2.0-1")
  depends+=(
    "${_mingw_suff}-mingw64-crt"
    "${_mingw_suff}-mingw64-headers"
    "${_mingw_suff}-mingw64-winpthreads"
    "${_mingw_suff}-mingw64-windows-default-manifest"
    "${_mingw_suff}-mingw64-binutils"
  )
  _package "x86_64-w64-mingw32"
}
