Inconsistent behavior of find_package in config mode when COMPONENTS is specified without REQUIRED
From the docs https://cmake.org/cmake/help/latest/command/find_package.html
The REQUIRED option stops processing with an error message if the package cannot be found.
A package-specific list of required components may be listed after the COMPONENTS option (or after the REQUIRED option if present).
Available components and their influence on whether a package is considered to be found are defined by the target package.
Assuming that the target package considers the package not to be found if a component is missing, this is what i expect from reading the above
find_package(Foo) -> if not found, warns
find_package(Foo REQUIRED) -> if not found, errors
find_package(Foo COMPONENTS A) -> if component not found, errors
find_package(Foo REQUIRED COMPONENTS A) -> if component not found, errors
find_package(Foo OPTIONAL_COMPONENTS A) -> if component not found, warns
This is the behavior I observe with Qt6 package though
find_package(Qt6) -> if not found, warns
find_package(Qt6 REQUIRED) -> if not found, errore
find_package(Qt6 COMPONENTS A) -> if component not found, WARNS
find_package(Qt6 REQUIRED COMPONENTS A) -> if component not found, errors
find_package(Qt6 OPTIONAL_COMPONENTS A) -> if component not found, warns
Note that the Qt6Config.cmake
sets Qt6_FOUND
to false if the component is not found, and also sets Qt6_NOT_FOUND_MESSAGE
.
If I dump the value of Qt6_FIND_REQUIRED_A
, it says 1
, so CMake considers the component to be required as the documentation says it would be, even if REQUIRED
was not specified to the find_package
call.
And yet despite that, CMake only warns rather than errors out.
Is this a documentation bug? A CMake bug?
Is the package configuration file supposed to message(FATAL_ERROR)
in such a case?
Sample project:
find_package_components_warning.zip
$ cmake ..
-- The CXX compiler identification is AppleClang 12.0.0.12000032
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
>> find_package(Umbrella COMPONENTS Core) Umbrella_FOUND 1
-- Could NOT find NonExistent (missing: NonExistent_DIR)
CMake Warning at CMakeLists.txt:9 (find_package):
Found package configuration file:
/Volumes/T3/Dev/projects/cmake/general/find_package_components_warning/pkgs/Umbrella/UmbrellaConfig.cmake
but it set Umbrella_FOUND to FALSE so package "Umbrella" is considered to
be NOT FOUND. Reason given by package:
Failed to find Umbrella component "NonExistent"
>> find_package(Umbrella COMPONENTS NonExistent) Umbrella_FOUND 0
-- Configuring done
-- Generating done