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-2018 Oracle and/or its affiliates."
LICENSE="GNU GPL v2"
REVISION="2"
jdkBuild="jdk-${portVersion%.*}+${portVersion##*.}"
srcGitRev="dcec3cae223a418402d1958bccee2cf730743c87"
SOURCE_URI="https://github.com/korli/haiku-jdk9u/archive/$srcGitRev.tar.gz"
CHECKSUM_SHA256="4863a0897ef994fd75c97693f9b181c645ad4e85f6ac71e586b87f238c42246c"
SOURCE_DIR="haiku-jdk9u-$srcGitRev"
SOURCE_FILENAME="jdk9u-$jdkBuild-$srcGitRev.tar.gz"
srcGitRev2="f0c77b8129cd4bee917d3e015114b5f5e3f756c2"
SOURCE_URI_2="https://github.com/korli/haiku-jdk9u-hotspot/archive/$srcGitRev2.tar.gz"
CHECKSUM_SHA256_2="9936ec44541fb9c8ca830732ffd14f0c7da3d696d1b090fb1a0208f6ba75eaf2"
SOURCE_DIR_2="haiku-jdk9u-hotspot-$srcGitRev2"
SOURCE_FILENAME_2="hotspot-$jdkBuild-$srcGitRev2.tar.gz"
srcGitRev3="6bed65ab56a0640b72f864cc55d9d60e47307d2d"
SOURCE_URI_3="https://github.com/korli/haiku-jdk9u-jdk/archive/$srcGitRev3.tar.gz"
CHECKSUM_SHA256_3="351ad82ad36d2d1eac4d98087a82da3cddaa15576adedd71045bf5c86047c12f"
SOURCE_DIR_3="haiku-jdk9u-jdk-$srcGitRev3"
SOURCE_FILENAME_3="jdk-$jdkBuild-$srcGitRev3.tar.gz"
SOURCE_URI_4="https://hg.openjdk.java.net/jdk-updates/jdk9u/langtools/archive/$jdkBuild.tar.bz2"
CHECKSUM_SHA256_4="6d07b7cf25c93ad2723b6d3418fda988275e4312549b26b9076f8b198bcfabed"
SOURCE_DIR_4="langtools-$jdkBuild"
SOURCE_FILENAME_4="langtools-$jdkBuild.tar.bz2"
SOURCE_URI_5="https://hg.openjdk.java.net/jdk-updates/jdk9u/corba/archive/$jdkBuild.tar.bz2"
CHECKSUM_SHA256_5="087c0b74aa9cb1bc00b2d783c569e598427fe757cce96431b616b7a065a3f974"
SOURCE_DIR_5="corba-$jdkBuild"
SOURCE_FILENAME_5="corba-$jdkBuild.tar.bz2"
SOURCE_URI_6="https://hg.openjdk.java.net/jdk-updates/jdk9u/jaxp/archive/$jdkBuild.tar.bz2"
CHECKSUM_SHA256_6="289f2a95356269366c871c8f5dc571bdc343009c0269206d2f03f9ec8388366d"
SOURCE_DIR_6="jaxp-$jdkBuild"
SOURCE_FILENAME_6="jaxp-$jdkBuild.tar.bz2"
SOURCE_URI_7="https://hg.openjdk.java.net/jdk-updates/jdk9u/jaxws/archive/$jdkBuild.tar.bz2"
CHECKSUM_SHA256_7="d23195c44c146136bcc266380ecd03df699ebe2ebd1100345eba3cd388a2468a"
SOURCE_DIR_7="jaxws-$jdkBuild"
SOURCE_FILENAME_7="jaxws-$jdkBuild.tar.bz2"
SOURCE_URI_8="https://hg.openjdk.java.net/jdk-updates/jdk9u/nashorn/archive/$jdkBuild.tar.bz2"
CHECKSUM_SHA256_8="f58404743fd6db74bf11cdf1c4cecd78ed7108924e4049e3eb89ffe217cf0be3"
SOURCE_DIR_8="nashorn-$jdkBuild"
SOURCE_FILENAME_8="nashorn-$jdkBuild.tar.bz2"
SOURCE_URI_9="https://ftp.osuosl.org/pub/blfs/conglomeration/openjdk/jtreg-4.2-b13-433.tar.gz"
CHECKSUM_SHA256_9="ddf76660d4f519302b9c6310febcde3ff45313c435fff4e157854d1ff50785b1"
SOURCE_DIR_9="jtreg"
SOURCE_URI_10="https://web.cs.ucla.edu/~eggert/tz/release/2018g/tzdata2018g-rearguard.tar.gz"
CHECKSUM_SHA256_10="2df3ae0fa3e8a76e5f8b3bdb3bfc93afad3adccd14a02b74a5e64049851ffa9d"
SOURCE_DIR_10=""
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="
	openjdk9$secondaryArchSuffix = $portVersion compat >= 9
	java:environment = 9
	"
