# SliTaz package receipt.

PACKAGE="binutils"
VERSION="2.36.1"
CATEGORY="development"
SHORT_DESC="GNU assembler, linker and binary utilities."
MAINTAINER="pankso@slitaz.org"
TARBALL="$PACKAGE-$VERSION.tar.bz2"
WEB_SITE="http://www.gnu.org/software/binutils/"
WGET_URL="$GNU_MIRROR/$PACKAGE/$TARBALL"
PROVIDE="libbfd"
TAGS="assembler linker"

DEPENDS="glibc zlib"
BUILD_DEPENDS="glibc-dev zlib-dev"

# Rules to compile & install the temporary toolchain.
precook_tmp_toolchain()
{
	mkdir -p binutils-build && cd binutils-build
	{ $src/configure \
		--target=$HOST_SYSTEM --disable-nls \
		--disable-werror &&
	make
	 } || return 1

	#If building on x86_64, create a symlink to ensure the sanity of the toolchain.
	[ "$ARCH" = x86_64 ] && mkdir /tools/lib && ln -s lib /tools/lib64

	make install
}

cook_tmp_toolchain()
{
	mkdir -p binutils-build && cd binutils-build
	{ CC="$HOST_SYSTEM-gcc -B/tools/lib/" \
		AR=$HOST_SYSTEM-ar RANLIB=$HOST_SYSTEM-ranlib \
		$src/configure $CONFIGURE_ARGS \
		--disable-nls --with-lib-path=/tools/lib &&
	make &&
	make install &&
	make -C ld clean &&
	make -C ld LIB_PATH=/usr/lib:/lib
	} || return 1
	cp ld/ld-new /tools/bin
}

# Rules to configure and make the package.
compile_rules()
{
	cd $src
	mkdir -p ../binutils-build && cd ../binutils-build
	$src/configure \
		--program-prefix=$TOOLPREFIX \
		--enable-shared \
		--disable-multilib \
		--with-bugurl="https://bugs.slitaz.org/" \
		--disable-werror \
		$CONFIGURE_ARGS &&
	make tooldir=/usr &&
	make tooldir=/usr install &&
	# This is the default binutils, so create symlinks to the target platform
	# binutil binaries. Delete duplicate binaries to save space.
	for binutil in $(ls -1 $DESTDIR/usr/bin | grep "${TOOLPREFIX}") ; do
		path="$DESTDIR/usr/bin/$(echo "$binutil" | sed "s:$TOOLPREFIX::g")"
		if [ -e $path ] ; then
			rm -f $path
		fi
		ln -s $binutil $path
	done
}

# Rules to gen a SliTaz package suitable for Tazpkg.
genpkg_rules()
{
	mkdir -p $fs/usr/lib
	cp -a $_pkg/usr/bin $fs/usr
	cp -a $_pkg/usr/lib/*.so* $fs/usr/lib
	cp -a $_pkg/usr/lib/*.a $fs/usr/lib
	cp -a $_pkg/usr/include $fs/usr

	# libiberty.h
	cp -a $src/include/libiberty.h $fs/usr/include

	# Remove 'strings' (Busybox).
	#rm $fs/usr/lib/libiberty.a
	rm $fs/usr/bin/strings $fs/usr/bin/*-strings

	# Remove build directory (cookutils and tazwok path).
	rm -rf $WOK/$PACKAGE/source/$PACKAGE-build
	rm -rf $WOK/$PACKAGE/$PACKAGE-build
}