# Maintainer: Celeste <cielesti@protonmail.com>
pkgname=openjdk8-loongarch
pkgver=8.422.05
pkgrel=0
_majorver=${pkgver%%.*}
_pkgver=${pkgver#*.}
_pkgver=${_majorver}u${_pkgver/\./-b}-ls-ga
pkgdesc="Loongarch port of OpenJDK $_majorver"
url="https://github.com/loongson/jdk${_majorver}u"
arch="loongarch64"
license="GPL-2.0-only WITH Classpath-exception-2.0"
options="sover-namecheck"
makedepends_build="
	openjdk$_majorver-bootstrap
	autoconf
	bash
	gawk
	grep
	make
	sed
	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
	"
[ "$CBUILD" = "$CTARGET" ] && makedepends="
	$makedepends_build
	$makedepends_host
	java-cacerts
	java-common
	"
subpackages="
	$pkgname-demos
	$pkgname-doc
	$pkgname-jre
	$pkgname-jre-lib:jrelib:noarch
	$pkgname-jre-base:jrebase
	$pkgname-jdk
	"
source="jdk-$_pkgver.tar.gz::https://github.com/loongson/jdk${_majorver}u/archive/refs/tags/jdk$_pkgver.tar.gz
	icedtea-hotspot-lfs64.patch
	icedtea-hotspot-musl.patch
	icedtea-hotspot-noagent-musl.patch
	icedtea-int-conversion.patch
	icedtea-issue13032.patch
	icedtea-jdk-disable-vfork.patch
	icedtea-jdk-execinfo.patch
	icedtea-jdk-fix-ipv6-init.patch
	icedtea-jdk-fix-libjvm-load.patch
	icedtea-jdk-implicit.patch
	icedtea-jdk-includes.patch
	icedtea-jdk-musl.patch
	icedtea-pointer-types.patch
	loongarch-fpu_control.patch
	Example.java
	"
builddir="$srcdir/jdk${_majorver}u-jdk$_pkgver"

case "$CARCH" in
loongarch64)
	_jarch="$CARCH"
	_jvm_variants=server
	provides="openjdk$_majorver-bootstrap openjdk$_majorver"
	replaces="openjdk$_majorver"
	provider_priority=$((_majorver + 1))
	;;
esac

_java_home="/usr/lib/jvm/java-1.8-openjdk"
_jrelib="$_java_home/jre/lib/$_jarch"

# Exclude xawt from ldpath to avoid duplicate provides for libmawt.so
# (also in headless). in future this should be a virtual provides.
ldpath="$_jrelib:$_jrelib/native_threads:$_jrelib/headless:$_jrelib/server:$_jrelib/jli"
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

prepare() {
	default_prepare

	# update autoconf files to detect alpine
	update_config_guess
	update_config_sub

	case "$CARCH" in
	loongarch64)
		;;
	*)
		error "Please use community/openjdk8"
		return 1
		;;
	esac
}

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

	# 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 \
		--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-giflib=system \
		--with-jobs=${JOBS:-4} \
		--with-native-debug-symbols=none \
		$ccache \
		$_with_jtreg \
		--with-milestone="fcs" \
		--with-jvm-variants=$_jvm_variants \
		--with-debug-level=release \
		--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 JOBS="${JOBS:-4}" images
}

check() {
	cd build/linux-*-$_jvm_variants-release/images/j2sdk-image

	./bin/java -version

	./bin/javac "$srcdir"/Example.java -d "$builddir"
	./bin/java -classpath "$builddir" Example
}

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

	mkdir -p "$pkgdir"/$_java_home

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

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

jrelib() {
	pkgdesc="$pkgdesc Java Runtime (class libraries)"
	depends="java-cacerts"
	provides="openjdk$_majorver-jre-lib"
	replaces="openjdk$_majorver-jre-lib"

	amove \
		$_java_home/jre/lib/images \
		$_java_home/jre/lib/\*.jar \
		$_java_home/jre/lib/security \
		$_java_home/jre/lib/ext/\*.jar \
		$_java_home/jre/lib/cmm \
		$_java_home/jre/ASSEMBLY_EXCEPTION \
		$_java_home/jre/THIRD_PARTY_README \
		$_java_home/jre/LICENSE
}

jre() {
	pkgdesc="$pkgdesc Java Runtime"
	depends="font-dejavu"
	provides="java-jre openjdk$_majorver-jre"
	replaces="openjdk$_majorver-jre"

	amove \
		$_java_home/jre/bin/policytool \
		$_java_home/bin/appletviewer \
		$_java_home/bin/policytool \
		$_java_home/jre/lib/$_jarch/libawt_xawt.so \
		$_java_home/jre/lib/$_jarch/libfontmanager.so \
		$_java_home/jre/lib/$_jarch/libjawt.so \
		$_java_home/jre/lib/$_jarch/libjsoundalsa.so \
		$_java_home/jre/lib/$_jarch/libsplashscreen.so
}

