-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
345 lines (279 loc) · 9.73 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
# brlib Makefile - GNU make only
#
# Copyright (C) 2021-2024 Bruno Raoult ("br")
# Licensed under the GNU General Public License v3.0 or later.
# Some rights reserved. See COPYING.
#
# You should have received a copy of the GNU General Public License along with this
# program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
#
# SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
#
SHELL := /bin/bash
CC := gcc
LD := ld
BEAR := bear
TOUCH := touch
RM := rm
MKDIR := mkdir -p
RMDIR := rmdir
SRCDIR := ./src
INCDIR := ./include
OBJDIR := ./obj
LIBDIR := ./lib
BINDIR := ./bin
DEPDIR := ./dep
TESTDIR := ./test
SRC := $(wildcard $(SRCDIR)/*.c) # brlib sources
SRC_FN := $(notdir $(SRC)) # source basename
OBJ := $(addprefix $(OBJDIR)/,$(SRC_FN:.c=.o))
LIB := br_$(shell uname -m) # library name
SLIB := $(addsuffix .a, $(LIBDIR)/lib$(LIB)) # static lib
DLIB := $(addsuffix .so, $(LIBDIR)/lib$(LIB)) # dynamic lib
DEP_FN := $(SRC_FN) $(LIBSRC_FN)
DEP := $(addprefix $(DEPDIR)/,$(DEP_FN:.c=.d))
TEST := $(wildcard $(TESTDIR)/*.c)
TEST_FN := $(notdir $(TEST))
BIN := $(addprefix $(BINDIR)/,$(TEST_FN:.c=))
CCLSCMDS := compile_commands.json
##################################### Check for compiler and requested build
BUILDS := release dev perf debug
# last compilation build
BUILDFILE := .lastbuild
lastbuild := $(file < $(BUILDFILE))
$(info brlib last:$(lastbuild))
# default to gcc
CC ?= cc
ifeq ($(CC),cc)
CC = gcc
endif
# if no build specified, use last one
ifeq ($(build),)
build := $(lastbuild)
endif
# if build is still undefined, set a default
ifeq ($(build),)
build := release
endif
$(info brlib build=$(build))
# check for valid build
ifeq ($(filter $(build),$(BUILDS)),)
$(error Error: Unknown build=`$(build)`. Possible builds are: $(BUILDS))
endif
# if new build, rewrite BUILDFILE
ifneq ($(build),$(lastbuild))
$(info New build:`$(build)` (was:$(lastbuild)))
$(file >$(BUILDFILE),$(build))
endif
##################################### pre-processor flags
override CPPFLAGS += -I $(INCDIR)
ifeq ($(BUILD),release)
CPPFLAGS += -DNDEBUG # assert (unused)
CPPFLAGS += -DBUG_ON=0 CPPFLAGS += -DWARN_ON=0 else # ifeq ($(BUILD),dev)
#CPPFLAGS += -DDEBUG #CPPFLAGS += -DDEBUG_DEBUG_C CPPFLAGS += -DDEBUG_DEBUG CPPFLAGS += -DDEBUG_POOL CPPFLAGS += -DBUG_ON=1 CPPFLAGS += -DWARN_ON=1 # warn_on in bug.h
endif
# remove extraneous spaces (due to spaces before comments)
CPPFLAGS := $(strip $(CPPFLAGS))
##################################### compiler flags
CFLAGS := -std=gnu11
CFLAGS += -Wall
CFLAGS += -Wextra
CFLAGS += -march=native
CFLAGS += -Wmissing-declarations
CFLAGS += -Wno-unused-result
# TODO: specific to dynamic
CFLAGS += -fPIC
### dev OR release
ifeq ($(BUILD),release)
CFLAGS += -O3
CFLAGS += -funroll-loops
CFLAGS += -flto
else ifeq ($(BUILD),dev)
CFLAGS += -Og
CFLAGS += -g
CFLAGS += -ginline-points # inlined funcs debug info
# for gprof
#CFLAGS += -pg
# Next one may be useful for valgrind (some invalid instructions)
# CFLAGS += -mno-tbm
else ifeq ($(BUILD),perf)
CFLAGS += -O3
CFLAGS += -g # symbols (gdb, perf, etc.)
CFLAGS += -ginline-points # inlined funcs debug info
CFLAGS += -funroll-loops
else ifeq ($(BUILD),debug)
CFLAGS += -O0
CFLAGS += -g # symbols (gdb, perf, etc.)
# for gprof
#CFLAGS += -pg
# Next one may be useful for valgrind (when invalid instructions)
#CFLAGS += -mno-tbm
endif
CFLAGS := $(strip $(CFLAGS))
##################################### archiver/linker/dependency flags
ARFLAGS := rcs
LDFLAGS := -L$(LIBDIR)
LIBS := -l$(LIB)
DEPFLAGS = -MMD -MP -MF $(DEPDIR)/$*.d
ifeq ($(BUILD),release)
LDFLAGS += -flto
endif
##################################### General targets
.PHONY: all libs lib-static lib-dynamic compile test emacs ccls bear clean \
cleanall cleanallall
# default: build libraries
all: libs
# libraries
libs: lib-static lib-dynamic
lib-static: $(SLIB)
lib-dynamic: $(DLIB)
# build objects
compile: $(OBJ)
# build test binaries
test: $(BIN)
# setup emacs projectile/ccls
emacs: $(PRJROOT) $(EMACSLSP)
# update compile-commands.json
ccls bear: $(CCLSCMDS)
# cleanup - need to think about a better system :-)
clean: cleandep cleanobj cleanlib cleanbin
cleanall: clean cleandepdir cleanobjdir cleanlibdir cleanbindir
cleanallall: cleanall cleanemacs
##################################### cleaning functions
# rmfiles - deletes a list of files in a directory if they exist.
# $(1): the directory
# $(2): the list of files to delete
# $(3): The string to include in action output - "cleaning X files."
# see: https://stackoverflow.com/questions/6783243/functions-in-makefiles
#
# Don't use wildcard like "$(DIR)/*.o", so we can control mismatches between
# list and actual files in directory.
# See rmdir below.
define rmfiles
@#echo "rmfiles=+$(1)+"
$(eval $@_EXIST = $(wildcard $(1)))
@#echo "existfile=+${$@_EXIST}+"
@if [[ -n "${$@_EXIST}" ]]; then \
echo "cleaning $(2) files." ; \
$(RM) ${$@_EXIST} ; \
fi
endef
# rmdir - deletes a directory if it exists.
# $(1): the directory
# $(2): The string to include in action output - "removing X dir."
#
# Don't use $(RM) -rf, to control unexpected dep files.
# See rmfile above.
define rmdir
@#echo "rmdir +$(1)+"
$(eval $@_EXIST = $(wildcard $(1)))
@#echo "existdir=+${$@_EXIST}+"
@if [[ -n "${$@_EXIST}" ]]; then \
echo "removing $(2) dir." ; \
$(RMDIR) ${$@_EXIST} ; \
fi
endef
##################################### dirs creation
.PHONY: alldirs
ALLDIRS := $(DEPDIR) $(OBJDIR) $(LIBDIR) $(BINDIR)
alldirs: $(ALLDIRS)
# Here, we have something like:
# a: a
# a will be built if (1) older than a, or (2) does not exist. Here only (2).
$(ALLDIRS): $@
@echo creating $@ directory.
@$(MKDIR) $@
##################################### Dependencies files
.PHONY: cleandep cleandepdir
-include $(wildcard $(DEP))
# Don't use $(DEPDIR)/*.d, to control mismatches between dep and src files.
# See second rule below.
cleandep:
$(call rmfiles,$(DEP),depend)
@#echo cleaning dependency files.
@#$(RM) -f $(DEP)
cleandepdir:
$(call rmdir,$(DEPDIR),depend)
@#[ -d $(DEPDIR) ] && echo cleaning depend files && $(RM) -f $(DEP) || true
##################################### objects
.PHONY: cleanobj cleanobjdir
cleanobj:
$(call rmfiles,$(OBJ),brlib object)
cleanobjdir:
$(call rmdir,$(OBJDIR),brlib objects)
# $(OBJDIR)/%.o: $(BUILDFILE)
$(OBJDIR)/%.o: $(SRCDIR)/%.c $(BUILDFILE) | $(OBJDIR) $(DEPDIR)
@echo compiling $< "->" $@.
$(CC) -c $(DEPFLAGS) $(CPPFLAGS) $(CFLAGS) $< -o $@
##################################### brlib libraries
.PHONY: cleanlib cleanlibdir
cleanlib:
$(call rmfiles,$(DLIB) $(SLIB),library)
cleanlibdir:
$(call rmdir,$(LIBDIR),libraries)
#$(DLIB): CFLAGS += -fPIC
$(DLIB): LDFLAGS += -shared
$(DLIB): $(OBJ) | $(LIBDIR)
@echo "building shared library ($@)."
@$(CC) $(CFLAGS) $(LDFLAGS) $^ -o $@
$(SLIB): $(OBJ) | $(LIBDIR)
@echo "building static library ($@)."
@$(AR) $(ARFLAGS) $@ $? > /dev/null
##################################### testing
.PHONY: cleanbin cleanbindir
CUTESTSRC := $(TESTDIR)/cutest/CuTest.c
cleanbin:
$(call rmfiles,$(BIN),binary)
cleanbindir:
$(call rmdir,$(BINDIR),binaries)
$(BINDIR)/%: $(TESTDIR)/%.c $(SLIB) $(DLIB) | $(BINDIR)
$(CC) $(CPPFLAGS) $(CFLAGS) $< $(CUTESTSRC) $(LDFLAGS) $(LIBS) -o $@
##################################### pre-processed (.i) and assembler (.s) output
%.i: %.c
@echo generating $@
@$(CC) -E $(CPPFLAGS) $(CFLAGS) $< -o $@
%.s: %.c
@echo generating $@
@$(CC) -S -fverbose-asm $(CPPFLAGS) $(CFLAGS) $< -o $@
##################################### Emacs & LSP (ccls)
.PHONY: cleanemacs cleanccls
#EMACSLSP := .dir-locals.el
CCLSFILES := .projectile .ccls .ccls-root
CCLSCACHE := .ccls-cache
cleanemacs:
$(call rmfiles, $(PRJROOT) $(EMACSLSP), Emacs/ccls);
$(CCLSFILES):
printf "creating Emacs dot-files.\n"
@$(TOUCH) $@
cleanccls:
$(call rmfiles, $(CCLSFILES), ccls);
# generate compile_commands.json.
# TODO: add includes and Makefile dependencies.
# also, if cclsfile is newer than sources, no need to clean objects file
# (and to run bear).
# maybe run cleanobj cleanlibobj in commands ?
$(CCLSCMDS): cleanobj $(SRC) | $(CCLSROOT)
@echo "Generating ccls compile commands file ($@)."
@$(BEAR) -- $(MAKE) test
##################################### valgrind (mem check)
.PHONY: memcheck
VALGRIND := valgrind
VALGRINDFLAGS := --leak-check=full --show-leak-kinds=all
VALGRINDFLAGS += --track-origins=yes --sigill-diagnostics=yes
VALGRINDFLAGS += --quiet --show-error-list=yes
VALGRINDFLAGS += --log-file=valgrind.out
# We need to suppress libreadline leaks here. See :
# https://stackoverflow.com/questions/72840015
VALGRINDFLAGS += --suppressions=etc/libreadline.supp
memcheck: targets
@$(VALGRIND) $(VALGRINDFLAGS) $(BINDIR)/brchess
##################################### Makefile debug
.PHONY: showflags wft
showflags:
@echo CFLAGS: "$(CFLAGS)"
@echo CPPFLAGS: $(CPPFLAGS)
@echo DEPFLAGS: $(DEPFLAGS)
@echo LDFLAGS: $(LDFLAGS)
wtf:
@printf "OBJDIR=%s\n\n" "$(OBJDIR)"
@printf "OBJ=%s\n\n" "$(OBJ)"