[CMake] find_package REQUIRED ignores OPTIONAL_COMPONENTS

Nicholas Braden nicholas11braden at gmail.com
Tue Mar 8 11:00:00 EST 2016


Jakob, I don't think there is any confusion about what REQUIRED means.
Whether or not REQUIRED is provided, the list of OPTIONAL_COMPONENTS
should not be required under any circumstances. The example error
message seems pretty clear to me that the expected behavior and actual
behavior are different. I went and looked at the source code of the
find module:

https://github.com/Kitware/CMake/blob/master/Modules/FindQt4.cmake

It seems that there is no check whatsoever for
Qt4_FIND_REQUIRED_<component>, so the find module just blindly assumes
that all components are required.

More info: https://cmake.org/cmake/help/latest/manual/cmake-developer.7.html#find-modules

To me this seems like a bug in the find module.



On Tue, Mar 8, 2016 at 9:34 AM, Jakob van Bethlehem
<jsvanbethlehem at gmail.com> wrote:
> Hej Alexander,
>
> Yes, you're missing a subtle detail. You assume that the 'REQUIRED' keyword
> reflects the fact that COMPONENTS are required. This is not the case. The
> REQUIRED keyword reflects that the entire package Qt4 is required, see
> https://cmake.org/cmake/help/v3.0/command/find_package.html where it says:
> 'The REQUIRED option stops processing with an error message if the package
> cannot be found'
>
> Confusing, I agree.
>
> Sincerely,
> Jakob
>
> On Fri, Mar 4, 2016 at 9:18 AM, Alexander Stein
> <alexander.stein+cmake at mailbox.org> wrote:
>>
>> Hi,
>>
>> I want to use some required Qt component while others are optional.
>> Apparently if you specify REQUIRED in find_package OPTIONAL_COMPONENTS is
>> ignored.
>> Here is a minimal CMakeLists.txt:
>> project(test)
>> cmake_minimum_required(VERSION 3.5)
>>
>> find_package(Qt4 REQUIRED COMPONENTS QtCore QtGui OPTIONAL_COMPONENTS
>> Invalid)
>>
>> /home/alex/repo/cmake/build/bin/cmake --version
>> cmake version 3.5.20160303-gf37f
>>
>> cmake fails with:
>> CMake Error at
>> /home/alex/repo/cmake/Modules/FindPackageHandleStandardArgs.cmake:148
>> (message):
>>   Could NOT find Qt4 (missing: QT_INVALID_INCLUDE_DIR QT_INVALID_LIBRARY)
>>   (found version "4.8.7")
>> Call Stack (most recent call first):
>>   /home/alex/repo/cmake/Modules/FindPackageHandleStandardArgs.cmake:388
>> (_FPHSA_FAILURE_MESSAGE)
>>   /home/alex/repo/cmake/Modules/FindQt4.cmake:1333
>> (FIND_PACKAGE_HANDLE_STANDARD_ARGS)
>>   CMakeLists.txt:4 (find_package)
>>
>> I expected that if required components are missing cmake bails out while
>> continuing when OPTIONAL_COMPONENTS are missing. My current workaround is:
>> find_package(Qt4 OPTIONAL_COMPONENTS Invalid)
>> find_package(Qt4 REQUIRED COMPONENTS QtCore QtGui)
>>
>> But I would rather use a single line. Am I missing something here?
>>
>> Best regards,
>> Alexander
>>
>> --
>>
>> Powered by www.kitware.com
>>
>> Please keep messages on-topic and check the CMake FAQ at:
>> http://www.cmake.org/Wiki/CMake_FAQ
>>
>> Kitware offers various services to support the CMake community. For more
>> information on each offering, please visit:
>>
>> CMake Support: http://cmake.org/cmake/help/support.html
>> CMake Consulting: http://cmake.org/cmake/help/consulting.html
>> CMake Training Courses: http://cmake.org/cmake/help/training.html
>>
>> Visit other Kitware open-source projects at
>> http://www.kitware.com/opensource/opensource.html
>>
>> Follow this link to subscribe/unsubscribe:
>> http://public.kitware.com/mailman/listinfo/cmake
>
>
>
> --
>
> Powered by www.kitware.com
>
> Please keep messages on-topic and check the CMake FAQ at:
> http://www.cmake.org/Wiki/CMake_FAQ
>
> Kitware offers various services to support the CMake community. For more
> information on each offering, please visit:
>
> CMake Support: http://cmake.org/cmake/help/support.html
> CMake Consulting: http://cmake.org/cmake/help/consulting.html
> CMake Training Courses: http://cmake.org/cmake/help/training.html
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/cmake


More information about the CMake mailing list