# POWER Maintainer: Alexander Baldeck <alex.bldck@gmail.com>
# Contributor: Alexander 'hatred' Drozdov <adrozdoff@gmail.com>
# Contributor: toha257 <toha257@gmail.com>
# Contributor: Allan McRae <allan@archlinux.org>
# Contributor: Kevin Mihelich <kevin@archlinuxarm.org>
# Contributor: Tavian Barnes <tavianator@tavianator.com>
# Maintainer: Stefan Schmidt <thrimbor.github@gmail.com>

_target=x86_64-pc-linux-gnu
pkgname="${_target}-gcc"
pkgver=14.1.1+r150+g789f05536df
_commit=789f05536df8fcea58cc3ccede4b2383e17b8be1
pkgrel=1
pkgdesc="The GNU Compiler Collection (${_target})"
arch=(x86_64 powerpc64le powerpc64 powerpc riscv64)
license=(GPL LGPL FDL custom)
url='http://gcc.gnu.org'
depends=("${_target}-binutils>=2.42" "${_target}-glibc>=2.39" "${_target}-libxcrypt" libmpc elfutils zlib)
makedepends=(git)
checkdepends=(dejagnu inetutils)
options=(!emptydirs !debug !distcc !lto !strip)
conflicts=("${_target}-gcc-stage1" "${_target}-gcc-stage2")
replaces=("${_target}-gcc-stage1" "${_target}-gcc-stage2")
provides=("${_target}-gcc-stage1=${pkgver}" "${_target}-gcc-stage2=${pkgver}")
source=(git+https://sourceware.org/git/gcc.git#commit=${_commit}
        gcc11-Wno-format-security.patch)
validpgpkeys=(F3691687D867B81B51CE07D9BBE43771487328A9  # bpiotrowski@archlinux.org
              86CFFCA918CF3AF47147588051E8B148A9999C34  # evangelos@foutrelis.com
              13975A70E63C361C73AE69EF6EEB81F8981C74C7  # richard.guenther@gmail.com
              D3A93CAD751C2AF4F8C7AD516C35B99309B5FA62) # Jakub Jelinek <jakub@redhat.com>
sha256sums=('187c930e1a02ab09425d3331125068384540df95fd401193bbf92192e39291f0'
            '504e4b5a08eb25b6c35f19fdbe0c743ae4e9015d0af4759e74150006c283585e')

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

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

  sed -i 's@/lib64@/lib@g' gcc/config/rs6000/t-*

  # hack! - some configure tests for header files using "$CPP $CPPFLAGS"
  sed -i "/ac_cpp=/s/\$CPPFLAGS/\$CPPFLAGS -O2/" {libiberty,gcc}/configure

  # configure.ac: When adding -Wno-format, also add -Wno-format-security
  patch -Np0 < "$srcdir/gcc11-Wno-format-security.patch"

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

build() {
  cd gcc-build

  # using -pipe causes spurious test-suite failures
  # http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48565
  CFLAGS=${CFLAGS/-pipe}
  CFLAGS=${CFLAGS/-Wformat -Werror=format-security/}
  CXXFLAGS=${CXXFLAGS/-pipe/}
  CXXFLAGS=${CXXFLAGS/ -Wformat -Werror=format-security/}

  "$srcdir/gcc/configure" --prefix=/usr \
      --program-prefix=${_target}- \
      --with-local-prefix=/usr/${_target} \
      --with-sysroot=/usr/${_target} \
      --with-build-sysroot=/usr/${_target} \
      --with-as=/usr/bin/${_target}-as \
      --with-ld=/usr/bin/${_target}-ld \
      --libdir=/usr/lib --libexecdir=/usr/lib \
      --disable-nls \
      --enable-languages=c,c++,lto \
      --enable-shared \
      --enable-threads=posix \
      --with-system-zlib \
      --enable-__cxa_atexit \
      --disable-libunwind-exceptions \
      --enable-clocale=gnu \
      --disable-libstdcxx-pch \
      --disable-libssp \
      --enable-gnu-unique-object \
      --enable-linker-build-id \
      --enable-lto \
      --enable-plugin \
      --enable-install-libiberty \
      --with-linker-hash-style=gnu \
      --enable-gnu-indirect-function \
      --disable-multilib \
      --disable-werror \
      --enable-checking=release \
      --enable-default-pie \
      --enable-default-ssp \
      --target=${_target} \
      --host=${CHOST} \
      --build=${CHOST} \
      --with-build-config=bootstrap-lto \
      --enable-link-serialization=1 \
      --enable-secureplt

  make STAGE1_CFLAGS="${CFLAGS}" STAGE2_CFLAGS="${CFLAGS}"
}

package() {
  cd gcc-build

  make DESTDIR="$pkgdir" install-gcc install-target-libgcc \
    install-target-libstdc++-v3

  rm -rf "$pkgdir/usr/share"

  # strip it manually
  strip "$pkgdir/usr/bin/"* 2>/dev/null || true
  find "$pkgdir/usr/lib" -type f -exec /usr/bin/${_target}-strip \
    --strip-unneeded {} \; 2>/dev/null || true

  install -d -m 755 "$pkgdir/usr/$_target/usr/lib"
  mv $pkgdir/usr/$_target/lib64/* $pkgdir/usr/$_target/usr/lib/
  rm -r $pkgdir/usr/$_target/lib{,64}
}
