-
Notifications
You must be signed in to change notification settings - Fork 9
/
CMakeLists.txt
249 lines (215 loc) · 9.54 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
# (C) Copyright 2022- ECMWF.
# (C) Copyright 2022- Meteo-France.
#
# This software is licensed under the terms of the Apache Licence Version 2.0
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
# In applying this licence, ECMWF does not waive the privileges and immunities
# granted to it by virtue of its status as an intergovernmental organisation
# nor does it submit to any jurisdiction.
cmake_minimum_required (VERSION 3.24)
## find ecbuild
find_package( ecbuild 3.7 QUIET HINTS ${CMAKE_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/../ecbuild )
if( NOT ecbuild_FOUND )
include(cmake/field_api_fetchcontent_ecbuild.cmake)
endif()
## find ecbuild toolchain
if( DEFINED ENV{ECBUILD_TOOLCHAIN} AND NOT CMAKE_TOOLCHAIN_FILE)
set(CMAKE_TOOLCHAIN_FILE ${CMAKE_CURRENT_SOURCE_DIR}/$ENV{ECBUILD_TOOLCHAIN})
endif()
## define project and field_api target name
project(field_api VERSION 0.3.3 LANGUAGES Fortran)
set(LIBNAME field_api)
## find required packages
ecbuild_find_package(OpenMP COMPONENTS Fortran REQUIRED)
## find fypp
ecbuild_find_package( fckit )
find_program( FYPP fypp )
if( fckit_FOUND AND fckit_HAVE_FCKIT_VENV )
set( FYPP ${FCKIT_VENV_EXE} -m fypp )
elseif( fckit_FOUND )
# This is only needed for building in environments with python3 older than 3.8
list( APPEND _fckit_fypp_path "${FYPP}" )
list( LENGTH _fckit_fypp_path _list_length )
MATH( EXPR _last_entry "${_list_length} - 1" )
list( GET _fckit_fypp_path ${_last_entry} FYPP )
elseif( FYPP MATCHES FYPP-NOTFOUND )
include(cmake/field_api_fetchcontent_fypp.cmake)
set(FYPP ${fypp_SOURCE_DIR}/bin/fypp)
ecbuild_info("fypp downloaded to: ${FYPP}")
endif()
## find OpenACC
if( ${CMAKE_VERSION} VERSION_LESS "3.25" )
if ( FIELD_API_ENABLE_ACC OR (NOT DEFINED FIELD_API_ENABLE_ACC AND (ENABLE_ACC OR NOT DEFINED ENABLE_ACC)) )
# See https://gitlab.kitware.com/cmake/cmake/-/issues/23691, fixed in CMake 3.25
# (TL;DR: FindOpenACC sets OpenACC_<LANG>_FOUND correctly but does not set
# OpenACC_FOUND unless all three C, CXX, and Fortran have been found - even if
# only one language has been requested via COMPONENTS)
find_package( OpenACC COMPONENTS Fortran )
if( OpenACC_Fortran_FOUND )
set( OpenACC_FOUND ON )
endif()
endif()
endif()
ecbuild_add_option( FEATURE ACC
DEFAULT ON
DESCRIPTION "Support for using GPUs with OpenACC"
REQUIRED_PACKAGES "OpenACC COMPONENTS Fortran" )
## set general compiler flags
include(cmake/field_api_compile_options.cmake)
## choose to build dp or sp or both
ecbuild_add_option( FEATURE SINGLE_PRECISION
DESCRIPTION "Compile field_api in single precision" DEFAULT ON)
ecbuild_add_option( FEATURE DOUBLE_PRECISION
DESCRIPTION "Compile field_api in double precision" DEFAULT ON)
set(DEFAULT_PRECISION sp)
if(HAVE_SINGLE_PRECISION)
list(APPEND fiat_components single)
list(APPEND precisions sp)
endif()
if(HAVE_DOUBLE_PRECISION)
set(DEFAULT_PRECISION dp)
list(APPEND precisions dp)
list(APPEND fiat_components double)
endif()
## find fiat
ecbuild_find_package(NAME fiat COMPONENTS ${fiat_components})
if( NOT fiat_FOUND )
if(NOT UTIL_MODULE_PATH)
ecbuild_critical("If not building with fiat, then the path for utility modules must be specified")
endif()
ecbuild_info( "Checking for FIAT components in ${UTIL_MODULE_PATH}" )
find_file( ABOR1_PATH abor1.F90 REQUIRED
HINTS ${UTIL_MODULE_PATH} ${UTIL_MODULE_PATH}/src/fiat/util
)
ecbuild_info( "Found ABOR1: ${ABOR1_PATH}" )
find_file( OML_PATH oml_mod.F90 REQUIRED
HINTS ${UTIL_MODULE_PATH} ${UTIL_MODULE_PATH}/src/fiat/oml
)
ecbuild_info( "Found OML: ${OML_PATH}" )
find_file( PARKIND1_PATH parkind1.F90 REQUIRED
HINTS ${UTIL_MODULE_PATH} ${UTIL_MODULE_PATH}/src/parkind
)
ecbuild_info( "Found PARKIND1: ${PARKIND1_PATH}" )
list(APPEND srcs ${ABOR1_PATH} ${OML_PATH} ${PARKIND1_PATH})
endif()
list(APPEND srcs field_async_module.F90 field_basic_module.F90 field_defaults_module.F90 dev_alloc.c c_malloc.c field_constants_module.F90 field_abort_module.F90 field_statistics_module.F90 field_backtrace.c)
## check for CUDA
include(CheckLanguage)
check_language(CUDA)
ecbuild_add_option( FEATURE CUDA
DESCRIPTION "CUDA" DEFAULT ON
CONDITION CMAKE_CUDA_COMPILER AND HAVE_ACC )
## buddy allocator option
ecbuild_add_option( FEATURE BUDDY_MALLOC
DESCRIPTION "Use buddy allocator for shadow host allocation"
DEFAULT ON
)
check_symbol_exists(backtrace execinfo.h HAVE_BACKTRACE)
## fypp preprocessor flags
if(HAVE_BUDDY_MALLOC)
list( APPEND fypp_defines "-DUSE_BUDDY_MALLOC")
endif()
if(HAVE_CUDA)
list( APPEND fypp_defines "-DCUDA")
endif()
if(fiat_FOUND)
list( APPEND fypp_defines "-DWITH_FIAT")
endif()
## preprocess fypp files
foreach (SUFF IN ITEMS IM RM RD LM)
string (TOLOWER ${SUFF} suff)
foreach (RANK RANGE 1 5)
foreach (FUNC IN ITEMS "" _shuffle _access _util _array _array_util _gang _factory _gather _data)
add_custom_command (OUTPUT field_${RANK}${suff}${FUNC}_module.F90
COMMAND ${FYPP} -DRANK=${RANK} -DSUFF='${SUFF}' ${fypp_defines} -m os -M ${CMAKE_CURRENT_SOURCE_DIR} -m fieldType
${CMAKE_CURRENT_SOURCE_DIR}/field_RANKSUFF${FUNC}_module.fypp > field_${RANK}${suff}${FUNC}_module.F90
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/field_RANKSUFF${FUNC}_module.fypp
VERBATIM)
list(APPEND srcs "field_${RANK}${suff}${FUNC}_module.F90")
set_source_files_properties( "field_${RANK}${suff}${FUNC}_module.F90" PROPERTIES COMPILE_OPTIONS $<${HAVE_CUDA}:-cuda>)
endforeach ()
endforeach ()
endforeach ()
foreach (SRC IN ITEMS dev_alloc_module field_factory_module field_access_module field_gang_module field_array_module field_module
field_shuffle_module field_util_module field_array_util_module field_shuffle_type_module host_alloc_module
field_gathscat_module field_gathscat_type_module)
add_custom_command (OUTPUT ${SRC}.F90
COMMAND ${FYPP} -m os ${fypp_defines} -M ${CMAKE_CURRENT_SOURCE_DIR} -m fieldType ${CMAKE_CURRENT_SOURCE_DIR}/${SRC}.fypp > ${SRC}.F90
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${SRC}.fypp
VERBATIM)
list(APPEND srcs "${SRC}.F90")
endforeach ()
set_source_files_properties("dev_alloc_module.F90" PROPERTIES COMPILE_OPTIONS $<${HAVE_CUDA}:-cuda>)
set_source_files_properties("host_alloc_module.F90" PROPERTIES COMPILE_OPTIONS $<${HAVE_CUDA}:-cuda>)
## determine lib type
cmake_dependent_option(BUILD_SHARED_LIBS "Dynamically link field_api" ON "NOT HAVE_ACC" OFF)
if( HAVE_ACC )
ecbuild_warn("OpenACC builds force static linking.")
endif()
list(APPEND prec_srcs field_module.F90 field_array_module.F90 field_array_util_module.F90 field_util_module.F90)
list(REMOVE_ITEM srcs ${prec_srcs})
## build precision independent source files
ecbuild_add_library(
TARGET ${LIBNAME}
TYPE OBJECT
SOURCES ${srcs}
DEFINITIONS $<$<NOT:${fiat_FOUND}>:${FIELD_API_DEFINITIONS}> $<${fiat_FOUND}:WITH_FIAT>
PRIVATE_LIBS
$<${HAVE_ACC}:OpenACC::OpenACC_Fortran>
$<${fiat_FOUND}:fiat>
$<${fiat_FOUND}:parkind_${DEFAULT_PRECISION}>
OpenMP::OpenMP_Fortran
)
target_include_directories( ${LIBNAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
set_property(TARGET ${LIBNAME} PROPERTY C_STANDARD 99)
set_target_properties( ${LIBNAME} PROPERTIES Fortran_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/include/${LIBNAME} )
target_link_options( ${LIBNAME} PRIVATE $<${HAVE_CUDA}:-cuda> )
if( HAVE_BACKTRACE )
target_compile_definitions( ${LIBNAME} PRIVATE HAVE_BACKTRACE )
endif()
# set install location for .mod files
install(DIRECTORY ${CMAKE_BINARY_DIR}/include/${LIBNAME} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
foreach(prec ${precisions})
## add field_api target
ecbuild_add_library(
TARGET ${LIBNAME}_${prec}
SOURCES ${prec_srcs} $<TARGET_OBJECTS:${LIBNAME}>
DEFINITIONS $<$<NOT:${fiat_FOUND}>:${FIELD_API_DEFINITIONS}>
PRIVATE_LIBS
$<${fiat_FOUND}:fiat>
$<${fiat_FOUND}:parkind_${prec}>
OpenMP::OpenMP_Fortran
)
target_include_directories( ${LIBNAME}_${prec} PRIVATE ${CMAKE_BINARY_DIR}/include/${LIBNAME})
set_property(TARGET ${LIBNAME}_${prec} PROPERTY C_STANDARD 99)
set_target_properties( ${LIBNAME}_${prec} PROPERTIES Fortran_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/include/${LIBNAME}_${prec} )
target_link_options( ${LIBNAME}_${prec} PUBLIC $<${HAVE_CUDA}:-cuda> )
if( HAVE_ACC AND CMAKE_Fortran_COMPILER_ID MATCHES "PGI|NVHPC")
target_link_options( ${LIBNAME}_${prec} INTERFACE SHELL:${OpenACC_Fortran_FLAGS} )
endif()
# export target usage interface
target_include_directories( ${LIBNAME}_${prec}
INTERFACE $<BUILD_INTERFACE:${CMAKE_BINARY_DIR}/include/${LIBNAME}_${prec}>
INTERFACE $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/${LIBNAME}_${prec}>
INTERFACE $<BUILD_INTERFACE:${CMAKE_BINARY_DIR}/include/${LIBNAME}>
INTERFACE $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/${LIBNAME}>
)
if( prec MATCHES sp )
target_compile_definitions( ${LIBNAME}_${prec} PRIVATE PARKIND1_SINGLE )
endif()
# set install location for .mod files
install(DIRECTORY ${CMAKE_BINARY_DIR}/include/${LIBNAME}_${prec} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
endforeach()
## build tests
ecbuild_add_option(
FEATURE TESTS
DEFAULT ON
DESCRIPTION "Enable field_api testing"
CONDITION fiat_FOUND
)
if( HAVE_TESTS )
include(CTest)
add_subdirectory(tests)
endif()
## export the project
ecbuild_install_project(NAME ${PROJECT_NAME})