-
Notifications
You must be signed in to change notification settings - Fork 29
/
CMakeLists.txt
416 lines (335 loc) · 14.7 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
407
408
409
410
411
412
413
414
415
416
##===------------------------------------------------------------------------------*- CMake -*-===##
##
## S E R I A L B O X
##
## This file is distributed under terms of BSD license.
## See LICENSE.txt for more information.
##
##===------------------------------------------------------------------------------------------===##
##
## This is the master CMake file of the serialbox project.
##
##===------------------------------------------------------------------------------------------===##
set(CMAKE_INSTALL_PREFIX "${CMAKE_CURRENT_SOURCE_DIR}/install" CACHE PATH "CMake install prefix")
cmake_policy(SET CMP0048 NEW)
cmake_minimum_required(VERSION 3.12.0)
project(Serialbox LANGUAGES C CXX VERSION 2.6.2)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_C_EXTENSIONS OFF)
list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/modules")
include(ExternalProject)
include(CMakeParseArguments)
include(CheckCXXCompilerFlag)
include(SerialboxInstallTargets)
#---------------------------------------- Configure ------------------------------------------------
if(UNIX)
set(SERIALBOX_ON_UNIX 1)
endif(UNIX)
if(WIN32)
set(SERIALBOX_ON_WIN32 1)
endif(WIN32)
# Serialbox version
set(Serialbox_VERSION_STRING
"${Serialbox_VERSION_MAJOR}.${Serialbox_VERSION_MINOR}.${Serialbox_VERSION_PATCH}")
message(STATUS "Serialbox version: ${Serialbox_VERSION_STRING}")
#---------------------------------------- User options ---------------------------------------------
option(SERIALBOX_BUILD_SHARED "Build shared libraries" ON)
option(SERIALBOX_ENABLE_C "Build C Interface" ON)
option(SERIALBOX_ENABLE_PYTHON "Build Python Interface" ${SERIALBOX_ENABLE_C})
option(SERIALBOX_ENABLE_FORTRAN "Build Fortran Interface" OFF)
option(SERIALBOX_ENABLE_FTG "Build FortranTestGenerator frontend" OFF)
option(SERIALBOX_ENABLE_SDB "Install the stencil-debugger (sdb)" ON)
option(SERIALBOX_LOGGING "Enable logging" ON)
option(SERIALBOX_ASYNC_API "Enable the asynchronous API" ON)
option(SERIALBOX_EXAMPLES "Build example exectuables" ON)
option(SERIALBOX_USE_OPENSSL "Use OpenSSL library" OFF)
option(SERIALBOX_USE_NETCDF "Use NetCDF library" OFF)
option(SERIALBOX_TESTING "Build unittest executables" OFF)
option(SERIALBOX_TESTING_GRIDTOOLS "Build gridtools unitests and examples" OFF)
option(SERIALBOX_TESTING_STELLA "Build STELLA unitests" OFF)
option(SERIALBOX_TESTING_DEATH_TESTS "Run death-tests" OFF)
option(SERIALBOX_TESTING_LARGE_FILE_TESTS "Run large file (>4GB) tests" OFF)
option(SERIALBOX_TESTING_FORTRAN "Build tests for the Fortran interface")
option(SERIALBOX_BENCHMARKING "Build benchmark exectuables" OFF)
option(SERIALBOX_DOCUMENTATION "Build and install the documentation" OFF)
option(SERIALBOX_CODE_COVERAGE "Generate code coverage" OFF)
option(SERIALBOX_VERBOSE_WARNINGS "Enable verbose warnings (-Wall)" OFF)
#---------------------------------------- CMake options --------------------------------------------
## Perform checks to make sure we support the current compiler
include(SerialboxCheckCxxCompilerSupport)
## Set C++ standard to C++17
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Compiler specific
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-sign-compare")
elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
elseif(CMAKE_CXX_COMPILER_ID MATCHES "Intel")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -wd488")
endif()
# Set build type to Release if nothing was specified (instead of Debug)
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release" CACHE STRING
"Choose the type of build, options are: Debug Release RelWithDebInfo" FORCE)
message(STATUS "Setting build type to 'Release' as none was specified")
endif(NOT CMAKE_BUILD_TYPE)
# Clear all cmake generated files
if( NOT TARGET clean-all )
add_custom_target(clean-all
COMMAND ${CMAKE_MAKE_PROGRAM} clean
COMMAND ${CMAKE_COMMAND} -P
"${PROJECT_SOURCE_DIR}/cmake/modules/SerialboxCleanAll.cmake")
endif()
# We need thread support
find_package(Threads REQUIRED)
# Set script directory
set(SCRIPT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/tools)
# If we build the Fortran Interface, we need a Fortran compiler
if(SERIALBOX_ENABLE_FORTRAN)
enable_language(Fortran)
include(SerialboxFortranCompilerFlags)
endif()
if(SERIALBOX_BUILD_SHARED AND CMAKE_Fortran_COMPILER_ID MATCHES "Cray")
message(WARNING "Cray Fortran compiler detected, disabling shared libraries")
set(SERIALBOX_BUILD_SHARED OFF)
set(SERIALBOX_ENABLE_SDB OFF)
set(SERIALBOX_ENABLE_PYTHON OFF)
endif()
# Set shared library flags
if(SERIALBOX_BUILD_SHARED)
set(BUILD_SHARED_LIBS ON)
# Use, i.e. don't skip the full RPATH for the build tree
set(CMAKE_SKIP_BUILD_RPATH FALSE)
# When building, don't use the install RPATH already (but later on when installing)
set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
set(CMAKE_MACOSX_RPATH ON)
# Add the automatically determined parts of the RPATH which point to directories outside the
# build tree to the install RPATH
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
# The RPATH to be used when installing, but only if it's not a system directory
list(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${CMAKE_INSTALL_PREFIX}/lib" isSystemDir)
if("${isSystemDir}" STREQUAL "-1")
SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
endif("${isSystemDir}" STREQUAL "-1")
endif()
if(NOT(SERIALBOX_LOGGING))
add_definitions(-DSERIALBOX_DISABLE_LOGGING)
else()
set(SERIALBOX_HAS_LOGGING 1)
endif()
if(SERIALBOX_ASYNC_API)
add_definitions(-DSERIALBOX_ASYNC_API)
endif(SERIALBOX_ASYNC_API)
## Third party libraries
set(SERIALBOX_EXTERNAL_LIBRARIES)
## Enable testing if one ore more of the tests is requested
if(SERIALBOX_TESTING_GRIDTOOLS OR
SERIALBOX_TESTING_STELLA OR
SERIALBOX_TESTING_DEATH_TESTS OR
SERIALBOX_TESTING_FORTRAN)
set(SERIALBOX_TESTING ON)
endif()
## IDE Support
include(IDESupport)
#---------------------------------------- GTest ----------------------------------------------------
if(SERIALBOX_TESTING)
set(tmp_BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS})
set(BUILD_SHARED_LIBS OFF)
include(FetchGoogletest)
fetch_googletest()
set(BUILD_SHARED_LIBS ${tmp_BUILD_SHARED_LIBS})
if(SERIALBOX_ENABLE_C)
set(SERIALBOX_HAS_C 1)
endif(SERIALBOX_ENABLE_C)
endif(SERIALBOX_TESTING)
#---------------------------------------- pFUnit ---------------------------------------------------
if(SERIALBOX_TESTING_FORTRAN)
if(SERIALBOX_ENABLE_FORTRAN)
set(PFUNIT_ROOT_ENV "$ENV{PFUNIT_ROOT}")
find_package(PFUNIT 4.4.1 REQUIRED)
else()
message(WARNING "You need to enable Fortran (-DSERIALBOX_ENABLE_FORTRAN=ON) to build the Fortran tests.")
endif()
endif()
#---------------------------------------- OpenSSL --------------------------------------------------
if(${SERIALBOX_USE_OPENSSL})
find_package(OpenSSL REQUIRED)
if(NOT(${OpenSSL_FOUND}))
message(FATAL_ERROR "OpenSSL library not found!")
endif()
include_directories(SYSTEM ${OPENSSL_INCLUDE_DIR})
set(SERIALBOX_EXTERNAL_LIBRARIES ${SERIALBOX_EXTERNAL_LIBRARIES} ${OPENSSL_LIBRARIES})
set(SERIALBOX_HAS_OPENSSL 1)
endif()
#---------------------------------------- NetCDF ---------------------------------------------------
if(${SERIALBOX_USE_NETCDF})
find_package(NetCDF REQUIRED)
if(NOT(${NetCDF_FOUND}))
message(FATAL_ERROR "NetCDF library not found!")
endif()
set(SERIALBOX_HAS_NETCDF 1)
serialbox_install_targets( TARGETS NETCDF_TARGET )
endif()
#---------------------------------------- Python ---------------------------------------------------
if(SERIALBOX_ENABLE_PYTHON)
find_package(Python3 COMPONENTS Interpreter)
# Python tests are enabled by default if we can find "nose" and "numpy"
set(ENABLE_PYTHON_TESTS OFF)
if(SERIALBOX_TESTING AND PYTHONINTERP_FOUND)
include(FindPythonModule)
find_python_module(nose)
find_python_module(numpy)
if(PY_NOSE_FOUND AND PY_NUMPY_FOUND)
set(ENABLE_PYTHON_TESTS ON)
endif()
endif()
option(SERIALBOX_TESTING_PYTHON "Run Python tests" ${ENABLE_PYTHON_TESTS})
mark_as_advanced(ENABLE_PYTHON_TESTS)
endif(SERIALBOX_ENABLE_PYTHON)
#---------------------------------------- Filesystem -----------------------------------------------
# link with stdc++fs for gcc < 9.0 (in gcc 9 it's part of the runtime)
link_libraries( "$<$<AND:$<CXX_COMPILER_ID:GNU>,$<VERSION_LESS:$<CXX_COMPILER_VERSION>,9.0>>:-lstdc++fs>" )
#---------------------------------------- GridTools ------------------------------------------------
if(SERIALBOX_TESTING_GRIDTOOLS)
find_package(GridTools QUIET REQUIRED)
set(SERIALBOX_HAS_GRIDTOOLS 1)
endif(SERIALBOX_TESTING_GRIDTOOLS)
# If ${REQUIRED_BOOST_COMPONENTS} is empty we still need the headers
find_package(Boost 1.54 REQUIRED)
set(SERIALBOX_BOOST_VERSION ${Boost_VERSION})
#---------------------------------------- STELLA ---------------------------------------------------
if(SERIALBOX_TESTING_STELLA)
# If STELLA_ROOT is not provided, we try to find it in external/
set(STELLA_ROOT_ENV "$ENV{STELLA_ROOT}")
if(NOT(STELLA_ROOT) AND NOT(STELLA_ROOT_ENV) AND
EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/external/stella/CMakeLists.txt")
set(STELLA_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/external/stella")
set(BUILD_STELLA "ON" BOOL)
set(STELLA_INSTALL_DIR "${CMAKE_BINARY_DIR}/external/stella/install")
# Forward toolchain
set(CMAKE_EXTERNAL_ARGS -Wno-dev
-DSTELLA_ENABLE_BENCHMARK=OFF
-DSTELLA_ENABLE_COMMUNICATION=OFF
-DSTELLA_ENABLE_SERIALIZATION=OFF
-DSTELLA_ENABLE_TESTING=OFF
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
-DCMAKE_GENERATOR=${CMAKE_GENERATOR}
-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
-DCMAKE_INSTALL_PREFIX=${STELLA_INSTALL_DIR}
)
# Build STELLA
ExternalProject_Add(
stella
URL "${CMAKE_CURRENT_SOURCE_DIR}/external/stella"
PREFIX "external/stella"
CMAKE_ARGS ${CMAKE_EXTERNAL_ARGS}
)
message(STATUS "Building STELLA from: ${CMAKE_CURRENT_SOURCE_DIR}/external/stella")
set(STELLA_INCLUDE_DIRS "${STELLA_INSTALL_DIR}/include/STELLA")
set(STELLA_LIBRARIES "${STELLA_INSTALL_DIR}/lib/libSharedInfrastructure.a"
"${STELLA_INSTALL_DIR}/lib/libStella.a"
"${STELLA_INSTALL_DIR}/lib/libStellaUtils.a")
else()
find_package(STELLA QUIET REQUIRED)
endif()
include_directories(SYSTEM ${STELLA_INCLUDE_DIRS})
set(SERIALBOX_HAS_STELLA 1)
endif(SERIALBOX_TESTING_STELLA)
#---------------------------------------- ClangTools -----------------------------------------------
find_package(ClangTools)
if("$ENV{CMAKE_EXPORT_COMPILE_COMMANDS}" STREQUAL "1" OR CLANG_TIDY_FOUND)
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
endif()
if(CLANG_FORMAT_BIN)
# Runs clang format and updates files in place.
add_custom_target(format
COMMAND ${SCRIPT_DIR}/run-clang-format.sh
${CMAKE_CURRENT_SOURCE_DIR}
${CLANG_FORMAT_BIN}
1
`find ${CMAKE_CURRENT_SOURCE_DIR}/src
-name \*.h -print -o -name \*.cpp -print`)
# Runs clang format and exits with a non-zero exit code if any files need to be reformatted
add_custom_target(check-format
COMMAND ${SCRIPT_DIR}/run-clang-format.sh
${CMAKE_CURRENT_SOURCE_DIR}
${CLANG_FORMAT_BIN}
0
`find ${CMAKE_CURRENT_SOURCE_DIR}/src
-name \*.h -print -o -name \*.cpp -print`)
endif()
if(CLANG_TIDY_BIN)
# Runs clang-tidy and attempts to fix any warning automatically
add_custom_target(clang-tidy
COMMAND ${SCRIPT_DIR}/run-clang-tidy.sh
${CLANG_TIDY_BIN}
${CMAKE_BINARY_DIR}/compile_commands.json
1
`find ${CMAKE_CURRENT_SOURCE_DIR}/src
-name \*.h -print -o -name \*.cpp -print`)
# Runs clang-tidy and exits with a non-zero exit code if any errors are found.
add_custom_target(check-clang-tidy
COMMAND ${SCRIPT_DIR}/run-clang-tidy.sh
${CLANG_TIDY_BIN}
${CMAKE_BINARY_DIR}/compile_commands.json
0
`find ${CMAKE_CURRENT_SOURCE_DIR}/src
-name \*.h -print -o -name \*.cpp -print`)
endif()
# --------------------------------------- Code Coverage --------------------------------------------
if(SERIALBOX_CODE_COVERAGE)
include(SerialboxCoverage)
serialbox_enable_coverage(${CMAKE_BINARY_DIR})
set(SERIALBOX_EXTERNAL_LIBRARIES ${SERIALBOX_EXTERNAL_LIBRARIES}
${CMAKE_SHARED_LINKER_FLAGS_COVERAGE})
endif(SERIALBOX_CODE_COVERAGE)
#---------------------------------------- Compilation ----------------------------------------------
# Generate serialbox/core/Config.h
set(SERIALBOX_CONFIG_FILE_DISCLAIMER "WARNING! All changes made in this file will be lost!")
set(SERIALBOX_CXX_CONFIG_FILE_IN ${PROJECT_SOURCE_DIR}/src/serialbox/core/Config.h.cmake)
set(SERIALBOX_CXX_CONFIG_FILE ${PROJECT_SOURCE_DIR}/src/serialbox/core/Config.h)
configure_file(${SERIALBOX_CXX_CONFIG_FILE_IN} ${SERIALBOX_CXX_CONFIG_FILE})
install(FILES ${SERIALBOX_CXX_CONFIG_FILE}
DESTINATION ${CMAKE_INSTALL_PREFIX}/include/serialbox/core)
# Install serialbox headers
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/src/serialbox DESTINATION include
FILES_MATCHING PATTERN "*.h" PATTERN "*.hpp")
if(SERIALBOX_ENABLE_C)
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/src/serialbox-c DESTINATION include
FILES_MATCHING PATTERN "*.h")
endif()
## Build Serialbox
add_subdirectory(src)
## Build unittests
if(SERIALBOX_TESTING)
enable_testing()
include(SerialboxTestScript)
serialbox_test_init()
if(SERIALBOX_TESTING_DEATH_TESTS)
add_definitions(-DSERIALBOX_RUN_DEATH_TESTS)
endif()
if(SERIALBOX_TESTING_LARGE_FILE_TESTS)
add_definitions(-DSERIALBOX_RUN_LARGE_FILE_TESTS)
endif()
# Generate utility/Config.h
set(SERIALBOX_CPP_CONFIG_FILE_IN ${PROJECT_SOURCE_DIR}/test/utility/Config.h.cmake)
set(SERIALBOX_CPP_CONFIG_FILE ${PROJECT_BINARY_DIR}/test/utility/Config.h)
configure_file(${SERIALBOX_CPP_CONFIG_FILE_IN} ${SERIALBOX_CPP_CONFIG_FILE})
include_directories(${PROJECT_BINARY_DIR}/test)
add_subdirectory(test)
endif(SERIALBOX_TESTING)
## Build examples
if(SERIALBOX_EXAMPLES)
add_subdirectory(examples)
endif(SERIALBOX_EXAMPLES)
## Build documentation
if(SERIALBOX_DOCUMENTATION)
add_subdirectory(docs)
endif()
if(SERIALBOX_TESTING)
serialbox_test_end()
endif(SERIALBOX_TESTING)
include(cmake/Packaging.cmake)