[cmake-developers] Branches on next

Ben Boeckel ben.boeckel at kitware.com
Tue Feb 11 17:54:01 EST 2014


On Tue, Feb 11, 2014 at 17:18:22 -0500, Matthew Woehlke wrote:
> On a loosely related note, did you know that there are now at least
> two Python parsers for CMake script? (Besides I believe a C++ one in
> KDevelop...)
> 
> 1. https://github.com/ijt/cmakelists_parsing
> 2. https://github.com/mwoehlke-kitware/Slicer/blob/3269-publish-extension-wizard/Utilities/Scripts/SlicerWizard/CMakeParser.py
> 
> It would be interesting to know how accurate these are. (Although if
> the README for the (1) is accurate, that one isn't anything to write
> home about.)
> 
> (One reason I mention it is because when I wrote (2), I made an
> attempt to look at CMake's own parsing code. Alas, I do not speak
> lex...)

Parsing in CMake is split into separate sections: the part which parses
the lines into CMake's command calls and the part which expands
variables (which is why "${cmd}(${args})" isn't allowed).
ExpandVariablesInString is the part which takes a string which may have
variables in it and dereferences them. It also handles @-expansion.
The basics aren't too hard…it's the corner cases and known what the
various parameters are in different parts of the code. These include
whether quotes are escaped or not, whether to actually deal with escape
characters, and whether it is @-only.

If you're looking for corner-cases, check out the RunCMake.Syntax test
in the source tree. If you can handle that, you're probably doing pretty
well on the basics. If you actually want to know what CMake will do when
it gets somewhere, you actually have to parse the code.

Corner cases I hit while developing the new parser:

  - in "\@var@", 'var' is expanded;
  - "${@var@}" and "@${var}@" are *not* the same (all @ expansion takes
    place before any ${} expansion);
  - '\n', '\t', and '\r' are only interpreted in certain contexts (see
    noEscapes in the EVIS method);
  - '\;' is always literal (don't ask why, but if you don't keep the
    slash around, things fail);
  - in '@var@', if 'var' is empty, it is left alone…sometimes (the
    removeEmpty parameter);
  - don't forget "$CACHE{}".

--Ben



More information about the cmake-developers mailing list