# Maintainer: Celeste <cielesti@protonmail.com>
_majorver=23
_buildver=25
_loongver=0
pkgname=openjdk$_majorver-loongarch
pkgver=0.0.$_buildver.$_loongver
pkgrel=0
_gittag="jdk-$_majorver+$_buildver-ls-$_loongver"
_prevmajorver=$((_majorver - 1))
_provide_java=false
pkgdesc="Loongarch port of OpenJDK $_majorver"
url="https://github.com/loongson/jdk"
arch="loongarch64"
license="GPL-2.0-only WITH Classpath-exception-2.0"
options="sover-namecheck"
makedepends_build="
	openjdk$_prevmajorver-loongarch
	autoconf
	bash
	gawk
	grep
	make
	zip
	zlib-dev
	"
makedepends_host="
	alsa-lib-dev
	cups-dev
	elfutils-dev
	fontconfig-dev
	freetype-dev
	giflib-dev
	lcms2-dev
	libffi-dev
	libjpeg-turbo-dev
	libx11-dev
	libxext-dev
	libxrandr-dev
	libxrender-dev
	libxt-dev
	libxtst-dev
	linux-headers
	zlib-dev
	"
makedepends="
	$makedepends_build
	$makedepends_host
	java-cacerts
	java-common
	"
subpackages="
	$pkgname-demos::noarch
	$pkgname-doc
	$pkgname-jmods::noarch
	$pkgname-jre
	$pkgname-jre-headless:jre_headless
	$pkgname-jdk
	"
$_provide_java && subpackages="$subpackages
	$pkgname-static-libs:static_libs
	"
_gtest=1.15.2
source="$pkgname-$_gittag.tar.gz::https://github.com/loongson/jdk/archive/refs/tags/$_gittag.tar.gz
	gtest-$_gtest.tar.gz::https://github.com/google/googletest/archive/v$_gtest.tar.gz
	JDK-8218814_Wformat-security.patch
	JDK-8299245_disable-tests.patch
	basename.patch
	fpu_control.patch
	lfs64.patch
	"
builddir="$srcdir/jdk-${_gittag/+/-}"

provides="openjdk$_majorver-bootstrap=$pkgver-r$pkgrel"
provider_priority=$_majorver

_java_home="/usr/lib/jvm/java-$_majorver-openjdk"

ldpath="$_java_home/lib:$_java_home/lib/server"
sonameprefix="$pkgname:"

# enable running the JTReg tests in check?
# see comment in that function for explanation
_run_jtreg=${_run_jtreg:-0}
if [ $_run_jtreg -ne 0 ]; then
	makedepends="$makedepends java-jtreg"
	checkdepends="$checkdepends font-freefont xvfb-run"
fi

_jvm_variants=server

