SUMMARY="An open-source implementation of the Java Platform, SE"
DESCRIPTION="OpenJDK (Open Java Development Kit) is a free and open source \
implementation of the Java Platform, Standard Edition (Java SE). It is the \
result of an effort Sun Microsystems began in 2006.

The implementation is licensed under the GNU General Public License (GNU GPL) \
with a linking exception. Were it not for the GPL linking exception, components \
that linked to the Java class library would be subject to the terms of the GPL \
license. OpenJDK is the official Java SE 8 reference implementation."
HOMEPAGE="https://openjdk.java.net/"
COPYRIGHT="2007-2015 Oracle and/or its affiliates."
LICENSE="GNU GPL v2"
REVISION="7"
SOURCE_URI="https://bitbucket.org/hamishm/haiku-jdk8u/get/6e3a7c2446ea.zip"
CHECKSUM_SHA256="68cb8171d84b1a0c12e20b2ecc968a12ed8f64d39858a6e3af9e287877c2245e"
SOURCE_DIR="hamishm-haiku-jdk8u-6e3a7c2446ea"
PATCHES="openjdk-1.8.u40_b27.patchset"
SOURCE_URI_2="https://bitbucket.org/hamishm/haiku-jdk8u-hotspot/get/e5ab6f7ff241.zip"
CHECKSUM_SHA256_2="241b78f84c7d39a1b93c78e5bf0e02414aae92574450fd043fa6771a9bf18103"
SOURCE_DIR_2="hamishm-haiku-jdk8u-hotspot-e5ab6f7ff241"
PATCHES_2="openjdk-1.8.u40_b27-source2.patchset"
SOURCE_URI_3="https://bitbucket.org/hamishm/haiku-jdk8u-jdk/get/9b3967c76020.zip"
CHECKSUM_SHA256_3="a64e0109b51f4bf9e5902a7053ea69fe3caa58df03146c2dc0d3c586e389ee86"
SOURCE_DIR_3="hamishm-haiku-jdk8u-jdk-9b3967c76020"
PATCHES_3="openjdk-1.8.u40_b27-source3.patchset"
SOURCE_URI_4="https://ports-mirror.haiku-os.org/openjdk/langtools-jdk8u40-b27.tar.bz2"
CHECKSUM_SHA256_4="5a9ee52b24bd3bf42ff7e0fa355ac4bc9327d1da3d60499897300129ca984366"
SOURCE_DIR_4="langtools-jdk8u40-b27"
SOURCE_FILENAME_4="langtools-jdk8u40-b27.tar.bz2"
SOURCE_URI_5="https://ports-mirror.haiku-os.org/openjdk/corba-jdk8u40-b27.tar.bz2"
CHECKSUM_SHA256_5="4e9b83d2716d41d5af3b86ddae9302c6516a6c9ba96c8724c84a32b6c2bc8e87"
SOURCE_DIR_5="corba-jdk8u40-b27"
SOURCE_FILENAME_5="corba-jdk8u40-b27.tar.bz2"
SOURCE_URI_6="https://ports-mirror.haiku-os.org/openjdk/jaxp-jdk8u40-b27.tar.bz2"
CHECKSUM_SHA256_6="408880924559130f714dbd5fc5ac6d3ec9b060490f20d3886490f92f6c0067b8"
SOURCE_DIR_6="jaxp-jdk8u40-b27"
SOURCE_FILENAME_6="jaxp-jdk8u40-b27.tar.bz2"
SOURCE_URI_7="https://ports-mirror.haiku-os.org/openjdk/jaxws-jdk8u40-b27.tar.bz2"
CHECKSUM_SHA256_7="31a95d3e6ed051e58d919c7d800e396cfbd48d43389dc955e10574d0e50151f9"
SOURCE_DIR_7="jaxws-jdk8u40-b27"
SOURCE_FILENAME_7="jaxws-jdk8u40-b27.tar.bz2"
SOURCE_URI_8="https://ports-mirror.haiku-os.org/openjdk/nashorn-jdk8u40-b27.tar.bz2"
CHECKSUM_SHA256_8="07fc791ea33a06b949a63d438479cb401762c4fd3334beecc77e4bf7ed4e2389"
SOURCE_DIR_8="nashorn-jdk8u40-b27"
SOURCE_FILENAME_8="nashorn-jdk8u40-b27.tar.bz2"
ADDITIONAL_FILES="
	elf.h
	"

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

DISABLE_SOURCE_PACKAGE=yes
	# at least as long as Ant and a complete SDK image are part of the "sources" package

