# Makefile for easy development workflows.
# See development.md for docs.
# Note GitHub Actions call uv directly for setup and lint, but run `make test`
# so CI and local test selection stay identical.

.DEFAULT_GOAL := default
DOCS_ADDR ?= localhost:8000
# Workers for the long pytest targets. 6 of 8 cores leaves room for builders; 0 is one process.
# loadfile keeps a file's tests together, on one worker and in order. No worker restarts: xdist
# would otherwise rerun the test that killed one (by the memory cap, say) up to 4 times per worker.
PYTEST_WORKERS ?= 6
PYTEST_PARALLEL = -n $(PYTEST_WORKERS) --dist=loadfile --max-worker-restart=0

.PHONY: default install lint test test-prove test-pilot test-tumbler test-fold test-fold-new test-bytecode test-conformance test-conformance-repeat conformance-export conformance-workspace render-corpus render-compare watch-pilot-burner watch-pilot-completed test-hypothesis test-integration test-fuzz test-fuzz-gate verify upgrade build clean docs-clean docs-generated-clean docs-generate docs-serve docs-build docs-check

default: install verify

install:
	uv sync --locked --all-extras --dev

lint:
	uv run devtools/lint.py

test:
	uv run pytest -m "not integration and not hypothesis and not fuzz and not tumbler and not conformance" --runner-backend=both $(PYTEST_PARALLEL)

test-fold:
	uv run pytest -m "not integration and not hypothesis and not fuzz and not tumbler and not conformance" --fold-check -q $(PYTEST_PARALLEL)

# The test files this branch adds, under --fold-check: the mergeq `fold-new` stage, so a new file
# cannot first meet the lockstep in the full `test-fold` run.
test-fold-new:
	uv run python devtools/fold_new_tests.py

# These tests cover the code that reads bytecode, which can change with any Python release.
# CI runs the full suite on the oldest and newest Python, and only these tests on the versions
# between.
BYTECODE_TESTS = \
  tests/core/test_tag.py \
  tests/core/test_tag_type_classes.py \
  tests/core/test_function_source.py \
  tests/core/test_kernel_calls_the_function.py \
  tests/core/test_reentrant_scan.py \
  tests/core/test_run_function_arguments.py \
  tests/core/test_function_gate.py

test-bytecode:
	uv run pytest -m "not integration and not hypothesis and not fuzz and not tumbler and not conformance" --runner-backend=both -q $(BYTECODE_TESTS)

test-prove:
	uv run pytest tests/core/analysis/ -k "prove or packml_diagnosis" -q
	uv run pytest tests/facts -q

test-pilot:
	uv run pytest tests/core/analysis/ -k "pilot" -q $(PYTEST_PARALLEL)

test-tumbler:
	uv run pytest tests/tumbler/ -q

# Conformance cases over Modbus TCP against pyrung's local soft PLC.
# Point at a real CLICK running the exported program with PLC=host[:port];
# run some groups only with GROUP=name[,name...], or a project of picked cases with CASES=.
CONFORMANCE_ARGS = $(if $(PLC),--conformance-plc=$(PLC)) $(if $(GROUP),--conformance-group=$(GROUP)) $(if $(CASES),--conformance-cases=$(CASES)) $(if $(TARGET),--conformance-target=$(TARGET))

test-conformance:
	uv run pytest -m "conformance and not suite_repeat" tests/conformance/ -q -rP $(CONFORMANCE_ARGS)

# The suite twice on one PLC, then each group alone: a case must not depend on what ran before it.
# It takes as long as several suite runs, so it runs before a hardware round, not in the gate.
test-conformance-repeat:
	uv run pytest -m suite_repeat tests/conformance/ -q -rP $(CONFORMANCE_ARGS)

# Write the conformance CLICK project (main, one subroutine per group, nicknames).
# PART=coils, copy, math, drum1, drum2, drum3, control, system or boot writes one part: the whole suite is over CLICK's 8000 steps.
# PART=units, fixed_scan, text_flasher, follow_flags, follow_values, follow_timing, follow_extra or a follow2_* part writes a part on memory of its own; fixed_scan runs with fixed scan on, at 100 ms.
# CASES=group/id,group,... (or @file) writes only those cases, every rung in main.
conformance-export:
	uv run python -m tests.conformance.suite $(if $(PART),--part $(PART)) $(if $(CASES),--cases $(CASES)) $(or $(OUT),build/conformance$(if $(PART),-$(PART))$(if $(CASES),-cases))

