[CMake] Passing values back from SUBDIRS

Brad King brad.king at kitware.com
Wed Mar 23 08:00:36 EST 2005


John Biddiscombe wrote:
> 
>>  (Perhaps I could write data to a file using append and then read it 
>> back somehow)
> 
> 
> I am having trouble with this too.
> If I write a blank file at the start of the CMakelists file in the top 
> dir, it does not actually write anything until after the subdirs have 
> written their stuff. So it overwrites them. Additionally, the Subdirs 
> write out stuff 3 times (3? seems a strange number of times to scan each 
> cmake file).
> So the subdirs are evaluated before the main dir has a chance to 
> initialize things and if you attempt to read the file, it occurts before 
> the sub dirs have written anything
> 
> FILE(WRITE filename "STUFF")
> SUBDIR(subdir writes "MORE STUFF" into same file using APPEND)
> FILE(READ filename variable)
> 
> variable now holds "STUFF". Blinkin Flip.  If I remove the FILE(WRITE 
> filename "STUFF") from the top level cmake file, then I get
> MORE STUFF MORE STUFF MORE STUFF
> as my contents - and the next time I run it I get 6 of them because I've 
> not reset the file with blank at start.
> 
> hmmm. There must be an easy way of doing this.

This is not possible because subdirectory code always executes AFTER 
parent directory code.  The SUBDIRS command is NOT processed inline.  It 
merely tells CMake to go process the subdirectories listed after it 
finishes with the current directory.

You can approximate the behavior by setting a cache variable in the 
subdirectory but the parent directory won't see the value until the next 
configuration:

SET(SUBDIR_INFO "..." CACHE INTERNAL "my subdir info")

We are attempting to solve this problem for the next version of CMake to 
make subdirectories more inuitive.

-Brad


More information about the CMake mailing list