In commit "cmake: Add LibmultiprocessMacros module" (028d697b110166b2e1b0114594af4377a9583c93)
I think source_include_prefix variable is redundant here and will always be identical to include_prefix. The code is just removing ${CMAKE_SOURCE_DIR} from the beginning of include_prefix and then adding it back again, so it has no effect. I also found this function generally hard to wrap my head around so would suggest adding documentation:
--- a/cmake/LibmultiprocessMacros.cmake
+++ b/cmake/LibmultiprocessMacros.cmake
@@ -2,6 +2,51 @@
# 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
@@ -14,18 +59,10 @@ function(target_capnp_sources target include_prefix)
message(FATAL_ERROR "Target 'Libmultiprocess::mpgen' does not exist.")
endif()
- set(source_include_prefix ${CMAKE_SOURCE_DIR})
- set(build_include_prefix ${CMAKE_BINARY_DIR})
- file(RELATIVE_PATH relative_path ${CMAKE_SOURCE_DIR} ${include_prefix})
- if(relative_path)
- string(APPEND source_include_prefix "/" "${relative_path}")
- string(APPEND build_include_prefix "/" "${relative_path}")
- endif()
-
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 Libmultiprocess::mpgen ${CMAKE_CURRENT_SOURCE_DIR} ${source_include_prefix} ${CMAKE_CURRENT_SOURCE_DIR}/${capnp_file} ${TCS_IMPORT_PATHS}
+ COMMAND Libmultiprocess::mpgen ${CMAKE_CURRENT_SOURCE_DIR} ${include_prefix} ${CMAKE_CURRENT_SOURCE_DIR}/${capnp_file} ${TCS_IMPORT_PATHS}
DEPENDS ${capnp_file}
VERBATIM
)
@@ -36,7 +73,16 @@ function(target_capnp_sources target include_prefix)
${CMAKE_CURRENT_BINARY_DIR}/${capnp_file}.proxy-types.c++
)
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}>)
+
if(TARGET Libmultiprocess::multiprocess)
target_link_libraries(${target} PRIVATE Libmultiprocess::multiprocess)
endif()