[CMake] Question on internal CMAKE_* variables and scope

Brett Delle Grazie brett.dellegrazie at gmail.com
Tue Jul 31 06:23:42 EDT 2012


Forgot to cc list... (sorry!)

Hi Fabio,

On 31 July 2012 10:26, Fabio Fracassi <fabio.fracassi at charite.de> wrote:
> Hi,
>
> I ran into some problems when I tried to set a cmake internal variable
> (CMAKE_MODULE_PATH) from a function.
> I understand that when I use list() commands an new local scoped variable is
> created that I have to "export" to the parent scope. I tried to do that but
> it seems that the scope of cmake internal variables is a different thing.
> As a workaround I used a macro, but I'd rather not.

Functions are always in a new scope. You'll need to export
CMAKE_MODULE_PATH with:
set(CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH};<new_path_here>" PARENT_SCOPE)

which will set it in the calling scope or possibly set_property(
GLOBAL .... ) although I'm not sure that would work.

I've never done this from inside a function - instead I've always set
this at the top-level CMakeLists.txt within an include file:
include(cmake/module_paths.txt NO_POLICY_SCOPE)

And in cmake/module_paths.txt use something like:
... use an include guard here (omitted) ...
list( APPEND CMAKE_MODULE_PATH "$(CMAKE_CURRENT_LIST_DIR}/my_modules" )

The NO_POLICY_SCOPE on the include prevents the need for using set(
... PARENT_SCOPE) which will fail if this is the top scope.

>
> Is this a bug? Or is there something different I need to do?
>
> regards
>
> Fabio
>
> --
> Dipl.-Inf. Fabio Fracassi
> BZMM - Charite & Fraunhofer IPK (bzmm.charite.de)
> Augustenburger Platz. 1
> 13353 Berlin
>
> --
>
> 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:
> http://www.cmake.org/mailman/listinfo/cmake

--
Best Regards,

Brett Delle Grazie


-- 
Best Regards,

Brett Delle Grazie


More information about the CMake mailing list