SUMMARY="C/C++ cross-compiler for Atmel AVR"
DESCRIPTION="
Compiler for Atmel AVR platform.
"
HOMEPAGE="http://gcc.gnu.org"

srcGitRev="c20a732df8f8b6d0f32c872817f3a0498a5c4761"
SOURCE_URI="https://github.com/haiku/buildtools/archive/$srcGitRev.tar.gz"
CHECKSUM_SHA256="6d3b677cd8ef777bf6fb3186a15b5122e46345595a996370325c758b3d8a4ab6"
SOURCE_FILENAME="$portVersionedName.tar.gz"
REVISION="1"
LICENSE="
	GNU GPL v2
	GNU LGPL v2
	"
COPYRIGHT="1988-2013 Free Software Foundation, Inc."


ARCHITECTURES="x86 x86_64 arm"
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="
	avr_gcc$secondaryArchSuffix = $portVersion compat >= 4
	cmd:avr_c++$secondaryArchSuffix = $portVersion compat >= 4
	cmd:avr_cc$secondaryArchSuffix = $portVersion compat >= 4
	cmd:avr_cpp$secondaryArchSuffix = $portVersion compat >= 4
	cmd:avr_g++$secondaryArchSuffix = $portVersion compat >= 4
	cmd:avr_gcc$secondaryArchSuffix = $portVersion compat >= 4
	cmd:avr_gcc_4.8.2$secondaryArchSuffix = $portVersion compat >= 4
	cmd:avr_gcov$secondaryArchSuffix = $portVersion compat >= 4
	lib:avr_libstdc++$secondaryArchSuffix = $portVersion compat >= 4
	"

REQUIRES="
	haiku$secondaryArchSuffix
	cmd:avr_as$secondaryArchSuffix
	lib:libstdc++$secondaryArchSuffix
	"
BUILD_REQUIRES="
	cmd:avr_as$secondaryArchSuffix
	"
BUILD_PREREQUIRES="
	haiku${secondaryArchSuffix}_devel
	cmd:autoconf
	cmd:awk
	cmd:bison
	cmd:find
	cmd:flex
	cmd:gcc$secondaryArchSuffix
	cmd:ld$secondaryArchSuffix
	cmd:make
	cmd:makeinfo
	cmd:sed
	cmd:strip
	cmd:tar
	cmd:xargs
	"

SOURCE_DIR="buildtools-$srcGitRev/gcc"

sourceDir=$(pwd)
relativeInstallDir="develop/tools/avr"
installDir="$prefix/$relativeInstallDir"
objectsDir=$(pwd)/../${portVersionedName}-obj

BUILD()
{
	rm -rf $objectsDir

	# Touch some files generated by bison, so that bison won't run to update
	# them. Fixes issues with newer bison versions.
	# And while at it, touch gperf target, too (as gperf may not be installed).
	(cd $sourceDir/gcc; touch c-parse.c c-parse.h cexp.c cp/parse.c \
		cp/parse.h c-gperf.h)

	mkdir -p $objectsDir
	cd $objectsDir

	# Setting a quoted string in an environment variable is ugly.
	echo '#define LIBRARY_PATH_ENV "AVR_LIBRARY_PATH"' > haiku_host.h

	CXXFLAGS="-O2 -include $objectsDir/haiku_host.h" \
	CFLAGS="-O2 -U_FORTIFY_SOURCE" "$sourceDir/configure" \
		--build=$effectiveTargetMachineTriple \
		--prefix=$installDir --libexecdir=$installDir/lib --mandir=$manDir \
		--docdir=$docDir --disable-nls --disable-libssp --with-gnu-ld \
		--enable-languages=c,c++ --enable-lto --enable-frame-pointer \
		--with-pkgversion=$(echo $portVersion | cut -c 7-) --target=avr

	make $jobArgs
}

INSTALL()
{
	cd $objectsDir

	make install-strip

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

	echo "Creating required symlinks"

	# make all tools available via default paths
	mkdir -p $binDir
	for f in c++ cc cpp g++ gcc gcov; do
		symlinkRelative -sfn $installDir/bin/avr-$f $binDir
	done

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

	echo "Cleanup"
	rm -rf $installDir/info
	rm -rf $installDir/share
}
