ROOT := $(CURDIR)
LOCAL_PYTHON := $(ROOT)/tools/environments/python/bin/python
SPLAT := $(ROOT)/tools/environments/python/bin/splat
PYTHON ?= $(if $(wildcard $(LOCAL_PYTHON)),$(LOCAL_PYTHON),python3)
BOOTSTRAP_PYTHON ?= python3
USE_SYSTEM_MIPS_BINUTILS ?= 0

ifeq ($(USE_SYSTEM_MIPS_BINUTILS),1)
BUILD_BINUTILS_CHECK := tools/bootstrap/binutils_system.py --check
else
BUILD_BINUTILS_CHECK := tools/bootstrap/binutils.py --check
endif

export HOME := $(ROOT)/tmp/home
export TMPDIR := $(ROOT)/tmp
export XDG_CACHE_HOME := $(ROOT)/tmp/cache
export PIP_CACHE_DIR := $(ROOT)/tmp/pip-cache
export PYTHONPYCACHEPREFIX := $(ROOT)/tmp/pycache
export NPM_CONFIG_CACHE := $(ROOT)/tmp/npm-cache
export CARGO_HOME := $(ROOT)/tools/environments/cargo
export RUSTUP_HOME := $(ROOT)/tools/environments/rustup
export GOPATH := $(ROOT)/tools/environments/go
export GOMODCACHE := $(ROOT)/tools/environments/go/pkg/mod

.DEFAULT_GOAL := help

.PHONY: help workspace verify-target verify-inputs tools python-tools toolchain toolchain-system compiler compiler-281 compiler-281-prebuilt check-tools check-build-tools info extract map split split-incremental build build-incremental match match-incremental overlays verify-overlays check-metadata check-translation-unit-headers check-matching-source-contracts check-unmatched-contracts check-psyq-declarations check-psyq-signature-resolutions check-declaration-visibility build-overlays match-overlays inventory classify-functions candidates candidate-builds check-candidate-builds candidate-contract-hashes check-notes check-note-links review-deferred siblings adjacent-units external-attempts basic-types global-usage check-global-usage progress check-progress disc-files disc-layout verify-disc runtime-files verify-runtime-files check-pc audit clean

help:
	@printf '%s\n' \
		'Available targets:' \
		'  tools          Install pinned project tools beneath tools/' \
		'  check-tools    Verify pinned local project tools' \
		'  check-build-tools  Verify only tools required for a clean build' \
		'  info           Show the verified PS-X executable header' \
		'  extract        Extract the verified header and loaded payload' \
		'  map            Validate the top-level executable region map' \
		'  split          Split the executable into temporary analysis output' \
		'  build          Build the assembly/data PS-X executable baseline' \
		'  match          Build and compare the complete target executable' \
		'  match-incremental  Reuse validated split output and unchanged objects, then relink and match' \
		'  overlays       Extract verified runtime overlay module images' \
		'  verify-overlays  Verify extracted overlay images and metadata' \
		'  check-metadata Verify tracked manifests and CSV tables only' \
		'  check-translation-unit-headers  Reject foreign prototypes in built C sources' \
		'  check-matching-source-contracts  Reject pins, inline asm, and mixed -G matching C' \
		'  check-unmatched-contracts  Verify unmatched function/data declarations and exceptions' \
		'  check-psyq-signature-resolutions  Verify local Psy-Q signature conflict decisions' \
		'  check-declaration-visibility  Reject calls that compile only through an implicit declaration' \
		'  check-psyq-declarations  Require SDK declarations to come from src/psyq headers' \
		'  candidate-builds  Run the normal build and validate tracked source candidates' \
		'  check-candidate-builds  Verify tracked source-candidate metadata' \
		'  candidate-contract-hashes  Print current canonical candidate contracts' \
		'  build-overlays Build verified runtime overlay module images' \
		'  match-overlays Build and compare all configured overlay modules' \
		'  inventory      Update the tracked resident-function inventory' \
		'  classify-functions  Apply verified ownership classifications' \
		'  candidates     List smallest zero-attempt game functions' \
		'  review-deferred  List terminal histories for hypothesis review' \
		'  siblings       Find exact-C functions with similar instruction shapes' \
		'  adjacent-units  List sources adjacent in the image but still apart (#39)' \
		'  external-attempts  Validate external-reference/refinement attempts' \
		'  basic-types    Verify all C sources use src/types.h' \
		'  check-g32      Verify G32/CALL32 on stored guest pointers (src/port_ptr.h)' \
		'  global-usage   Regenerate tracked game-global usage reports' \
		'  check-global-usage  Verify tracked game-global usage reports' \
		'  progress       Update README and generate current progress metrics' \
		'  check-progress Verify that the README progress snapshot is current' \
		'  check-notes    Verify grouped translation-unit notes match the build config' \
		'  check-note-links  Verify local paths referenced from notes exist' \
		'  disc-files     Extract the tracked DATA files from the disc image' \
		'  disc-layout    Regenerate the tracked ISO9660 LBA manifest' \
		'  verify-disc    Verify BIN/CUE layout and extracted file contents' \
		'  runtime-files  Regenerate executable file-index/LBA metadata' \
		'  verify-runtime-files  Verify runtime file order against disc LBAs' \
		'  check-pc       Build and run native PC unit and deterministic smoke tests' \
		'  audit          Verify exact output, metadata, and repository policy' \
		'  clean          Remove known generated project output under tmp/' \
		'  verify-target  Validate only the SLUS executable needed to build' \
		'  verify-inputs  Validate the SLUS-01411 executable and DATA files' \
		'  workspace      Validate that commands are running from the project root'

