# Template file for 'binutils'
pkgname=binutils
version=2.23.51.0.3
revision=1
makedepends="flex perl"
subpackages="$pkgname-devel"
short_desc="GNU binary utilities"
maintainer="Juan RP <xtraeme@gmail.com>"
homepage="http://www.gnu.org/software/binutils/"
license="GPL-3"
distfiles="${KERNEL_SITE}/devel/binutils/$pkgname-$version.tar.xz"
checksum=2162905dabf191a2a803eab8d31e7be52d96d6ccd1e0ce42e25d82c6be918cd0
long_desc="
 The GNU binutils package contains essential tools to assemble, link and
 manipulate binary and object files. They are often used with a compiler
 and additional libraries for building software.

 It includes the GNU linker, the portable GNU assembler, ar for modifying
 archives (such as libraries), nm for listing symbols from object files,
 objdump for displaying object file information, ranlib for generating
 archive indexes, size for listing section sizes of object files,
 strings for outputing the printable characters in files, strip for
 stripping out symbols (like debugging information) from object files,
 and other tools."

do_configure() {
	./configure ${CONFIGURE_SHARED_ARGS} --enable-threads \
		--enable-64-bit-bfd --enable-multilib --enable-plugins \
		--enable-secureplt --with-mmap --enable-shared
}

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_MACHINE" = "x86_64" ]; then
		mkdir -p ${SRCPKGDESTDIR}/usr/lib
		cd ${SRCPKGDESTDIR}/usr && ln -s lib lib64 && cd ${wrksrc}
	fi

	make DESTDIR=${DESTDIR} tooldir=/usr install

	# 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

	# Fix for multilib builds.
	if [ -d ${DESTDIR}/usr/lib32 ]; then
		mv ${DESTDIR}/usr/lib32/* ${DESTDIR}/usr/lib
		rmdir ${DESTDIR}/usr/lib32
	fi

	# Remove the requirement of PACKAGE{,_VERSION} to be defined
	# from bfd.h... wth?
	sed -i 35,+3d ${DESTDIR}/usr/include/bfd.h
}
