SUMMARY="Google's open source JavaScript engine"
DESCRIPTION="V8 is Google's open source JavaScript engine. V8 is written in \
C++ and is used in Google Chrome, the open source browser from Google. V8 \
implements ECMAScript as specified in ECMA-262, 5th edition, and runs on \
Windows (XP or newer), Mac OS X (10.5 or newer), and Linux systems that use \
IA-32, x64, or ARM processors. V8 can run standalone, or can be embedded into \
any C++ application."
HOMEPAGE="https://v8.dev/"
COPYRIGHT="2006-2018 The V8 Project Authors"
LICENSE="BSD (3-clause)"
REVISION="2"
SOURCE_URI="https://github.com/v8/v8/archive/$portVersion.tar.gz"
SOURCE_DIR="v8-$portVersion"
CHECKSUM_SHA256="a9b1c74ff2fae728e1c94acf525321669679ef56da9cc4d781c427b26032bbf7"
srcChromeVersion="74.0.3729.157"
SOURCE_URI_2="https://gsdview.appspot.com/chromium-browser-official/chromium-$srcChromeVersion-lite.tar.xz"
CHECKSUM_SHA256_2="d7406d4efba9916000f8a0f087e7eaf443523f264a59a51d2089c4b2e4c66be0"
SOURCE_DIR_2="chromium-$srcChromeVersion"

PATCHES="v8-$portVersion.patchset"
PATCHES_2="build-$portVersion.patch"

ARCHITECTURES="all !x86_gcc2 ?x86"

if [ "$targetArchitecture" = x86_gcc2 ]; then
	SECONDARY_ARCHITECTURES="x86"
	ARCH=x86
else
	ARCH=x64
fi

# On x86_gcc2 we don't want to install the commands in bin/<arch>/, but in bin/.
commandBinDir=$binDir
if [ "$targetArchitecture" = x86_gcc2 ]; then
    commandBinDir=$prefix/bin
fi

PROVIDES="
	v8$secondaryArchSuffix = $portVersion
	cmd:d8$commandSuffix = $portVersion
	lib:libv8$secondaryArchSuffix = $portVersion
	lib:libv8_for_testing$secondaryArchSuffix = $portVersion
	lib:libv8_libbase$secondaryArchSuffix = $portVersion
	lib:libv8_libplatform$secondaryArchSuffix = $portVersion
	"
REQUIRES="
	haiku$secondaryArchSuffix
	lib:libexecinfo$secondaryArchSuffix
	lib:libssp$secondaryArchSuffix
	"

PROVIDES_devel="
	v8${secondaryArchSuffix}_devel = $portVersion
	devel:libv8$secondaryArchSuffix
	"
REQUIRES_devel="
	v8$secondaryArchSuffix == $portVersion
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	gcc${secondaryArchSuffix}_syslibs
	gcc${secondaryArchSuffix}_syslibs_devel
	devel:libgcc$secondaryArchSuffix
	devel:libexecinfo$secondaryArchSuffix
	"
BUILD_PREREQUIRES="
	cmd:gcc$secondaryArchSuffix
	cmd:g++$secondaryArchSuffix
	cmd:gn
	cmd:ld$secondaryArchSuffix
	cmd:ninja
	cmd:python
	cmd:which
	"

BUILD()
{
	cd $sourceDir

	mkdir -p base/trace_event/common
	mkdir -p build
	mkdir -p buildtools
	mkdir -p third_party/jinja2
	mkdir -p third_party/markupsafe
	mkdir -p tools/clang
	mkdir -p third_party/googletest/src

	cp -rf /$sourceDir2/base/trace_event/common/* $sourceDir/base/trace_event/common
	cp -rf /$sourceDir2/build/* $sourceDir/build/
	cp -rf /$sourceDir2/buildtools/* $sourceDir/buildtools
	cp -rf /$sourceDir2/third_party/jinja2/* $sourceDir/third_party/jinja2
	cp -rf /$sourceDir2/third_party/markupsafe/* $sourceDir/third_party/markupsafe
	cp -rf /$sourceDir2/tools/clang/* $sourceDir/tools/clang
	cp -rf /$sourceDir2/third_party/googletest/src/* $sourceDir/third_party/googletest/src

	mkdir -p out.gn

	gn gen out.gn/$ARCH.release --args="target_os=\"haiku\" target_cpu=\"$ARCH\"
        v8_enable_embedded_builtins = false is_debug = false symbol_level=0
        is_clang=false treat_warnings_as_errors=false use_sysroot=false
        v8_enable_i18n_support=false is_component_build=true
        v8_use_snapshot=false"

	LIBRARY_PATH=$LIBRARY_PATH:. ninja -C out.gn/$ARCH.release $jobArgs
}

INSTALL()
{
	mkdir $commandBinDir
	cp -af out.gn/$ARCH.release/d8 ${commandBinDir}
	mkdir -p ${includeDir}/v8
	cp -af include/*  ${includeDir}/v8
	mkdir -p $libDir
	cp -af out.gn/$ARCH.release/*.so ${libDir}

	prepareInstalledDevelLibs libv8
	packageEntries devel $developDir
}

TEST()
{
	export LIBRARY_PATH=$LIBRARY_PATH::./out.gn/$ARCH.release

	python tools/run-tests.py --gn $jobArgs --exit-after-n-failures=0 -p color \
	--report -v -m release unittests message mkgrokdump fuzzer \
	preparser wasm-spec-tests webkit cctest mjsunit
}
