aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Mensinger <daniel@mensinger-ka.de>2019-07-11 22:10:38 +0200
committerDaniel Mensinger <daniel@mensinger-ka.de>2019-07-31 17:50:02 +0200
commitbee74eea16b4f7849c154706398224c9a542c4bd (patch)
tree819b186c5235e9b77967548954849ba3e23c10a4
parent679ddb0ae780bfd4d81c586b0512255c0d1e24b6 (diff)
downloadmeson-bee74eea16b4f7849c154706398224c9a542c4bd.zip
meson-bee74eea16b4f7849c154706398224c9a542c4bd.tar.gz
meson-bee74eea16b4f7849c154706398224c9a542c4bd.tar.bz2
cmake: Parse a subset of generator expressions
-rw-r--r--mesonbuild/cmake/__init__.py2
-rw-r--r--mesonbuild/cmake/generator.py129
-rw-r--r--mesonbuild/cmake/traceparser.py7
3 files changed, 135 insertions, 3 deletions
diff --git a/mesonbuild/cmake/__init__.py b/mesonbuild/cmake/__init__.py
index 01e1980..f9835a1 100644
--- a/mesonbuild/cmake/__init__.py
+++ b/mesonbuild/cmake/__init__.py
@@ -23,10 +23,12 @@ __all__ = [
'CMakeTarget',
'CMakeTraceLine',
'CMakeTraceParser',
+ 'parse_generator_expressions',
]
from .common import CMakeException
from .client import CMakeClient
from .executor import CMakeExecutor
+from .generator import parse_generator_expressions
from .interpreter import CMakeInterpreter
from .traceparser import CMakeTarget, CMakeTraceLine, CMakeTraceParser
diff --git a/mesonbuild/cmake/generator.py b/mesonbuild/cmake/generator.py
new file mode 100644
index 0000000..a30d2de
--- /dev/null
+++ b/mesonbuild/cmake/generator.py
@@ -0,0 +1,129 @@
+# Copyright 2019 The Meson development team
+
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+
+# http://www.apache.org/licenses/LICENSE-2.0
+
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+from .. import mesonlib
+
+def parse_generator_expressions(raw: str) -> str:
+ '''Parse CMake generator expressions
+
+ Most generator expressions are simply ignored for
+ simplicety, however some are required for some common
+ use cases.
+ '''
+
+ out = '' # type: str
+ i = 0 # type: int
+
+ def equal(arg: str) -> str:
+ col_pos = arg.find(',')
+ if col_pos < 0:
+ return '0'
+ else:
+ return '1' if arg[:col_pos] == arg[col_pos + 1:] else '0'
+
+ def vers_comp(op: str, arg: str) -> str:
+ col_pos = arg.find(',')
+ if col_pos < 0:
+ return '0'
+ else:
+ return '1' if mesonlib.version_compare(arg[:col_pos], '{}{}'.format(op, arg[col_pos + 1:])) else '0'
+
+ supported = {
+ # Boolean functions
+ 'BOOL': lambda x: '0' if x.upper() in ['0', 'FALSE', 'OFF', 'N', 'NO', 'IGNORE', 'NOTFOUND'] or x.endswith('-NOTFOUND') else '1',
+ 'AND': lambda x: '1' if all([y == '1' for y in x.split(',')]) else '0',
+ 'OR': lambda x: '1' if any([y == '1' for y in x.split(',')]) else '0',
+ 'NOT': lambda x: '0' if x == '1' else '1',
+
+ '0': lambda x: '',
+ '1': lambda x: x,
+
+ # String operations
+ 'STREQUAL': equal,
+ 'EQUAL': equal,
+ 'VERSION_LESS': lambda x: vers_comp('<', x),
+ 'VERSION_GREATER': lambda x: vers_comp('>', x),
+ 'VERSION_EQUAL': lambda x: vers_comp('=', x),
+ 'VERSION_LESS_EQUAL': lambda x: vers_comp('<=', x),
+ 'VERSION_GREATER_EQUAL': lambda x: vers_comp('>=', x),
+
+ # String modification
+ 'LOWER_CASE': lambda x: x.lower(),
+ 'UPPER_CASE': lambda x: x.upper(),
+
+ # Always assume the BUILD_INTERFACE is valid.
+ # INSTALL_INTERFACE is always invalid for subprojects and
+ # it should also never appear in CMake config files, used
+ # for dependencies
+ 'INSTALL_INTERFACE': lambda x: '',
+ 'BUILD_INTERFACE': lambda x: x,
+
+ # Constants
+ 'ANGLE-R': lambda x: '>',
+ 'COMMA': lambda x: ',',
+ 'SEMICOLON': lambda x: ';',
+ }
+
+ # Recursively evaluate generator expressions
+ def eval_generator_expressions() -> str:
+ nonlocal i
+ i += 2
+
+ func = '' # type: str
+ args = '' # type: str
+ res = '' # type: str
+ exp = '' # type: str
+
+ # Determine the body of the expression
+ while i < len(raw):
+ if raw[i] == '>':
+ # End of the generator expression
+ break
+ elif i < len(raw) - 1 and raw[i] == '$' and raw[i + 1] == '<':
+ # Nested generator expression
+ exp += eval_generator_expressions()
+ else:
+ # Generator expression body
+ exp += raw[i]
+
+ i += 1
+
+ # Split the expression into a function and arguments part
+ col_pos = exp.find(':')
+ if col_pos < 0:
+ func = exp
+ else:
+ func = exp[:col_pos]
+ args = exp[col_pos + 1:]
+
+ func = func.strip()
+ args = args.strip()
+
+ # Evaluate the function
+ if func in supported:
+ res = supported[func](args)
+
+ return res
+
+ while i < len(raw):
+ if i < len(raw) - 1 and raw[i] == '$' and raw[i + 1] == '<':
+ # Generator expression detected --> try resolving it
+ out += eval_generator_expressions()
+ else:
+ # Normal string, leave unchanged
+ out += raw[i]
+
+ i += 1
+
+ return out
diff --git a/mesonbuild/cmake/traceparser.py b/mesonbuild/cmake/traceparser.py
index 6106d16..3a3f269 100644
--- a/mesonbuild/cmake/traceparser.py
+++ b/mesonbuild/cmake/traceparser.py
@@ -16,6 +16,7 @@
# or an interpreter-based tool.
from .common import CMakeException
+from .generator import parse_generator_expressions
from .. import mlog
from typing import List, Tuple, Optional
@@ -448,7 +449,6 @@ class CMakeTraceParser:
# The trace format is: '<file>(<line>): <func>(<args -- can contain \n> )\n'
reg_tline = re.compile(r'\s*(.*\.(cmake|txt))\(([0-9]+)\):\s*(\w+)\(([\s\S]*?) ?\)\s*\n', re.MULTILINE)
reg_other = re.compile(r'[^\n]*\n')
- reg_genexp = re.compile(r'\$<.*>')
loc = 0
while loc < len(trace):
mo_file_line = reg_tline.match(trace, loc)
@@ -466,9 +466,10 @@ class CMakeTraceParser:
file = mo_file_line.group(1)
line = mo_file_line.group(3)
func = mo_file_line.group(4)
- args = mo_file_line.group(5).split(' ')
+ args = mo_file_line.group(5)
+ args = parse_generator_expressions(args)
+ args = args.split(' ')
args = list(map(lambda x: x.strip(), args))
- args = list(map(lambda x: reg_genexp.sub('', x), args)) # Remove generator expressions
yield CMakeTraceLine(file, line, func, args)