forked from tastyspleen/r1q2-archive
-
Notifications
You must be signed in to change notification settings - Fork 2
/
module.mk
279 lines (167 loc) · 5.32 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
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
.PHONY: default all bindirs clean cppcheck distclean tidy
.SUFFIXES:
#initialize these
LIBRARIES:=
PROGRAMS:=
ALLSRC=
# directories which might contain object files
# used for both clean and bindirs
ALLDIRS:=baseq2
default: all
ifeq ($(AFL_NET),y)
CFLAGS+=-DUSE_AFL_NET -DUSE_AFL
endif # AFL
ifeq ($(AFL),y)
CFLAGS+=-DUSE_AFL
endif # AFL
ifeq ($(ASAN),y)
OPTFLAGS+=-fsanitize=address
LDFLAGS_asan?=-fsanitize=address
LDFLAGS+=$(LDFLAGS_asan)
endif
ifeq ($(TSAN),y)
CFLAGS+=-DPIC
OPTFLAGS+=-fsanitize=thread -fpic
LDFLAGS_tsan?=-fsanitize=thread -pie
LDFLAGS+=$(LDFLAGS_tsan)
endif
ifeq ($(UBSAN),y)
OPTFLAGS+=-fsanitize=undefined
LDFLAGS+=-fsanitize=undefined
CXXFLAGS+=-frtti
endif
ifeq ($(LTO),y)
CFLAGS+=$(LTOCFLAGS)
LDFLAGS+=$(LTOLDFLAGS) $(OPTFLAGS)
endif
CFLAGS+=$(OPTFLAGS)
ifeq ($(USE_JPEG),y)
CFLAGS+=-isystem$(TOPDIR)/foreign/libjpeg-turbo
endif
ifeq ($(USE_GLEW),y)
CFLAGS+=-isystem$(TOPDIR)/foreign/glew/include
CFLAGS+=-DUSE_GLEW -DGLEW_STATIC -DGLEW_NO_GLU
endif # USE_GLEW
ifeq ($(USE_LIBWEBSOCKETS),y)
CFLAGS+=-isystem$(TOPDIR)/foreign/libwebsockets/lib
CFLAGS+=-DUSE_LIBWEBSOCKETS
endif # USE_LIBWEBSOCKETS
ifeq ($(USE_OPENAL),y)
CFLAGS+=-DUSE_OPENAL
endif # USE_OPENAL
ifeq ($(USE_PNG),y)
CFLAGS+=-isystem$(TOPDIR)/foreign/libpng
endif # USE_PNG
CFLAGS+=-isystem$(TOPDIR)/foreign/remotery/lib
ifeq ($(USE_REMOTERY),y)
CFLAGS+=-DRMT_ENABLED
endif # USE_REMOTERY
CFLAGS+=-isystem$(TOPDIR)/foreign/zlib
ifneq ($(BUILTIN_GAME),)
CFLAGS+=-DGAME_HARD_LINKED=\"$(BUILTIN_GAME)\"
endif
ifeq ($(PURE_CLIENT),y)
CFLAGS+=-DNO_SERVER
endif
# (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:= \
client \
ctfgame \
foreign \
game \
linux \
qcommon \
ref_gl \
ref_null \
server \
utils \
win32 \
# empty line
$(eval $(foreach directory, $(DIRS), $(call directory-module,$(directory)) ))
TARGETS:=$(foreach PROG,$(PROGRAMS),$(PROG)$(EXESUFFIX))
TARGETS+=$(foreach LIB,$(LIBRARIES),$(LIB)/game$(SOSUFFIX))
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
clean:
rm -f $(TARGETS) $(foreach dir,$(ALLDIRS),$(dir)/*$(OBJSUFFIX))
distclean: clean
rm -f $(foreach dir,$(ALLDIRS),$(dir)/*.d)
-rmdir -p --ignore-fail-on-non-empty $(ALLDIRS)
# rules here
foreign/%$(OBJSUFFIX): foreign/%.c | bindirs
$(CC) -c -MF foreign/$*.d -MP -MMD -std=gnu99 $(CFLAGS) -w -o $@ $<
foreign/%$(OBJSUFFIX): foreign/%.cpp | bindirs
$(CXX) -c -MF foreign/$*.d -MP -MMD $(CXXFLAGS) -w -o $@ $<
%$(OBJSUFFIX): %.c | bindirs
$(CC) -c -MF $*.d -MP -MMD -std=gnu99 $(CFLAGS) -o $@ $<
%$(OBJSUFFIX): %.cpp | bindirs
$(CXX) -c -MF $*.d -MP -MMD $(CXXFLAGS) -o $@ $<
%.sh$(OBJSUFFIX): %.c | bindirs
$(CC) -c -MF $*.d -MP -MMD -std=gnu99 $(SOCFLAGS) $(CFLAGS) -o $@ $<
# $(call program-target, progname)
define program-target
ALLSRC+=$$(filter %.c,$$($1_SRC))
ALLSRC+=$$(filter %.cpp,$$($1_SRC))
$1_SRC+=$$(foreach module, $$($1_MODULES), $$(SRC_$$(module)))
# if any .cpp files, link with CXX
ifneq (,$$(findstring .cpp,$$($1_SRC)))
LINK:=$(CXX)
else
LINK:=$(CC)
endif
$1_OBJ:=$$($1_SRC:.c=$(OBJSUFFIX))
$1_OBJ:=$$($1_OBJ:.cpp=$(OBJSUFFIX))
$1$(EXESUFFIX): $$($1_OBJ) $(JS_LIBS) | bindirs
$$(LINK) $(LDFLAGS) -o $$@ $$($1_OBJ) $$(foreach module, $$($1_MODULES), $$(LDLIBS_$$(module))) $$($1_LIBS) $(LDLIBS) $$(foreach jslib, $$(JS_LIBS), --js-library $(TOPDIR)/$$(jslib))
endef # program-target
$(eval $(foreach PROGRAM,$(PROGRAMS), $(call program-target,$(PROGRAM)) ) )
# $(call library-target, progname)
define library-target
ALLSRC+=$$(filter %.c,$$($1_SRC))
$1_SRC+=$$(foreach module, $$($1_MODULES), $$(SRC_$$(module)))
$1_OBJ:=$$($1_SRC:.c=.sh$(OBJSUFFIX))
$1/game$(SOSUFFIX): $$($1_OBJ) | bindirs
$(CC) $(LDFLAGS) -shared -o $$@ $$^ $$(foreach module, $$($1_MODULES), $$(LDLIBS_$$(module))) $$($1_LIBS) $(LDLIBS)
endef # library-target
$(eval $(foreach LIBRARY,$(LIBRARIES), $(call library-target,$(LIBRARY)) ) )
export CC
export CXX
export CFLAGS
export CXXFLAGS
export TOPDIR
compile_commands.json: $(ALLSRC)
@echo '[' > $@
@$(TOPDIR)/compile_commands.sh $(ALLSRC) >> $@
@echo ']' >> $@
cppcheck:
cppcheck -I $(TOPDIR) -i $(TOPDIR)/foreign --enable=warning,performance,information --inconclusive -D__linux__ -DHAVE_UNISTD_H -DHAVE_STDARG_H -UNO_ZLIB -U_MSC_VER -U_WIN32 -U_WIN64 $(TOPDIR) 2> cppcheck.log
NOTFOREIGNSRC:=$(filter-out foreign/%,$(ALLSRC))
NOTFOREIGNSRC:=$(sort $(NOTFOREIGNSRC))
tidy: $(NOTFOREIGNSRC) | compile_commands.json
clang-tidy -checks=*,-clang-analyzer-alpha-*,-clang-analyzer-core-*,-clang-analyzer-osx-*,-google-readability-casting,-google-readability-function,-llvm-include-order -p compile_commands.json $(foreach f, $(NOTFOREIGNSRC),$(TOPDIR)/$(f)) > tidy.log
-include $(foreach FILE,$(ALLSRC),$(patsubst %.cpp,%.d,$(patsubst %.c,%.d,$(FILE))))