Skip to content

cmake: Include headers from every target_capnp_sources call in TARGET_headers - #372

Open
ryanofsky wants to merge 1 commit into
bitcoin-core:masterfrom
ryanofsky:pr/capnphdrs
Open

ryanofsky wants to merge 1 commit into
bitcoin-core:masterfrom
ryanofsky:pr/capnphdrs

Conversation

@ryanofsky

Copy link
Copy Markdown
Collaborator

target_capnp_sources creates a TARGET_headers custom 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 building TARGET_headers does not generate them.

Bitcoin Core runs into this in bitcoin/bitcoin#10102, which adds wallet.capnp to bitcoin_ipc with a second target_capnp_sources call (it is only built with the wallet enabled). The CI iwyu job only builds the *_headers targets before running IWYU, so wallet.capnp.h and wallet.capnp.proxy.h were 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_headers depend on it, since dependencies cannot be added to an existing custom target.

…_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).
@DrahtBot

DrahtBot commented Oct 2, 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 hebasto

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

@hebasto hebasto left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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})

Comment on lines +130 to +131
add_custom_target("${target}_headers_${headers_hash}" DEPENDS ${generated_headers})
add_dependencies("${target}_headers" "${target}_headers_${headers_hash}")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

style nit: Quoting here seems unnecessary.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants