#!/usr/bin/make -f
include /usr/share/java/java_defaults.mk
export JAVA_HOME := /usr/lib/jvm/default-java

DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/default.mk

export DEB_BUILD_MAINT_OPTIONS=hardening=+all

%:
	dh $@ --buildsystem=maven --with maven-repo-helper

override_dh_auto_configure:
	sed -i 's/@LZ4_JAVA_VERSION@/$(DEB_VERSION_UPSTREAM)/' pom.xml
	dh_auto_configure

override_dh_auto_build-indep:
	dh_auto_build -- package -P '!native-linux' -Dmaven.compiler.source=$(java_compat_level) -Dmaven.compiler.target=$(java_compat_level)

override_dh_auto_build-arch:
	dh_auto_build -- compile -P '!native-linux' -Dmaven.compiler.source=$(java_compat_level) -Dmaven.compiler.target=$(java_compat_level)
	mkdir -p build
	# See debian/README.source for rationale and implications of
	# linking liblz4-java.so statically against liblz4.a here.
	gcc $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) \
	    -I$(JAVA_HOME)/include/ \
	    -I$(JAVA_HOME)/include/linux \
	    -Ibuild/jni-headers \
	    -fPIC -shared -Wall -g \
	    -o build/liblz4-java.so \
	    src/jni/net_jpountz_lz4_LZ4JNI.c \
	    src/jni/net_jpountz_xxhash_XXHashJNI.c \
	    -l:liblz4.a -lxxhash

override_dh_auto_test-indep:
	# Tests require the JNI native library, which is only built during
	# the arch pass; see override_dh_auto_test-arch.

override_dh_auto_test-arch:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
ifeq (,$(filter alpha armel ia64 mips64el mipsel powerpc riscv64 x32, $(DEB_BUILD_ARCH)))
	dh_auto_test -- test -P '!native-linux' -DargLine=-Djava.library.path=$(CURDIR)/build
endif
endif

override_dh_auto_install-arch:
	dh_install -pliblz4-jni build/*.so usr/lib/$(DEB_HOST_MULTIARCH)/jni
	# Record the lz4 version statically linked in in the JNI library.
	# Runs after dh_prep, which deletes debian/*.substvars.
	dpkg-query -f 'lz4:StaticBuiltUsing=$${source:Package} (= $${source:Version})\n' -W liblz4-dev > debian/liblz4-jni.substvars
