[CMake] Pass variables to sub directory CMakeLists.txt

Alan W. Irwin irwin at beluga.phys.uvic.ca
Thu Feb 21 05:54:19 EST 2008


On 2008-02-21 08:25+0100 Vandenbroucke Sander wrote:

> Hi,
>
> I want to pass a variable to CMakeLists.txt's in sub directories:
>
> Top directory CMakeLists.txt:
> set( VAR1 "" )
> add_subdirectory (subdir1)
> add_subdirectory (subdir2)
>
> Each subdir CMakeLists.txt:
>
> ...
> set( VAR1 "${VAR1} extend_var1" )
> ...
>
> I use VAR1 later on in the linker.
>
> How can I make this work?

The reason why this does not work in the present form is you are attempting
to pass VAR1 up from subdir1 to top-level, before passing it down again to
subdir2 from the top-level.  Passing variables down is fine, but passing
variables from subdirectories to their parent normally does not occur with
CMake.

>From recent discussion on this list you should use the
GET_DIRECTORY_PROPERTY command to pass variable information to parent
directories.  So instead of the above use the following (subject to some
uncertainty since I have no practical experience with this method).

Top directory CMakeLists.txt:
set( VAR1 "" )
add_subdirectory (subdir1)
get_directory_property(VAR1 DIRECTORY subdir1 VARIABLES VAR1)
add_subdirectory (subdir2)
get_directory_property(VAR1 DIRECTORY subdir2 VARIABLES VAR1)
...

Anyhow, you get the general idea subject to some uncertainty on my part of
what the exact syntax for GET_DIRECTORY_PROPERTY should be.  Perhaps I am
just too sleepy at this time in the morning, but that particular
documentation seems to be a bit unclear to me.  So if the above syntax
doesn't work (which you can confirm by using the MESSAGE command), then you
may have to adjust it a bit.

Good luck.

Alan
__________________________
Alan W. Irwin

Astronomical research affiliation with Department of Physics and Astronomy,
University of Victoria (astrowww.phys.uvic.ca).

Programming affiliations with the FreeEOS equation-of-state implementation
for stellar interiors (freeeos.sf.net); PLplot scientific plotting software
package (plplot.org); the libLASi project (unifont.org/lasi); the Loads of
Linux Links project (loll.sf.net); and the Linux Brochure Project
(lbproject.sf.net).
__________________________

Linux-powered Science
__________________________


More information about the CMake mailing list