Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1# Copyright (c) 2024-present The Bitcoin Core developers2# Distributed under the MIT software license, see the accompanying3# file COPYING or https://opensource.org/license/mit/.5#[=[7target_capnp_sources8--------------------10This function adds build steps to generate C++ files from Cap'n Proto files11and build them as part of a specified target.13Arguments:15target: The name of the CMake target (e.g., a library or executable) to16which the generated source files will be added. This target must already17be defined elsewhere in the CMake scripts.19include_prefix: Absolute path indicating what portion of capnp source paths20should be used in relative #include statements in the generated C++21files. For example, if the .capnp path is /home/src/lib/schema.capnp22and include_prefix is /home/src, generated includes look like:24#include <lib/schema.capnp.h>26And if include_prefix is /home/src/lib, generated includes look like:28#include <schema.capnp.h>30Pass ${CMAKE_CURRENT_SOURCE_DIR} or a subdirectory of it to include files31relative to the current source directory (the typical usage).33Additional Unnamed Arguments:35After `target` and `include_prefix`, all unnamed arguments are treated as36paths to `.capnp` schema files. These should be paths relative to37${CMAKE_CURRENT_SOURCE_DIR}.39Optional Keyword Arguments:41IMPORT_PATHS: Specifies additional directories to search for imported42`.capnp` files.44ONLY_CAPNP: If specified, only the Cap'n Proto serialization files45(`.capnp.h`, `.capnp.c++`) are generated and compiled. The mpgen proxy46files (`.capnp.proxy-client.c++`, `.capnp.proxy-server.c++`,47`.capnp.proxy-types.c++`, etc.) are skipped. Useful when you need48Cap'n Proto serialization without the multiprocess RPC proxy49infrastructure.51Example:52# Assuming `my_library` is a target and `lib/` contains `.capnp` schema53# files with imports from `include/`.54target_capnp_sources(my_library "${CMAKE_CURRENT_SOURCE_DIR}"55lib/schema1.capnp lib/schema2.capnp56IMPORT_PATHS ${CMAKE_CURRENT_SOURCE_DIR}/include)58#]=]60function(target_capnp_sources target include_prefix)61cmake_parse_arguments(PARSE_ARGV 262"TCS" # prefix63"ONLY_CAPNP" # options64"" # one_value_keywords65"IMPORT_PATHS" # multi_value_keywords66)68set(MPGEN_BINARY "")69if(MPGEN_EXECUTABLE)70set(MPGEN_BINARY "${MPGEN_EXECUTABLE}")71if(NOT EXISTS "${MPGEN_BINARY}")72message(FATAL_ERROR "MPGEN_EXECUTABLE: \"${MPGEN_BINARY}\" does not exist.")73endif()74elseif(TARGET Libmultiprocess::multiprocess)75set(MPGEN_BINARY Libmultiprocess::mpgen)76else()77message(FATAL_ERROR "No usable mpgen. Set MPGEN_EXECUTABLE or enable the internal target.")78endif()80get_property(mp_include_dir GLOBAL PROPERTY MP_INCLUDE_DIR)81set(generated_headers "")82foreach(capnp_file IN LISTS TCS_UNPARSED_ARGUMENTS)83add_custom_command(84OUTPUT ${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.h85COMMAND ${MPGEN_BINARY} ${CMAKE_CURRENT_SOURCE_DIR} ${include_prefix} ${CMAKE_CURRENT_SOURCE_DIR}/${capnp_file} ${TCS_IMPORT_PATHS} ${mp_include_dir}86DEPENDS ${capnp_file}87VERBATIM88)89# Skip linting for capnp-generated files but keep it for mpgen-generated ones90set_source_files_properties(${capnp_file}.c++ PROPERTIES SKIP_LINTING TRUE) # Ignored before cmake 3.2791target_sources(${target} PRIVATE92${CMAKE_CURRENT_BINARY_DIR}/${capnp_file}.c++93)94if(NOT TCS_ONLY_CAPNP)95target_sources(${target} PRIVATE96${CMAKE_CURRENT_BINARY_DIR}/${capnp_file}.proxy-client.c++97${CMAKE_CURRENT_BINARY_DIR}/${capnp_file}.proxy-server.c++98${CMAKE_CURRENT_BINARY_DIR}/${capnp_file}.proxy-types.c++99)100endif()101list(APPEND generated_headers ${capnp_file}.h)102endforeach()104# Translate include_prefix from a source path to a binary path and add it as a105# target include directory.106set(build_include_prefix ${CMAKE_CURRENT_BINARY_DIR})107file(RELATIVE_PATH relative_path ${CMAKE_CURRENT_SOURCE_DIR} ${include_prefix})108if(relative_path)109string(APPEND build_include_prefix "/" "${relative_path}")110endif()111target_include_directories(${target} PUBLIC $<BUILD_INTERFACE:${build_include_prefix}> ${mp_include_dir})113if(TARGET Libmultiprocess::multiprocess)114target_link_libraries(${target} PRIVATE Libmultiprocess::multiprocess)115endif()117# Add a custom target that can be specified as a dependency of c++ targets118# that include generated headers. It can be necessary to specify these119# dependencies explicitly because while cmake detect dependencies of non120# generated files on generated headers, it does not reliably detect121# dependencies of generated headers on other generated headers.122if(NOT TARGET "${target}_headers")123add_custom_target("${target}_headers" DEPENDS ${generated_headers})124endif()125endfunction()
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.