# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=java-jna
_pkgname=${pkgname#java-}
pkgver=5.11.0
pkgrel=1
pkgdesc="JNA provides Java programs easy access to native shared libraries."
url="https://github.com/java-native-access/jna"
# riscv64: blocked by java-jre-headless
arch="all !riscv64"
license="Apache-2.0 LGPL-2.1"
depends="java-jre-headless"
makedepends="apache-ant autoconf automake libffi-dev>=3.2 libtool"
subpackages="$pkgname-native"
source="$pkgname-$pkgver.tar.gz::https://github.com/java-native-access/$_pkgname/archive/$pkgver.tar.gz
	0001-jar-without-natives.patch
	nosys-default-false.patch
	no-Werror.patch
	"
builddir="$srcdir/$_pkgname-$pkgver"

prepare() {
	default_prepare

	# Disable Java AWT support (requires some X libs).
	sed -i -E "s/^(CDEFINES=.*)$/\1 -DNO_JAWT/g" native/Makefile

	# Remove bundled libffi sources, we're gonna use headers form libffi-dev.
	rm -r native/libffi

	# Remove some bundled JARs.
	rm -r dist
	find lib/native -name "*.jar" -exec rm {} +
}

build() {
	ant clean

	# Parallel build doesn't work here!
	MAKEFLAGS="-j1" \
		ant native -Drelease=true -Ddynlink.native=true \
		&& ant jar -Domit-jnilib=true
}

package() {
	depends="$depends $pkgname-native=$pkgver-r$pkgrel"

	install -m644 -D "$builddir"/build/jna.jar \
		"$pkgdir"/usr/share/java/$_pkgname-$pkgver.jar
	ln -sf $_pkgname-$pkgver.jar "$pkgdir"/usr/share/java/$_pkgname.jar
}

native() {
	local soname="libjnidispatch.so"

	cd "$builddir"

	local sover="$(sed -En 's/^JNA_JNI_VERSION=([^ ]+).*/\1/p' native/Makefile)"
	[ -n "$sover" ]

	install -m755 -D build/native-linux-*/$soname \
		"$subpkgdir"/usr/lib/$soname.$sover
	ln -sf $soname.$sover "$subpkgdir"/usr/lib/$soname
}

sha512sums="
0122b56c24125e5dea541bee71b43f127df50c8f90b2c240271c677a8d598d9640bb920bd3390856124e8b1bc89a9bd41d2b6c569ae7275f68a13c08fd07027d  java-jna-5.11.0.tar.gz
f866ec3488fc91e991358be5efbdbd2a0beaaac136ff8053965e01221164bc15169a820c26be16525d6f1a46a677d6a846c43e0707879e78cae307b1554f7b67  0001-jar-without-natives.patch
98181302672cc99d6bc06ea9e8c3c43d16c9c999bb34c08942f1df131607b68b09c1da0a3205bcef9b7f6ee947cc7c85070276cc3f67b28d2a25ef6596b0b790  nosys-default-false.patch
5d78bdbc8e5dff96af6c33a16a84c92ccfe050e8a8bd9c8e57f13c2f02f91e89c694690143b41ce02ae0fb284a788708c209c334c1e606aae076d0d138507a8d  no-Werror.patch
"
