#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all reproducible=+all future=+lfs

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

ifneq ($(DEB_HOST_ARCH_BITS),64)
$(error Iceoryx is unsupported and demonstrably broken on all 32-bit architectures)
endif

BUILD_DOC := $(if $(filter nodoc,$(DEB_BUILD_OPTIONS)),OFF,ON)
BUILD_TEST := $(if $(filter nocheck,$(DEB_BUILD_OPTIONS)),OFF,ON)
LINK_LIBATOMIC := $(if $(filter mipsel powerpc riscv64,$(DEB_HOST_ARCH)),ON,OFF)

DOC_COMPONENTS = posh hoofs binding_c introspection
IOX_SOVERSION = 2
SHM_AVAIL = $(shell df -B1000000 --output=avail /dev/shm | tail -n1 || echo 0)

%:
	dh $@ --buildsystem=cmake --sourcedirectory=iceoryx_meta --builddirectory=obj

override_dh_auto_clean-indep:
	dh_auto_clean --buildsystem=cmake --sourcedirectory=iceoryx_meta --builddirectory=obj-indep

override_dh_auto_configure-arch:
	dh_auto_configure --buildsystem=cmake --sourcedirectory=iceoryx_meta --builddirectory=obj -- \
		-DIOX_SOVERSION=$(IOX_SOVERSION) \
		-DBINDING_C=ON \
		-DBUILD_DOC=OFF \
		-DBUILD_SHARED_LIBS=ON \
		-DBUILD_TEST=$(BUILD_TEST) \
		-DINTROSPECTION=ON \
		-DTOML_CONFIG=ON

override_dh_auto_configure-indep:
ifeq ($(BUILD_DOC),ON)
	dh_auto_configure --buildsystem=cmake --sourcedirectory=iceoryx_meta --builddirectory=obj-indep -- \
		-DIOX_SOVERSION=$(IOX_SOVERSION) \
		-DBUILD_DOC=ON
endif

override_dh_auto_build-indep:
ifeq ($(BUILD_DOC),ON)
	$(MAKE) -C obj-indep $(addprefix doxygen_iceoryx_,$(DOC_COMPONENTS))
endif

POSH_SHM_TESTS=*PoshRuntime.*:*PoshRuntime_test.*:*PoshRuntimeNode_test.*:*PoshRuntimeSingleProcess_test.*:*IceoryxRoudiMemoryManager_test.*:*PortManager_test.*:*ProcessManager_test.*
BINDING_C_SHM_TESTS=*Chunk_test.*:*iox_node_test.*:*BindingC_Runtime_test.*:*iox_service_discovery_test.*:*iox_sub_test.*:*iox_pub_test.*

override_dh_auto_test-arch:
	./obj/hoofs/test/hoofs_moduletests --gtest_filter="-*.TimingTest_*"
	./obj/posh/test/posh_moduletests --gtest_filter="-*.TimingTest_*:$(POSH_SHM_TESTS)"
	./obj/binding_c/test/binding_c_moduletests --gtest_filter="-*.TimingTest_*:$(BINDING_C_SHM_TESTS)"
	# SHM INTENSIVE TESTS
	# Only execute these if at least 250M shared memory are available
	[ $(SHM_AVAIL) -lt 250 ] || \
		./obj/posh/test/posh_moduletests --gtest_filter="$(POSH_SHM_TESTS)"
	[ $(SHM_AVAIL) -lt 250 ] || \
		./obj/binding_c/test/binding_c_moduletests --gtest_filter="$(BINDING_C_SHM_TESTS)"
	[ $(SHM_AVAIL) -lt 250 ] || \
		./obj/posh/test/posh_integrationtests --gtest_filter="-*.TimingTest_*"

override_dh_auto_test-indep:

DOC_DIR = debian/iceoryx-doc/usr/share/doc/iceoryx-doc
override_dh_auto_install-indep:
ifeq ($(BUILD_DOC),ON)
	mkdir -p $(DOC_DIR)
	set -ex; for component in $(DOC_COMPONENTS); do \
		mkdir -p $(DOC_DIR)/$$component; \
		cp -r obj-indep/doc/iceoryx_$$component/html $(DOC_DIR)/$$component; \
	done
	dh_doxygen
endif

execute_after_dh_auto_install-arch:
	rm -r debian/tmp/usr/share/doc

execute_before_dh_installman-arch: debian/iox-roudi.1 debian/iox-introspection-client.1
	true

%.1: %.1.in
	sed -e 's/@VERSION@/$(DEB_VERSION_UPSTREAM)/g' $< >$@
