# Maintainer: Maxr1998 <aur@maxr1998.de>
# Contributor: Emil Renner Berthing <aur@esmil.dk>

_target=riscv64-unknown-elf
pkgname=${_target}-binutils
pkgver=2.45.1
pkgrel=1
pkgdesc='Assemble and manipulate binary and object files for 32bit and 64bit RISC-V'
arch=('i686' 'x86_64')
url='http://www.gnu.org/software/binutils/'
license=('GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-2.0-or-later AND LGPL-3.0-or-later AND GFDL-1.3-or-later AND FSFAP')
depends=('zlib' 'binutils')
source=("https://ftp.gnu.org/gnu/binutils/binutils-${pkgver}.tar.xz")
sha256sums=('5fe101e6fe9d18fdec95962d81ed670fdee5f37e3f48f0bef87bddf862513aa5')

prepare() {
	cd "binutils-${pkgver}/"
	sed -i '/ac_cpp=/s/\$CPPFLAGS/\$CPPFLAGS -O2/' libiberty/configure
}

build() {
	cd "binutils-${pkgver}/"
	unset CPPFLAGS

	./configure \
		--target=${_target} \
		--prefix=/usr \
		--with-arch=rv64gc_zifencei \
		--with-sysroot=/usr/${_target} \
		--with-gnu-as \
		--with-gnu-ld \
		--with-system-zlib \
		--disable-nls \
		--enable-multilib \
		--enable-ld=default \
		--enable-gold \
		--enable-plugins \
		--enable-deterministic-archives

	make
}

check() {
	cd "binutils-${pkgver}/"

	# unset LDFLAGS as testsuite makes assumptions about which ones are active
	# do not abort on errors - manually check log files
	make LDFLAGS="" -k check || true
}

package() {
	cd "binutils-${pkgver}/"
	make DESTDIR="${pkgdir}" install

	# Remove info documents that conflict with host version
	rm -rf "${pkgdir}/usr/share/info/"

	# Remove libraries that conflict with host version - https://bugs.archlinux.org/task/69682
	rm -rf "${pkgdir}/usr/lib/"
}
