I am using CMake 3.31 and the Visual Studio generator in this example, but I don’t think it should matter for the problem that I am facing.
My project has many targets T1, T2,… All targets have install rules
install(TARGETS ${tgt}
RUNTIME COMPONENT "Bin"
LIBRARY COMPONENT "Bin"
)
install(TARGETS ${tgt} RUNTIME_DEPENDENCIES
...
EXCLUDE_FROM_ALL
RUNTIME COMPONENT "BinWithDeps" EXCLUDE_FROM_ALL
LIBRARY COMPONENT "BinWithDeps" EXCLUDE_FROM_ALL
)
I need to be able to install subsets of targets with their runtime dependencies. For that I create custom targets CT1, CT2,… like
add_custom_target(${customTgt}
COMMAND ${CMAKE_COMMAND}
-DBUILD_TYPE="$<CONFIG>"
-DCOMPONENT="BinWithDeps"
-DCMAKE_INSTALL_PREFIX="${MyPrefix}"
-P ${installScript}
)
for which I explicitly specify top level dependencies like
add_dependencies(${customTgt} T2)
(T2 transitively depends on T1 in this example). For each custom target CTn I figure out the full dependency subset by recursively examining the LINK_LIBRARIES
property of the dependencies and generate a CTn_install.cmake
script that ends up looking like
if(EXISTS build/T1/cmake_install.cmake)
include(build/T1/cmake_install.cmake)
endif()
if(EXISTS build/T2/cmake_install.cmake)
include(build/T2/cmake_install.cmake)
endif()
...
Then
cmake --build build -DCMAKE_INSTALL_PREFIX=MyPrefix --config Release --target CTn
is expected to do what I need. However, it fails with a message like
file Could not resolve runtime dependencies:
T1.dll
during processing of T2’s cmake_install.cmake
. The error points to the line in the script with the file(GET_RUNTIME_DEPENDENCIES...)
command which lists the T1 artifact in the POST_EXCLUDE_FILES_STRICT
section.
The same error happens if I just run
cmake --install build --component BinWithDeps --config Release --prefix MyPrefix
so, the problem does not seem to result from the way the custom target is defined.
I understand why in-project targets are excluded from the runtime dependency checks, but what I don’t understand is why inability to resolve their artifacts’ location should be considered a failure if they are explicitly excluded by CMake itself.
More importantly, I am looking for suggestions how to achieve what I need, maybe using a different approach.