build() {
	if [ $_run_jtreg -ne 0 ]; then
		_with_jtreg="--with-jtreg=/usr/share/java/jtreg"
	else
		_with_jtreg="--with-jtreg=no"
	fi

	if [ -n "$USE_CCACHE" ]; then
		# workaround ccache being disallowed
		export PATH="/usr/bin:/bin:/sbin:/usr/sbin"
		local ccache="--enable-ccache"
	fi

	if [ -z "$JOBS" ]; then
		export JOBS=$(printf '%s\n' "$MAKEFLAGS" | sed -n -e 's/.*-j\([0-9]\+\).*/\1/p')
	fi

	# we want to build hotspot with better optimisations; it's set to this
	# (prepended) anyway, and it's huge
	export CFLAGS="$CFLAGS -O3"
	export CXXFLAGS="$CXXFLAGS -O3"

	# CFLAGS, CXXFLAGS and LDFLAGS are ignored as shown by a warning
	# in the output of ./configure unless used like such:
	#  --with-extra-cflags="$CFLAGS"
	#  --with-extra-cxxflags="$CXXFLAGS"
	#  --with-extra-ldflags="$LDFLAGS"
	# See also paragraph "Configure Control Variables" from "common/doc/building.md"
	# shellcheck disable=2097 disable=2098
	CFLAGS='' CXXFLAGS='' LDFLAGS='' \
		bash ./configure \
		--build=$CBUILD \
		--openjdk-target=$CHOST \
		--prefix="$_java_home" \
		--sysconfdir=/etc \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--localstatedir=/var \
		--with-extra-cflags="$CFLAGS" \
		--with-extra-cxxflags="$CXXFLAGS" \
		--with-extra-ldflags="$LDFLAGS" \
		--with-zlib=system \
		--with-libjpeg=system \
		--with-giflib=system \
		--with-libpng=system \
		--with-lcms=system \
		--with-jobs=${JOBS:-4} \
		--with-test-jobs=${JOBS:-4} \
		--with-native-debug-symbols=none \
		--with-gtest="$srcdir/googletest-$_gtest" \
		$ccache \
		$_with_jtreg \
		--disable-warnings-as-errors \
		--disable-precompiled-headers \
		--enable-dtrace=no \
		--with-jvm-variants=$_jvm_variants \
		--with-debug-level=release \
		--with-version-pre= \
		--with-version-build="$_buildver" \
		--with-version-opt="ls-$_loongver-alpine-r$pkgrel" \
		--with-vendor-name="Alpine" \
		--with-vendor-url="https://alpinelinux.org/" \
		--with-vendor-bug-url="https://gitlab.alpinelinux.org/alpine/aports/issues" \
		--with-vendor-vm-bug-url="https://gitlab.alpinelinux.org/alpine/aports/issues"

	MAKEFLAGS='' make jdk-image
	if $_provide_java; then
		MAKEFLAGS='' make static-libs-image
	fi
}

check() {
	# run the gtest unittest suites
	# they don't take long, DO NOT DISABLE THEM!
	# override make variable, disable -Werror
	MAKEFLAGS='' make test-hotspot-gtest

	# The jtreg tests take very, very long to finish and show some failures (9 - 12 on my machine, varying between runs)
	# I think these are not critical and can be safely ignored.
	# As the tests take too long, they are disabled by default.
	# When updating this aport please let them run at least once on your machine to see if the failure count changes.
	if [ $_run_jtreg -ne 0 ]; then
		_logfile=$( mktemp -p "$builddir" )
		MAKEFLAGS='' xvfb-run make \
			run-test-tier1 \
			run-test-tier2 \
			run-test-tier3 \
			| tee "$_logfile"
		msg "---------------------------------------"
		msg "The build log can be found at $_logfile"
		# abort the build so you may take a look at the logfile
		false
		return 1
	fi
}

