-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathMakefile.common
355 lines (291 loc) · 9.15 KB
/
Makefile.common
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
###########################################################################
# Standard Make variable definitions
#
# These are the common variable definitions used across different projects.
# This will usually be included at the top of a separate project specific
# Makefile using 'include Makefile.common'.
###########################################################################
#==========================================================================
# Command-line options
# Modify for your system -or- use command-line argument OPTION=string
#==========================================================================
#--------------------------------------------------------------------------
# Operating system variables
#--------------------------------------------------------------------------
# Operating system: 'macosx', 'linux', or 'windows'
ifndef PLATFORM
UNAME := $(shell uname)
ifeq ($(UNAME), Linux)
PLATFORM = linux
else ifeq ($(UNAME), Darwin)
PLATFORM = macosx
ifneq ($(shell sw_vers -productName),macOS)
OSX_VERSION := $(shell sw_vers -productVersion)
OSX_VERSION := $(word 2,$(subst ., , $(OSX_VERSION)))
endif
else
PLATFORM = windows
# uname returns:
# MSYS/MinGW: MINGW32_NT-6.2
# MSYS2/MinGW-32: MINGW32_NT-10.0
# MSYS2/MinGW-64: MINGW64_NT-10.0
# MSYS2: MSYS_NT-10.0
ifneq (,$(findstring MINGW32,$(UNAME)))
MINGW_VERSION := 32
else ifneq (,$(findstring MINGW64,$(UNAME)))
MINGW_VERSION := 64
else ifneq (,$(findstring MSYS_NT,$(UNAME)))
MINGW_VERSION := 64
endif
# uname -o returns 'Msys' for both Msys and Msys2, so can't be used
ifneq (,$(wildcard /msys.bat))
MSYS_VERSION := 1
else
MSYS_VERSION := 2
endif
endif
endif
# Architecture: '32' or '64'
UNAMEM := $(shell uname -m)
ifeq ($(shell which getconf 2>/dev/null),)
ifeq ($(UNAMEM), $(findstring $(UNAMEM), x86_64 ia86 amd64 arm64))
ARCH = 64
else
ARCH = 32
endif
else
# Set equal to number of bits in a type long int
ARCH := $(shell getconf LONG_BIT)
endif
#--------------------------------------------------------------------------
# Build settings
#--------------------------------------------------------------------------
# Language version
CXX_VERSION ?= 14
ifneq ($(CXX_VERSION),)
CXXFLAGS += -std=c++$(CXX_VERSION)
endif
# Name of the library
# This should be defined prior to including this file
LIB_NAME ?=
# Dynamic library major/minor version numbers
DLIB_MAJ = 1
DLIB_MIN = 0
# Build configuration: 'Release' or 'Debug'
# 'Release' aggressively optimizes for speed at the expense of greater
# compile times and program size. 'Debug' generates code suitable for a
# debugging tool.
BUILD_CONFIG = Release
# Whether to build a dynamic library versus a static library
DYNAMIC = 0
# Whether to show verbose output
VERBOSE = 0
# Whether Make should determine the dependencies of each C or C++ file as
# it compiles it. This will be automatically disabled for universal builds
# on Mac OSX.
DEP_TRACK = 1
# Whether to enable strict warnings
STRICT_WARNINGS = 0
# Whether to automatically run executables after building
AUTORUN = 1
# Platform-specific library file extensions
SLIB_EXT = a
DLIB_EXT = so
DLIB_OPT = shared
# Flags required for including and linking against library
LINK_CPPFLAGS ?=
LINK_LDFLAGS ?=
# Flags handed to 'ar' command
SLIB_FLAGS ?=
DLIB_FLAGS ?=
#--------------------------------------------------------------------------
# Default paths
#--------------------------------------------------------------------------
PREFIX = /usr/local/
CONFPREFIX = $(PREFIX)/etc/
MANPREFIX = $(PREFIX)/share/man/
# Locations of built objects
BUILD_DIR ?= build/
BIN_DIR = $(BUILD_DIR)bin/
OBJ_DIR = $(BUILD_DIR)obj/
# Location of library installation
DESTDIR = $(PREFIX)
# Check for some common shared library directories
ifneq ($(wildcard /usr/local/),)
HAS_USR_LOCAL = 1
endif
ifneq ($(wildcard /opt/local/),)
HAS_OPT_LOCAL = 1
endif
ifneq ($(wildcard /usr/X11/),)
HAS_USR_X11 = 1
endif
# Platform-dependent directory
PLATFORM_DIR ?=
#--------------------------------------------------------------------------
# Default tools/commands
#--------------------------------------------------------------------------
# NOTE: ar -s is completely equivalent to running ranlib
AR = ar crs
INSTALL = install
RANLIB = ranlib
RM = rm -f
DBG = gdb
# Let user override variables
-include Makefile.user
#--------------------------------------------------------------------------
# Finalize variables
#--------------------------------------------------------------------------
# Dependency tracking doesn't work with universal libraries, so disable it
ifeq ($(ARCH), 32_64)
DEP_TRACK = 0
endif
# Emscripten (is Unix-like)
ifeq ($(PLATFORM), em)
AR = emar crs
RANLIB = emranlib
LDFLAGS += -lm
LDFLAGS += -lstdc++
DLIB_FLAGS += -shared
DLIB_FLAGS += -Wl,-soname,$(DLIB_MAJ_FILE)
# Linux:
else ifeq ($(PLATFORM), linux)
PLATFORM_DIR = linux/
ifeq ($(ARCH), 32)
CFLAGS += -m32
else ifeq ($(ARCH), 64)
CFLAGS += -m64
else ifeq ($(ARCH), 32_64)
CFLAGS += -m32 -m64
endif
CPPFLAGS += -D__LINUX__ -DLINUX
CPPFLAGS += -I/usr/local/include/ -I/usr/include/
LDFLAGS += -lm
LDFLAGS += -lstdc++
DLIB_FLAGS += -shared
DLIB_FLAGS += -Wl,-soname,$(DLIB_MAJ_FILE)
# Mac OSX:
else ifeq ($(PLATFORM), macosx)
PLATFORM_DIR = osx/
# BUILD_ARCHS is a space-separated list in double quotes, e.g. "arm64 x86_64 i386"
ifdef BUILD_ARCHS
CFLAGS += $(addprefix -arch ,$(BUILD_ARCHS))
endif
# Oh where, oh where have my SDKs gone?
CHECK_SDK_DIR = /Applications/Xcode.app
ifeq ($(wildcard $(CHECK_SDK_DIR)),)
CHECK_SDK_DIR = /Library/Developer/CommandLineTools/SDKs/
ifeq ($(wildcard $(CHECK_SDK_DIR)),)
OSX_SDK_PATH := /Developer/SDKs/
else
OSX_SDK_PATH := $(CHECK_SDK_DIR)
endif
else
OSX_SDK_PATH := $(CHECK_SDK_DIR)/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/
endif
# Set system root directory for stdc++ etc.
OSX_SDK := $(lastword $(shell ls $(OSX_SDK_PATH)))
CFLAGS += -isysroot $(OSX_SDK_PATH)$(OSX_SDK)
# Needed to link to libraries installed through MacPorts
ifdef HAS_OPT_LOCAL
LDFLAGS += -L/opt/local/lib/
CPPFLAGS += -I/opt/local/include/
endif
# Needed to link to libraries installed through homebrew or by user
ifneq ($(wildcard /usr/local/lib),)
LDFLAGS += -L/usr/local/lib/
endif
ifneq ($(wildcard /usr/local/include),)
CPPFLAGS += -I/usr/local/include/
endif
LDFLAGS += -lstdc++
DLIB_EXT = dylib
DLIB_OPT = dynamiclib
DLIB_FLAGS += -dynamiclib
DLIB_FLAGS += -current_version $(DLIB_MAJ).$(DLIB_MIN) -compatibility_version $(DLIB_MAJ).$(DLIB_MIN)
# Windows:
else ifeq ($(PLATFORM), windows)
PLATFORM_DIR = windows/
ifeq ($(MSYS_VERSION), 2)
LDFLAGS += -L/usr/lib/
CPPFLAGS += -I/usr/include/
CPPFLAGS += -D_WIN$(MINGW_VERSION)
PREFIX := /mingw$(MINGW_VERSION)/
else ifeq ($(MSYS_VERSION), 1)
LDFLAGS += -L/mingw/lib/
CPPFLAGS += -I/mingw/include/
PREFIX := /local/
CC := gcc
# This fixes a bug in MINGW/MSYS math.h (!!!)
CPPFLAGS += -U _hypot -D _hypot=hypot
endif
# Location of user installed libs
LDFLAGS += -L$(PREFIX)/lib/
CPPFLAGS += -I$(PREFIX)/include/
CPPFLAGS += -I`pwd`
# SLIB_EXT = lib
# DLIB_EXT = dll
endif
# Whether to echo commands to the terminal...
ifeq ($(VERBOSE), 0)
AR := @$(AR)
CC := @$(CC)
CXX := @$(CXX)
#Redundant, gdb echoes by default
#DBG := @$(DBG)
endif
# Append optimization flags
ifeq ($(BUILD_CONFIG), Release)
CPPFLAGS += -DNDEBUG
CFLAGS += -O3
#CFLAGS += -flto=8 # link-time optimization
#CFLAGS += -fpeel-loops # not supported by clang
else
CFLAGS += -O0 -fno-inline
ifeq ($(BUILD_CONFIG), Debugger)
CFLAGS += -ggdb3
endif
endif
# Append warning flags
ifneq ($(STRICT_WARNINGS), 0)
# See https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
CFLAGS += \
-Wreturn-type \
-Wformat -Wmissing-braces -Wparentheses -Wswitch \
-Wignored-qualifiers \
-Wmisleading-indentation \
-Warray-bounds=1 -Wvla -Waddress \
-Wsizeof-pointer-div -Wsizeof-pointer-memaccess \
-Wunused-variable -Wsign-compare -Wtautological-compare -Wbool-operation -Wno-unknown-pragmas \
-Wenum-compare \
-Wuninitialized -Winit-self
CXXFLAGS += -Wnon-virtual-dtor -Woverloaded-virtual
endif
# Turn some warnings into errors as they can cause program crashes
CFLAGS += -Werror=return-type
SLIB_FILE = lib$(LIB_NAME).$(SLIB_EXT)
DLIB_FILE = lib$(LIB_NAME).$(DLIB_EXT)
DLIB_MAJ_FILE = lib$(LIB_NAME).$(DLIB_MAJ).$(DLIB_EXT)
DLIB_MIN_FILE = lib$(LIB_NAME).$(DLIB_MAJ).$(DLIB_MIN).$(DLIB_EXT)
DLIB_PATH = $(addprefix $(BUILD_DIR)lib/, $(DLIB_MIN_FILE))
SLIB_PATH = $(addprefix $(BUILD_DIR)lib/, $(SLIB_FILE))
# Set file directory/name of specified library type
ifneq ($(DYNAMIC), 0)
CFLAGS += -fPIC
LIB_FILE = $(DLIB_MIN_FILE)
LIB_PATH = $(DLIB_PATH)
else
LIB_FILE = $(SLIB_FILE)
LIB_PATH = $(SLIB_PATH)
endif
#--------------------------------------------------------------------------
# Utility functions
#--------------------------------------------------------------------------
RemoveDir = @if test -d $(1); then $(RM) $(1)* && rmdir $(1); fi
define RemoveBuildDir
$(call RemoveDir, $(BUILD_DIR)include/)
$(call RemoveDir, $(BUILD_DIR)lib/)
$(call RemoveDir, $(BIN_DIR))
$(call RemoveDir, $(OBJ_DIR))
$(call RemoveDir, $(BUILD_DIR))
endef