-
Notifications
You must be signed in to change notification settings - Fork 393
/
CompilerFlags.cmake
275 lines (231 loc) · 13.3 KB
/
CompilerFlags.cmake
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
# Compiler-agnostic compiler flags first
ADD_CXX_DEFINITIONS("-DOBJEXXFCL_ALIGN=64") # Align ObjexxFCL arrays to 64B
ADD_CXX_DEBUG_DEFINITIONS("-DOBJEXXFCL_ARRAY_INIT_DEBUG") # Initialize ObjexxFCL arrays to aid debugging
if (NOT OPENGL_FOUND)
add_definitions("-DEP_NO_OPENGL")
endif()
# Make sure expat is compiled as a static library
ADD_DEFINITIONS("-DXML_STATIC")
SET(CMAKE_CXX_STANDARD 17)
if ( APPLE )
# Force no auto ptr
# TODO remove this after kiva/boost is updated to a version that supports
# C++17
ADD_DEFINITIONS("-DBOOST_NO_AUTO_PTR")
endif()
IF ( MSVC AND NOT ( "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel" ) ) # Visual C++ (VS 2013)
# COMPILER FLAGS
ADD_COMPILE_OPTIONS("/nologo")
ADD_COMPILE_OPTIONS("/EHsc")
ADD_COMPILE_OPTIONS("/MP") # Enables multi-processor compilation of source within a single project
STRING (REGEX REPLACE "/W3" "/W1" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") # Increase to /W2 then /W3 as more serious warnings are addressed (using regex to avoid VC override warnings)
# Disabled Warnings: Enable some of these as more serious warnings are addressed
# 4068 Unknown pragma
# 4101 Unreferenced local variable
# 4102 Unreferenced label
# 4244 Narrowing conversions
# 4258 Definition from the loop is ignored
# 4355 Passing this pointer in class initializer (object is incomplete so bases/members can only use this in limited ways)
# 4996 Deprecated functions (/D_SCL_SECURE_NO_WARNINGS /D_CRT_SECURE_NO_WARNINGS /D_CRT_NONSTDC_NO_WARNINGS)
# 4503 The decorated name was longer than the compiler limit (4096), and was truncated.
ADD_COMPILE_OPTIONS(/wd4068 /wd4101 /wd4102 /wd4244 /wd4258 /wd4355 /wd4996 /wd4503) # Disables warning messages listed above
ADD_DEFINITIONS(/DNOMINMAX) # Avoid build errors due to STL/Windows min-max conflicts
ADD_DEFINITIONS(/DWIN32_LEAN_AND_MEAN) # Excludes rarely used services and headers from compilation
# ADD_CXX_DEFINITIONS("-d2SSAOptimizer-") # this disables this optimizer which has known major issues
# ADDITIONAL RELEASE-MODE-SPECIFIC FLAGS
ADD_CXX_RELEASE_DEFINITIONS("/GS-") # Disable buffer overrun checks for performance in release mode
# ADDITIONAL DEBUG-MODE-SPECIFIC FLAGS
ADD_CXX_DEBUG_DEFINITIONS("/Ob0") # Disable inlining
ADD_CXX_DEBUG_DEFINITIONS("/RTCsu") # Runtime checks
ADD_CXX_DEBUG_DEFINITIONS("/fp:strict") # Floating point model
ADD_CXX_DEBUG_DEFINITIONS("/DMSVC_DEBUG") # Triggers code in main.cc to catch floating point NaNs
ELSEIF ( CMAKE_COMPILER_IS_GNUCXX OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang" ) # g++/Clang
option(ENABLE_THREAD_SANITIZER "Enable thread sanitizer testing in gcc/clang" FALSE)
set(LINKER_FLAGS "")
if (ENABLE_THREAD_SANITIZER)
ADD_CXX_DEFINITIONS("-fsanitize=thread")
ADD_DEFINITIONS("-ggdb -fno-omit-frame-pointer")
set(LINKER_FLAGS "${LINKER_FLAGS} -fsanitize=thread -ggdb")
endif()
option(ENABLE_ADDRESS_SANITIZER "Enable address sanitizer testing in gcc/clang" FALSE)
if (ENABLE_ADDRESS_SANITIZER)
ADD_CXX_DEFINITIONS("-fsanitize=address")
ADD_DEFINITIONS("-ggdb -fno-omit-frame-pointer")
set(LINKER_FLAGS "${LINKER_FLAGS} -fsanitize=address -ggdb")
endif()
option(ENABLE_MEMORY_SANITIZER "Enable reads of unintialized memory sanitizer testing in gcc/clang" FALSE)
if (ENABLE_MEMORY_SANITIZER)
ADD_CXX_DEFINITIONS("-fsanitize=memory")
ADD_DEFINITIONS("-ggdb -fno-omit-frame-pointer")
set(LINKER_FLAGS "${LINKER_FLAGS} -fsanitize=memory -ggdb")
endif()
option(ENABLE_UNDEFINED_SANITIZER "Enable undefined behavior sanitizer testing in gcc/clang" FALSE)
if (ENABLE_UNDEFINED_SANITIZER)
ADD_CXX_DEFINITIONS("-fsanitize=undefined")
ADD_DEFINITIONS("-ggdb -fno-omit-frame-pointer")
set(LINKER_FLAGS "${LINKER_FLAGS} -fsanitize=undefined -ggdb")
endif()
option(ENABLE_COVERAGE "Enable Coverage Reporting in GCC" FALSE)
if (ENABLE_COVERAGE)
ADD_DEFINITIONS("--coverage -O0")
set(LINKER_FLAGS "${LINKER_FLAGS} --coverage")
endif()
mark_as_advanced(ENABLE_THREAD_SANITIZER ENABLE_ADDRESS_SANITIZER ENABLE_UNDEFINED_SANITIZER)
if(CMAKE_HOST_UNIX)
if(NOT APPLE)
set(LINKER_FLAGS "${LINKER_FLAGS} -pthread")
ADD_DEFINITIONS("-pthread")
endif()
endif()
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${LINKER_FLAGS}")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${LINKER_FLAGS}")
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${LINKER_FLAGS}")
# COMPILER FLAGS
ADD_CXX_DEFINITIONS("-pipe") # Faster compiler processing
ADD_CXX_DEFINITIONS("-pedantic") # Turn on warnings about constructs/situations that may be non-portable or outside of the standard
ADD_CXX_DEFINITIONS("-Wall -Wextra") # Turn on warnings
ADD_CXX_DEFINITIONS("-Wno-unknown-pragmas")
if (CMAKE_COMPILER_IS_GNUCXX AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 9.0)
ADD_CXX_DEFINITIONS("-Wno-deprecated-copy")
endif()
ADD_CXX_DEFINITIONS("-Wno-attributes") # Don't warn on attributes Clang doesn't know
ADD_CXX_DEFINITIONS("-Wno-delete-non-virtual-dtor")
ADD_CXX_DEFINITIONS("-Wno-missing-braces")
if ( CMAKE_COMPILER_IS_GNUCXX ) # g++
ADD_CXX_DEFINITIONS("-Wno-unused-but-set-parameter -Wno-unused-but-set-variable") # Suppress unused-but-set warnings until more serious ones are addressed
ADD_CXX_DEFINITIONS("-Wno-maybe-uninitialized")
ADD_CXX_DEFINITIONS("-Wno-aggressive-loop-optimizations")
elseif( "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang")
ADD_CXX_DEFINITIONS("-Wno-vexing-parse")
ADD_CXX_DEFINITIONS("-Wno-invalid-source-encoding")
endif()
# ADDITIONAL GCC-SPECIFIC FLAGS
if ( CMAKE_COMPILER_IS_GNUCXX ) # g++
ADD_CXX_DEBUG_DEFINITIONS("-ffloat-store") # Improve debug run solution stability
ADD_CXX_DEBUG_DEFINITIONS("-fsignaling-nans") # Disable optimizations that may have concealed NaN behavior
ADD_CXX_DEBUG_DEFINITIONS("-D_GLIBCXX_DEBUG") # Standard container debug mode (bounds checking, ...)
# ADD_CXX_RELEASE_DEFINITIONS("-finline-limit=2000") # More aggressive inlining This is causing unit test failures on Ubuntu 14.04
endif()
ADD_CXX_DEBUG_DEFINITIONS("-ggdb") # Produces debugging information specifically for gdb
ADD_CXX_RELEASE_DEFINITIONS("-fno-stack-protector")
# ADD_CXX_RELEASE_DEFINITIONS("-Ofast") # -Ofast (or -ffast-math) needed to auto-vectorize floating point loops
ELSEIF ( WIN32 AND "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel" )
# Disabled Warnings: Enable some of these as more serious warnings are addressed
# 161 Unrecognized pragma
# 177 Variable declared but never referenced
# 488 Template parameter not used ...
# 809 Exception specification consistency warnings that fire in gtest code
# 869 Parameter never referenced
# 1786 Use of deprecated items
# 2259 Non-pointer conversions may lose significant bits
# 3280 Declaration hides variable
# 10382 xHOST remark
# 11074 Inlining inhibited
# 11075 Inlining inhibited
# COMPILER FLAGS
ADD_CXX_DEFINITIONS("/nologo") # Skip banner text
ADD_CXX_DEFINITIONS("/Qcxx-features") # Enables standard C++ features without disabling Microsoft extensions
ADD_CXX_DEFINITIONS("/Wall") # Enable "all" warnings
ADD_CXX_DEFINITIONS("/Qdiag-disable:161,177,488,809,869,1786,2259,3280,10382,11074,11075") # Disable warnings listed above
ADD_CXX_DEFINITIONS("/DNOMINMAX") # Avoid build errors due to STL/Windows min-max conflicts
ADD_CXX_DEFINITIONS("/DWIN32_LEAN_AND_MEAN") # Excludes rarely used services and headers from compilation
# Optimization options that had no significant benefit for EnergyPlus
# /Qipo instead of /Qip
# /Qopt-prefetch
# /Qparallel
# /Qunroll-aggressive
# ADDITIONAL RELEASE-MODE-SPECIFIC FLAGS
ADD_CXX_RELEASE_DEFINITIONS("/O3") # Agressive optimization
ADD_CXX_RELEASE_DEFINITIONS("/Qprec-div-") # Faster division
ADD_CXX_RELEASE_DEFINITIONS("/Qansi-alias") # Better optimization via strict aliasing rules
ADD_CXX_RELEASE_DEFINITIONS("/Qip") # Inter-procedural optimnization within a single file
ADD_CXX_RELEASE_DEFINITIONS("/Qinline-factor:225") # Aggressive inlining
# ADD_CXX_RELEASE_DEFINITIONS("/fp:fast=2") # Aggressive optimizations on floating-point data
# ADDITIONAL DEBUG-MODE-SPECIFIC FLAGS
ADD_CXX_DEBUG_DEFINITIONS("/fp:source") # Use source-specified floating point precision
ADD_CXX_DEBUG_DEFINITIONS("/Qtrapuv") # Initialize local variables to unusual values to help detect use uninitialized
ADD_CXX_DEBUG_DEFINITIONS("/check:stack,uninit") # Enables runtime checking of the stack (buffer over and underruns; pointer verification) and uninitialized variables
ADD_CXX_DEBUG_DEFINITIONS("/Gs0") # Enable stack checking for all functions
ADD_CXX_DEBUG_DEFINITIONS("/GS") # Buffer overrun detection
ADD_CXX_DEBUG_DEFINITIONS("/Qfp-stack-check") # Tells the compiler to generate extra code after every function call to ensure fp stack is as expected
ADD_CXX_DEBUG_DEFINITIONS("/traceback") # Enables traceback on error
ELSEIF ( UNIX AND "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel" )
# Disabled Warnings: Enable some of these as more serious warnings are addressed
# 161 Unrecognized pragma
# 177 Variable declared but never referenced
# 488 Template parameter not used ...
# 809 Exception specification consistency warnings that fire in gtest code
# 869 Parameter never referenced
# 1786 Use of deprecated items
# 2259 Non-pointer conversions may lose significant bits
# 3280 Declaration hides variable
# 10382 xHOST remark
# 11074 Inlining inhibited
# 11075 Inlining inhibited
# COMPILER FLAGS
ADD_CXX_DEFINITIONS("-Wall") # Enable "all" warnings
ADD_CXX_DEFINITIONS("-diag-disable:161,177,488,809,869,1786,2259,3280,10382,11074,11075") # Disable warnings listed above
IF(NOT APPLE)
ADD_CXX_DEFINITIONS(-pthread)
ENDIF()
# Optimization options that had no significant benefit for EnergyPlus
# -ipo instead of -ip
# -opt-prefetch
# -parallel
# -unroll-aggressive
# ADDITIONAL RELEASE-MODE-SPECIFIC FLAGS
ADD_CXX_RELEASE_DEFINITIONS("-O3") # Agressive optimization
# ADD_CXX_RELEASE_DEFINITIONS("-Ofast") # More aggressive optimizations (instead of -O3) (enables -no-prec-div and -fp-model fast=2)
ADD_CXX_RELEASE_DEFINITIONS("-no-prec-div") # Faster division (enabled by -Ofast)
ADD_CXX_RELEASE_DEFINITIONS("-ansi-alias") # Enables more aggressive optimizations on floating-point data
ADD_CXX_RELEASE_DEFINITIONS("-ip") # Enables inter-procedural optimnization within a single file
ADD_CXX_RELEASE_DEFINITIONS("-inline-factor=225") # Enables more aggressive inlining
# ADDITIONAL DEBUG-MODE-SPECIFIC FLAGS
ADD_CXX_DEBUG_DEFINITIONS("-strict-ansi") # Strict language conformance: Performance impact so limit to debug build
ADD_CXX_DEBUG_DEFINITIONS("-fp-model source") # Use source-specified floating point precision
ADD_CXX_DEBUG_DEFINITIONS("-ftrapuv") # Initialize local variables to unusual values to help detect use uninitialized
ADD_CXX_DEBUG_DEFINITIONS("-check=stack,uninit") # Enables runtime checking of the stack (buffer over and underruns; pointer verification) and uninitialized variables
ADD_CXX_DEBUG_DEFINITIONS("-fstack-security-check") # Buffer overrun detection
ADD_CXX_DEBUG_DEFINITIONS("-fp-stack-check") # Check the floating point stack after every function call
ADD_CXX_DEBUG_DEFINITIONS("-traceback") # Enables traceback on error
ENDIF () # COMPILER TYPE
# Add Color Output if Using Ninja:
# Wave to do it before the folders are imported etc (here is the perfect place)
# We use "add_compile_options" instead of just appending to CXX_FLAGS
# That way it'll work for pretty much everything including Fortran stuff
macro(AddFlagIfSupported flag test)
CHECK_CXX_COMPILER_FLAG(${flag} ${test})
if( ${${test}} )
message(STATUS "Adding ${flag}")
# On Mac with Ninja (kitware binary for fortran support) and brew gfortran, I get build errors due to this flag.
if(("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang") AND BUILD_FORTRAN)
add_compile_options($<$<NOT:$<COMPILE_LANGUAGE:Fortran>>:${flag}>)
else()
add_compile_options("${flag}")
endif()
else()
message(STATUS "Flag ${flag} isn't supported")
endif()
endmacro()
if("Ninja" STREQUAL ${CMAKE_GENERATOR})
include(CheckCXXCompilerFlag)
# Clang
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang")
AddFlagIfSupported(-fcolor-diagnostics COMPILER_SUPPORTS_fdiagnostics_color)
endif()
# g++
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
AddFlagIfSupported(-fdiagnostics-color=always COMPILER_SUPPORTS_fdiagnostics_color)
# On some older gcc, it doesn't say that it's supported, but it works anyways
if (NOT COMPILER_SUPPORTS_fdiagnostics_color)
message(STATUS "Forcing -fdiagnostics-color=always")
add_compile_options (-fdiagnostics-color=always)
endif()
endif()
endif()
# Xcode/Ninja generators undefined MAKE
if(CMAKE_GENERATOR MATCHES "Make")
set(MAKE "$(MAKE)")
else()
set(MAKE make)
endif()