#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS=hardening=+all

%:
	dh $@ --with systemd --builddirectory=build-deb

override_dh_auto_clean:
	dh_auto_clean
	rm -rf build-deb

# Upstream wishes to use CMAKE_BUILD_TYPE=Release, and ensures that has a
# sensible basis of options (eg no -O3, including -g). Debian specific options
# come from CFLAGS as usual.
#
# Upstream encourages the use of Ninja to build the source, convince dh to use
# it until someone writes native support for dh+cmake+ninja.
override_dh_auto_configure:
	dh_auto_configure -- -GNinja \
			-DDISTRO_FLAVOUR=Debian \
			-DCMAKE_BUILD_TYPE=Release \
			-DCMAKE_INSTALL_SYSCONFDIR:PATH=/etc \
			-DCMAKE_INSTALL_SYSTEMD_SERVICEDIR:PATH=/lib/systemd/system \
			-DCMAKE_INSTALL_INITDDIR:PATH=/etc/init.d \
			-DCMAKE_INSTALL_LIBEXECDIR:PATH=/usr/lib \
			-DCMAKE_INSTALL_UDEV_RULESDIR:PATH=/lib/udev/rules.d

override_dh_auto_build:
	ninja -C build-deb -v

override_dh_auto_install:
	DESTDIR=$(CURDIR)/debian/tmp ninja -C build-deb install

# The following files are not used on Debian
# FIXME: should they be?
INST_EXCLUDE := "etc/rsyslog.d/srp_daemon.conf" \
		"usr/sbin/run_srp_daemon" \
		"usr/sbin/srp_daemon.sh"
INST_EXCLUDE := $(addprefix -X,$(INST_EXCLUDE))
override_dh_install:
	dh_install --fail-missing $(INST_EXCLUDE)

# cmake installs the correct init scripts in the correct place, just setup the
# pre-postrms
override_dh_installinit:
	dh_installinit -pibacm --onlyscripts
	dh_installinit -piwpmd --onlyscripts
	dh_installinit -psrptools --onlyscripts
	dh_installinit --remaining-packages

# Provider plugin libaries are not shared libraries and do not belong in the
# shlibs file.
# librspreload is a LD_PRELOAD library and does not belong in the shlib files
SHLIBS_EXCLUDE = "/libibverbs/" "librspreload" "/ibacm/"
SHLIBS_EXCLUDE := $(addprefix --exclude=,$(SHLIBS_EXCLUDE))
override_dh_makeshlibs:
	dh_makeshlibs $(SHLIBS_EXCLUDE)

# Create dbg packages.
# FIXME: should we just have a rdma-core-dbg package ?
override_dh_strip:
	dh_strip -plibibcm1 --dbg-package=libibcm1-dbg
	dh_strip -plibibumad3 --dbg-package=libibumad3-dbg
	dh_strip -plibibverbs1 --dbg-package=libibverbs1-dbg
	dh_strip -plibrdmacm1 --dbg-package=librdmacm1-dbg

	dh_strip --remaining-packages

# Upstream encourages the use of 'build' as the developer build output
# directory, allow that directory to be present and still allow dh to work.
.PHONY: build
build:
	dh $@ --with systemd --builddirectory=build-deb
