|
| 1 | +# Copyright (c) The Bitcoin Core developers |
| 2 | +# Distributed under the MIT software license, see the accompanying |
| 3 | +# file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 4 | + |
| 5 | +cmake_minimum_required(VERSION 3.12) |
| 6 | + |
| 7 | +project("Libmultiprocess" CXX) |
| 8 | +if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR) |
| 9 | + set(CMAKE_CXX_STANDARD 20) |
| 10 | + set(CMAKE_CXX_STANDARD_REQUIRED YES) |
| 11 | +endif() |
| 12 | + |
| 13 | +include("cmake/compat_find.cmake") |
| 14 | + |
| 15 | +find_package(Threads REQUIRED) |
| 16 | +find_package(CapnProto 0.7 REQUIRED) |
| 17 | + |
| 18 | +# Check for list-of-pointers memory access bug from Nov 2022 |
| 19 | +# https://nvd.nist.gov/vuln/detail/CVE-2022-46149 |
| 20 | +# https://github.com/advisories/GHSA-qqff-4vw4-f6hx |
| 21 | +# https://github.com/capnproto/capnproto/security/advisories/GHSA-qqff-4vw4-f6hx |
| 22 | +# https://github.com/capnproto/capnproto/blob/master/security-advisories/2022-11-30-0-pointer-list-bounds.md |
| 23 | +# https://capnproto.org/news/2022-11-30-CVE-2022-46149-security-advisory.html |
| 24 | +# https://dwrensha.github.io/capnproto-rust/2022/11/30/out_of_bounds_memory_access_bug.html |
| 25 | +if(CapnProto_VERSION STREQUAL "0.7.0" |
| 26 | + OR CapnProto_VERSION STREQUAL "0.8.0" |
| 27 | + OR CapnProto_VERSION STREQUAL "0.9.0" |
| 28 | + OR CapnProto_VERSION STREQUAL "0.9.1" |
| 29 | + OR CapnProto_VERSION STREQUAL "0.10.0" |
| 30 | + OR CapnProto_VERSION STREQUAL "0.10.1" |
| 31 | + OR CapnProto_VERSION STREQUAL "0.10.2") |
| 32 | + message(FATAL_ERROR |
| 33 | + "Cap'n Proto ${CapnProto_VERSION} is affected by CVE-2022-46149.\n" |
| 34 | + "Please install an updated package.\n" |
| 35 | + "Details: https://github.com/advisories/GHSA-qqff-4vw4-f6hx |
| 36 | + ") |
| 37 | +endif() |
| 38 | + |
| 39 | +set(MPGEN_EXECUTABLE "" CACHE FILEPATH "If specified, should be full path to an external mpgen binary to use rather than the one built internally.") |
| 40 | + |
| 41 | +option(MP_ENABLE_CLANG_TIDY "Run clang-tidy with the compiler." OFF) |
| 42 | +if(MP_ENABLE_CLANG_TIDY) |
| 43 | + find_program(CLANG_TIDY_EXECUTABLE NAMES clang-tidy) |
| 44 | + if(NOT CLANG_TIDY_EXECUTABLE) |
| 45 | + message(FATAL_ERROR "MP_ENABLE_CLANG_TIDY is ON but clang-tidy is not found.") |
| 46 | + endif() |
| 47 | + set(CMAKE_CXX_CLANG_TIDY "${CLANG_TIDY_EXECUTABLE}") |
| 48 | + |
| 49 | + # Workaround for nix from https://gitlab.kitware.com/cmake/cmake/-/issues/20912#note_793338 |
| 50 | + # Nix injects header paths via $NIX_CFLAGS_COMPILE; CMake tags these as |
| 51 | + # CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES and omits them from the compile |
| 52 | + # database, so clang-tidy, which ignores $NIX_CFLAGS_COMPILE, can't find capnp |
| 53 | + # headers. Setting them as standard passes them to clang-tidy. |
| 54 | + set(CMAKE_CXX_STANDARD_INCLUDE_DIRECTORIES ${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES}) |
| 55 | +endif() |
| 56 | + |
| 57 | +option(MP_ENABLE_IWYU "Run include-what-you-use with the compiler." OFF) |
| 58 | +if(MP_ENABLE_IWYU) |
| 59 | + find_program(IWYU_EXECUTABLE NAMES include-what-you-use iwyu) |
| 60 | + if(NOT IWYU_EXECUTABLE) |
| 61 | + message(FATAL_ERROR "MP_ENABLE_IWYU is ON but include-what-you-use was not found.") |
| 62 | + endif() |
| 63 | + set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE "${IWYU_EXECUTABLE};-Xiwyu;--error") |
| 64 | + if(DEFINED ENV{IWYU_MAPPING_FILE}) |
| 65 | + list(APPEND CMAKE_CXX_INCLUDE_WHAT_YOU_USE "-Xiwyu" "--mapping_file=$ENV{IWYU_MAPPING_FILE}") |
| 66 | + endif() |
| 67 | +endif() |
| 68 | + |
| 69 | +include("cmake/compat_config.cmake") |
| 70 | +include("cmake/pthread_checks.cmake") |
| 71 | +include(GNUInstallDirs) |
| 72 | + |
| 73 | +# Set MP_INCLUDE_DIR as a global property so target_capnp_sources function can |
| 74 | +# use it, and its callers don't need to specify the include directory manually |
| 75 | +# to avoid "error: Import failed: /mp/proxy.capnp" failures from capnproto. |
| 76 | +set_property(GLOBAL PROPERTY MP_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/include") |
| 77 | + |
| 78 | +# Set a convenience variable for subdirectories. |
| 79 | +if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR) |
| 80 | + set(MP_STANDALONE TRUE) |
| 81 | + include(CTest) |
| 82 | +else() |
| 83 | + set(MP_STANDALONE FALSE) |
| 84 | +endif() |
| 85 | + |
| 86 | +# Prevent include directories from parent project from leaking into this one. |
| 87 | +set_property(DIRECTORY PROPERTY INCLUDE_DIRECTORIES "") |
| 88 | + |
| 89 | +# Generated C++ preprocessor defines |
| 90 | +configure_file(include/mp/config.h.in "${CMAKE_CURRENT_BINARY_DIR}/include/mp/config.h") |
| 91 | + |
| 92 | +# Generated C++ Capn'Proto schema files |
| 93 | +capnp_generate_cpp(MP_PROXY_SRCS MP_PROXY_HDRS include/mp/proxy.capnp) |
| 94 | +set_source_files_properties("${MP_PROXY_SRCS}" PROPERTIES SKIP_LINTING TRUE) # Ignored before cmake 3.27 |
| 95 | + |
| 96 | +# util library |
| 97 | +add_library(mputil OBJECT src/mp/util.cpp) |
| 98 | +target_include_directories(mputil PRIVATE |
| 99 | + $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> |
| 100 | + $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>) |
| 101 | +target_link_libraries(mputil PUBLIC CapnProto::kj) |
| 102 | + |
| 103 | +# libmultiprocess.a runtime library |
| 104 | +set(MP_PUBLIC_HEADERS |
| 105 | + ${MP_PROXY_HDRS} |
| 106 | + include/mp/proxy-io.h |
| 107 | + include/mp/proxy-types.h |
| 108 | + include/mp/proxy.h |
| 109 | + include/mp/type-char.h |
| 110 | + include/mp/type-chrono.h |
| 111 | + include/mp/type-context.h |
| 112 | + include/mp/type-data.h |
| 113 | + include/mp/type-decay.h |
| 114 | + include/mp/type-exception.h |
| 115 | + include/mp/type-function.h |
| 116 | + include/mp/type-interface.h |
| 117 | + include/mp/type-map.h |
| 118 | + include/mp/type-message.h |
| 119 | + include/mp/type-number.h |
| 120 | + include/mp/type-optional.h |
| 121 | + include/mp/type-pair.h |
| 122 | + include/mp/type-pointer.h |
| 123 | + include/mp/type-set.h |
| 124 | + include/mp/type-string.h |
| 125 | + include/mp/type-struct.h |
| 126 | + include/mp/type-threadmap.h |
| 127 | + include/mp/type-tuple.h |
| 128 | + include/mp/type-vector.h |
| 129 | + include/mp/type-void.h |
| 130 | + include/mp/util.h) |
| 131 | +add_library(multiprocess STATIC |
| 132 | + ${MP_PROXY_SRCS} |
| 133 | + ${MP_PUBLIC_HEADERS} |
| 134 | + src/mp/proxy.cpp |
| 135 | + $<TARGET_OBJECTS:mputil>) |
| 136 | +add_library(Libmultiprocess::multiprocess ALIAS multiprocess) |
| 137 | +target_include_directories(multiprocess PUBLIC |
| 138 | + $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include> |
| 139 | + $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> |
| 140 | + $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>) |
| 141 | +target_link_libraries(multiprocess PUBLIC CapnProto::capnp) |
| 142 | +target_link_libraries(multiprocess PUBLIC CapnProto::capnp-rpc) |
| 143 | +target_link_libraries(multiprocess PUBLIC CapnProto::kj) |
| 144 | +target_link_libraries(multiprocess PUBLIC CapnProto::kj-async) |
| 145 | +set_target_properties(multiprocess PROPERTIES |
| 146 | + PUBLIC_HEADER "${MP_PUBLIC_HEADERS}") |
| 147 | +install(TARGETS multiprocess EXPORT LibTargets |
| 148 | + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT lib |
| 149 | + PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/mp COMPONENT lib) |
| 150 | + |
| 151 | +# mpgen code generator |
| 152 | +add_executable(mpgen src/mp/gen.cpp $<TARGET_OBJECTS:mputil>) |
| 153 | +add_executable(Libmultiprocess::mpgen ALIAS mpgen) |
| 154 | +target_include_directories(mpgen PRIVATE $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>) |
| 155 | +target_include_directories(mpgen PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>) |
| 156 | +target_link_libraries(mpgen PRIVATE CapnProto::capnp) |
| 157 | +target_link_libraries(mpgen PRIVATE CapnProto::capnp-rpc) |
| 158 | +target_link_libraries(mpgen PRIVATE CapnProto::capnpc) |
| 159 | +target_link_libraries(mpgen PRIVATE CapnProto::kj) |
| 160 | +target_link_libraries(mpgen PRIVATE Threads::Threads) |
| 161 | +set_target_properties(mpgen PROPERTIES |
| 162 | + INSTALL_RPATH_USE_LINK_PATH TRUE) |
| 163 | +set_target_properties(mpgen PROPERTIES |
| 164 | + PUBLIC_HEADER include/mp/proxy.capnp) |
| 165 | +install(TARGETS mpgen EXPORT BinTargets |
| 166 | + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT bin |
| 167 | + PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/mp COMPONENT bin) |
| 168 | + |
| 169 | +# makefile include to invoke mpgen code generator, for downstream Make projects |
| 170 | +install(FILES "include/mpgen.mk" |
| 171 | + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} COMPONENT bin) |
| 172 | + |
| 173 | +# pkg-config module to build against libmultiprocess library, for downstream autoconf projects |
| 174 | +configure_file(pkgconfig/libmultiprocess.pc.in "${CMAKE_CURRENT_BINARY_DIR}/pkgconfig/libmultiprocess.pc" @ONLY) |
| 175 | +install(FILES "${CMAKE_CURRENT_BINARY_DIR}/pkgconfig/libmultiprocess.pc" |
| 176 | + DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig COMPONENT lib) |
| 177 | + |
| 178 | +# cmake include to invoke mpgen code generator, for downstream CMake projects |
| 179 | +install( |
| 180 | + FILES |
| 181 | + ${CMAKE_CURRENT_SOURCE_DIR}/cmake/TargetCapnpSources.cmake |
| 182 | + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/Libmultiprocess COMPONENT bin) |
| 183 | + |
| 184 | +# CMake target import files, for downstream CMake projects |
| 185 | +install(EXPORT BinTargets |
| 186 | + NAMESPACE Libmultiprocess:: |
| 187 | + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/Libmultiprocess COMPONENT bin) |
| 188 | +install(EXPORT LibTargets |
| 189 | + NAMESPACE Libmultiprocess:: |
| 190 | + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/Libmultiprocess COMPONENT lib) |
| 191 | + |
| 192 | +# CMake find_package config file, for downstream CMake projects |
| 193 | +include(CMakePackageConfigHelpers) |
| 194 | +configure_package_config_file( |
| 195 | + ${PROJECT_SOURCE_DIR}/cmake/Config.cmake.in |
| 196 | + LibmultiprocessConfig.cmake |
| 197 | + INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/Libmultiprocess |
| 198 | + NO_SET_AND_CHECK_MACRO) |
| 199 | +install( |
| 200 | + FILES |
| 201 | + ${CMAKE_CURRENT_BINARY_DIR}/LibmultiprocessConfig.cmake |
| 202 | + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/Libmultiprocess |
| 203 | + COMPONENT common) |
| 204 | + |
| 205 | +# Makefile targets to support "make install-bin" "make install-lib" |
| 206 | +add_custom_target(install-bin |
| 207 | + COMMAND ${CMAKE_COMMAND} -DCOMPONENT=bin -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_install.cmake |
| 208 | + COMMAND ${CMAKE_COMMAND} -DCOMPONENT=common -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_install.cmake |
| 209 | + VERBATIM) |
| 210 | +add_dependencies(install-bin mpgen) |
| 211 | +add_custom_target(install-lib |
| 212 | + COMMAND ${CMAKE_COMMAND} -DCOMPONENT=lib -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_install.cmake |
| 213 | + COMMAND ${CMAKE_COMMAND} -DCOMPONENT=common -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_install.cmake |
| 214 | + VERBATIM) |
| 215 | +add_dependencies(install-lib multiprocess) |
| 216 | + |
| 217 | +# Example and test subdirectories |
| 218 | +add_subdirectory(example EXCLUDE_FROM_ALL) |
| 219 | +add_subdirectory(test EXCLUDE_FROM_ALL) |
0 commit comments