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

.DEFAULT_GOAL := help
.DELETE_ON_ERROR:

.NOTPARALLEL:
.PHONY: help nix-shell build boards docs configurator fmt test app-native-test coverage verification verification-tools miri kani verification-inventory pre-commit check clean

help: ## Show the command interface (the default)
	@printf '%s\n' 'Kaptan developer commands' '' \
		'Usage: make <target>' '' \
		'  help                         Show this help' \
		'  nix-shell                    Enter the Nix development shell' \
		'  build                        Build all firmware and documentation' \
		'  boards                       Build and package every board' \
		'  docs                         Test and build the mdBook project' \
		'  configurator                 Start the local configurator web application' \
		'  fmt                          Check Rust, shell, Python, Nix, and TOML formatting' \
		'  test                         Run shared-library, host algorithm, and script tests' \
		'  app-native-test              Check the app UTF conversion function with a host API stub' \
		'  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' \
		'  pre-commit                   Run fmt, firmware clippy/test/docs, and mdBook checks' \
		'  check                        Run pre-commit plus whitespace checks' \
		'  clean                        Remove generated build artifacts' \
		'' 'Component workflows' \
		'  make -C firmware help' \
		'  make -C firmware/boards/<board> build|flash' \
		'  make -C docs build|test|check|clean' \
		'' 'Native tools (run from firmware/)' \
		'  make test' \
		'  cargo run --locked --offline -p kaptan-configurator -- --baud 115200'

nix-shell: ## Enter the reproducible Nix development shell
	nix develop

build: boards ## Build all firmware boards and documentation
	+@$(MAKE) -C docs build

boards:
	+@$(MAKE) -C firmware/boards build

docs:
	+@$(MAKE) -C docs check

configurator:
	+@$(MAKE) -C firmware/tools/configurator run

fmt:
	+@$(MAKE) -C firmware fmt
	shfmt -d $$(rg --files -g '*.sh')
	ruff format --check $$(rg --files -g '*.py')
	ruff check firmware/scripts
	nixfmt --check $$(rg --files -g '*.nix')
	taplo format --check $$(rg --files -g '*.toml')

test:
	+@$(MAKE) -C firmware test
	+@$(MAKE) app-native-test

app-native-test:
	python3 ground_suite/windows/runner/test_utf8_from_utf16.py \
		--work-dir "$(or $(CARGO_TARGET_DIR),target)/native-tests/utf8"

coverage:
	+@$(MAKE) -C firmware coverage

verification verification-tools miri kani verification-inventory:
	+@$(MAKE) -C firmware $@

pre-commit: fmt test
	+@$(MAKE) -C firmware clippy
	+@$(MAKE) -C firmware docs
	+@$(MAKE) -C docs check
	@printf '%s\n' 'Pre-commit passed. Coverage was not run; make coverage is a required final gate.'

check: pre-commit ## Run pre-commit plus whitespace checks
	git diff HEAD --check

clean: ## Remove firmware and documentation build artifacts
	+@$(MAKE) -C firmware clean
	+@$(MAKE) -C docs clean
