# Template file for 'openjdk7-bootstrap'
# Adapted from openjdk7 from Alpine
pkgname=openjdk7-bootstrap
# 2.6.19 fails to build: https://github.com/alpinelinux/aports/pull/9813
_icedtea_ver=2.6.18
_bootstrap_jdk_home="/usr/lib/jvm/java-1.5-gcj"
_final_jdk_home="/usr/lib/jvm/java-1.7-openjdk"
version=7u221b02
revision=3
wrksrc=icedtea-${_icedtea_ver}
build_style=gnu-configure
configure_args="
 --with-parallel-jobs=${XBPS_MAKEJOBS}
 --disable-dependency-tracking
 --disable-downloading
 --disable-arm32-jit
 --with-jdk-home=${_bootstrap_jdk_home}
 --prefix=${_final_jdk_home}"
make_build_target=icedtea-boot
make_build_args="SHELL=/bin/bash"
hostmakedepends="cpio tar unzip zip ca-certificates autoconf automake pkg-config
 libxslt wget gcc6-gcj-jdk-compat which"
makedepends="nss-devel cups-devel libjpeg-turbo-devel giflib-devel libpng-devel
 libXt-devel libXtst-devel libXp-devel libXrender-devel libXinerama-devel
 libXcomposite-devel libmpc-devel libgcj-devel lcms2-devel zlib-devel
 alsa-lib-devel freetype-devel fontconfig-devel gtk+-devel mit-krb5-devel
 pcsclite-devel lksctp-tools-devel"
short_desc="OpenJDK 7 via IcedTea (bootstrap)"
maintainer="q66 <daniel@octaforge.org>"
license="GPL-2.0-only WITH Classpath-exception-2.0"
homepage="https://icedtea.classpath.org/"
lib32disabled=yes

if [ -n "$XBPS_DEBUG_PKGS" ]; then
	configure_args+=" --enable-native-debuginfo"
else
	configure_args+=" --disable-native-debuginfo"
fi

# using ccache causes ICEs with pregenerated headers
export CCACHE_DISABLE=1

_rhino_ver=1.7.7.2
_ant_ver=1.9.11

_drops_ver=$_icedtea_ver
_drops_url=https://icedtea.classpath.org/download/drops/icedtea7/${_drops_ver}

distfiles="
 https://icedtea.classpath.org/download/source/icedtea-${_icedtea_ver}.tar.xz
 ${_drops_url}/openjdk.tar.bz2>openjdk-${_drops_ver}.tar.bz2
 ${_drops_url}/corba.tar.bz2>corba-${_drops_ver}.tar.bz2
 ${_drops_url}/jaxp.tar.bz2>jaxp-${_drops_ver}.tar.bz2
 ${_drops_url}/jaxws.tar.bz2>jaxws-${_drops_ver}.tar.bz2
 ${_drops_url}/jdk.tar.bz2>jdk-${_drops_ver}.tar.bz2
 ${_drops_url}/langtools.tar.bz2>langtools-${_drops_ver}.tar.bz2
 ${_drops_url}/hotspot.tar.bz2>hotspot-${_drops_ver}.tar.bz2
 https://archive.apache.org/dist/ant/binaries/apache-ant-${_ant_ver}-bin.tar.gz
 https://github.com/mozilla/rhino/releases/download/Rhino${_rhino_ver//./_}_Release/rhino-${_rhino_ver}.zip"

checksum="
 af4031b21400d9194bce9aae4dceb012590f88fe8c51efcaa67de33eb7e6152f
 da5fc1846ec80409ad5af870dbeb03d077b4add3ba54c0dde00414bea0376392
 c115dfa1027c58b1b6444908e26b613c428661fb210e891c30e6a65e77208776
 632aa746ebfd8238c64226bf949d20cc2f1d1423eaabfd19897199b13b434e3c
 94fbcc00f3d89b9f3b940af5e7b469d1b945ca99e84b51a7abdadcde33a7be14
 57b5741ab0807fbc71f15efce11546d943baffeb5f5cf55ec16fed69634abc36
 915f492188b7b214e280532df061adc6c999f0d190ea5f1325bc70b77a2af4f0
 8ce20d0c5a16373fe7bea5437d91a513b861788bd24d79ac04e43e73cb82eb54
 54569cd31a01975ea6e6eb911e8ebd41016425c7ede2e3980d3feb42cc548bb1
 b223595717dd360061b35aaa7e952271fcbbd6a763a78927283480227aaa0247"

