diff --git a/cmake/TargetCapnpSources.cmake b/cmake/TargetCapnpSources.cmake index 2334e260..172986f4 100644 --- a/cmake/TargetCapnpSources.cmake +++ b/cmake/TargetCapnpSources.cmake @@ -121,5 +121,13 @@ function(target_capnp_sources target include_prefix) # dependencies of generated headers on other generated headers. if(NOT TARGET "${target}_headers") add_custom_target("${target}_headers" DEPENDS ${generated_headers}) + else() + # target_capnp_sources can be called more than once for the same target, + # for example to add optional schemas or schemas with a different include + # prefix. Dependencies can't be added to an existing custom target, so add + # these headers through a helper target that ${target}_headers depends on. + 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}") endif() endfunction()