|
1 | 1 | .DEFAULT_GOAL := help |
2 | 2 |
|
3 | | -BUILD_DIR := build |
4 | 3 | CLANG_FORMAT := $(shell if command -v clang-format >/dev/null 2>&1; then echo clang-format; fi) |
5 | 4 | CLANG_TIDY := $(shell if command -v clang-tidy >/dev/null 2>&1; then echo clang-tidy; fi) |
6 | 5 | CPPCHECK := $(shell if command -v cppcheck >/dev/null 2>&1; then echo cppcheck; fi) |
7 | 6 | CLANG_TIDY_EXTRA_ARGS := $(shell if [ "$$(uname)" = "Darwin" ]; then echo "--extra-arg=--sysroot=$$(xcrun --show-sdk-path)"; fi) |
8 | 7 |
|
9 | 8 | SRC_FILES := src/*.c include/*.h |
10 | 9 | TEST_FILES := tests/unit/*.c |
11 | | -EXAMPLE_FILES := $(wildcard examples/*/*.c) |
12 | | -ALL_FILES := $(SRC_FILES) $(TEST_FILES) $(EXAMPLE_FILES) |
| 10 | +CMD_FILES := $(wildcard cmd/*/*.c cmd/*/*.h) |
| 11 | +ALL_FILES := $(SRC_FILES) $(TEST_FILES) $(CMD_FILES) |
13 | 12 |
|
14 | 13 | PRESET ?= default |
| 14 | +JOBS ?= 4 |
| 15 | +PROJECT_DIR := $(CURDIR) |
| 16 | +BUILD_ROOT := build |
| 17 | +PRESET_BUILD_DIR := $(if $(filter default,$(PRESET)),$(BUILD_ROOT),$(BUILD_ROOT)/$(PRESET)) |
15 | 18 |
|
16 | | -.PHONY: help install build clean clean-bin test format lint check check-all fix |
| 19 | +.PHONY: help configure build clean clean-bin clean-cache test format lint check check-all fix |
17 | 20 |
|
18 | 21 | help: ## Show available make targets |
19 | 22 | @echo "Usage: make <target>" |
20 | 23 | @echo "" |
21 | 24 | @echo "Targets:" |
22 | 25 | @awk 'BEGIN {FS = ":.*## "} /^[a-zA-Z_-]+:.*## / {printf " %-15s %s\n", $$1, $$2}' $(MAKEFILE_LIST) |
23 | 26 |
|
24 | | -configure: ## Configure cmake |
| 27 | +configure: ## Configure cmake (use PRESET=release for release mode) |
25 | 28 | cmake --preset $(PRESET) |
26 | 29 |
|
27 | | -build: ## Build the project |
28 | | - @test -d "$(BUILD_DIR)" || cmake --preset $(PRESET) |
29 | | - cmake --build --preset $(PRESET) |
| 30 | +build: ## Build the project (use PRESET=release for release mode) |
| 31 | + cmake --preset $(PRESET) |
| 32 | + cmake --build --preset $(PRESET) -j $(JOBS) |
| 33 | + |
| 34 | +clean: ## Remove build directories |
| 35 | + @test -n "$(PROJECT_DIR)" && [ "$(PROJECT_DIR)" != "/" ] |
| 36 | + rm -rf "$(PROJECT_DIR)/$(BUILD_ROOT)" |
30 | 37 |
|
31 | | -clean: ## Remove build directory |
32 | | - @test -n "$(CURDIR)" && [ "$(CURDIR)" != "/" ] |
33 | | - rm -rf "$(CURDIR)/$(BUILD_DIR)" |
| 38 | +clean-bin: ## Remove built binaries for the selected preset |
| 39 | + @test -n "$(PROJECT_DIR)" && [ "$(PROJECT_DIR)" != "/" ] |
| 40 | + @test -d "$(PROJECT_DIR)/$(PRESET_BUILD_DIR)/cmd/bin" || exit 0 |
| 41 | + find "$(PROJECT_DIR)/$(PRESET_BUILD_DIR)/cmd/bin" -mindepth 1 -delete |
34 | 42 |
|
35 | | -clean-bin: ## Remove built binaries from the build directory |
36 | | - @test -n "$(CURDIR)" && [ "$(CURDIR)" != "/" ] |
37 | | - @test -d "$(CURDIR)/$(BUILD_DIR)/bin" || exit 0 |
38 | | - find "$(CURDIR)/$(BUILD_DIR)/bin" -mindepth 1 -delete |
| 43 | +clean-cache: ## Remove CMake cache for the selected preset |
| 44 | + @test -n "$(PROJECT_DIR)" && [ "$(PROJECT_DIR)" != "/" ] |
| 45 | + rm -f "$(PROJECT_DIR)/$(PRESET_BUILD_DIR)/CMakeCache.txt" |
39 | 46 |
|
40 | 47 | test: ## Run tests |
41 | | - ctest --preset default |
| 48 | + @test -f "$(PROJECT_DIR)/$(PRESET_BUILD_DIR)/CMakeCache.txt" || cmake --preset $(PRESET) |
| 49 | + cmake --build --preset $(PRESET) -j $(JOBS) |
| 50 | + ctest --preset $(PRESET) |
42 | 51 |
|
43 | 52 | format: ## Check code formatting |
44 | 53 | @test -n "$(CLANG_FORMAT)" || { echo "error: clang-format not found"; exit 1; } |
45 | 54 | $(CLANG_FORMAT) --dry-run --Werror $(ALL_FILES) --verbose |
46 | 55 |
|
47 | 56 | lint: ## Check code linting |
48 | 57 | @test -n "$(CLANG_TIDY)" || { echo "error: clang-tidy not found"; exit 1; } |
49 | | - $(CLANG_TIDY) -p $(BUILD_DIR) $(CLANG_TIDY_EXTRA_ARGS) \ |
50 | | - --header-filter="^$(CURDIR)/(src|include|tests)/" src/*.c tests/unit/*.c |
| 58 | + @test -f "$(PRESET_BUILD_DIR)/compile_commands.json" || cmake --preset default |
| 59 | + $(CLANG_TIDY) --config-file=$(PROJECT_DIR)/.clang-tidy -p $(PRESET_BUILD_DIR) $(CLANG_TIDY_EXTRA_ARGS) \ |
| 60 | + --header-filter="^$(PROJECT_DIR)/(src|include|tests)/" src/*.c tests/unit/*.c |
51 | 61 |
|
52 | 62 | check: ## Static analysis |
53 | 63 | @test -n "$(CPPCHECK)" || { echo "error: cppcheck not found"; exit 1; } |
| 64 | + @test -f "$(PRESET_BUILD_DIR)/compile_commands.json" || cmake --preset default |
54 | 65 | $(CPPCHECK) --enable=warning,style,performance,portability --error-exitcode=1 \ |
55 | | - --check-level=exhaustive --project=$(BUILD_DIR)/compile_commands.json \ |
56 | | - --suppress=missingIncludeSystem -i$(BUILD_DIR) |
| 66 | + --check-level=exhaustive --project=$(PRESET_BUILD_DIR)/compile_commands.json \ |
| 67 | + --suppress=missingIncludeSystem -i$(PRESET_BUILD_DIR) |
57 | 68 |
|
58 | 69 | check-all: test format lint check ## Run all checks |
59 | 70 |
|
|
0 commit comments