#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# OBS has no network at build time, so cargo builds from the vendor/ directory
# the orig tarball carries and never from crates.io (debian/README.source).
# CARGO_HOME has to be inside the tree as well: the build user on an OBS worker
# has no writable home.
export CARGO_HOME = $(CURDIR)/debian/cargo_home
export CARGO_NET_OFFLINE = true

# Upstream's release profile sets strip = true, which would leave dh_strip
# nothing to move into the -dbgsym package.
export CARGO_PROFILE_RELEASE_STRIP = false
export CARGO_PROFILE_RELEASE_DEBUG = true

# This needs rustc 1.88 for let chains: on trixie that is rustc from
# trixie-backports, which is unversioned, and on noble it is the versioned
# cargo-1.89 package. debian/control asks for either; build with the one that
# got installed.
CARGO := $(or $(wildcard /usr/bin/cargo-1.89),cargo)

%:
	dh $@

override_dh_auto_build:
	$(CARGO) build --release --locked --offline

# --lib --bins, not the default: cargo would otherwise run doctests through
# rustdoc, and the versioned cargo-1.89 on noble keeps rustdoc in
# /usr/lib/rust-1.89/bin where it is not on PATH, so the doctest step dies on
# "could not execute process rustdoc" after a clean build.
override_dh_auto_test:
	$(CARGO) test --release --locked --offline --lib --bins

override_dh_auto_clean:
	$(CARGO) clean --offline
	$(RM) -r $(CARGO_HOME)

# dh_clean deletes *.orig as patch leftovers, and `cargo vendor` writes a
# Cargo.toml.orig in every crate it vendors, so without this the clean target
# strips 250-odd files out of the upstream source.
override_dh_clean:
	dh_clean -XCargo.toml.orig