PROVIDES="
	openjdk8$secondaryArchSuffix = $portVersion compat >= 1.8
	cmd:jexec = $portVersion compat >= 1.8
	cmd:appletviewer = $portVersion compat >= 1.8
	cmd:apt = $portVersion compat >= 1.8
	cmd:extcheck = $portVersion compat >= 1.8
	cmd:idlj = $portVersion compat >= 1.8
	cmd:jar = $portVersion compat >= 1.8
	cmd:jarsigner = $portVersion compat >= 1.8
	cmd:java = $portVersion compat >= 1.8
	cmd:java_config = $portVersion compat >= 1.8
	cmd:javac = $portVersion compat >= 1.8
	cmd:javadoc = $portVersion compat >= 1.8
	cmd:javah = $portVersion compat >= 1.8
	cmd:javap = $portVersion compat >= 1.8
	cmd:jcmd = $portVersion compat >= 1.8
	cmd:jconsole = $portVersion compat >= 1.8
	cmd:jdb = $portVersion compat >= 1.8
	cmd:jdeps = $portVersion compat >= 1.8
	cmd:jhat = $portVersion compat >= 1.8
	cmd:jinfo = $portVersion compat >= 1.8
	cmd:jjs = $portVersion compat >= 1.8
	cmd:jmap = $portVersion compat >= 1.8
	cmd:jps = $portVersion compat >= 1.8
	cmd:jrunscript = $portVersion compat >= 1.8
	cmd:jsadebugd = $portVersion compat >= 1.8
	cmd:jstack = $portVersion compat >= 1.8
	cmd:jstat = $portVersion compat >= 1.8
	cmd:jstatd = $portVersion compat >= 1.8
	cmd:keytool = $portVersion compat >= 1.8
	cmd:native2ascii = $portVersion compat >= 1.8
	cmd:orbd = $portVersion compat >= 1.8
	cmd:pack200 = $portVersion compat >= 1.8
	cmd:policytool = $portVersion compat >= 1.8
	cmd:rmic = $portVersion compat >= 1.8
	cmd:rmid = $portVersion compat >= 1.8
	cmd:rmiregistry = $portVersion compat >= 1.8
	cmd:schemagen = $portVersion compat >= 1.8
	cmd:serialver = $portVersion compat >= 1.8
	cmd:servertool = $portVersion compat >= 1.8
	cmd:tnameserv = $portVersion compat >= 1.8
	cmd:unpack200 = $portVersion compat >= 1.8
	cmd:wsgen = $portVersion compat >= 1.8
	cmd:wsimport = $portVersion compat >= 1.8
	cmd:xjc = $portVersion compat >= 1.8
	"
REQUIRES="
	haiku$secondaryArchSuffix
	lib:libfreetype$secondaryArchSuffix
	lib:libiconv$secondaryArchSuffix
	lib:libjpeg$secondaryArchSuffix
	lib:libz$secondaryArchSuffix
	ca_root_certificates_java
	"

SUMMARY_sources="JDK source files, demos and examples"
PROVIDES_sources="
	openjdk8${secondaryArchSuffix}_sources = $portVersion compat >= 1.8
	"
REQUIRES_sources="
	openjdk8$secondaryArchSuffix
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	openjdk$secondaryArchSuffix >= 1.7
	ca_root_certificates
	devel:libfreetype$secondaryArchSuffix
	devel:libiconv$secondaryArchSuffix
	devel:libjpeg$secondaryArchSuffix
	devel:libz$secondaryArchSuffix
	"
BUILD_PREREQUIRES="
	cmd:cpio
	cmd:make
	cmd:gcc$secondaryArchSuffix
	cmd:ld$secondaryArchSuffix
	cmd:sed
	cmd:tar
	cmd:zip
	cmd:awk
	cmd:hostname
	cmd:find
	cmd:unzip
	cmd:unzipsfx
	cmd:head
	cmd:file
	cmd:which
	cmd:autoconf
	"

BUILD()
{
	# prepare the directory structure
	ln -sfn $sourceDir2 hotspot
	ln -sfn $sourceDir3 jdk
	ln -sfn $sourceDir4 langtools
	ln -sfn $sourceDir5 corba
	ln -sfn $sourceDir6 jaxp
	ln -sfn $sourceDir7 jaxws
	ln -sfn $sourceDir8 nashorn

	cp $portDir/additional-files/elf.h hotspot/src/share/vm/utilities

	# If ASLR is enabled, the JVM can fail to find a large enough area for
	# the heap.
	export DISABLE_ASLR=1

	# Verify that we can allocate a large enough heap before starting.
	maxHeapSize=960M
	java -XX:ThreadStackSize=1536 -XX:MaxPermSize=160m \
		-Xmx${maxHeapSize} -version

	freeTypeHeaders=$(finddir B_SYSTEM_HEADERS_DIRECTORY)$secondaryArchSubDir/freetype2
	freeTypeLib=$(finddir B_SYSTEM_DEVELOP_DIRECTORY)/lib$secondaryArchSubDir

	#OpenJDK has an internal limitation of 256 characters in a path. And with the default src dir we go past it.
	cd ..
	mv $sourceDir src
	cd src
	ln -s src $sourceDir

	# set the C++ standard as the default changed since gcc6
	EXTRA_CXX_FLAGS="-std=gnu++98 -fno-delete-null-pointer-checks -fno-lifetime-dse"
	EXTRA_CFLAGS="-std=gnu++98 -Wno-error -fno-delete-null-pointer-checks -fno-lifetime-dse"

	bash ./common/autoconf/autogen.sh
	bash ./configure --with-boot-jdk-jvmargs="-Xmx${maxHeapSize}" \
	                 --with-freetype-include="${freeTypeHeaders}" \
	                 --with-freetype-lib="${freeTypeLib}" \
			 --with-extra-cxxflags="$EXTRA_CXX_FLAGS" \
			 --with-extra-cflags="$EXTRA_CFLAGS"

	make DEBUG_BINARIES=true SCTP_WERROR= images
}

INSTALL()
{
	# install the generated SDK image dir
	jdkDir=$libDir/openjdk

	mkdir -p $(dirname $jdkDir)
	cp -a build/haiku-*/images/j2sdk-image $jdkDir

	# set up the cacerts link
	ln -sf $dataDir/ssl/java/cacerts $jdkDir/jre/lib/security/

	# symlink the executables to binDir
	mkdir -p $prefix/bin
	symlinkRelative -s $jdkDir/bin/* $prefix/bin
		# TODO: We should probably link only a subset.

	# create a profile.d file that sets up JAVA_HOME
	jdkProfile=$dataDir/profile.d/openjdk.sh
	mkdir -p $(dirname $jdkProfile)
	echo "JAVA_HOME=$jdkDir" > $jdkProfile
	echo "export JAVA_HOME" >> $jdkProfile

	packageEntries sources \
		$libDir/openjdk/src.zip \
		$libDir/openjdk/sample \
		$libDir/openjdk/demo
}
