# Tool versions — keep in sync with .github/workflows/ci.yml
CARGO_DENY_VERSION          ?= 0.19.2
CARGO_TARPAULIN_VERSION     ?= 0.35.1
CARGO_SEMVER_CHECKS_VERSION ?= 0.50.0

.PHONY: check build test lint fmt fmt-check doc deny audit machete coverage semver wheel sdist tools bench bench-save bench-cmp oss-fixtures oss-clones oss-metrics oss-oracle check-generated formal help

## ─── Pre-commit gate ──────────────────────────────────────────────────────────
check: fmt-check lint test deny machete

## ─── Core ─────────────────────────────────────────────────────────────────────
build:
	cargo build --release --locked

test:
	cargo test --locked

lint:
	cargo clippy --all-targets --locked -- -D warnings
	RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --locked

fmt:
	cargo fmt

fmt-check:
	cargo fmt -- --check

doc:
	RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --locked

## ─── Benchmarks ───────────────────────────────────────────────────────────────
# bench-save/bench-cmp: pass BASELINE=<name> (e.g. make bench-save BASELINE=main)
bench:
	cargo bench --benches --locked

bench-save:
	cargo bench --benches --locked -- --save-baseline $(BASELINE)

bench-cmp:
	cargo bench --benches --locked -- --baseline $(BASELINE)

## ─── OSS dogfooding (Phase 1 §17) ─────────────────────────────────────────────
# oss-fixtures: in-repo regression skeleton (always available, no network).
# oss-clones:   clone the 20-project §17 validation set into target/oss-clones/.
# oss-metrics:  measure FP rate / crashes / cold-run speed vs §17 exit criteria.
#               Pass ARGS=--gate to fail when any criterion misses.
oss-fixtures:
	scripts/run-oss-fixture.sh --build

oss-clones:
	scripts/clone-oss-fixtures.sh

oss-metrics:
	scripts/oss-metrics.sh --build $(ARGS)

# oss-oracle: CHK001 remove-and-test oracle (#85 WS2). RUNS UNTRUSTED PROJECT
#             TESTS in disposable copies of target/oss-clones/ — opt-in, local
#             or isolated runner only, never release/default CI. Installs
#             nothing; pass ARGS="--python /path/to/venv/bin/python" to use a
#             pre-provisioned interpreter. See docs/dev/chk001-remove-and-test.md.
oss-oracle:
	scripts/oss-remove-and-test.py --build --execute $(ARGS)

## ─── Security & supply chain ──────────────────────────────────────────────────
deny:
	cargo deny check advisories licenses bans sources

audit:
	cargo audit --deny warnings

machete:
	cargo machete

## ─── Generated artifacts ───────────────────────────────────────────────────────
check-generated:
	python3 tests/test_harvest_package_map.py
	python3 scripts/generate-package-map.py
	python3 scripts/generate-stdlib-modules.py
	git diff --exit-code src/resolver/bundled/ src/resolver/stdlib/

## ─── Formal models (docs/dev/formal) ──────────────────────────────────────────
# Requires python3 with z3-solver (`pip install z3-solver`). Runs every model and
# fails if any property has a counterexample. TLA+ (TLC) is run separately; see
# docs/dev/formal/README.md.
formal:
	@status=0; for m in relative_import_model deps_rules_z3 exit_status_z3 ignore_model; do \
		python3 docs/dev/formal/$$m.py || status=1; \
	done; exit $$status

## ─── Code coverage ────────────────────────────────────────────────────────────
# NOTE: --fail-under is intentionally omitted until the analyzer is implemented.
# Re-enable at 95% once Phase 1 (v0.1 MVP) coverage is established.
# See docs/dev/ci-porting-notes.md.
coverage:
	cargo tarpaulin --out html --skip-clean --timeout 300 -- --test-threads=1

## ─── Semver ───────────────────────────────────────────────────────────────────
semver:
	cargo semver-checks --baseline-rev origin/main

## ─── Python / maturin distribution ───────────────────────────────────────────
wheel:
	uvx maturin build --release

sdist:
	uvx maturin sdist

## ─── Tool installation ────────────────────────────────────────────────────────
tools:
ifndef SKIP_TOOL_INSTALL
	cargo install cargo-deny@$(CARGO_DENY_VERSION) --locked
	cargo install cargo-tarpaulin@$(CARGO_TARPAULIN_VERSION) --locked
	cargo install cargo-semver-checks@$(CARGO_SEMVER_CHECKS_VERSION) --locked
endif

help:
	@grep -E '^## ' Makefile | sed 's/^## //'
	@echo ""
	@grep -E '^[a-zA-Z_-]+:' Makefile | grep -v '^help:' | awk -F: '{print "  " $$1}'
