forked from OpenFAST/openfast
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenfastFortranOptions.cmake
More file actions
258 lines (221 loc) · 10.5 KB
/
OpenfastFortranOptions.cmake
File metadata and controls
258 lines (221 loc) · 10.5 KB
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
#
# Copyright 2016 National Renewable Energy Laboratory
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
################################################################################
# OpenFAST Fortran Options
#
# Utility functions to set Fortran compiler options depending on system
# architecture and compiler type. The entry point is the function
# `set_fast_fortran` that configures various options once the compiler is
# auto-detected.
#
# The remaining functions provide customization for specific compiler/arch to
# avoid nested if-else conditionals.
#
# Available functions:
#
# - set_fast_gfortran
# - set_fast_intel_fortran
# - set_fast_intel_fortran_posix
# - set_fast_intel_fortran_windows
#
################################################################################
#
# SET_FAST_FORTRAN - Set Fortran compiler options based on compiler/arch
#
macro(set_fast_fortran)
get_filename_component(FCNAME "${CMAKE_Fortran_COMPILER}" NAME)
# Abort if we do not have gfortran or Intel Fortran Compiler.
if (NOT (${CMAKE_Fortran_COMPILER_ID} STREQUAL "GNU" OR
${CMAKE_Fortran_COMPILER_ID} MATCHES "^Intel" OR
${CMAKE_Fortran_COMPILER_ID} STREQUAL "Flang"))
message(FATAL_ERROR "OpenFAST requires GFortran, Intel, or Flang Compiler. Compiler detected by CMake: ${FCNAME}.")
endif()
# Verify proper compiler versions are available
# see https://github.com/OpenFAST/openfast/issues/88
if(${CMAKE_Fortran_COMPILER_ID} STREQUAL "GNU")
if("${CMAKE_Fortran_COMPILER_VERSION}" STREQUAL "")
message(WARNING "A version of GNU GFortran greater than 4.6.0 is required but CMake could not detect your GFortran version.")
elseif("${CMAKE_Fortran_COMPILER_VERSION}" VERSION_LESS "4.6.0")
message(FATAL_ERROR "A version of GNU GFortran greater than 4.6.0 is required. GFortran version detected by CMake: ${CMAKE_Fortran_COMPILER_VERSION}.")
endif()
elseif(${CMAKE_Fortran_COMPILER_ID} MATCHES "^Intel")
if("${CMAKE_Fortran_COMPILER_VERSION}" VERSION_LESS "11")
message(FATAL_ERROR "A version of Intel ifort greater than 11 is required. ifort version detected by CMake: ${CMAKE_Fortran_COMPILER_VERSION}.")
endif()
endif()
# Force all .mod files to be stored in a single directory
set(CMAKE_Fortran_MODULE_DIRECTORY "${CMAKE_BINARY_DIR}/ftnmods"
CACHE STRING "Set the Fortran Modules directory" FORCE)
include_directories(${CMAKE_Fortran_MODULE_DIRECTORY})
# Get OS/Compiler specific options
if (${CMAKE_Fortran_COMPILER_ID} STREQUAL "GNU")
set_fast_gfortran()
elseif(${CMAKE_Fortran_COMPILER_ID} MATCHES "^Intel")
set_fast_intel_fortran()
elseif(${CMAKE_Fortran_COMPILER_ID} STREQUAL "Flang")
set_fast_flang()
endif()
# If double precision option enabled, set preprocessor define to use
# real64 for ReKi reals
if (DOUBLE_PRECISION)
add_definitions(-DOPENFAST_DOUBLE_PRECISION)
endif()
endmacro(set_fast_fortran)
#
# CHECK_F2008_FEATURES - Check if Fortran2008 features are available
#
macro(check_f2008_features)
include(CheckFortranSourceCompiles)
check_fortran_source_compiles(
"program test
use iso_fortran_env, only: compiler_version, real32, real64, real128
integer, parameter :: quki = real128
integer, parameter :: dbki = real64
integer, parameter :: reki = real32
end program test"
HAS_FORTRAN2008
SRC_EXT F90)
if (HAS_FORTRAN2008)
message(STATUS "Enabling Fortran 2008 features")
add_definitions(-DHAS_FORTRAN2008_FEATURES)
endif()
endmacro(check_f2008_features)
#
# SET_FAST_GFORTRAN - Customizations for GNU Fortran compiler
#
macro(set_fast_gfortran)
if(NOT WIN32)
set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -fPIC ")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
endif(NOT WIN32)
# Fix free-form compilation for OpenFAST
#set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -ffree-line-length-none -cpp -fopenmp")
set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -ffree-line-length-none -cpp")
# Disable stack reuse within routines: issues seen with gfortran 9.x, but others may also exhibit
# see section 3.16 of https://gcc.gnu.org/onlinedocs/gcc-9.2.0/gcc.pdf
# and https://github.com/OpenFAST/openfast/pull/595
set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -fstack-reuse=none")
# If double precision, make constants double precision
if (DOUBLE_PRECISION)
set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -fdefault-real-8 -fdefault-double-8")
endif()
# debug flags
if(CMAKE_BUILD_TYPE MATCHES Debug)
set( CMAKE_Fortran_FLAGS_DEBUG "${CMAKE_Fortran_FLAGS_DEBUG} -fcheck=all,no-array-temps -pedantic -fbacktrace -finit-real=inf -finit-integer=9999." )
endif()
if(CYGWIN)
# increase the default 2MB stack size to 16 MB
MATH(EXPR stack_size "16 * 1024 * 1024")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS},--stack,${stack_size}")
endif()
# Profiling
# set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -pg")
# set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pg")
check_f2008_features()
endmacro(set_fast_gfortran)
#
# SET_FAST_INTEL_FORTRAN - Customizations for Intel Fortran Compiler
#
macro(set_fast_intel_fortran)
if(WIN32)
set_fast_intel_fortran_windows()
else(WIN32)
set_fast_intel_fortran_posix()
endif(WIN32)
endmacro(set_fast_intel_fortran)
#
# SET_FAST_INTEL_FORTRAN_POSIX - Customizations for Intel Fortran Compiler posix
# arch
#
macro(set_fast_intel_fortran_posix)
set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -fpic -fpp")
# debug flags
if(CMAKE_BUILD_TYPE MATCHES Debug)
if(${CMAKE_Fortran_COMPILER_ID} MATCHES "IntelLLVM")
# NOTE: there is a bug in the 2024 and 2025 IFX compiler causing conflicts between the `check:uninit` and `-lm -ldl` flags
# When this is fixed in IFX, we will want to update this to check against versions before fix
# See here: https://community.intel.com/t5/Intel-Fortran-Compiler/ifx-IFX-2023-2-0-20230721-linker-problems-with-check-uninit/m-p/1527816
set( CMAKE_Fortran_FLAGS_DEBUG "${CMAKE_Fortran_FLAGS_DEBUG} -check all,noarg_temp_created,nouninit -traceback -init=huge,infinity" )
else()
set( CMAKE_Fortran_FLAGS_DEBUG "${CMAKE_Fortran_FLAGS_DEBUG} -check all,noarg_temp_created -traceback -init=huge,infinity" )
endif()
endif()
# If double precision, make real and double constants 64 bits
if (DOUBLE_PRECISION)
if("${CMAKE_Fortran_COMPILER_VERSION}" VERSION_GREATER "19")
set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -real-size 64 -double-size 64")
else()
set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -real_size 64 -double_size 64")
endif()
endif()
check_f2008_features()
### Intel profiling flags
# https://software.intel.com/content/www/us/en/develop/documentation/fortran-compiler-developer-guide-and-reference/top/compiler-reference/compiler-options/compiler-option-details/optimization-report-options/qopt-report-qopt-report.html
# phases: vec, par, openmp
# set(CMAKE_Fortran_FLAGS_RELWITHDEBINFO "${CMAKE_Fortran_FLAGS_RELWITHDEBINFO} -qopt-report-phase=vec,openmp -qopt-report=5")
# set(CMAKE_Fortran_FLAGS_RELWITHDEBINFO "${CMAKE_Fortran_FLAGS_RELWITHDEBINFO} -qopt-report-routine=Create_Augmented_Ln2_Src_Mesh") # Create_Augmented_Ln2_Src_Mesh, Morison_CalcOutput, VariousWaves_Init
# https://software.intel.com/content/www/us/en/develop/documentation/fortran-compiler-developer-guide-and-reference/top/compiler-reference/compiler-options/compiler-option-details/output-debug-and-precompiled-header-pch-options/debug-linux-and-macos.html
# set(CMAKE_Fortran_FLAGS_RELWITHDEBINFO "${CMAKE_Fortran_FLAGS_RELWITHDEBINFO} -debug all")
# set(CMAKE_Fortran_FLAGS_RELWITHDEBINFO "${CMAKE_Fortran_FLAGS_RELWITHDEBINFO} -debug inline-debug-info")
# Intel processor feature sets
# https://software.intel.com/content/www/us/en/develop/documentation/fortran-compiler-developer-guide-and-reference/top/compiler-reference/compiler-options/compiler-option-details/code-generation-options/xhost-qxhost.html
# set(CMAKE_Fortran_FLAGS_RELWITHDEBINFO "${CMAKE_Fortran_FLAGS_RELWITHDEBINFO} -xHOST") # Use feature set for CPU used to compile
# set(CMAKE_Fortran_FLAGS_RELWITHDEBINFO "${CMAKE_Fortran_FLAGS_RELWITHDEBINFO} -xSKYLAKE-AVX512") # Use Eagle processor feature set
endmacro(set_fast_intel_fortran_posix)
#
# SET_FAST_INTEL_FORTRAN_WINDOWS - Customizations for Intel Fortran Compiler
# windows arch
#
macro(set_fast_intel_fortran_windows)
# Turn off specific warnings
# - 5199: too many continuation lines
# - 5268: 132 column limit
set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} /Qdiag-disable:5199,5268 /fpp")
# If double precision, make constants double precision
if (DOUBLE_PRECISION)
if("${CMAKE_Fortran_COMPILER_VERSION}" VERSION_GREATER "19")
set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} /real-size:64 /double-size:64")
else()
set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} /real_size:64 /double_size:64")
endif()
endif()
# increase the default 2MB stack size to 16 MB
MATH(EXPR stack_size "16 * 1024 * 1024")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /STACK:${stack_size}")
# debug flags
if(CMAKE_BUILD_TYPE MATCHES Debug)
if(${CMAKE_Fortran_COMPILER_ID} MATCHES "IntelLLVM")
set( CMAKE_Fortran_FLAGS_DEBUG "${CMAKE_Fortran_FLAGS_DEBUG} /check:all,noarg_temp_created,nouninit /traceback /Qinit=huge,infinity" )
else()
set( CMAKE_Fortran_FLAGS_DEBUG "${CMAKE_Fortran_FLAGS_DEBUG} /check:all,noarg_temp_created /traceback /Qinit=huge,infinity" )
endif()
endif()
check_f2008_features()
endmacro(set_fast_intel_fortran_windows)
#
# set_fast_flang - Customizations for GNU Fortran compiler
#
macro(set_fast_flang)
set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -fno-backslash -cpp -fPIC")
# Deal with Double/Single precision
if (DOUBLE_PRECISION)
add_definitions(-DOPENFAST_DOUBLE_PRECISION)
set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -fdefault-real-8")
endif (DOUBLE_PRECISION)
add_definitions(-DFLANG_COMPILER)
check_f2008_features()
endmacro(set_fast_flang)