CXX        := g++
BUILD_DIR  := build
NPROC := $(shell nproc)

LINT_SRC := $(shell find src include -name '*.hpp' -o -name '*.cpp')
FORMAT_SRC := $(shell find src test -name '*.hpp' -o -name '*.cpp')

.PHONY: build test clean format lint

# Configure + build (no test run)
build:
	cmake -S . -B $(BUILD_DIR) \
		-DCMAKE_CXX_COMPILER=$(CXX) \
		-DBUILD_TESTS=ON \
		-DCMAKE_CXX_FLAGS="-Wno-interference-size"
	cmake --build $(BUILD_DIR) -j$(NPROC)

# Build + run tests
test: build
	cd $(BUILD_DIR) && ctest --output-on-failure

# Remove build directory
clean:
	rm -rf $(BUILD_DIR)

# Format source files with clang-format
format:
	clang-format -i $(FORMAT_SRC)

# Run clang-tidy using compile_commands.json from build directory
lint:
	@test -f $(BUILD_DIR)/compile_commands.json || (echo "Run 'make build' first" && exit 1)
	echo $(LINT_SRC) | xargs -n1 -P$(NPROC) clang-tidy --quiet -p $(BUILD_DIR)
