[CMake] Recursively calling a macro: Is this possible in CMake?

Mike Jackson mike.jackson at bluequartz.net
Sat Jan 17 16:40:59 EST 2009


The CMake files for Boost does something like that to get a list of 
dependencies. If you are interested in that code it is in the Trunk of 
the Boost SVN repository. If you need more detail let me know.

Gotta Go
Mike

James Bigler wrote:
> This is a classic graph traversing problem. Each target is a node in the 
> graph. What you need to do is make sure you only recurse on nodes you 
> haven't visited yet. You can probably do this with a list of visited 
> targets and  use the list(FIND_ITEM) (double check the command in the 
> docs) to determine if you should recurse. Make sure to add the new node 
> to the visited list before you recurse.
> 
> James
> 
> On Jan 16, 2009, at 7:57 PM, "Robert Dailey" <rcdailey at gmail.com> wrote:
> 
>>
>> On Fri, Jan 16, 2009 at 6:23 PM, James Bigler <jamesbigler at gmail.com> 
>> wrote:
>> This code seems to produce the correct output with either a macro or a 
>> function prototype.
>>
>> macro( foo )
>>   list( APPEND my_list "+" )
>>   list( LENGTH my_list length )
>>   if( length LESS ${somevar} )
>>     foo()
>>   endif()
>>   # Add this line in for functions
>>   set( my_list ${my_list} PARENT_SCOPE )
>> endmacro()
>>
>> set(somevar 100)
>> set(my_list "")
>> foo()
>> message("my_list = ${my_list}")
>>
>>
>> My guess is that the code isn't necessarily inlined, but interpreted 
>> on the fly with special rules about variable scope.
>>
>> James
>>
>> Well my real code is slightly more complex. When I run it I get 
>> infinite recursion of some sort and I have to forcefully close CMake 
>> in order to stop it. Recursion in CMake isn't entirely intuitive so if 
>> someone could breeze over this code and give me some pointers I'd 
>> really appreciate that.
>>
>>
>> macro( add_includes_impl target_name )
>>     get_target_property( target_dependencies ${target_name} 
>> LINK_INTERFACE_LIBRARIES )
>>     foreach( dep ${target_dependencies} )
>>         if( IS_TARGET ${dep} )
>>             message( "${dep}" )
>>             list( APPEND flattened_includes ${dep_includes} )
>>             add_includes_impl( ${dep} )
>>         endif()
>>     endforeach()
>> endmacro()
>>
>> macro( add_includes target_name )
>>     set( flattened_includes )
>>     add_includes_impl( target_name )
>> endmacro()
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> CMake mailing list
> CMake at cmake.org
> http://www.cmake.org/mailman/listinfo/cmake


More information about the CMake mailing list