# Maintainer: Giancarlo Razzolini <grazzolini@archlinux.org>
# Maintainer: Frederik Schwan <freswa at archlinux dot org>
# Contributor:  Bartłomiej Piotrowski <bpiotrowski@archlinux.org>
# Contributor: Allan McRae <allan@archlinux.org>
# Contributor: Daniel Kozak <kozzi11@gmail.com>

# toolchain build order: linux-api-headers->glibc->binutils->gcc->glibc->binutils->gcc
# NOTE: libtool requires rebuilt with each new gcc version

pkgname=(
  gcc
  gcc-ada
  gcc-d
  gcc-fortran
  gcc-gcobol
  gcc-go
  gcc-libs
  gcc-m2
  gcc-objc
  gcc-rust
  libasan
  libatomic
  libgcc
  libgccjit
  libgcobol
  libgfortran
  libgm2
  libgo
  libgomp
  libgphobos
  libitm
  liblsan
  libobjc
  libquadmath
  libstdc++
  libtsan
  libubsan
  lto-dump
)
pkgver=16.1.1+r12+g301eb08fa2c5
pkgrel=2
pkgdesc='The GNU Compiler Collection'
arch=(aarch64 x86_64)
license=(GPL-3.0-with-GCC-exception GFDL-1.3-or-later)
url='https://gcc.gnu.org'
makedepends=(
  binutils
  doxygen
  gcc-ada
  gcc-d
  git
  libisl
  libmpc
  python
  rust
  tar
  zstd
)
checkdepends=(
  dejagnu
  expect
  inetutils
  python-pytest
  tcl
)
if [[ "$pkgver" =~ \+g([0-9a-f]*)$ ]]; then
  _git_ref="commit=${BASH_REMATCH[1]}"
else
  _git_ref="tag=releases/gcc-${pkgver}"
fi
options=(
  !emptydirs
  !lto
)
_libdir=usr/lib/gcc/$CHOST/${pkgver%%+*}
source=("git+https://sourceware.org/git/gcc.git#${_git_ref}"
        c89 c99
        gcc-ada-repro.patch
       )
sha256sums=('e7641bfddebaada298ca0a631183ef8d81bb3c5dfd3aef30bc7d4223f1c0e450'
            '7b09ec947f90b98315397af675369a1e3dfc527fa70013062e6e85c4be0275ab'
            '44ea973558842f3f4bd666bdaf6e810fd7b7c7bd36b5cc4c69f93d2cd0124fc7'
            '1773f5137f08ac1f48f0f7297e324d5d868d55201c03068670ee4602babdef2f')
pkgver() {
  cd gcc
  echo "$(cat gcc/BASE-VER)+$(git describe --abbrev=12 --tags | sed 's/[^-]*-[^-]*-//;s/[^-]*-/r&/;s/-/+/g;s/_/./')"
}

prepare() {
  [[ ! -d gcc ]] && ln -s gcc-${pkgver/+/-} gcc
  cd gcc

  # Do not run fixincludes
  sed -i 's@\./fixinc\.sh@-c true@' gcc/Makefile.in

  # Arch Linux installs x86_64 libraries /lib
  sed -i '/m64=/s/lib64/lib/' gcc/config/i386/t-linux64

  # Arch Linux ARM installs aarch64 libraries /lib
  sed -i '/lp64=/s/lib64/lib/' gcc/config/aarch64/t-aarch64-linux

  # Reproducible gcc-ada
  patch -Np0 < "$srcdir/gcc-ada-repro.patch"

  # ALARM: Specify build host types, triplet patch
  [[ $CARCH == "armv7h" ]] && CONFIGFLAG="--host=armv7l-unknown-linux-gnueabihf --build=armv7l-unknown-linux-gnueabihf --with-arch=armv7-a --with-float=hard --with-fpu=neon"
  [[ $CARCH == "aarch64" ]] && CONFIGFLAG="--host=aarch64-unknown-linux-gnu --build=aarch64-unknown-linux-gnu --with-arch=armv8-a"

  mkdir -p "$srcdir/gcc-build"
  mkdir -p "$srcdir/libgccjit-build"
}

