|
| 1 | +# Copyright (c) 2024-present The Bitcoin Core developers |
| 2 | +# Distributed under the MIT software license, see the accompanying |
| 3 | +# file COPYING or https://opensource.org/license/mit/. |
| 4 | + |
| 5 | +#[=[ |
| 6 | +
|
| 7 | +target_capnp_sources |
| 8 | +-------------------- |
| 9 | +
|
| 10 | +This function adds build steps to generate C++ files from Cap'n Proto files |
| 11 | +and build them as part of a specified target. |
| 12 | +
|
| 13 | +Arguments: |
| 14 | +
|
| 15 | + target: The name of the CMake target (e.g., a library or executable) to |
| 16 | + which the generated source files will be added. This target must already |
| 17 | + be defined elsewhere in the CMake scripts. |
| 18 | +
|
| 19 | + include_prefix: Absolute path indicating what portion of capnp source paths |
| 20 | + should be used in relative #include statements in the generated C++ |
| 21 | + files. For example, if the .capnp path is /home/src/lib/schema.capnp |
| 22 | + and include_prefix is /home/src, generated includes look like: |
| 23 | +
|
| 24 | + #include <lib/schema.capnp.h> |
| 25 | +
|
| 26 | + And if include_prefix is /home/src/lib, generated includes look like: |
| 27 | +
|
| 28 | + #include <schema.capnp.h> |
| 29 | +
|
| 30 | + The specified include_prefix should be ${CMAKE_SOURCE_DIR} or a |
| 31 | + subdirectory of it to include files relative to the project root. It can |
| 32 | + be ${CMAKE_CURRENT_SOURCE_DIR} to include files relative to the current |
| 33 | + source directory. |
| 34 | +
|
| 35 | +Additional Unnamed Arguments: |
| 36 | +
|
| 37 | + After `target` and `include_prefix`, all unnamed arguments are treated as |
| 38 | + paths to `.capnp` schema files. These should be paths relative to |
| 39 | + ${CMAKE_CURRENT_SOURCE_DIR}. |
| 40 | +
|
| 41 | +Optional Keyword Arguments: |
| 42 | +
|
| 43 | + IMPORT_PATHS: Specifies additional directories to search for imported |
| 44 | + `.capnp` files. |
| 45 | +
|
| 46 | +Example: |
| 47 | + # Assuming `my_library` is a target and `lib/` contains `.capnp` schema |
| 48 | + # files with imports from `include/`. |
| 49 | + target_capnp_sources(my_library "${CMAKE_SOURCE_DIR}" |
| 50 | + lib/schema1.capnp lib/schema2.capnp |
| 51 | + IMPORT_PATHS ${CMAKE_SOURCE_DIR}/include) |
| 52 | +
|
| 53 | +#]=] |
| 54 | + |
| 55 | +function(target_capnp_sources target include_prefix) |
| 56 | + cmake_parse_arguments(PARSE_ARGV 2 |
| 57 | + "TCS" # prefix |
| 58 | + "" # options |
| 59 | + "" # one_value_keywords |
| 60 | + "IMPORT_PATHS" # multi_value_keywords |
| 61 | + ) |
| 62 | + |
| 63 | + if(NOT TARGET Libmultiprocess::mpgen) |
| 64 | + message(FATAL_ERROR "Target 'Libmultiprocess::mpgen' does not exist.") |
| 65 | + endif() |
| 66 | + |
| 67 | + foreach(capnp_file IN LISTS TCS_UNPARSED_ARGUMENTS) |
| 68 | + add_custom_command( |
| 69 | + 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 |
| 70 | + COMMAND Libmultiprocess::mpgen ${CMAKE_CURRENT_SOURCE_DIR} ${include_prefix} ${CMAKE_CURRENT_SOURCE_DIR}/${capnp_file} ${TCS_IMPORT_PATHS} |
| 71 | + DEPENDS ${capnp_file} |
| 72 | + VERBATIM |
| 73 | + ) |
| 74 | + target_sources(${target} PRIVATE |
| 75 | + ${CMAKE_CURRENT_BINARY_DIR}/${capnp_file}.c++ |
| 76 | + ${CMAKE_CURRENT_BINARY_DIR}/${capnp_file}.proxy-client.c++ |
| 77 | + ${CMAKE_CURRENT_BINARY_DIR}/${capnp_file}.proxy-server.c++ |
| 78 | + ${CMAKE_CURRENT_BINARY_DIR}/${capnp_file}.proxy-types.c++ |
| 79 | + ) |
| 80 | + endforeach() |
| 81 | + |
| 82 | + # Translate include_prefix from a source path to a binary path and add it as a |
| 83 | + # target include directory. |
| 84 | + set(build_include_prefix ${CMAKE_BINARY_DIR}) |
| 85 | + file(RELATIVE_PATH relative_path ${CMAKE_SOURCE_DIR} ${include_prefix}) |
| 86 | + if(relative_path) |
| 87 | + string(APPEND build_include_prefix "/" "${relative_path}") |
| 88 | + endif() |
| 89 | + target_include_directories(${target} PUBLIC $<BUILD_INTERFACE:${build_include_prefix}>) |
| 90 | + |
| 91 | + if(TARGET Libmultiprocess::multiprocess) |
| 92 | + target_link_libraries(${target} PRIVATE Libmultiprocess::multiprocess) |
| 93 | + endif() |
| 94 | +endfunction() |
0 commit comments