#!/usr/bin/make -f

include /usr/share/dpkg/architecture.mk

export DEB_BUILD_MAINT_OPTIONS=hardening=+all

ifneq ($(DEB_BUILD_ARCH),$(DEB_HOST_ARCH))
export _PYTHON_SYSCONFIGDATA_NAME=_sysconfigdata__${DEB_HOST_ARCH_OS}_${DEB_HOST_MULTIARCH}
endif

NON_COHERENT_DMA_ARCHS = alpha arc armel armhf hppa m68k sh4

dh_params = --with python3 --builddirectory=build-deb

%:
	dh $@ $(dh_params)

override_dh_auto_clean:
	dh_auto_clean
	rm -rf build-deb
	for package in ibverbs-providers libibverbs-dev rdma-core; do \
		test ! -e debian/$$package.install.backup || mv debian/$$package.install.backup debian/$$package.install; \
	done

# 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.
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_SHAREDSTATEDIR:PATH=/var/lib" \
		     "-DCMAKE_INSTALL_RUNDIR:PATH=/run" \
		     "-DCMAKE_INSTALL_UDEV_RULESDIR:PATH=/lib/udev/rules.d" \
		     "-DCMAKE_INSTALL_PERLDIR:PATH=/usr/share/perl5" \
		     "-DENABLE_STATIC=1" \
		     $(EXTRA_CMAKE_FLAGS)

override_dh_auto_configure:
	if [ -e /usr/bin/python3 ]; then \
		dh_auto_configure $(DH_AUTO_CONFIGURE) \
				-DPYTHON_EXECUTABLE:PATH=/usr/bin/python3 \
				-DCMAKE_INSTALL_PYTHON_ARCH_LIB:PATH=/usr/lib/python3/dist-packages; \
	else \
		dh_auto_configure $(DH_AUTO_CONFIGURE) \
			        -DNO_PYVERBS=1; \
	fi


override_dh_auto_build:
	ninja -C build-deb -v

# upstream does not ship test cases
override_dh_auto_test:

override_dh_auto_install:
# Some providers are disabled on architectures that are not able to do coherent DMA
ifeq (,$(filter-out $(NON_COHERENT_DMA_ARCHS),$(DEB_HOST_ARCH)))
	for package in ibverbs-providers libibverbs-dev rdma-core; do \
		test -e debian/$$package.install.backup || cp debian/$$package.install debian/$$package.install.backup; \
	done
	sed -i '/efa\|hns\|ionic\|mana\|mlx[45]/d' debian/ibverbs-providers.install debian/libibverbs-dev.install debian/rdma-core.install
endif
	DESTDIR=$(CURDIR)/debian/tmp ninja -C build-deb install

# The following files are not used on Debian (we ship our own sysvinit script)
INST_EXCLUDE := "etc/init.d/srpd" \
		"etc/init.d/ibacm" \
		"usr/sbin/run_srp_daemon" \
		"usr/sbin/srp_daemon.sh"
INST_EXCLUDE := $(addprefix -X,$(INST_EXCLUDE))
override_dh_install:
	if [ -e build-deb/python/pyverbs/__init__.py ]; then \
		dh_install --fail-missing $(INST_EXCLUDE); \
	else \
		dh_install -Npython3-pyverbs --fail-missing $(INST_EXCLUDE) --remaining-packages; \
	fi

# cmake installs the correct init scripts in the correct place, just setup the
# pre-postrms
override_dh_installinit:
	dh_installinit -prdma-core --onlyscripts --name=iwpmd
	dh_installinit --remaining-packages

override_dh_installsystemd:
	dh_installsystemd -pibacm --no-start ibacm.service
	dh_installsystemd -pibacm ibacm.socket
	dh_installsystemd --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)

# 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 $@ $(dh_params)
