# Template file for 'openjdk17'
pkgname=openjdk17
version=17.0.5+7
revision=1
_gtest_ver=1.8.1
_java_ver="${version%%.*}"
_jdk_update="${version#*+}"
_base_version="${version%+*}"
_jdk_home="usr/lib/jvm/openjdk${_java_ver}"
archs="x86_64* i686* aarch64* armv7* ppc64*"
create_wrksrc=yes
build_wrksrc="jdk${_java_ver}u-jdk-${version/+/-}"
build_style=gnu-configure
configure_args="--disable-warnings-as-errors
 --enable-unlimited-crypto
 --with-zlib=system
 --with-libjpeg=system
 --with-giflib=system
 --with-libpng=system
 --with-lcms=system
 --with-jtreg=no
 --with-gtest=../googletest-release-${_gtest_ver}
 --with-debug-level=release
 --with-native-debug-symbols=internal
 --with-jobs=${XBPS_ORIG_MAKEJOBS}
 --with-version-pre=
 --with-version-build=${_jdk_update}
 --with-version-opt=void-r${revision}
 --with-vendor-name=Void
 --with-vendor-url=https://voidlinux.org/
 --with-vendor-bug-url=https://github.com/void-linux/void-packages/issues
 --with-vendor-vm-bug-url=https://github.com/void-linux/void-packages/issues"
make_build_args="images $(vopt_if docs docs)"
make_install_args="INSTALL_PREFIX=\"${DESTDIR}/usr/lib\""
make_check_target="test-hotspot-gtest"
hostmakedepends="pkg-config automake autoconf cpio tar unzip zip ca-certificates
 openssl zlib-devel which make-ca"
makedepends="libXrender-devel libXtst-devel libXt-devel libXrandr-devel
 giflib-devel libjpeg-turbo-devel cups-devel freetype-devel alsa-lib-devel
 fontconfig-devel zlib-devel lcms2-devel"
depends="${pkgname}-jre-${version}_${revision}"
short_desc="OpenJDK Java Development Kit (version ${_java_ver})"
maintainer="classabbyamp <void@placeviolette.net>"
license="GPL-2.0-only WITH Classpath-exception-2.0"
homepage="http://openjdk.java.net/"
distfiles="https://github.com/openjdk/jdk${_java_ver}u/archive/jdk-${version}.tar.gz
 https://github.com/google/googletest/archive/refs/tags/release-${_gtest_ver}.tar.gz"
checksum="f268e91052adb773ed5ecac70b232e70e830c89dd2d570c8c872bfaf2772867e
 9bf1fe5182a604b4135edc1a425ae356c9ad15e9b23f9f12a02e80184c3a249c"
provides="java-environment-${version}_1"
patch_args="-Np1 --directory=$build_wrksrc"

# Build and check are still parallel, but don't use -jN.
disable_parallel_build=yes
disable_parallel_check=yes
build_options="docs"

case "$XBPS_TARGET_MACHINE" in
	ppc64*) ;;
	# no hotspot JIT for ppc32
	ppc*) _use_zero=yes ;;
	armv[56]*) broken="Unsupported architecture" ;;
esac

if [ -n "$_use_zero" ]; then
	makedepends+=" libffi-devel"
	configure_args+=" --with-jvm-variants=zero"
	case "$XBPS_TARGET_MACHINE" in
	ppc*) configure_args+=" --with-boot-jdk-jvmargs=-XX:ThreadStackSize=2560";;
	esac
fi
configure_args+=" --with-boot-jdk-jvmargs=-Xlog:disable"

case "$XBPS_TARGET_LIBC" in
	glibc) build_options_default+=" docs";;
esac

if [ ! "$CROSS_BUILD" ]; then
	hostmakedepends+=" openjdk16-bootstrap"
	configure_args+=" --with-boot-jdk=/usr/lib/jvm/openjdk16"
else
	hostmakedepends+=" openjdk17"
	configure_args+=" --with-boot-jdk=/usr/lib/jvm/openjdk17"
fi

alternatives="
 jdk:/usr/bin/jar:/${_jdk_home}/bin/jar
 jdk:/usr/bin/jarsigner:/${_jdk_home}/bin/jarsigner
 jdk:/usr/bin/java:/${_jdk_home}/bin/java
 jdk:/usr/bin/javac:/${_jdk_home}/bin/javac
 jdk:/usr/bin/javadoc:/${_jdk_home}/bin/javadoc
 jdk:/usr/bin/javap:/${_jdk_home}/bin/javap
 jdk:/usr/bin/jcmd:/${_jdk_home}/bin/jcmd
 jdk:/usr/bin/jconsole:/${_jdk_home}/bin/jconsole
 jdk:/usr/bin/jdb:/${_jdk_home}/bin/jdb
 jdk:/usr/bin/jdeprscan:/${_jdk_home}/bin/jdeprscan
 jdk:/usr/bin/jdeps:/${_jdk_home}/bin/jdeps
 jdk:/usr/bin/jfr:/${_jdk_home}/bin/jfr
 jdk:/usr/bin/jhsdb:/${_jdk_home}/bin/jhsdb
 jdk:/usr/bin/jimage:/${_jdk_home}/bin/jimage
 jdk:/usr/bin/jinfo:/${_jdk_home}/bin/jinfo
 jdk:/usr/bin/jlink:/${_jdk_home}/bin/jlink
 jdk:/usr/bin/jmap:/${_jdk_home}/bin/jmap
 jdk:/usr/bin/jmod:/${_jdk_home}/bin/jmod
 jdk:/usr/bin/jpackage:/${_jdk_home}/bin/jpackage
 jdk:/usr/bin/jps:/${_jdk_home}/bin/jps
 jdk:/usr/bin/jrunscript:/${_jdk_home}/bin/jrunscript
 jdk:/usr/bin/jshell:/${_jdk_home}/bin/jshell
 jdk:/usr/bin/jstack:/${_jdk_home}/bin/jstack
 jdk:/usr/bin/jstat:/${_jdk_home}/bin/jstat
 jdk:/usr/bin/jstatd:/${_jdk_home}/bin/jstatd
 jdk:/usr/bin/keytool:/${_jdk_home}/bin/keytool
 jdk:/usr/bin/rmiregistry:/${_jdk_home}/bin/rmiregistry
 jdk:/usr/bin/serialver:/${_jdk_home}/bin/serialver
