Interpreter

Helper functions for executing CMake code in Python.

class cmake_parser.interpreter.Context(parent=None, var=<factory>, env=<factory>, cache=<factory>, functions=<factory>, macros=<factory>)

Execution context for CMake code.

Parameters:
  • parent (Optional[Self]) – the parent of a scoped context or None for the top-level context.

  • var (Dict[str, str]) – defined CMake variables

  • env (Dict[str, str]) – available environment variables

  • cache (Dict[str, str]) – defined CMake cache variables

  • functions (Dict[str, Function]) – defined functions

  • macros (Dict[str, Macro]) – defined macros

exists(f)

Helper function to check if a file or directory exists

Parameters:

f (str) – a path name

Return type:

bool

Returns:

True if the path name refers to an existing file or directory.

class cmake_parser.interpreter.LookAheadIterator(iterable)
cmake_parser.interpreter.eval_bool_expr(ctx, args)

Evaluate boolean expressions.

Some CMake command such as if() have a boolean expression as argument. This function will evaluate such expressions to False or True given the current execution context.

Note

This function does not resolve variable references, so you need to pass the argument list through resolve_args() first.

Parameters:
  • ctx (Context) – the current execution context

  • args (List[Token]) – the boolean expression with all variable references resolved

Return type:

bool

Returns:

the boolean value of the expression

Raise:

CMakeExprError if the expression is malformed.

cmake_parser.interpreter.eval_expr(ctx, args)

Evaluate boolean expressions.

Some CMake command such as if() have a boolean expression as argument. This function will evaluate such expressions to False or True given the current execution context.

Note

This function does not resolve variable references, so you need to pass the argument list through resolve_args() first.

Parameters:
  • ctx (Context) – the current execution context

  • args (List[Token]) – the boolean expression with all variable references resolved

Return type:

bool

Returns:

the boolean value of the expression

Raise:

CMakeExprError if the expression is malformed.

cmake_parser.interpreter.resolve_args(ctx, args)

Resolve variable references in argument lists.

Due to the semantics of unquoted variable references, this function may also change the number and type of arguments, which means that the returned argument list can have more or fewer items than the input, depending on the execution context.

The returned token list will have a proper 1:1 relation between function arguments and tokens. Boolean expressions are suitable input for eval_bool_expr() after they have been processed by this function.

Parameters:
Return type:

List[Token]

Returns:

a new argument list with all variable references resolved