[CMake] lexical scoping

Brandon Van Every bvanevery at gmail.com
Fri Nov 2 12:54:51 EDT 2007


Is it feasible to modify the CMake language to have lexical scoping?

My code would be a lot more readable and bug free if I didn't have to
write things like this all day long:

MACRO(SPLIT_ON_LASTWORD the_stream the_word
  splitonlastword_preamble splitonlastword_got_match splitonlastword_trail
  )
  # splitonlastword_preamble or splitonlastword_trail could evaluate
to the same thing as the_stream, so don't use them directly.
  # Don't modify splitonlastword_preamble or splitonlastword_trail if
there's no match.
  PREAMBLE_LASTWORD(${the_stream} ${the_word}
splitonlastword_scratch_preamble ${splitonlastword_got_match})
  IF(${splitonlastword_got_match})
    SET(splitonlastword_not_trail
"${splitonlastword_scratch_preamble}${the_word}")
    SET(splitonlastword_empty "")
    # read the_stream first!  The replaced arguments could evaluate to
the_stream.
    REPLACE_FIRST(
      splitonlastword_not_trail
      splitonlastword_empty
      ${splitonlastword_trail} ${the_stream})
    SET(${splitonlastword_preamble} "${splitonlastword_scratch_preamble}")
  ELSE(${splitonlastword_got_match})
    SET(${splitonlastword_preamble})
    SET(${splitonlastword_trail})
  ENDIF(${splitonlastword_got_match})
ENDMACRO(SPLIT_ON_LASTWORD)

This is just a helper function, that relies in turn on 2 other helper
functions.  Why do I end up writing such things at all?  I think it
happens when you're trying to build a 400MB codebase.  You just end up
needing more stuff than CMake inherently has.  I imagine CMake module
authors are in a similar position.

The only disambiguation convention I've come up with, that I can count
on preventing errors no matter how much code I write, is to use some
form of the entire function name as a prefix.  Previously I was using
an acronym of the function name as a prefix.  But I started noticing
it was pretty likely I'd end up with the same three letter acronyms at
some point.  Prefixes like "the_" or "my_" don't disambiguate
anything, they become common patterns as I continue to code.  The only
reason I've still got the_ prefixes is because those arguments are
supposed to be read-only, so aliasing shouldn't matter.


Cheers,
Brandon Van Every


More information about the CMake mailing list