-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakefile
More file actions
226 lines (185 loc) · 8.91 KB
/
Copy pathmakefile
File metadata and controls
226 lines (185 loc) · 8.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# --- Toolchain ----------------------------------------------------------------
CC := clang
AR := ar
CLANG_FORMAT ?= clang-format
# --- Flags --------------------------------------------------------------------
CPPFLAGS := -Iinclude -Iinclude/cfgpack -Ithird_party/lz4 -Ithird_party/heatshrink -Ithird_party/littlefs
CFLAGS := -Wall -Wextra -std=c99 -Os -DCFGPACK_LZ4 -DCFGPACK_HEATSHRINK -DCFGPACK_LITTLEFS
CFLAGS_HOSTED := $(CFLAGS) -DCFGPACK_HOSTED
LDFLAGS :=
LDLIBS :=
# -MJ (emit compile_commands.json fragment) is a clang-only flag. Detect the
# compiler once and gate it so `make CC=gcc` works.
ifneq (,$(findstring clang,$(shell $(CC) --version 2>/dev/null | head -1)))
MJ_FLAG = -MJ $(JSON)/$(@F).json
else
MJ_FLAG =
endif
# --- Directories --------------------------------------------------------------
BUILD := build
OUT := $(BUILD)/out
OBJ := $(BUILD)/obj
JSON := $(BUILD)/json
# --- Documentation ------------------------------------------------------------
DOCS_VENV := docs/.venv
DOCS_PIP := $(DOCS_VENV)/bin/pip
DOCS_SPHINX := $(DOCS_VENV)/bin/sphinx-build
# --- Outputs ------------------------------------------------------------------
LIB := $(OUT)/libcfgpack.a
# --- Sources ------------------------------------------------------------------
# Core library (excludes io_file.c for embedded use)
CORESRC := src/core.c \
src/crc32.c \
src/decompress.c \
src/io.c \
src/io_littlefs.c \
src/msgpack.c \
src/schema_parser.c \
src/tokens.c \
src/wbuf.c \
third_party/lz4/lz4.c \
third_party/heatshrink/heatshrink_decoder.c \
third_party/littlefs/lfs.c \
third_party/littlefs/lfs_util.c
# File I/O wrapper (optional, for desktop/POSIX)
IOFILESRC := src/io_file.c
# Compression tool
COMPRESS_TOOL := $(OUT)/cfgpack-compress
COMPRESS_SRC := tools/cfgpack-compress.c
COMPRESS_DEPS := third_party/lz4/lz4.c third_party/heatshrink/heatshrink_encoder.c
# Schema-pack tool (JSON/.map -> msgpack binary)
SCHEMA_PACK_TOOL := $(OUT)/cfgpack-schema-pack
SCHEMA_PACK_SRC := tools/cfgpack-schema-pack.c
# Schema-validate tool
SCHEMA_VALIDATE_TOOL := $(OUT)/cfgpack-schema-validate
SCHEMA_VALIDATE_SRC := tools/cfgpack-schema-validate.c
# Encoder libraries needed for tests (to generate compressed test data)
ENCODER_DEPS := third_party/heatshrink/heatshrink_encoder.c
# Test sources
TESTSRC := tests/basic.c \
tests/core_edge.c \
tests/coverage.c \
tests/decompress.c \
tests/io_edge.c \
tests/io_littlefs.c \
tests/json_edge.c \
tests/json_remap.c \
tests/measure.c \
tests/msgpack.c \
tests/msgpack_decode.c \
tests/msgpack_schema.c \
tests/null_args.c \
tests/parser.c \
tests/parser_bounds.c \
tests/runtime.c \
tests/test.c
# All project sources for formatting
FORMAT_FILES := $(shell find src include tests examples tools -name '*.c' -o -name '*.h' | grep -v third_party)
# --- Objects / Dependencies ---------------------------------------------------
COREOBJ := $(CORESRC:%.c=$(OBJ)/%.o)
IOFILEOBJ := $(IOFILESRC:%.c=$(OBJ)/%.o)
ENCODEROBJ := $(ENCODER_DEPS:%.c=$(OBJ)/%.o)
OBJECTS := $(COREOBJ) $(IOFILEOBJ) $(ENCODEROBJ)
TESTBINS := $(filter-out $(OUT)/test,$(TESTSRC:tests/%.c=$(OUT)/%))
TESTCOMMON := $(OBJ)/tests/test.o
DEPS := $(OBJECTS:.o=.d) $(TESTSRC:%.c=$(OBJ)/%.d)
# --- Vpath / Default goal -----------------------------------------------------
vpath %.c src tests
.DEFAULT_GOAL := all
# --- Build rules --------------------------------------------------------------
all: $(LIB) ## Build static library (core only, no file I/O)
# Core library without io_file.c
$(LIB): $(COREOBJ)
@mkdir -p $(OUT)
@echo "AR $(LIB)"
@$(AR) rcs $(LIB) $(COREOBJ)
# Generic object compilation
$(OBJ)/%.o: %.c
@mkdir -p $(@D) $(JSON)
@echo "CC $<"
@$(CC) $(CPPFLAGS) $(CFLAGS) -MMD -MP $(MJ_FLAG) -c $< -o $@
# Test objects need CFLAGS_HOSTED for printf support
$(OBJ)/tests/%.o: tests/%.c
@mkdir -p $(@D) $(JSON)
@echo "CC (hosted) $<"
@$(CC) $(CPPFLAGS) $(CFLAGS_HOSTED) -MMD -MP $(MJ_FLAG) -c $< -o $@
# io_file.c needs CFLAGS_HOSTED for stdio
$(OBJ)/src/io_file.o: src/io_file.c
@mkdir -p $(@D) $(JSON)
@echo "CC (hosted) $<"
@$(CC) $(CPPFLAGS) $(CFLAGS_HOSTED) -MMD -MP $(MJ_FLAG) -c $< -o $@
# --- Test targets -------------------------------------------------------------
tests: $(TESTBINS) ## Build all test binaries
# Tests link against core lib + io_file.o + encoder (for decompression tests)
$(OUT)/%: $(OBJ)/tests/%.o $(TESTCOMMON) $(LIB) $(IOFILEOBJ) $(ENCODEROBJ)
@mkdir -p $(OUT)
@echo "LD $@"
@$(CC) $(LDFLAGS) -o $@ $< $(TESTCOMMON) $(IOFILEOBJ) $(ENCODEROBJ) $(LIB) $(LDLIBS)
# --- Tool targets -------------------------------------------------------------
tools: $(COMPRESS_TOOL) $(SCHEMA_PACK_TOOL) $(SCHEMA_VALIDATE_TOOL) ## Build all tools
$(COMPRESS_TOOL): $(COMPRESS_SRC) $(COMPRESS_DEPS)
@mkdir -p $(OUT)
@echo "CC $(COMPRESS_TOOL)"
@$(CC) $(CFLAGS_HOSTED) -Ithird_party/lz4 -Ithird_party/heatshrink -o $@ $(COMPRESS_SRC) $(COMPRESS_DEPS)
$(SCHEMA_PACK_TOOL): $(SCHEMA_PACK_SRC) $(LIB) $(IOFILEOBJ)
@mkdir -p $(OUT)
@echo "CC $(SCHEMA_PACK_TOOL)"
@$(CC) $(CPPFLAGS) $(CFLAGS_HOSTED) -o $@ $(SCHEMA_PACK_SRC) $(IOFILEOBJ) $(LIB)
$(SCHEMA_VALIDATE_TOOL): $(SCHEMA_VALIDATE_SRC) $(LIB) $(IOFILEOBJ)
@mkdir -p $(OUT)
@echo "CC $(SCHEMA_VALIDATE_TOOL)"
@$(CC) $(CPPFLAGS) $(CFLAGS_HOSTED) -o $@ $(SCHEMA_VALIDATE_SRC) $(IOFILEOBJ) $(LIB)
# --- Documentation target -----------------------------------------------------
docs: ## Generate Sphinx documentation
@mkdir -p $(BUILD)/docs/doxygen
@test -d $(DOCS_VENV) || python3 -m venv $(DOCS_VENV)
@$(DOCS_PIP) install -q -r docs/requirements.txt
@doxygen Doxyfile
@$(DOCS_SPHINX) -q -b html docs $(BUILD)/docs/html
@echo "Documentation: $(BUILD)/docs/html/index.html"
# --- Utility targets ----------------------------------------------------------
compile_commands: $(OBJECTS) ## Generate compile_commands.json from dep JSON
@mkdir -p $(OUT)
@cat $(JSON)/* > $(JSON)/temp.json
@sed -e '1s/^/[\n/' -e '$$s/,$$/\n]/' $(JSON)/temp.json > compile_commands.json
@rm $(JSON)/temp.json
SAN_FLAGS := -fsanitize=address,undefined -fno-sanitize-recover=undefined -fno-omit-frame-pointer
test-asan: clean ## Rebuild tests with ASan+UBSan and run the full test suite
@$(MAKE) tests \
CFLAGS="-Wall -Wextra -std=c99 -O1 -g -DCFGPACK_LZ4 -DCFGPACK_HEATSHRINK -DCFGPACK_LITTLEFS $(SAN_FLAGS)" \
LDFLAGS="$(SAN_FLAGS)"
@scripts/run-tests.sh
COV_FLAGS := -fprofile-instr-generate -fcoverage-mapping
coverage: clean ## Rebuild with LLVM coverage, run tests, and generate report
@$(MAKE) tests \
CFLAGS="-Wall -Wextra -std=c99 -O0 -g -DCFGPACK_LZ4 -DCFGPACK_HEATSHRINK -DCFGPACK_LITTLEFS $(COV_FLAGS)" \
LDFLAGS="$(COV_FLAGS)"
@scripts/run-coverage.sh
stack-usage-O0: clean ## Build with -fstack-usage at -O0 and report per-function stack sizes
@$(MAKE) all CFLAGS="-Wall -Wextra -std=c99 -DCFGPACK_LZ4 -DCFGPACK_HEATSHRINK -DCFGPACK_LITTLEFS -O0 -fstack-usage"
@echo "--- Stack usage at -O0 ---"
@find $(OBJ) -name '*.su' -exec cat {} + | sort -t$$'\t' -k2 -n -r
stack-usage-Os: clean ## Build with -fstack-usage at -Os and report per-function stack sizes
@$(MAKE) all CFLAGS="-Wall -Wextra -std=c99 -DCFGPACK_LZ4 -DCFGPACK_HEATSHRINK -DCFGPACK_LITTLEFS -Os -fstack-usage"
@echo "--- Stack usage at -Os ---"
@find $(OBJ) -name '*.su' -exec cat {} + | sort -t$$'\t' -k2 -n -r
format: ## Auto-format all source files with clang-format
$(CLANG_FORMAT) -i $(FORMAT_FILES)
format-check: ## Check formatting without modifying files
$(CLANG_FORMAT) --dry-run --Werror $(FORMAT_FILES)
clean: ## Remove build artifacts
-@$(RM) -rvf -- $(BUILD) compile_commands.json .cache
-@$(RM) -rf -- tests/fuzz/corpus_map/* tests/fuzz/corpus_json/* \
tests/fuzz/corpus_msgpack/* tests/fuzz/corpus_msgpack_mutator/* \
tests/fuzz/corpus_pagein/* \
tests/fuzz/corpus_decode/* tests/fuzz/build/*
clean-docs: ## Remove generated documentation
-@$(RM) -rf -- $(BUILD)/docs docs/.venv
help: ## List targets with descriptions
@awk 'BEGIN {FS = ":.*##"} /^[a-zA-Z0-9][^:]*:.*##/ {printf "%-16s %s\n", $$1, $$2}' $(MAKEFILE_LIST)
# --- Fuzz targets (delegated to tests/fuzz/Makefile) ---------------------------
fuzz: ## Build all fuzz harnesses (requires clang with libFuzzer)
@$(MAKE) -C tests/fuzz fuzz ROOT=$(CURDIR) BUILD=$(CURDIR)/$(BUILD) OUT=$(CURDIR)/$(OUT) CC=$(CC)
# --- Phony / Includes ---------------------------------------------------------
.PHONY: all tests clean clean-docs help docs tools format format-check compile_commands fuzz test-asan coverage stack-usage-O0 stack-usage-Os
-include $(DEPS)