#!/usr/bin/make -f
# -*- makefile -*-
# 本包已迁移为 Rust 构建，使用 cargo 替代原 configure + make。

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

include /usr/share/dpkg/architecture.mk

# sbuild/pbuilder 常将 HOME 设为 /sbuild-nonexistent，Cargo 默认写 ~/.cargo 会权限失败。
# 必须把注册表缓存放在源码树内可写路径。
export CARGO_HOME = $(CURDIR)/debian/cargo-home

# 使用 rustc 的 -Z stack-protector 启用栈保护。
# 取值可为 strong 或 all；如需最强保护可在构建时覆盖：
#   make STACK_PROTECTOR_LEVEL=all
STACK_PROTECTOR_LEVEL ?= strong
export RUSTC_BOOTSTRAP = 1
export RUSTFLAGS += -Z stack-protector=$(STACK_PROTECTOR_LEVEL) \
					-C link-arg=-Wl,-z,relro \
					-C link-arg=-Wl,-z,now \
					-C link-arg=-Wl,-z,noexecstack


# 不再运行 configure（无 autotools）
override_dh_auto_configure:
	@echo "Rust 构建，跳过 configure"

override_dh_auto_clean:
	[ ! -f Cargo.toml ] || cargo clean
	[ ! -d target ] || rm -rf target
	rm -rf debian/cargo-home

# 构建环境需能访问 .cargo/config.toml 中的镜像；勿加 --offline，否则无法从镜像拉取 crate。
override_dh_auto_build:
	mkdir -p "$(CARGO_HOME)"
	cargo build --release --locked

override_dh_auto_install:
	install -d debian/kylin-patch/usr/bin
	install -m 755 target/release/patch debian/kylin-patch/usr/bin/patch

# Rust 项目无 Makefile，用 cargo test 替代 make test
override_dh_auto_test:
	mkdir -p "$(CARGO_HOME)"
	cargo test --release --locked

%:
	dh $@

.PHONY: override_dh_auto_configure override_dh_auto_clean override_dh_auto_build override_dh_auto_install override_dh_auto_test
