This repository has been archived by the owner on Sep 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Makefile
394 lines (348 loc) · 9.54 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
# VARS
ifndef MKSUB
MODULE ?= game
CROSS ?=
OUTDIR ?= .
MODULECFLAGS := -DMODULEID_GAME=0 -DMODULEID_SERVER=1 -DMODULEID_TOOLBOX=2
ifeq ($(MODULE),game)
MODULEID := 0
MODULECFLAGS += -DMODULE_GAME
MODULECFLAGS += -DMODULE_SERVER
else ifeq ($(MODULE),server)
MODULEID := 1
MODULECFLAGS += -DMODULE_SERVER
else ifeq ($(MODULE),toolbox)
MODULEID := 2
MODULECFLAGS += -DMODULE_TOOLBOX
else
.PHONY: error
error:
@echo Invalid module: $(MODULE)
@exit 1
endif
ifndef OS
ifeq ($(CROSS),)
CC ?= gcc
STRIP ?= strip
WINDRES ?= true
else ifeq ($(CROSS),win32)
ifndef M32
CC = x86_64-w64-mingw32-gcc
STRIP = x86_64-w64-mingw32-strip
WINDRES = x86_64-w64-mingw32-windres
else
CC = i686-w64-mingw32-gcc
STRIP = i686-w64-mingw32-strip
WINDRES = i686-w64-mingw32-windres
endif
else ifeq ($(CROSS),emscr)
CC = emcc
STRIP ?= true
USEGLES = y
else ifeq ($(CROSS),android)
STRIP ?= true
USESDL2 = y
USEGLES = y
else
.PHONY: error
error:
@echo Invalid cross-compilation target: $(CROSS)
@exit 1
endif
SHCMD = unix
else
CC = gcc
STRIP = strip
WINDRES = windres
CROSS = win32
ifdef MSYS2
SHCMD = unix
else
SHCMD = win32
endif
endif
ifdef M32
CCFLAGS += -m32
endif
ifndef DEBUG
ifndef NOLTO
CCFLAGS += -flto=auto
endif
endif
CC += $(CCFLAGS)
empty :=
space := ${empty} ${empty}
${space} := ${space}
SRCDIR ?= src
ifeq ($(CROSS),)
ifndef M32
PLATFORMDIR := $(subst ${ },_,$(subst /,_,$(shell uname -s)_$(shell uname -m)))
else
PLATFORMDIR := $(subst ${ },_,$(subst /,_,$(shell i386 uname -s)_$(shell i386 uname -m)))
endif
else ifeq ($(CROSS),win32)
ifndef MSYS2
ifdef OS
ifeq ($(shell echo %PROGRAMFILES(x86)%),)
PLATFORMDIR := Windows_x86
else
PLATFORMDIR := Windows_x86_64
endif
else
ifndef M32
PLATFORMDIR := $(subst ${ },_,Windows_Cross_$(shell uname -m))
else
PLATFORMDIR := $(subst ${ },_,Windows_Cross_$(shell i386 uname -m))
endif
endif
else
ifndef M32
PLATFORMDIR := $(subst ${ },_,$(subst /,_,$(shell uname -s)_$(shell uname -m)))
else
PLATFORMDIR := $(subst ${ },_,$(subst /,_,$(shell i386 uname -s)_x86))
endif
endif
else ifeq ($(CROSS),emscr)
PLATFORMDIR := Emscripten
else ifeq ($(CROSS),android)
ifdef CMAKE_ARCH
PLATFORMDIR := Android_$(CMAKE_ARCH)
else
PLATFORMDIR := Android
endif
endif
OBJDIR ?= obj/$(PLATFORMDIR)/$(MODULE)
SRCDIR := $(patsubst %/,%,$(SRCDIR))
OBJDIR := $(patsubst %/,%,$(OBJDIR))
SRCDIR := $(patsubst %\,%,$(SRCDIR))
OBJDIR := $(patsubst %\,%,$(OBJDIR))
DIRS := $(sort $(dir $(wildcard $(SRCDIR)/*/)))
ifeq ($(CROSS),emscr)
DIRS := $(filter-out $(wildcard $(SRCDIR)/zlib/),$(DIRS))
endif
DIRS := $(patsubst %/,%,$(DIRS))
DIRS := $(patsubst %\,%,$(DIRS))
DIRS := $(patsubst $(SRCDIR),,$(DIRS))
BASEDIRS := $(notdir $(DIRS))
ifeq ($(MODULE),server)
BINNAME := ccserver
else ifeq ($(MODULE),toolbox)
BINNAME := cctoolbx
else
BINNAME := cavecube
endif
ifeq ($(CROSS),win32)
BINEXT := .exe
else ifeq ($(CROSS),emscr)
BINEXT := .html
else ifeq ($(CROSS),android)
BINNAME := lib$(BINNAME)
BINEXT := .so
endif
BIN := $(BINNAME)$(BINEXT)
ifeq ($(SHCMD),unix)
BIN := $(OUTDIR)/$(BIN)
else ifeq ($(SHCMD),win32)
BIN := $(OUTDIR)\\$(BIN)
endif
CFLAGS += -Wall -Wextra -D_DEFAULT_SOURCE -D_GNU_SOURCE -pthread -ffast-math
ifeq ($(MODULE),game)
ifdef USEGLES
CFLAGS += -DUSEGLES
ifeq ($(CROSS),win32)
WRFLAGS += -DUSEGLES
endif
endif
ifdef USESDL2
CFLAGS += -DUSESDL2
ifeq ($(CROSS),win32)
WRFLAGS += -DUSESDL2
endif
endif
endif
ifeq ($(CROSS),win32)
WRFLAGS += $(MODULECFLAGS) -DMODULE=$(MODULE)
else ifeq ($(CROSS),emscr)
CFLAGS += -msimd128 -s USE_ZLIB=1
else ifeq ($(CROSS),android)
CFLAGS += -fPIC
endif
ifneq ($(CROSS),emscr)
CFLAGS += -fno-exceptions
ifneq ($(CROSS),android)
CFLAGS += -fno-stack-clash-protection -fcf-protection=none
endif
endif
ifdef DEBUG
CFLAGS += -Og -g -DDEBUG=$(DEBUG)
ifeq ($(CROSS),win32)
WRFLAGS += -DDEBUG=$(DEBUG)
endif
else
CFLAGS += -O2
endif
ifdef NATIVE
CFLAGS += -march=native -mtune=native
endif
ifdef M32
CFLAGS += -DM32
ifeq ($(CROSS),win32)
WRFLAGS += -DM32
endif
endif
CFLAGS += $(MODULECFLAGS) -DMODULEID=$(MODULEID) -DMODULE=$(MODULE)
BINFLAGS += -lm
ifeq ($(MODULE),game)
ifeq ($(CROSS),)
ifdef USESDL2
BINFLAGS += -lSDL2
else
BINFLAGS += -lglfw
endif
BINFLAGS += -lX11
else ifeq ($(CROSS),win32)
BINFLAGS += -lws2_32 -lwinmm
ifdef USESDL2
BINFLAGS += -l:libSDL2.a -lole32 -loleaut32 -limm32 -lsetupapi -lversion
else
BINFLAGS += -lglfw3
endif
BINFLAGS += -lgdi32
else ifeq ($(CROSS),emscr)
BINFLAGS += -s USE_WEBGL2=1
ifdef USESDL2
BINFLAGS += -s USE_SDL=2
else
BINFLAGS += -s USE_GLFW=3
endif
BINFLAGS += --preload-file resources/ --shell-file extras/emscr_shell.html
endif
else ifeq ($(MODULE),server)
ifeq ($(CROSS),win32)
BINFLAGS += -lwinmm
endif
endif
ifeq ($(CROSS),)
BINFLAGS += -pthread -lpthread -ldl
else ifeq ($(CROSS),win32)
BINFLAGS += -pthread -l:libwinpthread.a
else ifeq ($(CROSS),emscr)
BINFLAGS += -O2 -s WASM=1 -s INITIAL_MEMORY=1024MB -s ASYNCIFY=1
BINFLAGS += -s USE_ZLIB=1 -s USE_PTHREADS=1 -s PTHREAD_POOL_SIZE=navigator.hardwareConcurrency
endif
GENSENT = $(OBJDIR)/.mkgen
UTILMK = util.mk
export
else
export SHCMD
export UTILMK := ../../$(UTILMK)
endif
# DEFINES
ifeq ($(SHCMD),unix)
define null
@echo > /dev/null
endef
else ifeq ($(SHCMD),win32)
define null
@echo. > NUL
endef
endif
ifeq ($(SHCMD),unix)
define mkdir
@[ ! -d "$@" ] && echo Creating $@... && mkdir -p "$@"; true
endef
else ifeq ($(SHCMD),win32)
define mkdir
@if not exist "$@" echo Creating $@... & md "$(subst /,\,$@)"
endef
endif
ifeq ($(SHCMD),unix)
define MKSRC
$(subst .mk,,$(subst $(OBJDIR)/,$(SRCDIR)/,$@))
endef
else ifeq ($(SHCMD),win32)
define MKSRC
$(subst .mk,,$(subst $(OBJDIR)/,$(SRCDIR)\,$@))
endef
endif
# RULES AND TARGETS
ifndef MKSUB
build: mkfiles compile $(BIN)
$(null)
cleanmk: $(wildcard $(OBJDIR)/*.mk)
$(wildcard $(OBJDIR)/*.mk): .FORCE
ifeq ($(SHCMD),unix)
@[ ! -d $(MKSRC) ] && rm -f $@ || exit 0
else ifeq ($(SHCMD),win32)
@if not exist $(MKSRC) del /Q $@
endif
mkfiles: $(OBJDIR) cleanmk $(GENSENT)
$(OBJDIR):
$(mkdir)
$(GENSENT): $(wildcard $(SRCDIR)/*/*.c $(SRCDIR)/*/*.h) $(SRCDIR)
@echo Writing makefiles...
ifeq ($(SHCMD),unix)
@rm -f $(OBJDIR)/.mkgen
else ifeq ($(SHCMD),win32)
@if exist "$(OBJDIR)\.mkgen" del /Q "$(OBJDIR)\.mkgen"
endif
@$(MAKE) --no-print-directory -f gen.mk
ifeq ($(SHCMD),unix)
@touch $@
else ifeq ($(SHCMD),win32)
@type NUL > $@
endif
compile: .FORCE
@$(MAKE) --no-print-directory -f $(lastword $(MAKEFILE_LIST)) MKSUB=y compile
$(BIN):
@$(MAKE) --no-print-directory -f $(lastword $(MAKEFILE_LIST)) MKSUB=y bin
run: build
@echo Running $(BIN)...
ifeq ($(SHCMD),unix)
@./$(BIN)
else ifeq ($(SHCMD),win32)
@.\\$(BIN)
endif
clean:
@echo Removing $(OBJDIR)...
ifeq ($(SHCMD),unix)
@rm -rf $(OBJDIR)
else ifeq ($(SHCMD),win32)
@if exist "$(subst /,\,$(OBJDIR))" rmdir /S /Q "$(subst /,\,$(OBJDIR))"
endif
@echo Removing $(BIN)...
ifeq ($(SHCMD),unix)
@rm -f $(BIN)
ifeq ($(CROSS),emscr)
@rm -f $(BINNAME)*.html
@rm -f $(BINNAME)*.js
@rm -f $(BINNAME)*.wasm
@rm -f $(BINNAME)*.data
endif
else ifeq ($(SHCMD),win32)
@if exist $(BIN) del /Q $(BIN)
endif
.PHONY: $(BIN) $(OBJDIR) build mkfiles cleanmk compile run clean
.NOTPARALLEL:
else
$(BASEDIRS): .FORCE
@$(MAKE) --no-print-directory -C "$(SRCDIR)/$@" -f "../../$(OBJDIR)/[email protected]" NAME="$@" CFLAGS="$(CFLAGS) -I../../$(SRCDIR)" SRCDIR="../../$(SRCDIR)" OBJDIR="../../$(OBJDIR)"
compile: $(BASEDIRS) .FORCE
$(BIN): $(wildcard $(OBJDIR)/*/*.o)
@echo Building $@...
ifeq ($(CROSS),win32)
@$(WINDRES) $(WRFLAGS) -DORIG_NAME="$(BIN)" -DINT_NAME="$(BINNAME)" $(SRCDIR)/main/version.rc -o $(OBJDIR)/version.o
@$(CC) $(OBJDIR)/version.o $^ $(BINFLAGS) -o $@
else ifeq ($(CROSS),android)
@$(CC) -shared $^ $(BINFLAGS) -o $@
else
@$(CC) $^ $(BINFLAGS) -o $@
endif
ifndef DEBUG
@$(STRIP) --strip-all $@
endif
bin: $(BIN) .FORCE
$(null)
.PHONY: bin compile $(BASEDIRS)
endif
.FORCE: