-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
465 lines (385 loc) · 13.7 KB
/
Makefile
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
# How to install dependencies and build the app:
#
# GCC
# sudo apt -y install build-essential clang
#
# LLVM for sanitizer
# sudo apt -y install llvm
#
# Libraries
# sudo apt -y install libgoogle-perftools-dev
#
# Inatall stat and test tools
# sudo apt-get install cloc valgrind clang-tools cppcheck
#
# Install tools fot Unit tests CUnit
# sudo apt-get install libcunit1 libcunit1-doc libcunit1-dev
#
# make release # or
# make debug # or
# make prod[uction]
#
# Perf tool:
# sudo apt-get install linux-tools-common linux-tools-generic linux-tools-`uname -r`
# make perf # or
# make test
#
# Define our suffix list for quick compilation
.SUFFIXES: # Delete the default suffixes
.SUFFIXES: .c .o .h .d # Define our suffix list
#
# Compiler flags
#
CFLAGS += -pipe -std=c11
CFLAGS += -fbuiltin
# To pass #define inside a code:
# make DEFINES=-DWRITE_CSV=false memtest
CFLAGS += $(DEFINES)
# libc lib for static
LDFLAGS += -lrational -lsqlite -lsha512 -lpcre
EXE = precizer
# If define PRODUCTION is set, then a mode is activated
# in which some functions that exist only for additional
# research will not be run. By default, the mode is activated
# only for the production build.
#
# To activate in debug modes, excluding checks and disabling
# counters, just specify PRODUCTION= as a variable for make.
# For example:
PRODUCTION ?= PRODUCTION
STATIC = -static
SRC = src
STRIP = -s
# Flags for additional checks. Must have!
WFLAGS += -Wall -Wextra -Wpedantic -Wshadow
WFLAGS += -Wconversion -Wsign-conversion -Winit-self -Wunreachable-code -Wformat-y2k
WFLAGS += -Wformat-nonliteral -Wformat-security -Wmissing-include-dirs
WFLAGS += -Wswitch-default -Wtrigraphs -Wstrict-overflow=5
WFLAGS += -Wfloat-equal -Wundef -Wshadow
WFLAGS += -Wbad-function-cast -Wcast-qual -Wcast-align
WFLAGS += -Wwrite-strings
WFLAGS += -Winline
# If it is not clang, then these options are for gcc
ifneq ($(CC), clang)
GCCWFLAGS += -Wlogical-op
endif
# Arguments for tests
ARGS = --update tests/examples/diffs
# Config settings:
# The --no-print-directory option of make tells make not to print the message about entering and leaving the working directory.
MAKEFLAGS += --no-print-directory
CONFIG += ordered
# Build of dependent static library
SUBDIRS = libs
LIBPATH = libs/sqlite libs/rational libs/sha512 libs/pcre
# Additional include headers of external libraries
INCPATH=$(foreach d,$(LIBPATH),-I$d)
# Additional rpath
LIBSEARCHPATH = -Wl,-rpath,\$$ORIGIN
COSMOBIN = $(CURDIR)/libs/cosmocc/cosmocc/bin/
#
# Print of variables
#
# If you want to find out the value of a makefile variable, just:
#make print-VARIABLE
# and it will return:
#VARIABLE = the_value_of_the_variable
#
print-% : ; @echo $* = $($*)
#
# Project files
#
SRCS = $(wildcard $(SRC)/*.c)
HDRS = $(wildcard $(SRC)/*.h)
# Exclude a file
OBJS = $(SRCS:.c=.o)
PREPROC = $(SRCS:.c=.i) # Preproc files http://www.viva64.com/en/t/0076/
PREPROC += $(SRCS:.c=.i.h)
# Asm
ASM = $(SRCS:.c=.asm)
#
# Sanitize build settings
#
STZDIR = $(SRC)/sanitize
STZEXE = $(STZDIR)/$(EXE)
STZOBJS = $(addprefix $(STZDIR)/, $(notdir $(OBJS)))
STZDEP = $(STZOBJS:.o=.d)
STZLIBDIR = $(subst $(SRC)/,,$(STZDIR))
STZLIBPATH = $(foreach d,$(LIBPATH),-L$d/$(DBGLIBDIR))
STZINCPATH = $(foreach d,$(INCPATH),$d/$(DBGLIBDIR))
STZDYNLIB = $(foreach d,$(LIBPATH),-Wl,-rpath,\$$ORIGIN/$d/$(DBGLIBDIR),-rpath,\$$ORIGIN/../../$d/$(DBGLIBDIR))
STZCFLAGS += -fsanitize=address -g -O0 -DDEBUG
#
# Cosmopolitan settings
#
COSMODIR = $(SRC)/cosmo
COSMOEXE = $(COSMODIR)/$(EXE)
COSMOOBJS = $(addprefix $(COSMODIR)/, $(notdir $(OBJS)))
COSMODEP = $(COSMOOBJS:.o=.d)
COSMOLIBDIR = $(subst $(SRC)/,,$(COSMODIR))
COSMOLIBPATH = $(foreach d,$(LIBPATH),-L$d/$(DBGLIBDIR))
COSMOINCPATH = $(foreach d,$(INCPATH),$d/$(DBGLIBDIR))
COSMODYNLIB = $(foreach d,$(LIBPATH),-Wl,-rpath,\$$ORIGIN/$d/$(DBGLIBDIR),-rpath,\$$ORIGIN/../../$d/$(DBGLIBDIR))
#
# Unittest build settings
#
#
# Project files
#
UNITSRCS = basic.c
# Exclude a file
UNITOBJS = $(UNITSRCS:.c=.o)
UNITDIR = $(SRC)/unittests
UNITEXE = $(UNITDIR)/$(EXE)
UNITPATH = $(addprefix $(UNITDIR)/, $(UNITOBJS))
UNITDEP = $(UNITPATH:.o=.d)
UNITLIBDIR = $(subst $(SRC)/,,$(UNITDIR))
UNITLIBPATH = $(foreach d,$(LIBPATH),-L$d/$(UNITLIBDIR))
UNITINCPATH = $(foreach d,$(INCPATH),$d/$(UNITLIBDIR))
UNITDYNLIB = $(foreach d,$(LIBPATH),-Wl,-rpath,\$$ORIGIN/$d/$(DBGLIBDIR),-rpath,\$$ORIGIN/../../$d/$(DBGLIBDIR))
UNITCFLAGS += -DUNITTEST -g -O0 -DDEBUG
ifdef STATIC
UNITCFLAGS += -static-libasan
endif
#
# Debug build settings
#
DBGDIR = $(SRC)/debug
DBGEXE = $(DBGDIR)/$(EXE)
DBGOBJS = $(addprefix $(DBGDIR)/, $(notdir $(OBJS)))
DBGDEP = $(DBGOBJS:.o=.d)
DBGLIBDIR = $(subst $(SRC)/,,$(DBGDIR))
DBGLIBPATH = $(foreach d,$(LIBPATH),-L$d/$(DBGLIBDIR))
DBGINCPATH = $(foreach d,$(INCPATH),$d/$(DBGLIBDIR))
DBGDYNLIB = $(foreach d,$(LIBPATH),-Wl,-rpath,\$$ORIGIN/$d/$(DBGLIBDIR),-rpath,\$$ORIGIN/../../$d/$(DBGLIBDIR))
DBGCFLAGS += -g -ggdb -ggdb1 -ggdb2 -ggdb3 -O0 -DDEBUG
# Activation of the Gprof profiler.
# Works incorrectly with Valgrind.
# It is better to use Callgrind - the call graph format
# is supported by visualization tools like kcachegrind.
#DBGCFLAGS += -pg
#
# Production build settings
#
PRODDIR = $(SRC)/production
PRODEXE = $(PRODDIR)/$(EXE)
PRODOBJS = $(addprefix $(PRODDIR)/, $(notdir $(OBJS)))
PRODDEP = $(PRODOBJS:.o=.d)
PRODLIBDIR = $(subst $(SRC)/,,$(PRODDIR))
PRODLIBPATH = $(foreach d,$(LIBPATH),-L$d/$(RELLIBDIR))
PRODINCPATH = $(foreach d,$(INCPATH),$d/$(RELLIBDIR))
PRODDYNLIB = $(foreach d,$(LIBPATH),-Wl,-rpath,\$$ORIGIN/$d/$(RELLIBDIR),-rpath,\$$ORIGIN/../../$d/$(RELLIBDIR))
PRODCFLAGS = -O3 -funroll-loops -DNDEBUG -D$(PRODUCTION)
ifneq ($(CC), cosmocc)
PRODCFLAGS += -march=native
endif
# If static build, then add flags
ifdef STATIC
PRODLDFLAGS += -lc
endif
ifneq ($(CC), clang)
PRODCFLAGS += -flto
endif
#
# Release build settings
#
RELDIR = $(SRC)/release
RELEXE = $(RELDIR)/$(EXE)
RELOBJS = $(addprefix $(RELDIR)/, $(notdir $(OBJS)))
RELDEP = $(RELOBJS:.o=.d)
RELLIBDIR = $(subst $(SRC)/,,$(RELDIR))
RELLIBPATH = $(foreach d,$(LIBPATH),-L$d/$(RELLIBDIR))
RELINCPATH = $(foreach d,$(INCPATH),$d/$(RELLIBDIR))
RELDYNLIB = $(foreach d,$(LIBPATH),-Wl,-rpath,\$$ORIGIN/$d/$(RELLIBDIR),-rpath,\$$ORIGIN/../../$d/$(RELLIBDIR))
RELCFLAGS = -O3 -funroll-loops -DNDEBUG
ifneq ($(CC), cosmocc)
RELCFLAGS += -march=native
endif
# If static build, then add flags
ifdef STATIC
RELLDFLAGS += -lc
endif
# If it is not clang, then these options are for gcc
ifneq ($(CC), clang)
RELWFLAGS += -Wsuggest-attribute=const -Wsuggest-attribute=pure -Wsuggest-attribute=noreturn -Wsuggest-attribute=format -Wmissing-format-attribute
RELCFLAGS += -flto
endif
# https://stackoverflow.com/questions/17834582/run-make-in-each-subdirectory
TOPTARGETS := all
.PHONY: all clean debug prep release remake clang openmp one test sanitize banner $(SUBDIRS)
# Default build
all: $(SUBDIRS) release
$(SUBDIRS):
@$(MAKE) -s -C $@ $(MAKECMDGOALS)
# @$(MAKE) -C $@ all
# Clang
clang: CC = clang
clang: all
#
# Sanitize rules
#
sanitize: CC = clang
sanitize: $(SUBDIRS) $(STZEXE)
ASAN_OPTIONS=symbolize=1 ASAN_SYMBOLIZER_PATH=$(shell which llvm-symbolizer) $(STZEXE) $(ARGS)
$(STZEXE): $(STZOBJS)
@$(CC) $(LIBSEARCHPATH) $(CFLAGS) $(STZCFLAGS) $(STZLIBPATH) $(STZDYNLIB) $(WFLAGS) -o $(STZEXE) $^ $(LDFLAGS)
@echo "$@ linked."
-include $(STZDEP)
$(STZDIR)/%.o: $(SRC)/%.c
@mkdir -p $(STZDIR)
@$(CC) -MM $(INCPATH) $(STZINCPATH) $(CFLAGS) $(STZCFLAGS) $(WFLAGS) $< | sed '1s/^/$$\(STZDIR\)\//' > $(@D)/$(*F).d
@$(CC) -c $(INCPATH) $(STZINCPATH) $(CFLAGS) $(STZCFLAGS) $(WFLAGS) -o $@ $<
@echo $<" compiled."
#
# Cosmopolitan rules
#
#cosmo: $(SUBDIRS) $(COSMOEXE) banner
cosmo: $(COSMOEXE) banner
$(COSMOEXE): $(COSMOOBJS)
$(CC) $(LIBSEARCHPATH) $(COSMOLIBPATH) $(COSMODYNLIB) -o $(COSMOEXE) $^ $(LDFLAGS)
@echo "$@ linked."
-include $(COSMODEP)
$(COSMODIR)/%.o: $(SRC)/%.c
mkdir -p $(COSMODIR)
$(CC) -MM $(INCPATH) $(COSMOINCPATH) $< | sed '1s/^/$$\(COSMODIR\)\//' > $(@D)/$(*F).d
$(CC) -c $(INCPATH) $(COSMOINCPATH) -o $@ $<
@echo $<" compiled."
#
# Debug rules
#
debug: $(SUBDIRS) $(DBGEXE) banner
$(DBGEXE): $(DBGOBJS)
@$(CC) $(LIBSEARCHPATH) $(CFLAGS) $(DBGCFLAGS) $(STATIC) $(DBGLIBPATH) $(DBGDYNLIB) $(WFLAGS) $(GCCWFLAGS) -o $(DBGEXE) $^ $(LDFLAGS)
@echo "$@ linked."
@cp $@ ./
@echo "$@ moved to current directory"
-include $(DBGDEP)
$(DBGDIR)/%.o: $(SRC)/%.c
@mkdir -p $(DBGDIR)
@$(CC) -MM $(INCPATH) $(DBGINCPATH) $(CFLAGS) $(DBGCFLAGS) $(WFLAGS) $(GCCWFLAGS) $< | sed '1s/^/$$\(DBGDIR\)\//' > $(@D)/$(*F).d
@$(CC) -c $(INCPATH) $(DBGINCPATH) $(CFLAGS) $(DBGCFLAGS) $(WFLAGS) $(GCCWFLAGS) -o $@ $<
@echo $<" compiled."
#
# Production rules
#
prod: $(SUBDIRS) production banner
production: $(PRODEXE)
$(PRODEXE): $(PRODOBJS)
@$(CC) $(LIBSEARCHPATH) $(CFLAGS) $(WFLAGS) $(GCCWFLAGS) $(STATIC) $(STRIP) $(PRODLIBPATH) $(PRODDYNLIB) $(PRODLDFLAGS) -o $(PRODEXE) $^ $(LDFLAGS)
@echo "$@ linked."
@cp $@ ./
@echo "$@ moved to current directory"
-include $(PRODDEP)
$(PRODDIR)/%.o: $(SRC)/%.c
@mkdir -p $(PRODDIR)
@$(CC) -MM $(INCPATH) $(PRODINCPATH) $(CFLAGS) $(PRODCFLAGS) $< | sed '1s/^/$$\(PRODDIR\)\//' > $(@D)/$(*F).d
@$(CC) -c $(INCPATH) $(PRODINCPATH) $(CFLAGS) $(PRODCFLAGS) -o $@ $<
@echo $<" compiled."
#
# Release rules
#
release: $(SUBDIRS) $(RELEXE) banner
# Linking problem with "undefined reference to 'dlopen' "
# https://stackoverflow.com/a/11221504/7104681
$(RELEXE): $(RELOBJS)
@$(CC) $(LIBSEARCHPATH) $(CFLAGS) $(WFLAGS) $(GCCWFLAGS) $(STATIC) $(STRIP) $(RELLIBPATH) $(RELDYNLIB) $(RELLDFLAGS) -o $(RELEXE) $^ $(LDFLAGS)
@echo "$@ linked."
@cp $@ ./
@echo "$@ moved to current directory"
-include $(RELDEP)
$(RELDIR)/%.o: $(SRC)/%.c
@mkdir -p $(RELDIR)
@$(CC) -MM $(INCPATH) $(RELINCPATH) $(CFLAGS) $(WFLAGS) $(GCCWFLAGS) $(RELWFLAGS) $(RELCFLAGS) $< | sed '1s/^/$$\(RELDIR\)\//' > $(@D)/$(*F).d
@$(CC) -c $(INCPATH) $(RELINCPATH) $(CFLAGS) $(WFLAGS) $(GCCWFLAGS) $(RELWFLAGS) $(RELCFLAGS) -o $@ $<
@echo $<" compiled."
#
# GCC Static Analysis
#
gccanalyzer: WFLAGS += -fanalyzer -fanalyzer-call-summaries -fanalyzer-transitivity -fanalyzer-verbose-edges -fanalyzer-verbose-state-changes -fanalyzer-verbosity=3 -Wanalyzer-too-complex
#gccanalyzer: WFLAGS += -fdump-analyzer -fdump-analyzer-callgraph -fdump-analyzer-exploded-graph -fdump-analyzer-exploded-nodes -fdump-analyzer-exploded-nodes-2 -fdump-analyzer-exploded-nodes-3 -fdump-analyzer-feasibility -fdump-analyzer-json -fdump-analyzer-state-purge -fdump-analyzer-stderr -fdump-analyzer-supergraph
gccanalyzer: CC = gcc-12
gccanalyzer: debug
#
# Unittesting
#
unittest: $(SUBDIRS) $(UNITEXE)
$(UNITEXE): $(UNITOBJS)
@$(CC) $(LIBSEARCHPATH) $(CFLAGS) $(UNITCFLAGS) $(UNITLIBPATH) $(UNITDYNLIB) $(WFLAGS) $(GCCWFLAGS) -o $(UNITEXE) $^ $(LDFLAGS)
@echo "$@ linked."
-include $(UNITDEP)
$(UNITDIR)/%.o: $(SRC)/%.c
@mkdir -p $(UNITDIR)
@$(CC) -MM $(INCPATH) $(UNITINCPATH) $(CFLAGS) $(UNITCFLAGS) $(WFLAGS) $(GCCWFLAGS) $< | sed '1s/^/$$\(UNITDIR\)\//' > $(@D)/$(*F).d
@$(CC) -c $(INCPATH) $(UNITINCPATH) $(CFLAGS) $(UNITCFLAGS) $(WFLAGS) $(GCCWFLAGS) -o $@ $<
@echo $<" compiled."
# Optional preprocessor files
%.i:%.c clean-preproc
@$(CC) -E -C -o $@ $(INCPATH) $(RELINCPATH) $(CFLAGS) $<
# C-C++ Beautifier
# @bcpp -na $@ > [email protected]
@bcpp -na -s -i 4 $@ > [email protected]
@sed -i 's/[ \t]*\# [[:digit:]]\+ \".*//g' [email protected]
# @sed -i '/^ *$//d' [email protected]
# Optional Assembler files
%.asm:%.c clean-asm
@$(CC) -S -C $(INCPATH) $(RELINCPATH) $(CFLAGS) $(WFLAGS) $(GCCWFLAGS) $(RELWFLAGS) $(RELCFLAGS) $(RELLDFLAGS) -o $@ $(LDFLAGS) $<
#
# Other rules
#
remake: clean all
# Tests
test: sanitize clang-analyzer cachegrind callgrind massif cppcheck memtest gccanalyzer perf
cppcheck:
cppcheck --enable=all --platform=unix64 --std=c11 -q --force -i libs -i tests --inconclusive .
memtest: debug
valgrind -v --tool=memcheck --leak-check=full --leak-resolution=high --undef-value-errors=no --show-reachable=yes --num-callers=20 $(DBGDIR)/$(EXE) $(ARGS)
cachegrind: debug
valgrind --tool=cachegrind --branch-sim=yes $(DBGDIR)/$(EXE) $(ARGS)
callgrind: debug
valgrind --tool=callgrind --dump-instr=yes --collect-jumps=yes $(DBGDIR)/$(EXE) $(ARGS)
helgrind: debug
valgrind --tool=helgrind --read-var-info=yes --track-origins=yes --num-callers=20 $(DBGDIR)/$(EXE) $(ARGS)
massif: debug
valgrind --tool=massif --stacks=yes --num-callers=20 $(DBGDIR)/$(EXE) $(ARGS)
ms_print ./massif.out.*
clang-analyzer:
# Run clang static analyzer and view analysis results in a web browser when the build command completes
scan-build -V make debug
splint:
splint -I /usr/include/x86_64-linux-gnu +posixlib $(SRCS) $(INCPATH)
doc:
@doxygen Doxyfile
spellcheck:
@~/.cargo/bin/typos libs/sha512/ libs/rational/ src/ README.md README.ru.md TODO
gource:
gource --seconds-per-day 0.1 --auto-skip-seconds 1
#https://eax.me/c-cpp-profiling/
#https://perf.wiki.kernel.org/index.php/Main_Page
perf:
sudo perf stat $(DBGDIR)/$(EXE) $(ARGS)
# Statistic code info and count of lines
stat: cloc
cloc:
@cloc --exclude-dir=$(STZDIR),$(DBGDIR),$(PRODDIR),$(RELDIR),$(OMPDIR) ../
# Character | prevent threading with clean
cleanall: clean
@$(MAKE) -C $(SUBDIRS) clean
clean: | clean-preproc clean-asm
@rm -rf *.out.* doc $(STZDEP) $(DBGDEP) $(PRODDEP) $(RELDEP) \
$(DBGEXE) $(STZEXE) $(PRODEXE) $(RELEXE) \
$(STZOBJS) $(DBGOBJS) $(PRODOBJS) $(RELOBJS)
@test -d $(STZDIR) && rm -rf $(STZDIR) || true
@test -d $(DBGDIR) && rm -rf $(DBGDIR) || true
@test -d $(PRODDIR) && rm -rf $(PRODDIR) || true
@test -d $(RELDIR) && rm -rf $(RELDIR) || true
@test -f $(EXE) && rm -f $(EXE) || true
@echo $(EXE) cleared.
clean-preproc:
@rm -rf $(PREPROC)
clean-asm:
@rm -rf $(ASM)
banner:
@printf "Now some tests could be running:\n"
@printf "\033[1mStage 1. Adding:\033[0m\n./precizer --progress --database=database1.db tests/examples/diffs/diff1\n"
@printf "\033[1mStage 2. Adding:\033[0m\n./precizer --progress --database=database2.db tests/examples/diffs/diff2\n"
@printf "\033[1mFinal stage. Comparing:\033[0m\n./precizer --compare database1.db database2.db\n"