skip_extraction="
 openjdk-${_drops_ver}.tar.bz2
 corba-${_drops_ver}.tar.bz2
 jaxp-${_drops_ver}.tar.bz2
 jaxws-${_drops_ver}.tar.bz2
 jdk-${_drops_ver}.tar.bz2
 langtools-${_drops_ver}.tar.bz2
 hotspot-${_drops_ver}.tar.bz2"

shlib_provides="libawt.so libawt_xawt.so libjava.so libjli.so libjvm.so libjawt.so"
nocross=yes

CFLAGS="-fcommon"

post_extract() {
	# ensure they're cleaned up
	mv ../apache-ant-${_ant_ver} ${wrksrc}/apache-ant
	mv ../rhino${_rhino_ver} ${wrksrc}/rhino

	# distrib patches are relative to wrksrc
	for patch in $(cat ${FILESDIR}/dist_patches_${XBPS_TARGET_LIBC}.txt); do
		case "$patch" in
			icedtea-*.patch) cp ${FILESDIR}/$patch ${wrksrc}
		esac
	done
}

# we need to set up these vars for both configure and build
_setup_build_env() {
	[ -n "$DISTRIBUTION_PATCHES" ] && return 0

	export ALT_PARALLEL_COMPILE_JOBS=$XBPS_MAKEJOBS
	export HOTSPOT_BUILD_JOBS=$XBPS_MAKEJOBS

	export JAVA_HOME=${_bootstrap_jdk_home}
	export PATH=$JAVA_HOME/bin:${wrksrc}/apache-ant/bin:$PATH

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

	# from alpine: disable jvm-breaking optimizations and c++ std
	export EXTRA_CPP_FLAGS="$CXXFLAGS -std=gnu++98 -fno-delete-null-pointer-checks -fno-lifetime-dse -fno-strict-overflow"
	export EXTRA_CFLAGS="$CFLAGS -std=gnu++98 -Wno-error -fno-delete-null-pointer-checks -fno-lifetime-dse -fno-strict-overflow"

	local _patches=
	for patch in $(cat ${FILESDIR}/dist_patches_${XBPS_TARGET_LIBC}.txt); do
		case "$patch" in
			icedtea-*.patch) _patches+=" $patch";;
		esac
	done

	export DISTRIBUTION_PATCHES="$_patches"
}

pre_configure() {
	./autogen.sh
}

do_configure() {
	_setup_build_env

	local _args=
	local _src_prefix="${XBPS_SRCDISTDIR}/${pkgname}-${version}"

	_args+=" --with-rhino=${wrksrc}/rhino/lib/rhino-${_rhino_ver}.jar"
	_args+=" --with-openjdk-src-zip=${_src_prefix}/openjdk-${_drops_ver}.tar.bz2"
	_args+=" --with-hotspot-src-zip=${_src_prefix}/hotspot-${_drops_ver}.tar.bz2"
	_args+=" --with-corba-src-zip=${_src_prefix}/corba-${_drops_ver}.tar.bz2"
	_args+=" --with-jaxp-src-zip=${_src_prefix}/jaxp-${_drops_ver}.tar.bz2"
	_args+=" --with-jaxws-src-zip=${_src_prefix}/jaxws-${_drops_ver}.tar.bz2"
	_args+=" --with-jdk-src-zip=${_src_prefix}/jdk-${_drops_ver}.tar.bz2"
	_args+=" --with-langtools-src-zip=${_src_prefix}/langtools-${_drops_ver}.tar.bz2"

	./configure ${configure_args} ${_args}
}

pre_build() {
	_setup_build_env
}

do_build() {
	make -j1 ${make_build_args} ${make_build_target}
}

do_install() {
	mkdir -p ${DESTDIR}/${_final_jdk_home}
	cp -a ${wrksrc}/openjdk.build-boot/j2sdk-image/* ${DESTDIR}/${_final_jdk_home}
	cp -a ${wrksrc}/openjdk.build-boot/include ${DESTDIR}/${_final_jdk_home}
	rm -f ${DESTDIR}/${_final_jdk_home}/src.zip
}

post_install() {
	rm -rf ${DESTDIR}/usr/lib/bin
	vlicense ${DESTDIR}/${_final_jdk_home}/ASSEMBLY_EXCEPTION
	vlicense ${DESTDIR}/${_final_jdk_home}/LICENSE
}