jrebase() {
	pkgdesc="$pkgdesc Java Runtime (no GUI support)"
	depends="$pkgname-jre-lib=$pkgver-r$pkgrel java-common"
	provides="java-jre-headless openjdk$_majorver-jre-base"
	replaces="openjdk$_majorver-jre-base"

	amove \
		$_java_home/bin/java \
		$_java_home/bin/orbd \
		$_java_home/bin/rmid \
		$_java_home/bin/servertool \
		$_java_home/bin/unpack200 \
		$_java_home/bin/keytool \
		$_java_home/bin/pack200 \
		$_java_home/bin/rmiregistry \
		$_java_home/bin/tnameserv \
		$_java_home/lib/$_jarch/jli

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

	ln -s java-1.8-openjdk "$subpkgdir"/usr/lib/jvm/java-8-openjdk
}

doc() {
	default_doc

	amove $_java_home/man
}

demos() {
	pkgdesc="$pkgdesc Java Demos and Samples"
	depends="$pkgname-jdk=$pkgver-r$pkgrel"
	provides="openjdk$_majorver-demos"
	replaces="openjdk$_majorver-demos"

	amove \
		$_java_home/demo \
		$_java_home/sample
}

jdk() {
	pkgdesc="$pkgdesc (JDK) ($_jvm_variants variant)"
	depends="$pkgname-jre=$pkgver-r$pkgrel"
	provides="java-jdk openjdk$_majorver-jdk"
	replaces="openjdk$_majorver-jdk"

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

sha512sums="
bb12ea5d8c12b728095b0353857bcc782b343874299195b39dcf183124b245b279bf82736267ecdaf82c383b37b9bbb99159c668e9122c6472874db1d9d5cb39  jdk-8u422-b05-ls-ga.tar.gz
6717172162cfc638de0e7a9ef80fd512f215a094d3dd6ca472c735445e612a804c4e97ced32a9618385130c054b6d333670ab2bd644f4f11daf150f93cd077d1  icedtea-hotspot-lfs64.patch
28709285390a997adbd56ebda42ef718fbc08daf572b8568f484436d255514f9d25f033e3333dff8aa352fc9846057ac5bb42fa955d3e5e44eddc96dc273c07c  icedtea-hotspot-musl.patch
19459dbb922f5a71cd15b53199481498626a783c24f91d2544d55b7dddd2cdb34a64bbf0226b99548612dd1743af01b3f9ff32c30abbbc90ce727ca2dbbbd1f9  icedtea-hotspot-noagent-musl.patch
4fcad23d533d6a1fd5526223cb76330aebbbbab3766c43a5b6d5d8cc9c729381d153cced81aae2666798c82e90e10e193db33207b357e01fb5ebd0b686263394  icedtea-int-conversion.patch
e1bca07aa1a25258ee9c8e9870f8d475db788bab1a354c5458196012aaf20fba8b3968b646d18792ab7135e9b75cbebafc53fda176166bc6ddfd13e4a43b1c8d  icedtea-issue13032.patch
c70fee8f1a10c155494ab9c6e55e979d8a3e23f12358ffe9de2029ae15feff361957f1aa43af43c90ecf00523f92e3333b40311a8d0c132ebd456486cb85ed8a  icedtea-jdk-disable-vfork.patch
f6365cfafafa008bd6c1bf0ccec01a63f8a39bd1a8bc87baa492a27234d47793ba02d455e5667a873ef50148df3baaf6a8421e2da0b15faac675867da714dd5f  icedtea-jdk-execinfo.patch
48533f87fc2cf29d26b259be0df51087d2fe5b252e72d00c6ea2f4add7b0fb113141718c116279c5905e03f64a1118082e719393786811367cf4d472b5d36774  icedtea-jdk-fix-ipv6-init.patch
b135991c76b0db8fa7c363e0903624668e11eda7b54a943035c214aa4d7fc8c3e8110ed200edcec82792f3c9393150a9bd628625ddf7f3e55720ff163fbbb471  icedtea-jdk-fix-libjvm-load.patch
21a16599d7e0aaf2997db59ada5af63e25484949ca965a34536ee44b950a132a6b5ca0b4922a6919d840a84ba0bb8413d87d2b97ea2c65c00e8766cc470c267d  icedtea-jdk-implicit.patch
de246b2f23a9aa1054e327bdf4ab34136d9480b34f7646f9823bddd9bc252131c0b05d790778fc5df3816e2386bdee5178a37a2ec7f30919e137a775dfd6fbb4  icedtea-jdk-includes.patch
3b01de971f64f082d3e289cf337e635ef001381e8ca427a77baa9c52c7ba423889f57665779ca5b3c8bcefb8feacbea31dfaac580c969a4f061439069ee34aae  icedtea-jdk-musl.patch
4380e167a1600d980b5aa53941cdaf3a83c742169d174af39efb81b544d6961d0e77e9cd37eb7b7afd14e205d0039f47f2b1452a8c4010b1295dca0d70a9d435  icedtea-pointer-types.patch
5f3552dbb430a84fef70c0f6594f2c1db3bd51a1a17cd7d137ded804d35e0430815ddf1aebc58507875d17d5392af6e005027da54651f71cf1e09d1cba10dd53  loongarch-fpu_control.patch
601ac59e59d32cbaf91f7541bd8173b38c68cbec96a7fb74227017afc8e5dc6ce4b8be03e92dd34cf52370e5d52beb780808af57b17c79c53ae8ec64cd34a92d  Example.java
"
