# POWER Maintainer: Alexander Baldeck <alex.bldck@gmail.com>
# 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>

# toolchain build order: linux-api-headers->glibc->binutils->gcc->glibc->binutils->gcc

pkgname=binutils
pkgver=2.43.1
_commit=beb2cdbcda911764b2bed5e57921fe90493260bd
pkgrel=1.1
pkgdesc='A set of programs to assemble and manipulate binary and object files'
arch=(x86_64 powerpc64le powerpc64 powerpc espresso riscv64)
url='https://www.gnu.org/software/binutils/'
license=(GPL-2.0-or-later GPL-3.0-or-later LGPL-2.0-or-later LGPL-3.0-or-later GFDL-1.3 FSFAP)
depends=(glibc jansson libelf zlib zstd)
makedepends=(git)
checkdepends=(dejagnu debuginfod bc)
optdepends=('debuginfod: for debuginfod server/client functionality')
conflicts=(binutils-multilib)
replaces=(binutils-multilib)
provides=(
  libctf.so
  libgprofng.so
  libsframe.so
)
backup=('etc/gprofng.rc')
options=(staticlibs !distcc !ccache)
source=(git+https://sourceware.org/git/binutils-gdb.git#commit=${_commit}
        gold-warn-unsupported.patch)
sha256sums=('47cedf324f140726c948191c0e8ca697ba76df9cf7f0007f5e3bd044f3af5f42'
            '2d430b66f84a19c154725ec535280c493be1d34f1a90f95208dce02fecd0e4e4')
validpgpkeys=(3A24BC1E8FB409FA9F14371813FCEF89DD9E3C4F)

pkgver() {
  cd binutils-gdb
  git describe --tags | sed 's/[^-]*-//;s/[^-]*-/&r/;s/-/+/g;s/_/./g'
}

prepare() {
  [[ ! -d binutils-gdb ]] && ln -s binutils-${pkgver} binutils-gdb
  mkdir -p binutils-build

  cd binutils-gdb

  # Turn off development mode (-Werror, gas run-time checks, date in sonames)
  sed -i '/^development=/s/true/false/' bfd/development.sh

  # Creds @Fedora
  # Change the gold configuration script to only warn about
  # unsupported targets.  This allows the binutils to be built with
  # BPF support enabled.
  patch -Np1 -i "${srcdir}"/gold-warn-unsupported.patch
}

build() {
  cd binutils-build

  case "${CARCH}" in
    powerpc) _configure_flags=(--enable-64-bit-bfd) ;;
  esac
  # disable building all targets, we have cross-compilers/* for that
  _configure_flags=(--enable-targets=no)

  "${srcdir}"/binutils-gdb/configure \
    --prefix=/usr \
    --sysconfdir="${pkgdir}"/etc \
    --with-lib-path=/usr/lib:/usr/local/lib \
    --with-bugurl=https://gitlab.archlinux.org/archlinux/packaging/packages/binutils/-/issues \
    --enable-cet \
    --enable-colored-disassembly \
    --enable-default-execstack=no \
    --enable-deterministic-archives \
    --enable-gold \
    --enable-install-libiberty \
    --enable-jansson \
    --enable-ld=default \
    --enable-new-dtags \
    --enable-pgo-build=lto \
    --enable-plugins \
    --enable-relro \
    --enable-shared \
    --enable-threads \
    --disable-gdb \
    --disable-gdbserver \
    --disable-libdecnumber \
    --disable-readline \
    --disable-sim \
    --disable-werror \
    --with-debuginfod \
    --with-pic \
    --with-system-zlib ${_configure_flags[@]}

  make -O tooldir=/usr

  # Extract the FSF All Permissive License
  # <https://www.gnu.org/prep/maintain/html_node/License-Notices-for-Other-Files.html>
  # used for some linker scripts.
  tail -n 5 "${srcdir}"/binutils-gdb/ld/scripttempl/README > FSFAP
}

check() {
  cd binutils-build

  # Use minimal flags for testsuite
  # ld testsuite uses CFLAGS_FOR_TARGET and requires -g
  # gold testsuite requires CXXFLAGS/CFLAGS with default PIE/PIC disabled
  make -O CFLAGS_FOR_TARGET="-O2 -g" \
          CXXFLAGS="-O2 -no-pie -fno-PIC" \
          CFLAGS="-O2 -no-pie" \
          LDFLAGS="" \
          check || true
}

package() {
  cd binutils-build
  make prefix="${pkgdir}"/usr tooldir="${pkgdir}"/usr install

  for _bin in bin addr2line ar as c++filt elfedit gprof ld ld.bfd nm objcopy objdump ranlib readelf size strings strip; do
    ln -s ${_bin} ${pkgdir}/usr/bin/${CHOST}-${_bin}
  done

  # install PIC version of libiberty
  install -m644 libiberty/pic/libiberty.a "${pkgdir}"/usr/lib

  # Remove unwanted files
  rm -f "${pkgdir}"/usr/share/man/man1/{dlltool,windres,windmc}*

  # No shared linking to these files outside binutils
  rm -f "${pkgdir}"/usr/lib/lib{bfd,opcodes}.so
  tee "${pkgdir}"/usr/lib/libbfd.so << EOS
/* GNU ld script */

INPUT( /usr/lib/libbfd.a -lsframe -liberty -lz -lzstd -ldl )
EOS

  tee "${pkgdir}"/usr/lib/libopcodes.so << EOS
/* GNU ld script */

INPUT( /usr/lib/libopcodes.a -lbfd )
EOS

  # install FSF All Permissive License
  install -Dm644 -t "${pkgdir}"/usr/share/licenses/${pkgname}/ FSFAP
}