REQUIRES="
	openjdk9${secondaryArchSuffix}_jre == $portVersion
	"

PROVIDES_default="
	openjdk9${secondaryArchSuffix}_default = $portVersion
	cmd:jar = $portVersion compat >= 9
	cmd:jarsigner = $portVersion compat >= 9
	cmd:java = $portVersion compat >= 9
	cmd:javac = $portVersion compat >= 9
	cmd:javadoc = $portVersion compat >= 9
	cmd:javah = $portVersion compat >= 9
	cmd:javap = $portVersion compat >= 9
	cmd:jcmd = $portVersion compat >= 9
	cmd:jconsole = $portVersion compat >= 9
	cmd:jdb = $portVersion compat >= 9
	cmd:jinfo = $portVersion compat >= 9
	cmd:jmap = $portVersion compat >= 9
	cmd:jps = $portVersion compat >= 9
	cmd:jstack = $portVersion compat >= 9
	cmd:jstat = $portVersion compat >= 9
	cmd:jstatd = $portVersion compat >= 9
	cmd:keytool = $portVersion compat >= 9
	cmd:orbd = $portVersion compat >= 9
	cmd:pack200 = $portVersion compat >= 9
	cmd:rmic = $portVersion compat >= 9
	cmd:rmid = $portVersion compat >= 9
	cmd:rmiregistry = $portVersion compat >= 9
	cmd:schemagen = $portVersion compat >= 9
	cmd:serialver = $portVersion compat >= 9
	cmd:servertool = $portVersion compat >= 9
	cmd:tnameserv = $portVersion compat >= 9
	cmd:unpack200 = $portVersion compat >= 9
	cmd:wsgen = $portVersion compat >= 9
	cmd:wsimport = $portVersion compat >= 9
	cmd:xjc = $portVersion compat >= 9
	"
REQUIRES_default="
	openjdk9${secondaryArchSuffix} == $portVersion
	"
CONFLICTS_default="
	openjdk8${secondaryArchSuffix}_default
	"

PROVIDES_jre="
	openjdk9${secondaryArchSuffix}_jre = $portVersion compat >= 9
	java:runtime = 9
	"
REQUIRES_jre="
	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="
	openjdk9${secondaryArchSuffix}_sources = $portVersion compat >= 9
	"
REQUIRES_sources="
	openjdk9$secondaryArchSuffix
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	java:environment == 8
	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
	cmd:pkg_config$secondaryArchSuffix
	"

TEST_REQUIRES="
	cmd:true
	"