build() {
  local _confflags=(
    --prefix=/usr
    --libdir=/usr/lib
    --libexecdir=/usr/lib
    --mandir=/usr/share/man
    --infodir=/usr/share/info
    --with-bugurl=https://github.com/archlinuxcn/repo/issues
    --with-build-config=bootstrap-lto
    --with-linker-hash-style=gnu
    --with-system-zlib
    --enable-__cxa_atexit
    --enable-cet=auto
    --enable-checking=release
    --enable-clocale=gnu
    --enable-default-pie
    --enable-default-ssp
    --enable-gnu-indirect-function
    --enable-gnu-unique-object
    --enable-libstdcxx-backtrace
    --enable-link-serialization=1
    --enable-linker-build-id
    --enable-lto
    --enable-plugin
    --enable-shared
    --enable-threads=posix
    --disable-libssp
    --disable-libstdcxx-pch
    --disable-multilib
    --disable-werror
    $CONFIGFLAG
  )

  cd gcc-build

  # Credits @allanmcrae
  # https://github.com/allanmcrae/toolchain/blob/f18604d70c5933c31b51a320978711e4e6791cf1/gcc/PKGBUILD
  # TODO: properly deal with the build issues resulting from this
  CFLAGS=${CFLAGS/-Werror=format-security/}
  CXXFLAGS=${CXXFLAGS/-Werror=format-security/}

  # Workaround arm pacman bug
  export FFLAGS=
  export FCFLAGS=

  "$srcdir/gcc/configure" \
    --enable-languages=ada,c,c++,d,fortran,go,lto,m2,objc,obj-c++,rust,cobol \
    --enable-bootstrap \
    "${_confflags[@]:?_confflags unset}"

  # see https://bugs.archlinux.org/task/71777 for rationale re *FLAGS handling
  make -O STAGE1_CFLAGS="-O2" \
       BOOT_CFLAGS="$CFLAGS" \
       BOOT_LDFLAGS="$LDFLAGS" \
       LDFLAGS_FOR_TARGET="$LDFLAGS" \
       bootstrap

  # make documentation
  make -O -C $CHOST/libstdc++-v3/doc doc-man-doxygen

  # Build libgccjit separately, to avoid building all compilers with --enable-host-shared
  # which brings a performance penalty
  cd "$srcdir"/libgccjit-build

  "$srcdir/gcc/configure" \
    --enable-languages=jit \
    --disable-bootstrap \
    --enable-host-shared \
    "${_confflags[@]:?_confflags unset}"

  # see https://bugs.archlinux.org/task/71777 for rationale re *FLAGS handling
  make -O STAGE1_CFLAGS="-O2" \
       BOOT_CFLAGS="$CFLAGS" \
       BOOT_LDFLAGS="$LDFLAGS" \
       LDFLAGS_FOR_TARGET="$LDFLAGS" \
       all-gcc

  cp -a gcc/libgccjit.so* ../gcc-build/gcc/
}

check() {
  cd gcc-build

  return # Skip for now

  # disable libphobos test to avoid segfaults
  sed -i '/maybe-check-target-libphobos \\/d' Makefile

  # do not abort on error as some are "expected"
  make -O -k check || true
  "$srcdir/gcc/contrib/test_summary"
}

