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

_realname=gcc
_mingw_suff=mingw-w64-cross
pkgname=("${_mingw_suff}-${_realname}")
pkgver=10.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')
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
        0001-Cygwin-avoid-installing-libffi-V2.patch
        0002-Cygwin-use-SysV-ABI-on-x86_64.patch
        0003-Cygwin-MinGW-Do-not-version-lto-plugins.patch
        0004-Cygwin-add-dummy-pthread-tsaware-and-large-address-a.patch
        0005-Cygwin-handle-dllimport-properly-in-medium-model-V2.patch
        0006-Cygwin-MinGW-skip-test.patch
        0007-Cygwin-define-RTS_CONTROL_ENABLE-and-DTR_CONTROL_ENA.patch
        0008-Cygwin-fix-some-implicit-declaration-warnings.patch
        0009-Cygwin-__cxa-atexit.patch
        0010-Cygwin-prevent-modules-from-being-unloaded-before-th.patch
        0011-Cygwin-libgomp-soname.patch
        0012-Cygwin-g-time.patch
        0013-Cygwin-newlib-ftm.patch
        0014-Cygwin-define-STD_UNIX.patch
        0015-libstdc-use-lt_host_flags-for-libstdc-.la.patch
        0016-libstdc-regenerate-src-Makefile.in-for-lt_host_flags.patch
        0017-libstdc-use-a-link-test-to-test-for-Wl-z-relro.patch
        0018-libstdc-regenerate-configure.patch
        0130-libstdc++-in-out.patch
        0140-gcc-8.2.0-diagnostic-color.patch
        0150-Handle-spaces-in-path-for-default-manifest.patch
        0151-makedeps-properly-handle-win-paths.patch
        0152-gcc-9-branch-clone_function_name_1-Retain-any-stdcall-suffix.patch
        0153-libgomp-Don-t-hard-code-MS-printf-attributes.patch
        0154-Windows-New-feature-to-allow-overriding.patch
        0155-gcc-config-i386-mingw32.h-Ensure-lmsvcrt-precede-lke.patch
        0950-10.2.0-configure-msys2.patch
        0951-10.2.0-msys2-spec.patch
        0953-10.2.0-testsuite-msys2.patch)
sha256sums=('27e879dccc639cd7b0cc08ed575c1669492579529b53c9ff27b0b96265fa867d'
            '84f699d1a345b563d70147694b93c92a159f2cc91bf9584d54bfdbe8cdf9ecd2'
            '32301742d515ac6cac5181a7133c388b09c1ddd1507bcc6964bfbf2a756fda14'
            '9b525568457aae4e97a3aa36e92ea3ad85f31f76772c954c662eb9509f9497cc'
            'aeb2c85dd73a4111cfc90d002193412936be698e04b989cf6940a9c641af9c8b'
            '02934797f382c8071b2660d26b5ee0d738101e72b0289aee3fbc117ee55ccb29'
            'dc88c7738bef3f7d6c062b7a07590588d5cd7a2cdef6c55079add4ff01d22e95'
            'c7501151cc5912facaa8a8a62ce675d74daddbb0fa68d69cfb7a7262f40d5e3b'
            'e2530b6e5e539b02a29d804fbc6ef8ef30a685913961519a00c12f607e4211b9'
            '33039e0844536ab42f72dd555bcb30c06c027d403ed83648886362f6bcbafa23'
            '6b0f9553b34b55b94b3d2c908ba12d8c0568ba989941f6e8480adde153ec2aa0'
            '360d3690a5d5c06f19e089d9f2ce1e7706b3fefd0f63580ecb4da3aef6b43c98'
            '4bf2962a76f83758cd07a131b53f0ebda39cf460036f07ad6e9ed7beafcc791d'
            'b8342467f4cd94d28a6d6c91a623148a106742bcd345e36158f7c171ef8f3828'
            '1fb907f1fcaa6270c17364afe5433bd15eef611448b7dc192c60d543ea539fd2'
            '4a04d6809315b4f0bf224f55f27ae6fff9e6c1d28938e23ec7f395806f176b3b'
            '1c15c1ff7aa612323e77070875f436839cf4ed2a3080c998ebf66282e1ed52dc'
            '1fd8d6ce76ed98b97202de58d8cac7d179ecbb55dbe81cebb3a4b0fc813be016'
            'a94c48e659fd3cfef6d8cd4d65d2c28bfc88488864f853e2e36f27a2dc435939'
            '055289699c4222ef0b8125abdc8c9ceeff0712876c86e6d552a056fbacc14284'
            '5240a9e731b45c17a164066c7eb193c1fbee9fd8d9a2a5afa2edbcde9510da47'
            '11dd5388a1e1c0a2bc8bc3824726598784b9b9e0896a3d77950ba6a4569f1128'
            '4233a8d893787413b316de3ac320fd65c46844d463b98c0a98fdc17100cca505'
            '21e31addcf13bc6fa6650b02ed0f2e195a1226260ae48c536840cc9230de2cfc'
            '276ecc392c777d4b17d771a987e80dca50ff25d8f65671d5de139be73997064b'
            '7f5fc74961ec0face6c4c56d3fc3613c38c5133bb68648191065bbf597f3e906'
            'c7359f4c7015bc1fb02bc13449fa9826669273bd1f0663ba898decb67e8487fc'
            '8b8bfa768c4e21ab45aa6b5815aeea795e05140d690e44e2294ffbb1dcb6e3a2'
            '941d9d1d12ab3549a15fe8c26eab35c236a28443a1e32dca23a39d4451b86ae2'
            '369c7a74312ea06e8e7298e4fcca018a39a626650cd4a5dd565b6f929a0f4787')

