[CMake] find_package with config file always set xyz_FOUND to true?

Michael Hertling mhertling at online.de
Tue Aug 30 10:27:03 EDT 2011


On 08/30/2011 12:02 AM, Anton Deguet wrote:
> Hello,
> 
> I am trying to use the following syntax:
> find_package (xyz REQUIRED xyz1 xyz2) using a config file, i.e. NOT Findxyz.cmake.
> 
> I have a file named xyz-config.cmake that contains some code to check if the required components are available or not (based on how xyz was compiled).  Ideally I would like the find_package command to set xyz_FOUND to false if:
> - xyz-config.cmake file is not found (that's working by default)
> OR
> - xyz-config.cmake file is found BUT one or more component is missing
> 
> In my xyz-config.cmake, I tried:
> - set (xyz_FOUND FALSE)  # overall package not found as required
> - set (xyz2_FOUND FALSE)  # component not found, hoping find_package would compare this to the list xyz_FIND_COMPONENTS
> 
> But in all cases, it seems that find_package resets xyz_FOUND to 1 as long as the file xyz-config.cmake is found.  Is there a way to set xyz_FOUND to 0 within xyz-config.cmake?

AFAIK, there isn't, and that's absolutely reasonable, IMO; see [1] for
a related discussion. Since the config file is installed along with the
package - in contrast to find modules - the presence of the config file
means that the package is present, too, so setting the package's FOUND
variable unconditionally to TRUE is perfect. If the package has several
components, and you want to know which of them are present, the config
file should be component-aware and provide component-specific XXX_YY_
FOUND variables, i.e. for package XXX and component YY, as it is
recommended in [2]. In this way, you can issue

FIND_PACKAGE(XXX COMPONENTS YY1 YY2 YY3 ...)

and refer to variables XXX_[YY{1,2,3,...}_]FOUND afterwards. Of course,
if one uses REQUIRED instead of COMPONENTS, the config file is expected
to bail out if not all requested components and their prerequisites are
found. Furthermore, note:

(1) If XXX is not found, no variable XXX_YYY_FOUND has received a
    definite value, so you should always check for XXX_FOUND AND XXX_
    YY_FOUND in order to avoid to refer to an accidental value alone.
(2) Personally, I'd consider it as a good style to check XXX_YY_FOUND
    only for components YY that have been requested explicitly in the
    FIND_PACKAGE() call. This is to take account for config files and
    find modules that possibly don't look for components which aren't
    requested by the user or aren't needed as prerequisites; think of
    a component that is expensive to find, so it isn't searched if it
    isn't required. In other words, do not rely on XXX_YY_FOUND to
    hold a definite value if you haven't requested YY explicitly.

Regards,

Michael

[1] http://www.mail-archive.com/cmake@cmake.org/msg28431.html
[2] ${CMAKE_ROOT}/Modules/readme.txt


More information about the CMake mailing list