#!/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 optimize=-lto
# https://android.googlesource.com/platform/build/soong/+/refs/tags/platform-tools-29.0.6/cc/config/global.go#121
export DEB_CFLAGS_MAINT_APPEND = -fPIC -gdwarf-4
export DEB_CXXFLAGS_MAINT_APPEND = -fPIC -gdwarf-4
export DEB_LDFLAGS_MAINT_APPEND = -fPIC
export DEB_CPPFLAGS_MAINT_APPEND = -DNDEBUG -UDEBUG \
  -fmessage-length=0 \
  -fno-exceptions \
  -fno-strict-aliasing \
  -no-canonical-prefixes \

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

## For get-orig-source
export DEB_SOURCE
export DEB_VERSION_UPSTREAM

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

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

endif

%:
	dh $@ --with bash-completion

get-orig-source:
	debian/scripts/get-orig-source

## system/core

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

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

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

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

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

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

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

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

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

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

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

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

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

# Whatever depends on BoringSSL must be disabled on MIPS
NON_MIPS_COMPONENTS = s/core/adb s/core/fastboot
ifneq ($(filter amd64 i386 armel armhf arm64 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/core/append2simg \
  s/core/img2simg \
  s/core/libbase.so \
  s/core/libcutils.so \
  s/core/liblog.so \
  s/core/libsparse.so \
  s/core/libziparchive.so \
  s/core/simg2img \
  s/core/simg2simg \

ifeq ($(filter amd64 i386 armel armhf arm64 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
	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

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 mipsel mips64el ppc64el riscv64,$(DEB_HOST_ARCH)),)
	LD_LIBRARY_PATH=debian/out/system/core debian/out/system/core/adb version
	LD_LIBRARY_PATH=debian/out/system/core debian/out/system/core/adb help
	LD_LIBRARY_PATH=debian/out/system/core debian/out/system/core/adb keygen key.txt && ls -l key.txt* && rm key.txt*
	LD_LIBRARY_PATH=debian/out/system/core debian/out/system/core/fastboot --version
	LD_LIBRARY_PATH=debian/out/system/core debian/out/system/core/fastboot devices
endif
endif

override_dh_auto_install:
	echo ignore upstream build system install procedure

override_dh_auto_configure-arch:
	rm -rf external/boringssl external/libunwind external/selinux development/[a-s]* development/v* development/testrunner art

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)
