# Maintainer:  Alexey Pavlov <alexpux@gmail.com>
# Contributor: Alexey Borzenkov <snaury@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>
# Contributor: Renato Silva <br.renatosilva@gmail.com>
# Contributor: wirx6 <wirx654@gmail.com>
# Contributor: Kirill Müller <krlmlr@mailbox.org>

if [[ ${CARCH} != i686 ]]; then
  _enable_fortran=yes
  _enable_ada=yes
  _enable_objc=yes
  _enable_rust=no
  _enable_jit=yes
else
  _enable_fortran=yes # Change it to no after dropping gimp
  _enable_ada=no
  _enable_objc=no
  _enable_rust=no
  _enable_jit=no
fi

_threads="posix"

_realname=gcc
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-libs"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-lto-dump"
         $([[ "$_enable_fortran" == "yes" ]] && echo \
           "${MINGW_PACKAGE_PREFIX}-${_realname}-libgfortran" \
           "${MINGW_PACKAGE_PREFIX}-${_realname}-fortran")
         $([[ "$_enable_ada" == "yes" ]] && echo "${MINGW_PACKAGE_PREFIX}-${_realname}-ada")
         $([[ "$_enable_objc" == "yes" ]] && echo "${MINGW_PACKAGE_PREFIX}-${_realname}-objc")
         $([[ "$_enable_rust" == "yes" ]] && echo "${MINGW_PACKAGE_PREFIX}-${_realname}-rust")
         $([[ "$_enable_jit" == "yes" ]] && echo "${MINGW_PACKAGE_PREFIX}-libgccjit")
        )
pkgver=15.1.0
_rc=""
_snapshot=
if [[ -z ${_rc} ]]; then
  _version=${pkgver%%.*}
  _libdir=lib/gcc/${CHOST}/${pkgver%%+*}
else
  _version=${pkgver}-${_rc}
  _libdir=lib/gcc/${CHOST}/${pkgver%%.*}.0.1
fi
if [[ -z ${_snapshot} ]]; then
  _sourcedir=${_realname}-${pkgver}
  _url=https://ftp.gnu.org/gnu/gcc/${_realname}-${pkgver%%+*}
else
  _sourcedir=${_realname}-${_version}-${_snapshot}
  _url=https://gcc.gnu.org/pub/gcc/snapshots/${_version}-${_snapshot}
fi
pkgrel=7
pkgdesc="GCC for the MinGW-w64"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64')
url="https://gcc.gnu.org"
msys2_references=(
  "cpe: cpe:/a:gnu:gcc"
)
license=('spdx:GPL-3.0-or-later')
makedepends=("${MINGW_PACKAGE_PREFIX}-${_realname}"
             $([[ "$_enable_ada" == "yes" ]] && echo "${MINGW_PACKAGE_PREFIX}-${_realname}-ada")
             $([[ "$_enable_rust" == "yes" ]] && echo "${MINGW_PACKAGE_PREFIX}-rust")
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-binutils"
             "${MINGW_PACKAGE_PREFIX}-crt"
             "${MINGW_PACKAGE_PREFIX}-headers"
             "${MINGW_PACKAGE_PREFIX}-gmp"
             "${MINGW_PACKAGE_PREFIX}-gperf"
             "${MINGW_PACKAGE_PREFIX}-isl"
             "${MINGW_PACKAGE_PREFIX}-libiconv"
             "${MINGW_PACKAGE_PREFIX}-mpc"
             "${MINGW_PACKAGE_PREFIX}-mpfr"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-windows-default-manifest"
             "${MINGW_PACKAGE_PREFIX}-winpthreads"
             "${MINGW_PACKAGE_PREFIX}-zlib"
             "${MINGW_PACKAGE_PREFIX}-zstd")
