#!/usr/bin/make -f

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

## Security Hardening
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
# https://android.googlesource.com/platform/build/soong/+/refs/tags/platform-tools-35.0.2/cc/config/global.go#393
export DEB_CFLAGS_MAINT_APPEND = -fPIC -std=gnu2x
export DEB_CXXFLAGS_MAINT_APPEND = -fPIC -std=gnu++2b
export DEB_LDFLAGS_MAINT_APPEND = -fPIC
export DEB_CPPFLAGS_MAINT_APPEND = -DNDEBUG -UDEBUG \
  -fmessage-length=0 \
  -fno-exceptions \
  -fno-strict-aliasing \
  -no-canonical-prefixes \

## For get-orig-source
export DEB_SOURCE

## system
export DEB_HOST_MULTIARCH
export DEB_VERSION
# https://android.googlesource.com/platform/development/+/refs/tags/platform-tools-35.0.2/sdk/plat_tools_source.prop_template
export PLATFORM_TOOLS_VERSION = 35.0.2

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

%:
	dh $@ --with bash-completion

# Depends: git-buildpackage repo ssh
get-orig-source:
	debian/scripts/get-orig-source

ifeq ("$(wildcard ../$(DEB_SOURCE)_$(PLATFORM_TOOLS_VERSION).orig.tar.xz)","")
update: get-orig-source
else
update:
endif
	gbp import-orig ../$(DEB_SOURCE)_$(PLATFORM_TOOLS_VERSION).orig.tar.xz \
		--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\""

## system

s/lib%.a: debian/system/lib%.mk
	dh_auto_build --buildsystem=makefile -- --file=$<

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

debian/manpages/system/%.1: debian/manpages/system/%.1.md
ifneq (, $(shell which pandoc))
	pandoc --standalone --from=markdown-smart --to=man --output=$@ $<
else
	ronn -r $<
endif

s/libbacktrace.so: debian/system/libbacktrace.mk s/libcutils.so e/libcpu_features.so
	dh_auto_build --buildsystem=makefile -- --file=$<

s/libbase.so: debian/system/libbase.mk s/liblog.so
	dh_auto_build --buildsystem=makefile -- --file=$<

s/liblog.so: debian/system/liblog.mk
	dh_auto_build --buildsystem=makefile -- --file=$<

s/adb: debian/system/adb.mk s/libziparchive.so s/libadb.a s/libcrypto_utils.a s/libcutils.so debian/manpages/system/adb.1
	dh_auto_build --buildsystem=makefile -- --file=$<

s/adbd: debian/system/adbd.mk s/libadb.a s/libcrypto_utils.a s/libcutils.so
	dh_auto_build --buildsystem=makefile -- --file=$<

s/fastboot: debian/system/fastboot.mk s/adb s/extras/libext4_utils.a s/libsparse.so debian/manpages/system/fastboot.1
	dh_auto_build --buildsystem=makefile -- --file=$<

s/simg2img: debian/system/simg2img.mk s/libbase.so s/libsparse.so
	dh_auto_build --buildsystem=makefile -- --file=$<

s/simg2simg: debian/system/simg2simg.mk s/simg2img
	dh_auto_build --buildsystem=makefile -- --file=$<

s/img2simg: debian/system/img2simg.mk s/simg2img
	dh_auto_build --buildsystem=makefile -- --file=$<

s/append2simg: debian/system/append2simg.mk s/simg2img
	dh_auto_build --buildsystem=makefile -- --file=$<

COMPONENTS = \
  e/libcpu_features.so \
  s/append2simg \
  s/img2simg \
  s/libbacktrace.so \
  s/libbase.so \
  s/libcutils.so \
  s/liblog.so \
  s/libsparse.so \
  s/libutils.so \
  s/libziparchive.so \
  s/simg2img \
  s/simg2simg \

# Whatever depends on BoringSSL must be disabled on MIPS
NON_MIPS_COMPONENTS = s/adb s/adbd s/fastboot
ifneq ($(filter amd64 i386 armel armhf arm64 loong64 mipsel mips64el ppc64el riscv64,$(DEB_HOST_ARCH)),)
  COMPONENTS += $(NON_MIPS_COMPONENTS)
endif

# Most components only support ARM, x86 and MIPS, but some can be built
# on all architectures.
COMPONENTS_ANY_ARCH = \
  s/append2simg \
  s/img2simg \
  s/libbase.so \
  s/libcutils.so \
  s/liblog.so \
  s/libsparse.so \
  s/libziparchive.so \
  s/simg2img \
  s/simg2simg \

ifeq ($(filter amd64 i386 armel armhf arm64 loong64 mipsel mips64el ppc64el riscv64,$(DEB_HOST_ARCH)),)
  COMPONENTS := $(filter $(COMPONENTS_ANY_ARCH), $(COMPONENTS))
endif

## development

debian/manpages/development/%.1: debian/manpages/development/%.1.md
ifneq (, $(shell which pandoc))
	pandoc --standalone --from=markdown-smart --to=man --output=$@ $<
else
	ronn -r $<
endif

development/etc1tool: debian/development/etc1tool.mk debian/manpages/development/etc1tool.1 f/native/libETC1.a
	dh_auto_build --buildsystem=makefile -- --file=$<

COMPONENTS += development/etc1tool

## frameworks/native

f/native/libETC1.a: debian/frameworks/native/libETC1.mk
	dh_auto_build --buildsystem=makefile -- --file=$<

COMPONENTS += f/native/libETC1.a

## system/extras

s/extras/libext4_utils.a: debian/system/extras/libext4_utils.mk
	dh_auto_build --buildsystem=makefile -- --file=$<

## libnativehelper

libnativehelper/libnativehelper.so: debian/libnativehelper/libnativehelper.mk s/liblog.so
	dh_auto_build --buildsystem=makefile -- --file=$<

