CPP_SOURCES := src/gwframe/_core.cpp
CLANG_FORMAT ?= clang-format
CLANG_TIDY ?= clang-tidy
TIDY_BUILD_DIR ?= build-tidy

.PHONY: help
help :
	@echo
	@echo 'Commands:'
	@echo
	@echo '  make test                  run tests'
	@echo '  make lint                  run linters (Python + C++)'
	@echo '  make lint-cpp              check C++ formatting without modifying files'
	@echo '  make format                run code formatter (Python + C++)'
	@echo '  make format-cpp            run clang-format on C++ sources'
	@echo '  make check                 run static analysis (Python + C++)'
	@echo '  make check-cpp             run clang-tidy on C++ sources'
	@echo

.PHONY: test
test :
	GWFRAME_TEST_DATA_DIR=src/gwframe/tests/data python -m pytest --pyargs gwframe.tests --cov=gwframe --cov-report=term-missing -v

.PHONY: lint
lint : lint-cpp
	ruff check src

.PHONY: lint-cpp
lint-cpp :
	$(CLANG_FORMAT) --dry-run --Werror $(CPP_SOURCES)

.PHONY: format
format : format-cpp
	ruff format src
	ruff check --fix src

.PHONY: format-cpp
format-cpp :
	$(CLANG_FORMAT) -i $(CPP_SOURCES)

.PHONY: check
check : check-cpp
	mypy src

# clang-tidy needs a compile_commands.json from a configured CMake build.
# Configure once into TIDY_BUILD_DIR; subsequent runs reuse it.
$(TIDY_BUILD_DIR)/compile_commands.json :
	cmake -B $(TIDY_BUILD_DIR) -S . -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_CXX_COMPILER=clang++

.PHONY: check-cpp
check-cpp : $(TIDY_BUILD_DIR)/compile_commands.json
	$(CLANG_TIDY) -p $(TIDY_BUILD_DIR) $(CPP_SOURCES)

.PHONY: all
all : format lint check test
