[CMake] lexical scoping

KSpam keesling_spam at cox.net
Fri Nov 2 15:23:43 EDT 2007


Ken,

> But to the question at hand...adding scope to variables. Variables have
> scope right now of
>
> 1) the CMakeListfile they are defined in
> 2) any included files that are included after the variable is set
> 3) and subdirectories of the directory they were defined in and processed
> after the setting of the variable
> 4) they can be access globally using get_directory_property and specifying
> the directory etc

I do like the effect of "cascading" variables down the CMakeLists.txt 
hierarchical structure.  This is very handy.  Of course, for variables that 
are not required to be passed down the chain, this is a maintenance headache.  
IMHO, your solution of adding functions would resolve much of this headache.

> the macro command is like a cpp macro so scoping does not make a lot of
> sense for it. What would/could make sense is a function command that
> creates a function. (think of the difference in a macro versus a function
> in c, macros have no inherent scope they are just string replacements,
> functions do have scope built in). With a function command we could
> probably figure out some way to create variables that are scoped to the
> function only. Most of the places we are using macros really could/should
> be functions. My main question would be if we had a function, how would you
> want variables handled.

I agree that most macros would be better suited as functions.  I am certain 
that this is the case for my projects.  I would suspect that 80% of my macros 
should really be functions.

> 1) all set commands create "local" variables to the function (a mess for
> functions that do an include of other cmake code)
> 2) a special set command or signature that creates a local variable scoped
> to the current function
> 3) a mode to switch the behavior of set (yuck)
> 4) something else

My vote would be for 2.  I like to be able to grep my CMakeLists.txt files.  
Having two set commands (or signatures) would ease the task of grepping 
for "global" vs. local variables.  This would also make it easy to set either 
type of variable in a function definition.

Thanks,
Justin


More information about the CMake mailing list