SUMMARY="GCC compiler for Motorola 6809"
DESCRIPTION="This is a version of the GCC compiler for the Motorla 6809. It \
uses the lwtools as a backend. Note that there is no C library provided."
HOMEPAGE="http://lwtools.projects.l-w.ca"
SOURCE_URI="ftp://ftp.irisa.fr/pub/mirrors/gcc.gnu.org/gcc/releases/gcc-$portVersion/gcc-$portVersion.tar.bz2"
CHECKSUM_SHA256="8eebf51c908151d1f1a3756c8899c5e71572e8469a547ad72a1ef16a08a31b59"
REVISION="2"
COPYRIGHT="1988-2013 Free Software Foundation, Inc."
LICENSE="
	GNU GPL v2
	GNU LGPL v2
	"
PATCHES="gcc6809-$portVersion.patchset"

ARCHITECTURES=""
if [ $effectiveTargetArchitecture != x86_gcc2 ]; then
	# x86_gcc2 is fine as primary target architecture as long as we're building
	# for a different secondary architecture.
	ARCHITECTURES="$ARCHITECTURES x86_gcc2"
fi
SECONDARY_ARCHITECTURES="x86"

PROVIDES="
	gcc6809$secondaryArchSuffix = $portVersion compat >= 4
	cmd:m6809_unknown_c++$secondaryArchSuffix
	cmd:m6809_unknown_cpp$secondaryArchSuffix
	cmd:m6809_unknown_g++$secondaryArchSuffix
	cmd:m6809_unknown_gcc_$portVersion$secondaryArchSuffix
	cmd:m6809_unknown_gcc$secondaryArchSuffix
	cmd:m6809_unknown_gcov$secondaryArchSuffix
	"
REQUIRES="
	haiku$secondaryArchSuffix
	lib:libmpc$secondaryArchSuffix
	lib:libmpfr$secondaryArchSuffix
	lib:libgmp$secondaryArchSuffix
	cmd:m6809_unknown_ld
	cmd:lwasm
	"
BUILD_REQUIRES="
	devel:libgmp$secondaryArchSuffix
	devel:libmpfr$secondaryArchSuffix
	devel:libmpc$secondaryArchSuffix
	"
BUILD_PREREQUIRES="
	haiku${secondaryArchSuffix}_devel
	cmd:autoconf
	cmd:awk
	cmd:find
	cmd:flex
	cmd:gcc$secondaryArchSuffix
	cmd:ld$secondaryArchSuffix
	cmd:make
	cmd:makeinfo
	cmd:sed
	cmd:strip
	cmd:tar
	cmd:which
	cmd:xargs
	cmd:m6809_unknown_ld
	"

SOURCE_DIR="gcc-$portVersion"

sourceDir=$(pwd)
objectsDir=$(pwd)/../${portVersionedName}-obj

BUILD()
{
	rm -rf $objectsDir

	mkdir -p $objectsDir
	cd $objectsDir

	# Setting a quoted string in an environment variable is ugly (the quotes
	# are removed at various stages and need an absurd amount of escaping), so
	# let's use a file and --include instead.
	echo '#define LIBRARY_PATH_ENV "M6809_LIBRARY_PATH"' > haiku_host.h

	CXXFLAGS="-O2 -include $objectsDir/haiku_host.h" \
	CFLAGS="-O2 -include $objectsDir/haiku_host.h" \
	runConfigure $sourceDir/configure --enable-languages=c,c++ \
		--target=m6809-unknown --program-prefix=m6809-unknown- \
		--enable-obsolete --srcdir=$sourceDir --disable-threads --disable-nls \
		--disable-libssp --with-as=$(which m6809-unknown-as) \
		--with-ar=$(which m6809-unknown-ar) --with-ld=$(which m6809-unknown-ld)

	make $jobArgs all-gcc
}

INSTALL()
{
	cd $objectsDir
	make install-gcc
	# Remove buggy (empty) dir left over by install
	rm -r $prefix/m6809-unknown
}
