[CMake] lexical scoping

Miguel A. Figueroa-Villanueva miguelf at ieee.org
Sat Nov 3 12:13:07 EDT 2007


On 11/3/07, Bill Hoffman wrote:
> David Cole wrote:
> > After all the discussion / suggestions that have been part of this
> > thread, I like the following best:
> >
> > local(scope_name)
> >   set(var1 "value1")
> >   set(var2 "value2")
> > endlocal(scope_name)
> >
> I would prefer to declare the variables that are part of the scope.  I
> think you would want to reference and set global variables from within a
> scope.  The PARENT_SCOPE maybe needed as well.
>
> local(scope_name var1 var2 ... varN)
>
> endlocal(scope_name)

Maybe I'm missing the point, so bare with me...

I like David's proposal, since it would be the closest to a regular
language except that instead of the typical curly braces you have the
local/endlocal keywords. Making a function that implicitly creates a
scope would avoid the cumbersome typing.

I would think that, in terms of referencing a variable, you would need
to go up in scope until you find the variable if available. The
problem is setting the variable. In this case you do need the
declaration of the variable if you want it in the local scope and it
is already available in a higher level scope. For example,

set(var1 ...) # creates global variable var1
set(var2 ...) # creates global variable var2
local(scope_name)
  set(var1 ...) # create a local variable or set the global variable?
  set(var3 ...) # create local variable var3
  if(var1 ...) ... # depends on the behaviour of set(var1 ...)
  if(var2 ...) ... # access the global variable var2
  if(var3 ...) ... # access the local variable var3
endlocal(scope_name)

The only question mark, from my point of view, is on setting var1. It
seems to me that for that case you have two options:

1. default to creating the local var1 and requiring an escape for
setting the global variable (PARENT_SCOPE, SETGLOBAL, or any other
variation).

2. default to creating everything global and require an escape for
setting the local variable (local(scope_name var1 var2 ... varN),
SETLOCAL, or any other variation). Note that this breaks the intuitive
behaviour above for set(var3 ...).

I prefer the first (1) case. I think it is more popular the case where
you have many local variables and a few relevant global ones, than
viceversa. If this is the case, then the first approach would be less
typing and more intuitive.

In terms of the CACHE, I would consider it the highest level scope and
SET(... CACHE ...) obviously refers to that global scope. Note that
when referencing a local variable that also exists in the CACHE might
create a change in behaviour... I'm not sure what happens currently,
but I think CACHE variables hide regular variables, right?

In any case, I think this discussion is finally leading somewhere!
Creating variable scopes is a quite useful feature.

--Miguel


More information about the CMake mailing list