workspace:
	@$(PYTHON) tools/project/workspace.py

verify-target: workspace
	@$(PYTHON) tools/project/verify_inputs.py --executable-only

verify-inputs: workspace
	@$(PYTHON) tools/project/verify_inputs.py

tools: python-tools toolchain compiler

python-tools: verify-target
	@$(BOOTSTRAP_PYTHON) tools/bootstrap/bootstrap.py

toolchain: verify-target
	@$(BOOTSTRAP_PYTHON) tools/bootstrap/binutils.py

toolchain-system: verify-target
	@$(BOOTSTRAP_PYTHON) tools/bootstrap/binutils_system.py

compiler: compiler-281

compiler-281: verify-target
	@$(BOOTSTRAP_PYTHON) tools/bootstrap/old_gcc.py

compiler-281-prebuilt: verify-target
	@$(BOOTSTRAP_PYTHON) tools/bootstrap/old_gcc_prebuilt.py

check-tools: workspace
	@$(PYTHON) tools/bootstrap/bootstrap.py --check
	@$(PYTHON) tools/bootstrap/binutils.py --check
	@$(PYTHON) tools/bootstrap/old_gcc.py --check

check-build-tools: workspace
	@$(PYTHON) tools/bootstrap/bootstrap.py --check
	@$(PYTHON) $(BUILD_BINUTILS_CHECK)
	@$(PYTHON) tools/bootstrap/old_gcc_prebuilt.py --check

info: verify-target
	@$(PYTHON) tools/project/psx_exe.py info

extract: verify-target
	@$(PYTHON) tools/project/psx_exe.py extract

map: verify-target
	@$(PYTHON) tools/project/validate_image_map.py

split: map check-build-tools
	@$(PYTHON) tools/project/clean.py generated splat
	@$(PYTHON) tools/project/generate_build_config.py
	@$(SPLAT) split tmp/generated/slus_01411.split.yaml

build: split
	@$(PYTHON) tools/project/clean.py project-build
	@$(PYTHON) tools/project/build_baseline.py
	@$(PYTHON) tools/project/candidate_builds.py

match: build
	@$(PYTHON) tools/project/match.py

overlays: workspace
	@$(PYTHON) tools/project/overlay_extract.py extract

verify-overlays: workspace
	@$(PYTHON) tools/project/overlay_extract.py verify

check-metadata:
	@$(PYTHON) tools/project/overlay_extract.py verify-metadata
	@$(PYTHON) tools/project/candidate_builds.py --check
	@$(PYTHON) tools/project/translation_unit_headers.py
	@$(PYTHON) tools/project/unmatched_contracts.py
	@$(PYTHON) tools/project/psyq_declaration_contracts.py
	@$(PYTHON) tools/project/c_type_definitions.py
	@$(PYTHON) tools/project/check_note_links.py
	@$(PYTHON) tools/project/psyq_signatures.py --check-resolutions

check-translation-unit-headers:
	@$(PYTHON) tools/project/translation_unit_headers.py

check-matching-source-contracts:
	@$(PYTHON) tools/project/matching_source_contracts.py

check-unmatched-contracts:
	@$(PYTHON) tools/project/unmatched_contracts.py