COMPONENTS += libnativehelper/libnativehelper.so

## dalvik

dalvik/hprof-conv: debian/dalvik/hprof-conv.mk
	dh_auto_build --buildsystem=makefile -- --file=$<

COMPONENTS += dalvik/hprof-conv

## external

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

override_dh_auto_build-arch: $(COMPONENTS)

ifeq (,$(findstring nocheck, $(DEB_BUILD_OPTIONS)))
override_dh_auto_test-arch:
	debian/out/development/etc1tool development/tools/templates/ic_launcher_ldpi.png --encode && debian/out/development/etc1tool development/tools/templates/ic_launcher_ldpi.pkm --decode
ifneq ($(filter amd64 i386 armel armhf arm64 loong64 mipsel mips64el ppc64el riscv64,$(DEB_HOST_ARCH)),)
	LD_LIBRARY_PATH=debian/out/system debian/out/system/adb version
	LD_LIBRARY_PATH=debian/out/system debian/out/system/adb help
	LD_LIBRARY_PATH=debian/out/system debian/out/system/adb keygen key.txt && ls -l key.txt* && rm key.txt*
	LD_LIBRARY_PATH=debian/out/system debian/out/system/adbd --version
	LD_LIBRARY_PATH=debian/out/system debian/out/system/fastboot --version
	LD_LIBRARY_PATH=debian/out/system debian/out/system/fastboot devices
endif
endif

override_dh_auto_install:
	echo ignore upstream build system install procedure

override_dh_auto_configure-arch:
	rm -rf development/[a-s]* development/v* development/testrunner \
		external/lzma/{Asm,CPP,CS,DOC,Java} external/lzma/C/*.c external/lzma/C/Util
	mkdir -p debian/out/dalvik/tools debian/out/development debian/out/external \
		debian/out/frameworks/native debian/out/system/extras debian/out/libnativehelper
ifneq ($(filter amd64 i386 armel armhf arm64 loong64 mipsel mips64el ppc64el riscv64,$(DEB_HOST_ARCH)),)
	for proto in packages/modules/adb/fastdeploy/proto packages/modules/adb/proto; do \
		(cd $$proto && \
			find . -name '*.proto' -printf 'Regenerate %p\n' \
			-exec protoc --cpp_out=. {} \;) \
	done
endif

override_dh_gencontrol:
	dh_gencontrol
	dh_gencontrol -padb -- -v1:$(DEB_VERSION)
	dh_gencontrol -pandroid-libbacktrace -- -v1:$(DEB_VERSION)
	dh_gencontrol -pandroid-libbacktrace-dev -- -v1:$(DEB_VERSION)
	dh_gencontrol -pandroid-libbase -- -v1:$(DEB_VERSION)
	dh_gencontrol -pandroid-libbase-dev -- -v1:$(DEB_VERSION)
	dh_gencontrol -pandroid-libcutils -- -v1:$(DEB_VERSION)
	dh_gencontrol -pandroid-libcutils-dev -- -v1:$(DEB_VERSION)
	dh_gencontrol -pandroid-liblog -- -v1:$(DEB_VERSION)
	dh_gencontrol -pandroid-liblog-dev -- -v1:$(DEB_VERSION)
	dh_gencontrol -pandroid-libsparse -- -v1:$(DEB_VERSION)
	dh_gencontrol -pandroid-libsparse-dev -- -v1:$(DEB_VERSION)
	dh_gencontrol -pandroid-libutils -- -v1:$(DEB_VERSION)
	dh_gencontrol -pandroid-libutils-dev -- -v1:$(DEB_VERSION)
	dh_gencontrol -pandroid-libziparchive -- -v1:$(DEB_VERSION)
	dh_gencontrol -pandroid-libziparchive-dev -- -v1:$(DEB_VERSION)
	dh_gencontrol -pandroid-platform-frameworks-native-headers -- -v1:$(DEB_VERSION)
	dh_gencontrol -pandroid-platform-system-core-headers -- -v1:$(DEB_VERSION)
	dh_gencontrol -pandroid-sdk-libsparse-utils -- -v1:$(DEB_VERSION)
	dh_gencontrol -pfastboot -- -v1:$(DEB_VERSION)
	dh_gencontrol -pmkbootimg -- -v1:$(DEB_VERSION)

override_dh_makeshlibs:
	dh_makeshlibs -pandroid-libbacktrace -V "android-libbacktrace (= 1:$(DEB_VERSION))"
	dh_makeshlibs -pandroid-libbase -V "android-libbase (= 1:$(DEB_VERSION))"
	dh_makeshlibs -pandroid-libcutils -V "android-libcutils (= 1:$(DEB_VERSION))"
	dh_makeshlibs -pandroid-liblog -V "android-liblog (= 1:$(DEB_VERSION))"
	dh_makeshlibs -pandroid-libsparse -V "android-libsparse (= 1:$(DEB_VERSION))"
	dh_makeshlibs -pandroid-libutils -V "android-libutils (= 1:$(DEB_VERSION))"
	dh_makeshlibs -pandroid-libziparchive -V "android-libziparchive (= 1:$(DEB_VERSION))"
	dh_makeshlibs -pandroid-platform-frameworks-native-headers -V "android-platform-frameworks-native-headers (= 1:$(DEB_VERSION))"
	dh_makeshlibs -pandroid-platform-system-core-headers -V "android-platform-system-core-headers (= 1:$(DEB_VERSION))"
	dh_makeshlibs -pandroid-sdk-libsparse-utils -V "android-sdk-libsparse-utils (= 1:$(DEB_VERSION))"

override_dh_installsystemd:
	dh_installsystemd --no-stop-on-upgrade --no-restart-after-upgrade
