SUMMARY="A small and fast C compiler"
DESCRIPTION="* SMALL! You can compile and execute C code everywhere, for \
example on rescue disks (about 100KB for x86 TCC executable, including C \
preprocessor, C compiler, assembler and linker).
* FAST! tcc generates x86 code. No byte code overhead. Compile, assemble and \
link several times faster than GCC.
* UNLIMITED! Any C dynamic library can be used directly. TCC is heading \
torward full ISOC99 compliance. TCC can of course compile itself.
* SAFE! tcc includes an optional memory and bound checker. Bound checked code \
can be mixed freely with standard code.
* Compile and execute C source directly. No linking or assembly necessary. \
Full C preprocessor and GNU-like assembler included.
* C script supported : just add '#!/usr/local/bin/tcc -run' at the first line \
of your C source, and execute it directly from the command line.
* With libtcc, you can use TCC as a backend for dynamic code generation."
HOMEPAGE="https://bellard.org/tcc/"
COPYRIGHT="2001-2014 Fabrice Bellard"
LICENSE="GNU LGPL v2"
REVISION="3"
SOURCE_URI="http://download.savannah.gnu.org/releases/tinycc/tcc-0.9.26.tar.bz2"
CHECKSUM_SHA256="521e701ae436c302545c3f973a9c9b7e2694769c71d9be10f70a2460705b6d71"
PATCHES="tcc-$portVersion.patchset"

ARCHITECTURES="?all !x86_gcc2"
SECONDARY_ARCHITECTURES="x86"

PROVIDES="
	tcc$secondaryArchSuffix = $portVersion
	cmd:arm_eabi_tcc$secondaryArchSuffix
	cmd:arm_fpa_ld_tcc$secondaryArchSuffix
	cmd:arm_fpa_tcc$secondaryArchSuffix
	cmd:arm_vfp_tcc$secondaryArchSuffix
	cmd:c67_tcc$secondaryArchSuffix
	cmd:i386_win32_tcc$secondaryArchSuffix
	cmd:tcc$secondaryArchSuffix
	cmd:x86_64_tcc$secondaryArchSuffix
	cmd:x86_64_win32_tcc$secondaryArchSuffix
	"
REQUIRES="
	haiku$secondaryArchSuffix
	"

PROVIDES_devel="
	tcc${secondaryArchSuffix}_devel = $portVersion
	devel:libtcc$secondaryArchSuffix
	"
REQUIRES_devel="
	"

BUILD_PREREQUIRES="
	haiku${secondaryArchSuffix}_devel
	cmd:gcc$secondaryArchSuffix
	cmd:make
	"

BUILD()
{
	local hybrid
	# Borrowed from gcc
	local sysincdir=(
			  "/boot/system/develop/headers/os"
			  "/boot/system/develop/headers/os/app"
			  "/boot/system/develop/headers/os/device"
			  "/boot/system/develop/headers/os/drivers"
			  "/boot/system/develop/headers/os/game"
			  "/boot/system/develop/headers/os/interface"
			  "/boot/system/develop/headers/os/kernel"
			  "/boot/system/develop/headers/os/locale"
			  "/boot/system/develop/headers/os/mail"
			  "/boot/system/develop/headers/os/media"
			  "/boot/system/develop/headers/os/midi"
			  "/boot/system/develop/headers/os/midi2"
			  "/boot/system/develop/headers/os/net"
			  "/boot/system/develop/headers/os/opengl"
			  "/boot/system/develop/headers/os/storage"
			  "/boot/system/develop/headers/os/support"
			  "/boot/system/develop/headers/os/translation"
			  "/boot/system/develop/headers/os/add-ons/graphics"
			  "/boot/system/develop/headers/os/add-ons/input_server"
			  "/boot/system/develop/headers/os/add-ons/mail_daemon"
			  "/boot/system/develop/headers/os/add-ons/registrar"
			  "/boot/system/develop/headers/os/add-ons/screen_saver"
			  "/boot/system/develop/headers/os/add-ons/tracker"
			  "/boot/system/develop/headers/os/be_apps/Deskbar"
			  "/boot/system/develop/headers/os/be_apps/NetPositive"
			  "/boot/system/develop/headers/os/be_apps/Tracker"
			  "/boot/system/develop/headers/3rdparty"
			  "/boot/system/develop/headers/bsd"
			  "/boot/system/develop/headers/glibc"
			  "/boot/system/develop/headers/gnu"
			  "/boot/system/develop/headers/posix"
			)
	if [ -n "$secondaryArchSuffix" ]; then
		hybrid="${secondaryArchSuffix#_}"
		sysincdir=("/boot/system/non-packaged/develop/headers/$hybrid"
			   ${sysincdir[@]}
			   "/boot/system/develop/headers/$hybrid"
			   "/boot/system/develop/headers/$hybrid/os"
			   "/boot/system/develop/headers/$hybrid/os/opengl")
	else
		sysincdir=("/boot/system/non-packaged/develop/headers"
			   ${sysincdir[@]})
	fi

	sysincdir+=("/boot/system/develop/headers"
		    # TCC owns include paths, refer to tcc.h
		    "{B}/include")

	local crtpre=("/boot/system/non-packaged/develop/lib${hybrid:+/$hybrid}"
		      "/boot/system/develop/lib${hybrid:+/$hybrid}")

	local libdir=(${crtpre[@]}
		      "/boot/system/non-packaged/lib${hybrid:+/$hybrid}"
		      "/boot/system/lib${hybrid:+/$hybrid}")

	runConfigure ./configure --enable-cross \
		--sysincludepaths=$(IFS=:; echo "${sysincdir[*]}") \
		--libpaths=$(IFS=:; echo "${libdir[*]}") \
		--crtprefix=$(IFS=:; echo "${crtpre[*]}") \
		--elfinterp=/system/runtime_loader
	make $jobArgs
}

INSTALL()
{
	make install
	prepareInstalledDevelLib libtcc
	packageEntries devel $developDir
}

TEST()
{
	if [ "$targetArchitecture" == "x86_gcc2" && -z "$secondaryArchSuffix" ]; then
		echo "Sorry, test doesn't support gcc2"
	else
		make test
	fi
}