check-psyq-signature-resolutions:
	@$(PYTHON) tools/project/psyq_signatures.py --check-resolutions

check-psyq-declarations:
	@$(PYTHON) tools/project/psyq_declaration_contracts.py

check-declaration-visibility: check-build-tools
	@$(PYTHON) tools/project/check_declaration_visibility.py

build-overlays: overlays check-build-tools
	@$(PYTHON) tools/project/overlay_build.py build
	@$(PYTHON) tools/project/candidate_builds.py --overlays

match-overlays: build-overlays
	@$(PYTHON) tools/project/overlay_build.py verify

split-incremental: map check-build-tools
	@$(PYTHON) tools/project/split_incremental.py

build-incremental: split-incremental
	@$(PYTHON) tools/project/build_incremental.py
	@$(PYTHON) tools/project/candidate_builds.py

match-incremental: build-incremental
	@$(PYTHON) tools/project/match.py

inventory: split
	@$(PYTHON) tools/project/function_inventory.py

classify-functions: inventory
	@$(PYTHON) tools/project/classify_functions.py

candidates: workspace
	@$(PYTHON) tools/project/select_candidates.py $(CANDIDATE_ARGS)

candidate-builds: build

check-candidate-builds:
	@$(PYTHON) tools/project/candidate_builds.py --check

candidate-contract-hashes:
	@$(PYTHON) tools/project/candidate_builds.py --print-contract-hashes

review-deferred: workspace
	@$(PYTHON) tools/project/review_deferred.py $(REVIEW_DEFERRED_ARGS)

siblings: verify-inputs
	@$(PYTHON) tools/project/find_siblings.py $(SIBLING_ARGS)

adjacent-units: workspace
	@$(PYTHON) tools/project/adjacent_units.py --self-test
	@$(PYTHON) tools/project/adjacent_units.py $(ARGS)

external-attempts:
	@$(PYTHON) tools/project/record_external_attempt.py --check

basic-types:
	@$(PYTHON) tools/project/centralize_basic_types.py --check

.PHONY: check-g32
check-g32:
	@$(PYTHON) tools/project/check_g32.py --self-test
	@$(PYTHON) tools/project/check_g32.py --report

global-usage: split
	@$(PYTHON) tools/project/global_usage.py

check-global-usage: split
	@$(PYTHON) tools/project/global_usage.py --check

progress: split
	@$(PYTHON) tools/project/progress.py

check-progress: split
	@$(PYTHON) tools/project/progress.py --check

check-notes:
	@$(PYTHON) tools/project/check_notes.py --self-test
	@$(PYTHON) tools/project/check_notes.py

check-note-links:
	@$(PYTHON) tools/project/check_note_links.py

check-data-symbols:
	@$(PYTHON) tools/project/check_data_symbol_ownership.py --self-test
	@$(PYTHON) tools/project/check_data_symbol_ownership.py

check-data-symbols-objects:
	@$(PYTHON) tools/project/check_data_symbol_ownership.py --objects

disc-files: workspace
	@$(PYTHON) tools/project/disc_image.py extract $(FILES)

disc-layout: verify-inputs
	@$(PYTHON) tools/project/disc_image.py write

verify-disc: verify-inputs
	@$(PYTHON) tools/project/disc_image.py verify

runtime-files: verify-disc
	@$(PYTHON) tools/project/runtime_files.py write

verify-runtime-files: verify-disc
	@$(PYTHON) tools/project/runtime_files.py verify

check-pc:
	@./build-pc.sh
	@cmake -S . -B tmp/pc/cmake-test -DBUILD_TESTING=ON
	@cmake --build tmp/pc/cmake-test
	@$(PYTHON) tools/pc/smoke.py

audit: match verify-runtime-files
	@$(PYTHON) tools/project/function_inventory.py
	@$(PYTHON) tools/project/classify_functions.py
	@$(PYTHON) tools/project/centralize_basic_types.py --check
	@$(PYTHON) tools/project/translation_unit_headers.py
	@$(PYTHON) tools/project/matching_source_contracts.py
	@$(PYTHON) tools/project/unmatched_contracts.py
	@$(PYTHON) tools/project/check_declaration_visibility.py
	@$(PYTHON) tools/project/check_note_links.py
	@$(PYTHON) tools/project/audit_repository.py

clean: workspace
	@$(PYTHON) tools/project/clean.py extract generated splat project-build candidate-build incremental overlays reports
