[CMake] find_package scoping mystery

Andreas Pakulat apaku at gmx.de
Mon Apr 16 05:01:11 EDT 2012


On 16.04.12 01:37:58, Dave Abrahams wrote:
> 
> consider this simple wrapper:
> 
>   function(my_find_package)
>     find_package(${ARGV})
>   endfunction()
> 
> If I replace all the calls to find_package in my project with calls to
> my_find_package, everything breaks.  It appears variable settings in any
> found <packagename>Config.cmake files are visible inside
> my_find_package, but disappear from the point of view of the caller,
> unless I "manually" hoist them into the parent scope using
> 
>   function(my_find_package)
>     find_package(${ARGV})
>     set(some_variable ${some_variable} PARENT_SCOPE)
>   endfunction()
> 
> I've tried hoisting everything as in
> https://github.com/ryppl/ryppl-cmake/blob/find-package-hook/Modules/Ryppl.cmake#L9,
> but apparently that is too much and it clobbers some variables that it
> shouldn't.
> 
> Can anyone explain to me what I'm failing to grasp, here?

Functions have their own scope and the find module are written to be
executed in the global scope. (functions are still a relatively new
feature in cmake)

I can see a few options:
- Don't run find_package inside a function.
- Use a macro instead of a function, macro's do not create a new scope
- inspect all the find-modules you use and adapt them to use
  PARENT_SCOPE for variables when they do not end up in the cache.

Andreas



More information about the CMake mailing list