"

post_extract() {
	chmod +x $build_wrksrc/configure
	if [ "$XBPS_TARGET_LIBC" = "musl" ]; then
		rm -r $build_wrksrc/src/jdk.hotspot.agent
	fi
}

post_patch() {
	if [ "$XBPS_TARGET_LIBC" = "musl" ]; then
		for f in "${FILESDIR}"/musl_patches/*.patch; do
			msg_normal "Applying ${f#$FILESDIR/}\n"
			patch -lNp1 -i "$f"
		done
	fi
}

do_configure() {
	CFLAGS=${CFLAGS/-D_FORTIFY_SOURCE=2/}
	CXXFLAGS=${CXXFLAGS/-D_FORTIFY_SOURCE=2/}

	# force ELFv2 for ppc64 just in case
	case "$XBPS_TARGET_MACHINE" in
		ppc64*)
			CFLAGS+=" -DABI_ELFv2"
			CXXFLAGS+=" -DABI_ELFv2"
			;;
	esac

	configure_args=${configure_args/--with-libtool-sysroot=$XBPS_CROSS_BASE}
	if [ "$XBPS_CCACHE" ] && [ -z "$CROSS_BUILD" ]; then
		configure_args+=" --enable-ccache"
		CC="/usr/bin/cc"
		CXX="/usr/bin/c++"
	fi
	./configure ${configure_args} \
		--with-extra-cflags="$CFLAGS" \
		--with-extra-cxxflags="$CXXFLAGS" \
		--with-extra-ldflags="$LDFLAGS" \
		READELF=$READELF AR=$AR STRIP=$STRIP NM=$NM \
		OBJDUMP=$OBJDUMP OBJCOPY=$OBJCOPY
}

post_install() {
	rm -rf ${DESTDIR}/usr/lib/bin
	mv ${DESTDIR}/usr/lib/jvm/openjdk-${_base_version} ${DESTDIR}/$_jdk_home
	vmkdir $_jdk_home/lib/security
	make-ca -g -f --destdir "${PWD}/ca" -k "${DESTDIR}/$_jdk_home/bin/keytool"
	mv ./ca/etc/pki/tls/java/cacerts ${DESTDIR}/$_jdk_home/lib/security/
	chmod -R ugo+rw ./ca
	rm -rf ./ca
}

subpackages="openjdk17-src openjdk17-jre openjdk17-doc openjdk17-jmods"

openjdk17-jre_package() {
	shlib_provides="libawt.so libawt_xawt.so libjava.so libjli.so libjvm.so libjawt.so"
	short_desc+=" - runtime components"
	provides="java-runtime-${version}_1"
	depends="xbps-triggers>=0.122_1"
	triggers="openjdk-profile"
	alternatives="
	 java:/usr/bin/java:/${_jdk_home}/bin/java
	 java:/usr/bin/jfr:/${_jdk_home}/bin/jfr
	 java:/usr/bin/keytool:/${_jdk_home}/bin/keytool
	 java:/usr/bin/rmiregistry:/${_jdk_home}/bin/rmiregistry
	"
	pkg_install() {
		for bf in java jfr keytool rmiregistry; do
			vmove $_jdk_home/bin/$bf
		done

		for f in lib legal conf release; do
			vmove $_jdk_home/$f
		done

		vlicense ASSEMBLY_EXCEPTION
		vlicense LICENSE
	}
}

openjdk17-doc_package() {
	nostrip=yes
	noverifyrdeps=yes
	noshlibprovides=yes
	short_desc+=" - documentation"
	pkg_install() {
		if [ "$build_option_docs" ]; then
			local _docdir=usr/share/doc/openjdk$_java_ver
			vmkdir $_docdir
			cp -a $wrksrc/$build_wrksrc/build/linux-*/images/docs/* ${PKGDESTDIR}/$_docdir
		fi
		vmove $_jdk_home/man/man1
	}
}

openjdk17-src_package() {
	nostrip=yes
	noverifyrdeps=yes
	noshlibprovides=yes
	short_desc+=" - source code"
	pkg_install() {
		vmove "$_jdk_home/lib/src.zip"
	}
}

openjdk17-jmods_package() {
	short_desc+=" - JMODs"
	depends="${pkgname}-${version}_${revision}"
	pkg_install() {
		vmove $_jdk_home/jmods
	}
}
