#!/usr/bin/make -f

include /usr/share/dpkg/architecture.mk
include /usr/share/dpkg/pkg-info.mk

export DEB_HOST_MULTIARCH

# Security Hardening
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export DEB_CFLAGS_MAINT_APPEND = -fPIC
export DEB_CXXFLAGS_MAINT_APPEND = -fPIC
export DEB_LDFLAGS_MAINT_APPEND = -fPIC

DEB_CPPFLAGS_MAINT_APPEND += \
  -DNDEBUG \
  -I/usr/include/android \
  -UDEBUG
# Clang defines a macro `mips = 1` which disrupts the namespace declaration `art::mips`
DEB_CPPFLAGS_MAINT_APPEND += -Umips
export DEB_CPPFLAGS_MAINT_APPEND

COMPONENTS_ANY = debian/out/dmtracedump
COMPONENTS_AOSP = debian/out/dexdump debian/out/dexlist

# MIPS is left out because ART only supports MIPSr6 while Debian is by default MIPSr2
ifeq ($(filter amd64 i386 armel armhf arm64,$(DEB_HOST_ARCH)),)
  COMPONENTS += $(COMPONENTS_ANY)
else
  COMPONENTS += $(COMPONENTS_ANY) $(COMPONENTS_AOSP)
endif

debian/out/%: debian/%.mk
	dh_auto_build --buildsystem=makefile -- --file=$<

debian/out/lib%.so: debian/lib%.mk
	dh_auto_build --buildsystem=makefile -- --file=$<

debian/out/dexdump: debian/dexdump.mk debian/out/libart.so
	dh_auto_build --buildsystem=makefile -- --file=$<

debian/out/libart.so: debian/libart.mk debian/out/libsigchain.so
	dh_auto_build --buildsystem=makefile -- --file=$<

%:
	dh $@

override_dh_auto_build: $(COMPONENTS)

override_dh_auto_clean:
	dh_auto_clean
	make clean --file=debian/libart.mk

override_dh_auto_install:
	for bin in dexdump dmtracedump; do \
		help2man -N -n "Dex Tool" --no-discard-stderr \
			--version-string="$(DEB_VERSION)" \
			-o debian/out/$$bin.1 debian/out/$$bin; \
	done
	dh_auto_install
