SHELL := /bin/sh
MAKEFLAGS += --no-print-directory
export CARGO_BUILD_JOBS ?= 2

.DEFAULT_GOAL := help
.DELETE_ON_ERROR:

override BOARDS := geprc-f722-v2 kakute-f7-mini-v3
override BOARD_PACKAGES := $(addprefix kaptan-,$(BOARDS))
override BOARD_PACKAGE_ARGS := $(addprefix -p ,$(BOARD_PACKAGES))
override HOST_EXCLUDES := $(addprefix --exclude ,$(BOARD_PACKAGES))
override HOST_FEATURES := --features kaptan-navigation/sensor-snapshots

.NOTPARALLEL:
.PHONY: help fmt clippy test native-test docs coverage target-checks verification verification-tools miri kani verification-inventory pre-commit check clean

help:
	@printf '%s\n' 'Usage: make -C firmware <target>' '' \
		'Workspace targets:' \
		'  fmt     Check Rust formatting' \
		'  clippy  Run host and embedded Clippy checks' \
		'  test    Run Rust and firmware script tests' \
		'  native-test  Check cross-crate error IDs at Rust/C++ release final link' \
		'  docs    Build Rust API documentation with warnings denied' \
		'  coverage  Required final strict-core line/branch coverage gate' \
		'  verification  Run all gates and retain source-bound evidence' \
		'  verification-tools  Install the pinned Miri and Kani tools' \
		'  miri    Run the manifest-selected Miri checks' \
		'  kani    Run the manifest-selected Kani proofs' \
		'  verification-inventory  Show verification owners, claims and gaps' \
		'  target-checks  Check sensor-free consumers and link cross-route fixtures' \
		'  pre-commit  Run fmt, clippy, test, and docs' \
		'  check   Alias for pre-commit' \
		'  clean   Remove Cargo build artifacts' '' \
		'For other Cargo options, run cargo directly from firmware/.' \
		'Host gates exclude bare-metal boards; navigation/sensor-snapshots is enabled.'

fmt:
	cargo fmt --all --check

clippy:
	cargo clippy --workspace --all-targets $(HOST_EXCLUDES) \
		$(HOST_FEATURES) --locked --offline -- -D warnings
	cargo clippy -p platform-stm32f7 --all-targets \
		--locked --offline -- -D warnings
	cargo clippy $(BOARD_PACKAGE_ARGS) $(addprefix --bin ,$(BOARD_PACKAGES)) --target thumbv7em-none-eabihf \
		--locked --offline -- -D warnings

test:
	cargo +1.90.0 test --workspace $(HOST_EXCLUDES) $(HOST_FEATURES) --locked --offline
	cargo +1.90.0 test -p platform-stm32f7 -p synopsys-usb-otg --lib --locked --offline
	node --test tools/configurator/web/view.test.cjs
	python3 -m unittest scripts/test-analyze-fw.py \
		scripts/test-summarize-test-coverage.py scripts/test-build-workflow.py \
		scripts/test-verify.py
	+@$(MAKE) native-test

native-test:
	python3 libs/kaptan-err-reportable-macros/tests/final_link.py \
		--work-dir "$(or $(CARGO_TARGET_DIR),target)/native-tests/final-link"

docs:
	RUSTDOCFLAGS='-D warnings' cargo doc --workspace --no-deps \
		$(HOST_EXCLUDES) $(HOST_FEATURES) --locked --offline

coverage:
	scripts/report-test-coverage.sh

verification:
	python3 scripts/verify.py all

verification-tools:
	python3 scripts/verify.py setup

miri kani:
	python3 scripts/verify.py $@

verification-inventory:
	python3 scripts/verify.py inventory

target-checks:
	cargo check -p kaptan-configurator -p kaptan-diagnostics-mavlink --locked --offline
	cargo check -p kaptan-mavlink -p kaptan-log --no-default-features --locked --offline
	cargo check -p kaptan-flight-runtime --no-default-features \
		--target thumbv7em-none-eabihf --locked --offline
	RUSTFLAGS='-C link-arg=-Tlink.x -L platforms/stm32f7/link/f722' \
		cargo build -p platform-stm32f7 --example mpu_dual_uart --example icm_dual_uart \
		--release --target thumbv7em-none-eabihf --locked --offline

pre-commit: fmt clippy test docs
	@printf '%s\n' 'Pre-commit passed. Coverage was not run; make coverage is a required final gate.'

check: pre-commit

clean:
	cargo clean
