# Template file for 'binutils'
pkgname=binutils
version=2.27
revision=1
bootstrap=yes
short_desc="GNU binary utilities"
maintainer="Juan RP <xtraeme@voidlinux.eu>"
homepage="http://www.gnu.org/software/binutils/"
license="GPL-3"
distfiles="${GNU_SITE}/$pkgname/$pkgname-$version.tar.bz2"
checksum=369737ce51587f92466041a97ab7d2358c6d9e1b6490b3940eb09fb0a9a6ac88

if [ "$CHROOT_READY" ]; then
	hostmakedepends="flex perl"
fi
makedepends+=" zlib-devel"

pre_configure() {
	sed -i "/ac_cpp=/s/\$CPPFLAGS/\$CPPFLAGS -O2/" libiberty/configure
	# Drop bashism!
	sed -e 's,source,\.,g' -i ld/scripttempl/elf32msp430.sc
}
do_configure() {
	if [ "$XBPS_TARGET_MACHINE" = "x86_64" ]; then
		CONFIGFLAG="--enable-64-bit-bfd --enable-multilib"
	fi
	if [ "$CROSS_BUILD" ]; then
		# Do not use configure_args nor build_style=gnu-configure,
		# avoiding --with-sysroot to $XBPS_CROSS_BASE.
		CONFIGFLAG="--host=$XBPS_CROSS_TRIPLET --with-build-sysroot=$XBPS_CROSS_BASE"
	fi
	if [ "$XBPS_TARGET_MACHINE" = "mipsel-musl" ]; then
		CONFIGFLAG+=" --with-float=soft --without-fp"
	fi
	./configure --build=$XBPS_TRIPLET --prefix=/usr --enable-threads \
		--enable-plugins --enable-secureplt --with-mmap \
		--disable-shared --enable-gold --disable-werror \
		--enable-deterministic-archives --enable-ld=default \
		--disable-nls $CONFIGFLAG
}
do_build() {
	make ${makejobs}

	# Rebuild libiberty.a with -fPIC
	cp -a libiberty libiberty-pic
	make -C libiberty-pic clean
	make CFLAGS="$CFLAGS -fPIC" -C libiberty-pic

	# Rebuild libbfd.a with -fPIC
	# hidden visability prevent 3rd party shared libraries exporting bfd non-stable API
	cp -a bfd bfd-pic
	make -C bfd-pic clean
	make CFLAGS="$CFLAGS -fPIC -fvisibility=hidden" -C bfd-pic

	# Rebuild libopcodes.a with -fPIC
	cp -a opcodes opcodes-pic
	make -C opcodes-pic clean
	make CFLAGS="$CFLAGS -fPIC" -C opcodes-pic
}
do_install() {
	if [ "$XBPS_TARGET_MACHINE" = "x86_64" ]; then
		mkdir -p ${DESTDIR}/usr/lib
		cd ${DESTDIR}/usr
		ln -s lib lib64
		cd ${wrksrc}
	fi
	make DESTDIR=${DESTDIR} tooldir=/usr install

	# 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

	# Add some useful headers
	install -m644 include/libiberty.h ${DESTDIR}/usr/include
	install -m644 include/demangle.h ${DESTDIR}/usr/include

	# install libraries rebuilt with -fPIC
	install -m644 libiberty-pic/libiberty.a ${DESTDIR}/usr/lib
	install -m644 bfd-pic/libbfd.a ${DESTDIR}/usr/lib
	install -m644 opcodes-pic/libopcodes.a ${DESTDIR}/usr/lib

	# 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

	# Remove info file that conflicts with autoconf.
	rm -f ${DESTDIR}/usr/share/info/standards.info
	rm -f ${DESTDIR}/usr/lib64

	# 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:-${XBPS_TRIPLET}}-${f##*/}
	done
}

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