Optional `find_package` cannot be handled through dependency providers
As explained in the documentation
For a dependency named depName, the provider must set depName_FOUND to true if it fulfilled the request. If the provider returns without setting this variable, CMake will assume the request was not fulfilled and will fall back to the built-in implementation.
This is problematic when dealing with optional dependencies, because there is no way to mark the dependency as not found and stop the search.
Let's say that I'm building a project that does
find_package(Foo)
if (Foo_FOUND)
# enable Foo features
and I don't want Foo
to be used by this project, there is no way to do that with a dependency provider. One might argue that it is an anti-pattern to enable features based on what is found while configuring, but a lot of projects work this way, and CMake allows it with optional find_package
, so it would be nice if we had a way to handle that properly with dependency providers.
Some alternatives that come to mind:
- have a global
CMAKE_*
variable to disable the fallback entirely. One can still usefind_package(BYPASS_PROVIDER)
to use the default behavior. - interpret
Foo_FOUND
as a tristate boolean: if truthey or falsey => stop the search, if unset => fallback.
What do you think?