SUMMARY="The GNU assembler, linker and binary utilities"
HOMEPAGE="http://www.gnu.org/software/binutils"
commit="20a46bd5f9af3618ab03662921b8d30e00916c13"
SOURCE_URI="https://github.com/haiku/buildtools/archive/$commit.tar.gz"
SOURCE_FILENAME="$portName-$portVersion.tar.gz"
SOURCE_DIR="buildtools-$commit/legacy/binutils"
CHECKSUM_SHA256="23673f9e754e2301cddb4b29f0516ada757190ae7751779536073fe2060443fc"
REVISION="4"
LICENSE="
	GNU GPL v2
	GNU LGPL v2
	"
COPYRIGHT="1988-2006 Free Software Foundation, Inc."

ARCHITECTURES="x86_gcc2 !x86"
SECONDARY_ARCHITECTURES="x86_gcc2"

PROVIDES="
	binutils$secondaryArchSuffix = $portVersion compat >= 2.17
	cmd:addr2line$secondaryArchSuffix = $portVersion compat >= 2.17
	cmd:ar$secondaryArchSuffix = $portVersion compat >= 2.17
	cmd:as$secondaryArchSuffix = $portVersion compat >= 2.17
	cmd:c++filt$secondaryArchSuffix = $portVersion compat >= 2.17
	cmd:gprof$secondaryArchSuffix = $portVersion compat >= 2.17
	cmd:ld$secondaryArchSuffix = $portVersion compat >= 2.17
	cmd:nm$secondaryArchSuffix = $portVersion compat >= 2.17
	cmd:objcopy$secondaryArchSuffix = $portVersion compat >= 2.17
	cmd:objdump$secondaryArchSuffix = $portVersion compat >= 2.17
	cmd:ranlib$secondaryArchSuffix = $portVersion compat >= 2.17
	cmd:readelf$secondaryArchSuffix = $portVersion compat >= 2.17
	cmd:size$secondaryArchSuffix = $portVersion compat >= 2.17
	cmd:strings$secondaryArchSuffix = $portVersion compat >= 2.17
	cmd:strip$secondaryArchSuffix = $portVersion compat >= 2.17
	"

REQUIRES="
	haiku$secondaryArchSuffix
	"
BUILD_REQUIRES="
	"
BUILD_PREREQUIRES="
	haiku${secondaryArchSuffix}_devel
	cmd:autoconf
	cmd:find
	cmd:flex
	cmd:gcc$secondaryArchSuffix
	cmd:ld$secondaryArchSuffix
	cmd:make
	cmd:sed
	cmd:tar
	cmd:xargs
	"

BUILD_PACKAGE_ACTIVATION_PHASE=INSTALL

binutilsDir=$(pwd)
relativeInstallDir="develop/tools$secondaryArchSubDir"
installDir="$prefix/$relativeInstallDir"
binutilsObjectsDir=$binutilsDir/../binutils-obj

BUILD()
{
	rm -rf $binutilsObjectsDir

	# Touch all *.info files, as we don't want to build them
	cd $binutilsDir
	find . -name \*.info | xargs touch -d "1996-01-01 01:01:01 +0000"
	find . -name \*.texi | xargs touch -d "1996-01-01 01:01:01 +0000"

	# build binutils
	mkdir -p $binutilsObjectsDir
	cd $binutilsObjectsDir
	CFLAGS="-O2" CXXFLAGS="-O2" runConfigure --omit-dirs "docDir dataRootDir" \
		"$binutilsDir/configure" \
		--exec-prefix=$installDir \
		--includedir=$includeDir/binutils \
		--disable-nls --enable-shared=yes

	make

	# patch the makefiles that are too dumb to handle no makeinfo
	sed -i 's@INFO_DEPS = $(srcdir)/gprof.info@INFO_DEPS=@' gprof/Makefile
	sed -i 's@HTMLS = gprof.html@HTMLS=@' gprof/Makefile
	sed -i 's@HTMLS = bfd.html@HTMLS=@' bfd/doc/Makefile
	sed -i 's@HTMLS = binutils.html@HTMLS=@' binutils/doc/Makefile
	sed -i 's@HTMLFILES = standards.html configure.html@HTMLFILES=@' etc/Makefile
	sed -i 's@HTMLS = as.html@HTMLS=@' gas/doc/Makefile
	sed -i 's@HTMLS = ld.html@HTMLS=@' ld/Makefile
	sed -i 's@HTMLS = libiberty.html@HTMLS=@' libiberty/Makefile
}

INSTALL()
{
	cd $binutilsObjectsDir
	archName=$(grep '^target_alias' Makefile | cut -d= -f2)

	make install
	make install-html

	# Remove the development libraries and headers. Nobody uses those anyway.
	rm -r $libDir
	rm -r $developDir/headers

	# no info documentation
	rm -rf $infoDir
	rm -rf $prefix/share

	### Strip #################################################

	echo "Strip debug info"

	cd $prefix
	find bin -type f | xargs -r strip --strip-debug
	strip --strip-debug $installDir/$archName/bin/*

	### Symlinks ##############################################

	echo "Creating required symlinks"

	# There are copies of a subset of the commands below installDir. We
	# overwrite those with symlinks to the ones in binDir.
	for file in $installDir/i586-pc-haiku/bin/*; do
		symlinkRelative -sfn $binDir/$(basename $file) $file
	done

	### Cleanup #################################################

	echo "Cleanup"

	# delete man pages for commands that we aren't including
	cd $manDir
	for cmd in dlltool nlmconv windmc windres; do
		rm -f man1/$cmd.1
	done
}

DESCRIPTION="
The GNU Binutils are a collection of binary tools. The main ones are:
- ld - the GNU linker.
- as - the GNU assembler.
But they also include:
- addr2line - Converts addresses into filenames and line numbers.
- ar - A utility for creating, modifying and extracting from archives.
- c++filt - Filter to demangle encoded C++ symbols.
- dlltool - Creates files for building and using DLLs.
- gold - A new, faster, ELF only linker, still in beta test.
- gprof - Displays profiling information.
- nlmconv - Converts object code into an NLM.
- nm - Lists symbols from object files.
- objcopy - Copys and translates object files.
- objdump - Displays information from object files.
- ranlib - Generates an index to the contents of an archive.
- readelf - Displays information from any ELF format object file.
- size - Lists the section sizes of an object or archive file.
- strings - Lists printable strings from files.
- strip - Discards symbols.
- windmc - A Windows compatible message compiler.
- windres - A compiler for Windows resource files.
Most of these programs use BFD, the Binary File Descriptor library, to do \
low-level manipulation. Many of them also use the opcodes library to assemble \
and disassemble machine instructions.
The binutils have been ported to most major Unix variants as well as Wintel \
systems, and their main reason for existence is to give the GNU system (and \
GNU/Linux) the facility to compile and link programs.
"
