#!/usr/bin/make -f

export DH_VERBOSE=1

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

%:
	dh $@

override_dh_auto_build-arch:
	# /usr/src/packages/SOURCES is OBS specific path
	tar xf /usr/src/packages/SOURCES/cloud-hypervisor-52.0.0-vendor.tar.gz
	mkdir -p .cargo
	cp /usr/src/packages/SOURCES/config.toml .cargo/
	cargo build --release --target=$(RUST_DEF_TARGET) --all --offline

override_dh_auto_install:
	install -d debian/cloud-hypervisor/usr
	install -d debian/cloud-hypervisor/usr/bin
	install -D -m755  ./target/$(RUST_DEF_TARGET)/release/cloud-hypervisor debian/cloud-hypervisor/usr/bin
	/sbin/setcap cap_net_admin+ep debian/cloud-hypervisor/usr/bin/cloud-hypervisor
	install -D -m755  ./target/$(RUST_DEF_TARGET)/release/ch-remote debian/cloud-hypervisor/usr/bin
	install -d debian/cloud-hypervisor/usr/lib
	install -d debian/cloud-hypervisor/usr/lib/cloud-hypervisor
	install -D -m755 target/$(RUST_DEF_TARGET)/release/vhost_user_block debian/cloud-hypervisor/usr/lib/cloud-hypervisor
	install -D -m755 target/$(RUST_DEF_TARGET)/release/vhost_user_net debian/cloud-hypervisor/usr/lib/cloud-hypervisor

override_dh_auto_clean:
	cargo clean

ifeq ($(DEB_BUILD_ARCH_CPU), amd64)
    RUST_DEF_TARGET := x86_64-unknown-linux-gnu
else ifeq ($(DEB_BUILD_ARCH_CPU), aarch64)
    RUST_DEF_TARGET := aarch64-unknown-linux-gnu
else ifeq ($(DEB_BUILD_ARCH_CPU), arm64)
    RUST_DEF_TARGET := aarch64-unknown-linux-gnu
else
    $(error unrecognized build host instruction set $(DEB_HOST_ARCH_CPU)!)
endif

ifeq ($(DEB_BUILD_ARCH_CPU), amd64)
    RUST_ARCH := x86_64
else ifeq ($(DEB_BUILD_ARCH_CPU), aarch64)
    RUST_ARCH := aarch64
else ifeq ($(DEB_BUILD_ARCH_CPU), arm64)
    RUST_ARCH := aarch64
else
    $(error unrecognized build host instruction set $(DEB_HOST_ARCH_CPU)!)
endif

ifeq ($(DEB_BUILD_ARCH_CPU), amd64)
    CARGO_PKG_FEATURE_OPTS := --no-default-features --features "mshv,kvm"
else ifeq ($(DEB_BUILD_ARCH_CPU), aarch64)
    CARGO_PKG_FEATURE_OPTS := --all
else ifeq ($(DEB_BUILD_ARCH_CPU), arm64)
    CARGO_PKG_FEATURE_OPTS := --all
else
    $(error unrecognized build host instruction set $(DEB_HOST_ARCH_CPU)!)
endif

RUST_DEF_TARGET := $(RUST_ARCH)-unknown-linux-gnu