_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 \
    0001-Cygwin-avoid-installing-libffi-V2.patch \
    0002-Cygwin-use-SysV-ABI-on-x86_64.patch \
    0003-Cygwin-MinGW-Do-not-version-lto-plugins.patch \
    0004-Cygwin-add-dummy-pthread-tsaware-and-large-address-a.patch \
    0005-Cygwin-handle-dllimport-properly-in-medium-model-V2.patch \
    0006-Cygwin-MinGW-skip-test.patch \
    0007-Cygwin-define-RTS_CONTROL_ENABLE-and-DTR_CONTROL_ENA.patch \
    0008-Cygwin-fix-some-implicit-declaration-warnings.patch \
    0009-Cygwin-__cxa-atexit.patch \
    0010-Cygwin-prevent-modules-from-being-unloaded-before-th.patch \
    0011-Cygwin-libgomp-soname.patch \
    0012-Cygwin-g-time.patch \
    0013-Cygwin-newlib-ftm.patch \
    0014-Cygwin-define-STD_UNIX.patch \
    0015-libstdc-use-lt_host_flags-for-libstdc-.la.patch \
    0016-libstdc-regenerate-src-Makefile.in-for-lt_host_flags.patch \
    0017-libstdc-use-a-link-test-to-test-for-Wl-z-relro.patch \
    0018-libstdc-regenerate-configure.patch

  # MINGW patches
  apply_patch_with_msg \
    0130-libstdc++-in-out.patch \
    0140-gcc-8.2.0-diagnostic-color.patch \
    0150-Handle-spaces-in-path-for-default-manifest.patch \
    0151-makedeps-properly-handle-win-paths.patch \
    0152-gcc-9-branch-clone_function_name_1-Retain-any-stdcall-suffix.patch \
    0153-libgomp-Don-t-hard-code-MS-printf-attributes.patch \
    0154-Windows-New-feature-to-allow-overriding.patch \
    0155-gcc-config-i386-mingw32.h-Ensure-lmsvcrt-precede-lke.patch


  # MSYS2 Patches
  apply_patch_with_msg \
    0950-10.2.0-configure-msys2.patch \
    0951-10.2.0-msys2-spec.patch \
    0953-10.2.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

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