# 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=10.2.0
pkgrel=1
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.xz
        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
        0950-10.2.0-configure-msys2.patch
        0951-10.2.0-msys2-spec.patch
        0953-10.2.0-testsuite-msys2.patch)
sha256sums=('b8dd4368bb9c7f0b98188317ee0254dd8cc99d1e3a18d0ff146c855fe16c1d8c'
            '84f699d1a345b563d70147694b93c92a159f2cc91bf9584d54bfdbe8cdf9ecd2'
            '32301742d515ac6cac5181a7133c388b09c1ddd1507bcc6964bfbf2a756fda14'
            '9b525568457aae4e97a3aa36e92ea3ad85f31f76772c954c662eb9509f9497cc'
            'aeb2c85dd73a4111cfc90d002193412936be698e04b989cf6940a9c641af9c8b'
            '02934797f382c8071b2660d26b5ee0d738101e72b0289aee3fbc117ee55ccb29'
            'dc88c7738bef3f7d6c062b7a07590588d5cd7a2cdef6c55079add4ff01d22e95'
            'c7501151cc5912facaa8a8a62ce675d74daddbb0fa68d69cfb7a7262f40d5e3b'
            'e2530b6e5e539b02a29d804fbc6ef8ef30a685913961519a00c12f607e4211b9'
            '33039e0844536ab42f72dd555bcb30c06c027d403ed83648886362f6bcbafa23'
            '6b0f9553b34b55b94b3d2c908ba12d8c0568ba989941f6e8480adde153ec2aa0'
            '360d3690a5d5c06f19e089d9f2ce1e7706b3fefd0f63580ecb4da3aef6b43c98'
            '4bf2962a76f83758cd07a131b53f0ebda39cf460036f07ad6e9ed7beafcc791d'
            'b8342467f4cd94d28a6d6c91a623148a106742bcd345e36158f7c171ef8f3828'
            '1fb907f1fcaa6270c17364afe5433bd15eef611448b7dc192c60d543ea539fd2'
            '4a04d6809315b4f0bf224f55f27ae6fff9e6c1d28938e23ec7f395806f176b3b'
            '1c15c1ff7aa612323e77070875f436839cf4ed2a3080c998ebf66282e1ed52dc'
            '1fd8d6ce76ed98b97202de58d8cac7d179ecbb55dbe81cebb3a4b0fc813be016'
            'a94c48e659fd3cfef6d8cd4d65d2c28bfc88488864f853e2e36f27a2dc435939'
            '055289699c4222ef0b8125abdc8c9ceeff0712876c86e6d552a056fbacc14284'
            '8b8bfa768c4e21ab45aa6b5815aeea795e05140d690e44e2294ffbb1dcb6e3a2'
            '941d9d1d12ab3549a15fe8c26eab35c236a28443a1e32dca23a39d4451b86ae2'
            '369c7a74312ea06e8e7298e4fcca018a39a626650cd4a5dd565b6f929a0f4787')

# 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-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

  # 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

  # 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

  export lt_cv_deplibs_check_method='pass_all'

  ${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-libgomp \
    --disable-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 \
    --enable-libstdcxx-filesystem-ts

  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"
  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
}
