#!/usr/bin/make -f

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

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export CFLAGS CXXFLAGS CPPFLAGS LDFLAGS
export DEB_HOST_RUST_TYPE DEB_HOST_GNU_TYPE

# crosvm is a cargo workspace application, not a library crate, so it is
# built with the Debian cargo wrapper directly rather than with dh-cargo's
# buildsystem (which is meant for debcargo-generated crate packages).
CARGO = /usr/share/cargo/bin/cargo
export CARGO_HOME = $(CURDIR)/debian/cargo_home
export DEB_CARGO_CRATE = crosvm_$(DEB_VERSION_UPSTREAM)

# Upstream's default feature set includes gpu and slirp:
#   gpu   pulls rutabaga_gfx and magma-gpu, which are not in Debian.
#   slirp is a Windows-only network backend upstream.
# Everything needed to run a Linux guest is in the set below. If this
# changes, regenerate debian/patches/debian-trim-workspace.patch
# (see debian/README.source).
CROSVM_FEATURES = qcow,balloon,net,usb,audio

# dh_auto_install is overridden and installs explicitly, so the single-binary
# add-on (implicit before compat 15) is not wanted.
%:
	dh $@ --without=single-binary

override_dh_auto_configure:
	# Resolve against the system registry instead of upstream's lockfile.
	rm -f Cargo.lock
	$(CARGO) prepare-debian debian/cargo_registry --link-from-system

override_dh_auto_build:
	$(CARGO) build --release --bin crosvm \
		--no-default-features --features $(CROSVM_FEATURES)

# The upstream test suite starts virtual machines and needs /dev/kvm, which a
# build chroot does not have.
override_dh_auto_test:

override_dh_auto_install:
	install -D -m0755 target/$(DEB_HOST_RUST_TYPE)/release/crosvm \
		debian/crosvm/usr/bin/crosvm
	# crosvm embeds the compiled seccomp policies, but the sources are what
	# the policies' own @include paths refer to, so --seccomp-policy-dir works.
	set -e; for p in jail/seccomp/$(DEB_HOST_GNU_CPU)/*.policy; do \
		install -D -m0644 "$$p" \
			"debian/crosvm/usr/share/policy/crosvm/$$(basename $$p)"; \
	done
	/usr/share/cargo/bin/dh-cargo-built-using crosvm

override_dh_auto_clean:
	rm -rf target debian/cargo_home debian/cargo_registry