options=('!emptydirs') # '!strip' 'debug')
source=(${_url}/${_sourcedir}.tar.xz{,.sig}
        "gdbinit"
        0003-Windows-Follow-Posix-dir-exists-semantics-more-close.patch
        0005-Windows-Don-t-ignore-native-system-header-dir.patch
        0007-Build-EXTRA_GNATTOOLS-for-Ada.patch
        0008-Prettify-linking-no-undefined.patch
        0011-Enable-shared-gnat-implib.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
        0021-PR14940-Allow-a-PCH-to-be-mapped-to-a-different-addr.patch
        0140-gcc-diagnostic-color.patch
        0200-add-m-no-align-vector-insn-option-for-i386.patch
        2001-fix-building-rust-on-mingw-w64.patch
        3001-fix-codeview-crashes.patch
        9002-native-tls.patch
        0001-libstdc-Search-for-tzdata-on-Windows-msys.patch
        cf588f1a8e7406ced5b08f32f9d23f015a240a31.patch::https://gcc.gnu.org/cgit/gcc/patch/?id=cf588f1a8e7406ced5b08f32f9d23f015a240a31)
sha256sums=('e2b09ec21660f01fecffb715e0120265216943f038d0e48a9868713e54f06cea'
            'SKIP'
            'bce81824fc89e5e62cca350de4c17a27e27a18a1a1ad5ca3492aec1fc5af3234'
            '3fc35067823f87e48dd3bfd265e7593000dc110fa9ad5f16c81b896765fc897a'
            'f7da015a69cff58fbc4b39c7072dfff334a01024470bb092ba31ad648899adf0'
            '90d5cb570083f9dea4ac0f0f87e3e8d2230f5052e6f9b946061a20a224a9d195'
            '8a40ea004a803b7e6aabd6c58d859502a57c87755dbcfedbdfed161f2fb7eb62'
            '5ef4148acc4a2b7ed648d4fe75fab5545e84b4b93a12d6ba4e4ea6061dd635fa'
            'e98805ead7d78ee2a92f237894c4b2b7ddc1688e1b517d8c04f28d440202e40f'
            'fd9bdecb2bbc4796bbc9f00b708dac42ef9e3464a06d6d27e5475cee117de5be'
            'ad1f7b5e7afaaec008b7cbd14feea13a10989fa91bda7003af72d457619bb199'
            'a2abb2c2d793b42c457275e5f5c406d21a61ef07441f696d91a775d01072e17c'
            'ba3649f29049f6d4eea4a481975d9f6851c0c2604d847881b46084a943c4c8f2'
            '1484911163634f30324827619c873a6267b377abba0df8bbedfd128163c53ea4'
            'a411f59953553a3d01dd64c79b1acbb91eca092c4cb01bfd26b1c51c3eb798e8'
            'f21f9ab731e9cab8fd4ca8c289497f859ef7004e6fa0507877dc2559ac2071c3'
            'd977683efc6f6b4dd675a8f1978416d22d5b5d1fa53614b9a18bc18efa231391'
            '29392b22067ee63c06f111f3bc40a0ab6872970555527f452b4f579cced0f552'
            'fca7f20abc77eebcf91a18acc5bcc6b5e092822a5def80bac9eeec8d020276b2')
validpgpkeys=(F3691687D867B81B51CE07D9BBE43771487328A9  # bpiotrowski@archlinux.org
              86CFFCA918CF3AF47147588051E8B148A9999C34  # evangelos@foutrelis.com
              13975A70E63C361C73AE69EF6EEB81F8981C74C7  # richard.guenther@gmail.com
              D3A93CAD751C2AF4F8C7AD516C35B99309B5FA62) # Jakub Jelinek <jakub@redhat.com>

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f ${_fname} ]; then
      rm -rf ${_fname}
    fi
  done
}
# =========================================== #

