[CMake] How to pass values up to parent CMakeLists.txt

James Bigler jamesbigler at gmail.com
Tue Mar 31 18:13:50 EDT 2009


On Tue, Mar 31, 2009 at 2:50 PM, Tyler Roscoe <tyler at cryptio.net> wrote:

> On Tue, Mar 31, 2009 at 04:47:23PM -0400, Lezz Giles wrote:
> > What I want to do is pass values up from src/CMakeLists.txt and
> tst/CMakeLists.txt that can be used in the custom_command.  A normal
> variable doesn't work - the value is scoped to the current CMakeLists.txt
> file.  I can define the values in the project CMakeLists.txt, but then that
> separates them from where they really should be, i.e.
> src|tst/CMakeLists.txt.
>
> Look at the PARENT_SCOPE argument to set():
>
> If PARENT_SCOPE is present, the variable will be set in the scope above
> the current scope. Each new directory or function creates a new scope.
> This command will set the value of a variable into the parent directory
> or calling function (whichever is applicable to the case at hand) If
> VALUE is not specified then the variable is removed from the parent
> scope.
>

I just recently discovered another way to do this.  I was troubled that
pushing it up to the parent scope only pushes it up one level, so if you
want to push a variable up to the top, you must push it at each level in the
tree.  My discovery was that using the environment variable mechanism is a
great alternative to a global variable. ;)

CMakeLists.txt:
set(ENV{myvar})

path/some/where/CMakeLists.txt:
set(ENV{myvar} "$ENV{myvar};${new_stuff}")

CMakeLists.txt:

set(myvar "$ENV{myvar}")

You can also look into define/get/set_property, but I've never used those.

James
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20090331/aea448f9/attachment.htm>


More information about the CMake mailing list