[CMake] global variable

Brad King brad.king at kitware.com
Fri Sep 9 10:18:44 EDT 2005


klaas.holwerda wrote:
> I wonder what is the scope of a variable, and not only that, i also 
> notice command that commands like INCLUDE_DIRECTORIES in some 
> subdirectory, are not noticed at the top or other subdirectories at the 
> same level.
> 
> Can someone explain what is the scope of commands in Cmake?
> I looks like that it is the directory where it is in and all 
> subdirectories?

That is correct.  The top-level directory is initialized with the cache. 
  The subdirectory state is initialized with that of the parent 
directory up to the point of the ADD_SUBDIRECTORY command.  Except for 
the cache, no information comes back up out of a directory.  This allows 
subprojects to do whatever they want without affecting the parent project.

> Is there a way to make the scope of variables global? ( i tried CACHE , 
> which did not help ).

If you do

SET(MYVAR "myvalue" CACHE INTERNAL "")

in the subdirectory the result will be available in the parent directory 
on the NEXT configure step.  Currently there is no easy way to get 
information from a subdirectory in a parent directory during a single 
configure step.

You'll be surprised how rarely this is really needed if the directory 
structure is organized properly.  However, there should still be an 
easier way to do it for the times it is needed than using the file 
write/read trick.  When designing the ADD_SUBDIRECTORY command we 
considered adding an optional argument that might work like this:

ADD_SUBDIRECTORY(foo EXPORT_VARIABLES FOO_VAR1 FOO_VAR2)

The ADD_SUBDIRECTORY command would evaluate the subdirectory 
configuration and then set the variables FOO_VAR1 and FOO_VAR2 to their 
subdirectory values in the current directory.  This would allow 
subdirectories to continue to work in isolation but the parent directory 
would have the option of getting information it wants.  Since the 
variables are explicitly listed then it will be clear when/how these 
variables are set just from reading the CMake code in a directory and 
its parents without exploring all children.

This was never implemented though.  You can submit a feature request for 
it here:

http://www.cmake.org/Bug

-Brad


More information about the CMake mailing list