package_gcc() {
  pkgdesc="The GNU Compiler Collection - C and C++ frontends"
  depends=(
    "gcc-libs=$pkgver-$pkgrel"
    'binutils>=2.28'
    libmpc
    zstd
    libisl.so
  )
  options=(
    !emptydirs
    staticlibs
  )

  cd gcc-build

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

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

  make -C $CHOST/libgcc DESTDIR="$pkgdir" install
  rm -f "$pkgdir"/usr/lib/libgcc_s{,_asneeded}.so*

  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 -C $CHOST/libatomic DESTDIR="$pkgdir" install

  make DESTDIR="$pkgdir" install-libcc1
  install -d "$pkgdir/usr/share/gdb/auto-load/usr/lib"
  mv "$pkgdir"/usr/lib/libstdc++.so.6.*-gdb.py \
     "$pkgdir/usr/share/gdb/auto-load/usr/lib/"
  rm "$pkgdir"/usr/lib/libstdc++.so*
  rm "$pkgdir"/usr/lib/libatomic.so*

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

  make -C lto-plugin DESTDIR="$pkgdir" install
  install -dm755 "$pkgdir"/usr/lib/bfd-plugins/
  ln -s /$_libdir/liblto_plugin.so \
     "$pkgdir/usr/lib/bfd-plugins/"

  make -C $CHOST/libgomp DESTDIR="$pkgdir" install-nodist_{libsubinclude,toolexeclib}HEADERS
  make -C $CHOST/libitm DESTDIR="$pkgdir" install-nodist_toolexeclibHEADERS
  make -C $CHOST/libquadmath DESTDIR="$pkgdir" install-nodist_libsubincludeHEADERS
  make -C $CHOST/libsanitizer DESTDIR="$pkgdir" install-nodist_{saninclude,toolexeclib}HEADERS
  make -C $CHOST/libsanitizer/asan DESTDIR="$pkgdir" install-nodist_toolexeclibHEADERS
  make -C $CHOST/libsanitizer/tsan DESTDIR="$pkgdir" install-nodist_toolexeclibHEADERS
  make -C $CHOST/libsanitizer/lsan DESTDIR="$pkgdir" install-nodist_toolexeclibHEADERS

  make -C gcc DESTDIR="$pkgdir" install-man install-info
  rm "$pkgdir"/usr/share/man/man1/{gccgo,gfortran,lto-dump,gdc,gm2,gcobol}.1
  rm "$pkgdir"/usr/share/man/man3/gcobol-io.3
  rm "$pkgdir"/usr/share/info/{gccgo,gfortran,gnat-style,gnat_rm,gnat_ugn,gdc,m2}.info

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

  # many packages expect this symlink
  ln -s gcc "$pkgdir"/usr/bin/cc

  # create cc-rs compatible symlinks
  # https://github.com/rust-lang/cc-rs/blob/1.0.73/src/lib.rs#L2578-L2581
  for binary in {c++,g++,gcc,gcc-ar,gcc-nm,gcc-ranlib}; do
    ln -s /usr/bin/$binary "$pkgdir"/usr/bin/aarch64-linux-gnu-$binary
  done

  # POSIX conformance launcher scripts for c89 and c99
  install -Dm755 "$srcdir/c89" "$pkgdir/usr/bin/c89"
  install -Dm755 "$srcdir/c99" "$pkgdir/usr/bin/c99"

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

  # byte-compile python libraries
  python -m compileall "$pkgdir/usr/share/gcc-${pkgver%%+*}/"
  python -O -m compileall "$pkgdir/usr/share/gcc-${pkgver%%+*}/"

  # Install Runtime Library Exception
  install -d "$pkgdir/usr/share/licenses/$pkgname/"
  ln -s /usr/share/licenses/gcc-libs/RUNTIME.LIBRARY.EXCEPTION \
     "$pkgdir/usr/share/licenses/$pkgname/"
}

package_gcc-ada() {
  pkgdesc='Ada front-end for GCC (GNAT)'
  depends=(
    "gcc=$pkgver-$pkgrel"
    libisl.so
  )
  options=(
    !emptydirs
    staticlibs
  )

  cd gcc-build/gcc
  make DESTDIR="$pkgdir" ada.install-{common,info}
  install -m755 gnat1 "$pkgdir/$_libdir"

  cd "$srcdir"/gcc-build/$CHOST/libada
  make DESTDIR="$pkgdir" INSTALL="install" \
       INSTALL_DATA="install -m644" install-libada

  ln -s gcc "$pkgdir/usr/bin/gnatgcc"

  # insist on dynamic linking, but keep static libraries because gnatmake complains
  mv "$pkgdir"/$_libdir/adalib/libgna{rl,t}-${pkgver%%.*}.so "$pkgdir/usr/lib"
  ln -s libgnarl-${pkgver%%.*}.so "$pkgdir/usr/lib/libgnarl.so"
  ln -s libgnat-${pkgver%%.*}.so "$pkgdir/usr/lib/libgnat.so"
  rm -f "$pkgdir"/$_libdir/adalib/libgna{rl,t}.so

  # Install Runtime Library Exception
  install -d "$pkgdir/usr/share/licenses/$pkgname/"
  ln -s /usr/share/licenses/gcc-libs/RUNTIME.LIBRARY.EXCEPTION \
     "$pkgdir/usr/share/licenses/$pkgname/"
}

