# Contributor: Natanael Copa <ncopa@alpinelinux.org>
# Contributor: Ariadne Conill <ariadne@dereferenced.org>
# Maintainer: Pablo Correa Gómez <ablocorrea@hotmail.com>
pkgname=binutils-cross
pkgver=2.42
pkgrel=0
pkgdesc="Tools necessary to build programs for foreign architectures"
url="https://www.gnu.org/software/binutils/"
makedepends_build="bison flex texinfo"
makedepends_host="zlib-dev zstd-dev jansson-dev"
makedepends="$makedepends_build $makedepends_host"
arch="all"
license="GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-2.1-or-later AND BSD-3-Clause"
source="https://ftp.gnu.org/gnu/binutils/binutils-$pkgver.tar.xz
	binutils-ld-fix-static-linking.patch
	0001-Revert-PR25882-.gnu.attributes-are-not-checked-for-s.patch
	"
builddir="$srcdir/binutils-$pkgver"
_targets="aarch64 armhf armv7 ppc64le riscv64 s390x x86 x86_64"
options="!check"
for target in $_targets
do
	subpackages="$subpackages binutils-$target:_$target"
done

build() {
	for target in $_targets
	do
		local _arch_configure=""
		local _cross_configure="--disable-install-libiberty"
		local _gold_configure="--disable-gold"
		local _plugin_configure="--disable-plugins"
		if [ "$target" = "x86_64" ]; then
			_arch_configure="--enable-targets=x86_64-pep"
		fi

		mkdir "$srcdir/$target"
		cd "$srcdir/$target"
		# missing --with-bug-url and --enable-threads, only for gold
		local ctarget="$(arch_to_hostspec "$target")"
		"$builddir"/configure \
			--build=$CBUILD \
			--host=$CHOST \
			--target=$ctarget \
			--program-prefix=$ctarget- \
			--prefix=/usr \
			--mandir=/usr/share/man \
			--infodir=/usr/share/info \
			$_arch_configure \
			$_cross_configure \
			$_gold_configure \
			$_plugin_configure \
			--disable-gprofng \
			--disable-multilib \
			--disable-nls \
			--disable-werror \
			--enable-64-bit-bfd \
			--enable-colored-disassembly \
			--enable-default-execstack=no \
			--enable-default-hash-style=gnu \
			--enable-deterministic-archives \
			--enable-jansson \
			--enable-ld=default \
			--enable-new-dtags \
			--enable-relro \
			--with-mmap \
			--with-pic \
			--with-system-zlib \
			--with-zstd
		make
	done
}

package() {
	pkgdesc="Tools necessary to build programs for foreign architectures"
	for target in $_targets
	do
		depends="$depends binutils-$target"
	done
	mkdir -p "$pkgdir"
}

_install_target() {
	local target="$1"
	pkgdesc="Tools necessary to build programs for $target"
	cd "$srcdir/$target"
	make install DESTDIR="$subpkgdir"
	rm -rf "$subpkgdir"/usr/share
	rm -rf "$subpkgdir"/usr/lib
	rm -rf "$subpkgdir"/usr/include
}

_aarch64() {
	_install_target aarch64
}

_armhf() {
	_install_target armhf
}

_armv7() {
	_install_target armv7
}

_ppc64le() {
	_install_target ppc64le
}

_riscv64() {
	_install_target riscv64
}

_s390x() {
	_install_target s390x
}

_x86() {
	_install_target x86
}

_x86_64() {
	_install_target x86_64
}

sha512sums="
155f3ba14cd220102f4f29a4f1e5cfee3c48aa03b74603460d05afb73c70d6657a9d87eee6eb88bf13203fe6f31177a5c9addc04384e956e7da8069c8ecd20a6  binutils-2.42.tar.xz
ecee33b0e435aa704af1c334e560f201638ff79e199aa11ed78a72f7c9b46f85fbb227af5748e735fd681d1965fcc42ac81b0c8824e540430ce0c706c81e8b49  binutils-ld-fix-static-linking.patch
70ec22bd72ef6dddecfd970613387dd4a8cdc8730dd3cbf03d5a0c3a7c4d839383167bb06dad21bf7c235329fd44b5dc4aefe762f68544f17155cf002bf1be4a  0001-Revert-PR25882-.gnu.attributes-are-not-checked-for-s.patch
"
