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-2019 Oracle and/or its affiliates."
LICENSE="GNU GPL v2"
REVISION="1"
jdkBuild="jdk-${portVersion%.*}+${portVersion##*.}"
srcGitRev="7c3bc110a54073826adc95e5a7845a694cf835ca"
SOURCE_URI="https://github.com/korli/haiku-jdk11u/archive/$srcGitRev.tar.gz"
CHECKSUM_SHA256="59b9b0a1984f95c4bac185232e492c42905578fff88091adf2f9ba9d7b9898c6"
SOURCE_DIR="haiku-jdk11u-$srcGitRev"
SOURCE_FILENAME="jdk11u-$jdkBuild-$srcGitRev.tar.gz"
SOURCE_URI_2="https://ftp.osuosl.org/pub/blfs/conglomeration/openjdk/jtreg-4.2-b13-433.tar.gz"
CHECKSUM_SHA256_2="ddf76660d4f519302b9c6310febcde3ff45313c435fff4e157854d1ff50785b1"
SOURCE_DIR_2="jtreg"
PATCHES="openjdk11-$portVersion.patchset"
ADDITIONAL_FILES="
	elf.h
	"

ARCHITECTURES="all !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="
	openjdk11$secondaryArchSuffix = $portVersion compat >= 11
	java:environment = 11
	"
REQUIRES="
	openjdk11${secondaryArchSuffix}_jre == $portVersion
	"

PROVIDES_default="
	openjdk11${secondaryArchSuffix}_default = $portVersion
	cmd:jar = $portVersion compat>= 11
	cmd:jarsigner = $portVersion compat>= 11
	cmd:java = $portVersion compat>= 11
	cmd:javac = $portVersion compat>= 11
	cmd:javadoc = $portVersion compat>= 11
	cmd:javah = $portVersion compat>= 11
	cmd:javap = $portVersion compat>= 11
	cmd:jcmd = $portVersion compat>= 11
	cmd:jconsole = $portVersion compat>= 11
	cmd:jdb = $portVersion compat>= 11
	cmd:jinfo = $portVersion compat>= 11
	cmd:jmap = $portVersion compat>= 11
	cmd:jps = $portVersion compat>= 11
	cmd:jstack = $portVersion compat>= 11
	cmd:jstat = $portVersion compat>= 11
	cmd:jstatd = $portVersion compat>= 11
	cmd:keytool = $portVersion compat>= 11
	cmd:pack200 = $portVersion compat>= 11
	cmd:rmic = $portVersion compat>= 11
	cmd:rmid = $portVersion compat>= 11
	cmd:rmiregistry = $portVersion compat>= 11
	cmd:serialver = $portVersion compat>= 11
	cmd:unpack200 = $portVersion compat>= 11
	"
REQUIRES_default="
	openjdk11${secondaryArchSuffix} == $portVersion
	"
CONFLICTS_default="
	openjdk8${secondaryArchSuffix}_default
	openjdk9${secondaryArchSuffix}_default
	openjdk10${secondaryArchSuffix}_default
	"

PROVIDES_jre="
	openjdk11${secondaryArchSuffix}_jre = $portVersion compat>= 11
	java:runtime = 11
	"
REQUIRES_jre="
	haiku$secondaryArchSuffix
	lib:libfreetype$secondaryArchSuffix
	lib:libiconv$secondaryArchSuffix
	lib:libjpeg$secondaryArchSuffix
	lib:libz$secondaryArchSuffix
	ca_root_certificates_java
	dejavu
	"

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

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	java:environment == 10
	ca_root_certificates
	devel:libfontconfig$secondaryArchSuffix
	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:gawk
	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/openjdk10.sh
	export PATH=$JDK10_HOME/bin:$PATH
	export COMPANY=HaikuPorts

	ln -sfn $sourceDir2 jtreg

	cp $portDir/additional-files/elf.h src/hotspot/share/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.
	java -XX:ThreadStackSize=1536 -Xmx1024M -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

	bash ./configure --with-boot-jdk-jvmargs="-XX:CompressedClassSpaceSize=512m" \
					--with-freetype-include="${freeTypeHeaders}" \
					--with-freetype-lib="${freeTypeLib}" \
					--with-jtreg=./jtreg \
					--with-version-build="${portVersion//*.}" \
					--with-version-pre="" \
					--with-version-opt="" \
					--with-num-cores=1 \
					--disable-javac-server

	make images LOG=info
}

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

	mkdir -p $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 serialver"
	bins_runtime="java keytool pack200 rmid rmiregistry 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 JDK11_HOME
	jdk11Profile=$dataDir/profile.d/openjdk11.sh
	echo "JDK11_HOME=$jdkDir" > $jdk11Profile
	echo "export JDK11_HOME" >> $jdk11Profile

	# create a profile.d file that sets up JRE11_HOME
	jre11Profile=$dataDir/profile.d/openjre11.sh
	echo "JRE11_HOME=$(getPackagePrefix jre)/$relativeLibDir/openjdk11" > $jre11Profile
	echo "export JRE11_HOME" >> $jre11Profile

	find $jdkDir -name '*.diz' -o -name '*.debuginfo' -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/java \
		$jdkDir/bin/jjs \
		$jdkDir/bin/jrunscript \
		$jdkDir/bin/keytool \
		$jdkDir/bin/pack200 \
		$jdkDir/bin/rmid \
		$jdkDir/bin/rmiregistry \
		$jdkDir/bin/unpack200 \
		$jdkDir/conf \
		$jdkDir/legal \
		$jdkDir/lib \
		$jdkDir/release \
		$dataDir/profile.d/openjre11.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
	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
	make test-only JOBS=1 TEST=jdk_rmi
	make test-only JOBS=1 TEST=jdk_security
	make test-only JOBS=1 TEST=jdk_text
	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
}
