-
Notifications
You must be signed in to change notification settings - Fork 2
/
module.mk
237 lines (146 loc) · 5.12 KB
/
module.mk
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
.PHONY: default all bindirs clean cppcheck distclean
.SUFFIXES:
#initialize these
PROGRAMS:=
ALLSRC:=
# directories which might contain object files
# used for both clean and bindirs
ALLDIRS:=
default: all
ifeq ($(ASAN),y)
OPTFLAGS+=-fsanitize=address
LDFLAGS_asan?=-fsanitize=address
LDFLAGS+=$(LDFLAGS_asan)
endif # ASAN
ifeq ($(TSAN),y)
CFLAGS+=-DPIC
OPTFLAGS+=-fsanitize=thread -fpic
LDFLAGS_tsan?=-fsanitize=thread -pie
LDFLAGS+=$(LDFLAGS_tsan)
endif # TSAN
ifeq ($(UBSAN),y)
OPTFLAGS+=-fsanitize=undefined -fno-sanitize-recover=all
LDFLAGS+=-fsanitize=undefined -fno-sanitize-recover=all
endif # UBSAN
ifeq ($(LTO),y)
CFLAGS+=$(LTOCFLAGS)
LDFLAGS+=$(LTOLDFLAGS) $(OPTFLAGS)
endif # LTO
# CivetWeb
CFLAGS+=-DNO_SSL -DUSE_IPV6=1 -DUSE_WEBSOCKET=1
# date tz
CFLAGS+=-DHAS_REMOTE_API=0 -DUSE_OS_TZDB=1
CFLAGS+=$(OPTFLAGS)
CFLAGS+=-I.
CFLAGS+=-I$(TOPDIR)
CFLAGS+=-isystem$(TOPDIR)/foreign
CFLAGS+=-isystem$(TOPDIR)/foreign/civetweb/include
CFLAGS+=-isystem$(TOPDIR)/foreign/date/include
CFLAGS+=-isystem$(TOPDIR)/foreign/fmt/include
CFLAGS+=-isystem$(TOPDIR)/foreign/inja/include
CFLAGS+=-isystem$(TOPDIR)/foreign/libvlcpp
CFLAGS+=-isystem$(TOPDIR)/foreign/nlohmann_json/single_include
CFLAGS+=-isystem$(TOPDIR)/foreign/pybind11/include
CFLAGS+=-isystem$(TOPDIR)/foreign/sqlpp11/include
CFLAGS+=-isystem$(TOPDIR)/foreign/tartanllama_optional/include
CFLAGS+=-isystem$(TOPDIR)/foreign/CxxUrl
CFLAGS+=-isystem$(TOPDIR)/foreign/LURLParser
CFLAGS+=-isystem$(TOPDIR)/foreign/uri-library
EMBED:=listMedia.template create_database.sql footer.template header.template history.template playlist.template standby.png utuputki.css utuputki.js
# (call directory-module, dirname)
define directory-module
# save old
DIRS_$1:=$$(DIRS)
dir:=$1
include $(TOPDIR)/$1/module.mk
ALLDIRS+=$1
ALLSRC+=$$(SRC_$1)
# restore saved
DIRS:=$$(DIRS_$1)
endef # directory-module
DIRS:= \
foreign \
utuputki \
# empty line
$(eval $(foreach directory, $(DIRS), $(call directory-module,$(directory)) ))
TARGETS:=$(foreach PROG,$(PROGRAMS),$(EXEPREFIX)$(PROG)$(EXESUFFIX))
all: $(TARGETS)
# check if a directory needs to be created
# can't use targets with the same name as directory because unfortunate
# interaction with VPATH (targets always exists because source dir)
# $(call missingdir, progname)
define missingdir
ifneq ($$(shell test -d $1 && echo n),n)
MISSINGDIRS+=$1
endif
endef # missingdir
MISSINGDIRS:=
$(eval $(foreach d, $(ALLDIRS), $(call missingdir,$(d)) ))
# create directories which might contain object files
bindirs:
ifneq ($(MISSINGDIRS),)
mkdir -p $(MISSINGDIRS)
endif
EMBED_HEADERS:=$(foreach f, $(EMBED), $(f).h)
clean:
rm -f $(TARGETS) $(foreach dir,$(ALLDIRS),$(dir)/*$(OBJSUFFIX))
distclean: clean
rm -f $(foreach dir,$(ALLDIRS),$(dir)/*.d) $(EMBED_HEADERS)
-rmdir -p --ignore-fail-on-non-empty $(ALLDIRS)
# rules here
# -MD generates dependencies for system headers
# -MMD does not
# we use -MD because we ship some headers which are included with -isystem
# to avoid generating warnings from them
# but we want them to be dependencies
%$(OBJSUFFIX): %.cpp | bindirs
$(CXX) -c -MF $*.d -MP -MD $(CXXFLAGS) -o $@ $<
%$(OBJSUFFIX): %.c | bindirs
$(CC) -c -MF $*.d -MP -MD $(CFLAGS) -o $@ $<
# no warnings in foreign code
foreign/%$(OBJSUFFIX): foreign/%.cpp | bindirs
$(CXX) -c -MF foreign/$*.d -MP -MD $(CXXFLAGS) -w -o $@ $<
foreign/%$(OBJSUFFIX): foreign/%.cc | bindirs
$(CXX) -c -MF foreign/$*.d -MP -MD $(CXXFLAGS) -w -o $@ $<
foreign/%$(OBJSUFFIX): foreign/%.c | bindirs
$(CC) -c -MF foreign/$*.d -MP -MD $(CFLAGS) -w -o $@ $<
# $(call embed-file, filename, headername)
define embed-file
$2: $1 | embed-bin
./embed-bin $1 $$@
endef # embed-file
$(eval $(foreach f, $(EMBED), $(call embed-file, $(TOPDIR)/$(f), $(f).h ) ) )
# $(call resolve-modules, progname)
define resolve-modules
OLD_MODULES:=$$($1_MODULES)
$1_MODULES:=$$(sort $$($1_MODULES) $$(foreach MODULE, $$($1_MODULES), $$(DEPENDS_$$(MODULE))) )
# recurse if changed
ifneq ($$($1_MODULES),$$(OLD_MODULES))
$$(eval $$(call resolve-modules,$1) )
endif
endef # resolve-modules
$(eval $(foreach PROGRAM,$(PROGRAMS), $(call resolve-modules,$(PROGRAM)) ) )
# $(call program-target, progname)
define program-target
ALLSRC+=$$(filter %.cpp,$$($1_SRC))
ALLSRC+=$$(filter %.cc,$$($1_SRC))
ALLSRC+=$$(filter %.c,$$($1_SRC))
$1_SRC+=$$(foreach module, $$($1_MODULES), $$(SRC_$$(module)))
$1_OBJ:=$$($1_SRC:.cpp=$(OBJSUFFIX))
$1_OBJ:=$$($1_OBJ:.cc=$(OBJSUFFIX))
$1_OBJ:=$$($1_OBJ:.c=$(OBJSUFFIX))
$(EXEPREFIX)$1$(EXESUFFIX): $$($1_OBJ) | bindirs
$(CXX) $(LDFLAGS) -o $$@ $$^ $$(foreach module, $$($1_MODULES), $$(LDLIBS_$$(module))) $$($1_LIBS) $(LDLIBS)
endef # program-target
$(eval $(foreach PROGRAM,$(PROGRAMS), $(call program-target,$(PROGRAM)) ) )
export CXX
export CXXFLAGS
export TOPDIR
compile_commands.json: $(ALLSRC)
@echo '[' > $@
@$(TOPDIR)/compile_commands.sh $(filter %.cpp,$(ALLSRC)) >> $@
@echo ']' >> $@
JOBS?=1
cppcheck:
cppcheck -j $(JOBS) $(foreach directory,$(INCLUDEDIRS),-I $(directory)) -i $(TOPDIR)/foreign --enable=all $(TOPDIR) 2> cppcheck.log
-include $(foreach FILE,$(ALLSRC),$(patsubst %.c,%.d,$(patsubst %.cpp,%.d,$(patsubst %.cc,%.d,$(FILE)))))