-
Notifications
You must be signed in to change notification settings - Fork 58
/
SetupCompilerOptions.cmake
394 lines (329 loc) · 15.7 KB
/
SetupCompilerOptions.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
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and
# other BLT Project Developers. See the top-level LICENSE file for details
#
# SPDX-License-Identifier: (BSD-3-Clause)
############################
# Setup compiler options
############################
#####################################################
# Set some variables to simplify determining compiler
# Compiler string list from:
# https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_COMPILER_ID.html
####################################################3
# use CMAKE_BUILD_TOOL to identify visual studio
# and CMAKE_CXX_COMPILER_ID for all other cases
if("${CMAKE_BUILD_TOOL}" MATCHES "(msdev|devenv|nmake|MSBuild)")
set(COMPILER_FAMILY_IS_MSVC 1)
message(STATUS "Compiler family is MSVC")
if(CMAKE_GENERATOR_TOOLSET AND "${CMAKE_GENERATOR_TOOLSET}" MATCHES "Intel")
set(COMPILER_FAMILY_IS_MSVC_INTEL 1)
message(STATUS "Toolset is ${CMAKE_GENERATOR_TOOLSET}")
endif()
else()
#Determine C/C++ compiler family.
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
set(C_COMPILER_FAMILY_IS_GNU 1)
message(STATUS "C Compiler family is GNU")
elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") # For Clang or AppleClang
set(C_COMPILER_FAMILY_IS_CLANG 1)
message(STATUS "C Compiler family is Clang")
elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "XL")
set(C_COMPILER_FAMILY_IS_XL 1)
message(STATUS "C Compiler family is XL")
elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
set(C_COMPILER_FAMILY_IS_INTEL 1)
message(STATUS "C Compiler family is Intel")
elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "IntelLLVM")
set(C_COMPILER_FAMILY_IS_INTELLLVM 1)
message(STATUS "C Compiler family is IntelLLVM")
elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "PGI")
set(C_COMPILER_FAMILY_IS_PGI 1)
message(STATUS "C Compiler family is PGI")
elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Cray")
set(C_COMPILER_FAMILY_IS_CRAY 1)
message(STATUS "C Compiler family is Cray")
else()
message(STATUS "C Compiler family not set!!!")
endif()
# Determine Fortran compiler family
if("${CMAKE_Fortran_COMPILER_ID}" STREQUAL "GNU")
set(Fortran_COMPILER_FAMILY_IS_GNU 1)
message(STATUS "Fortran Compiler family is GNU")
elseif("${CMAKE_Fortran_COMPILER_ID}" MATCHES "Clang") # For Clang or AppleClang
set(Fortran_COMPILER_FAMILY_IS_CLANG 1)
message(STATUS "Fortran Compiler family is Clang")
elseif("${CMAKE_Fortran_COMPILER_ID}" MATCHES "Flang") # For Flang compilers
set(Fortran_COMPILER_FAMILY_IS_CLANG 1 CACHE BOOL "")
message(STATUS "Fortran Compiler family is Clang")
elseif("${CMAKE_Fortran_COMPILER_ID}" STREQUAL "XL")
set(Fortran_COMPILER_FAMILY_IS_XL 1)
message(STATUS "Fortran Compiler family is XL")
elseif("${CMAKE_Fortran_COMPILER_ID}" STREQUAL "Intel")
set(Fortran_COMPILER_FAMILY_IS_INTEL 1)
message(STATUS "Fortran Compiler family is Intel")
elseif("${CMAKE_Fortran_COMPILER_ID}" STREQUAL "IntelLLVM")
set(Fortran_COMPILER_FAMILY_IS_INTELLLVM 1)
message(STATUS "Fortran Compiler family is IntelLLVM")
elseif("${CMAKE_Fortran_COMPILER_ID}" STREQUAL "PGI")
set(Fortran_COMPILER_FAMILY_IS_PGI 1)
message(STATUS "Fortran Compiler family is PGI")
elseif("${CMAKE_Fortran_COMPILER_ID}" STREQUAL "Cray")
set(Fortran_COMPILER_FAMILY_IS_CRAY 1)
message(STATUS "Fortran Compiler family is Cray")
elseif(ENABLE_FORTRAN)
message(STATUS "Fortran Compiler family not set!!!")
endif()
endif()
#################################################################################
# PGI doesn't support a SYSTEM flag for include directories. Since this is CMake's
# default for imported targets, we need to disable this feature for PGI.
#################################################################################
if(${C_COMPILER_FAMILY_IS_PGI})
set(CMAKE_NO_SYSTEM_FROM_IMPORTED TRUE)
endif()
################################################
# Support for extra compiler flags and defines
################################################
message(STATUS "Adding optional BLT definitions and compiler flags")
####################################################
# create relocatable static libs by default
####################################################
set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
##############################################
# Support extra definitions for all targets
##############################################
if(BLT_DEFINES)
add_definitions(${BLT_DEFINES})
message(STATUS "Added \"${BLT_DEFINES}\" to definitions")
endif()
if(COMPILER_FAMILY_IS_MSVC)
# Visual studio can give a warning that /bigobj is required due to the size of some object files
set( BLT_CXX_FLAGS "${BLT_CXX_FLAGS} /bigobj" )
set( BLT_C_FLAGS "${BLT_C_FLAGS} /bigobj" )
endif()
##########################################
# If set, BLT_<LANG>_FLAGS are added to
# all targets that use <LANG>-Compiler
##########################################
if(BLT_C_FLAGS)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${BLT_C_FLAGS}")
message(STATUS "Updated CMAKE_C_FLAGS to \"${CMAKE_C_FLAGS}\"")
endif()
if(BLT_CXX_FLAGS)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${BLT_CXX_FLAGS}")
message(STATUS "Updated CMAKE_CXX_FLAGS to \"${CMAKE_CXX_FLAGS}\"")
endif()
if(ENABLE_FORTRAN AND BLT_FORTRAN_FLAGS)
set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} ${BLT_FORTRAN_FLAGS}")
message(STATUS "Updated CMAKE_Fortran_FLAGS to \"${CMAKE_Fortran_FLAGS}\"")
endif()
if(ENABLE_CUDA AND BLT_CUDA_FLAGS)
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} ${BLT_CUDA_FLAGS}")
message(STATUS "Updated CMAKE_CUDA_FLAGS to \"${CMAKE_CUDA_FLAGS}\"")
endif()
if(BLT_EXE_LINKER_FLAGS)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${BLT_EXE_LINKER_FLAGS}")
message(STATUS "Updated CMAKE_EXE_LINKER_FLAGS to \"${CMAKE_EXE_LINKER_FLAGS}\"")
endif()
###############################################################
# Support extra flags based on CMake configuration type
###############################################################
#
# We guard this approach to avoid issues with CMake generators
# that support multiple configurations, like Visual Studio.
#
###############################################################
if(NOT CMAKE_CONFIGURATION_TYPES)
set(cfg_types DEBUG RELEASE RELWITHDEBINFO MINSIZEREL)
foreach(cfg_type in ${cfg_types})
# flags for the C compiler
if(BLT_C_FLAGS_${cfg_type})
set(CMAKE_C_FLAGS_${cfg_type}
"${CMAKE_C_FLAGS_${cfg_type}} ${BLT_C_FLAGS_${cfg_type}}")
message(STATUS "Updated CMAKE_C_FLAGS_${cfg_type} to \"${CMAKE_C_FLAGS_${cfg_type}}\"")
endif()
# flags for the C++ compiler
if(BLT_CXX_FLAGS_${cfg_type})
set(CMAKE_CXX_FLAGS_${cfg_type}
"${CMAKE_CXX_FLAGS_${cfg_type}} ${BLT_CXX_FLAGS_${cfg_type}}")
message(STATUS "Updated CMAKE_CXX_FLAGS_${cfg_type} to \"${CMAKE_CXX_FLAGS_${cfg_type}}\"")
endif()
# flags for the Fortran compiler
if(ENABLE_FORTRAN AND BLT_FORTRAN_FLAGS_${cfg_type})
set(CMAKE_Fortran_FLAGS_${cfg_type}
"${CMAKE_Fortran_FLAGS_${cfg_type}} ${BLT_FORTRAN_FLAGS_${cfg_type}}")
message(STATUS "Updated CMAKE_Fortran_FLAGS_${cfg_type} to \"${CMAKE_Fortran_FLAGS_${cfg_type}}\"")
endif()
endforeach()
endif()
################################
# RPath Settings
################################
# only apply rpath settings for builds using shared libs
if(BUILD_SHARED_LIBS)
# use, i.e. don't skip the full RPATH for the build tree
set(CMAKE_SKIP_BUILD_RPATH FALSE)
# when building, don't use the install RPATH already
# (but later on when installing)
set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
set(CMAKE_INSTALL_NAME_DIR "${CMAKE_INSTALL_PREFIX}/lib")
# add the automatically determined parts of the RPATH
# which point to directories outside the build tree to the install RPATH
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
# the RPATH to be used when installing, but only if it's not a system directory
list(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${CMAKE_INSTALL_PREFIX}/lib" isSystemDir)
if("${isSystemDir}" STREQUAL "-1")
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
endif()
endif()
##################################################################
# Additional compiler warnings and treatment of warnings as errors
##################################################################
blt_append_custom_compiler_flag(
FLAGS_VAR BLT_ENABLE_ALL_WARNINGS_C_FLAG
DEFAULT "-Wall -Wextra"
CLANG "-Wall -Wextra"
# Additional possibilities for clang include:
# "-Wdocumentation -Wdeprecated -Weverything"
PGI "-Minform=warn"
MSVC "/W4"
# Additional possibilities for visual studio include:
# "/Wall /wd4619 /wd4668 /wd4820 /wd4571 /wd4710"
XL " " # qinfo=<grp> produces additional messages on XL
# qflag=<x>:<x> defines min severity level to produce messages on XL
# where x is i info, w warning, e error, s severe; default is:
# (default is qflag=i:i)
)
blt_append_custom_compiler_flag(
FLAGS_VAR BLT_ENABLE_ALL_WARNINGS_CXX_FLAG
DEFAULT "-Wall -Wextra"
CLANG "-Wall -Wextra"
# Additional possibilities for clang include:
# "-Wdocumentation -Wdeprecated -Weverything"
PGI "-Minform=warn"
MSVC "/W4"
# Additional possibilities for visual studio include:
# "/Wall /wd4619 /wd4668 /wd4820 /wd4571 /wd4710"
XL " " # qinfo=<grp> produces additional messages on XL
# qflag=<x>:<x> defines min severity level to produce messages on XL
# where x is i info, w warning, e error, s severe; default is:
# (default is qflag=i:i)
)
blt_append_custom_compiler_flag(
FLAGS_VAR BLT_WARNINGS_AS_ERRORS_CXX_FLAG
DEFAULT "-Werror"
MSVC "/WX"
XL "-qhalt=w"
)
blt_append_custom_compiler_flag(
FLAGS_VAR BLT_WARNINGS_AS_ERRORS_C_FLAG
DEFAULT "-Werror"
MSVC "/WX"
PGI " "
XL "-qhalt=w"
)
#
# Modify flags to avoid static linking runtime issues on MS Windows.
#
# When building on Windows, you can link in the runtime library
# - statically (with /MT), or
# - dynamically (with /MD).
# See https://docs.microsoft.com/en-us/cpp/build/reference/md-mt-ld-use-run-time-library?view=vs-2019.
#
# Mixing /MD with /MT can cause linking errors. CMake specifies
# /MD when generating project files for MSVC and provides no way to
# change this. This can be a problem with Google Test in particular,
# which when building statically replaces all /MD with /MT. HDF5, on
# the other hand, sternly warns against the use of /MT, even when
# built statically.
#
# See https://gitlab.kitware.com/cmake/community/wikis/FAQ#dynamic-replace.
# Once we require CMake >= 3.15 we can address the issue differently, using
# CMAKE_MSVC_RUNTIME_LIBRARY:
# https://cmake.org/cmake/help/latest/variable/CMAKE_MSVC_RUNTIME_LIBRARY.html
#
if ( COMPILER_FAMILY_IS_MSVC AND NOT BUILD_SHARED_LIBS )
if ( BLT_ENABLE_MSVC_STATIC_MD_TO_MT )
foreach(_lang C CXX)
foreach(_build
FLAGS FLAGS_DEBUG FLAGS_RELEASE
FLAGS_MINSIZEREL FLAGS_RELWITHDEBINFO)
set(_flag CMAKE_${_lang}_${_build})
if(${_flag} MATCHES "/MD")
string(REGEX REPLACE "/MD" "/MT" ${_flag} "${${_flag}}")
endif()
endforeach()
endforeach()
elseif (ENABLE_GTEST)
message(FATAL_ERROR
"For static linking with MS Visual Studio using GTEST, you must set BLT_ENABLE_MSVC_STATIC_MD_TO_MT to ON in order to enable changing /MD to /MT.")
endif()
endif()
set(langFlags "CMAKE_C_FLAGS" "CMAKE_CXX_FLAGS")
if (ENABLE_ALL_WARNINGS)
message(STATUS "Enabling all compiler warnings on all targets.")
set (CMAKE_CXX_FLAGS "${BLT_ENABLE_ALL_WARNINGS_CXX_FLAG} ${CMAKE_CXX_FLAGS}")
set (CMAKE_C_FLAGS "${BLT_ENABLE_ALL_WARNINGS_C_FLAG} ${CMAKE_C_FLAGS}")
endif()
if (ENABLE_WARNINGS_AS_ERRORS)
message(STATUS "Enabling treatment of warnings as errors on all targets.")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${BLT_WARNINGS_AS_ERRORS_CXX_FLAG}")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${BLT_WARNINGS_AS_ERRORS_C_FLAG}")
endif()
################################
# Enable Fortran
################################
if(ENABLE_FORTRAN)
# if enabled but no fortran compiler, halt the configure
if(CMAKE_Fortran_COMPILER)
message(STATUS "Fortran support enabled.")
else()
message(FATAL_ERROR "Fortran support selected, but no Fortran compiler was found.")
endif()
list(APPEND langFlags "CMAKE_Fortran_FLAGS")
else()
message(STATUS "Fortran support disabled.")
endif()
###################################
# Output compiler and linker flags
###################################
foreach(flagVar ${langFlags} "CMAKE_EXE_LINKER_FLAGS" )
message(STATUS "${flagVar} flags are: ${${flagVar}}")
endforeach()
##################################
# Remove implicit link directories
##################################
if(BLT_CMAKE_IMPLICIT_LINK_DIRECTORIES_EXCLUDE)
message(STATUS "Removing implicit link directories: ${BLT_CMAKE_IMPLICIT_LINK_DIRECTORIES_EXCLUDE}")
list(REMOVE_ITEM CMAKE_CXX_IMPLICIT_LINK_DIRECTORIES ${BLT_CMAKE_IMPLICIT_LINK_DIRECTORIES_EXCLUDE})
message(STATUS "Updated CXX implicit Link Directories: ${CMAKE_CXX_IMPLICIT_LINK_DIRECTORIES}")
list(REMOVE_ITEM CMAKE_C_IMPLICIT_LINK_DIRECTORIES ${BLT_CMAKE_IMPLICIT_LINK_DIRECTORIES_EXCLUDE})
message(STATUS "Updated C implicit Link Directories: ${CMAKE_C_IMPLICIT_LINK_DIRECTORIES}")
if (ENABLE_FORTRAN)
list(REMOVE_ITEM CMAKE_Fortran_IMPLICIT_LINK_DIRECTORIES ${BLT_CMAKE_IMPLICIT_LINK_DIRECTORIES_EXCLUDE})
message(STATUS "Updated Fortran implicit Link Directories: ${CMAKE_Fortran_IMPLICIT_LINK_DIRECTORIES}")
endif ()
if (ENABLE_CUDA)
list(REMOVE_ITEM CMAKE_CUDA_IMPLICIT_LINK_DIRECTORIES ${BLT_CMAKE_IMPLICIT_LINK_DIRECTORIES_EXCLUDE})
message(STATUS "Updated CUDA implicit Link Directories: ${CMAKE_CUDA_IMPLICIT_LINK_DIRECTORIES}")
endif ()
endif()
##################################
# Remove implicit link libraries
##################################
if(BLT_CMAKE_IMPLICIT_LINK_LIBRARIES_EXCLUDE)
message(STATUS "Removing implicit link libraries: ${BLT_CMAKE_IMPLICIT_LINK_LIBRARIES_EXCLUDE}")
list(REMOVE_ITEM CMAKE_CXX_IMPLICIT_LINK_LIBRARIES ${BLT_CMAKE_IMPLICIT_LINK_LIBRARIES_EXCLUDE})
message(STATUS "Updated CXX implicit Link Libraries: ${CMAKE_CXX_IMPLICIT_LINK_LIBRARIES}")
list(REMOVE_ITEM CMAKE_C_IMPLICIT_LINK_LIBRARIES ${BLT_CMAKE_IMPLICIT_LINK_LIBRARIES_EXCLUDE})
message(STATUS "Updated C implicit Link Libraries: ${CMAKE_C_IMPLICIT_LINK_LIBRARIES}")
if (ENABLE_FORTRAN)
list(REMOVE_ITEM CMAKE_Fortran_IMPLICIT_LINK_LIBRARIES ${BLT_CMAKE_IMPLICIT_LINK_LIBRARIES_EXCLUDE})
message(STATUS "Updated Fortran implicit Link Libraries: ${CMAKE_Fortran_IMPLICIT_LINK_LIBRARIES}")
endif ()
if (ENABLE_CUDA)
list(REMOVE_ITEM CMAKE_CUDA_IMPLICIT_LINK_LIBRARIES ${BLT_CMAKE_IMPLICIT_LINK_LIBRARIES_EXCLUDE})
message(STATUS "Updated CUDA implicit Link Libraries: ${CMAKE_CUDA_IMPLICIT_LINK_LIBRARIES}")
endif ()
endif()