|
| 1 | +# Copyright (c) Brandon Pacewic |
| 2 | +# SPDX‑License‑Identifier: MIT |
| 3 | + |
| 4 | +BUILD_DIR ?= build |
| 5 | +CMAKE_GENERATOR ?= Unix Makefiles |
| 6 | +BUILD_TESTS ?= ON # pass BUILD_TESTS=OFF to skip |
| 7 | +BUILD_BENCH ?= OFF |
| 8 | +BUILD_TOOLS ?= OFF |
| 9 | +BUILD_TYPE ?= Debug # Release | RelWithDebInfo ... |
| 10 | +JOBS ?= $(shell nproc 2>/dev/null || sysctl -n hw.ncpu) |
| 11 | + |
| 12 | +CMAKE_FLAGS = \ |
| 13 | + -G "$(CMAKE_GENERATOR)" \ |
| 14 | + -DCMAKE_BUILD_TYPE=$(BUILD_TYPE) \ |
| 15 | + -DBUILD_TESTING=$(BUILD_TESTS) \ |
| 16 | + -DBUILD_BENCHMARKS=$(BUILD_BENCH) \ |
| 17 | + -DBUILD_TOOLS=$(BUILD_TOOLS) \ |
| 18 | + |
| 19 | +.PHONY: build test bench tools \ |
| 20 | + format clean distclean help |
| 21 | + |
| 22 | +configure: |
| 23 | + @mkdir -p "$(BUILD_DIR)" |
| 24 | + @cmake -S . -B "$(BUILD_DIR)" $(CMAKE_FLAGS) |
| 25 | + |
| 26 | +build: configure |
| 27 | + @cmake --build "$(BUILD_DIR)" -- -j$(JOBS) |
| 28 | + |
| 29 | +test: build |
| 30 | + @ctest --test-dir "$(BUILD_DIR)" --output-on-failure |
| 31 | + |
| 32 | +bench: BUILD_BENCH := ON |
| 33 | +bench: build |
| 34 | + @cmake --build "$(BUILD_DIR)" -j$(JOBS) |
| 35 | + |
| 36 | +tools: BUILD_TOOLS := ON |
| 37 | +tools: build |
| 38 | + |
| 39 | +format: |
| 40 | + @clang-format -i -style=file $(shell git ls-files '*.hpp' '*.h' '*.cpp') |
| 41 | + |
| 42 | +clean: |
| 43 | + @cmake --build "$(BUILD_DIR)" --target clean || true |
| 44 | + |
| 45 | +distclean: |
| 46 | + @rm -rf "$(BUILD_DIR)" |
| 47 | + |
| 48 | +help: |
| 49 | + @echo "Targets:" |
| 50 | + @echo " build - configure and build" |
| 51 | + @echo " test - run ctest (RUN_TESTS=ON)" |
| 52 | + @echo " bench - build & run benchmarks (BUILD_BENCH=ON)" |
| 53 | + @echo " tools - build project-defined tools (BUILD_TOOLS=ON)" |
| 54 | + @echo " format - run clang-format on all source files" |
| 55 | + @echo " clean - clean build artefacts" |
| 56 | + @echo " distclean - clean entire build dir" |
| 57 | + @echo "" |
| 58 | + @echo "Knobs (override with VAR=value):" |
| 59 | + @echo " BUILD_DIR, CMAKE_GENERATOR, BUILD_TYPE, JOBS" |
| 60 | + @echo " BUILD_TESTS, BUILD_BENCH, BUILD_TOOLS" |
| 61 | + |
| 62 | +# Default to help when running `make` without targets |
| 63 | +.DEFAULT_GOAL := help |
0 commit comments