-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
370 lines (310 loc) · 8.48 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
# Compilation options:
# * LTO - Link time optimization
# * ASAN - Address sanitizer
# * TSAN - Thread sanitizer
# * UBSAN - Undefined behavior sanitizer
# * ESAN - Engine sanitizer
# * DEBUG - Debug build
# * PROFILE - Profile build
# * SRCDIR - Out of tree builds
# * UNUSED - Removed unused references
LTO ?= 0
ASAN ?= 0
TSAN ?= 0
UBSAN ?= 0
ESAN ?= 0
DEBUG ?= 0
PROFILE ?= 0
SRCDIR ?= src
UNUSED ?= 1
#
# Some recursive make functions to avoid shelling out
#
rwildcard = $(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2) $(filter $(subst *,%,$2),$d))
uniq = $(if $1,$(firstword $1) $(call uniq,$(filter-out $(firstword $1),$1)))
# C compiler
CC := gcc
CC ?= clang
# C++ compiler
# We use the C frontend with -xc++ to avoid linking in C++ runtime library.
CXX := $(CC) -xc++
# Determine if the C or C++ compiler should be used as the linker frontend.
ifneq (,$(findstring -xc++,$(CXX)))
LD := $(CC)
else
LD := $(CXX)
endif
# Determine build type.
ifeq ($(DEBUG), 1)
TYPE := debug
else ifeq ($(PROFILE),1)
TYPE := profile
else
TYPE := release
endif
# Determine if Emscripten is being used.
ifneq (,$(findstring emcc,$(CC)))
EMSCRIPTEN := 1
else
EMSCRIPTEN := 0
endif
# Determine the binary output file name.
ifneq (,$(findstring mingw,$(CC)))
BIN := rex.exe
else ifeq ($(EMSCRIPTEN), 1)
BIN := rex.html
else
BIN := rex
endif
# Artifacts generated and what to remove on clean.
ARTIFACTS := $(BIN)
ifeq ($(EMSCRIPTEN), 1)
# Emscripten generates additional artifacts so list them here.
ARTIFACTS += rex.js
ARTIFACTS += rex.wasm
ARTIFACTS += rex.data
ARTIFACTS += rex.worker.js
ifeq ($(DEBUG), 1)
ARTIFACTS += rex.wasm.map
endif
endif
# Build artifact directories.
OBJDIR := .build/$(TYPE)/objs
DEPDIR := .build/$(TYPE)/deps
# Collect all .cpp, .c and .S files for build in the source directory.
SRCS := $(call rwildcard, $(SRCDIR)/, *cpp)
SRCS += $(call rwildcard, $(SRCDIR)/, *c)
SRCS += $(call rwildcard, $(SRCDIR)/, *S)
# Generate object and dependency filenames.
OBJS := $(filter %.o,$(SRCS:%.cpp=$(OBJDIR)/%.o))
OBJS += $(filter %.o,$(SRCS:%.c=$(OBJDIR)/%.o))
OBJS += $(filter %.o,$(SRCS:%.S=$(OBJDIR)/%.o))
DEPS := $(filter %.d,$(SRCS:%.cpp=$(DEPDIR)/%.d))
DEPS += $(filter %.d,$(SRCS:%.c=$(DEPDIR)/%.d))
#
# Shared C and C++ compilation flags.
#
CFLAGS := -Isrc
CFLAGS += -Wall
CFLAGS += -Wextra
ifeq ($(EMSCRIPTEN), 1)
# When building with Emscripten ensure we are generating code that is compiled
# with support for atomics and bulk-memory features for threading support.
CFLAGS += -s USE_PTHREADS=1
CFLAGS += -s USE_SDL=2
else ifneq (,$(findstring mingw,$(CC)))
# When building with mingw ensure we use a platform toolset that requires
# Windows Vista as a minimum target.
CFLAGS += -D_WIN32_WINNT=0x0600
CFLAGS += -I/usr/x86_64-w64-mingw32/include/SDL2
else
CFLAGS += `sdl2-config --cflags`
endif
# Give each function and data it's own section so the linker can remove unused
# references to each, producing smaller, tighter binaries.
ifeq ($(UNUSED), 1)
CFLAGS += -ffunction-sections
CFLAGS += -fdata-sections
endif
# Disable unneded features in release builds.
ifneq ($(DEBUG),1)
# These are needed for stack traces in debug builds.
CFLAGS += -fno-unwind-tables
CFLAGS += -fno-asynchronous-unwind-tables
endif
# Enable link-time optimizations if requested.
ifeq ($(LTO),1)
CFLAGS += -flto
endif
# Enable engine sanitizer if requested.
ifeq ($(ESAN),1)
CFLAGS += -DRX_ESAN
endif
ifeq ($(DEBUG),1)
# Generate source maps with Emscripten.
ifeq ($(EMSCRIPTEN),1)
CFLAGS += -g4
else
CFLAGS += -g
endif
CFLAGS += -DRX_DEBUG
# Optimize for debugging.
CFLAGS += -O0
# Ensure there's a frame pointer in debug builds.
CFLAGS += -fno-omit-frame-pointer
else ifeq ($(PROFILE),1)
# Enable profile options in profile builds.
CFLAGS += -pg
CFLAGS += -no-pie
# Enable debug symbols and assertions in profile builds.
CFLAGS += -g
CFLAGS += -DRX_DEBUG
# Use slightly less aggressive optimizations in profile builds.
CFLAGS += -O2
CFLAGS += -fno-inline-functions
CFLAGS += -fno-inline-functions-called-once
CFLAGS += -fno-optimize-sibling-calls
else
# Enable assertions in release temporarily.
CFLAGS += -DRX_DEBUG
# Disable default C assertions.
CFLAGS += -DNDEBUG
# Highest optimization flag in release builds.
CFLAGS += -O3
# Disable all the stack protection features in release builds.
CFLAGS += -fno-stack-protector
CFLAGS += -fno-stack-check
ifeq ($(CC),gcc)
CFLAGS += -fno-stack-clash-protection
endif
# Disable frame pointer in release builds when AddressSanitizer isn't present.
ifeq ($(ASAN),1)
CFLAGS += -fno-omit-frame-pointer
else
CFLAGS += -fomit-frame-pointer
endif
endif
# Sanitizer selection.
ifeq ($(ASAN),1)
CFLAGS += -fsanitize=address
endif
ifeq ($(TSAN),1)
CFLAGS += -fsanitize=thread -DRX_TSAN
endif
ifeq ($(UBSAN),1)
CFLAGS += -fsanitize=undefined
endif
#
# C compilation flags.
#
CCFLAGS := $(CFLAGS)
CCFLAGS += -std=c11 -D_DEFAULT_SOURCE
#
# C++ compilation flags.
#
CXXFLAGS := $(CFLAGS)
CXXFLAGS += -std=c++20
# Disable some unneeded features.
CXXFLAGS += -fno-exceptions
CXXFLAGS += -fno-rtti
#
# Dependency flags.
#
DEPFLAGS := -MMD
DEPFLAGS += -MP
#
# Output flags (the thing after -o bin)
#
ifeq ($(EMSCRIPTEN), 1)
OFLAGS := --preload-file ./base/
OFLAGS += --shell-file src/rx/web/shell.html
endif
#
# Linker flags.
#
LDFLAGS := -ldl
LDFLAGS += -lm
# Emscripten specific linker flags.
ifeq ($(EMSCRIPTEN), 1)
# Emscripten ports.
LDFLAGS += -s USE_PTHREADS=1
LDFLAGS += -s USE_SDL=2
LDFLAGS += -s USE_WEBGL2=1
# Preinitialize a thread pool with four webworkers here to avoid blocking
# in main waiting for our builtin thread pool to initialize since we depend
# on synchronous thread construction of our thread pool.
LDFLAGS += -s PTHREAD_POOL_SIZE=5
# Allow the heap to grow when we run out of memory in the browser.
LDFLAGS += -s ALLOW_MEMORY_GROWTH=1
# Record all the Emscripten flags in the build.
LDFLAGS += -s RETAIN_COMPILER_SETTINGS=1
# Allocate 1 GiB for Rex.
LDFLAGS += -s INITIAL_MEMORY=1073741824
ifeq ($(DEBUG), 1)
# When building debug builds with Emscripten the -g flag must also be given
# to the linker. Similarly, a source map base must be given so that the
# browser can find it when debugging.
#
# Note that the port 6931 is the default port for emrun.
LDFLAGS += -g4 --source-map-base http://localhost:6931/
# Additional assertions and loggging.
LDFLAGS += -s ASSERTIONS=2
LDFLAGS += -s RUNTIME_LOGGING=1
# OpenGL debug stuff.
#
# ES3 permits some things even FULL_ES3=1 does not support and we can catch
# these with some additional assertions and debugging flags here.
#
# Typical issues that come up include draw buffer misuse and feedback loops
# caused by render to texture.
LDFLAGS += -s GL_ASSERTIONS=1
LDFLAGS += -s GL_DEBUG=1
else
# Enable some potentially unsafe OpenGL optimizations for release builds.
LDFLAGS += -s GL_UNSAFE_OPTS=1
LDFLAGS += -s GL_TRACK_ERRORS=0
endif
else
LDFLAGS += -lpthread
LDFLAGS += -lSDL2
endif
# Strip unused symbols if requested.
ifeq ($(UNUSED), 1)
LDFLAGS += -Wl,--gc-sections
endif
# Enable profiling if requested.
ifeq ($(PROFILE),1)
LDFLAGS += -pg
LDFLAGS += -no-pie
endif
# Enable link-time optimizations if requested.
ifeq ($(LTO),1)
LDFLAGS += -flto
endif
# Sanitizer selection.
ifeq ($(ASAN),1)
LDFLAGS += -fsanitize=address
endif
ifeq ($(TSAN),1)
LDFLAGS += -fsanitize=thread
endif
ifeq ($(UBSAN),1)
LDFLAGS += -fsanitize=undefined
endif
# Strip the binary when not a debug build.
ifneq (,$(findstring RX_DEBUG,$(CFLAGS)))
STRIP := true
else
STRIP := strip
endif
all: $(BIN)
# Build artifact directories..
$(DEPDIR):
@mkdir -p $(addprefix $(DEPDIR)/,$(call uniq,$(dir $(SRCS))))
$(OBJDIR):
@mkdir -p $(addprefix $(OBJDIR)/,$(call uniq,$(dir $(SRCS))))
$(OBJDIR)/%.o: %.cpp $(DEPDIR)/%.d | $(OBJDIR) $(DEPDIR)
$(CXX) -MT $@ $(DEPFLAGS) -MF $(DEPDIR)/$*.Td $(CXXFLAGS) -c -o $@ $<
@mv -f $(DEPDIR)/$*.Td $(DEPDIR)/$*.d
$(OBJDIR)/%.o: %.c $(DEPDIR)/%.d | $(OBJDIR) $(DEPDIR)
$(CC) -MT $@ $(DEPFLAGS) -MF $(DEPDIR)/$*.Td $(CCFLAGS) -c -o $@ $<
@mv -f $(DEPDIR)/$*.Td $(DEPDIR)/$*.d
$(OBJDIR)/%.o: %.S | $(OBJDIR)
$(CC) -c -o $@ $<
@mv -f $(DEPDIR)/$*.Td $(DEPDIR)/$*.d
$(BIN): $(OBJS)
$(LD) $(OBJS) $(LDFLAGS) -o $@ $(OFLAGS)
$(STRIP) $@
ifeq ($(EMSCRIPTEN), 1)
mkdir -p web
mv -t web/ $(ARTIFACTS)
cp src/rx/web/rex.css web/
cp src/rx/web/rex.module.js web/
endif
clean:
rm -rf $(DEPDIR) $(OBJDIR) $(ARTIFACTS)
doc:
doxygen $(SRCDIR)/rx/Doxyfile
.PHONY: clean doc $(DEPDIR) $(OBJDIR)
$(DEPS):
include $(wildcard $(DEPS))