Registry Mirror
Skip to content

Conversation

@raus7n

@raus7n raus7n commented Nov 15, 2026 •

Copy link
Copy Markdown

Fixes #397.

CMake 4.5 expects every generated file that a target installs to be the
output of a custom command attached to exactly one target. Otherwise it
emits the author warning reported in #397 for the Makefile generators.

capnp_generate_cpp() lists both proxy.capnp.c++ and proxy.capnp.h as
outputs of one command, which ended up attached to both mp_headers and
multiprocess. Adding add_dependencies(multiprocess mp_headers), as the
warning suggests, does not help: the command is still attached to two targets.

This change runs the same capnp compile command directly, with
proxy.capnp.c++ as a byproduct, so the command is attached only to
mp_headers. multiprocess no longer lists the generated header as a source
(it is still installed through PUBLIC_HEADER) and depends on mp_headers
so the generated source exists before it is compiled. mp_headers still only
generates headers and does not compile anything.

Tested with CMake 4.5.0: the warning is gone with the Makefile generator
(Ninja did not show it before either).

@DrahtBot

DrahtBot commented Nov 15, 2026 •

Copy link
Copy Markdown

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
Concept ACK ryanofsky

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

@ryanofsky ryanofsky left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review 0bb8703. Thanks for tracking this down! The fix works: with CMake 4.5.0 the warning from #397 is gone with the Makefile generator, and install_prep/lib still builds and installs proxy.capnp.h with both Make and Ninja. I also checked that the simpler fixes don't work (add_dependencies alone, as the description says, and installing the header with install(FILES)), so replacing capnp_generate_cpp() with our own custom command seems necessary.

I'd like the custom command moved into a function in cmake/ instead of inlined in the main CMakeLists.txt, and the same fix made in target_capnp_sources(). Details and a tested diff are in the inline comment. Happy to ACK after that.

Minor: the description says the warning is gone with Makefile and Ninja generators, but Ninja didn't show it on master either.

Comment thread CMakeLists.txt Outdated
Comment on lines +142 to +160
# Generated C++ Capn'Proto schema files. Same as capnp_generate_cpp(), but with
# the source file as a byproduct so that only the mp_headers target owns the
# command. CMake 4.5 "install_prep/" targets need installed generated headers
# to be the output of a single target.
set(MP_PROXY_SRCS "${CAPNPC_OUTPUT_DIR}/include/mp/proxy.capnp.c++")
set(MP_PROXY_HDRS "${CAPNPC_OUTPUT_DIR}/include/mp/proxy.capnp.h")
add_custom_command(
OUTPUT ${MP_PROXY_HDRS}
BYPRODUCTS ${MP_PROXY_SRCS}
COMMAND ${CAPNP_EXECUTABLE} compile
-o ${CAPNPC_CXX_EXECUTABLE}:${CAPNPC_OUTPUT_DIR}
--src-prefix ${CMAKE_CURRENT_SOURCE_DIR}
-I ${CMAKE_CURRENT_SOURCE_DIR}
-I ${CAPNP_INCLUDE_DIRECTORY}
${CMAKE_CURRENT_SOURCE_DIR}/include/mp/proxy.capnp
DEPENDS include/mp/proxy.capnp
COMMENT "Compiling Cap'n Proto schema include/mp/proxy.capnp"
VERBATIM
)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In commit "cmake: Fix CMake 4.5 install_prep warning for generated proxy.capnp.h" (0bb8703)

I think this custom command should move into a function in cmake/ instead of being inlined in the middle of the main CMakeLists.txt. The function can say clearly what it does differently from capnp_generate_cpp() and why, and it can be dropped if Cap'n Proto adds a way to do this (capnproto/capnproto#582 asks for a target-based capnp_generate_cpp()).

I'd also make the same fix in target_capnp_sources() in this PR. It attaches the mpgen command to both ${target} and ${target}_headers the same way. That doesn't warn today because nobody installs those headers, but fixing both shows that the problem isn't specific to proxy.capnp.h, and keeps the two consistent.

