#!/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


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 utils binding_c introspection

%:
	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=1 \
		-DBINDING_C=ON \
		-DBUILD_DOC=OFF \
		-DBUILD_SHARED_LIBS=ON \
		-DBUILD_TEST=$(BUILD_TEST) \
		-DINTROSPECTION=ON \
		-DTOML_CONFIG=ON \
		-DDEB_LINK_LIBATOMIC=$(LINK_LIBATOMIC)

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

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

override_dh_auto_test-arch:
	$(MAKE) -C obj all_tests || true

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' $< >$@
