# Template file for 'openjdk8'
# TODO: better places for bin and man?
# TODO: make -headless versions
# TODO: config files?
pkgname=openjdk8
version=8u322b04
revision=4
_jdk_update="${version#*u}"
_jdk_update="${_jdk_update%b*}"
_jdk_build="${version#*b}"
_jdk_home="usr/lib/jvm/java-1.8-openjdk"
# we're using aarch64 port repo to get aarch64 JIT; the repo is
# otherwise the same as the normal one, just with aarch64 port added
_repo_ver="aarch64-shenandoah-jdk${version/b/-b}"
archs="x86_64* i686* aarch64* arm* ppc64*"
create_wrksrc=yes
build_style=gnu-configure
configure_args="
 --prefix=${XBPS_DESTDIR}/${XBPS_CROSS_TRIPLET}/${pkgname}-${version}/usr/lib
 --with-update-version=${_jdk_update}
 --with-build-number=b${_jdk_build}
 --with-debug-level=release
 --with-native-debug-symbols=internal
 --with-milestone=fcs
 --enable-unlimited-crypto
 --with-zlib=system
 --with-giflib=system
 --with-freetype-lib=${XBPS_CROSS_BASE}/usr/lib
 --with-freetype-include=${XBPS_CROSS_BASE}/usr/include/freetype2
 --with-jtreg=no
 --with-jobs=${XBPS_MAKEJOBS}
 --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"
 --disable-option-checking"
make_build_args="images $(vopt_if docs docs) COMPILER_WARNINGS_FATAL=false"
hostmakedepends="pkg-config automake autoconf cpio tar unzip zip ca-certificates
 zlib-devel make-ca which"
makedepends="libXrender-devel libXtst-devel libXt-devel libXrandr-devel
 giflib-devel cups-devel freetype-devel alsa-lib-devel fontconfig-devel
 zlib-devel"
depends="openjdk8-jre-${version}_${revision}"
short_desc="OpenJDK Java Development Kit (version 8)"
maintainer="Toyam Cox <Vaelatern@voidlinux.org>"
license="GPL-2.0-only WITH Classpath-exception-2.0"
homepage="http://openjdk.java.net/"
distfiles="
 http://hg.openjdk.java.net/aarch64-port/jdk8u-shenandoah/archive/${_repo_ver}.tar.gz>jdk8u-${_repo_ver}.tar.gz
 http://hg.openjdk.java.net/aarch64-port/jdk8u-shenandoah/corba/archive/${_repo_ver}.tar.gz>corba-${_repo_ver}.tar.gz
 http://hg.openjdk.java.net/aarch64-port/jdk8u-shenandoah/hotspot/archive/${_repo_ver}.tar.gz>hotspot-${_repo_ver}.tar.gz
 http://hg.openjdk.java.net/aarch64-port/jdk8u-shenandoah/jdk/archive/${_repo_ver}.tar.gz>jdk-${_repo_ver}.tar.gz
 http://hg.openjdk.java.net/aarch64-port/jdk8u-shenandoah/jaxws/archive/${_repo_ver}.tar.gz>jaxws-${_repo_ver}.tar.gz
 http://hg.openjdk.java.net/aarch64-port/jdk8u-shenandoah/jaxp/archive/${_repo_ver}.tar.gz>jaxp-${_repo_ver}.tar.gz
 http://hg.openjdk.java.net/aarch64-port/jdk8u-shenandoah/langtools/archive/${_repo_ver}.tar.gz>langtools-${_repo_ver}.tar.gz
 http://hg.openjdk.java.net/aarch64-port/jdk8u-shenandoah/nashorn/archive/${_repo_ver}.tar.gz>nashorn-${_repo_ver}.tar.gz"