The diff below does both. It applies on top of this PR:

  • Adds cmake/capnp_generate_cpp.cmake with an mp_capnp_generate_cpp() function. It runs the same capnp command, with the .c++ file as a byproduct, adds it to multiprocess, creates mp_headers owning the command, and makes multiprocess depend on it. The comment explains the difference from capnp_generate_cpp() and lists the optional CAPNPC_* variables it doesn't support.
  • Changes target_capnp_sources() to list the generated .c++ files as byproducts, include all three generated headers in ${target}_headers, and make ${target} depend on it. Because add_custom_target dependencies can't be extended later, a second call for the same target adds a ${target}_headers_N target.

I tested it with CMake 4.5.0 and both Make and Ninja. There's no warning, install_prep/lib installs the header, mp_headers and mptest_headers run the generators without compiling anything else, and editing a schema regenerates and recompiles it. mptest passes, the examples build, and splitting the example's target_capnp_sources() into three calls for one target still works. In Bitcoin Core there are no CMake warnings, and the header targets used by the IWYU CI job build without compiling anything. A full Bitcoin Core build with -DENABLE_IPC=ON passes the unit tests and the interface_ipc* functional tests. I haven't tested CMake 3.12, which the olddeps CI job uses.

The names are just suggestions.

Diff
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 8522e6453f5..a8ace76690e 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -118,6 +118,7 @@ endif()

 include("cmake/compat_config.cmake")
 include("cmake/pthread_checks.cmake")
+include("cmake/capnp_generate_cpp.cmake")
 include(GNUInstallDirs)

 # Set MP_INCLUDE_DIR as a global property so target_capnp_sources function can
@@ -139,31 +140,6 @@ set_property(DIRECTORY PROPERTY INCLUDE_DIRECTORIES "")
 # Generated C++ preprocessor defines
 configure_file(include/mp/config.h.in "${CMAKE_CURRENT_BINARY_DIR}/include/mp/config.h")

