# Template file for 'binutils'
pkgname=binutils
version=2.39
revision=1
bootstrap=yes
hostmakedepends="pkgconf"
makedepends="zlib-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}/$pkgname/$pkgname-$version.tar.xz"
checksum=645c25f563b8adc0a81dbd6a41cffbf4d37083a382e02d5d3df4f65c09516d00

subpackages="binutils-doc"
if [ "$CHROOT_READY" ]; then
	hostmakedepends+=" flex perl texinfo"
	makedepends+=" elfutils-devel"
	checkdepends="bc"
	depends="binutils-doc"
	subpackages+=" binutils-devel"
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"
	else
		conf+=" --without-debuginfod --disable-shared --disable-install-libbfd"
	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}"
	fi

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

	case "$XBPS_TARGET_MACHINE" in
		ppc*)
			conf+=" --enable-secureplt"
			;;
		x86_64*)
			conf+=" --enable-targets=x86_64-pep"
			;;
		i686*)
			conf+=" --enable-targets=x86_64-linux-gnu,x86_64-pep"
			;;
	esac

	# 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 \
		$conf
}

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

# 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

	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/libbfd.a -liberty -lz )" \
			>${DESTDIR}/usr/lib/libbfd.so
		echo "INPUT ( /usr/lib/libopcodes.a -lbfd )" \
			>${DESTDIR}/usr/lib/libopcodes.so
	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="zlib-devel libiberty-devel"
	short_desc+=" - development files"
	pkg_install() {
		vmove usr/include
		vmove "usr/lib/*.a"
		vmove "usr/lib/libbfd.so"
		vmove "usr/lib/libopcodes.so"
	}
}

binutils-doc_package() {
	short_desc+=" - info files"
	pkg_install() {
		vmove usr/share/info
		# Remove info file that conflicts with autoconf.
		rm -f ${PKGDESTDIR}/usr/share/info/standards.info
	}
}
