# 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=15.2.0
pkgrel=1
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'
             'isl-devel' 'gmp-devel' 'mpc-devel' 'mpfr-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-Always-define-WIN32_LEAN_AND_MEAN-before-windows.h.patch
        0002-add-m-no-align-vector-insn-option-for-i386.patch
        0003-Cygwin-use-SysV-ABI-on-x86_64.patch
        0004-Cygwin-add-dummy-pthread-tsaware-and-large-address-a.patch
        0005-Cygwin-handle-dllimport-properly-in-medium-model-V2.patch
        0006-Cygwin-fix-some-implicit-declaration-warnings-and-re.patch
        0007-Cygwin-define-RTS_CONTROL_ENABLE-and-DTR_CONTROL_ENA.patch
        0008-Cygwin-__cxa-atexit.patch
        0009-Cygwin-libgomp-soname.patch
        0010-Cygwin-g-time.patch
        0011-Cygwin-newlib-ftm.patch
        0012-Cygwin-define-STD_UNIX.patch
        0101-Cygwin-enable-libgccjit-not-just-for-MingW.patch
        0102-Cygwin-testsuite-fixes-for-libgccjit.patch
        0301-libstdc-Implement-most-of-locale-features-for-newlib.patch
        0401-fix-build-stage1-now-depends-on-libintl-libiconv.patch
        0403-fix-build-pretty-print.patch
        0404-fix-build-opts.patch
        0950-11.2.0-configure-msys2.patch
        0951-11.2.0-msys2-spec.patch)
sha256sums=('438fd996826b0c82485a29da03a72d71d6e3541a83ec702df4271f6fe025d24e'
            'ae993ebe11402172611a9fa1550e0611dfc212ab9250a3eaff1022342c9bde71'
            '707faae90300f04fc48949f74af51f47f7505f44f9813bfc26660511ae93b6c1'
            '40871b6750e393b7627dc039c3460b39894fb7bb2514c71b39a1f163fe8e3f30'
            'de93958671c8230c2df79f7da7c406d4e0dfc53e119e9cb04ca44b8a02806671'
            '10365122aa55cdc32c4ff502a14229cb50ddf2b6a4b70521665ef2d6cb182c5a'
            'a843c038042811265d6ce5917b0c76b709e2591822e5cab53a32b7e9c806da29'
            '6e9d2575d9897a9b996c3049b1d1dc1176d33e1373a81bee33c04e3954319124'
            'd9706816609b0154c369b3b8130f2af3e54be8a3e3463aca7eb6060b06cf476c'
            'efb1189f6b6da8ad8a345c567f1f9fe52f76c96a11912f3fc76dfe54cdd189e4'
            '205cf51c9327f249dc11b20785f0ada2204c1112d5123d6debd124e30f6bf9c7'
            '1f6203d53cb9b4957acfa132f0238bd1e5d9670a53efb4d626ec80fc5cee6646'
            '6d037a2da5e1314c6aa76a97bdfdbc6e72d980e62b9e49e947feb70d9346faaa'
            'ec8bc93a2bd10095952a8bee6cd5e43cd3a67022bddbed45c28dde54c504538d'
            'b5fa45f9f131db2b1d1d064fcb2c459a1f6baa118e406f6cc7d5fa4aae24c707'
            '8abcf5ee41507d2814eaf0c0bbfca1a200ccb71833887966ba38378b917fc319'
            '4c637318e3cecc7ef6f05c03ec69444b255ee003f745ba3bdd6ca10780426195'
            '2a48b2a29652d7dee5578aed9e56dcd448d9f50b19387dfa462fc2ffb4ad5d9e'
            '25d1b2d4cd985cdf3e140b9724fd0c10590a7d06519aef6391c3f3029bffd6eb'
            '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-Always-define-WIN32_LEAN_AND_MEAN-before-windows.h.patch \
    0002-add-m-no-align-vector-insn-option-for-i386.patch \
    0003-Cygwin-use-SysV-ABI-on-x86_64.patch \
    0004-Cygwin-add-dummy-pthread-tsaware-and-large-address-a.patch \
    0005-Cygwin-handle-dllimport-properly-in-medium-model-V2.patch \
    0006-Cygwin-fix-some-implicit-declaration-warnings-and-re.patch \
    0007-Cygwin-define-RTS_CONTROL_ENABLE-and-DTR_CONTROL_ENA.patch \
    0008-Cygwin-__cxa-atexit.patch \
    0009-Cygwin-libgomp-soname.patch \
    0010-Cygwin-g-time.patch \
    0011-Cygwin-newlib-ftm.patch \
    0012-Cygwin-define-STD_UNIX.patch \
    0101-Cygwin-enable-libgccjit-not-just-for-MingW.patch \
    0102-Cygwin-testsuite-fixes-for-libgccjit.patch \
    0403-fix-build-pretty-print.patch \
    0404-fix-build-opts.patch

  # Skipped Cygwin patches
  #
  # Would need a dependency on libiconv and libintl development files, see
  # https://github.com/msys2/MSYS2-packages/pull/5458#issuecomment-3008811572
  # https://cygwin.com/pipermail/cygwin-apps/2025-May/044326.html
  # 0301-libstdc-Implement-most-of-locale-features-for-newlib.patch
  # 0401-fix-build-stage1-now-depends-on-libintl-libiconv.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

  ${srcdir}/gcc-${pkgver}/configure \
    --build=${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 $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"

  cd ${srcdir}/build-MSYS
  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-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' 'libiconv' 'libintl' 'mpc' 'mpfr' 'zlib' '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

  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/${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/${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
}