prepare() {
  cd "${srcdir}"/${_sourcedir}

  apply_patch_with_msg \
    0003-Windows-Follow-Posix-dir-exists-semantics-more-close.patch \
    0005-Windows-Don-t-ignore-native-system-header-dir.patch \
    0007-Build-EXTRA_GNATTOOLS-for-Ada.patch \
    0008-Prettify-linking-no-undefined.patch \
    0011-Enable-shared-gnat-implib.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 \
    0021-PR14940-Allow-a-PCH-to-be-mapped-to-a-different-addr.patch

  # Enable diagnostic color under mintty
  # based on https://github.com/BurntSushi/ripgrep/issues/94#issuecomment-261761687
  apply_patch_with_msg \
    0140-gcc-diagnostic-color.patch

  # workaround for AVX misalignment issue for pass-by-value arguments
  #   cf. https://github.com/msys2/MSYS2-packages/issues/1209
  #   cf. https://sourceforge.net/p/mingw-w64/discussion/723797/thread/bc936130/
  #  Issue is longstanding upstream at https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54412
  #  Potential alternative: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=939559
  # https://github.com/msys2/MINGW-packages/pull/8317#issuecomment-824548411
  apply_patch_with_msg \
    0200-add-m-no-align-vector-insn-option-for-i386.patch

  apply_patch_with_msg \
    2001-fix-building-rust-on-mingw-w64.patch

  # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120051
  apply_patch_with_msg \
    3001-fix-codeview-crashes.patch

  # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120495
  # https://github.com/msys2/MINGW-packages/issues/24209
  apply_patch_with_msg \
    cf588f1a8e7406ced5b08f32f9d23f015a240a31.patch

  # Use MSYS zoneinfo
  apply_patch_with_msg \
    0001-libstdc-Search-for-tzdata-on-Windows-msys.patch

  # apply_patch_with_msg \
  #   9002-native-tls.patch

  autoreconf -fiv

  # do not expect ${prefix}/mingw symlink - this should be superceded by
  # 0005-Windows-Don-t-ignore-native-system-header-dir.patch .. but isn't!
  sed -i 's/${prefix}\/mingw\//${prefix}\//g' configure

  # change hardcoded /mingw prefix to the real prefix .. isn't this rubbish?
  # it might work at build time and could be important there but beyond that?!
  local MINGW_NATIVE_PREFIX=$(cygpath -am ${MINGW_PREFIX})
  sed -i "s#\\/mingw\\/#${MINGW_NATIVE_PREFIX//\//\\/}\\/#g" gcc/config/mingw/mingw32.h
}

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

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("--disable-libstdcxx-debug")
  else
    _extra_config+=("--enable-libstdcxx-debug")
  fi

  if [[ "${CARCH}" == "i686" ]]; then
    _extra_config+=(
      "--disable-sjlj-exceptions"
      "--with-dwarf2"
    )
    LDFLAGS+=" -Wl,--large-address-aware"
    local _arch=pentium4
  else
    _extra_config+=("--enable-plugin")
    local _arch=nocona
  fi

  local _languages="c,lto,c++"
  if [ "$_enable_fortran" == "yes" ]; then
    _languages+=",fortran"
  fi
  if [ "$_enable_ada" == "yes" ]; then
    _languages+=",ada"
  fi
  if [ "$_enable_objc" == "yes" ]; then
    _languages+=",objc,obj-c++"
  fi
  if [ "$_enable_rust" == "yes" ]; then
    _languages+=",rust"
  fi
  if [ "$_enable_jit" == "yes" ]; then
    _languages+=",jit"
  fi

  # so libgomp DLL gets built despide static libdl
  export lt_cv_deplibs_check_method='pass_all'

  # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105507#c3
  # At least with mingw32 + dwarf-2 exceptions there can only be one libgcc in
  # process, or exceptions will no longer work. Since some of the gcc deps are
  # linked dynamically like gmp/zlib/zstd and those pull in libgcc we can't
  # allow libgcc to be linked statically. The default is "-static-libstdc++
  # -static-libgcc" for both, so we drop "-static-libgcc" here:
  # GCC 14 Update: Since we dropped 32bit Ada with GCC 14.1 and GCC doesn't use
  # exceptions elsewhere this could in theory be removed, in case it makes problems.
  _extra_config+=(
    '--with-boot-ldflags="-static-libstdc++"'
    '--with-stage1-ldflags="-static-libstdc++"'
  )

  # In addition adaint.c does `#include <accctrl.h>` which pulls in msxml.h, hacky hack:
  CPPFLAGS+=" -DCOM_NO_WINDOWS_H"

  ../${_sourcedir}/configure \
    --prefix=${MINGW_PREFIX} \
    --with-local-prefix=${MINGW_PREFIX}/local \
    --with-native-system-header-dir=${MINGW_PREFIX}/include \
    --libexecdir=${MINGW_PREFIX}/lib \
    --enable-bootstrap \
    --enable-checking=release \
    --with-arch=${_arch} \
    --with-tune=generic \
    --enable-mingw-wildcard \
    --enable-languages=${_languages} \
    --enable-shared \
    --enable-static \
    --enable-libatomic \
    --enable-threads=${_threads} \
    --enable-graphite \
    --enable-fully-dynamic-string \
    --enable-libstdcxx-backtrace=yes \
    --enable-libstdcxx-filesystem-ts \
    --enable-libstdcxx-time \
    --disable-libstdcxx-pch \
    --enable-lto \
    --enable-libgomp \
    --disable-libssp \
    --disable-multilib \
    --disable-rpath \
    --disable-win32-registry \
    --disable-nls \
    --disable-werror \
    --disable-symvers \
    --with-libiconv \
    --with-system-zlib \
    --with-{gmp,mpfr,mpc,isl}=${MINGW_PREFIX} \
    --with-pkgversion="Rev${pkgrel}, Built by MSYS2 project" \
    --with-bugurl="https://github.com/msys2/MINGW-packages/issues" \
    --with-gnu-as \
    --with-gnu-ld \
    --with-libstdcxx-zoneinfo="yes" \
    "${_extra_config[@]}"

  # While we're debugging -fopenmp problems at least.
  # .. we may as well not strip anything.
  if check_option "strip" "n"; then
    sed -i 's,^STRIP = .*$,STRIP = true,g'                   Makefile
    sed -i 's,^STRIP_FOR_TARGET=.*$,STRIP_FOR_TARGET=true,g' Makefile
  fi

  # https://gcc.gnu.org/onlinedocs/gccint/Makefile.html
  make -O STAGE1_CFLAGS="-O2" \
          profiledbootstrap
}

