# 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

pkgbase=gcc
pkgname=('gcc' 'gcc-libs')
# NOTE: gdb must be rebuilt with each new gcc version.
# NOTE: libtool must be rebuilt with each new gcc version.
# FIXME: run compileall on /usr/share/gcc-${pkgver}/python/libstdcxx on the next version update
pkgver=13.4.0
pkgrel=2
pkgdesc="The GNU Compiler Collection"
arch=('i686' 'x86_64')
license=('spdx:GPL-3.0-or-later AND GPL-2.0-or-later AND LGPL-2.1-or-later AND LGPL-3.0-or-later AND GCC-exception-3.1 AND GFDL-1.3-or-later')
url="https://gcc.gnu.org/"
msys2_repository_url="https://gcc.gnu.org/git/?p=gcc.git"
msys2_references=(
  "cpe: cpe:/a:gnu:gcc"
)
makedepends=('gcc' 'binutils' 'autotools'
             'mpc-devel' 'gmp-devel' 'mpfr-devel' 'isl-devel' 'zlib-devel'
             'windows-default-manifest' 'gperf')
options=('!emptydirs') # '!strip' 'debug')
source=(https://ftp.gnu.org/gnu/gcc/gcc-${pkgver}/gcc-${pkgver}.tar.xz
        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
        0950-11.2.0-configure-msys2.patch
        0951-11.2.0-msys2-spec.patch)
sha256sums=('9c4ce6dbb040568fdc545588ac03c5cbc95a8dbf0c7aa490170843afb59ca8f5'
            'bc788aa466a83184d285cc2f6c1ffc40c6ed416dd08c6999015262a53f1ab1b5'
            '704acfaeb11d24d3fe5aab34bc883c184ca93aff03d752016c9a50fdd82c5655'
            'c5676fd62d5f7f69be26062b95d42ef47f28151af83b83efa3998ecd8e939e19'
            '03ecf29419119286ebcef35864f9bd26d3bf3eaa355945856953204ed727e173'
            '06214859ae4030ce30bb68aa35aa4a9ac92d1e79e34133089dcbfeb7cc8066e8'
            'a843c038042811265d6ce5917b0c76b709e2591822e5cab53a32b7e9c806da29'
            'e27f82af1eb7a4aad955ec36d5fd7d9a4c24575845bcf718372df9bc2b099eb6'
            'de5692673731f90b888bf301a547481ab8e3cc0029713b6fd5abaabd860250f0'
            'a40e7025507130a2a5d2eb2eea8ba4b053398c307cd55b3c9421a8507bd315ed'
            '2658eb376f7829179963978b69a048ed105a41508adc55d0fc0d607c14181926'
            '83b6aea4a462ae80121fd68d42c6234d02e20865132197a10575bbf95fd33b7e'
            '929b2d0e26259bfc79a9d67a7cf28bbd8d26289ef0933094672b1e09a43d75cc'
            '3707b0aab99b203cbd9e513be46c7d4600de06e6c8344160b7fb1779061d08da'
            '646b6e802e46f932fa793e0e9348c7c98064e46a9bac76f4ad258b66b2b3bba9'
            '82bc03f43fd7e103c1373433340d6654cbc771723db54430b18eff48a3935a45')

# 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 libgomp/config/cygwin/plugin-suffix.h \
    > /dev/null 2>&1

  # 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

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

build() {
  mkdir -p ${srcdir}/build-MSYS && cd ${srcdir}/build-MSYS

  case ${CARCH} in
    i686)
      _arch=pentium4
      _arch_conf="--disable-sjlj-exceptions"
    ;;
    x86_64)
      _arch=nocona
      _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/}

  export lt_cv_deplibs_check_method='pass_all'

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

  ${srcdir}/gcc-${pkgver}/configure \
    --build=${CYGWIN_CHOST} \
    --prefix=/usr \
    --libexecdir=/usr/lib \
    --enable-bootstrap \
    --enable-static \
    --enable-shared \
    --enable-shared-libgcc \
    --enable-version-specific-runtime-libs \
    --with-arch=${_arch} \
    --with-tune=generic \
    --disable-multilib \
    ${_arch_conf} \
    --enable-__cxa_atexit \
    --with-dwarf2 \
    --enable-languages=c,c++,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 \
    --enable-libstdcxx-filesystem-ts

  make

  # make documentation
  #cd $CYGWIN_CHOST/libstdc++-v3
  #make doc-man-doxygen
}

#check() {
#  cd ${srcdir}/build-MSYS

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

  local CYGWIN_CHOST="${CHOST/-msys/-cygwin}"
  cd ${srcdir}/build-MSYS
  make -j1 -C $CYGWIN_CHOST/libgcc DESTDIR=${pkgdir} install-shared
  for lib in libgomp libstdc++-v3/src; do
    make -j1 -C $CYGWIN_CHOST/$lib DESTDIR=${pkgdir} install-toolexeclibLTLIBRARIES
  done

  make -j1 -C $CYGWIN_CHOST/libstdc++-v3/po DESTDIR=${pkgdir} install
  make -j1 -C $CYGWIN_CHOST/libgomp DESTDIR=${pkgdir} install-info
  make -j1 -C $CYGWIN_CHOST/libquadmath DESTDIR=${pkgdir} install-info

  make -j1 DESTDIR=${pkgdir} install-target-libquadmath
  #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=("!strip")
  provides=('msys2-devel')
  conflicts=('gcc-fortran<=11.3.0-4')

  cd ${srcdir}/build-MSYS
  local CYGWIN_CHOST="${CHOST/-msys/-cygwin}"

  if check_option "strip" "n"; then
    export STRIP=true
  fi

  make -j1 DESTDIR=${pkgdir} install
  # 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...

  # remove all DLLs
  rm -f ${pkgdir}/usr/bin/*.dll

  rm -f ${pkgdir}/usr/lib/libiberty.a
  # rm -f ${pkgdir}/usr/lib/gcc/${CYGWIN_CHOST}/${pkgver}/libatomic{.dll,}.a
  rm -f ${pkgdir}/usr/share/info/{libgomp,libquadmath}.info
  rm -f ${pkgdir}/usr/share/locale/{de,fr}/LC_MESSAGES/libstdc++.mo

  # remove static libraries - note libstdc++.a is needed for the binutils and glibc testsuite
  #rm ${pkgdir}/usr/lib/gcc/${CYGWIN_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 \
  #  ${CYGWIN_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
}
