#!/usr/bin/make -f

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

export DEB_HOST_MULTIARCH

# Security Hardening
# disable LTO for now as the package uses clang. See #1015353
export DEB_BUILD_MAINT_OPTIONS = hardening=+all optimize=-lto
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
export DEB_CPPFLAGS_MAINT_APPEND


COMPONENTS = debian/out/dmtracedump
MAN_COMPONENTS = debian/out/dmtracedump.1

# MIPS is left out because ART only supports MIPSr6 while Debian is by default MIPSr2
ifneq ($(filter amd64 i386 armhf arm64,$(DEB_HOST_ARCH)),)
  COMPONENTS += debian/out/dexdump debian/out/dexlist
  MAN_COMPONENTS += debian/out/dexdump.1 debian/out/dexlist.1
  COMPONENTS += debian/out/libdexfile_external.so debian/out/libdexfile_support.so
endif

ifneq (, $(shell which clang))
  export CC=clang
  export CXX=clang++
endif

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

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

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

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

debian/out/libnativeloader.so: debian/libnativeloader.mk debian/out/libnativebridge.so
	dh_auto_build --buildsystem=makefile -- --file=$<

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

# Only for debug purpose to build standalone object
debian/out/asm_defines.h debian/out/operator_out.cc: debian/libart.mk
	dh_auto_build --buildsystem=makefile -- --file=$< $@
debian/out/mterp.S: debian/libart.mk debian/out/asm_defines.h
	dh_auto_build --buildsystem=makefile -- --file=$< $@
runtime/%.o libartbase/%.o libdexfile/%.o debian/out/%.o: debian/libart.mk
	dh_auto_build --buildsystem=makefile -- --file=$< $@

debian/out/%.1: debian/out/%
	help2man -N -n "Dex Tool" --no-discard-stderr --version-string="$(DEB_VERSION)" \
		-o $@ $<

%:
	dh $@

override_dh_auto_build: $(COMPONENTS) $(MAN_COMPONENTS)

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

override_dh_dwz:
	echo Skip dh_dwz since it cannot reduce size for this package.

override_dh_gencontrol:
	dh_gencontrol
	dh_gencontrol -pandroid-libnativebridge -- -v1:$(DEB_VERSION)
	dh_gencontrol -pandroid-libnativeloader -- -v1:$(DEB_VERSION)
	dh_gencontrol -pdmtracedump -- -v1:$(DEB_VERSION)

override_dh_shlibdeps:
	dh_shlibdeps -l/usr/lib/$(DEB_HOST_MULTIARCH)/android