checksum="1b93adf81488be199243f684398d616c20e68bd3bef990d9eafb3d607ab3d02e
 03a7e0fc8ce0ed938c80c69890812e1dbad423df1718115e28f930908a9b5f9b
 2213194f9b3aef81340710ca63fd00fbabc42a82b6b30d6955640e66293ab497
 80646bf709598067622227c982efdbd88b3c6e8da0024e6f9a264145d942a99c
 8c8fc97a8c056cc520472d292b85dbe25a7edbfa4545a8c45275b7b378b0fb65
 2d659ba2c64b19dba192ed1be1e6033efb7445809f8e6966439619a417975a73
 16cc13cc5234ac1ed3dcebc8c8586a4e31b43cf5c9a4b496fd1f29279fa6b827
 68357e18386d833a882a9a988bf4535e5ce8dfa90beeeed7a1939aa75c0e991f"

build_options="docs"
desc_option_docs="Build documentation"

# no hotspot JIT for arm32 and ppc32
case "$XBPS_TARGET_MACHINE" in
	ppc64*) ;;
	arm*|ppc*|mips*) _use_zero=yes ;;
esac

# specifically do not enable docs because with zero it takes hours
if [ -n "$_use_zero" ]; then
	makedepends+=" libffi-devel"
	configure_args+=" --with-jvm-variants=zero"
else
	build_options_default+=" docs"
fi

provides="java-environment-${version}_1"

if [ ! "$CROSS_BUILD" ]; then
	hostmakedepends+=" openjdk7-bootstrap"
	configure_args+=" --with-boot-jdk=/usr/lib/jvm/java-1.7-openjdk"
else
	hostmakedepends+=" openjdk8"
	configure_args+=" --with-boot-jdk=/${_jdk_home}"
fi

# enabling ccache segfaults gcc
export CCACHE_DISABLE=1

alternatives="
 jdk:/usr/bin/appletviewer:/${_jdk_home}/bin/appletviewer
 jdk:/usr/bin/extcheck:/${_jdk_home}/bin/extcheck
 jdk:/usr/bin/idlj:/${_jdk_home}/bin/idlj
 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/java-rmi.cgi:/${_jdk_home}/bin/java-rmi.cgi
 jdk:/usr/bin/javac:/${_jdk_home}/bin/javac
 jdk:/usr/bin/javadoc:/${_jdk_home}/bin/javadoc
 jdk:/usr/bin/javah:/${_jdk_home}/bin/javah
 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/jdeps:/${_jdk_home}/bin/jdeps
 jdk:/usr/bin/jhat:/${_jdk_home}/bin/jhat
 jdk:/usr/bin/jinfo:/${_jdk_home}/bin/jinfo
 jdk:/usr/bin/jjs:/${_jdk_home}/bin/jjs
 jdk:/usr/bin/jmap:/${_jdk_home}/bin/jmap
 jdk:/usr/bin/jps:/${_jdk_home}/bin/jps
 jdk:/usr/bin/jrunscript:/${_jdk_home}/bin/jrunscript
 jdk:/usr/bin/jsadebugd:/${_jdk_home}/bin/jsadebugd
 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/native2ascii:/${_jdk_home}/bin/native2ascii
 jdk:/usr/bin/orbd:/${_jdk_home}/bin/orbd
 jdk:/usr/bin/pack200:/${_jdk_home}/bin/pack200
 jdk:/usr/bin/policytool:/${_jdk_home}/bin/policytool
 jdk:/usr/bin/rmic:/${_jdk_home}/bin/rmic
 jdk:/usr/bin/rmid:/${_jdk_home}/bin/rmid
 jdk:/usr/bin/rmiregistry:/${_jdk_home}/bin/rmiregistry
 jdk:/usr/bin/schemagen:/${_jdk_home}/bin/schemagen
 jdk:/usr/bin/serialver:/${_jdk_home}/bin/serialver
 jdk:/usr/bin/servertool:/${_jdk_home}/bin/servertool
 jdk:/usr/bin/tnameserv:/${_jdk_home}/bin/tnameserv
 jdk:/usr/bin/unpack200:/${_jdk_home}/bin/unpack200
 jdk:/usr/bin/wsgen:/${_jdk_home}/bin/wsgen
 jdk:/usr/bin/wsimport:/${_jdk_home}/bin/wsimport
 jdk:/usr/bin/xjc:/${_jdk_home}/bin/xjc
