#!/usr/bin/make -f
export DH_VERBOSE = 1

include /usr/share/dpkg/architecture.mk

export DEB_CXXFLAGS_MAINT_APPEND = -D_GLIBCXX_ASSERTIONS

ifeq ($(DEB_HOST_ARCH),arm64)
# GCC's -Wpsabi fires ~2700 times on arm64 builds, disable noise
DEB_CXXFLAGS_MAINT_APPEND += -Wno-psabi
endif

export DEB_BUILD_MAINT_OPTIONS = hardening=+bindnow optimize=-lto

%:
	dh $@ --buildsystem=cmake+ninja

override_dh_auto_configure:
	dh_auto_configure -- \
		-DDAWN_ENABLE_INSTALL=ON \
		-DTINT_INSTALL_CMD_TOOLS=ON \
		-DCMAKE_BUILD_TYPE=Release \
		-DDAWN_BUILD_MONOLITHIC_LIBRARY=SHARED \
		-DDAWN_ENABLE_RTTI=ON \
		-DDAWN_FETCH_DEPENDENCIES=OFF \
		-DTINT_BUILD_CMD_TOOLS=ON \
		-DDAWN_BUILD_TESTS=ON \
		-DTINT_BUILD_TESTS=OFF \
		-DDAWN_BUILD_PROTOBUF=OFF \
		-DTINT_BUILD_IR_BINARY=OFF

# Timestamps for build-time analysis
execute_before_dh_auto_configure:
	@echo "TIMING dh_auto_configure start: $$(date -u '+%a, %d %b %Y %H:%M:%S %z')"

execute_after_dh_auto_configure:
	@echo "TIMING dh_auto_configure done:  $$(date -u '+%a, %d %b %Y %H:%M:%S %z')"

execute_after_dh_auto_build:
	@echo "TIMING dh_auto_build done:      $$(date -u '+%a, %d %b %Y %H:%M:%S %z')"

# dawn_unittests is CPU-only (no GPU/Vulkan needed), safe to run on any buildd.
# Not wired up via CTest upstream (no add_test() calls), so run it directly.
override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	obj-$(DEB_HOST_MULTIARCH)/dawn_unittests --gtest_brief=1 --gtest_fail_if_no_test_linked
endif

execute_after_dh_auto_test:
	@echo "TIMING dh_auto_test done:       $$(date -u '+%a, %d %b %Y %H:%M:%S %z')"

execute_after_dh_auto_install:
	@echo "TIMING dh_auto_install done:    $$(date -u '+%a, %d %b %Y %H:%M:%S %z')"

execute_after_dh_install:
	@echo "TIMING dh_install done:         $$(date -u '+%a, %d %b %Y %H:%M:%S %z')"

execute_after_dh_dwz:
	@echo "TIMING dh_dwz done:             $$(date -u '+%a, %d %b %Y %H:%M:%S %z')"

execute_after_dh_strip:
	@echo "TIMING dh_strip done:           $$(date -u '+%a, %d %b %Y %H:%M:%S %z')"

execute_after_dh_shlibdeps:
	@echo "TIMING dh_shlibdeps done:       $$(date -u '+%a, %d %b %Y %H:%M:%S %z')"

execute_after_dh_builddeb:
	@echo "TIMING dh_builddeb done:        $$(date -u '+%a, %d %b %Y %H:%M:%S %z')"
