# Template file for 'openjdk8'
pkgname=openjdk8
version=8u332b05
revision=2
_jdk_update="${version#*u}"
_jdk_update="${_jdk_update%b*}"
_jdk_build="${version#*b}"
_jdk_home="usr/lib/jvm/openjdk8"
_commit="bdc2203a44df159d94ecd0e04a230e65cb84297e"
archs="x86_64* i686* aarch64* arm* ppc64*"
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_ORIG_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 GIT=true"
make_install_args="GIT=true"
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/"
# 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
distfiles="https://github.com/openjdk/shenandoah-jdk8u/archive/${_commit}.tar.gz"
checksum=4c39be8454a1b8b10a9ff845a4e7e639808799d3d2eddb1484df80835d5e88e4

disable_parallel_build=yes

build_options="docs"
desc_option_docs="Build documentation"

alternatives="jdk:/usr/lib/jvm/default-jdk:/${_jdk_home}"
provides="java-environment-${version}_1"

# 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

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

post_extract() {
	chmod +x configure
}

post_patch() {
	# 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;"

	# pretend we're a git checkout
	echo "${_commit}" > .gittip
	mkdir .git

	if [ "$XBPS_TARGET_LIBC" = "musl" ]; then
		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
	fi
}

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

	if [ "$XBPS_TARGET_WORDSIZE" = 32 ]; then
		CFLAGS+=" -D_LARGE_FILE_SOURCE=1 -D_FILE_OFFSET_BITS=64"
		CXXFLAGS+=" -D_LARGE_FILE_SOURCE=1 -D_FILE_OFFSET_BITS=64"
	fi

	./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
}

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}

	# don't duplicate binaries already in jre/bin
	for bin in "${DESTDIR}/${_jdk_home}/jre/bin"/*; do
		ln -srf "${bin}" "${bin/jre/}"
	done

	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="openjdk-common"
	alternatives="java:/usr/lib/jvm/default-jre:/${_jdk_home}/jre"
	pkg_install() {
		vmove "${_jdk_home}"/jre
		for bin in "${PKGDESTDIR}/${_jdk_home}/jre/bin"/*; do
			bin="$(basename "$bin")"
			vmove "${_jdk_home}/man/man1/${bin}.1"
			vmove "${_jdk_home}/man/ja_JP.UTF-8/man1/${bin}.1"
		done
	}
}

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"
	if [ -z "$build_option_docs" ]; then
		build_style=meta
	fi
	pkg_install() {
		if [ "$build_option_docs" ]; then
			vmkdir usr/share/doc/openjdk8
			cp -a build/linux-*/docs/* ${PKGDESTDIR}/usr/share/doc/openjdk8
		fi
	}
}

oracle-jre_package() {
	depends="openjdk8-jre>=${version}_${revision}"
	short_desc="Java Runtime Environment (transitional dummy package)"
	build_style=meta
}