"

post_extract() {
	mv jdk8u*/* .
	chmod +x configure
	for subrepo in corba hotspot jdk jaxws jaxp langtools nashorn; do
		mv ${subrepo}-${_repo_ver} ${subrepo}
	done

	# gcc complains about the signed shift left
	vsed -i jdk/src/share/native/com/sun/java/util/jar/pack/constants.h \
		-e's;(-1)<<13;~0u<<13;g'

	# wrong "return false;" instead of "return null;"
	vsed -i jdk/src/share/native/com/sun/java/util/jar/pack/jni.cpp \
		-e"295s;false;null;"
}

post_patch() {
	[ "$XBPS_TARGET_LIBC" != "musl" ] && return 0

	for f in ${FILESDIR}/musl_*.patch; do
		echo "Applying $f"
		patch -sNp1 -i "$f"
	done

	# add cross prefix for thread_db.h check, fix cross from glibc to musl
	sed -i "s,/usr/include/thread_db.h,${XBPS_CROSS_BASE}/usr/include/thread_db.h," \
	 hotspot/make/linux/makefiles/defs.make hotspot/make/linux/makefiles/sa.make \
	 hotspot/make/linux/makefiles/saproc.make
}

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

	case "$XBPS_TARGET_MACHINE" in
		# force ELFv2 for ppc64 just in case
		ppc64*)
			CFLAGS+=" -DABI_ELFv2"
			CXXFLAGS+=" -DABI_ELFv2"
			;;
		# with normal -O2 on mipshf there is a too-large jump which causes the
		# assembler to fail. optimising for size means the jump is made smaller
		mips*hf*)
			CFLAGS+=" -Os"
			CXXFLAGS+=" -Os"
			;;
	esac

	./configure ${configure_args} --with-extra-cflags="$CFLAGS" \
	 --with-extra-cxxflags="$CXXFLAGS" --with-extra-ldflags="$LDFLAGS"
}

pre_build() {
	# only use modified flags as specified in configure
	unset CFLAGS CXXFLAGS LDFLAGS
}

do_build() {
	# do not set disable_parallel_build in order to be able to read makejobs
	make -j1 ${make_build_args} ${make_build_target}
}

post_install() {
	# we manage this ourselves
	rm -rf ${DESTDIR}/usr/lib/bin

	# unify the install prefix
	mv ${DESTDIR}/usr/lib/jvm/openjdk-1.8.0_${_jdk_update} \
	   ${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

	vlicense ASSEMBLY_EXCEPTION
	vlicense LICENSE
	vlicense THIRD_PARTY_README
}

openjdk8-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}/jre/bin/java
	 java:/usr/bin/jjs:/${_jdk_home}/jre/bin/jjs
	 java:/usr/bin/keytool:/${_jdk_home}/jre/bin/keytool
	 java:/usr/bin/orbd:/${_jdk_home}/jre/bin/orbd
	 java:/usr/bin/pack200:/${_jdk_home}/jre/bin/pack200
	 java:/usr/bin/policytool:/${_jdk_home}/jre/bin/policytool
	 java:/usr/bin/rmid:/${_jdk_home}/jre/bin/rmid
	 java:/usr/bin/rmiregistry:/${_jdk_home}/jre/bin/rmiregistry
	 java:/usr/bin/servertool:/${_jdk_home}/jre/bin/servertool
	 java:/usr/bin/tnameserv:/${_jdk_home}/jre/bin/tnameserv
	 java:/usr/bin/unpack200:/${_jdk_home}/jre/bin/unpack200
	"
	pkg_install() {
		vmove ${_jdk_home}/jre
	}
}

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

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