# Regenerate src/plc/ in the conformance ClickNick workspace, for Preview Changes.
conformance-workspace:
	uv run python -m tests.conformance.suite $(if $(PART),--part $(PART)) --workspace $(or $(WORKSPACE),tests/conformance/workspace)

# Render the codegen's output for a fixed corpus (devtools/render_corpus.py), then compare two
# renders.  A pure refactor of the codegen compares equal.  BASE=ref renders that commit's tree
# with this branch's corpus scripts.
#   make render-corpus OUT=build/rc-base BASE=origin/main
#   make render-corpus OUT=build/rc-head
#   make render-compare A=build/rc-base B=build/rc-head
render-corpus:
	uv run python devtools/render_corpus.py render $(or $(OUT),build/render-corpus) $(if $(ONLY),--only $(ONLY)) $(if $(BASE),--base $(BASE))

render-compare:
	uv run python devtools/render_corpus.py compare $(A) $(B)

watch-pilot-burner:
	uv run python devtools/watch_pilot_decisions.py --target y_BurnerLoop --no-avoid --max-scans 20000 --wall-budget 240 --stall-budget 30 --output-budget 30 --memory-budget-mb 4096

watch-pilot-completed:
	uv run python devtools/watch_pilot_decisions.py --target Sts_State_Completed --avoid Cmd_State_Complete --max-scans 40000 --wall-budget 240 --stall-budget 30 --output-budget 30 --memory-budget-mb 4096

test-hypothesis:
	uv run pytest -m hypothesis

test-integration:
	uv run pytest -m integration

test-fuzz:
	uv run pytest tests/fuzz/ -n $(PYTEST_WORKERS) --dist=load --max-worker-restart=0

# The capped profile the merge queue runs (tests/fuzz/conftest.py).
test-fuzz-gate:
	FUZZ_PROFILE=gate uv run pytest tests/fuzz/ -n $(PYTEST_WORKERS) --dist=load --max-worker-restart=0

verify: lint test test-conformance test-hypothesis docs-check

upgrade:
	uv lock --upgrade
	uv sync --locked --all-extras --dev

build:
	uv build

# Improved Windows detection
ifeq ($(OS),Windows_NT)
    WINDOWS := 1
else
    ifeq ($(shell uname -s),Windows)
        WINDOWS := 1
    else
        WINDOWS := 0
    endif
endif

ifeq ($(WINDOWS),1)
	# Windows commands
	RM = powershell -Command "Remove-Item -Recurse -Force"
	RM_SITE = powershell -NoProfile -Command "if (Test-Path 'site') { Remove-Item -Recurse -Force 'site' }"
	FIND_PYCACHE = powershell -Command "Get-ChildItem -Path . -Filter '__pycache__' -Recurse -Directory | Remove-Item -Recurse -Force"
	RM_GENERATED_DOCS = powershell -NoProfile -Command "if (Test-Path '.docs-build') { Remove-Item -Recurse -Force '.docs-build' }; if (Test-Path 'generated-docs') { Remove-Item -Recurse -Force 'generated-docs' }; if (Test-Path 'docs/reference') { Remove-Item -Recurse -Force 'docs/reference' }; if (Test-Path 'docs/llms.txt') { Remove-Item -Force 'docs/llms.txt' }; if (Test-Path 'docs/__pycache__') { Remove-Item -Recurse -Force 'docs/__pycache__' }"
else
    # Unix commands
    RM = rm -rf
    RM_SITE = rm -rf site/
    FIND_PYCACHE = find . -type d -name "__pycache__" -exec rm -rf {} +
	RM_GENERATED_DOCS = rm -rf .docs-build/ generated-docs/ docs/reference/ docs/llms.txt docs/__pycache__/
endif

docs-generate:
	uv run --group docs python devtools/stage_docs.py docs generated-docs

docs-serve:
	uv run --group docs python devtools/serve_docs.py --dev-addr $(DOCS_ADDR)

docs-clean:
	$(RM_SITE)
	$(RM_GENERATED_DOCS)

docs-generated-clean:
	$(RM_GENERATED_DOCS)

docs-build: docs-clean
	uv run --group docs python devtools/stage_docs.py docs generated-docs
	uv run --group docs zensical build --clean --strict
	uv run --group docs python devtools/finalize_docs_site.py generated-docs site

docs-check: docs-build
	uv run --group docs python devtools/check_docs_site.py generated-docs site
	uv run --group docs python .github/scripts/check_public_site.py site

clean:
	$(RM) dist/
	$(RM) *.egg-info/
	$(RM) .pytest_cache/
	$(RM) .mypy_cache/
	$(RM) .venv/
	$(FIND_PYCACHE)