package() {
	depends="$pkgname-jdk=$pkgver-r$pkgrel"

	install -Dvm644 ASSEMBLY_EXCEPTION LICENSE README.md \
		-t "$pkgdir"/$_java_home/

	cd build/linux-*-$_jvm_variants-release/images/
	cp -a jdk/* "$pkgdir"/$_java_home/
	rm -v "$pkgdir"/$_java_home/lib/src.zip

	if $_provide_java; then
		local libstaticdir="lib/static/linux-loongarch64/musl"
		mkdir -p "$pkgdir"/$_java_home/$libstaticdir
		cp -a static-libs/lib/* "$pkgdir"/$_java_home/$libstaticdir/
	fi

	# symlink to shared java cacerts store
	rm -v "$pkgdir"/$_java_home/lib/security/cacerts
	ln -sv /etc/ssl/certs/java/cacerts \
		"$pkgdir"/$_java_home/lib/security/cacerts

	# symlink for java-common to work (expects jre in $_java_home/jre)
	ln -sv . "$pkgdir"/$_java_home/jre
}

jmods() {
	pkgdesc="$pkgdesc (jmods)"
	depends=

	provides="openjdk$_majorver-jmods"
	replaces="openjdk$_majorver-jmods"

	amove $_java_home/jmods
}

static_libs() {
	pkgdesc="$pkgdesc (libraries for static linking)"
	depends="$pkgname-jdk=$pkgver-r$pkgrel"

	$_provide_java && provides="java-jdk-static-libs"
	provides="$provides openjdk$_majorver-static-libs"
	replaces="openjdk$_majorver-static-libs"

	amove $_java_home/lib/static
}

jre() {
	pkgdesc="$pkgdesc (JRE)"
	depends="$pkgname-jre-headless=$pkgver-r$pkgrel"

	$_provide_java && provides="java-jre"
	provides="$provides openjdk$_majorver-jre"
	replaces="openjdk$_majorver-jre"

	amove \
		$_java_home/lib/libawt_xawt.so \
		$_java_home/lib/libfontmanager.so \
		$_java_home/lib/libjavajpeg.so \
		$_java_home/lib/libjawt.so \
		$_java_home/lib/libjsound.so \
		$_java_home/lib/liblcms.so \
		$_java_home/lib/libsplashscreen.so
}

jre_headless() {
	pkgdesc="$pkgdesc (JRE headless)"
	depends="java-common java-cacerts"

	$_provide_java && provides="java-jre-headless"
	provides="$provides openjdk$_majorver-jre-headless"
	replaces="openjdk$_majorver-jre-headless"

	amove \
		$_java_home/bin/java \
		$_java_home/bin/jfr \
		$_java_home/bin/jrunscript \
		$_java_home/bin/jwebserver \
		$_java_home/bin/keytool \
		$_java_home/bin/rmiregistry \
		$_java_home/conf \
		$_java_home/jre \
		$_java_home/release

	# Rest of the lib subdir (which were not taken by -jre subpkg).
	amove $_java_home/lib

	# ct.sym should stay in -jdk
	mkdir -p "$pkgdir"/$_java_home/lib
	mv "$subpkgdir"/$_java_home/lib/ct.sym "$pkgdir"/$_java_home/lib
}

doc() {
	default_doc

	amove $_java_home/man
}

demos() {
	pkgdesc="$pkgdesc (demos)"
	depends="$pkgname-jdk=$pkgver-r$pkgrel"

	provides="openjdk$_majorver-demos"
	replaces="openjdk$_majorver-demos"

	amove $_java_home/demo
}

jdk() {
	pkgdesc="$pkgdesc (JDK) ($_jvm_variants variant)"
	depends="$pkgname-jre=$pkgver-r$pkgrel $pkgname-jmods=$pkgver-r$pkgrel"

	$_provide_java && provides="java-jdk"
	provides="$provides openjdk$_majorver-jdk"
	replaces="openjdk$_majorver-jdk"

	amove \
		$_java_home/bin \
		$_java_home/lib \
		$_java_home/include
}

sha512sums="
33ce2d1ae6158426806c68150024ddc7e6613e7eb70dd7dbcb5d175b94fe90e9e83ed6dba3788c1ed529241514886688a0c7bc2c39c74dc10e98a5afa934ba4f  openjdk23-loongarch-jdk-23+25-ls-0.tar.gz
9046841044a2bf7edfd96854ad9c44ffae4fcb9fb59a075b367507c0762a98eb32cb6968d46663228272e26321e96f4dd287c95baa22c6af9bad902b8b6ede4e  gtest-1.15.2.tar.gz
25dd3e79a9132b271b4d1d3c70b8719290c69e9143969e06cf8e22f0a3f2b76c583a8df9631c1e021146a4f42f27071c84f0415c9094bd8c895c2af86b6f547a  JDK-8218814_Wformat-security.patch
403862d6e6919f40d705c59463e9d6ecc5ac5b3f95e107a7e91f84d64068176e4abe7b63f15ba8fc9b4ab2f1ab58f3a489e07716e4c370356e40c2699e870b4f  JDK-8299245_disable-tests.patch
78442694805b2f09330c1911f170c154d854192a75745637d4db688f6ded7ef38bb97c43ee63db5f3e47c5e2ea05f4713c0a5b578ad9b1e86f39b15d341d2532  basename.patch
e1869d5a0ff74883b8289eb9feb5e919894cfcf418e0a07ce353d4ff2f678e81cbbba3bcf7b3642aefda963e9d9f2e6c33974ad4bb55da8aab1618ff98fd1fe1  fpu_control.patch
bfc615e3bbf9efc6c017bb882c465755afac3dce26a5ca1b6d9b2cfbd8096bc835c8b38c458f17d685b25d8dbab4946a22e9803aa04b5ef1d9bda5be7d38b34f  lfs64.patch
"
