FetchContent, find_package, and FeatureSummary

Hello, are there any best practices or patterns how to properly use FetchContent, FeatureSummary and FindPackageName modules?

For example, if I want to integrate FeatureSummary with FetchContent there is additional code needed for each dependency:

get_cmake_property(packagesNotFound PACKAGES_NOT_FOUND)
list(REMOVE_ITEM packagesNotFound <PackageName>)
set_property(GLOBAL PROPERTY PACKAGES_NOT_FOUND packagesNotFound)
get_cmake_property(packagesFound PACKAGES_FOUND)
set_property(GLOBAL APPEND PROPERTY PACKAGES_FOUND <PackageName>)

Then another issue I’m having is all the additional possible things that find module might set, expose, etc. When the FetchContent_Declare calls find_package() itself, it doesn’t execute the code from the local or upstream FindPackageName.cmake module and code needs to be duplicated for that part also manually. Is there a better way? Or should the FindPackageName.cmake module be refactored into a regular module?

And the last issue I’m noticing at the moment is the logging. The FetchContent_Declare uses find_package() with QUIET option. Is there any practice how to customize the logging messages when package is found, like the default find_package() outputs, or how to disable the QUIET option when using FetchContent_Declare?

Thank you.

For example, if I want to integrate FeatureSummary with FetchContent there is additional code needed for each dependency

Integration between FetchContent and FeatureSummary has been raised in the issue tracker, but no-one is working on it that I’m aware of. https://gitlab.kitware.com/cmake/cmake/-/issues/25322

Then another issue I’m having is all the additional possible things that find module might set, expose, etc. When the FetchContent_Declare calls find_package() itself, it doesn’t execute the code from the local or upstream FindPackageName.cmake module and code needs to be duplicated for that part also manually. Is there a better way? Or should the FindPackageName.cmake module be refactored into a regular module?

This sounds to me like the thing you’re trying to bring in with FetchContent doesn’t meet the requirements of doing so. When a dependency is brought into the build with FetchContent, it is expected that the dependency will define the same targets, commands, etc. that it would if it had been brought in using find_package() from an installed package. This normally means the dependency project has to define ALIAS targets to match the namespaced names of the targets when installed.

Things get worse when you’re expecting details normally provided by a Find module rather than a package’s own <packageName>-config.cmake file. A Find module is maintained externally to the package it is providing. There’s nothing official about a Find module (except those provided by CMake itself). The package isn’t bound to honouring whatever the Find module author decided to make available. If you’re the author of both the package and the Find module, then get rid of the find module and provide a <packageName>-config.cmake instead, and include it with your package when you distribute it.

And the last issue I’m noticing at the moment is the logging. The FetchContent_Declare uses find_package() with QUIET option. Is there any practice how to customize the logging messages when package is found, like the default find_package() outputs, or how to disable the QUIET option when using FetchContent_Declare?

There’s no facility for disabling the QUIET behavior when FetchContent tries calling find_package() at the moment.

1 Like