#!/usr/bin/make -f
# vock, Debian packaging.
#
# Built with dh-cargo against Debian's system crate registry
# (/usr/share/cargo/registry). The only dependency, libc, comes from
# librust-libc-dev, so nothing is vendored and nothing is downloaded:
# dh_auto_configure symlinks the system registry into debian/cargo_registry
# and points cargo at it through $(CARGO_HOME)/config.toml.
#
# dh-cargo is written for debcargo-style packages that hold a single crate.
# Upstream is a virtual workspace with two members (the vock binary and the
# kcov-preload cdylib), so the steps dh-cargo cannot handle on its own are
# overridden below.

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

include /usr/share/dpkg/architecture.mk
include /usr/share/dpkg/buildflags.mk
include /usr/share/dpkg/pkg-info.mk
include /usr/share/rustc/architecture.mk

# The overrides call the Debian cargo wrapper directly, outside dh_auto_*,
# so they need the environment dh-cargo would otherwise set up for them.
# See the header of /usr/share/cargo/bin/cargo. The wrapper forwards LDFLAGS
# to rustc as -C link-arg=..., so the hardening flags reach the linker.
export CFLAGS CXXFLAGS CPPFLAGS LDFLAGS
export DEB_HOST_RUST_TYPE DEB_HOST_GNU_TYPE
export CARGO_HOME = $(CURDIR)/debian/cargo_home
export DEB_CARGO_CRATE = vock_$(DEB_VERSION_UPSTREAM)
# Fail instead of reaching for the network if a crate is ever missing from
# the system registry.
export CARGO_NET_OFFLINE = true
# The top-level Cargo.toml is a virtual manifest, which `cargo install`
# rejects; install the binary from its member directory instead.
export DEB_CARGO_INSTALL_PATH = $(CURDIR)/vock

CARGO = /usr/share/cargo/bin/cargo

# The wrapper always passes --target, so artifacts land under the target
# triple for native builds as well as cross builds.
ARTIFACTS = target/$(DEB_HOST_RUST_TYPE)/release

# A private helper directory, not a multiarch library path: kcov.so is loaded
# by vock alone and the package is not Multi-Arch: same, so co-installability
# across architectures does not apply. /usr/lib/vock is compiled into the
# binary's search path, so no build-time path substitution is needed.
LIBDIR = usr/lib/vock

%:
	dh $@ --buildsystem cargo

# dh-cargo deletes Cargo.lock before building, because it pins versions
# (libc 0.2.189, say) that the Debian registry may not carry; cargo then
# resolves against whatever librust-*-dev provides. Keep a copy so that
# `debian/rules clean` gives back the tracked file.
override_dh_auto_configure:
	[ ! -f Cargo.lock ] || cp -p Cargo.lock debian/Cargo.lock.upstream
	dh_auto_configure

override_dh_auto_clean:
	dh_auto_clean
	[ ! -f debian/Cargo.lock.upstream ] || mv debian/Cargo.lock.upstream Cargo.lock

# For a binary-only package dh-cargo's build step compiles nothing; it leaves
# the compile to `cargo install`, which only handles [[bin]] targets. Build
# the whole workspace here so the kcov-preload cdylib exists for
# dh_auto_install. `cargo install` then reuses these artifacts.
override_dh_auto_build:
	dh_auto_build
	$(CARGO) build --release --workspace

# dh-cargo's default test step is only `cargo build`; ask for the real test
# suite. The tests run the built binaries, so skip them when cross-building.
# nocheck is honoured by dh_auto_test itself.
override_dh_auto_test:
ifeq ($(DEB_BUILD_ARCH),$(DEB_HOST_ARCH))
	dh_auto_test -- test --release --workspace
else
	@echo "dh_auto_test: skipping the test suite (cross-building for $(DEB_HOST_ARCH))"
endif

override_dh_auto_install:
	# Installs usr/bin/vock and generates the cargo:*Built-Using substvars.
	dh_auto_install
	# The LD_PRELOAD coverage shim is a private helper, not a public library:
	# it is dlopened by vock alone, has no SONAME and no stable ABI, so it
	# belongs under a private directory rather than in the linker path.
	install -D -m 0644 $(ARTIFACTS)/libkcov_preload.so \
	  debian/vock/$(LIBDIR)/kcov.so

# The shim is deliberately not a public shared library.
override_dh_makeshlibs:
	dh_makeshlibs -Xkcov.so

override_dh_shlibdeps:
	dh_shlibdeps -Xkcov.so
