[CMake] lexical scoping

Bill Hoffman bill.hoffman at kitware.com
Sat Nov 3 12:27:42 EDT 2007


Miguel A. Figueroa-Villanueva wrote:
> 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 guess I think the code would be much clearer if you had to declare a 
variable local.  I think that the convention of any set in a local scope 
creating a new variable would be confusing and cause plenty of errors. 
Remember you can add scope anywhere even around existing stuff.  So lets 
say you have something like this:


local_scope()
    ....
    set(a "-flag")
    set(CMAKE_CXX_FLAGS ${a} )
# this is not going to set the variable that everyone expects!
# it will create a local variable that won't be used at all by the
# generators.
end_scope()

Declaring a variable as local is done in perl.  Which had the same sort 
of problem.  By default all variables in perl are global.  You have to 
put a "my" in front of the variable to specifically ask it to be a local 
variable.

I think this will be used a lot with existing modules that set plenty of 
global variables, and just adding a local_scope around some existing 
code will break it if you make all set's automatically create a new 
local variable.

-Bill



More information about the CMake mailing list