-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSetFortranFlags.cmake
304 lines (263 loc) · 12.2 KB
/
SetFortranFlags.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
#
# SetFortranFlags.cmake
# This file is part of PROJECTNAME.
#
# Copyright YEAR AUTHOR <EMAIL> [AUTHOR2 <EMAIL2>, ...]
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program. If not, see
# <https://www.gnu.org/licenses/>.
#
#
# This file is adapted from cmake_fortran_template
# <https://github.com/SethMMorton/cmake_fortran_template>
# Copyright (c) 2018 Seth M. Morton
#
######################################################
# Determine and set the Fortran compiler flags we want
######################################################
####################################################################
# Make sure that the default build type is RELEASE if not specified.
####################################################################
INCLUDE(${CMAKE_MODULE_PATH}/SetCompileFlag.cmake)
# Make sure the build type is uppercase
STRING(TOUPPER "${CMAKE_BUILD_TYPE}" BT)
IF(BT STREQUAL "RELEASE")
SET(CMAKE_BUILD_TYPE RELEASE CACHE STRING
"Choose the type of build, options are DEBUG, RELEASE, or TESTING."
FORCE)
ELSEIF(BT STREQUAL "DEBUG")
SET (CMAKE_BUILD_TYPE DEBUG CACHE STRING
"Choose the type of build, options are DEBUG, RELEASE, or TESTING."
FORCE)
ELSEIF(BT STREQUAL "TESTING")
SET (CMAKE_BUILD_TYPE TESTING CACHE STRING
"Choose the type of build, options are DEBUG, RELEASE, or TESTING."
FORCE)
ELSEIF(NOT BT)
SET(CMAKE_BUILD_TYPE RELEASE CACHE STRING
"Choose the type of build, options are DEBUG, RELEASE, or TESTING."
FORCE)
MESSAGE(STATUS "CMAKE_BUILD_TYPE not given, defaulting to RELEASE")
ELSE()
MESSAGE(FATAL_ERROR "CMAKE_BUILD_TYPE not valid, choices are DEBUG, RELEASE, or TESTING")
ENDIF(BT STREQUAL "RELEASE")
#########################################################
# If the compiler flags have already been set, return now
#########################################################
SET(RECOMPUTE_COMPILER_FLAGS FALSE CACHE BOOLEAN
"Whether to compute compiler flags again, even if build type is unchanged.")
IF(DEFAULT_Fortran_FLAGS_RELEASE AND DEFAULT_Fortran_FLAGS_TESTING AND
DEFAULT_Fortran_FLAGS_DEBUG AND DEFAULT_Fortran_FLAGS AND
DEFAULT_Fortran_FLAGS_BASIC AND CMAKE_BUILD_TYPE STREQUAL
BUILD_TYPE_USED_IN_FLAGS AND NOT RECOMPUTE_COMPILER_FLAGS)
SEPARATE_ARGUMENTS(DEFAULT_Fortran_FLAGS)
SEPARATE_ARGUMENTS(DEFAULT_Fortran_FLAGS_BASIC)
SEPARATE_ARGUMENTS(DEFAULT_Fortran_FLAGS_RELEASE)
SEPARATE_ARGUMENTS(DEFAULT_Fortran_FLAGS_TESTING)
SEPARATE_ARGUMENTS(DEFAULT_Fortran_FLAGS_DEBUG)
RETURN()
ENDIF()
UNSET(DEFAULT_Fortran_FLAGS_RELEASE CACHE)
UNSET(DEFAULT_Fortran_FLAGS_TESTING CACHE)
UNSET(DEFAULT_Fortran_FLAGS_DEBUG CACHE)
UNSET(DEFAULT_Fortran_FLAGS_BASIC CACHE)
UNSET(DEFAULT_Fortran_FLAGS CACHE)
########################################################################
# Determine the appropriate flags for this compiler for each build type.
# For each option type, a list of possible flags is given that work
# for various compilers. The first flag that works is chosen.
# If none of the flags work, nothing is added (unless the REQUIRED
# flag is given in the call). This way unknown compiles are supported.
#######################################################################
#####################
### GENERAL FLAGS ###
#####################
# Don't add underscores in symbols for C-compatability
SET_COMPILE_FLAG(DEFAULT_Fortran_FLAGS_BASIC "${DEFAULT_Fortran_FLAGS_BASIC}"
Fortran "-fno-underscoring")
# There is some bug where -march=native doesn't work on Mac
IF(APPLE)
SET(GNUNATIVE "-mtune=native")
ELSE()
SET(GNUNATIVE "-march=native")
ENDIF()
# Optimize for the host's architecture
SET_COMPILE_FLAG(DEFAULT_Fortran_FLAGS_BASIC "${DEFAULT_Fortran_FLAGS_BASIC}"
Fortran "-xHost" # Intel
"/QxHost" # Intel Windows
${GNUNATIVE} # GNU
"-ta=host" # Portland Group
)
# Turn on all warnings
SET_COMPILE_FLAG(DEFAULT_Fortran_FLAGS_BASIC "${DEFAULT_Fortran_FLAGS_BASIC}"
Fortran "-warn all" # Intel
"/warn:all" # Intel Windows
"-Wall" # GNU
# Portland Group (on by default)
)
SET_COMPILE_FLAG(DEFAULT_Fortran_FLAGS_BASIC "${DEFAULT_Fortran_FLAGS_BASIC}"
Fortran "-Wextra" # GNU
)
SET_COMPILE_FLAG(DEFAULT_Fortran_FLAGS_BASIC "${DEFAULT_Fortran_FLAGS_BASIC}"
Fortran "-Wsurprising" # GNU
)
SET_COMPILE_FLAG(DEFAULT_Fortran_FLAGS_BASIC "${DEFAULT_Fortran_FLAGS_BASIC}"
Fortran "-Wpedantic" # GNU
)
# Treat warnings as errors
SET_COMPILE_FLAG(DEFAULT_Fortran_FLAGS_BASIC "${DEFAULT_Fortran_FLAGS_BASIC}"
Fortran "-warn error" # Intel
"/warn:error" # Intel Windows
"-Werror" # GNU
# Portland Group (not available)
)
# Require code to be standard-complient
SET_COMPILE_FLAG(DEFAULT_Fortran_FLAGS_BASIC "${DEFAULT_Fortran_FLAGS_BASIC}"
Fortran "-stand=f18" # Intel
"-stand=f15" # Old versions of Intel
"-stand=f08" # Even older versions of Intel
"-std=f2018" # GNU
"-std=f2008ts" # Old versions of GNU
"-std=f2008" # Even older versions of GNU
"-Mstandard" # Portland Group
)
SEPARATE_ARGUMENTS(DEFAULT_Fortran_FLAGS_BASIC)
###################
### DEBUG FLAGS ###
###################
# NOTE: debugging symbols (-g or /debug:full) are already on by default
# Disable optimizations
SET_COMPILE_FLAG(DEFAULT_Fortran_FLAGS_DEBUG "${DEFAULT_Fortran_FLAGS_DEBUG}"
Fortran REQUIRED "-O0" # All compilers not on Windows
"/Od" # Intel Windows
)
# Traceback
SET_COMPILE_FLAG(DEFAULT_Fortran_FLAGS_DEBUG "${DEFAULT_Fortran_FLAGS_DEBUG}"
Fortran "-traceback" # Intel/Portland Group
"/traceback" # Intel Windows
"-fbacktrace" # GNU (gfortran)
"-ftrace=full" # GNU (g95)
)
# Check array bounds, pointers, recursion, etc. (varies by compiler)
SET_COMPILE_FLAG(DEFAULT_Fortran_FLAGS_DEBUG "${DEFAULT_Fortran_FLAGS_DEBUG}"
Fortran "-check all" # Intel
"/check:all" # Intel Windows
"-fcheck=all" # GNU (New style)
"-fbounds-check" # GNU (Old style)
"-Mbounds" # Portland Group
)
SET_COMPILE_FLAG(DEFAULT_Fortran_FLAGS_DEBUG "${DEFAULT_Fortran_FLAGS_DEBUG}"
Fortran "-chkptr" # Portland Group
)
SET_COMPILE_FLAG(DEFAULT_Fortran_FLAGS_DEBUG "${DEFAULT_Fortran_FLAGS_DEBUG}"
Fortran "-chkstk" # Portland Group
)
# Check for various floating point errors
SET_COMPILE_FLAG(DEFAULT_Fortran_FLAGS_DEBUG "${DEFAULT_Fortran_FLAGS_DEBUG}"
Fortran "-fpe-all=0" # Intel
"/fpe-all=0" # Intel Windows
"-ffpe-trap=invalid,zero,overflow" # GNU
"-Ktrap=fp,inv,ovf" # Portland Group
)
SEPARATE_ARGUMENTS(DEFAULT_Fortran_FLAGS_DEBUG)
#####################
### TESTING FLAGS ###
#####################
# Optimizations
SET_COMPILE_FLAG(DEFAULT_Fortran_FLAGS_TESTING "${DEFAULT_Fortran_FLAGS_TESTING}"
Fortran REQUIRED "-O0" # All compilers not on Windows
"/O0" # Intel Windows
)
# Debug symbols
SET_COMPILE_FLAG(DEFAULT_Fortran_FLAGS_TESTING "${DEFAULT_Fortran_FLAGS_TESTING}"
Fortran REQUIRED "-g" # All compilers not on Windows
"/g" # Intel Windows
)
# Coverage flags (only available for GNU and Intel Windows)
SET_COMPILE_FLAG(DEFAULT_Fortran_FLAGS_TESTING "${DEFAULT_Fortran_FLAGS_TESTING}"
Fortran "--coverage" # GNU
"/Qcov-gen" # Intel Windows
)
SEPARATE_ARGUMENTS(DEFAULT_Fortran_FLAGS_TESTING)
#####################
### RELEASE FLAGS ###
#####################
# Test with both -02 and -03, as the latter isn't always faster
SET_COMPILE_FLAG(DEFAULT_Fortran_FLAGS_RELEASE "${DEFAULT_Fortran_FLAGS_RELEASE}"
Fortran REQUIRED "-O3" # GNU, Intel, Portland Group
"/O3" # Intel Windows
)
# Unroll loops
SET_COMPILE_FLAG(DEFAULT_Fortran_FLAGS_RELEASE "${DEFAULT_Fortran_FLAGS_RELEASE}"
Fortran "-funroll-loops" # GNU
"-unroll" # Intel
"/unroll" # Intel Windows
"-Munroll" # Portland Group
)
# Inline functions
SET_COMPILE_FLAG(DEFAULT_Fortran_FLAGS_RELEASE "${DEFAULT_Fortran_FLAGS_RELEASE}"
Fortran "-inline" # Intel
"/Qinline" # Intel Windows
"-finline-functions" # GNU
"-Minline" # Portland Group
)
# Interprocedural (link-time) optimizations
SET_COMPILE_FLAG(DEFAULT_Fortran_FLAGS_RELEASE "${DEFAULT_Fortran_FLAGS_RELEASE}"
Fortran "-ipo" # Intel
"/Qipo" # Intel Windows
"-flto" # GNU
"-Mipa" # Portland Group
)
# Single-file optimizations
SET_COMPILE_FLAG(DEFAULT_Fortran_FLAGS_RELEASE "${DEFAULT_Fortran_FLAGS_RELEASE}"
Fortran "-ip" # Intel
"/Qip" # Intel Windows
)
# Vectorize code
SET_COMPILE_FLAG(DEFAULT_Fortran_FLAGS_RELEASE "${DEFAULT_Fortran_FLAGS_RELEASE}"
Fortran "-vec-report0" # Intel
"/Qvec-report0" # Intel Windows
"-Mvect" # Portland Group
)
SEPARATE_ARGUMENTS(DEFAULT_Fortran_FLAGS_RELEASE)
################################
### CHOOSE APPROPRIATE FLAGS ###
################################
IF(NOT DEFAULT_Fortran_FLAGS OR NOT CMAKE_BUILD_TYPE STREQUAL
BUILD_TYPE_USED_IN_FLAGS OR RECOMPUTE_COMPILER_FLAGS)
IF(CMAKE_BUILD_TYPE STREQUAL "RELEASE")
SET(DEFAULT_Fortran_FLAGS ${DEFAULT_Fortran_FLAGS_BASIC}
${DEFAULT_Fortran_FLAGS_RELEASE} CACHE STRING
"Default compiler flags to use" FORCE)
SET(BUILD_TYPE_Fortran_FLAGS ${DEFAULT_Fortran_FLAGS_RELEASE} CACHE STRING
"The Fortran flags for this build type" FORCE)
ELSEIF(CMAKE_BUILD_TYPE STREQUAL "DEBUG")
SET(DEFAULT_Fortran_FLAGS ${DEFAULT_Fortran_FLAGS_BASIC}
${DEFAULT_Fortran_FLAGS_DEBUG} CACHE STRING
"Default compiler flags to use" FORCE)
SET(BUILD_TYPE_Fortran_FLAGS ${DEFAULT_Fortran_FLAGS_DEBUG} CACHE STRING
"The Fortran flags for this build type" FORCE)
ELSEIF(CMAKE_BUILD_TYPE STREQUAL "TESTING")
SET(DEFAULT_Fortran_FLAGS ${DEFAULT_Fortran_FLAGS_BASIC}
${DEFAULT_Fortran_FLAGS_TESTING} CACHE STRING
"Default compiler flags to use" FORCE)
SET(BUILD_TYPE_Fortran_FLAGS ${DEFAULT_Fortran_FLAGS_TESTING} CACHE STRING
"The Fortran flags for this build type" FORCE)
ENDIF()
ENDIF()
SET(BUILD_TYPE_USED_IN_FLAGS ${BT} CACHE STRING
"The build type used to compute compiler flags" FORCE)
SET(RECOMPUTE_COMPILER_FLAGS FALSE CACHE BOOLEAN
"Whether to compute compiler flags again, even if build type is unchanged. Defaults to FALSE."
FORCE)