#!/usr/bin/make -f

export DH_VERBOSE = 1
export CARGO_HOME = $(CURDIR)/debian/cargo_home
# Use the system libssl-dev; do not let openssl-sys vendor/build OpenSSL from
# source (it does not recognise all Rust target triples, e.g. riscv64a23).
export OPENSSL_NO_VENDOR = 1
export ZSTD_SYS_USE_PKG_CONFIG = 1

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

CARGO_FEATURE_LIST_amd64 := kvm,io_uring,fw_cfg,sev_snp
CARGO_FEATURE_LIST_arm64 := kvm,io_uring,fw_cfg
CARGO_FEATURE_LIST_riscv64 := kvm,io_uring

CARGO_FEATURE_LIST := $(CARGO_FEATURE_LIST_$(DEB_HOST_ARCH))
CARGO_FEATURES := --no-default-features --features $(CARGO_FEATURE_LIST)

CARGO_VENDOR_DIR = debian/rust-vendor

%:
	dh $@

override_dh_auto_configure:
	mkdir -p $(CARGO_HOME)
	mkdir -p .cargo
	cp debian/cargo-config.toml .cargo/config.toml

override_dh_auto_build:
	cargo build --release --offline $(CARGO_FEATURES) \
		--package cloud-hypervisor

override_dh_auto_test:

override_dh_auto_install:
	install -D -m 0755 target/release/cloud-hypervisor \
		debian/cloud-hypervisor/usr/bin/cloud-hypervisor
	install -D -m 0755 target/release/ch-remote \
		debian/cloud-hypervisor/usr/bin/ch-remote

override_dh_auto_clean:
	cargo clean 2>/dev/null || true
	rm -rf .cargo $(CARGO_HOME)

# dh_clean removes *.orig files (treating them as patch artifacts), but
# vendored crates ship Cargo.toml.orig and cargo verifies checksums for
# every file listed in .cargo-checksum.json. Exclude rust-vendor/ from the
# cleanup so those files survive into the build.
override_dh_clean:
	dh_clean -Xrust-vendor

override_dh_dwz:
override_dh_strip:
	strip --strip-unneeded --remove-section=.comment --remove-section=.note \
		debian/cloud-hypervisor/usr/bin/cloud-hypervisor \
		debian/cloud-hypervisor/usr/bin/ch-remote

# The vendored-sources override in .cargo/config.toml (dropped in by
# dh_auto_configure) points crates-io at debian/rust-vendor/, so cargo cannot
# resolve anything while we are regenerating it.  Remove it first.
#
# The --exclude-crate-path='*#.git*' / '*#.mailmap' entries drop the VCS
# metadata that some crates ship.  dpkg-source strips those names from the
# debian tarball via its default --tar-ignore patterns, but cargo still
# expects every file listed in .cargo-checksum.json to be present, so leaving
# them in makes the unpacked source fail to build with
# "failed to calculate checksum of: .../.gitmodules".
vendor:
	rm -rf .cargo $(CARGO_VENDOR_DIR)
	python3 debian/vendor-dependencies.py \
		--output $(CARGO_VENDOR_DIR) \
		--target-features=x86_64-unknown-linux-gnu=$(CARGO_FEATURE_LIST_amd64) \
		--target-features=aarch64-unknown-linux-gnu=$(CARGO_FEATURE_LIST_arm64) \
		--target-features=riscv64gc-unknown-linux-gnu=$(CARGO_FEATURE_LIST_riscv64) \
		--exclude-crate-path='*#tests' \
		--exclude-crate-path='linux-loader#src/loader/**/*.bin' \
		--exclude-crate-path='*#**/*.png' \
		--exclude-crate-path='*#**/*.jpg' \
		--exclude-crate-path='*#**/*.dtb' \
		--exclude-crate-path='flate2#examples' \
		--exclude-crate-path='ring#src/rsa/signature_rsa_example_private_key.der' \
		--exclude-crate-path='ring#src/rsa/signature_rsa_example_public_key.der' \
		--exclude-crate-path='ring#pregenerated/*.o' \
		--exclude-crate-path='vcpkg#test-data' \
		--exclude-crate-path='zlib-rs#src/deflate/test-data' \
		--exclude-crate-path='gdbstub#examples' \
		--exclude-crate-path='proptest#proptest-regressions' \
		--exclude-crate-path='proptest#test-persistence-location' \
		--exclude-crate-path='*#.gitmodules' \
		--exclude-crate-path='*#.gitignore' \
		--exclude-crate-path='*#.gitattributes' \
		--exclude-crate-path='*#.mailmap' \
		--exclude-crate-path='libssh2-sys#libssh2' \
		--exclude-crate-path='openssl-src#openssl' \
		--exclude-crate-path='zstd-sys#zstd' \
		--exclude-crate-path='libz-sys#src'
	@echo "Regenerated ./debian/rust-vendor"

.PHONY: vendor
