#!/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 = 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 armel armhf arm64,$(DEB_HOST_ARCH)),)
 ifeq ($(findstring stage1,$(DEB_VERSION)),)
  COMPONENTS += debian/out/dexdump debian/out/dexlist
  MAN_COMPONENTS += debian/out/dexdump.1
 endif
  COMPONENTS += debian/out/libdexfile_external.so debian/out/libdexfile_support.so
  export CC=clang
  export CXX=clang++
endif

debian/out:
	mkdir -p $@

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

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

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

debian/out/dexlist: debian/out/libart.so
	dh_auto_build --buildsystem=makefile -- --file=debian/dexlist.mk

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

debian/out/libdexfile_external.so: debian/out/libart.so
	dh_auto_build --buildsystem=makefile -- --file=debian/libdexfile_external.mk

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

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

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

%:
	dh $@

override_dh_auto_build: debian/out $(COMPONENTS) $(MAN_COMPONENTS)

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

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