[CMake] lexical scoping

Philip Lowman philip at yhbt.com
Sun Nov 4 11:03:00 EST 2007


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Brandon Van Every wrote:
> And strongly consider SETLOCAL, SET_LOCAL, SET_CACHE or whatever.

Absolutely.  Here's what I would recommend...

SET() should always set a global variable to maintain complete backwards
compatibility.  SETCACHE() could be added to keep things consistent as
Brandon suggests.  Trying to make SET be able to set everything is just
going to over complicate the command... it's already complicated enough
as it is.  What really needs to happen is to break it into three
commands: one for globals, one for locals, and one for cache.

SETLOCAL() would be used to define local variables that are local to a
scoped region.  Scoped regions would include:
	- IF/ELSE/ENDIF scopes
	- FOREACH scopes
	- WHILE scopes
	- MACRO/ENDMACRO scopes

If CMake had a SETLOCAL command it would probably cover 95% of what
people need in terms of being able to create locally scoped variables
and backwards compatibility would be maintained by default.

If someone wanted to add the ability to declare additionally scoped
regions I suppose it could be added for completeness.  I honestly *hate*
the idea of having to maintain a list of scoped variables, however.
Sure it might make implementation easier but it's a hack:

LOCALSCOPE(scope_name var1 var2 var3 var4 var5 var6)
ENDLOCALSCOPE(scope_name)

If arbitrary scoped regions are needed at all it would be far easier
just to rely on something like this:

LOCALSCOPE(scope_name)
ENDLOCALSCOPE(scope_name)

In this case the scope_name really isn't needed at all to implement the
scope but it makes the code easier to read.

Not knowing much about CMake internals I can't estimate how hard it
would be to implement this but it would not be a trivial task.  The
concept of scopes would have to be introduced.  Variable lookup would
have to be able to be done on a stack of scopes.  Scopes would
automatically have to be created and destroyed at various times and the
list of current scopes would have to be in context during all variable
lookups.  If it was done correctly though you would have the exact same
scoping that C/C++, Java, and most other languages currently support.

- --
Philip Lowman
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFHLe00e0tOktX6RKkRAhqKAJ0b/0aSyZiZyqoLjVUqXu/PA12JTACeIqDj
2ECmcR/eYj++z2AkCNSnoIg=
=XD/p
-----END PGP SIGNATURE-----


More information about the CMake mailing list