forked from actor-framework/actor-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
474 lines (400 loc) · 17.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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
cmake_minimum_required(VERSION 3.13..3.27 FATAL_ERROR)
project(CAF VERSION 1.0.0 LANGUAGES CXX)
# -- version boilerplate -------------------------------------------------------
# Individual components of the version number.
set(CAF_VERSION_MAJOR ${PROJECT_VERSION_MAJOR}
CACHE INTERNAL "The major version of CAF" FORCE)
set(CAF_VERSION_MINOR ${PROJECT_VERSION_MINOR}
CACHE INTERNAL "The minor version of CAF" FORCE)
set(CAF_VERSION_PATCH ${PROJECT_VERSION_PATCH}
CACHE INTERNAL "The patch version of CAF" FORCE)
# The full version number as human-readable string.
set(CAF_VERSION "${CAF_VERSION_MAJOR}.${CAF_VERSION_MINOR}.${CAF_VERSION_PATCH}"
CACHE INTERNAL "The full CAF version string" FORCE)
# The full version number as integer.
if(CAF_VERSION_MAJOR LESS 1)
set(CAF_VERSION_INT ${CAF_VERSION_MAJOR})
else()
set(CAF_VERSION_INT "")
endif()
if(CAF_VERSION_MINOR LESS 10)
set(CAF_VERSION_INT ${CAF_VERSION_INT}0${CAF_VERSION_MINOR})
else()
set(CAF_VERSION_INT ${CAF_VERSION_INT}${CAF_VERSION_MINOR})
endif()
if(CAF_VERSION_PATCH LESS 10)
set(CAF_VERSION_INT ${CAF_VERSION_INT}0${CAF_VERSION_PATCH})
else()
set(CAF_VERSION_INT ${CAF_VERSION_INT}${CAF_VERSION_PATCH})
endif()
# -- includes ------------------------------------------------------------------
include(CMakeDependentOption)
include(CMakePackageConfigHelpers)
include(CheckCXXSourceCompiles)
include(CheckCXXSourceRuns)
include(GNUInstallDirs)
include(GenerateExportHeader)
# -- override CMake defaults for internal cache entries ------------------------
set(CMAKE_EXPORT_COMPILE_COMMANDS ON
CACHE INTERNAL "Write JSON compile commands database")
# -- general options -----------------------------------------------------------
option(BUILD_SHARED_LIBS "Build shared library targets" ON)
option(THREADS_PREFER_PTHREAD_FLAG "Prefer -pthread flag if available " ON)
# -- CAF options that are off by default ---------------------------------------
option(CAF_ENABLE_CPACK "Enable packaging via CPack" OFF)
option(CAF_ENABLE_CURL_EXAMPLES "Build examples with libcurl" OFF)
option(CAF_ENABLE_PROTOBUF_EXAMPLES "Build examples with Google Protobuf" OFF)
option(CAF_ENABLE_QT6_EXAMPLES "Build examples with the Qt6 framework" OFF)
option(CAF_ENABLE_ROBOT_TESTS "Add the Robot tests to CTest " OFF)
option(CAF_ENABLE_RUNTIME_CHECKS "Build CAF with extra runtime assertions" OFF)
# -- CAF options that are on by default ----------------------------------------
option(CAF_ENABLE_EXAMPLES "Build small programs showcasing CAF features" ON)
option(CAF_ENABLE_EXCEPTIONS "Build CAF with support for exceptions" ON)
option(CAF_ENABLE_IO_MODULE "Build legacy networking I/O module" ON)
option(CAF_ENABLE_NET_MODULE "Build networking I/O module" ON)
option(CAF_ENABLE_TESTING "Build unit test suites" ON)
# -- CAF options that depend on others -----------------------------------------
cmake_dependent_option(CAF_ENABLE_OPENSSL_MODULE "Build OpenSSL module" ON
"CAF_ENABLE_IO_MODULE" OFF)
# -- CAF options with non-boolean values ---------------------------------------
set(CAF_CXX_VERSION 17 CACHE STRING "Set the C++ version to use for CAF")
set(CAF_LOG_LEVEL "QUIET" CACHE STRING "Set log verbosity of CAF components")
set(CAF_EXCLUDE_TESTS "" CACHE STRING "List of excluded test suites")
set(CAF_SANITIZERS "" CACHE STRING
"Comma separated sanitizers, e.g., 'address,undefined'")
set(CAF_BUILD_INFO_FILE_PATH "" CACHE FILEPATH
"Optional path for writing CMake and compiler version information")
# -- macOS-specific options ----------------------------------------------------
if(APPLE)
set(CMAKE_MACOSX_RPATH ON CACHE INTERNAL "Use rpaths on macOS and iOS")
endif()
# -- project-specific CMake settings -------------------------------------------
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
# -- sanity checking -----------------------------------------------------------
if(CAF_ENABLE_OPENSSL_MODULE AND NOT CAF_ENABLE_IO_MODULE)
message(FATAL_ERROR "Invalid options: cannot build OpenSSL without I/O")
endif()
set(CAF_VALID_LOG_LEVELS QUIET ERROR WARNING INFO DEBUG TRACE)
if(NOT CAF_LOG_LEVEL IN_LIST CAF_VALID_LOG_LEVELS)
message(FATAL_ERROR "Invalid log level: \"${CAF_LOG_LEVEL}\"")
endif()
if(MSVC AND CAF_SANITIZERS)
message(FATAL_ERROR "Sanitizer builds are currently not supported on MSVC")
endif()
# -- get dependencies that are used in more than one module --------------------
if(CAF_ENABLE_OPENSSL_MODULE OR CAF_ENABLE_NET_MODULE)
if(NOT TARGET OpenSSL::SSL OR NOT TARGET OpenSSL::Crypto)
find_package(OpenSSL REQUIRED)
endif()
endif()
# -- base target setup ---------------------------------------------------------
# This target propagates compiler flags, extra dependencies, etc. All other CAF
# targets pull this target in as a PRIVATE dependency. Users that embed CAF into
# their own CMake scaffold (e.g., via FetchContent) may pass this target in with
# some properties predefined in order to force compiler flags or dependencies.
if(NOT TARGET caf_internal)
message(STATUS "Build CAF with C++${CAF_CXX_VERSION}")
add_library(caf_internal INTERFACE)
target_compile_features(caf_internal INTERFACE cxx_std_${CAF_CXX_VERSION})
endif()
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU")
target_compile_options(caf_internal INTERFACE -Wall -Wextra -pedantic
-ftemplate-depth=512 -ftemplate-backtrace-limit=0)
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
target_compile_options(caf_internal INTERFACE -Wdocumentation)
else()
target_compile_options(caf_internal INTERFACE
-Wno-missing-field-initializers)
endif()
elseif(MSVC)
target_compile_options(caf_internal INTERFACE /EHsc)
endif()
# -- unit testing setup --------------------------------------------------------
if(CAF_ENABLE_TESTING)
enable_testing()
endif()
# -- check whether we can use std::format --------------------------------------
if(NOT DEFINED CAF_USE_STD_FORMAT)
set(CAF_USE_STD_FORMAT OFF CACHE BOOL "Enable std::format support" FORCE)
if(NOT CMAKE_CROSSCOMPILING)
set(snippet "#include <format>
#include <iostream>
int main() { std::cout << std::format(\"{}\", \"ok\"); }")
check_cxx_source_runs("${snippet}" snippet_output)
if("${snippet_output}" STREQUAL "ok")
message(STATUS "Enable std::format support")
set(CAF_USE_STD_FORMAT ON CACHE BOOL "Enable std::format support" FORCE)
else()
message(STATUS "Disable std::format support: NOT available")
endif()
endif()
endif()
# -- export internal target (may be useful for re-using compiler flags) --------
set_target_properties(caf_internal PROPERTIES EXPORT_NAME internal)
add_library(CAF::internal ALIAS caf_internal)
install(TARGETS caf_internal EXPORT CAFTargets)
# -- create the libcaf_test target ahead of time for caf_core ------------------
add_library(libcaf_test)
# -- add uninstall target if it does not exist yet -----------------------------
if(NOT TARGET uninstall)
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/cmake/uninstall.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/uninstall.cmake"
IMMEDIATE @ONLY)
add_custom_target(
uninstall
COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/uninstall.cmake)
endif()
# -- utility functions ---------------------------------------------------------
# generates the implementation file for the enum that contains to_string,
# from_string and from_integer
function(caf_add_enum_type target enum_name)
string(REPLACE "." "/" path "${enum_name}")
set(hpp_file "${CMAKE_CURRENT_SOURCE_DIR}/caf/${path}.hpp")
set(cpp_file "${CMAKE_CURRENT_BINARY_DIR}/src/${path}_strings.cpp")
set(gen_file "${PROJECT_SOURCE_DIR}/cmake/caf-generate-enum-strings.cmake")
add_custom_command(OUTPUT "${cpp_file}"
COMMAND ${CMAKE_COMMAND}
"-DINPUT_FILE=${hpp_file}"
"-DOUTPUT_FILE=${cpp_file}"
-P "${gen_file}"
DEPENDS "${hpp_file}" "${gen_file}")
target_sources(${target} PRIVATE "${cpp_file}")
endfunction()
function(caf_export_and_install_lib component)
add_library(CAF::${component} ALIAS libcaf_${component})
string(TOUPPER "CAF_${component}_EXPORT" export_macro_name)
generate_export_header(
libcaf_${component}
EXPORT_MACRO_NAME ${export_macro_name}
EXPORT_FILE_NAME "caf/detail/${component}_export.hpp")
set_target_properties(libcaf_${component} PROPERTIES
EXPORT_NAME ${component}
SOVERSION ${CAF_VERSION}
VERSION ${CAF_VERSION_MAJOR}
OUTPUT_NAME caf_${component})
install(TARGETS libcaf_${component}
EXPORT CAFTargets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT ${component}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT ${component}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT ${component})
install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/caf"
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
COMPONENT ${component}
FILES_MATCHING PATTERN "*.hpp"
PATTERN "*/internal/*" EXCLUDE)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/caf/detail/${component}_export.hpp"
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/caf/detail/")
endfunction()
# -- convenience function for automating our component setup -------------------
function(caf_add_log_component name)
set(CAF_COMPONENT_NAME ${name})
configure_file("${PROJECT_SOURCE_DIR}/cmake/log.hpp.in"
"${CMAKE_CURRENT_BINARY_DIR}/caf/log/${name}.hpp"
@ONLY)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/caf/log/${name}.hpp"
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/caf/log/")
endfunction()
# Usage:
# caf_add_component(
# foo
# DEPENDENCIES
# INTERFACE
# ...
# PUBLIC
# ...
# PRIVATE
# ...
# HEADERS
# ...
# SOURCES
# ...
# )
function(caf_add_component name)
set(varargs DEPENDENCIES HEADERS SOURCES ENUM_TYPES)
cmake_parse_arguments(CAF_ADD_COMPONENT "" "" "${varargs}" ${ARGN})
if(NOT CAF_ADD_COMPONENT_HEADERS)
message(FATAL_ERROR "Cannot add CAF component without at least one header.")
endif()
if(NOT CAF_ADD_COMPONENT_SOURCES)
message(FATAL_ERROR "Cannot add CAF component without at least one source.")
endif()
foreach(param DEPENDENCIES HEADERS SOURCES)
if(NOT CAF_ADD_COMPONENT_${param})
message(FATAL_ERROR "caf_add_component(): missing parameter ${param}")
endif()
endforeach()
set(lib_target "libcaf_${name}")
if(NOT TARGET ${lib_target})
add_library(${lib_target} "${PROJECT_SOURCE_DIR}/cmake/dummy.cpp")
endif()
set_property(TARGET ${lib_target} PROPERTY POSITION_INDEPENDENT_CODE ON)
target_link_libraries(${lib_target} ${CAF_ADD_COMPONENT_DEPENDENCIES})
target_compile_definitions(${lib_target} PRIVATE "libcaf_${name}_EXPORTS")
target_include_directories(${lib_target} PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
$<INSTALL_INTERFACE:include>)
caf_add_log_component(${name})
if(BUILD_SHARED_LIBS)
set_target_properties(${lib_target} PROPERTIES
CXX_VISIBILITY_PRESET hidden
VISIBILITY_INLINES_HIDDEN ON)
endif()
if(CAF_ENABLE_TESTING)
add_executable("caf-${name}-test" ${CMAKE_CURRENT_SOURCE_DIR}/main.test.cpp)
target_compile_definitions(
"caf-${name}-test" PRIVATE
CAF_TEST_DATA_DIR="${CMAKE_CURRENT_SOURCE_DIR}/tests/data")
target_link_libraries("caf-${name}-test" PRIVATE libcaf_test
${CAF_ADD_COMPONENT_DEPENDENCIES} ${lib_target})
endif()
foreach(source_file ${CAF_ADD_COMPONENT_SOURCES})
get_filename_component(ext ${source_file} EXT)
if(ext STREQUAL ".cpp")
target_sources(${lib_target} PRIVATE ${source_file})
elseif(ext STREQUAL ".test.cpp" AND CAF_ENABLE_TESTING)
get_filename_component(test_name ${source_file} NAME_WE)
get_filename_component(test_path ${source_file} DIRECTORY)
string(REPLACE "/" "." test_name "${test_path}/${test_name}")
target_sources("caf-${name}-test" PRIVATE ${source_file})
set_property(SOURCE ${source_file} PROPERTY COMPILE_DEFINITIONS
CAF_TEST_SUITE_NAME=${test_name})
add_test(NAME ${test_name} COMMAND "caf-${name}-test" -s "^${test_name}$" -n -v5)
set_tests_properties(${test_name} PROPERTIES TIMEOUT 300)
if (MSVC)
set_source_files_properties(${source_file} PROPERTIES
COMPILE_FLAGS /wd4996)
else()
set_source_files_properties(${source_file} PROPERTIES
COMPILE_FLAGS -Wno-deprecated-declarations)
endif()
endif()
endforeach()
target_link_libraries(${lib_target} ${CAF_ADD_COMPONENT_DEPENDENCIES})
if(CAF_ADD_COMPONENT_ENUM_TYPES)
# OBJECT lib: workaround for https://gitlab.kitware.com/cmake/cmake/-/issues/18399
add_library(${name}_enums OBJECT)
set_property(TARGET ${name}_enums PROPERTY POSITION_INDEPENDENT_CODE ON)
target_link_libraries(${name}_enums CAF::internal)
if(NOT name STREQUAL core)
# Implementations need access to headers such as config.hpp.
target_link_libraries(${name}_enums CAF::core)
endif()
target_include_directories(${name}_enums PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_BINARY_DIR})
foreach(enum_name ${CAF_ADD_COMPONENT_ENUM_TYPES})
caf_add_enum_type(${name}_enums ${enum_name})
endforeach()
target_sources(${lib_target} PRIVATE $<TARGET_OBJECTS:${name}_enums>)
target_compile_definitions(${name}_enums PRIVATE libcaf_${name}_EXPORTS)
endif()
caf_export_and_install_lib(${name})
endfunction()
# -- convenience function for adding executable targets of tests ---------------
# Usage:
# caf_add_test_executable(
# foo
# DEPENDENCIES
# ...
# SOURCES
# ...
# )
function(caf_add_test_executable name)
if(NOT CAF_ENABLE_TESTING)
return()
endif()
set(varargs DEPENDENCIES SOURCES)
cmake_parse_arguments(args "" "" "${varargs}" ${ARGN})
if(NOT args_SOURCES)
message(FATAL_ERROR "Cannot add a CAF test executable without sources.")
endif()
add_executable(${name} ${args_SOURCES})
target_link_libraries(${name} PRIVATE CAF::internal ${args_DEPENDENCIES})
endfunction()
# -- build all components the user asked for -----------------------------------
add_subdirectory(libcaf_core)
add_subdirectory(libcaf_test)
if(CAF_ENABLE_NET_MODULE)
add_subdirectory(libcaf_net)
endif()
if(CAF_ENABLE_IO_MODULE)
add_subdirectory(libcaf_io)
endif()
if(CAF_ENABLE_OPENSSL_MODULE)
add_subdirectory(libcaf_openssl)
endif()
if(CAF_ENABLE_EXAMPLES)
add_subdirectory(examples)
endif()
# -- optionally add the Robot tests to CTest -----------------------------------
if(CAF_ENABLE_TESTING AND CAF_ENABLE_ROBOT_TESTS)
add_subdirectory(robot)
endif()
# -- add top-level compiler and linker flags that propagate to clients ---------
# Disable warnings regarding C++ classes at ABI boundaries on MSVC.
if(BUILD_SHARED_LIBS AND MSVC)
target_compile_options(libcaf_core INTERFACE /wd4275 /wd4251)
endif()
# Propgatate sanitizer flags to downstream targets.
if(CAF_SANITIZERS)
foreach(target caf_internal libcaf_core)
target_compile_options(${target} INTERFACE
-fsanitize=${CAF_SANITIZERS}
-fno-omit-frame-pointer)
if(CMAKE_VERSION VERSION_LESS 3.13)
target_link_libraries(${target} INTERFACE
-fsanitize=${CAF_SANITIZERS}
-fno-omit-frame-pointer)
else()
target_link_options(${target} INTERFACE
-fsanitize=${CAF_SANITIZERS}
-fno-omit-frame-pointer)
endif()
endforeach()
endif()
# -- generate and install .cmake files -----------------------------------------
export(EXPORT CAFTargets FILE CAFTargets.cmake NAMESPACE CAF::)
install(EXPORT CAFTargets
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/CAF"
NAMESPACE CAF::)
write_basic_package_version_file(
"${CMAKE_CURRENT_BINARY_DIR}/CAFConfigVersion.cmake"
VERSION ${CAF_VERSION}
COMPATIBILITY SameMajorVersion)
configure_package_config_file(
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/CAFConfig.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/CAFConfig.cmake"
INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/CAF")
install(
FILES
"${CMAKE_CURRENT_BINARY_DIR}/CAFConfig.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/CAFConfigVersion.cmake"
DESTINATION
"${CMAKE_INSTALL_LIBDIR}/cmake/CAF")
# -- extra file output (primarily for CAF CI) ----------------------------------
if(CAF_BUILD_INFO_FILE_PATH)
configure_file("${PROJECT_SOURCE_DIR}/cmake/caf-build-info.txt.in"
"${CAF_BUILD_INFO_FILE_PATH}"
@ONLY)
endif()
# -- CPack setup ---------------------------------------------------------------
if(CAF_ENABLE_CPACK)
include(InstallRequiredSystemLibraries)
set(CPACK_PACKAGE_NAME "CAF")
set(CPACK_PACKAGE_VENDOR "CAF Community")
set(CPACK_RESOURCE_FILE_LICENSE "${PROJECT_SOURCE_DIR}/LICENSE")
set(CPACK_PACKAGE_VERSION_MAJOR "${CAF_VERSION_MAJOR}")
set(CPACK_PACKAGE_VERSION_MINOR "${CAF_VERSION_MINOR}")
set(CPACK_PACKAGE_VERSION_PATCH "${CAF_VERSION_PATCH}")
set(CPACK_PACKAGE_DESCRIPTION
"To learn more about CAF, please visit https://www.actor-framework.org.")
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY
"CAF offers high-level building blocks for concurrency & distribution.")
set(CPACK_PACKAGE_HOMEPAGE_URL "https://www.actor-framework.org")
set(CPACK_PACKAGE_ICON "${PROJECT_SOURCE_DIR}/doc/png/logo-1000x1000.png")
if(NOT CPACK_SOURCE_GENERATOR)
set(CPACK_SOURCE_GENERATOR "TGZ")
endif()
include(CPack)
endif()