package_gcc-libs() {
  pkgdesc="GNU Compiler Collection (libraries) for MinGW-w64"
  depends=("${MINGW_PACKAGE_PREFIX}-libwinpthread" "${MINGW_PACKAGE_PREFIX}-tzdata")
  provides=("${MINGW_PACKAGE_PREFIX}-omp" "${MINGW_PACKAGE_PREFIX}-cc-libs")

  # Licensing information

  # Part of the package is GCCRLE, part is LGPL-2.1-or-later, see README generation below.
  # Since the packaged GCCRLE libraries are also spdx:GPL-3.0-or-later,
  # and LGPL-2.1-or-later is compatible with GPL-3.0-or-later, the whole package can
  # be redistributed under GPL-3.0-or-later.
  license=('spdx:GPL-3.0-or-later WITH GCC-exception-3.1 AND LGPL-2.1-or-later')

  cd "${srcdir}"/build-${MSYSTEM}

  make -C $CHOST/libgcc DESTDIR="${pkgdir}" install-shared
  install -dm755 "${pkgdir}"${MINGW_PREFIX}/bin
  mv "${pkgdir}"/libgcc_*.dll "${pkgdir}"${MINGW_PREFIX}/bin/

  # libitm* and libvtv* are disbled until fixed
  for lib in libatomic libgomp libquadmath libstdc++-v3/src; do
    make -C $CHOST/$lib DESTDIR="${pkgdir}" install-toolexeclibLTLIBRARIES
  done

  rm -r "${pkgdir}"${MINGW_PREFIX}/lib

  make -C $CHOST/libstdc++-v3/po DESTDIR="${pkgdir}" install

  # We explain the licensing in this generated README file
  mkdir -p "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}-libs
  cat << ENDFILE > "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}-libs/README
The libgcc, libstdc++, libgomp and libatomic libraries are covered by
GPL-3.0-or-later with GCC-exception-3.1. The libquadmath library is covered
by LGPL-2.1-or-later. The package as a whole can be redistributed under GPL-3.0-or-later.
ENDFILE

  # License files
  install -Dm644 "${srcdir}"/${_sourcedir}/COPYING3        "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}-libs/COPYING3
  install -Dm644 "${srcdir}"/${_sourcedir}/COPYING.LIB     "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}-libs/COPYING.LIB
  install -Dm644 "${srcdir}"/${_sourcedir}/COPYING.RUNTIME "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}-libs/COPYING.RUNTIME
}

