#!/usr/bin/make -f

# Built in place with the project Makefile (javac -> dex -> jar, cargo build,
# assemble out/). The cargo build resolves the ltk path dependency from the
# sibling ../ltk checkout and fetches crates.io dependencies, so this builds
# where the workspace lives rather than in a clean chroot.

include /usr/share/rustc/architecture.mk

export CARGO_PROFILE = release

OUT = out
# A cross build uses the tree `make arm64` fills, so `dpkg-buildpackage -nc` can package it without rebuilding.
ifneq ($(DEB_HOST_RUST_TYPE),$(DEB_BUILD_RUST_TYPE))
OUT = out-$(DEB_HOST_ARCH)
MAKE_ARGS = CARGO_TARGET=$(DEB_HOST_RUST_TYPE) PKG_CONFIG=$(DEB_HOST_GNU_TYPE)-pkg-config OUT=$(OUT)
endif

PKGDIR = debian/rustdroid
LIBDIR = $(PKGDIR)/usr/lib/rustdroid

%:
	dh $@ --buildsystem=makefile

override_dh_auto_build:
	$(MAKE) $(MAKE_ARGS)

# paths.rs resolves api-impl.jar, framework-res.apk and the native libraries as
# siblings of the executable (via dladdr), so everything ships together in one
# directory; /usr/bin/rustdroid is a wrapper that adds it to the library path.
override_dh_auto_install:
	install -d $(LIBDIR)
	install -m0755 $(OUT)/rustdroid $(LIBDIR)/rustdroid
	install -m0755 $(OUT)/libtranslation_layer_main.so $(LIBDIR)/libtranslation_layer_main.so
	install -m0755 $(OUT)/libandroid.so.0 $(LIBDIR)/libandroid.so.0
	ln -sf libandroid.so.0 $(LIBDIR)/libandroid.so
	ln -sf libandroid.so.0 $(LIBDIR)/libjnigraphics.so
	install -m0644 $(OUT)/api-impl.jar $(LIBDIR)/api-impl.jar
	install -m0644 $(OUT)/api-impl-classes.jar $(LIBDIR)/api-impl-classes.jar
	install -m0644 $(OUT)/android-icu4j.jar $(LIBDIR)/android-icu4j.jar
	install -m0644 $(OUT)/android-bouncycastle.jar $(LIBDIR)/android-bouncycastle.jar
	install -m0644 $(OUT)/framework-res.apk $(LIBDIR)/framework-res.apk
	install -d $(PKGDIR)/usr/bin
	install -m0755 debian/rustdroid.wrapper $(PKGDIR)/usr/bin/rustdroid
	install -D -m0644 data/rustdroid-install.desktop $(PKGDIR)/usr/share/applications/rustdroid-install.desktop

override_dh_auto_clean:
	$(MAKE) clean

# The Makefile has no test target.
override_dh_auto_test:

# The rpath already points at ART's private /usr/lib/<triplet>/art, and the bundled
# .so are loaded by ART via java.library.path, not as ELF dependencies of the
# launcher; nothing here is a conventional shared-library package.
override_dh_shlibdeps:
	dh_shlibdeps -- --ignore-missing-info
