-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGNUmakefile
353 lines (312 loc) · 8.48 KB
/
GNUmakefile
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
# disable built-in rules and variables
MAKEFLAGS := Rr
.SUFFIXES:
[0-9] = 0 1 2 3 4 5 6 7 8 9
[A-Z] = A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
[a-z] = a b c d e f g h i j k l m n o p q r s t u v w x y z
[markup] = ` ~ ! @ \# $$ % ^ & * ( ) - _ = + [ { ] } \ | ; : ' " , < . > / ?
[all] = $([0-9]) $([A-Z]) $([a-z]) $([markup])
[empty] :=
[space] := $([empty]) $([empty])
# platform detection
ifeq ($(platform),)
ifeq ($(OS),Windows_NT)
platform := windows
endif
endif
ifeq ($(platform),)
uname := $(shell uname)
ifeq ($(uname),)
platform := windows
else ifneq ($(findstring Windows,$(uname)),)
platform := windows
else ifneq ($(findstring NT,$(uname)),)
platform := windows
else ifneq ($(findstring Darwin,$(uname)),)
platform := macos
else ifneq ($(findstring Linux,$(uname)),)
platform := linux
else ifneq ($(findstring BSD,$(uname)),)
platform := bsd
else
$(error unknown platform, please specify manually.)
endif
endif
# common commands
ifeq ($(shell echo ^^),^)
# cmd
fixpath = $(subst /,\\,$1)
mkdir = @if not exist $(call fixpath,$1) (mkdir $(call fixpath,$1))
copy = @copy $(call fixpath,$1) $(call fixpath,$2)
rcopy = @xcopy /e /q /y $(call fixpath,$1) $(call fixpath,$2)
delete = $(info Deleting $1 ...) @del /q $(call fixpath,$1)
rdelete = $(info Deleting $1 ...) @if exist $(call fixpath,$1) (rmdir /s /q $(call fixpath,$1))
which = $(shell where $1 2> NUL)
else
# sh
mkdir = @mkdir -p $1
copy = @cp $1 $2
rcopy = @cp -R $1 $2
delete = $(info Deleting $1 ...) @rm -f $1
rdelete = $(info Deleting $1 ...) @rm -rf $1
which = $(shell which $1 2> /dev/null)
endif
ifeq ($(ccache), true)
compiler.c = ccache $(compiler) -x c -std=c11
compiler.cpp = ccache $(compiler) -x c++ -std=c++17 -fno-operator-names
compiler.objc = ccache $(compiler) -x objective-c -std=c11
compiler.objcpp = ccache $(compiler) -x objective-c++ -std=c++17 -fno-operator-names
else
compiler.c = $(compiler) -x c -std=c11
compiler.cpp = $(compiler) -x c++ -std=c++17 -fno-operator-names
compiler.objc = $(compiler) -x objective-c -std=c11
compiler.objcpp = $(compiler) -x objective-c++ -std=c++17 -fno-operator-names
endif
flags.c = -x c -std=c11
flags.cpp = -x c++ -std=c++17 -fno-operator-names
flags.objc = -x objective-c -std=c11
flags.objcpp = -x objective-c++ -std=c++17 -fno-operator-names
flags.deps = -MMD -MP -MF $(@:.o=.d)
# compiler detection
ifeq ($(compiler),)
# prefer clang
ifneq ($(call which,clang++),)
compiler := clang++
else
compiler := g++
endif
endif
machine_str := $(shell $(compiler) -dumpmachine)
ifneq ($(filter amd64-% x86_64-%,$(machine_str)),)
machine := amd64
else ifneq ($(filter arm64-% aarch64-%,$(machine_str)),)
machine := arm64
else ifneq ($(filter powerpc64-% powerpc64le-%,$(machine_str)),)
machine := ppc64
endif
# detect clang with msvc target
ifeq ($(msvc),)
ifneq ($(filter %-msvc,$(machine_str)),)
msvc := true
endif
endif
# explicit architecture flags to allow for cross-compilation on macos
ifeq ($(platform),macos)
ifeq ($(arch),amd64)
flags += -arch x86_64
options += -arch x86_64
else ifeq ($(arch),arm64)
flags += -arch arm64
options += -arch arm64
endif
ifneq ($(machine),$(arch))
local = false
endif
endif
# architecture detection
ifeq ($(arch),)
ifneq ($(machine),)
arch := $(machine)
else
$(error unknown arch, please specify manually.)
endif
endif
# build optimization levels
ifeq ($(build),debug)
symbols = true
flags += -Og -DBUILD_DEBUG
else ifeq ($(build),stable)
flags += -O1 -DBUILD_STABLE
else ifeq ($(build),minified)
flags += -Os -DBUILD_MINIFIED
else ifeq ($(build),release)
flags += -O2 -DBUILD_RELEASE
else ifeq ($(build),optimized)
flags += -O3 -fomit-frame-pointer -DBUILD_OPTIMIZED
else
$(error unrecognized build type.)
endif
# debugging information
ifeq ($(symbols),true)
flags += -g
ifeq ($(platform),windows)
ifeq ($(findstring clang++,$(compiler)),clang++)
flags += -gcodeview
ifeq ($(msvc),true)
options += -Wl,-debug
else
options += -Wl,-pdb=
endif
endif
endif
endif
# link-time optimization
ifeq ($(lto),true)
ifneq ($(findstring clang++,$(compiler)),clang++)
flags += -flto=auto -fno-fat-lto-objects
else
flags += -flto
options += -flto=thin
endif
endif
# openmp support
ifeq ($(openmp),true)
# macOS Xcode does not ship with OpenMP support
ifneq ($(platform),macos)
flags += -fopenmp
options += -fopenmp
endif
endif
# clang settings
ifeq ($(findstring clang++,$(compiler)),clang++)
flags += -fno-strict-aliasing -fwrapv
ifeq ($(arch),arm64)
# work around bad interaction with alignas(n) when n >= 4096
flags += -mno-global-merge
endif
# gcc settings
else ifeq ($(findstring g++,$(compiler)),g++)
flags += -fno-strict-aliasing -fwrapv -Wno-trigraphs
endif
# windows settings
ifeq ($(platform),windows)
extension := .exe
# target Windows 7
flags += -D_WIN32_WINNT=0x0601
ifeq ($(msvc),true)
flags += -D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_WARNINGS -DNOMINMAX
endif
options += -mthreads -lws2_32 -lole32 -lshell32
options += $(if $(findstring clang++,$(compiler)),-fuse-ld=lld)
options += -static
ifeq ($(console),true)
flags += -DSUBSYTEM_CONSOLE
options += -mconsole
else
flags += -DSUBSYTEM_WINDOWS
options += -mwindows
endif
ifeq ($(windres),)
ifeq ($(msvc),true)
windres := llvm-rc
else
windres := windres
endif
endif
endif
# macos settings
ifeq ($(platform),macos)
flags += -stdlib=libc++ -mmacosx-version-min=10.9 -Wno-auto-var-id -fobjc-arc
options += -lc++ -lobjc -mmacosx-version-min=10.9
# allow mprotect() on dynamic recompiler code blocks
options += -Wl,-segprot,__DATA,rwx,rw
endif
# linux settings
ifeq ($(platform),linux)
options += -ldl
endif
# bsd settings
ifeq ($(platform),bsd)
flags += -I/usr/local/include
options += -Wl,-rpath=/usr/local/lib
options += -Wl,-rpath=/usr/local/lib/gcc8
options += -lstdc++ -lm
endif
# threading support
ifeq ($(threaded),true)
ifneq ($(filter $(platform),linux bsd),)
flags += -pthread
options += -pthread -lrt
endif
endif
# paths
ifeq ($(object.path),)
object.path := obj
endif
ifeq ($(output.path),)
output.path := out
endif
# rules
default: all;
nall.verbose:
$(info Compiler Flags:)
$(foreach n,$(sort $(call unique,$(flags))),$(if $(filter-out -I%,$n),$(info $([space]) $n)))
$(info Linker Options:)
$(foreach n,$(sort $(call unique,$(options))),$(if $(filter-out -l%,$n),$(info $([space]) $n)))
%.o: $<
$(info Compiling $(subst ../,,$<) ...)
@$(call compile)
$(object.path):
$(call mkdir,$(object.path))
$(output.path):
$(call mkdir,$(output.path))
# function compile([arguments])
compile = \
$(strip \
$(if $(filter %.c,$<), \
$(compiler.c) $(flags.deps) $(flags) $1 -c $< -o $@ \
,$(if $(filter %.cpp,$<), \
$(compiler.cpp) $(flags.deps) $(flags) $1 -c $< -o $@ \
)) \
)
# function rwildcard(directory, pattern)
rwildcard = \
$(strip \
$(filter $(if $2,$2,%), \
$(foreach f, \
$(wildcard $1*), \
$(eval t = $(call rwildcard,$f/)) \
$(if $t,$t,$f) \
) \
) \
)
# function unique(source)
unique = \
$(eval __temp :=) \
$(strip \
$(foreach s,$1,$(if $(filter $s,$(__temp)),,$(eval __temp += $s))) \
$(__temp) \
)
# function strtr(source, from, to)
strtr = \
$(eval __temp := $1) \
$(strip \
$(foreach c, \
$(join $(addsuffix :,$2),$3), \
$(eval __temp := \
$(subst $(word 1,$(subst :, ,$c)),$(word 2,$(subst :, ,$c)),$(__temp)) \
) \
) \
$(__temp) \
)
# function strupper(source)
strupper = $(call strtr,$1,$([a-z]),$([A-Z]))
# function strlower(source)
strlower = $(call strtr,$1,$([A-Z]),$([a-z]))
# function strlen(source)
strlen = \
$(eval __temp := $(subst $([space]),_,$1)) \
$(words \
$(strip \
$(foreach c, \
$([all]), \
$(eval __temp := \
$(subst $c,$c ,$(__temp)) \
) \
) \
$(__temp) \
) \
)
# function streq(source)
streq = $(if $(filter-out xx,x$(subst $1,,$2)$(subst $2,,$1)x),,1)
# function strne(source)
strne = $(if $(filter-out xx,x$(subst $1,,$2)$(subst $2,,$1)x),1,)
# prefix
ifeq ($(platform),windows)
prefix := $(subst $([space]),\$([space]),$(strip $(call strtr,$(LOCALAPPDATA),\,/)))
else
prefix := $(HOME)/.local
endif
# objects
nall.objects := $(object.path)/nall.o
$(object.path)/nall.o: $(nall.path)/nall.cpp