Repository navigation
cmake: Include headers from every target_capnp_sources call in TARGET_headers - #372
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. ReviewsSee the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste |
hebasto
left a comment
There was a problem hiding this comment.
Concept ACK.
For bitcoin/bitcoin#10102, which requires CMake >=3.22, the following single line would work:
target_sources(${target}_headers PRIVATE ${generated_headers})For CMake <3.20, the following should work:
if(NOT TARGET ${target}_headers)
add_custom_target(${target}_headers)
endif()
set_property(TARGET ${target}_headers APPEND PROPERTY SOURCES ${generated_headers})| add_custom_target("${target}_headers_${headers_hash}" DEPENDS ${generated_headers}) | ||
| add_dependencies("${target}_headers" "${target}_headers_${headers_hash}") |
There was a problem hiding this comment.
style nit: Quoting here seems unnecessary.
There was a problem hiding this comment.
re: #372 (comment)
In commit "cmake: Include headers from every target_capnp_sources call in TARGET_headers" (b94506a)
Quoting here seems unnecessary.
Thanks, dropped the quotes around the target names. Kept them for headers_hash, because input contains a semicolon.
…_headers target_capnp_sources creates a TARGET_headers custom target that other targets can depend on to make sure generated headers exist, but it only did this on the first call for a target. Headers from later calls for the same target, for example optional schemas or schemas added with a different include prefix, were left out, so building TARGET_headers did not generate them. Bitcoin Core hit this when its multiprocess PR added wallet.capnp to bitcoin_ipc with a second call: the CI iwyu job only builds the *_headers targets, so wallet.capnp.h and wallet.capnp.proxy.h were never generated and files including them failed to compile. On later calls, add a helper target for that call's headers and make TARGET_headers depend on it, since dependencies cannot be added to an existing custom target. This change was written with Claude Opus 5.5 (1M context).
cdd4fdc to
b94506a
Compare
ryanofsky
left a comment
There was a problem hiding this comment.
Thanks for the review!
Rebased cdd4fdc -> b94506a (pr/capnphdrs.1 -> pr/capnphdrs.2, compare) to drop the unnecessary quotes hebasto pointed out (#372 (comment)), reword the comment explaining the helper target so it doesn't seem to contradict the add_dependencies call, and rebase onto master
| add_custom_target("${target}_headers_${headers_hash}" DEPENDS ${generated_headers}) | ||
| add_dependencies("${target}_headers" "${target}_headers_${headers_hash}") |
There was a problem hiding this comment.
re: #372 (comment)
In commit "cmake: Include headers from every target_capnp_sources call in TARGET_headers" (b94506a)
Quoting here seems unnecessary.
Thanks, dropped the quotes around the target names. Kept them for headers_hash, because input contains a semicolon.
| string(MD5 headers_hash "${CMAKE_CURRENT_SOURCE_DIR};${generated_headers}") | ||
| add_custom_target("${target}_headers_${headers_hash}" DEPENDS ${generated_headers}) | ||
| add_dependencies("${target}_headers" "${target}_headers_${headers_hash}") |
There was a problem hiding this comment.
re: #372 (review)
In commit "cmake: Include headers from every target_capnp_sources call in TARGET_headers" (b94506a)
For bitcoin/bitcoin#10102, which requires CMake >=3.22, the following single line would work
Thanks for the suggestions!
(For reference, because the list of files a custom target like ${target}_headers depends on can't be changed after the target is created, this PR adds a helper target that depends on the new header files, and uses add_dependencies to make ${target}_headers depend on that target, since dependencies between targets unlike files can be added at any time.
These alternative suggestions instead add the generated headers to the existing ${target}_headers target as sources, which can be extended later. A custom target that lists a generated file as a source gets the build rule for it, so building ${target}_headers generates those headers too.)
The benefit of the current approach over the suggested approaches is that it keeps ${target}_headers dependencies accurate when target_capnp_sources is called on a target from a different directory than the one that created it. With the suggested approaches, with relative header paths the function uses, configuring fails with "Cannot find source file", and with absolute paths it configures but silently doesn't generate those headers. Both suggestions work when every call for a target is in the same directory, which is the case in Bitcoin Core, so this is about being more future-proof.
An example of the different-directory case would be a src/wallet/CMakeLists.txt adding wallet.capnp to a bitcoin_ipc target created in src/ipc. Calls like this do need policy CMP0118 (CMake 3.20) or CMP0163 (CMake 3.30), which make generated files visible across directories. (With older policies they fail regardless of how headers are handled, because the target can't find the generated .c++ files.) They also need CMP0079 (CMake 3.13), which allows the function's target_link_libraries call on a target from another directory. Bitcoin Core's subtree build doesn't currently have CMP0079 enabled for this function even though Bitcoin Core requires CMake 3.22, because the function is defined in libmultiprocess's example/ directory, and CMake functions use the policies in effect where they are defined, here libmultiprocess's 3.12 policies. #209 would fix that by using the parent project's policy version, and adding cmake_policy(SET CMP0079 NEW) to the top of TargetCapnpSources.cmake would also fix it. The suggestions fail because they resolve relative header paths against the directory that created ${target}_headers. The helper target is created in the calling directory instead, which is why its name hashes CMAKE_CURRENT_SOURCE_DIR.
So I'd like to keep the helper target, unless we think it is ok for calls from other directories not to work.
Edit 2026-11-30: added CMP0079 and the paragraph about Bitcoin Core's subtree build, which this comment originally left out (#372 (comment)).
There was a problem hiding this comment.
re: #372 (review)
In commit "cmake: Include headers from every target_capnp_sources call in TARGET_headers" (b94506a)
Could you demonstrate that the current branch, which is more complex than the suggested alternative, does indeed work in the "different-directory case" described above?
Thanks for testing! Here is a small project, run from an empty directory, where ipc/ creates a bitcoin_ipc target and wallet/ adds a second schema to it:
git clone https://github.com/bitcoin-core/libmultiprocess
git -C libmultiprocess fetch origin pull/372/head
git -C libmultiprocess checkout FETCH_HEAD
mkdir ipc wallet
cat > CMakeLists.txt <<'EOF'
cmake_minimum_required(VERSION 3.31)
project(outer CXX)
set(CMAKE_CXX_STANDARD 20)
add_subdirectory(libmultiprocess EXCLUDE_FROM_ALL)
include(libmultiprocess/cmake/TargetCapnpSources.cmake)
add_subdirectory(ipc)
add_subdirectory(wallet)
EOF
cat > ipc/CMakeLists.txt <<'EOF'
add_library(bitcoin_ipc STATIC ipc.cpp)
target_capnp_sources(bitcoin_ipc ${CMAKE_CURRENT_SOURCE_DIR} common.capnp)
EOF
echo 'int f() { return 0; }' > ipc/ipc.cpp
printf '@0xd3f5a2c94b7e1a61;\nstruct Common { value @0 :Int32; }\n' > ipc/common.capnp
echo 'target_capnp_sources(bitcoin_ipc ${CMAKE_CURRENT_SOURCE_DIR} wallet.capnp)' > wallet/CMakeLists.txt
printf '@0xd3f5a2c94b7e1a62;\nstruct Wallet { value @0 :Int32; }\n' > wallet/wallet.capnp
cmake -B build
cmake --build build --target bitcoin_ipc_headers
ls build/ipc build/walletWith this PR, wallet.capnp.h and wallet.capnp.proxy.h are generated in build/wallet/, and bitcoin_ipc itself builds too. For comparison (CMake 4.1.6):
- With the function from master (
git -C libmultiprocess checkout HEAD~1 -- cmake), configuring and building succeed, but thewallet.capnp*.hheaders are not generated. - With the
else()branch replaced bytarget_sources(${target}_headers PRIVATE ${generated_headers}), configuring fails withCannot find source file: .../wallet/wallet.capnp.h. Theset_property(... APPEND PROPERTY SOURCES ...)variant fails withCannot find source file: wallet.capnp.h.
This example includes TargetCapnpSources.cmake from the outer project, the way find_package(Libmultiprocess COMPONENTS Bin) does. The same call from src/wallet in Bitcoin Core's current subtree build would fail, with or without this PR, because of a policy my earlier comment left out (CMP0079). I edited my comment above to explain: #372 (comment)
hebasto
left a comment
There was a problem hiding this comment.
ACK b94506a, tested on Fedora 45 in various scenarios, including variants of the branch from bitcoin/bitcoin#10102.
An example of the different-directory case would be a
src/wallet/CMakeLists.txtaddingwallet.capnpto abitcoin_ipctarget created insrc/ipc. Calls like this do need policy CMP0118 (CMake 3.20) or CMP0163 (CMake 3.30), which make generated files visible across directories. (With older policies they fail regardless of how headers are handled, because the target can't find the generated.c++files.) The suggestions fail because they resolve relative header paths against the directory that created${target}_headers. The helper target is created in the calling directory instead, which is why its name hashesCMAKE_CURRENT_SOURCE_DIR.
Could you demonstrate that the current branch, which is more complex than the suggested alternative, does indeed work in the "different-directory case" described above?
|
Thanks for the review! re: #372 (review)
Replied in #372 (comment) thread above |
target_capnp_sourcescreates aTARGET_headerscustom target that other targets can depend on to make sure generated headers exist, but it only creates it on the first call for a target. Headers from later calls for the same target, for example optional schemas or schemas added with a different include prefix, are left out, so buildingTARGET_headersdoes not generate them.Bitcoin Core runs into this in bitcoin/bitcoin#10102, which adds
wallet.capnptobitcoin_ipcwith a secondtarget_capnp_sourcescall (it is only built with the wallet enabled). The CI iwyu job only builds the*_headerstargets before running IWYU, sowallet.capnp.handwallet.capnp.proxy.hwere never generated and files including them failed to compile, which fails the job since bitcoin/bitcoin#36235.This PR makes later calls add a helper target for their headers and makes
TARGET_headersdepend on it, since dependencies cannot be added to an existing custom target.