-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakefile
More file actions
228 lines (190 loc) · 8 KB
/
makefile
File metadata and controls
228 lines (190 loc) · 8 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
227
228
BUILD ?= debug
CC ?= clang
# Detect the operating system
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Darwin)
# Platform-specific linker flags for dead code elimination
GC_SECTIONS_FLAG := -Wl,-dead_strip
# Use sed with -i '' for in-place editing on macOS
SED_INPLACE := sed -i ''
# macOS doesn't support static linking!
STATIC_FLAG :=
else
GC_SECTIONS_FLAG := -Wl,--gc-sections
SED_INPLACE := sed -i
STATIC_FLAG := -static
endif
# Architecture configuration
ARCH ?= native
ifeq ($(BUILD),release)
CFLAGS_BUILD := -O3 -DNDEBUG
LDFLAGS_BUILD := $(STATIC_FLAG) $(GC_SECTIONS_FLAG)
BUILD_SUFFIX :=
else
SANITIZER_FLAGS := -fsanitize=undefined -fsanitize=address -fno-omit-frame-pointer
CFLAGS_BUILD := -O1 -g3 $(SANITIZER_FLAGS)
LDFLAGS_BUILD := $(STATIC_FLAG) $(SANITIZER_FLAGS) $(GC_SECTIONS_FLAG)
BUILD_SUFFIX := -debug
endif
# -D_POSIX_C_SOURCE=200809L is for strnlen()
CFLAGS_COMMON := -std=c2x -march=$(ARCH) -D_POSIX_C_SOURCE=200809L \
-Wall -Wextra -Wpedantic -Wconversion \
-Wno-incompatible-pointer-types-discards-qualifiers \
-ffunction-sections -fdata-sections \
-MMD -MP
CFLAGS := $(CFLAGS_COMMON) $(CFLAGS_BUILD)
LDFLAGS := $(LDFLAGS_BUILD)
SRCEXT := c
SRCDIR := src
LIBDIR := lib
BUILDDIR := build/$(BUILD)
BINDIR := bin
TESTDIR := test
TESTTARGET := $(BINDIR)/run-tests
# External Library: Unity
UNITYRELTARBALL := v2.6.1.tar.gz
UNITYURL := https://github.com/ThrowTheSwitch/Unity/archive/refs/tags/${UNITYRELTARBALL}
UNITYDIR := $(LIBDIR)/Unity-2.6.1
UNITYOBJ := $(UNITYDIR)/src/unity.o
# Unity's header files are in the `$(UNITYDIR)/src` directory
INC := -I include -I $(UNITYDIR)/src
# I use this variable to filter out the entrypoint of the "runtime" executable
# when compiling for the "test" executable
ENTRYPOINTOBJ := server.o
TARGET := $(BINDIR)/server$(BUILD_SUFFIX)
TEST_TARGET := $(BINDIR)/run-tests
SOURCES := $(wildcard $(SRCDIR)/*.$(SRCEXT))
OBJECTS := $(patsubst $(SRCDIR)/%,$(BUILDDIR)/%,$(SOURCES:.$(SRCEXT)=.o))
TESTSOURCES := $(wildcard $(TESTDIR)/*.$(SRCEXT))
TESTOBJECTS := $(patsubst $(TESTDIR)/%,$(BUILDDIR)/%,$(TESTSOURCES:.$(SRCEXT)=.o))
# Dependency files generated by -MMD flag
DEPS := $(OBJECTS:.o=.d)
# JSON Compilation Database file
JSON_DB := compile_commands.json
JSON_PARTS := $(OBJECTS:=.json) $(TESTOBJECTS:=.json)
JSON_FRAGMENTS := $(OBJECTS:.o=.o.json) $(TESTOBJECTS:.o=.o.json)
.PHONY: all release debug test clean-libs clean-comp-db clean check-cppcheck check-infer check-csa help
all: $(TARGET) $(JSON_DB)
release:
$(MAKE) BUILD=release
debug:
$(MAKE) BUILD=debug
$(TARGET): $(OBJECTS)
@echo "Linking..."
@mkdir -p $(BINDIR)
$(CC) $(LDFLAGS) $^ -o $(TARGET)
# server.o contains the entrypoint of the non-test code, filter it out so there aren't two entrypoints
test: $(UNITYOBJ) $(TESTOBJECTS) $(filter-out $(BUILDDIR)/$(ENTRYPOINTOBJ),$(OBJECTS))
@echo "Linking test object files...";
@mkdir -p $(BINDIR)
$(CC) $(LDFLAGS) $^ -o $(TESTTARGET)
$(BUILDDIR)/%.o $(BUILDDIR)/%.o.json: $(SRCDIR)/%.$(SRCEXT)
@echo "Building object files...";
@mkdir -p $(BUILDDIR)
$(CC) $(CFLAGS) $(INC) -c -o $@ $< -MJ $@.json
$(JSON_DB): $(OBJECTS) $(TESTOBJECTS)
@echo "Creating JSON Compilation Database..."
@echo '[' > $@
@cat $(JSON_FRAGMENTS) >> $@
@$(SED_INPLACE) '$$ s/,$$//' $@
@echo ']' >> $@
$(BUILDDIR)/%.o $(BUILDDIR)/%.o.json: $(TESTDIR)/%.$(SRCEXT) | $(UNITYDIR)
@echo "Building test object files...";
@mkdir -p $(BUILDDIR)
$(CC) $(CFLAGS) $(INC) -c -o $@ $< -MJ $@.json
$(UNITYOBJ): $(UNITYDIR)/src/unity.$(SRCEXT) | $(UNITYDIR)
@echo "Building Unity lib object files...";
$(CC) $(CFLAGS) $(INC) -c -o $@ $(UNITYDIR)/src/unity.$(SRCEXT)
$(UNITYDIR):
@echo "Downloading dependency: Unity test framework..."
@mkdir -p $(LIBDIR)
wget --directory-prefix=$(LIBDIR) $(UNITYURL)
@tar --gunzip --extract --file $(LIBDIR)/$(UNITYRELTARBALL) --directory $(LIBDIR)/
@$(RM) -rf $(UNITYDIR)/.git/
@$(RM) $(LIBDIR)/$(UNITYRELTARBALL)
clean-libs:
@echo "Removing library files..."
$(RM) -rf $(UNITYDIR)
clean-comp-db:
@echo "Cleaning JSON Compilation Database..."
$(RM) -rf $(JSON_DB)
clean:
@echo "Cleaning...";
$(RM) -rf build/* $(BINDIR)/* infer-out/
# --suppress=missingIncludeSystem is needed if cppcheck cannot find the standard headers on your system
check-cppcheck:
cppcheck . --verbose \
--showtime=summary \
-j 4 \
-I include/ \
-i lib/ \
--enable=all \
--suppress=missingIncludeSystem
check-infer:
$(MAKE) clean
infer run --cost \
--bufferoverrun \
--loop-hoisting \
--procedures \
--procedures-attributes \
--procedures-name \
--procedures-summary \
--pulse \
--quandary \
--source-files \
--topl \
--no-cxx \
--html \
--write-html \
--issues-tests issues.txt \
--cost-issues-tests costs.txt \
-- make $(TARGET)
$(MAKE) clean
# Note: Z3 solver is required for crosscheck-with-z3
check-csa:
$(MAKE) clean
scan-build -analyze-headers \
-maxloop 30 \
-analyzer-config mode=deep \
-analyzer-config exploration_strategy=dfs \
-analyzer-config max-inlinable-size=500 \
-analyzer-config max-nodes=750000 \
-analyzer-config track-conditions=true \
-analyzer-config track-conditions-debug=true \
-analyzer-config eagerly-assume=false \
-analyzer-config graph-trim-interval=10 \
-analyzer-config unroll-loops=true \
-analyzer-config widen-loops=true \
-analyzer-config ipa=dynamic-bifurcate \
-analyzer-config ipa-always-inline-size=3 \
-analyzer-config aggressive-binary-operation-simplification=true \
-analyzer-config crosscheck-with-z3=true \
-analyzer-config exploration-strategy=unexplored_first_queue \
-analyzer-config notes-as-events=true \
--force-analyze-debug-code \
-enable-checker alpha \
-enable-checker core \
-enable-checker deadcode \
-enable-checker nullability \
-enable-checker optin \
-enable-checker security \
-enable-checker unix \
-enable-checker alpha.clone \
-enable-checker alpha.core \
-enable-checker alpha.deadcode \
-enable-checker alpha.security \
-enable-checker alpha.unix \
$(MAKE)
help:
@echo "Available targets:"
@echo " release Build optimized version"
@echo " debug Build with debug info"
@echo " test Build tests"
@echo " clean-libs Remove library files"
@echo " clean-comp-db Remove JSON Compilation Database"
@echo " clean Remove all build artifacts (including binaries)"
@echo " check-cppcheck Run static analysis with Cppcheck"
@echo " check-infer Run static analysis with Infer"
@echo " check-csa Run static analysis with Clang Static Analyzer"
@echo " help Show this help message"
-include $(DEPS)