#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all reproducible=+all future=+lfs
export DEB_CXXFLAGS_MAINT_APPEND = -fvisibility-inlines-hidden

include /usr/share/dpkg/architecture.mk

ENABLE_TESTING = $(if $(filter nocheck,$(DEB_BUILD_OPTIONS)),OFF,ON)

# Unfortunately, foonathan-memory needs to run a host binary to determine
# the (otherwise not accessible) sizes of the internal allocations for
# various STL containers. This is not particularly elegant, but it works.
ifneq ($(filter cross,$(DEB_BUILD_PROFILES)),)
QEMU_ARCH = $(DEB_HOST_ARCH)
ifneq ($(filter armhf armel,$(DEB_HOST_ARCH)),)
QEMU_ARCH = arm
endif
ifneq ($(filter ppc64el,$(DEB_HOST_ARCH)),)
QEMU_ARCH = ppc64le
endif
ifneq ($(filter powerpc,$(DEB_HOST_ARCH)),)
QEMU_ARCH = ppc
endif
ifneq ($(filter arm64,$(DEB_HOST_ARCH)),)
QEMU_ARCH = aarch64
endif
ifneq ($(filter amd64,$(DEB_HOST_ARCH)),)
QEMU_ARCH = x86_64
endif
ifneq ($(shell command -v qemu-$(QEMU_ARCH)-static),)
CROSSCOMPILING_EMULATOR = -DCMAKE_CROSSCOMPILING_EMULATOR=qemu-$(QEMU_ARCH)-static
endif
endif

%:
	dh $@ --buildsystem=cmake

override_dh_auto_configure:
	dh_auto_configure --buildsystem=cmake -- \
		$(CROSSCOMPILING_EMULATOR) \
		-DBUILD_SHARED_LIBS=ON \
		-DHOST_TRIPLET=$(DEB_HOST_MULTIARCH) \
		-DFOONATHAN_MEMORY_BUILD_TESTS=$(ENABLE_TESTING) \
		-DFOONATHAN_MEMORY_BUILD_EXAMPLES=OFF \
		-DFOONATHAN_MEMORY_BUILD_TOOLS=ON

execute_after_dh_auto_build-indep:
ifeq ($(filter nodoc,$(DEB_BUILD_OPTIONS)),)
	doxygen doc/Doxyfile
endif

execute_after_dh_auto_install:
	rm debian/tmp/usr/share/foonathan_memory/LICENSE \
	   debian/tmp/usr/share/foonathan_memory/README.md \
	   debian/tmp/usr/bin/nodesize_dbg

execute_after_dh_installdocs-indep:
ifeq ($(filter nodoc,$(DEB_BUILD_OPTIONS)),)
	dh_doxygen
endif