package_gcc-d() {
  pkgdesc="D frontend for GCC"
  depends=(
    "gcc=$pkgver-$pkgrel"
    libgphobos
    libisl.so
  )
  provides=(
    gdc
  )
  # replaces=(
  #   gdc
  # )
  options=(
    staticlibs
  )

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

  install -Dm755 gcc/gdc "$pkgdir"/usr/bin/gdc
  install -Dm755 gcc/d21 "$pkgdir"/"$_libdir"/d21

  make -C $CHOST/libphobos DESTDIR="$pkgdir" install
  rm -f "$pkgdir/usr/lib/"lib{gphobos,gdruntime}.so*

  # Install Runtime Library Exception
  install -d "$pkgdir/usr/share/licenses/$pkgname/"
  ln -s /usr/share/licenses/gcc-libs/RUNTIME.LIBRARY.EXCEPTION \
     "$pkgdir/usr/share/licenses/$pkgname/"
}

package_gcc-fortran() {
  pkgdesc='Fortran front-end for GCC'
  depends=(
    "gcc=$pkgver-$pkgrel"
    libgfortran
    libisl.so
  )

  cd gcc-build
  make -C $CHOST/libgfortran DESTDIR="$pkgdir" install-cafexeclibLTLIBRARIES \
       install-{toolexeclibDATA,nodist_fincludeHEADERS,gfor_cHEADERS}
  make -C $CHOST/libgomp DESTDIR="$pkgdir" install-nodist_fincludeHEADERS
  make -C gcc DESTDIR="$pkgdir" fortran.install-{common,man,info}
  install -Dm755 gcc/f951 "$pkgdir/$_libdir/f951"

  ln -s gfortran "$pkgdir/usr/bin/f95"

  # Install Runtime Library Exception
  install -d "$pkgdir/usr/share/licenses/$pkgname/"
  ln -s /usr/share/licenses/gcc-libs/RUNTIME.LIBRARY.EXCEPTION \
     "$pkgdir/usr/share/licenses/$pkgname/"
}

package_gcc-gcobol() {
  pkgdesc="Cobol frontend for GCC"
  depends=(
    "gcc=$pkgver-$pkgrel"
    libgcobol
    libisl.so
  )

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

  install -Dm755 gcc/gcobol "$pkgdir"/usr/bin/gcobol

  install -Dm644 $CHOST/libgcobol/libgcobol.spec "$pkgdir"/$_libdir/

  # Install Runtime Library Exception
  install -d "$pkgdir/usr/share/licenses/$pkgname/"
  ln -s /usr/share/licenses/gcc-libs/RUNTIME.LIBRARY.EXCEPTION \
     "$pkgdir/usr/share/licenses/$pkgname/"
}

package_gcc-go() {
  pkgdesc='Go front-end for GCC'
  depends=(
    "gcc=$pkgver-$pkgrel"
    libgo
    libisl.so
  )
  provides=(
    "go=1.17"
  )
  conflicts=(
    go
  )

  cd gcc-build
  make -C $CHOST/libgo DESTDIR="$pkgdir" install-exec-am
  make DESTDIR="$pkgdir" install-gotools
  make -C gcc DESTDIR="$pkgdir" go.install-{common,man,info}

  rm -f "$pkgdir"/usr/lib/libgo.so*
  install -Dm755 gcc/go1 "$pkgdir/$_libdir/go1"

  # Install Runtime Library Exception
  install -d "$pkgdir/usr/share/licenses/$pkgname/"
  ln -s /usr/share/licenses/gcc-libs/RUNTIME.LIBRARY.EXCEPTION \
     "$pkgdir/usr/share/licenses/$pkgname/"
}


package_gcc-libs() {
  pkgdesc='Runtime libraries shipped by GCC'
  depends=(
    'glibc>=2.27'
    libasan
    libatomic
    libgfortran
    libgcc
    libgomp
    liblsan
    libobjc
    libquadmath
    libstdc++
    libtsan
    libubsan
  )
}

