#!/usr/bin/make -f

# Exports DEB_{BUILD,HOST}_* flags
include /usr/share/dpkg/architecture.mk

# Pass this as version-info to dh_makeshlibs
SHLIBS_MINVER = 1:2.63

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# Support cross-compiling
ifneq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
CC := $(DEB_HOST_GNU_TYPE)-gcc
endif

# One upstream target, "_makenames", requires the BUILD compiler instead of HOST
BUILD_CC ?= cc
export BUILD_LDFLAGS = $(shell dpkg-architecture -a$(DEB_BUILD_ARCH) -c dpkg-buildflags --get LDFLAGS)
export BUILD_CPPFLAGS = $(shell dpkg-architecture -a$(DEB_BUILD_ARCH) -c dpkg-buildflags --get CPPFLAGS)

# Upstream's way of setting optimization
ifneq (,$(filter noopt, $(DEB_BUILD_OPTIONS)))
COPTS = COPTS=-O0
endif

# See pam_cap/Makefile for FORCELINKPAM
MAKE_ASSIGNMENTS = \
	prefix=/usr \
	FORCELINKPAM=yes \
	GOLANG=no

%:
	dh ${@}


override_dh_auto_clean:
	dh_auto_clean -- $(MAKE_ASSIGNMENTS)


override_dh_auto_build:
	# See pam_cap/Makefile for FORCELINKPAM
	dh_auto_build -- \
		$(MAKE_ASSIGNMENTS) \
		BUILD_CC=$(BUILD_CC) \
		CROSS_COMPILE=$(DEB_HOST_GNU_TYPE)- \
		$(COPTS) \
		DYNAMIC=yes \
		SUDO=$(DEB_GAIN_ROOT_CMD)
	# Some of test suite is only invoked when run as real root. Explicitly
	# build all binaries so we can include them in the autopkgtest.	
	dh_auto_build -D libcap -- $(MAKE_ASSIGNMENTS) cap_test
	dh_auto_build -D pam_cap -- $(MAKE_ASSIGNMENTS) test_pam_cap
	# Change lookup from sibling dir to current dir
	sed -i -e 's,\.\./progs/tcapsh-static,./tcapsh-static,g' tests/libcap_launch_test.c
	dh_auto_build -D tests -- $(MAKE_ASSIGNMENTS) \
		psx_test \
		libcap_psx_test \
		uns_test \
		libcap_launch_test \
		libcap_psx_launch_test \
		noop \
		exploit \
		noexploit
	dh_auto_build -D progs -- $(MAKE_ASSIGNMENTS) tcapsh-static


override_dh_installdirs:
	dh_installdirs
	# While the PAM module and the library are in /lib, according to the
	# FHS, development files must go into usr/lib/<triplet>.
	mkdir -p debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)


override_dh_auto_install:
	# Unfortunately, RPATH seems to be needed for the build-time tests
	# (avoiding it and going through LD_LIBRARY_PATH didn't work in all
	# cases)
	# List is identical to build above, minus noop (no RPATH)
	cd tests && chrpath -d \
		psx_test \
		libcap_psx_test \
		uns_test \
		libcap_launch_test \
		libcap_psx_launch_test \
		exploit \
		noexploit
	dh_auto_install -- \
		$(MAKE_ASSIGNMENTS) \
		lib=lib/$(DEB_HOST_MULTIARCH) \
		PKGCONFIGDIR=/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig \
		RAISE_SETFCAP=no


override_dh_auto_test:
	dh_auto_test -- $(MAKE_ASSIGNMENTS)


override_dh_makeshlibs:
	dh_makeshlibs -V'libcap2 (>= $(SHLIBS_MINVER))' -plibcap2 \
		--add-udeb=libcap2-udeb -- -c4
	dh_makeshlibs --remaining-packages -- -c4