package_gcc() {
  pkgdesc="GNU Compiler Collection (C,C++,OpenMP) for MinGW-w64"
  depends=("${MINGW_PACKAGE_PREFIX}-binutils"
           "${MINGW_PACKAGE_PREFIX}-crt"
           "${MINGW_PACKAGE_PREFIX}-headers"
           "${MINGW_PACKAGE_PREFIX}-isl"
           "${MINGW_PACKAGE_PREFIX}-gmp"
           "${MINGW_PACKAGE_PREFIX}-mpfr"
           "${MINGW_PACKAGE_PREFIX}-mpc"
           "${MINGW_PACKAGE_PREFIX}-${_realname}-libs=${pkgver}-${pkgrel}"
           "${MINGW_PACKAGE_PREFIX}-windows-default-manifest"
           "${MINGW_PACKAGE_PREFIX}-winpthreads"
           "${MINGW_PACKAGE_PREFIX}-zlib"
           "${MINGW_PACKAGE_PREFIX}-zstd")
  provides=("${MINGW_PACKAGE_PREFIX}-${_realname}-base"
            "${MINGW_PACKAGE_PREFIX}-cc")
  conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}-base"
             $([[ "$_enable_objc" == "yes" ]] || echo "${MINGW_PACKAGE_PREFIX}-${_realname}-objc")
             $([[ "$_enable_ada" == "yes" ]] || echo "${MINGW_PACKAGE_PREFIX}-${_realname}-ada")
             $([[ "$_enable_rust" == "yes" ]] || echo "${MINGW_PACKAGE_PREFIX}-${_realname}-rust")
             $([[ "$_enable_jit" == "yes" ]] || echo "${MINGW_PACKAGE_PREFIX}-libgccjit")
             $([[ "$_enable_fortran" == "yes" ]] || echo \
               "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran" \
               "${MINGW_PACKAGE_PREFIX}-gcc-fortran")
  )
  groups=("${MINGW_PACKAGE_PREFIX}-toolchain")

  cd "${srcdir}"/build-${MSYSTEM}

  make -C gcc DESTDIR="${pkgdir}" install-driver install-cpp install-gcc-ar \
    c++.install-common install-headers install-plugin install-lto-wrapper

  make -C c++tools DESTDIR="${pkgdir}" install

  install -m755 -t "${pkgdir}"${MINGW_PREFIX}/bin/ gcc/gcov{,-tool}.exe
  install -m755 -t "${pkgdir}"${MINGW_PREFIX}/${_libdir}/ \
    gcc/{cc1,cc1plus,collect2,lto1}.exe

  make -C $CHOST/libgcc DESTDIR="${pkgdir}" install
  rm "${pkgdir}"/libgcc_*.dll

  make -C $CHOST/libstdc++-v3/src DESTDIR="${pkgdir}" install
  make -C $CHOST/libstdc++-v3/include DESTDIR="${pkgdir}" install
  make -C $CHOST/libstdc++-v3/libsupc++ DESTDIR="${pkgdir}" install
  make -C $CHOST/libstdc++-v3/python DESTDIR="${pkgdir}" install
  make DESTDIR="${pkgdir}" install-libcc1
  rm "${pkgdir}"${MINGW_PREFIX}/bin/libstdc++*.dll

  for lib in libatomic libgomp libquadmath; do
    make -C $CHOST/$lib DESTDIR="${pkgdir}" install
    rm "${pkgdir}"${MINGW_PREFIX}/bin/${lib}*.dll
  done

  if [[ "$_enable_fortran" == "yes" ]]; then
    rm -r "${pkgdir}"${MINGW_PREFIX}/${_libdir}/finclude
  fi

  # install -d "${pkgdir}"${MINGW_PREFIX}/share/gdb/auto-load/usr/lib
  # mv "${pkgdir}"${MINGW_PREFIX}/lib/libstdc++.dll.a-gdb.py \
  #   "${pkgdir}"${MINGW_PREFIX}/share/gdb/auto-load/usr/lib/

  make DESTDIR="${pkgdir}" install-fixincludes
  make -C gcc DESTDIR="${pkgdir}" install-mkheaders

  make -C lto-plugin DESTDIR="${pkgdir}" install
  install -dm755 "${pkgdir}"${MINGW_PREFIX}/lib/bfd-plugins/
  cp "${pkgdir}"${MINGW_PREFIX}/${_libdir}/liblto_plugin.dll "${pkgdir}"${MINGW_PREFIX}/lib/bfd-plugins/

  make -C $CHOST/libgomp DESTDIR="${pkgdir}" install-nodist_{libsubinclude,toolexeclib}HEADERS
  make -C $CHOST/libquadmath DESTDIR="${pkgdir}" install-nodist_libsubincludeHEADERS

  make -C gcc DESTDIR="${pkgdir}" install-man install-info
  if [[ "$_enable_fortran" == yes ]]; then
    rm "${pkgdir}"${MINGW_PREFIX}/share/man/man1/{gfortran,lto-dump}.1
    rm "${pkgdir}"${MINGW_PREFIX}/share/info/gfortran.info
  fi
  if [ "$_enable_ada" == "yes" ]; then
    rm "${pkgdir}"${MINGW_PREFIX}/share/info/{gnat-style,gnat_rm,gnat_ugn}.info
  fi
  if [ "$_enable_jit" == "yes" ]; then
    rm "${pkgdir}"${MINGW_PREFIX}/share/info/libgccjit.info
  fi

  make -C libcpp DESTDIR="${pkgdir}" install
  make -C gcc DESTDIR="${pkgdir}" install-po

  # make -C $CHOST/libstdc++-v3/doc DESTDIR="${pkgdir}" doc-install-man

  cp "${pkgdir}"${MINGW_PREFIX}/bin/gcc.exe "${pkgdir}"${MINGW_PREFIX}/bin/cc.exe
  cp "${pkgdir}"${MINGW_PREFIX}/bin/gcc.exe "${pkgdir}"${MINGW_PREFIX}/bin/${CHOST}-cc.exe

  # install "custom" system gdbinit
  install -D -m644 "${srcdir}"/gdbinit "${pkgdir}"${MINGW_PREFIX}/etc/gdbinit
  sed -i 's|%GCC_NAME%|gcc-'${pkgver%%+*}'|g' "${pkgdir}"${MINGW_PREFIX}/etc/gdbinit

  # byte-compile python libraries
  ${MINGW_PREFIX}/bin/python -m compileall -o 0 -o 1 "${pkgdir}"${MINGW_PREFIX}/share/gcc-${pkgver%%+*}/
}