package_gcc-m2() {
  pkgdesc='Modula-2 frontend for GCC'
  depends=(
    "gcc=$pkgver-$pkgrel"
    libisl.so
  )

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

  install -Dm755 gcc/cc1gm2 "$pkgdir/$_libdir"/cc1gm2
  install -Dm755 gcc/gm2 "$pkgdir"/usr/bin/gm2

  # Install Runtime Library Exception
  install -d "$pkgdir/usr/share/licenses/$pkgname/"
  ln -s /usr/share/licenses/gcc-libs/RUNTIME.LIBRARY.EXCEPTION \
     "$pkgdir/usr/share/licenses/$pkgname/"
}

package_gcc-objc() {
  pkgdesc='Objective-C front-end for GCC'
  depends=(
    "gcc=$pkgver-$pkgrel"
    libobjc
    libisl.so
  )

  cd gcc-build
  make DESTDIR="$pkgdir" -C $CHOST/libobjc install-headers
  install -dm755 "$pkgdir/$_libdir"
  install -m755 gcc/cc1obj{,plus} "$pkgdir/$_libdir/"

  # Install Runtime Library Exception
  install -d "$pkgdir/usr/share/licenses/$pkgname/"
  ln -s /usr/share/licenses/gcc-libs/RUNTIME.LIBRARY.EXCEPTION \
     "$pkgdir/usr/share/licenses/$pkgname/"
}

package_gcc-rust() {
  pkgdesc="Rust frontend for GCC"
  depends=(
    "gcc=$pkgver-$pkgrel"
    libisl.so
  )

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

  install -Dm755 gcc/gccrs "$pkgdir"/usr/bin/gccrs
  install -Dm755 gcc/crab1 "$pkgdir"/usr/bin/crab1

  # Install Runtime Library Exception
  install -d "$pkgdir/usr/share/licenses/$pkgname/"
  ln -s /usr/share/licenses/gcc-libs/RUNTIME.LIBRARY.EXCEPTION \
     "$pkgdir/usr/share/licenses/$pkgname/"
}

package_libasan() {
  pkgdesc='Address Sanitizer runtime library shipped by GCC'
  depends=(
    'glibc>=2.27'
  )
  provides=(
    libasan.so
  )

  cd gcc-build

  make -C $CHOST/libsanitizer/asan DESTDIR="$pkgdir" install-toolexeclibLTLIBRARIES

  # Install Runtime Library Exception
  install -Dm644 "$srcdir/gcc/COPYING.RUNTIME" \
          "$pkgdir/usr/share/licenses/libasan/RUNTIME.LIBRARY.EXCEPTION"
}

package_libatomic() {
  pkgdesc='GNU Atomic library shipped by GCC'
  depends=(
    'glibc>=2.27'
  )
  provides=(
    libatomic.so
  )

  cd gcc-build

  make -C $CHOST/libatomic DESTDIR="$pkgdir" install-toolexeclibLTLIBRARIES

  # Install Runtime Library Exception
  install -Dm644 "$srcdir/gcc/COPYING.RUNTIME" \
          "$pkgdir/usr/share/licenses/libatomic/RUNTIME.LIBRARY.EXCEPTION"
}

package_libgcc() {
  pkgdesc='Low-level runtime library shipped by GCC'
  depends=(
    'glibc>=2.27'
  )
  provides=(
    libgcc_s.so
  )

  cd gcc-build
  make -C $CHOST/libgcc DESTDIR="$pkgdir" install-shared
  rm -f "$pkgdir/$_libdir/libgcc_eh.a"

  # Install Runtime Library Exception
  install -Dm644 "$srcdir/gcc/COPYING.RUNTIME" \
          "$pkgdir/usr/share/licenses/gcc-libs/RUNTIME.LIBRARY.EXCEPTION"
}

package_libgccjit() {
  pkgdesc="Just-In-Time Compilation with GCC backend"
  depends=(
    "gcc=$pkgver-$pkgrel"
    libisl.so
  )
  provides=(
    libgccjit.so
  )

  cd gcc-build
  make -C gcc DESTDIR="$pkgdir" jit.install-common jit.install-info

  # Install Runtime Library Exception
  install -d "$pkgdir/usr/share/licenses/$pkgname/"
  ln -s /usr/share/licenses/libgcc/RUNTIME.LIBRARY.EXCEPTION \
     "$pkgdir/usr/share/licenses/$pkgname/"
}

