#!/usr/bin/make -f

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

ifneq (,$(findstring coverage,$(DEB_BUILD_OPTIONS)))
	CONFIGURE_COVERAGE_FLAGS = "Db_coverage=true"
endif

ifneq (,$(filter all_tests,$(DEB_BUILD_OPTIONS)))
	WHOLE_DP_ALL_TESTS="-Dall_tests=true"
endif

ifneq (,$(filter sanitizer,$(DEB_BUILD_OPTIONS)))
	DATAPLANE_SANITIZER="-Db_sanitize=address"
endif

ifneq (,$(filter no_lto,$(DEB_BUILD_OPTIONS)))
	DATAPLANE_LTO="-Db_lto=false"
endif

ifneq (,$(filter nofused,$(DEB_BUILD_OPTIONS)))
	DATAPLANE_FUSED="-Dfused_mode=disabled"
endif

ifneq (,$(filter nocheck,$(DEB_BUILD_PROFILES)))
	DATAPLANE_WITH_TESTS="-Dwith_tests=disabled"
endif

DATAPLANE_CONFIGURE_FLAGS= \
	$(CONFIGURE_COVERAGE_FLAGS) \
	$(WHOLE_DP_ALL_TESTS) \
	$(DATAPLANE_SANITIZER) \
	$(DATAPLANE_LTO) \
	$(DATAPLANE_FUSED) \
	$(DATAPLANE_WITH_TESTS)

# Don't override the optimisation flags as they differ for different
# dataplane make targets
export DEB_CFLAGS_MAINT_STRIP = -O2
export DEB_CFLAGS_MAINT_APPEND
export DEB_CXXFLAGS_MAINT_STRIP = -O2
export DEB_CXXFLAGS_MAINT_APPEND

# Some hardening options actually help with performance, but some harm it
export DEB_BUILD_MAINT_OPTIONS=hardening=+format,-fortify,-stackprotector,+relro,+bindnow

# To be really strict change this to level 4
# Newer protobuf versions than the one in Debian Buster (3.6.1)
# reduce less C++ symbols. Tolerate for now until this branch
# is no longer used between different Debian relases or protobuf versions.
DPKG_GENSYMBOLS_CHECK_LEVEL ?= $(shell pkg-config --exists 'protobuf > 3.6.1' && echo 0 || echo 1)
export DPKG_GENSYMBOLS_CHECK_LEVEL

VERSION=$(shell scripts/get_version)

# Build in a separate directory
%:
	dh $@ --builddirectory=build --buildsystem=meson

override_dh_auto_configure:
	dh_auto_configure -- $(DATAPLANE_CONFIGURE_FLAGS)

override_dh_strip:
	debian/bin/debug_strip --auto-dbgsym

override_dh_auto_build:
	ninja -C build -v $(NINJA_ARGS)

# Does not interact well with custom stripping above
override_dh_dwz:

# _ are replaced with - as the former are not allowed in package names.
# The generated virtual packages have format:
# vyatta-dataplane-<cfg|op>-<PROTO>-<VERSION>
override_dh_gencontrol: OP_VERSIONS = $(shell ASAN_OPTIONS=verify_asan_link_order=0:detect_leaks=0 build/src/dataplane --list_cmd_versions \
					| sed "s/_/-/g ; \
					       s/ /-/ ; \
					       s/$$/,/ ; \
					       s/^/vyatta-dataplane-op-/" \
					| tr '\n' ' ')
override_dh_gencontrol: CFG_VERSIONS = $(shell ASAN_OPTIONS=verify_asan_link_order=0:detect_leaks=0 build/src/dataplane --list_msg_versions \
					 | sed "s/_/-/g ; \
						s/ /-/ ; \
						s/$$/,/ ; \
						s/^/vyatta-dataplane-cfg-/" \
					 | tr '\n' ' ')
override_dh_gencontrol:
	dh_gencontrol -- -V'protocols:Provides=$(CFG_VERSIONS) $(OP_VERSIONS)'

override_dh_installsystemd:
	dh_installsystemd --name=vyatta-dataplane
	dh_installsystemd --name=proc-xen

# Do not generate package-build-time versioned shlibs/symbols files.
# dh_makeshlibs behavior got changed with debian compat 12.
# This breaks building DANOS images based on a slightly outdated dataplane fork/branch,
# if version dependent packages got already build against a newer dataplane version.
# This breaks a very common private-build / continuous integration workflow.
override_dh_makeshlibs:
	dh_makeshlibs -VNone -- -v$(VERSION)
