CMAKE     ?= cmake
CTEST     ?= ctest
BUILD_DIR ?= build
BUILD_TYPE ?= Release
GENERATOR ?= Ninja

.PHONY: all configure build test bench format format-check tidy clean python-install python-test python-bench

all: build

configure:
	$(CMAKE) -S . -B $(BUILD_DIR) -G $(GENERATOR) -DCMAKE_BUILD_TYPE=$(BUILD_TYPE)

build: configure
	$(CMAKE) --build $(BUILD_DIR) -j

test: build
	$(CTEST) --test-dir $(BUILD_DIR) --output-on-failure

bench: build
	@$(BUILD_DIR)/benchmarks/gr_benchmark

# Python バインディング
python-install:
	pip install ".[test]"

python-test:
	pytest python/tests -q

python-bench:
	python3 python/benchmarks/compare_with_python.py

# サニタイザ付きのデバッグビルドでテストする
test-asan:
	$(CMAKE) -S . -B $(BUILD_DIR)-asan -G $(GENERATOR) -DCMAKE_BUILD_TYPE=Debug -DGAME_RESOLVER_SANITIZE=ON
	$(CMAKE) --build $(BUILD_DIR)-asan -j
	$(CTEST) --test-dir $(BUILD_DIR)-asan --output-on-failure

CLANG_FORMAT ?= clang-format
SOURCES = $(shell find include src tests examples benchmarks \( -name '*.hpp' -o -name '*.cpp' \))

format:
	@command -v $(CLANG_FORMAT) >/dev/null || { echo "clang-format がありません: brew install clang-format"; exit 1; }
	@$(CLANG_FORMAT) -i $(SOURCES)

format-check:
	@command -v $(CLANG_FORMAT) >/dev/null || { echo "clang-format がありません: brew install clang-format"; exit 1; }
	@$(CLANG_FORMAT) --dry-run --Werror $(SOURCES)

tidy: configure
	@find include src -name '*.hpp' -o -name '*.cpp' | xargs clang-tidy -p $(BUILD_DIR)

clean:
	rm -rf $(BUILD_DIR) $(BUILD_DIR)-asan