package_libgcobol() {
  pkgdesc='Cobol runtime libraries shipped by GCC'
  depends=(
    'glibc>=2.27'
  )
  provides=(
    libgcobol.so
  )

  cd gcc-build
  make -C $CHOST/libgcobol DESTDIR="$pkgdir" install-toolexeclibLTLIBRARIES

  # Install Runtime Library Exception
  install -Dm644 "$srcdir/gcc/COPYING.RUNTIME" \
          "$pkgdir/usr/share/licenses/libgcobol/RUNTIME.LIBRARY.EXCEPTION"
}

package_libgfortran() {
  pkgdesc='Runtime libraries shipped by GCC'
  depends=(
    'glibc>=2.27'
  )
  provides=(
    libgfortran.so
  )

  cd gcc-build
  make -C $CHOST/libgfortran DESTDIR="$pkgdir" install-toolexeclibLTLIBRARIES

  # Install Runtime Library Exception
  install -Dm644 "$srcdir/gcc/COPYING.RUNTIME" \
          "$pkgdir/usr/share/licenses/libgfortran/RUNTIME.LIBRARY.EXCEPTION"
}

package_libgo() {
  pkgdesc='Go runtime libraries shipped by GCC'
  depends=(
    'glibc>=2.27'
  )
  provides=(
    libgo.so
  )

  cd gcc-build
  make -C $CHOST/libgo DESTDIR="$pkgdir" install-toolexeclibLTLIBRARIES

  # Install Runtime Library Exception
  install -Dm644 "$srcdir/gcc/COPYING.RUNTIME" \
          "$pkgdir/usr/share/licenses/libgo/RUNTIME.LIBRARY.EXCEPTION"
}


package_libgomp() {
  pkgdesc='OpenMP library shipped by GCC'
  depends=(
    'glibc>=2.27'
  )
  provides=(
    libgomp.so
  )

  cd gcc-build

  make -C $CHOST/libgomp DESTDIR="$pkgdir" install-toolexeclibLTLIBRARIES
  make -C $CHOST/libgomp DESTDIR="$pkgdir" install-info

  # Install Runtime Library Exception
  install -Dm644 "$srcdir/gcc/COPYING.RUNTIME" \
          "$pkgdir/usr/share/licenses/libgomp/RUNTIME.LIBRARY.EXCEPTION"
}

package_libgm2() {
  pkgdesc='Address Sanitizer runtime library shipped by GCC'
  depends=(
    'glibc>=2.27'
  )
  provides=(
    libm2cor.so
    libm2iso.so
    libm2log.so
    libm2min.so
    libm2pim.so
  )

  cd gcc-build

  make -C $CHOST/libgm2 DESTDIR="$pkgdir" install

  # remove files provided by lib32-gcc-libs
  rm -rf "$pkgdir"/usr/lib32/

  # Install Runtime Library Exception
  install -Dm644 "$srcdir/gcc/COPYING.RUNTIME" \
          "$pkgdir/usr/share/licenses/libgm2/RUNTIME.LIBRARY.EXCEPTION"
}

package_libgphobos() {
  pkgdesc='D runtime libraries shipped by GCC'
  depends=(
    'glibc>=2.27'
  )
  provides=(
    libgdruntime.so
    libgphobos.so
  )

  cd gcc-build

  make -C $CHOST/libphobos DESTDIR="$pkgdir" install
  rm -rf "$pkgdir"/$_libdir/include/d/
  rm -f "$pkgdir"/usr/lib/libgphobos.spec

  # remove files provided by lib32-gcc-libs
  rm -rf "$pkgdir"/usr/lib32/

  # Install Runtime Library Exception
  install -Dm644 "$srcdir/gcc/COPYING.RUNTIME" \
          "$pkgdir/usr/share/licenses/libgphobos/RUNTIME.LIBRARY.EXCEPTION"
}

package_libitm() {
  pkgdesc='GNU Transactional Memory library shipped by GCC'
  depends=(
    'glibc>=2.27'
  )
  provides=(
    libitm.so
  )

  cd gcc-build

  make -C $CHOST/libitm DESTDIR="$pkgdir" install-toolexeclibLTLIBRARIES
  make -C $CHOST/libitm DESTDIR="$pkgdir" install-info

  # Install Runtime Library Exception
  install -Dm644 "$srcdir/gcc/COPYING.RUNTIME" \
          "$pkgdir/usr/share/licenses/libitm/RUNTIME.LIBRARY.EXCEPTION"
}

