forked from MFlowCode/MFC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
406 lines (327 loc) · 13.8 KB
/
CMakeLists.txt
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
cmake_minimum_required(VERSION 3.20)
# Note: If C is not included as a language,
# FIND_LIBRARY_USE_LIB64_PATHS is ignored.
project(MFC LANGUAGES C Fortran)
# === Options
option(MFC_MPI "Build with MPI" ON)
option(MFC_OpenACC "Build with OpenACC" OFF)
option(MFC_PRE_PROCESS "Build pre_process" OFF)
option(MFC_SIMULATION "Build simulation" OFF)
option(MFC_POST_PROCESS "Build post_process" OFF)
option(MFC_DOCUMENTATION "Build documentation" OFF)
option(MFC_ALL "Build everything" OFF)
if (MFC_BUILD_ALL)
set(MFC_PRE_PROCESS ON FORCE)
set(MFC_SIMULATION ON FORCE)
set(MFC_POST_PROCESS ON FORCE)
set(MFC_DOCUMENTATION ON FORCE)
endif()
# === Imports
include(GNUInstallDirs)
include(ExternalProject)
include(CheckIPOSupported)
include(CMakeParseArguments)
include(CheckFortranCompilerFlag)
# === Check Compiler Support & Tools
# === === Compiler Support
set(__err_msg "\
CMake detected the ${CMAKE_Fortran_COMPILER_ID} Fortran compiler \
v${CMAKE_Fortran_COMPILER_VERSION}. If you intended to use a different \
compiler (or a different version thereof), please:\n\
- Install the compiler or load its module. (e.g. module load gcc/10.1)\n\
- Set/Export the C, CXX, and FC environment variables. (e.g. 'export CC=gcc', \
'export CXX=g++', and 'export FC=gfortran'.\n\
- If using mfc.sh, delete the build/<code name> directory and try again. (e.g. 'rm -rf build/pre_process')\n")
if (CMAKE_Fortran_COMPILER_ID STREQUAL "GNU")
if (CMAKE_Fortran_COMPILER_VERSION VERSION_LESS 5)
message(FATAL_ERROR "${__err_msg}ERROR: GNU v5.0 or newer is required to build MFC.")
endif()
elseif ((CMAKE_Fortran_COMPILER_ID STREQUAL "NVHPC") OR (CMAKE_Fortran_COMPILER_ID STREQUAL "PGI"))
if (CMAKE_Fortran_COMPILER_VERSION VERSION_LESS 21.7)
message(FATAL_ERROR "${__err_msg}ERROR: NVHPC v21.7 or newer is required to build MFC.")
endif()
elseif (CMAKE_Fortran_COMPILER_ID STREQUAL "AppleClang" OR CMAKE_C_COMPILER_ID STREQUAL "AppleClang")
message(FATAL_ERROR "${__err_msg}ERROR: MFC does not support the Apple Clang compilers. Please consult the documentation.")
endif()
# === === Find Fypp
find_program(FYPP_EXE fypp REQUIRED)
# === Miscellaneous Configuration
# === === Enable C-Preprocessor for Fortran files
set(CMAKE_Fortran_PREPROCESS ON)
# === === Explicitly link to -ldl (or system equivalent)
link_libraries("${CMAKE_DL_LIBS}")
# === === Request that FIND_LIBRARY searches lib/ and lib64/
set_property(GLOBAL PROPERTY FIND_LIBRARY_USE_LIB64_PATHS ON)
# === === Let FindXXX use custom scripts from cmake/
list(PREPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/toolchain/cmake")
# === Compiler Flags
if (CMAKE_Fortran_COMPILER_ID STREQUAL "GNU")
add_compile_options(
$<$<COMPILE_LANGUAGE:Fortran>:-fdefault-real-8>
$<$<COMPILE_LANGUAGE:Fortran>:-ffree-line-length-none>
$<$<COMPILE_LANGUAGE:Fortran>:-fdefault-double-8>
)
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
add_compile_options(
-fcheck=all -fbacktrace
-fimplicit-none
)
endif()
if (CMAKE_Fortran_COMPILER_VERSION VERSION_GREATER 10)
add_compile_options(
$<$<COMPILE_LANGUAGE:Fortran>:-fallow-invalid-boz>
$<$<COMPILE_LANGUAGE:Fortran>:-fallow-argument-mismatch>
)
endif()
elseif (CMAKE_Fortran_COMPILER_ID STREQUAL "Cray")
add_compile_options(
-h nomessage=296:878:1391:1069
-h msgs -h static -h keepfiles
)
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
add_compile_options(-e D -h func_trace)
endif()
elseif (CMAKE_Fortran_COMPILER_ID STREQUAL "Flang")
add_compile_options(
$<$<COMPILE_LANGUAGE:Fortran>:-Mfreeform>
$<$<COMPILE_LANGUAGE:Fortran>:-Mpreprocess>
$<$<COMPILE_LANGUAGE:Fortran>:-fdefault-real-8>
)
elseif (CMAKE_Fortran_COMPILER_ID STREQUAL "Intel")
add_compile_options($<$<COMPILE_LANGUAGE:Fortran>:-free>)
elseif ((CMAKE_Fortran_COMPILER_ID STREQUAL "NVHPC") OR (CMAKE_Fortran_COMPILER_ID STREQUAL "PGI"))
add_compile_options(
$<$<COMPILE_LANGUAGE:Fortran>:-r8>
$<$<COMPILE_LANGUAGE:Fortran>:-Mfreeform>
$<$<COMPILE_LANGUAGE:Fortran>:-Mr8intrinsics>
$<$<COMPILE_LANGUAGE:Fortran>:-cpp>
-Minfo=accel
)
if (CMAKE_BUILD_TYPE STREQUAL "Release")
add_compile_options(
$<$<COMPILE_LANGUAGE:Fortran:-minline>
)
endif()
endif()
# === Release Optimizations
if (CMAKE_BUILD_TYPE STREQUAL "Release")
# === === Processor tuning
CHECK_FORTRAN_COMPILER_FLAG("-march=native" SUPPORTS_MARCH_NATIVE)
if (SUPPORTS_MARCH_NATIVE)
add_compile_options($<$<COMPILE_LANGUAGE:Fortran>:-march=native>)
else()
CHECK_FORTRAN_COMPILER_FLAG("-mcpu=native" SUPPORTS_MCPU_NATIVE)
if (SUPPORTS_MCPU_NATIVE)
add_compile_options($<$<COMPILE_LANGUAGE:Fortran>:-mcpu=native>)
endif()
endif()
# === === Enable LTO/IPO if supported
CHECK_IPO_SUPPORTED(RESULT SUPPORTS_IPO OUTPUT IPO_ERROR)
if (SUPPORTS_IPO)
message(STATUS "Enabled IPO / LTO")
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
else()
message(STATUS "IPO / LTO is NOT available")
endif()
endif()
## === DEBUG
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
add_compile_definitions(MFC_DEBUG)
endif()
## === HANDLE_SOURCES
# Gather F90 source files for a given target, including common/, and preprocessing .fpp -> .f90.
# Outputs:
# - <target>_SRCs: List of F90 source filepaths for src/[<target>,common]/ (including .fpp -> .f90)
macro(HANDLE_SOURCES target)
set(${target}_DIR "${CMAKE_SOURCE_DIR}/src/${target}")
set(common_DIR "${CMAKE_SOURCE_DIR}/src/common")
# Gather src/[<target>,common]/*.f90
file(GLOB ${target}_F90s CONFIGURE_DEPENDS "${${target}_DIR}/*.f90")
file(GLOB common_F90s CONFIGURE_DEPENDS "${common_DIR}/*.f90")
set(${target}_SRCs ${${target}_F90s} ${common_F90s})
# src/[<DIR>,common]/*.fpp -> src/<DIR>/autogen/*.f90
file(GLOB ${target}_FPPs CONFIGURE_DEPENDS "${${target}_DIR}/*.fpp")
file(GLOB common_FPPs CONFIGURE_DEPENDS "${common_DIR}/*.fpp")
# Locate src/[<DIR>,common]/include/*.fpp
file(GLOB ${target}_incs CONFIGURE_DEPENDS "${${target}_DIR}/include/*.fpp")
file(GLOB common_incs CONFIGURE_DEPENDS "${common_DIR}/include/*.fpp")
file(MAKE_DIRECTORY "${${target}_DIR}/autogen")
foreach(fpp ${${target}_FPPs} ${common_FPPs})
cmake_path(GET fpp FILENAME fpp_filename)
set(f90 "${${target}_DIR}/autogen/${fpp_filename}.f90")
add_custom_command(
OUTPUT ${f90}
COMMAND ${FYPP_EXE} -I "${common_DIR}"
-I "${common_DIR}/include"
-I "${${target}_DIR}/include"
--line-numbering
"${fpp}" "${f90}"
DEPENDS "${fpp};${${target}_incs};${common_incs}"
COMMENT "Preprocessing (Fypp) ${fpp_filename}"
VERBATIM
)
list(APPEND ${target}_SRCs ${f90})
endforeach()
endmacro()
HANDLE_SOURCES(pre_process)
HANDLE_SOURCES(simulation)
HANDLE_SOURCES(post_process)
## === MFC_SETUP_TARGET
# Setup a target with the following options:
# - TARGET: Target name
# - SOURCES: List of source files
# - OpenACC: Can be compiled with OpenACC
# - MPI: Can be compiled with MPI
function(MFC_SETUP_TARGET)
cmake_parse_arguments(ARGS "OpenACC;MPI" "TARGET" "SOURCES" ${ARGN})
add_executable(${ARGS_TARGET} ${ARGS_SOURCES})
if (MFC_MPI AND ARGS_MPI)
find_package(MPI COMPONENTS Fortran REQUIRED)
target_compile_definitions(${ARGS_TARGET} PRIVATE MFC_MPI)
target_link_libraries (${ARGS_TARGET} PRIVATE MPI::MPI_Fortran)
endif()
if (MFC_OpenACC AND ARGS_OpenACC)
find_package(OpenACC)
# This should be equivalent to if (NOT OpenACC_FC_FOUND)
if (NOT TARGET OpenACC::OpenACC_Fortran)
message(FATAL_ERROR "OpenACC + Fortran is unsupported.")
endif()
target_link_libraries(${ARGS_TARGET} PRIVATE OpenACC::OpenACC_Fortran)
if (CMAKE_Fortran_COMPILER_ID STREQUAL "GNU")
target_compile_options(${ARGS_TARGET} PRIVATE
"-foffload=amdgcn-amdhsa='-march=gfx90a'"
"-foffload-options=-lgfortran\ -lm"
"-fno-exceptions")
elseif(CMAKE_Fortran_COMPILER_ID STREQUAL "NVHPC" OR CMAKE_Fortran_COMPILER_ID STREQUAL "PGI")
find_package(cuTENSOR)
if (NOT cuTENSOR_FOUND)
message(WARNING
"Failed to locate the NVIDIA cuTENSOR library. MFC will be "
"built without support for it, disallowing the use of "
"cu_tensor=T. This can result in degraded performance.")
else()
target_link_libraries (${ARGS_TARGET} PRIVATE cuTENSOR::cuTENSOR)
target_compile_definitions(${ARGS_TARGET} PRIVATE MFC_cuTENSOR)
endif()
if (CMAKE_BUILD_TYPE STREQUAL "Release")
target_compile_options(${ARGS_TARGET}
PRIVATE -gpu=keep,ptxinfo,lineinfo
)
elseif (CMAKE_BUILD_TYPE STREQUAL "Debug")
target_compile_options(${ARGS_TARGET}
PRIVATE -gpu=keep,ptxinfo,lineinfo,autocompare,debug
)
endif()
endif()
elseif (CMAKE_Fortran_COMPILER_ID STREQUAL "Cray")
target_compile_options(${ARGS_TARGET} PRIVATE -h noacc -x acc)
endif()
string(TOUPPER "${ARGS_TARGET}" ARGS_TARGET_UPPER)
target_compile_definitions(${ARGS_TARGET} PRIVATE "MFC_${ARGS_TARGET_UPPER}")
install(TARGETS ${ARGS_TARGET} RUNTIME DESTINATION bin)
endfunction()
if (MFC_PRE_PROCESS)
MFC_SETUP_TARGET(
TARGET pre_process
SOURCES "${pre_process_SRCs}"
MPI
)
endif()
if (MFC_SIMULATION)
MFC_SETUP_TARGET(
TARGET simulation
SOURCES "${simulation_SRCs}"
MPI OpenACC
)
if (MFC_OpenACC AND (CMAKE_Fortran_COMPILER_ID STREQUAL "NVHPC" OR CMAKE_Fortran_COMPILER_ID STREQUAL "PGI"))
find_package(CUDAToolkit REQUIRED)
target_link_libraries(simulation PRIVATE CUDA::cudart CUDA::cufft)
else()
find_package(FFTW REQUIRED)
target_link_libraries(simulation PRIVATE FFTW::FFTW)
endif()
if (CMAKE_Fortran_COMPILER_ID STREQUAL "NVHPC" OR CMAKE_Fortran_COMPILER_ID STREQUAL "PGI")
find_package(CUDAToolkit REQUIRED)
target_link_libraries(simulation PRIVATE CUDA::nvToolsExt)
endif()
endif()
if (MFC_POST_PROCESS)
MFC_SETUP_TARGET(
TARGET post_process
SOURCES "${post_process_SRCs}"
MPI
)
find_package(FFTW REQUIRED)
find_package(SILO REQUIRED)
find_package(HDF5 REQUIRED)
target_link_libraries(post_process PRIVATE SILO::SILO HDF5::HDF5 FFTW::FFTW)
# -O0 is in response to https://github.com/MFlowCode/MFC-develop/issues/95
target_compile_options(post_process PRIVATE -O0)
endif()
add_custom_target(documentation)
macro(GEN_DOCS target name)
set(DOXYGEN_PROJECT_NAME "${name}")
set(DOXYGEN_INPUT "${CMAKE_CURRENT_SOURCE_DIR}/src/${target}/ ${CMAKE_CURRENT_BINARY_DIR}/doxygen/${target}/")
file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/docs/${target}"
DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/doxygen")
file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/docs/doxygen"
DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/doxygen/${target}")
configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/docs/Doxyfile.in"
"${CMAKE_CURRENT_BINARY_DIR}/doxygen/${target}/Doxyfile"
@ONLY
)
file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/docs/res"
DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/doxygen")
file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/docs/config.js"
DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/doxygen")
add_custom_command(
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/doxygen/${target}/html/index.html"
DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/doxygen/${target}/Doxyfile"
"${${target}_SRCs}"
COMMAND "${DOXYGEN_EXECUTABLE}"
"Doxyfile"
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/doxygen/${target}"
COMMENT "${target}: Generating documentation"
)
add_custom_target(
"${target}_doxygen" ALL
DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/doxygen/${target}/html/index.html"
)
add_dependencies("${target}_doxygen" doxygen-awesome-css)
add_dependencies(documentation "${target}_doxygen")
install(
DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/doxygen/${target}/html/"
DESTINATION "docs/mfc/${target}"
)
install(
DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/doxygen/res/"
DESTINATION "docs/mfc/res"
)
install(
FILES "${CMAKE_CURRENT_SOURCE_DIR}/docs/index.html"
DESTINATION "docs/mfc"
)
endmacro()
# === Documentation
if (MFC_DOCUMENTATION)
# === === Find Doxygen
find_package(Doxygen REQUIRED dot REQUIRED)
# === === Fetch CSS Theme
ExternalProject_Add(doxygen-awesome-css
PREFIX doxygen-awesome-css
GIT_REPOSITORY "https://github.com/jothepro/doxygen-awesome-css"
GIT_TAG "8a34f701d3435309a21080f44fbd4a466979aa19"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
)
set(theme_dirpath "${CMAKE_CURRENT_BINARY_DIR}/doxygen-awesome-css/src/doxygen-awesome-css/")
set(DOXYGEN_HTML_EXTRA_STYLESHEET "\"${theme_dirpath}/doxygen-awesome.css\"")
# === === Generate Documentation
GEN_DOCS(pre_process "MFC: Pre-Process")
GEN_DOCS(simulation "MFC: Simulation")
GEN_DOCS(post_process "MFC: Post-Process")
# === === Generate Landing Page
GEN_DOCS(documentation "MFC")
endif()