forked from rraju1/cmake-buildscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CompilerFlags.cmake
436 lines (327 loc) · 14.5 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
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# File which figures out the compiler flags to use based on the vendor and version of each compiler
#Note: must be included after OpenMPConfig, MPIConfig, and PythonConfig
#-------------------------------------------------------------------------------
# Handle CMake fortran compiler version issue
# See https://cmake.org/Bug/view.php?id=15372
#-------------------------------------------------------------------------------
if(CMAKE_Fortran_COMPILER_LOADED AND "${CMAKE_Fortran_COMPILER_VERSION}" STREQUAL "")
set(CMAKE_Fortran_COMPILER_VERSION ${CMAKE_C_COMPILER_VERSION} CACHE STRING "Fortran compiler version. May not be autodetected correctly on older CMake versions, fix this if it's wrong." FORCE)
message(FATAL_ERROR "Your CMake is too old to properly detect the Fortran compiler version. It is assumed to be the same as your C compiler version, ${CMAKE_C_COMPILER_VERSION}. If this is not correct, pass -DCMAKE_Fortran_COMPILER_VERSION=<correct version> to cmake. If it is correct,just run the configuration again.")
endif()
# create linker flags
# On Windows undefined symbols in shared libraries produce errors.
# This makes them do that on Linux too.
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${NO_UNDEFINED_FLAG}")
#-------------------------------------------------------------------------------
# Set default flags
#-------------------------------------------------------------------------------
set(NO_OPT_FFLAGS -O0)
set(NO_OPT_CFLAGS -O0)
set(NO_OPT_CXXFLAGS -O0)
set(OPT_FFLAGS -O3)
set(OPT_CFLAGS -O3)
set(OPT_CXXFLAGS -O3)
set(CMAKE_C_FLAGS_DEBUG "-g")
set(CMAKE_CXX_FLAGS_DEBUG "-g")
set(CMAKE_Fortran_FLAGS_DEBUG "-g")
#blank cmake's default optimization flags, we can't use these because not everything should be built optimized.
set(CMAKE_C_FLAGS_RELEASE "")
set(CMAKE_CXX_FLAGS_RELEASE "")
set(CMAKE_Fortran_FLAGS_RELEASE "")
#a macro to make things a little cleaner
#NOTE: we can't use add_compile_options because that will apply to all languages
macro(add_flags LANGUAGE) # FLAGS...
foreach(FLAG ${ARGN})
set(CMAKE_${LANGUAGE}_FLAGS "${CMAKE_${LANGUAGE}_FLAGS} ${FLAG}")
endforeach()
endmacro(add_flags)
#-------------------------------------------------------------------------------
# Now, the If Statements of Doom...
#-------------------------------------------------------------------------------
#gcc
#---------------------------------------------------------------------------------------------------------------------------------------------------------------------
if("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU")
add_flags(C -Wall -Wno-unused-function -Wno-unknown-pragmas)
if(NOT UNUSED_WARNINGS)
add_flags(C -Wno-unused-variable -Wno-unused-but-set-variable)
endif()
if(NOT UNINITIALIZED_WARNINGS)
add_flags(C -Wno-uninitialized -Wno-maybe-uninitialized)
endif()
if(${CMAKE_C_COMPILER_VERSION} VERSION_GREATER 4.1)
if(SSE)
if(TARGET_ARCH STREQUAL x86_64)
#-mfpmath=sse is default for x86_64, no need to specific it
set(OPT_CFLAGS ${OPT_CFLAGS} "-mtune=native")
else() # i386 needs to be told to use sse prior to using -mfpmath=sse
set(OPT_CFLAGS "${OPT_CFLAGS} -mtune=native -msse -mfpmath=sse")
endif()
endif()
endif()
if(DRAGONEGG)
#check dragonegg
check_c_compiler_flag(-fplugin=${DRAGONEGG} DRAGONEGG_C_WORKS)
if(NOT DRAGONEGG_C_WORKS)
message(FATAL_ERROR "Can't use C compiler with Dragonegg. Please fix whatever's broken. Check CMakeOutput.log for details.")
endif()
add_flags(C -fplugin=${DRAGONEGG})
endif()
endif()
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
add_flags(CXX -Wall -Wno-unused-function -Wno-unknown-pragmas)
# Kill it! Kill it with fire!
check_cxx_compiler_flag(-Wno-unused-local-typedefs SUPPORTS_WNO_UNUSED_LOCAL_TYPEDEFS)
if(SUPPORTS_WNO_UNUSED_LOCAL_TYPEDEFS)
add_flags(CXX -Wno-unused-local-typedefs)
endif()
if(NOT UNUSED_WARNINGS)
add_flags(CXX -Wno-unused-variable -Wno-unused-but-set-variable)
endif()
if(NOT UNINITIALIZED_WARNINGS)
add_flags(CXX -Wno-uninitialized -Wno-maybe-uninitialized)
endif()
if(${CMAKE_CXX_COMPILER_VERSION} VERSION_GREATER 4.1)
if(SSE)
if(TARGET_ARCH STREQUAL x86_64)
#-mfpmath=sse is default for x86_64, no need to specific it
set(OPT_CXXFLAGS ${OPT_CXXFLAGS} "-mtune=native")
else() # i386 needs to be told to use sse prior to using -mfpmath=sse
set(OPT_CXXFLAGS "${OPT_CXXFLAGS} -mtune=native -msse -mfpmath=sse")
endif()
endif()
endif()
if(DRAGONEGG)
#check dragonegg
check_cxx_compiler_flag(-fplugin=${DRAGONEGG} DRAGONEGG_CXX_WORKS)
if(NOT DRAGONEGG_CXX_WORKS)
message(FATAL_ERROR "Can't use C++ compiler with Dragonegg. Please fix whatever's broken. Check CMakeOutput.log for details.")
endif()
add_flags(CXX -fplugin=${DRAGONEGG})
endif()
endif()
if("${CMAKE_Fortran_COMPILER_ID}" STREQUAL "GNU")
add_flags(Fortran -Wall -Wno-tabs -Wno-unused-function -ffree-line-length-none)
if("${CMAKE_Fortran_COMPILER_VERSION}" VERSION_GREATER 4.1)
add_flags(Fortran -Wno-unused-dummy-argument)
endif()
if(NOT UNUSED_WARNINGS)
add_flags(Fortran -Wno-unused-variable)
endif()
if(NOT UNINITIALIZED_WARNINGS)
add_flags(Fortran -Wno-maybe-uninitialized)
endif()
if("${CMAKE_Fortran_COMPILER_VERSION}" VERSION_GREATER 4.1)
if(SSE)
if(TARGET_ARCH STREQUAL x86_64)
#-mfpmath=sse is default for x86_64, no need to specific it
set(OPT_FFLAGS ${OPT_FFLAGS} -mtune=native)
else() # i386 needs to be told to use sse prior to using -mfpmath=sse
set(OPT_FFLAGS ${OPT_FFLAGS} -mtune=native -msse -mfpmath=sse)
endif()
endif()
endif()
# gcc 4.1.2 does not support putting allocatable arrays in a Fortran type...
# so unfortunately file-less prmtop support in the sander API will not work
# in this case.
if("${CMAKE_Fortran_COMPILER_VERSION}" VERSION_LESS 4.2)
add_definitions(-DNO_ALLOCATABLES_IN_TYPE)
endif()
# Check dragonegg
if(DRAGONEGG)
#TODO: write check_fortran_compiler_flag
#check_fortran_compiler_flag(-fplugin=${DRAGONEGG} DRAGONEGG_FORTRAN_WORKS)
#if(NOT DRAGONEGG_FORTRAN_WORKS)
# message(FATAL_ERROR "Can't use Fortran compiler with Dragonegg. Please fix whatever's broken. Check CMakeOutput.log for details.")
#endif()
add_flags(Fortran -fplugin=${DRAGONEGG})
endif()
endif()
#clang
#---------------------------------------------------------------------------------------------------------------------------------------------------------------------
if("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_C_COMPILER_ID}" STREQUAL "AppleClang")
add_flags(C -Wall -Wno-unused-function)
list(APPEND OPT_CFLAGS "-mtune=native")
#if we are crosscompiling and using clang, tell CMake this
if(CROSSCOMPILE)
set(CMAKE_C_COMPILER_TARGET ${TARGET_TRIPLE})
endif()
if(NOT UNINITIALIZED_WARNINGS)
add_flags(C -Wno-sometimes-uninitialized)
endif()
if(OPENMP AND (${CMAKE_C_COMPILER_VERSION} VERSION_LESS 3.7))
message(FATAL_ERROR "Clang versions earlier than 3.7 do not support OpenMP! Disable it or change compilers!")
endif()
endif()
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang")
add_flags(CXX -Wall -Wno-unused-function)
list(APPEND OPT_CXXFLAGS "-mtune=native")
if(CROSSCOMPILE)
set(CMAKE_CXX_COMPILER_TARGET ${TARGET_TRIPLE})
endif()
if(NOT UNINITIALIZED_WARNINGS)
add_flags(CXX -Wno-sometimes-uninitialized)
endif()
if(TARGET_OSX AND DEFINED BUILD_PYTHON)
# on OS X, Python will link pytraj's extension modules to libstdc++, so cpptraj needs to use the same standard library
if(BUILD_PYTHON)
add_flags(CXX -stdlib=libstdc++)
endif()
endif()
if(OPENMP AND (${CMAKE_CXX_COMPILER_VERSION} VERSION_LESS 3.7))
message(FATAL_ERROR "Clang versions earlier than 3.7 do not support OpenMP! Disable it or change compilers!")
endif()
endif()
#msvc
#---------------------------------------------------------------------------------------------------------------------------------------------------------------------
if("${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC")
add_flags(C /D_CRT_SECURE_NO_WARNINGS /MP)
set(OPT_CFLAGS "/Ox")
set(NO_OPT_CFLAGS "/Od")
set(CMAKE_C_FLAGS_DEBUG "/Zi")
endif()
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
add_flags(CXX /D_CRT_SECURE_NO_WARNINGS /MP)
set(OPT_CXXFLAGS "/Ox")
set(NO_OPT_CFLAGS "/Od")
set(CMAKE_CXX_FLAGS_DEBUG "/Zi")
endif()
#intel
#---------------------------------------------------------------------------------------------------------------------------------------------------------------------
if("${CMAKE_C_COMPILER_ID}" STREQUAL "Intel")
set(CMAKE_C_FLAGS_DEBUG "-g -debug all")
set(OPT_CFLAGS -ip -O3)
# How flags get set for optimization depend on whether we have a MIC processor,
# the version of Intel compiler we have, and whether we are cross-compiling
# for multiple versions of SSE support. The following coordinates all of this.
# This was done assuming that MIC and SSE are mutually exclusive and that we want
# SSE instructions included only when optimize = yes. Note that use of an
# SSE_TYPES specification needs to be given in place of xHost not in addition to.
# This observed behavior is not what is reported by the Intel man pages. BPK
if(SSE)
# BPK removed section that modified O1 or O2 to be O3 if optimize was set to yes.
# We already begin with the O3 setting so it wasn't needed.
# For both coptflags and foptflags, use the appropriate settings
# for the sse flags (compiler version dependent).
if(${CMAKE_C_COMPILER_VERSION} VERSION_GREATER 11 OR ${CMAKE_C_COMPILER_VERSION} VERSION_EQUAL 11)
if(NOT "${SSE_TYPES}" STREQUAL "")
list(APPEND OPT_CFLAGS "-ax${SSE_TYPES}")
else()
list(APPEND OPT_CFLAGS -xHost)
endif()
else()
list(APPEND OPT_CFLAGS -axSTPW)
endif()
endif()
endif()
if("${CMAKE_Fortran_COMPILER_ID}" STREQUAL "Intel")
if(WIN32)
add_flags(Fortran /D_CRT_SECURE_NO_WARNINGS)
set(OPT_FFLAGS "/Ox")
set(CMAKE_Fortran_FLAGS_DEBUG "/Zi")
else()
set(CMAKE_Fortran_FLAGS_DEBUG "-g -debug all")
set(OPT_FFLAGS -ip -O3)
if(SSE)
if("${CMAKE_Fortran_COMPILER_VERSION}" VERSION_GREATER 11 OR ${CMAKE_Fortran_COMPILER_VERSION} VERSION_EQUAL 11)
if(NOT "${SSE_TYPES}" STREQUAL "")
list(APPEND OPT_FFLAGS "-ax${SSE_TYPES}")
else()
list(APPEND OPT_FFLAGS -xHost)
endif()
else()
list(APPEND OPT_FFLAGS -axSTPW)
endif()
endif()
# warning flags
add_flags(Fortran "-warn all" "-warn nounused")
option(IFORT_CHECK_INTERFACES "If enabled and Intel Fortran is in use, then ifort will check that types passed to functions are the correct ones, and produce warnings or errors for mismatches." FALSE)
if(NOT IFORT_CHECK_INTERFACES)
# disable errors from type mismatches
add_flags(Fortran -warn nointerfaces)
endif()
endif()
endif()
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
set(CMAKE_CXX_FLAGS_DEBUG "-g -debug all")
set(OPT_CXXFLAGS -O3)
endif()
# PGI
#---------------------------------------------------------------------------------------------------------------------------------------------------------------------
if("${CMAKE_C_COMPILER_ID}" STREQUAL "PGI")
set(OPT_CFLAGS -O2)
endif()
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "PGI")
set(OPT_CXXFLAGS -O2)
endif()
if("${CMAKE_Fortran_COMPILER_ID}" STREQUAL "PGI")
set(OPT_FFLAGS -fast -O3)
set(NO_OPT_FFLAGS -O1)
if(SSE)
list(APPEND OPT_FFLAGS -fastsse)
endif()
endif()
# Cray
#---------------------------------------------------------------------------------------------------------------------------------------------------------------------
if("${CMAKE_C_COMPILER_ID}" STREQUAL "Cray")
# NOTE: In order for GNU-like defines to work (e.g.
# -D_FILE_OFFSET_BITS etc.) cray compilers need '-h gnu'.
add_flags(C -h gnu)
# cray compilers have equivalent of -O3 on by default
set(OPT_CFLAGS "")
endif()
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Cray")
add_flags(CXX -h gnu)
set(OPT_CXXFLAGS "")
endif()
if("${CMAKE_Fortran_COMPILER_ID}" STREQUAL "Cray")
# Also, the fortran compile requires '-emf' to force
# the build of module files with all-lowercase names.
add_flags(Fortran -h gnu -emf)
set(OPT_FFLAGS "")
endif()
#-------------------------------------------------------------------------------
# Add some non-compiler-dependent items
#-------------------------------------------------------------------------------
if(LARGE_FILE_SUPPORT)
add_definitions(-D_FILE_OFFSET_BITS=64)
endif()
check_symbol_exists(mkstemp stdlib.h HAVE_MKSTEMP)
if(HAVE_MKSTEMP)
add_definitions(-DUSE_MKSTEMP)
endif()
#this doesn't seem to get defined automatically, at least in some situations
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
add_definitions(-DWIN32)
endif()
# the mother of all feature-test macros
# tells the C library to enable all functions which are specified by any standard
add_definitions(-D_GNU_SOURCE)
if(NOT DOUBLE_PRECISION)
add_definitions(-D_REAL_) #This is read by dprec.fh, where it determines the type of precision to use
endif()
# This definition gets applied to everything, everywhere
# I think you used to be able to enable or disable netcdf, and this was the switch for it
add_definitions(-DBINTRAJ)
#-------------------------------------------------------------------------------
# finalize the flags
#-------------------------------------------------------------------------------
#put the opt cxxflags into the CUDA flags
foreach(FLAG ${OPT_CXXFLAGS})
list(APPEND HOST_NVCC_FLAGS ${FLAG})
endforeach()
# disable optimization flags if optimization is disabled
if(NOT OPTIMIZE)
set(OPT_FFLAGS ${NO_OPT_FFLAGS})
set(OPT_CFLAGS ${NO_OPT_CFLAGS})
set(OPT_CXXFLAGS ${NO_OPT_CXXFLAGS})
endif()
#create space-separated versions of each flag set for use in PROPERTY COMPILE_FLAGS
list_to_space_separated(OPT_FFLAGS_SPC ${OPT_FFLAGS})
list_to_space_separated(OPT_CFLAGS_SPC ${OPT_CFLAGS})
list_to_space_separated(OPT_CXXFLAGS_SPC ${OPT_CXXFLAGS})
list_to_space_separated(NO_OPT_FFLAGS_SPC ${NO_OPT_FFLAGS})
list_to_space_separated(NO_OPT_CFLAGS_SPC ${NO_OPT_CFLAGS})
list_to_space_separated(NO_OPT_CXXFLAGS_SPC ${NO_OPT_CXXFLAGS})
# When a library links to an imported library with interface include directories, CMake uses the -isystem flag to include those directories
# Unfortunately, this seems to completely not work with Fortran, so we disable it.
set(CMAKE_NO_SYSTEM_FROM_IMPORTED TRUE)