#!/usr/bin/make -f

include /usr/share/dpkg/architecture.mk

export DEB_BUILD_MAINT_OPTIONS=hardening=+all

NON_COHERENT_DMA_ARCHS = alpha arc armel armhf hppa m68k sh4

ifneq ($(shell dh -l 2>&1 | grep -w -o python3 2>/dev/null),)
   PYTHON_DH = python3
else
   PYTHON_DH = python2
endif
ifneq ($(shell dh -l 2>&1 | grep -w -o systemd 2>/dev/null),)
   SYSTEMD_DH_EXTRA = ,systemd
else
   SYSTEMD_DH_EXTRA =
endif
DH_MODULES = $(PYTHON_DH)$(SYSTEMD_DH_EXTRA)

ifneq (,$(filter nopyverbs,$(DEB_BUILD_OPTIONS)))
   NO_PYVERBS = 1
else
ifneq (,$(wildcard /usr/bin/python3))
   NO_PYVERBS = 0
else
   NO_PYVERBS = 1
endif
endif

%:
	dh $@ --with $(DH_MODULES) --builddirectory=build-deb

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 := "--" \
		     "-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:
ifeq ($(EXTRA_CMAKE_FLAGS), -DNO_PYVERBS=1)
	dh_auto_configure $(DH_AUTO_CONFIGURE)
else
	dh_auto_configure $(DH_AUTO_CONFIGURE) \
		        -DNO_PYVERBS=$(NO_PYVERBS) \
			-DCMAKE_INSTALL_PYTHON_ARCH_LIB:PATH=/usr/lib/python3/dist-packages
endif

override_dh_auto_build:
	dh_auto_build -- -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\|mana\|mlx[45]/d' debian/ibverbs-providers.install debian/libibverbs-dev.install debian/rdma-core.install
endif
	dh_auto_install
	rm -f debian/tmp/lib/systemd/system/ibacm.socket
	mkdir installed_docs
	mv debian/tmp/usr/share/doc/rdma-core/* installed_docs/
	rm -rf debian/tmp/usr/share/doc
	rm -f \
		debian/tmp/etc/libibverbs.d/efa.driver \
		debian/tmp/etc/libibverbs.d/mlx4.driver \
		debian/tmp/usr/lib/*/libibverbs/libefa-rdmav*.so \
		debian/tmp/usr/lib/*/libibverbs/libmlx4-rdmav*.so \
		#

# 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:
	extra_params= ; \
	if [ ! -e build-deb/python/pyverbs/__init__.py ]; then \
		extra_params="-Npython3-pyverbs --remaining-packages" ; \
		mkdir -p installed_docs/tests; \
	fi ; \
	dh_install $$extra_params $(INST_EXCLUDE) --fail-missing

# cmake installs the correct init scripts in the correct place, just setup the
# pre-postrms
override_dh_installinit:
	dh_installinit -psrptools --error-handler=srptools_error_handler --no-start
	dh_installinit --remaining-packages

override_dh_installsystemd:
	dh_installsystemd -pibacm --no-start ibacm.service
	dh_installsystemd -psrptools --no-start --no-enable
	dh_installsystemd --remaining-packages

override_dh_systemd_start:
	dh_systemd_start -psrptools --no-start
	dh_systemd_start --remaining-packages

override_dh_systemd_enable:
	dh_systemd_enable -psrptools --no-enable
	dh_systemd_enable --remaining-packages

override_dh_compress:
	dh_compress -X .py

# 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)

override_dh_strip:
	dh_strip -plibibmad5 --dbg-package=libibmad5-dbg
	dh_strip -plibibnetdisc5 --dbg-package=libibnetdisc5-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 $(DH_MODULES) --builddirectory=build-deb
