TargetCapnpSources.cmake raw

   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      Pass ${CMAKE_CURRENT_SOURCE_DIR} or a subdirectory of it to include files
  31      relative to the current source directory (the typical usage).
  32  
  33  Additional Unnamed Arguments:
  34  
  35    After `target` and `include_prefix`, all unnamed arguments are treated as
  36    paths to `.capnp` schema files. These should be paths relative to
  37    ${CMAKE_CURRENT_SOURCE_DIR}.
  38  
  39  Optional Keyword Arguments:
  40  
  41    IMPORT_PATHS: Specifies additional directories to search for imported
  42      `.capnp` files.
  43  
  44    ONLY_CAPNP: If specified, only the Cap'n Proto serialization files
  45      (`.capnp.h`, `.capnp.c++`) are generated and compiled. The mpgen proxy
  46      files (`.capnp.proxy-client.c++`, `.capnp.proxy-server.c++`,
  47      `.capnp.proxy-types.c++`, etc.) are skipped. Useful when you need
  48      Cap'n Proto serialization without the multiprocess RPC proxy
  49      infrastructure.
  50  
  51  Example:
  52    # Assuming `my_library` is a target and `lib/` contains `.capnp` schema
  53    # files with imports from `include/`.
  54    target_capnp_sources(my_library "${CMAKE_CURRENT_SOURCE_DIR}"
  55                         lib/schema1.capnp lib/schema2.capnp
  56                         IMPORT_PATHS ${CMAKE_CURRENT_SOURCE_DIR}/include)
  57  
  58  #]=]
  59  
  60  function(target_capnp_sources target include_prefix)
  61    cmake_parse_arguments(PARSE_ARGV 2
  62      "TCS"           # prefix
  63      "ONLY_CAPNP"    # options
  64      ""              # one_value_keywords
  65      "IMPORT_PATHS"  # multi_value_keywords
  66    )
  67  
  68    set(MPGEN_BINARY "")
  69    if(MPGEN_EXECUTABLE)
  70      set(MPGEN_BINARY "${MPGEN_EXECUTABLE}")
  71      if(NOT EXISTS "${MPGEN_BINARY}")
  72        message(FATAL_ERROR "MPGEN_EXECUTABLE: \"${MPGEN_BINARY}\" does not exist.")
  73      endif()
  74    elseif(TARGET Libmultiprocess::multiprocess)
  75      set(MPGEN_BINARY Libmultiprocess::mpgen)
  76    else()
  77      message(FATAL_ERROR "No usable mpgen. Set MPGEN_EXECUTABLE or enable the internal target.")
  78    endif()
  79  
  80    get_property(mp_include_dir GLOBAL PROPERTY MP_INCLUDE_DIR)
  81    set(generated_headers "")
  82    foreach(capnp_file IN LISTS TCS_UNPARSED_ARGUMENTS)
  83      add_custom_command(
  84        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
  85        COMMAND ${MPGEN_BINARY} ${CMAKE_CURRENT_SOURCE_DIR} ${include_prefix} ${CMAKE_CURRENT_SOURCE_DIR}/${capnp_file} ${TCS_IMPORT_PATHS} ${mp_include_dir}
  86        DEPENDS ${capnp_file}
  87        VERBATIM
  88      )
  89      # Skip linting for capnp-generated files but keep it for mpgen-generated ones
  90      set_source_files_properties(${capnp_file}.c++ PROPERTIES SKIP_LINTING TRUE) # Ignored before cmake 3.27
  91      target_sources(${target} PRIVATE
  92        ${CMAKE_CURRENT_BINARY_DIR}/${capnp_file}.c++
  93      )
  94      if(NOT TCS_ONLY_CAPNP)
  95        target_sources(${target} PRIVATE
  96          ${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        )
 100      endif()
 101      list(APPEND generated_headers ${capnp_file}.h)
 102    endforeach()
 103  
 104    # Translate include_prefix from a source path to a binary path and add it as a
 105    # target include directory.
 106    set(build_include_prefix ${CMAKE_CURRENT_BINARY_DIR})
 107    file(RELATIVE_PATH relative_path ${CMAKE_CURRENT_SOURCE_DIR} ${include_prefix})
 108    if(relative_path)
 109      string(APPEND build_include_prefix "/" "${relative_path}")
 110    endif()
 111    target_include_directories(${target} PUBLIC $<BUILD_INTERFACE:${build_include_prefix}> ${mp_include_dir})
 112  
 113    if(TARGET Libmultiprocess::multiprocess)
 114      target_link_libraries(${target} PRIVATE Libmultiprocess::multiprocess)
 115    endif()
 116  
 117    # Add a custom target that can be specified as a dependency of c++ targets
 118    # that include generated headers. It can be necessary to specify these
 119    # dependencies explicitly because while cmake detect dependencies of non
 120    # generated files on generated headers, it does not reliably detect
 121    # dependencies of generated headers on other generated headers.
 122    if(NOT TARGET "${target}_headers")
 123      add_custom_target("${target}_headers" DEPENDS ${generated_headers})
 124    endif()
 125  endfunction()
 126