[CMake] lexical scoping

Eric Noulard eric.noulard at gmail.com
Sat Nov 3 14:19:13 EDT 2007


I'm taking this thread after some hours away from computer.
I read the whole thread (without missing someything I hope)
and the scoping name feature seems interesting.

I have some more ideas on syntax and keyword name
keep reading...

2007/11/3, Miguel A. Figueroa-Villanueva <miguelf at ieee.org>:
>
> The only thing I can think of to make it more pleasant to type (but
> functionally equivalent) is to have it integrate with the
> function/scoped_macro and set commands. Since we now have scope, then
> allow SET(... LOCAL) and have a function command as mentioned before,
> which has an implicit scope declaration.

I agree with that SET( ... LOCAL) would be nice but I would suggest
something more like C++ namespace (without the using keyword)

>
> macro(macro_name ...)
> local(scope_name var1 var2 ... varN)
> <code>
> endlocal(scope_name)
> endmacro(macro_name)

Why should a scope be "local"
I would rather write

set(scopename:scopedvar <value>)

which will define a variable "scopedvar" in scope "scopename"

the "scopename" may be declared (or not) with
scope(scopename)

you may refer to the variable elsewhere using the scope prefix

set(B ${scopename:scopedvar})

would set the "global" var B to value of the scopevar.

>
> function(macro_name ...)
>   set(var1 <value> LOCAL)
> endfunction(macro_name)
>

Then when you are in a function or macro you always
have a "local" scope at hand thus

set(local:localvar <value>)

means that you use a var 'localvar' in the local scope
which will disappear at the end of function or macro.

In the end if you want to put some var in a scope
you use:

set(<scopename>:VAR <value>)

in you want to refer to the var

${<scopename>:VAR}

You may declare as many scope as you want and
you always have:

the global scope:
set(GVAR <value>)               /  ${GVAR}
set(global:GVAR <value>)    /  ${global:GVAR}

the local scope is the same as other scopes
but variables used (set or referenced)
in this scope will disappear at the end  of FUNCTION or MACRO
block.

You may want to avoid the scope prefix using

usingscope(<scopename>)

endusingscope(<scopename>)

during which
set(a <value>)
is a synonym of
set(<scopename>:a value>)
but inside this you must refer/set global variable a with
global:a

Before writing this I've tried (with CMake 2.4.7)

SET(B;A "value")
which seems OK
but
MESSAGE("B:A = ${B:A}")
gives me:
syntax error, unexpected cal_SYMBOL, expecting } (10), when parsing
string "B = ${B:A}"

which makes me think using ":" as scoping operator
may be possible?

-- 
Erk


More information about the CMake mailing list