#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/default.mk
# The cc crate (C shim in libexec.so) reads CFLAGS but not CPPFLAGS,
# which carries -D_FORTIFY_SOURCE.
export CFLAGS += $(CPPFLAGS)
include /usr/share/rustc/architecture.mk

DESTDIR=$(CURDIR)/debian/tmp

export DEB_HOST_RUST_TYPE
export PATH:=/usr/share/cargo/bin:$(PATH)
export CARGO=/usr/share/cargo/bin/cargo
export CARGO_HOME=$(CURDIR)/debian/cargo_home
export CARGO_REGISTRY=$(CURDIR)/debian/cargo_registry
export DEB_CARGO_CRATE=$(DEB_SOURCE)_$(DEB_VERSION_UPSTREAM)

ifneq (,$(filter $(DEB_HOST_ARCH), i386 armhf))
	export INTERCEPT_LIBDIR=lib64
else
	export INTERCEPT_LIBDIR=lib
endif

CARGO_BASE_ARGS= --release --target $(DEB_HOST_RUST_TYPE)

%:
	dh $@ --with shell_completions	--buildsystem=rust

# The exit_codes tests assert sub-2s signal teardown and flake when run
# in parallel with the rest of the suite; run them on their own, serially.
override_dh_auto_test:
	dh_auto_test -- -- --skip cases::exit_codes::
	dh_auto_test -- -p integration-tests --test integration -- \
		--test-threads=1 cases::exit_codes::

execute_after_dh_auto_clean:
	rm -f Cargo.lock

override_dh_auto_install:
	target/$(DEB_HOST_RUST_TYPE)/release/generate-completions target/$(DEB_HOST_RUST_TYPE)/release/completions
	SRCDIR=target/$(DEB_HOST_RUST_TYPE)/release DESTDIR=${DESTDIR} PREFIX=/usr ./scripts/install.sh

# dh_fixperms makes everything under usr/libexec executable.
execute_after_dh_fixperms:
	chmod 0644 debian/bear/usr/libexec/bear/$(INTERCEPT_LIBDIR)/libexec.so
