-
Notifications
You must be signed in to change notification settings - Fork 167
/
Copy pathGNUmakefile
executable file
·260 lines (230 loc) · 6.49 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
# disable built-in rules and variables
MAKEFLAGS := Rr
.SUFFIXES:
# An empty variable to defeat make functions that trim whitespace.
[empty] =
[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])
[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
delete = $(info Deleting $1 ...) @del /q $(subst /,\,$1)
rdelete = $(info Deleting $1 ...) @del /s /q $(subst /,\,$1) && if exist $(subst /,\,$1) (rmdir /s /q $(subst /,\,$1))
else
# sh
delete = $(info Deleting $1 ...) @rm -f $1
rdelete = $(info Deleting $1 ...) @rm -rf $1
endif
compiler.c = $(compiler) -x c -std=c11
compiler.cpp = $(compiler) -x c++ -std=c++17
compiler.objc = $(compiler) -x objective-c -std=c11
compiler.objcpp = $(compiler) -x objective-c++ -std=c++17
flags.c = -x c -std=c11
flags.cpp = -x c++ -std=c++17
flags.objc = -x objective-c -std=c11
flags.objcpp = -x objective-c++ -std=c++17
flags.deps = -MMD -MP -MF $(@:.o=.d)
# compiler detection
ifeq ($(compiler),)
ifeq ($(platform),windows)
compiler := g++
compiler.cpp = $(compiler) -x c++ -std=gnu++17
flags.cpp = -x c++ -std=gnu++17
else ifeq ($(platform),macos)
compiler := clang++
else ifeq ($(platform),linux)
compiler := g++
else ifeq ($(platform),bsd)
compiler := g++8
else
compiler := g++
endif
endif
# build optimization levels
ifeq ($(build),debug)
flags += -Og -g -DBUILD_DEBUG
else ifeq ($(build),stable)
flags += -O1 -DBUILD_STABLE
else ifeq ($(build),size)
flags += -Os -DBUILD_SIZE
else ifeq ($(build),release)
flags += -O2 -DBUILD_RELEASE
else ifeq ($(build),performance)
flags += -O3 -DBUILD_PERFORMANCE
endif
# link-time optimization
ifeq ($(lto),true)
flags += -fwhole-program -flto -fno-fat-lto-objects
options += -fwhole-program -flto=jobserver
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 -Wno-everything
# gcc settings
else ifeq ($(findstring g++,$(compiler)),g++)
flags += -fno-strict-aliasing -fwrapv -Wno-trigraphs
endif
# windows settings
ifeq ($(platform),windows)
options += -mthreads -lpthread -lws2_32 -lole32
options += $(if $(findstring g++,$(compiler)),-static -static-libgcc -static-libstdc++)
options += $(if $(findstring true,$(console)),-mconsole,-mwindows)
windres := windres
endif
# macos settings
ifeq ($(platform),macos)
flags += -stdlib=libc++
options += -lc++ -lobjc
flags.arm64 += -target x86_64-apple-macos10.12
flags.amd64 += -target arm64-apple-macos11
universal := true
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
# 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 $< ...)
@$(call compile)
# function compile([arguments])
ifeq ($(universal),true)
compile = \
$(strip \
$(if $(filter %.c,$<), \
$(compiler.c) $(flags.arm64) $(flags.deps) $(flags) $1 -c $< -o [email protected] && \
$(compiler.c) $(flags.amd64) $(flags.deps) $(flags) $1 -c $< -o [email protected] && \
,$(if $(filter %.cpp,$<), \
$(compiler.cpp) $(flags.arm64) $(flags.deps) $(flags) $1 -c $< -o [email protected] && \
$(compiler.cpp) $(flags.amd64) $(flags.deps) $(flags) $1 -c $< -o [email protected] && \
)) \
lipo -create -output $@ [email protected] [email protected] \
)
else
compile = \
$(strip \
$(if $(filter %.c,$<), \
$(compiler.c) $(flags.deps) $(flags) $1 -c $< -o $@ \
,$(if $(filter %.cpp,$<), \
$(compiler.cpp) $(flags.deps) $(flags) $1 -c $< -o $@ \
)) \
)
endif
# 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