bitcoin/cmake/TargetCapnpSources.cmake
Ryan Ofsky 7562e2aeed Squashed 'src/ipc/libmultiprocess/' changes from a4f92969649..1fc65008f7d
1fc65008f7d Merge bitcoin-core/libmultiprocess#237: Made SpawnProcess() behavior safe post fork()
5205a87cd90 test: check SpawnProcess post-fork safety
69652f0edfa Precompute argv before fork in SpawnProcess
30a8681de62 SpawnProcess: avoid fd leak on close failure
d0fc1081d09 Merge bitcoin-core/libmultiprocess#196: ci: Add NetBSD job
7b171f45bfc Merge bitcoin-core/libmultiprocess#234: doc: Fix typos and grammar in documentation and comments
861da39cae9 ci: Add NetBSD job
458745e3940 Fix various typos, spelling mistakes, and grammatical errors in design.md and source code comments.
585decc8561 Merge bitcoin-core/libmultiprocess#236: ci: Install binary package `capnproto` on OpenBSD instead of building it
14e926a3ff3 refactor: extract MakeArgv helper
1ee909393f4 ci: Install binary package `capnproto` on OpenBSD instead of building it
470fc518d4b Merge bitcoin-core/libmultiprocess#230: cmake: add ONLY_CAPNP target_capnp_sources option
2d8886f26c4 Merge bitcoin-core/libmultiprocess#228: Add versions.md and version.h files describing version branches and tags
c1838be565d Merge bitcoin-core/libmultiprocess#225: Improve and document act support
a173f1704ce Merge bitcoin-core/libmultiprocess#223: ci: Replace nix-shell with equivalent nix develop command
625eaca42fb Merge bitcoin-core/libmultiprocess#229: Design Documentation Update
cc234be73a6 Design doc update
81c652687b8 cmake: add ONLY_CAPNP target_capnp_sources option
6e01d2d766e Add versions.md and version.h files describing version branches and tags
4e3f8fa0d2c doc: add instructions for using act
81712ff6bbf ci: disable KVM and sandbox inside act containers
18a2237a8ef ci: Replace nix-shell with equivalent nix develop command

git-subtree-dir: src/ipc/libmultiprocess
git-subtree-split: 1fc65008f7d64161e84c08cbd93109a23dd6a1e9
2026-01-13 07:24:18 -05:00

121 lines
4.7 KiB
CMake

# Copyright (c) 2024-present The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or https://opensource.org/license/mit/.
#[=[
target_capnp_sources
--------------------
This function adds build steps to generate C++ files from Cap'n Proto files
and build them as part of a specified target.
Arguments:
target: The name of the CMake target (e.g., a library or executable) to
which the generated source files will be added. This target must already
be defined elsewhere in the CMake scripts.
include_prefix: Absolute path indicating what portion of capnp source paths
should be used in relative #include statements in the generated C++
files. For example, if the .capnp path is /home/src/lib/schema.capnp
and include_prefix is /home/src, generated includes look like:
#include <lib/schema.capnp.h>
And if include_prefix is /home/src/lib, generated includes look like:
#include <schema.capnp.h>
The specified include_prefix should be ${CMAKE_SOURCE_DIR} or a
subdirectory of it to include files relative to the project root. It can
be ${CMAKE_CURRENT_SOURCE_DIR} to include files relative to the current
source directory.
Additional Unnamed Arguments:
After `target` and `include_prefix`, all unnamed arguments are treated as
paths to `.capnp` schema files. These should be paths relative to
${CMAKE_CURRENT_SOURCE_DIR}.
Optional Keyword Arguments:
IMPORT_PATHS: Specifies additional directories to search for imported
`.capnp` files.
Example:
# Assuming `my_library` is a target and `lib/` contains `.capnp` schema
# files with imports from `include/`.
target_capnp_sources(my_library "${CMAKE_SOURCE_DIR}"
lib/schema1.capnp lib/schema2.capnp
IMPORT_PATHS ${CMAKE_SOURCE_DIR}/include)
#]=]
function(target_capnp_sources target include_prefix)
cmake_parse_arguments(PARSE_ARGV 2
"TCS" # prefix
"ONLY_CAPNP" # options
"" # one_value_keywords
"IMPORT_PATHS" # multi_value_keywords
)
set(MPGEN_BINARY "")
if(MPGEN_EXECUTABLE)
set(MPGEN_BINARY "${MPGEN_EXECUTABLE}")
if(NOT EXISTS "${MPGEN_BINARY}")
message(FATAL_ERROR "MPGEN_EXECUTABLE: \"${MPGEN_BINARY}\" does not exist.")
endif()
elseif(TARGET Libmultiprocess::multiprocess)
set(MPGEN_BINARY Libmultiprocess::mpgen)
else()
message(FATAL_ERROR "No usable mpgen. Set MPGEN_EXECUTABLE or enable the internal target.")
endif()
get_property(mp_include_dir GLOBAL PROPERTY MP_INCLUDE_DIR)
set(generated_headers "")
foreach(capnp_file IN LISTS TCS_UNPARSED_ARGUMENTS)
add_custom_command(
OUTPUT ${capnp_file}.c++ ${capnp_file}.h ${capnp_file}.proxy-client.c++ ${capnp_file}.proxy-types.h ${capnp_file}.proxy-server.c++ ${capnp_file}.proxy-types.c++ ${capnp_file}.proxy.h
COMMAND ${MPGEN_BINARY} ${CMAKE_CURRENT_SOURCE_DIR} ${include_prefix} ${CMAKE_CURRENT_SOURCE_DIR}/${capnp_file} ${TCS_IMPORT_PATHS} ${mp_include_dir}
DEPENDS ${capnp_file}
VERBATIM
)
# Skip linting for capnp-generated files but keep it for mpgen-generated ones
set_source_files_properties(${capnp_file}.c++ PROPERTIES SKIP_LINTING TRUE) # Ignored before cmake 3.27
target_sources(${target} PRIVATE
${CMAKE_CURRENT_BINARY_DIR}/${capnp_file}.c++
)
if(NOT TCS_ONLY_CAPNP)
target_sources(${target} PRIVATE
${CMAKE_CURRENT_BINARY_DIR}/${capnp_file}.proxy-client.c++
${CMAKE_CURRENT_BINARY_DIR}/${capnp_file}.proxy-server.c++
${CMAKE_CURRENT_BINARY_DIR}/${capnp_file}.proxy-types.c++
)
endif()
list(APPEND generated_headers ${capnp_file}.h)
endforeach()
# Translate include_prefix from a source path to a binary path and add it as a
# target include directory.
set(build_include_prefix ${CMAKE_BINARY_DIR})
file(RELATIVE_PATH relative_path ${CMAKE_SOURCE_DIR} ${include_prefix})
if(relative_path)
string(APPEND build_include_prefix "/" "${relative_path}")
endif()
target_include_directories(${target} PUBLIC $<BUILD_INTERFACE:${build_include_prefix}> ${mp_include_dir})
if(TARGET Libmultiprocess::multiprocess)
target_link_libraries(${target} PRIVATE Libmultiprocess::multiprocess)
endif()
# Add a custom target that can be specified as a dependency of c++ targets
# that include generated headers. It can be necessary to specify these
# dependencies explicitly because while cmake detect dependencies of non
# generated files on generated headers, it does not reliably detect
# dependencies of generated headers on other generated headers.
if(NOT TARGET "${target}_headers")
add_custom_target("${target}_headers" DEPENDS ${generated_headers})
endif()
endfunction()