# Maintainer: David P. <megver83@parabola.nu>

_target=sh-elf
pkgname=$_target-gcc
pkgver=16.1.0
pkgrel=1
#_snapshot=8-20210107
pkgdesc='The GNU Compiler Collection - cross compiler for SuperH (bare-metal) target'
arch=(x86_64 i686 armv7h)
url='http://gcc.gnu.org/'
license=("GPL-3.0-with-GCC-exception AND GFDL-1.3-or-later")
depends=($_target-binutils libmpc zlib libisl zstd)
makedepends=(gmp mpfr $_target-newlib)
optdepends=("$_target-newlib: Standard C library ($_target target)")
options=(!emptydirs !strip !lto)
source=(https://gcc.gnu.org/pub/gcc/releases/gcc-$pkgver/gcc-$pkgver.tar.xz{,.sig}
        #https://gcc.gnu.org/pub/gcc/snapshots/$_snapshot/gcc-$_snapshot.tar.xz
        )
sha512sums=('b3454958891ab47e1e5b6cb9396c0ad3b04f32fe2a7bf1153a143f21013fdb6b295ca94c98964698a688e4c1d7555ffd8ffbc20187507cce6b1c32cbcc09897a'
            'SKIP')
validpgpkeys=(D3A93CAD751C2AF4F8C7AD516C35B99309B5FA62  # Jakub Jelinek <jakub@redhat.com>
              13975A70E63C361C73AE69EF6EEB81F8981C74C7) # Richard Guenther <richard.guenther@gmail.com>
if [ -n "$_snapshot" ]; then
  _basedir=gcc-$_snapshot
else
  _basedir=gcc-$pkgver
fi

prepare() {
  cd $_basedir

  echo $pkgver > gcc/BASE-VER

  mkdir $srcdir/build-gcc
}

build() {
  cd build-gcc

  CFLAGS=${CFLAGS/-Werror=format-security/}
  CXXFLAGS=${CXXFLAGS/-Werror=format-security/}

  export CFLAGS_FOR_TARGET='-g -Os -ffunction-sections -fdata-sections'
  export CXXFLAGS_FOR_TARGET='-g -Os -ffunction-sections -fdata-sections'

  #if [ "$CARCH" = i686 ]; then
    # using -Werror=format-security causes i686 build to fail
  #  CXXFLAGS+=' -Wno-error=format-security'
  #fi

"$srcdir"/$_basedir/configure \
      --target=$_target \
      --prefix=/usr \
      --with-sysroot=/usr/$_target \
      --with-native-system-header-dir=/include \
      --libexecdir=/usr/lib \
      --enable-languages=c,c++ \
      --enable-threads=single \
      --enable-plugins \
      --enable-multilib \
      --enable-libgcc \
      --disable-libgomp \
      --disable-libquadmath \
      --disable-libffi \
      --disable-libssp \
      --disable-libmudflap \
      --disable-decimal-float \
      --disable-libstdcxx-pch \
      --disable-nls \
      --disable-shared \
      --disable-tls \
      --with-newlib \
      --with-gnu-as \
      --with-gnu-ld \
      --with-system-zlib \
      --with-headers=/usr/$_target/include \
      --with-python-dir=share/gcc-$_target \
      --with-gmp \
      --with-mpfr \
      --with-mpc \
      --with-isl \
      --with-libelf \
      --enable-gnu-indirect-function \
      --with-pkgversion="Parabola GNU/Linux-libre Repositories" \
      --with-bugurl='https://labs.parabola.nu/'

  make
}

package() {
  cd build-gcc

  make DESTDIR="$pkgdir" install -j1

  # strip target binaries
  find "$pkgdir"/usr/lib/gcc/$_target/$pkgver "$pkgdir"/usr/$_target/lib \
       -type f -and \( -name \*.a -or -name \*.o \) \
       -exec $_target-objcopy -R .comment -R .note -R .debug_info -R .debug_aranges \
       -R .debug_pubnames -R .debug_pubtypes -R .debug_abbrev -R .debug_line \
       -R .debug_str -R .debug_ranges -R .debug_loc '{}' \;

  # strip host binaries
  find "$pkgdir"/usr/bin/ "$pkgdir"/usr/lib/gcc/$_target/$pkgver -type f -and \( -executable \) -exec strip '{}' \;

  # Remove files that conflict with host gcc package
  rm -r "$pkgdir"/usr/share/man/man7
  rm -r "$pkgdir"/usr/share/info
  rm "$pkgdir"/usr/lib/libcc1.*
}
