# Template file for 'binutils'
pkgname=binutils
version=2.41
revision=3
bootstrap=yes
hostmakedepends="pkgconf"
makedepends="zlib-devel libzstd-devel"
short_desc="GNU binary utilities"
maintainer="Enno Boland <gottox@voidlinux.org>"
license="GPL-3.0-or-later"
homepage="http://www.gnu.org/software/binutils/"
distfiles="${GNU_SITE}/binutils/binutils-${version}.tar.xz"
checksum=ae9a5789e23459e59606e6714723f2d3ffc31c03174191ef0d015bdf06007450

build_options="all_targets"
desc_option_all_targets="Enable all supported targets"

if [ "$CHROOT_READY" ]; then
	hostmakedepends+=" flex perl texinfo"
	makedepends+=" elfutils-devel"
	checkdepends="bc"
	depends="binutils-doc binutils-libs"
	subpackages="binutils-doc libiberty-devel binutils-libs binutils-devel"
else
	_makeinfo=MAKEINFO=/bin/true
	# Set to space force no subpackages
	subpackages=" "
fi

_get_triplet() {
	if [ -z "$XBPS_TRIPLET" ]; then
		echo $(
			source "${XBPS_COMMONDIR}/build-profiles/${XBPS_MACHINE}.sh"
			echo "$XBPS_TRIPLET"
		)
	else
		echo "$XBPS_TRIPLET"
	fi
}

do_configure() {
	local conf

	if [ "$CHROOT_READY" ]; then
		conf+=" --with-debuginfod --enable-shared --enable-install-libiberty"
	else
		conf+=" --without-debuginfod --disable-shared --disable-install-libbfd"
		sed -i "
			s;zlibinc=\$;&-I${XBPS_MASTERDIR}/usr/include;
			s;zlibdir=\$;&-L${XBPS_MASTERDIR}/usr/lib;
		" */configure
	fi
	if [ "$CROSS_BUILD" ]; then
		# we don't want --with-sysroot=${XBPS_CROSS_BASE} like gnu-configure
		conf+=" --host=${XBPS_CROSS_TRIPLET} --with-build-sysroot=${XBPS_CROSS_BASE}"
		# pkgconf adds sysroot includes to cflags and ldflags, which breaks binutils cross-compile with zstd
		vsed -e '/PKG_CONFIG_SYSROOT_DIR/d' -i ${XBPS_WRAPPERDIR}/${XBPS_CROSS_TRIPLET}-pkg-config
	fi

	# gprofng currently only supports x86 and aarch64 glibc.
	case "$XBPS_TARGET_MACHINE" in
		x86_64|i686|aarch64) conf+=" --enable-gprofng" ;;
		*) conf+=" --disable-gprofng" ;;
	esac

	case "$XBPS_TARGET_MACHINE" in
		ppc*)
			conf+=" --enable-secureplt"
			;;
		x86_64*|i686*)
			extra_targets=x86_64-pep
			;;
	esac

	# target archs supported by Void
	# borked: riscv64 (gold doesn't support it)
	void_targets="aarch64,armv5tel,armv6l,armv7l,i686,mips,mipsel,ppc,ppcle,ppc64,ppc64le,x86_64"
	conf+=" --enable-targets=$(echo "$void_targets" | sed -E -e 's/(,|$)/-linux-gnu\1/g'),${extra_targets}"

	if [ "$build_option_all_targets" ]; then
		conf+=" --enable-targets=all"
	fi

	# enable multilib on x86_64 glibc
	if [ "$XBPS_TARGET_MACHINE" = "x86_64" ]; then
		conf+=" --enable-multilib"
	else
		conf+=" --disable-multilib"
	fi

	# ensure softfloat on sf mips targets and set the default hash style
	case "$XBPS_TARGET_MACHINE" in
		mips*hf*) conf+=" --enable-default-hash-style=sysv" ;;
		mips*) conf+=" --without-fp --enable-default-hash-style=sysv" ;;
		*) conf+=" --enable-default-hash-style=gnu";;
	esac

	mkdir build && cd build
	../configure --build=$(_get_triplet) \
		--prefix=/usr \
		--libdir=/usr/lib \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--sysconfdir=/etc \
		--disable-werror \
		--disable-nls \
		--enable-threads \
		--enable-plugins \
		--enable-relro \
		--enable-gold \
		--enable-new-dtags \
		--enable-deterministic-archives \
		--enable-64-bit-bfd \
		--enable-ld=default \
		--with-system-zlib \
		--with-mmap \
		--with-pic \
		--with-zstd \
		$conf
}

do_build() {
	cd ${wrksrc}/build && make ${makejobs} ${_makeinfo}
}

# there is a testsuite now, but e.g. ifunc tests all fail on musl and
# also there are other tests that fail on different targets for fairly
# harmless reasons and it's just too much effort to fix it all
do_check() {
	[ "$CHROOT_READY" ] || return 0
	: # cba for now
}

do_install() {
	cd ${wrksrc}/build

	local _triplet=$(_get_triplet)
	make DESTDIR=${DESTDIR} tooldir=/usr install ${_makeinfo}

	cd ..

	# Remove ld (hardlink) and make a symlink to ld.bfd.
	rm -f ${DESTDIR}/usr/bin/ld
	ln -sfr ${DESTDIR}/usr/bin/ld.bfd ${DESTDIR}/usr/bin/ld

	if [ "$CHROOT_READY" ]; then
		# Remove these symlinks, they are not ABI stable.
		# Programs should compile static to the .a file.
		rm -f ${DESTDIR}/usr/lib/lib{bfd,opcodes}.so
		echo "INPUT ( /usr/lib${XBPS_TARGET_WORDSIZE}/libbfd.a -lsframe -liberty -lzstd -lz )" \
			>${DESTDIR}/usr/lib/libbfd.so
		echo "INPUT ( /usr/lib${XBPS_TARGET_WORDSIZE}/libopcodes.a -lbfd )" \
			>${DESTDIR}/usr/lib/libopcodes.so

		# Install PIC version of libiberty.a
		vinstall build/libiberty/pic/libiberty.a 755 usr/lib
	fi

	# Remove useless manpages.
	for f in dlltool nlmconv windres windmc; do
		rm -f ${DESTDIR}/usr/share/man/man1/${f}.1
	done
	# Create triplet symlinks
	for f in ${DESTDIR}/usr/bin/*; do
		ln -s ${f##*/} ${DESTDIR}/usr/bin/${XBPS_CROSS_TRIPLET:-${_triplet}}-${f##*/}
	done
}

binutils-devel_package() {
	depends="binutils-libs zlib-devel libzstd-devel libiberty-devel"
	short_desc+=" - development files"
	pkg_install() {
		vmove usr/include
		vmove "usr/lib/*.a"
		vmove "usr/lib/*.so"
	}
}

binutils-libs_package() {
	short_desc+=" - shared libraries"
	pkg_install() {
		vmove "usr/lib/lib*-${version}.so"
		vmove "usr/lib/*.so.*"
	}
}

binutils-doc_package() {
	short_desc+=" - info files"
	pkg_install() {
		vmove usr/share/info
	}
}

libiberty-devel_package() {
	reverts="20220713_1 20210106_1"
	short_desc="Library of utility functions used by GNU programs"
	conflicts="binutils-devel<=2.35.1_3"
	pkg_install() {
		vmove usr/include/libiberty
		vmove usr/lib/libiberty.a
	}
}