package_liblsan() {
  pkgdesc='Leak Sanitizer runtime library shipped by GCC'
  depends=(
    'glibc>=2.27'
  )
  provides=(
    liblsan.so
  )

  cd gcc-build

  make -C $CHOST/libsanitizer/lsan DESTDIR="$pkgdir" install-toolexeclibLTLIBRARIES

  # Install Runtime Library Exception
  install -Dm644 "$srcdir/gcc/COPYING.RUNTIME" \
          "$pkgdir/usr/share/licenses/liblsan/RUNTIME.LIBRARY.EXCEPTION"
}

package_libobjc() {
  pkgdesc='Ojective-C runtime libraries shipped by GCC'
  depends=(
    'glibc>=2.27'
  )
  provides=(
    libobjc.so
  )

  cd gcc-build
  make -C $CHOST/libobjc DESTDIR="$pkgdir" install-libs

  # remove files provided by lib32-gcc-libs
  rm -rf "$pkgdir"/usr/lib32/

  # Install Runtime Library Exception
  install -Dm644 "$srcdir/gcc/COPYING.RUNTIME" \
          "$pkgdir/usr/share/licenses/libobjc/RUNTIME.LIBRARY.EXCEPTION"
}

package_libquadmath() {
  pkgdesc='GCC __float128 library'
  depends=(
    'glibc>=2.27'
  )
  provides=(
    libquadmath.so
  )

  cd gcc-build

  make -C $CHOST/libquadmath DESTDIR="$pkgdir" install-toolexeclibLTLIBRARIES
  make -C $CHOST/libquadmath DESTDIR="$pkgdir" install-info

  # Install Runtime Library Exception
  install -Dm644 "$srcdir/gcc/COPYING.RUNTIME" \
          "$pkgdir/usr/share/licenses/libquadmath/RUNTIME.LIBRARY.EXCEPTION"
}

package_libstdc++ () {
  pkgdesc='C++ runtime libraries shipped by GCC'
  depends=(
    'glibc>=2.27'
  )
  provides=(
    libstdc++.so
  )

  cd gcc-build
  make -C $CHOST/libstdc++-v3/src DESTDIR="$pkgdir" install-toolexeclibLTLIBRARIES
  make -C $CHOST/libstdc++-v3/po DESTDIR="$pkgdir" install

  # Install Runtime Library Exception
  install -Dm644 "$srcdir/gcc/COPYING.RUNTIME" \
          "$pkgdir/usr/share/licenses/libstdc++/RUNTIME.LIBRARY.EXCEPTION"
}

package_libtsan() {
  pkgdesc='Thread Sanitizer runtime library shipped by GCC'
  depends=(
    'glibc>=2.27'
  )
  provides=(
    libtsan.so
  )

  cd gcc-build

  make -C $CHOST/libsanitizer/tsan DESTDIR="$pkgdir" install-toolexeclibLTLIBRARIES

  # Install Runtime Library Exception
  install -Dm644 "$srcdir/gcc/COPYING.RUNTIME" \
          "$pkgdir/usr/share/licenses/libtsan/RUNTIME.LIBRARY.EXCEPTION"
}

package_libubsan() {
  pkgdesc='Undefined Behavior Sanitizer runtime library shipped by GCC'
  depends=(
    'glibc>=2.27'
  )
  provides=(
    libubsan.so
  )

  cd gcc-build

  make -C $CHOST/libsanitizer/ubsan DESTDIR="$pkgdir" install-toolexeclibLTLIBRARIES

  # Install Runtime Library Exception
  install -Dm644 "$srcdir/gcc/COPYING.RUNTIME" \
          "$pkgdir/usr/share/licenses/libubsan/RUNTIME.LIBRARY.EXCEPTION"
}

package_lto-dump() {
  pkgdesc="Dump link time optimization object files"
  depends=(
    "gcc=$pkgver-$pkgrel"
    libisl.so
  )

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

  # Install Runtime Library Exception
  install -d "$pkgdir/usr/share/licenses/$pkgname/"
  ln -s /usr/share/licenses/gcc-libs/RUNTIME.LIBRARY.EXCEPTION \
     "$pkgdir/usr/share/licenses/$pkgname/"
}