package_gcc-libgfortran() {
  pkgdesc="GNU Compiler Collection (libgfortran) for MinGW-w64"
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}-libs=${pkgver}-${pkgrel}"
           "${MINGW_PACKAGE_PREFIX}-libwinpthread")
  provides=("${MINGW_PACKAGE_PREFIX}-fc-libs")

  cd "${srcdir}"/build-${MSYSTEM}
  make -C $CHOST/libgfortran DESTDIR="${pkgdir}" install-toolexeclibLTLIBRARIES
  rm -r "${pkgdir}"${MINGW_PREFIX}/lib

  install -Dm644 "${srcdir}"/${_sourcedir}/COPYING.RUNTIME \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}-libgfortran/COPYING.RUNTIME
}

package_gcc-fortran() {
  pkgdesc="GNU Compiler Collection (Fortran) for MinGW-w64"
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}=${pkgver}-${pkgrel}"
           "${MINGW_PACKAGE_PREFIX}-${_realname}-libgfortran=${pkgver}-${pkgrel}"
           "${MINGW_PACKAGE_PREFIX}-${_realname}-libs=${pkgver}-${pkgrel}"
           "${MINGW_PACKAGE_PREFIX}-gmp"
           "${MINGW_PACKAGE_PREFIX}-isl"
           "${MINGW_PACKAGE_PREFIX}-libwinpthread"
           "${MINGW_PACKAGE_PREFIX}-mpc"
           "${MINGW_PACKAGE_PREFIX}-mpfr"
           "${MINGW_PACKAGE_PREFIX}-zlib"
           "${MINGW_PACKAGE_PREFIX}-zstd")
  provides=("${MINGW_PACKAGE_PREFIX}-fc")

  cd "${srcdir}"/build-${MSYSTEM}

  make -C $CHOST/libgfortran DESTDIR="${pkgdir}" install
  rm "${pkgdir}"${MINGW_PREFIX}/bin/libgfortran*.dll
  make -C $CHOST/libgomp DESTDIR="${pkgdir}" install-nodist_fincludeHEADERS
  make -C gcc DESTDIR="${pkgdir}" fortran.install-{common,man,info}
  install -Dm755 gcc/f951.exe "${pkgdir}"${MINGW_PREFIX}/${_libdir}/f951.exe
}

