-
-
Notifications
You must be signed in to change notification settings - Fork 188
/
compiler_flags
376 lines (323 loc) · 13.1 KB
/
compiler_flags
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
# Find files subroutine for different operating system
# This is recursive version of the makefiles wildcard function
findfiles = $(strip $(subst \n,,$(foreach d,$(wildcard $(addsuffix *,$(1))),$(call findfiles,$(d)/,$(2)) $(wildcard $(d)/$(2)))))
ifneq ($(OS),Windows_NT)
GENERATE_DISTRIBUTION_TESTS=true
endif
# Detect operating system, set default compiler, and detect compiler type
# The defaults are configured for the compilers we test:
# https://github.com/stan-dev/stan/wiki/Supported-C---Compilers-and-Language-Features
## Detect operating system
ifneq ($(OS),Windows_NT)
OS := $(shell uname -s)
endif
## Set OS specific library filename extensions
ifeq ($(OS),Windows_NT)
LIBRARY_SUFFIX ?= .dll
endif
ifeq ($(OS),Darwin)
LIBRARY_SUFFIX ?= .dylib
endif
ifeq ($(OS),Linux)
LIBRARY_SUFFIX ?= .so
endif
## Set default compiler
ifeq (default,$(origin CXX))
ifeq ($(OS),Darwin) ## Darwin is Mac OS X
CXX := clang++
endif
ifeq ($(OS),Linux)
CXX := g++
endif
ifeq ($(OS),Windows_NT)
CXX := g++
endif
endif
# Detect compiler type
# - CXX_TYPE: {gcc, clang, mingw32-gcc, other}
# - CXX_MAJOR: major version of CXX
# - CXX_MINOR: minor version of CXX
ifneq (,$(findstring clang,$(CXX)))
CXX_TYPE ?= clang
endif
ifneq (,$(findstring mingw32-g,$(CXX)))
CXX_TYPE ?= mingw32-gcc
endif
ifneq (,$(findstring gcc,$(CXX)))
CXX_TYPE ?= gcc
endif
ifneq (,$(findstring g++,$(CXX)))
CXX_TYPE ?= gcc
endif
CXX_TYPE ?= other
CXX_VERSION := $(shell $(CXX) -dumpversion 2>&1)
CXX_MAJOR := $(word 1,$(subst ., ,$(CXX_VERSION)))
CXX_MINOR := $(word 2,$(subst ., ,$(CXX_VERSION)))
################################################################################
# Set optional compiler flags for performance
#
# These flags are compiler and compiler version specific optimization flags.
#
# Because clang-6 and clang-7's -dumpversion reports version 4.2.1
# and clang-6 needs either the llvm-ar or ar gold plugin to use link time
# optimization (flto), flto is only turned on for clang versions greater than 8.
# For gcc all versions after 4.9 can use the system `ar` and other utilities
# when compiling with flto.
#
# FLAGS:
# CXXFLAGS_OPTIM: Additional flags to CXXFLAGS
# CPPFLAGS_OPTIM: Additional flags to CXXFLAGS
# CXXFLAGS_OPTIM_TBB: Additional flags to TBB_CXXFLAGS
# CXXFLAGS_OPTIM_SUNDIALS: Additional flags to CXXFLAGS_SUNDIALS
# CPPFLAGS_OPTIM_SUNDIALS: Additional flags to CPPFLAGS_SUNDIALS
# CXXFLAGS_FLTO: Additional flags for compiling with FLTO
# CPPFLAGS_FLTO: Additional flags for compiling with FLTO
# CXXFLAGS_FLTO_TBB: Additional flags for compiling tbb with flto
# CXXFLAGS_FLTO_SUNDIALS: Additional flags for compiling c++ sundials with flto
# CPPFLAGS_FLTO_SUNDIALS: Additional flags for compiling c sundials with flto
# LDFLAGS_OPTIM: Adding optimization flags to LDFLAGS
# LDFLAGS_FLTO: Adding flto options to LDFLAGS
# LDFLAGS_MPI_FLTO: For adding flto options to MPI build
##
ifdef STAN_NO_RANGE_CHECKS
CXXFLAGS_NO_RANGE_CHECKS ?= -DSTAN_NO_RANGE_CHECKS
endif
################################################################################
# Set default compiler flags
#
# The options that have been commented are things that the user can set.
# They are commented because in make, undefined is different than empty;
# we can test for that everywhere, but it's a lot easier using the set if
# unassigned operator (`?=`) when it's not set.
##
O ?= 3
## setup includes
INC ?= $(INC_FIRST) -I $(if $(MATH),$(MATH),.) -I $(EIGEN) -I $(BOOST) $(INC_SUNDIALS)
INC_SUNDIALS ?= -I $(SUNDIALS)/include -I $(SUNDIALS)/src/sundials
INC_GTEST ?= -I $(GTEST)/include -I $(GTEST)
## setup precompiler options
CPPFLAGS_BOOST ?= -DBOOST_DISABLE_ASSERTS
CPPFLAGS_SUNDIALS ?= -DNO_FPRINTF_OUTPUT $(CPPFLAGS_OPTIM_SUNDIALS) $(CXXFLAGS_FLTO_SUNDIALS)
#CPPFLAGS_GTEST ?=
## setup compiler flags
CXXFLAGS_LANG ?= -std=c++1y
#CXXFLAGS_BOOST ?=
CXXFLAGS_SUNDIALS ?= -pipe $(CXXFLAGS_OPTIM_SUNDIALS) $(CPPFLAGS_FLTO_SUNDIALS)
#CXXFLAGS_GTEST
## These are the other compiler flags that can be set.
##
## They are listed here for documentation and are intentionally
## commented out.
# CXXFLAGS_OS
# CXXFLAGS_WARNINGS
# CXXFLAGS_EIGEN
## OPENCL specific flags
# CXXFLAGS_OPENCL
# LDFLAGS_OPENCL
# LDLIBS_OPENCL
## TBB specific flags
# CXXFLAGS_TBB
# LDFLAGS_TBB
# LDLIBS_TBB
## MPI specific flags
# CXXFLAGS_MPI
# LDFLAGS_MPI
# LDLIBS_MPI
################################################################################
# Update compiler flags with operating system specific modifications
##
ifeq ($(OS),Windows_NT)
CXXFLAGS_WARNINGS ?= -Wall -Wno-unused-function -Wno-uninitialized -Wno-unused-but-set-variable -Wno-unused-variable -Wno-sign-compare -Wno-unused-local-typedefs -Wno-int-in-bool-context -Wno-attributes
CPPFLAGS_GTEST ?= -DGTEST_HAS_PTHREAD=0
CPPFLAGS_OS ?= -D_USE_MATH_DEFINES
## Detecting Process Bitness:
## http://blogs.msdn.com/b/david.wang/archive/2006/03/26/howto-detect-process-bitness.aspx
ifeq ($(PROCESSOR_ARCHITECTURE)$(PROCESSOR_ARCHITEW6432),x86)
CXXFLAGS_OS ?= -m32
else
CXXFLAGS_OS ?= -m64
endif
ifneq (gcc,$(CXX_TYPE))
LDLIBS_OS ?= -static-libgcc
else
LDLIBS_OS ?= -static-libgcc -static-libstdc++
endif
EXE := .exe
endif
ifeq ($(OS),Darwin)
ifeq (clang,$(CXX_TYPE))
CXXFLAGS_OS ?= -Wno-unknown-warning-option -Wno-tautological-compare -Wno-sign-compare
endif
endif
ifeq ($(OS),Linux)
CPPFLAGS_GTEST ?= -DGTEST_HAS_PTHREAD=0
CXXFLAGS_WARNINGS ?= -Wno-sign-compare
ifeq (gcc,$(CXX_TYPE))
CXXFLAGS_OS ?= -pthread
endif
ifeq (mingw32-gcc,$(CXX_TYPE))
LDLIBS_OS ?= -static -lwinpthread
EXE := .exe
WINE ?= wine
endif
ifeq (clang,$(CXX_TYPE))
LDLIBS_OS ?= -lpthread
endif
endif
## makes reentrant version lgamma_r available from cmath
CXXFLAGS_OS += -D_REENTRANT
## silence warnings occuring due to the TBB and Eigen libraries
CXXFLAGS_WARNINGS += -Wno-ignored-attributes
################################################################################
# Setup OpenCL
#
# Adds the following to CXXFLAGS_OPENCL
# OPENCL_DEVICE_ID - The ID of the OpenCL device (default: 0)
# OPENCL_PLATFORM_ID The ID of the OpenCL platform (default: 0)
# Both IDs can be found through installing and calling clinfo
ifdef STAN_OPENCL
ifeq ($(OS),Darwin) # Mac OS X
LDLIBS_OPENCL ?= -framework OpenCL
else
LDLIBS_OPENCL ?= -lOpenCL
endif
OPENCL_DEVICE_ID ?= 0
OPENCL_PLATFORM_ID ?= 0
CPPFLAGS_OPENCL ?= -DSTAN_OPENCL -DOPENCL_DEVICE_ID=$(OPENCL_DEVICE_ID) -DOPENCL_PLATFORM_ID=$(OPENCL_PLATFORM_ID)
CPPFLAGS_OPENCL += -DCL_HPP_TARGET_OPENCL_VERSION=120 -DCL_HPP_MINIMUM_OPENCL_VERSION=120
CPPFLAGS_OPENCL += -DCL_HPP_ENABLE_EXCEPTIONS -Wno-ignored-attributes
CXXFLAGS_OPENCL ?= -I $(OPENCL)
ifdef INTEGRATED_OPENCL
CPPFLAGS_OPENCL += -DINTEGRATED_OPENCL=1
else
CPPFLAGS_OPENCL += -DINTEGRATED_OPENCL=0
endif
endif
################################################################################
# Setup Intel TBB
#
# Sets up TBB CXXFLAGS_TBB and LDFLAGS_TBB to compile and link to TBB
#
# The tbbmalloc and tbbmalloc_proxy libraries are optionally included
# as targets. By default these are included on MacOS only. This behavior
# can be altered by explicitly setting the TBB_LIBRARIES variable which
# should contain "tbb" or "tbb tbbmalloc tbbmalloc_proxy". Setting the
# TBB_LIBRARIES variable overrides the default.
TBB_BIN ?= $(MATH)lib/tbb
TBB_RELATIVE_PATH ?= ../$(notdir $(TBB))
TBB_BIN_ABSOLUTE_PATH = $(abspath $(TBB_BIN))
TBB_ABSOLUTE_PATH = $(abspath $(TBB))
ifeq ($(OS),Darwin)
TBB_LIBRARIES ?= tbb tbbmalloc tbbmalloc_proxy
else
TBB_LIBRARIES ?= tbb
endif
ifdef TBB_LIB
ifdef TBB_INC
CXXFLAGS_TBB ?= -I $(TBB_INC)
else
CXXFLAGS_TBB ?= -I $(TBB)/include
endif
LDFLAGS_TBB ?= -Wl,-L,"$(TBB_LIB)" -Wl,-rpath,"$(TBB_LIB)" -Wl,--disable-new-dtags -ltbb
LDLIBS_TBB ?= $(LDFLAGS_TBB)
else
ifeq ($(OS),Windows_NT)
TBB_TARGETS ?= $(addprefix $(TBB_BIN)/,$(addsuffix $(LIBRARY_SUFFIX),$(TBB_LIBRARIES)))
endif
ifeq ($(OS),Darwin)
TBB_TARGETS ?= $(addprefix $(TBB_BIN)/lib,$(addsuffix $(LIBRARY_SUFFIX), $(TBB_LIBRARIES)))
endif
ifeq ($(OS),Linux)
# Update the suffix with the internal TBB source code!
# The new version of TBB library is 12+ (e.g., libtbb.so.12.1 not libtbb.so.2)
TBB_TARGETS ?= $(addprefix $(TBB_BIN)/lib,$(addsuffix $(LIBRARY_SUFFIX).2,$(TBB_LIBRARIES)))
endif
CXXFLAGS_TBB ?= -I $(TBB)/include
LDFLAGS_TBB ?= -Wl,-L,"$(TBB_BIN_ABSOLUTE_PATH)" -Wl,-rpath,"$(TBB_BIN_ABSOLUTE_PATH)" $(LDFLAGS_FLTO_FLTO) $(LDFLAGS_OPTIM_TBB)
LDLIBS_TBB ?= $(LDFLAGS_TBB)
endif
ifdef TBB_INTERFACE_NEW
CPPFLAGS_TBB ?= -DTBB_INTERFACE_NEW
endif
################################################################################
# Setup STAN_THREADS
#
# Sets up CXXFLAGS_THREADS to use threading
ifdef STAN_THREADS
CXXFLAGS_THREADS ?= -DSTAN_THREADS
endif
################################################################################
# Setup MPI
#
# Sets up MPI CXXFLAGS_MPI and LDLIBS_MPI to compile and link to MPI
ifdef STAN_MPI
ifeq ($(OS),Windows_NT)
$(error MPI is not supported on Windows.)
endif
MPI_TARGETS ?= $(addsuffix $(LIBRARY_SUFFIX),$(BOOST)/stage/lib/libboost_serialization $(BOOST)/stage/lib/libboost_mpi) $(MPI_TEMPLATE_INSTANTIATION)
CPPFLAGS_MPI ?= -DSTAN_MPI
BOOST_LIBRARY_ABSOLUTE_PATH = $(abspath $(BOOST)/stage/lib)
LDFLAGS_MPI ?= -Wl,-L,"$(BOOST_LIBRARY_ABSOLUTE_PATH)" -Wl,-rpath,"$(BOOST_LIBRARY_ABSOLUTE_PATH)" $(LDFLAGS_MPI_FLTO) $(MPI_OPTIM)
CXXFLAGS_MPI ?= -Wno-delete-non-virtual-dtor
endif
CXXFLAGS += $(CXXFLAGS_LANG) $(CXXFLAGS_OS) $(CXXFLAGS_WARNINGS) $(CXXFLAGS_BOOST) $(CXXFLAGS_EIGEN) $(CXXFLAGS_OPENCL) $(CXXFLAGS_MPI) $(CXXFLAGS_THREADS) $(CXXFLAGS_TBB) $(CXXFLAGS_FLTO) $(CXXFLAGS_OPTIM) $(CXXFLAGS_NO_RANGE_CHECKS) -O$(O) $(INC)
CPPFLAGS += $(CPPFLAGS_LANG) $(CPPFLAGS_OS) $(CPPFLAGS_WARNINGS) $(CPPFLAGS_BOOST) $(CPPFLAGS_EIGEN) $(CPPFLAGS_OPENCL) $(CPPFLAGS_TBB) $(CPPFLAGS_MPI) $(CPPFLAGS_TBB) $(CPPFLAGS_FLTO) $(CPPFLAGS_OPTIM) $(CXXFLAGS_NO_RANGE_CHECKS)
LDFLAGS += $(LDFLAGS_LANG) $(LDFLAGS_OS) $(LDFLAGS_WARNINGS) $(LDFLAGS_EIGEN) $(LDFLAGS_BOOST) $(LDFLAGS_SUNDIALS) $(LDFLAGS_TBB) $(LDFLAGS_OPENCL) $(LDFLAGS_MPI) $(LDFLAGS_FLTO) $(LDFLAGS_OPTIM)
LDLIBS += $(LDLIBS_LANG) $(LDLIBS_OS) $(LDLIBS_WARNINGS) $(LDLIBS_EIGEN) $(LDLIBS_BOOST) $(LDLIBS_SUNDIALS) $(LDLIBS_TBB) $(LDLIBS_OPENCL) $(LDLIBS_MPI)
.PHONY: print-compiler-flags
print-compiler-flags:
@echo ' Current configuration:'
@echo ' - OS (Operating System): ' $(OS)
@echo ' - CXX (Compiler): ' $(CXX)
@echo ' - CXX_TYPE ' $(CXX_TYPE)
@echo ' - Compiler version: ' $(CXX_MAJOR).$(CXX_MINOR)
@echo ' - O (Optimization Level): ' $(O)
@echo ' Library configuration:'
@echo ' - EIGEN ' $(EIGEN)
@echo ' - BOOST ' $(BOOST)
@echo ' - SUNDIALS ' $(SUNDIALS)
@echo ' - TBB ' $(TBB)
@echo ' - GTEST ' $(GTEST)
@echo ' - STAN_THREADS ' $(STAN_THREADS)
@echo ' - STAN_OPENCL ' $(STAN_OPENCL)
@echo ' - STAN_MPI ' $(STAN_MPI)
@echo ' Compiler flags (each can be overriden separately):'
@echo ' - CXXFLAGS_LANG ' $(CXXFLAGS_LANG)
@echo ' - CXXFLAGS_WARNINGS ' $(CXXFLAGS_WARNINGS)
@echo ' - CXXFLAGS_OPTIM ' $(CXXFLAGS_OPTIM)
@echo ' - CXXFLAGS_FLTO ' $(CXXFLAGS_FLTO)
@echo ' - CXXFLAGS_BOOST ' $(CXXFLAGS_BOOST)
@echo ' - CXXFLAGS_EIGEN ' $(CXXFLAGS_EIGEN)
@echo ' - CXXFLAGS_OS ' $(CXXFLAGS_OS)
@echo ' - CXXFLAGS_GTEST ' $(CXXFLAGS_GTEST)
@echo ' - CXXFLAGS_THREADS ' $(CXXFLAGS_THREADS)
@echo ' - CXXFLAGS_OPENCL ' $(CXXFLAGS_OPENCL)
@echo ' - CXXFLAGS_TBB ' $(CXXFLAGS_TBB)
@echo ' - CXXFLAGS_OPTIM_TBB ' $(CXXFLAGS_OPTIM_TBB)
@echo ' - CXXFLAGS_MPI ' $(CXXFLAGS_MPI)
@echo ' - CFLAGS_SUNDIALS ' $(CFLAGS_SUNDIALS)
@echo ' - CXXFLAGS_SUNDIALS ' $(CXXFLAGS_SUNDIALS)
@echo ' - CXXFLAGS_OPTIM_SUNDIALS ' $(CXXFLAGS_OPTIM_SUNDIALS)
@echo ' LDLIBS:'
@echo ' - LDLIBS_LANG ' $(LDLIBS_LANG)
@echo ' - LDLIBS_WARNINGS ' $(LDLIBS_WARNINGS)
@echo ' - LDLIBS_EIGEN ' $(LDLIBS_EIGEN)
@echo ' - LDLIBS_BOOST ' $(LDLIBS_BOOST)
@echo ' - LDLIBS_SUNDIALS ' $(LDLIBS_SUNDIALS)
@echo ' - LDLIBS_OS ' $(LDLIBS_OS)
@echo ' - LDLIBS_GTEST ' $(LDLIBS_GTEST)
@echo ' - LDLIBS_OPENCL ' $(LDLIBS_OPENCL)
@echo ' - LDLIBS_TBB ' $(LDLIBS_TBB)
@echo ' - LDLIBS_MPI ' $(LDLIBS_MPI)
@echo ' LDFLAGS:'
@echo ' - LDFLAGS_LANG ' $(LDFLAGS_LANG)
@echo ' - LDFLAGS_WARNINGS ' $(LDFLAGS_WARNINGS)
@echo ' - LDFLAGS_EIGEN ' $(LDFLAGS_EIGEN)
@echo ' - LDFLAGS_BOOST ' $(LDFLAGS_BOOST)
@echo ' - LDFLAGS_SUNDIALS ' $(LDFLAGS_SUNDIALS)
@echo ' - LDFLAGS_OS ' $(LDFLAGS_OS)
@echo ' - LDFLAGS_GTEST ' $(LDFLAGS_GTEST)
@echo ' - LDFLAGS_OPENCL ' $(LDFLAGS_OPENCL)
@echo ' - LDFLAGS_TBB ' $(LDFLAGS_TBB)
@echo ' - LDFLAGS_MPI ' $(LDFLAGS_MPI)
@echo ''