[CMake] Second post: find_package with config file always set xyz_FOUND to true?
anton.deguet at jhu.edu
Mon Sep 19 16:01:38 EDT 2011
Sorry, I mistakenly trashed your previous answer, thank you for both replies.
It looks like I am not the first one to raise that question. If I understand well, the primary issue is to agree on the semantic of:
find_package (xyz COMPONENTS c1 c2)
if (xyz_FOUND) …
xyz_FOUND can mean:
- xyz has been found and since you asked for components c1 and c2, please check the variables xyz_c1_FOUND and xyz_c2_FOUND to see if these components are available
- xyz has been found along with c1 and c2
I would personally prefer the second approach as it makes it easy to test if everything has been found as required. Maybe there is yet another variable to indicate that all components have been found, say xyz_FOUND_AS_REQUIRED. That would allow users to write simpler code:
if (xyz_FOUND AND xyz_c1_FOUND AND xyz_c2_FOUND)
Ultimately I will stick to the recommended approach.
The second aspect of the issue (and this is what I initially encountered) is the different behavior of find_package for either a module find or a config file. I wrote a CMake file/scripts that sets xyz_FOUND and used it for both cases, i.e. installed it as either Findxyz.cmake or xyz-config.cmake. It was a bit disconcerting to have my xyz_FOUND variable overwritten in one case and not the other. I understand that this is strongly tied to the defined/accepted meaning of xyz_FOUND and I was apparently mis-using it. But ... maybe … the find_package could be slightly modified:
- send a warning to the user (developer message) if the variable xyz_FOUND is modified/set by the file xyz-config.cmake
- more subtle and might have implications I don't foresee, if after loading the file xyz-config.cmake find_package realizes that xyz_FOUND has been set, let it as is. That would allow me to keep using xyz_FOUND incorrectly :-)
Again, thank you for your answer.
On Sep 19, 2011, at 2:21 PM, Michael Hertling wrote:
> On 09/19/2011 07:09 PM, Anton Deguet wrote:
>> I haven't heard anything following my previous post. Is there any chance someone could provide some guidance?
>> On Aug 29, 2011, at 6:02 PM, Anton Deguet wrote:
>>> 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)
>>> - 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?
> Second try: http://email@example.com/msg37840.html
> Powered by www.kitware.com
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
> Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ
> Follow this link to subscribe/unsubscribe:
More information about the CMake