-# Generated C++ Capn'Proto schema files. Same as capnp_generate_cpp(), but with
-# the source file as a byproduct so that only the mp_headers target owns the
-# command. CMake 4.5 "install_prep/" targets need installed generated headers
-# to be the output of a single target.
-set(MP_PROXY_SRCS "${CAPNPC_OUTPUT_DIR}/include/mp/proxy.capnp.c++")
-set(MP_PROXY_HDRS "${CAPNPC_OUTPUT_DIR}/include/mp/proxy.capnp.h")
-add_custom_command(
-  OUTPUT ${MP_PROXY_HDRS}
-  BYPRODUCTS ${MP_PROXY_SRCS}
-  COMMAND ${CAPNP_EXECUTABLE} compile
-    -o ${CAPNPC_CXX_EXECUTABLE}:${CAPNPC_OUTPUT_DIR}
-    --src-prefix ${CMAKE_CURRENT_SOURCE_DIR}
-    -I ${CMAKE_CURRENT_SOURCE_DIR}
-    -I ${CAPNP_INCLUDE_DIRECTORY}
-    ${CMAKE_CURRENT_SOURCE_DIR}/include/mp/proxy.capnp
-  DEPENDS include/mp/proxy.capnp
-  COMMENT "Compiling Cap'n Proto schema include/mp/proxy.capnp"
-  VERBATIM
-)
-set_source_files_properties("${MP_PROXY_SRCS}" PROPERTIES SKIP_LINTING TRUE) # Ignored before cmake 3.27
-# Build-graph node for generated headers. This lets targets that include
-# the headers order themselves after generation without depending on the
-# library target that also uses them.
-add_custom_target(mp_headers DEPENDS ${MP_PROXY_HDRS})
-
 # util library
 add_library(mputil OBJECT src/mp/util.cpp)
 target_include_directories(mputil PRIVATE
@@ -201,13 +177,14 @@ set(MP_PUBLIC_HEADERS
   include/mp/util.h
   include/mp/version.h)
 add_library(multiprocess STATIC
-  ${MP_PROXY_SRCS}
   ${MP_PUBLIC_HEADERS}
   src/mp/proxy.cpp
   $<TARGET_OBJECTS:mputil>)
 add_library(Libmultiprocess::multiprocess ALIAS multiprocess)
-# MP_PROXY_SRCS are byproducts of mp_headers.
-add_dependencies(multiprocess mp_headers)
+# Generated C++ Cap'n Proto schema files, and an mp_headers target that
+# generates the header only.
+mp_capnp_generate_cpp(TARGET multiprocess HEADERS_TARGET mp_headers
+  HEADERS MP_PROXY_HDRS SCHEMAS include/mp/proxy.capnp)
 target_include_directories(multiprocess PUBLIC
   $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>
   $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
diff --git a/cmake/TargetCapnpSources.cmake b/cmake/TargetCapnpSources.cmake
index 2334e260204..64a7d15185f 100644
--- a/cmake/TargetCapnpSources.cmake
+++ b/cmake/TargetCapnpSources.cmake
@@ -80,8 +80,15 @@ function(target_capnp_sources target include_prefix)
   get_property(mp_include_dir GLOBAL PROPERTY MP_INCLUDE_DIR)
   set(generated_headers "")
   foreach(capnp_file IN LISTS TCS_UNPARSED_ARGUMENTS)
+    # List generated .c++ files as BYPRODUCTS, not OUTPUTS, so the command is
+    # attached only to the ${target}_headers custom target below, not also to
+    # ${target}, which compiles the .c++ files. CMake attaches a custom command
+    # to every target that lists one of its OUTPUTS as a source, and CMake 4.5
+    # warns about a command attached to two targets if the generated headers
+    # are installed (https://github.com/bitcoin-core/libmultiprocess/issues/397).
     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
+      OUTPUT ${capnp_file}.h ${capnp_file}.proxy-types.h ${capnp_file}.proxy.h
+      BYPRODUCTS ${capnp_file}.c++ ${capnp_file}.proxy-client.c++ ${capnp_file}.proxy-server.c++ ${capnp_file}.proxy-types.c++
       COMMAND ${MPGEN_BINARY} ${CMAKE_CURRENT_SOURCE_DIR} ${include_prefix} ${CMAKE_CURRENT_SOURCE_DIR}/${capnp_file} ${TCS_IMPORT_PATHS} ${mp_include_dir}
       DEPENDS ${capnp_file} ${MPGEN_BINARY}
       VERBATIM
@@ -98,7 +105,7 @@ function(target_capnp_sources target include_prefix)
         ${CMAKE_CURRENT_BINARY_DIR}/${capnp_file}.proxy-types.c++
       )
     endif()
-    list(APPEND generated_headers ${capnp_file}.h)
+    list(APPEND generated_headers ${capnp_file}.h ${capnp_file}.proxy-types.h ${capnp_file}.proxy.h)
   endforeach()

   # Translate include_prefix from a source path to a binary path and add it as a
@@ -114,12 +121,27 @@ function(target_capnp_sources target include_prefix)
     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.
+  # Add a custom target that runs the code generator. ${target} depends on it
+  # so the generated .c++ files exist before they are compiled, and it can also
+  # 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 this function is called more than once for the same
+  # target, each call adds another custom target that ${target}_headers
+  # depends on, since add_custom_target DEPENDS can't be extended later.
   if(NOT TARGET "${target}_headers")
     add_custom_target("${target}_headers" DEPENDS ${generated_headers})
+    add_dependencies(${target} "${target}_headers")
+  else()
+    get_property(call_count TARGET "${target}_headers" PROPERTY MP_CAPNP_CALL_COUNT)
+    if(NOT call_count)
+      set(call_count 1)
+    endif()
+    math(EXPR call_count "${call_count} + 1")
+    set_property(TARGET "${target}_headers" PROPERTY MP_CAPNP_CALL_COUNT "${call_count}")
+    add_custom_target("${target}_headers_${call_count}" DEPENDS ${generated_headers})
+    add_dependencies("${target}_headers" "${target}_headers_${call_count}")
+    add_dependencies(${target} "${target}_headers_${call_count}")
   endif()
 endfunction()
diff --git a/cmake/capnp_generate_cpp.cmake b/cmake/capnp_generate_cpp.cmake
new file mode 100644
index 00000000000..78c9b795592
--- /dev/null
+++ b/cmake/capnp_generate_cpp.cmake
@@ -0,0 +1,58 @@
+# Copyright (c) The Bitcoin Core developers
+# Distributed under the MIT software license, see the accompanying
+# file COPYING or http://www.opensource.org/licenses/mit-license.php.
+
+# mp_capnp_generate_cpp(TARGET target HEADERS_TARGET headers_target
+#                       HEADERS headers_var SCHEMAS schema...)
+#
+# Generate C++ code from Cap'n Proto schemas, compile it into TARGET, and add a
+# HEADERS_TARGET custom target that generates the headers without compiling
+# anything, so targets that only need the headers (like lint tools) can depend
+# on it. SCHEMAS paths are relative to the current source directory, and
+# generated header paths are returned in HEADERS.
+#
+# This runs the same capnp command as capnp_generate_cpp() from Cap'n Proto's
+# CapnProtoMacros.cmake, with one difference: generated .c++ files are listed
+# as BYPRODUCTS instead of OUTPUTS. CMake attaches a custom command to every
+# target that lists one of its OUTPUTS as a source, so with
+# capnp_generate_cpp() the command would belong to both TARGET and
+# HEADERS_TARGET. CMake 4.5 warns about that when the headers are installed,
+# because its "install_prep/" targets need each installed generated file to be
+# the output of a single target
+# (https://github.com/bitcoin-core/libmultiprocess/issues/397). Here the
+# command belongs only to HEADERS_TARGET, and TARGET depends on it.
+#
+# Unlike capnp_generate_cpp(), this ignores the optional CAPNPC_SRC_PREFIX,
+# CAPNPC_IMPORT_DIRS and CAPNPC_FLAGS variables, which libmultiprocess does not
+# use, and requires CAPNP_EXECUTABLE and CAPNPC_CXX_EXECUTABLE, which
+# CapnProtoConfig.cmake sets. It can be replaced with capnp_generate_cpp() if
+# Cap'n Proto ever supports this case
+# (https://github.com/capnproto/capnproto/issues/582).
+function(mp_capnp_generate_cpp)
+  cmake_parse_arguments(PARSE_ARGV 0 "MCGC" "" "TARGET;HEADERS_TARGET;HEADERS" "SCHEMAS")
+  set(sources "")
+  set(headers "")
+  foreach(schema IN LISTS MCGC_SCHEMAS)
+    set(output_base "${CAPNPC_OUTPUT_DIR}/${schema}")
+    add_custom_command(
+      OUTPUT "${output_base}.h"
+      BYPRODUCTS "${output_base}.c++"
+      COMMAND "${CAPNP_EXECUTABLE}" compile
+        -o "${CAPNPC_CXX_EXECUTABLE}:${CAPNPC_OUTPUT_DIR}"
+        --src-prefix "${CMAKE_CURRENT_SOURCE_DIR}"
+        -I "${CMAKE_CURRENT_SOURCE_DIR}"
+        -I "${CAPNP_INCLUDE_DIRECTORY}"
+        "${CMAKE_CURRENT_SOURCE_DIR}/${schema}"
+      DEPENDS "${schema}"
+      COMMENT "Compiling Cap'n Proto schema ${schema}"
+      VERBATIM
+    )
+    list(APPEND sources "${output_base}.c++")
+    list(APPEND headers "${output_base}.h")
+  endforeach()
+  set_source_files_properties(${sources} PROPERTIES SKIP_LINTING TRUE) # Ignored before cmake 3.27
+  add_custom_target(${MCGC_HEADERS_TARGET} DEPENDS ${headers})
+  target_sources(${MCGC_TARGET} PRIVATE ${sources})
+  add_dependencies(${MCGC_TARGET} ${MCGC_HEADERS_TARGET})
+  set(${MCGC_HEADERS} ${headers} PARENT_SCOPE)
+endfunction()

@raus7n
raus7n force-pushed the cmake-install-prep-warning branch from 0bb8703 to 29fd2a2 Compare December 2, 2026 14:32
@DrahtBot

DrahtBot commented Dec 2, 2026

Copy link
Copy Markdown

🐙 This pull request conflicts with the target branch and needs rebase.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CMake 4.5 warning about "install_prep/" targets

3 participants