package_gcc-ada() {
  pkgdesc="GNU Compiler Collection (Ada) for MinGW-w64"
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}=${pkgver}-${pkgrel}"
           "${MINGW_PACKAGE_PREFIX}-${_realname}-libs=${pkgver}-${pkgrel}"
           "${MINGW_PACKAGE_PREFIX}-gmp"
           "${MINGW_PACKAGE_PREFIX}-isl"
           "${MINGW_PACKAGE_PREFIX}-libwinpthread"
           "${MINGW_PACKAGE_PREFIX}-mpc"
           "${MINGW_PACKAGE_PREFIX}-mpfr"
           "${MINGW_PACKAGE_PREFIX}-zlib"
           "${MINGW_PACKAGE_PREFIX}-zstd")

  cd "${srcdir}"/build-${MSYSTEM}

  make -C gcc DESTDIR="${pkgdir}" ada.install-{common,info} install-gnatlib
  mv "${pkgdir}"${MINGW_PREFIX}/${_libdir}/adalib/*.dll "${pkgdir}"${MINGW_PREFIX}/bin/
  install -m755 gcc/gnat1.exe "${pkgdir}"${MINGW_PREFIX}/${_libdir}
}

package_gcc-objc() {
  pkgdesc="GNU Compiler Collection (ObjC,Obj-C++) for MinGW-w64"
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}=${pkgver}-${pkgrel}"
           "${MINGW_PACKAGE_PREFIX}-${_realname}-libs=${pkgver}-${pkgrel}"
           "${MINGW_PACKAGE_PREFIX}-gmp"
           "${MINGW_PACKAGE_PREFIX}-isl"
           "${MINGW_PACKAGE_PREFIX}-libwinpthread"
           "${MINGW_PACKAGE_PREFIX}-mpc"
           "${MINGW_PACKAGE_PREFIX}-mpfr"
           "${MINGW_PACKAGE_PREFIX}-zlib"
           "${MINGW_PACKAGE_PREFIX}-zstd")

  cd "${srcdir}"/build-${MSYSTEM}

  make -C $CHOST/libobjc DESTDIR="${pkgdir}" install-libs
  make -C $CHOST/libobjc DESTDIR="${pkgdir}" install-headers
  install -dm755 "${pkgdir}"${MINGW_PREFIX}/${_libdir}
  install -m755 gcc/cc1obj{,plus}.exe "${pkgdir}"${MINGW_PREFIX}/${_libdir}/

  install -Dm644 "${srcdir}/${_sourcedir}/COPYING.RUNTIME" \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}-objc/COPYING.RUNTIME
}

package_gcc-rust() {
  pkgdesc="GNU Compiler Collection (Rust) for MinGW-w64"
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}=${pkgver}-${pkgrel}"
           "${MINGW_PACKAGE_PREFIX}-${_realname}-libs=${pkgver}-${pkgrel}"
           "${MINGW_PACKAGE_PREFIX}-gmp"
           "${MINGW_PACKAGE_PREFIX}-isl"
           "${MINGW_PACKAGE_PREFIX}-libwinpthread"
           "${MINGW_PACKAGE_PREFIX}-mpc"
           "${MINGW_PACKAGE_PREFIX}-mpfr"
           "${MINGW_PACKAGE_PREFIX}-zlib"
           "${MINGW_PACKAGE_PREFIX}-zstd")

  cd "${srcdir}"/build-${MSYSTEM}

  make -C gcc DESTDIR="${pkgdir}" rust.install-{common,man,info}
  install -Dm755 gcc/crab1.exe "${pkgdir}"${MINGW_PREFIX}/${_libdir}/crab1.exe
}

package_gcc-lto-dump() {
  pkgdesc="Dump link time optimization object files (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}=$pkgver-$pkgrel"
           "${MINGW_PACKAGE_PREFIX}-${_realname}-libs=${pkgver}-${pkgrel}"
           "${MINGW_PACKAGE_PREFIX}-gmp"
           "${MINGW_PACKAGE_PREFIX}-isl"
           "${MINGW_PACKAGE_PREFIX}-libwinpthread"
           "${MINGW_PACKAGE_PREFIX}-mpc"
           "${MINGW_PACKAGE_PREFIX}-mpfr"
           "${MINGW_PACKAGE_PREFIX}-zlib"
           "${MINGW_PACKAGE_PREFIX}-zstd")

  cd "${srcdir}"/build-${MSYSTEM}

  make -C gcc DESTDIR="${pkgdir}" lto.install-{common,man,info}
}

package_libgccjit() {
  pkgdesc="GNU Compiler Collection (libgccjit) for MinGW-w64"
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}=${pkgver}-${pkgrel}"
           "${MINGW_PACKAGE_PREFIX}-${_realname}-libs=${pkgver}-${pkgrel}"
           "${MINGW_PACKAGE_PREFIX}-gmp"
           "${MINGW_PACKAGE_PREFIX}-isl"
           "${MINGW_PACKAGE_PREFIX}-libwinpthread"
           "${MINGW_PACKAGE_PREFIX}-mpc"
           "${MINGW_PACKAGE_PREFIX}-mpfr"
           "${MINGW_PACKAGE_PREFIX}-zlib"
           "${MINGW_PACKAGE_PREFIX}-zstd")

  cd "${srcdir}"/build-${MSYSTEM}

  make -C gcc DESTDIR="${pkgdir}" jit.install-common jit.install-info
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