BUILD()
{
	source /system/data/profile.d/openjdk8.sh
	export PATH=$JDK8_HOME/bin:$PATH
	export COMPANY=HaikuPorts

	# 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
	ln -sfn $sourceDir9 jtreg

	cp $portDir/additional-files/elf.h hotspot/src/share/vm/utilities
	cp -f $sourceDir10/* $sourceDir3/make/data/tzdata/

	# 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 -Xmx${maxHeapSize} -version

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

	bash ./common/autoconf/autogen.sh
	bash ./configure --with-boot-jdk-jvmargs="-Xmx${maxHeapSize}" \
					--with-freetype-include="${freeTypeHeaders}" \
					--with-freetype-lib="${freeTypeLib}" \
					--disable-freetype-bundling \
					--with-jtreg=./jtreg

	make images
}

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

	mkdir -p $jdkDir
	cp -a build/haiku-*/images/jre/* $jdkDir
	cp -a build/haiku-*/images/jdk/* $jdkDir

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

	# symlink the executables to binDir
	mkdir -p $prefix/bin
	bins="jar jarsigner javac javadoc javah javap jcmd jconsole jdb jinfo \
		jmap jps jstack jstat jstatd rmic schemagen serialver wsgen wsimport \
		xjc"
	bins_runtime="java keytool orbd pack200 rmid rmiregistry servertool \
		tnameserv unpack200"
	man_runtime=""
	for b in $bins $bins_runtime; do
		symlinkRelative -s $jdkDir/bin/$b $prefix/bin
	done
	#for b in $bins_runtime; do
	#	man_runtime+=" $jdkDir/man/man1/$b.1 $jdkDir/man/ja_JP.UTF-8/man1/$b.1"
	#done

	mkdir -p $dataDir/profile.d

	# create a profile.d file that sets up JAVA_HOME
	jdkProfile=$dataDir/profile.d/openjdk.sh
	echo "JAVA_HOME=$jdkDir" > $jdkProfile
	echo "export JAVA_HOME" >> $jdkProfile

	# create a profile.d file that sets up JDK9_HOME
	jdk9Profile=$dataDir/profile.d/openjdk9.sh
	echo "JDK9_HOME=$jdkDir" > $jdk9Profile
	echo "export JDK9_HOME" >> $jdk9Profile

	# create a profile.d file that sets up JRE9_HOME
	jre9Profile=$dataDir/profile.d/openjre9.sh
	echo "JRE9_HOME=$(getPackagePrefix jre)/$relativeLibDir/openjdk9" > $jre9Profile
	echo "export JRE9_HOME" >> $jre9Profile

	find $jdkDir -name '*.diz' -delete
	# not for jre
	mv $jdkDir/lib/libattach.so $jdkDir/lib/ct.sym $prefix

	packageEntries sources \
		$jdkDir/lib/src.zip \
		$jdkDir/demo

	packageEntries jre \
		$jdkDir/bin/appletviewer \
		$jdkDir/bin/idlj \
		$jdkDir/bin/java \
		$jdkDir/bin/jjs \
		$jdkDir/bin/jrunscript \
		$jdkDir/bin/keytool \
		$jdkDir/bin/orbd \
		$jdkDir/bin/pack200 \
		$jdkDir/bin/rmid \
		$jdkDir/bin/rmiregistry \
		$jdkDir/bin/servertool \
		$jdkDir/bin/tnameserv \
		$jdkDir/bin/unpack200 \
		$jdkDir/conf \
		$jdkDir/legal \
		$jdkDir/lib \
		$jdkDir/release \
		$dataDir/profile.d/openjre9.sh
	#	$man_runtime \
	#	$jdkDir/man/ja \

	mkdir -p $jdkDir/lib
	mv $prefix/libattach.so $prefix/ct.sym $jdkDir/lib/

	packageEntries default \
		$prefix/bin \
		$dataDir/profile.d/openjdk.sh
}

TEST()
{
	export DISABLE_ASLR=1
	cd ../src
	make test-only JOBS=1 TEST=langtools_jtreg
	make test-only JOBS=1 TEST=jdk_lang
	make test-only JOBS=1 TEST=jdk_util
	make test-only JOBS=1 TEST=jdk_math # OK
	make test-only JOBS=1 TEST=jdk_io
	make test-only JOBS=1 TEST=jdk_nio
	make test-only JOBS=1 TEST=jdk_net
	make test-only JOBS=1 TEST=jdk_time # OK
	make test-only JOBS=1 TEST=jdk_rmi
	make test-only JOBS=1 TEST=jdk_security
	make test-only JOBS=1 TEST=jdk_text # OK
	make test-only JOBS=1 TEST=jdk_management
	make test-only JOBS=1 TEST=jdk_instrument
	make test-only JOBS=1 TEST=jdk_jmx
	make test-only JOBS=1 TEST=jdk_jdi
}
