#!/usr/bin/make -f

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

## 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 -std=gnu17
export DEB_CXXFLAGS_MAINT_APPEND = -fPIC -std=gnu++2a
export DEB_LDFLAGS_MAINT_APPEND = -fPIC
export DEB_CPPFLAGS_MAINT_APPEND = -DNDEBUG -UDEBUG \
  -I/usr/include/android \
  -fmessage-length=0 \
  -fno-exceptions \
  -fno-strict-aliasing \
  -no-canonical-prefixes \

export DEB_HOST_MULTIARCH

ifneq (, $(shell which clang))
  export CC = clang
  export CXX = clang++
  export DEB_CFLAGS_MAINT_APPEND += -gdwarf-4
  export DEB_CXXFLAGS_MAINT_APPEND += -gdwarf-4
  export DEB_CPPFLAGS_MAINT_APPEND += \
    -Wno-c99-designator \
    -Wno-gnu-designator \
    -Wno-gnu-folding-constant \

endif
ifneq (, $(shell which lld))
  export DEB_LDFLAGS_MAINT_APPEND += -fuse-ld=lld -Wl,--build-id=sha1
endif

ifeq ($(IMPORT_VER),)
	GBP_IMPORT = --uscan
else ifneq ("$(wildcard ../$(DEB_SOURCE)_$(IMPORT_VER).tar.gz)","")
	GBP_IMPORT = ../$(DEB_SOURCE)_$(IMPORT_VER).orig.tar.xz
else
	GBP_IMPORT =
endif

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 riscv64,$(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

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

debian/out/dmtracedump: debian/dmtracedump.mk
	mkdir -p debian/out
	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

ifeq (,$(findstring nocheck, $(DEB_BUILD_OPTIONS)))
override_dh_auto_test-arch:
	debian/out/dmtracedump 2>&1 | grep 'Turn on HTML output'
ifneq ($(filter amd64 i386 armhf arm64 riscv64,$(DEB_HOST_ARCH)),)
	LD_LIBRARY_PATH=debian/out debian/out/dexdump -h 2>&1 | grep dexfile
	LD_LIBRARY_PATH=debian/out debian/out/dexlist -h 2>&1 | grep dexfile
endif
endif

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

get-orig-source:
	gbp import-orig $(GBP_IMPORT) \
		--no-interactive --debian-branch=$(shell git symbolic-ref --short HEAD) \
		--postimport="\
		dch -b -v\$$GBP_DEBIAN_VERSION \"New upstream release \$$GBP_UPSTREAM_VERSION\"; \
		git add debian/changelog; \
		git commit -m\"dch: Note new upstream version \$$GBP_UPSTREAM_VERSION\""
check-upstream:
	$(eval count = $$(shell git show upstream|wc -l))
	@test $(count) -gt 10 || ( \
		git reset --hard HEAD^^; \
		git tag -d upstream/$(shell echo $(DEB_VERSION_UPSTREAM)|sed "s/~/_/g;s/:/%/g"); \
		git checkout upstream; git reset --hard HEAD^; git checkout $(shell git symbolic-ref --short HEAD); \
		echo; \
		echo error: Line count of upstream/$(DEB_VERSION_UPSTREAM): $(count); \
		echo Upstream changes does not deserve an update.;\
		echo; \
		exit 1; \
	)
ifeq ($(GBP_IMPORT),)
update:
	@echo Cannot find orig to import: ../$(DEB_SOURCE)_$(IMPORT_VER).tar.gz
else
update: get-orig-source check-upstream
	@echo Package updated to upstream version: $(shell dpkg-parsechangelog -SVersion|sed 's/-[^-]*$$//'|sed 's/^[0-9]*://')
endif
