| 1 |
# SPDX-License-Identifier: Apache-2.0 |
| 2 |
# Copyright 2019 The Meson development team |
| 3 |
|
| 4 |
from __future__ import annotations |
| 5 |
|
| 6 |
from .. import mesonlib |
| 7 |
from .. import mlog |
| 8 |
from .common import cmake_is_debug |
| 9 |
import typing as T |
| 10 |
|
| 11 |
if T.TYPE_CHECKING: |
| 12 |
from .traceparser import CMakeTraceParser, CMakeTarget |
| 13 |
|
| 14 |
def parse_generator_expressions( |
| 15 |
raw: str, |
| 16 |
trace: 'CMakeTraceParser', |
| 17 |
*, |
| 18 |
context_tgt: T.Optional['CMakeTarget'] = None, |
| 19 |
) -> str: |
| 20 |
'''Parse CMake generator expressions |
| 21 |
|
| 22 |
Most generator expressions are simply ignored for |
| 23 |
simplicity, however some are required for some common |
| 24 |
use cases. |
| 25 |
''' |
| 26 |
|
| 27 |
# Early abort if no generator expression present |
| 28 |
if '$<' not in raw: |
| 29 |
return raw |
| 30 |
|
| 31 |
out = '' |
| 32 |
i = 0 |
| 33 |
|
| 34 |
def equal(arg: str) -> str: |
| 35 |
col_pos = arg.find(',') |
| 36 |
if col_pos < 0: |
| 37 |
return '0' |
| 38 |
else: |
| 39 |
return '1' if arg[:col_pos] == arg[col_pos + 1:] else '0' |
| 40 |
|
| 41 |
def vers_comp(op: str, arg: str) -> str: |
| 42 |
col_pos = arg.find(',') |
| 43 |
if col_pos < 0: |
| 44 |
return '0' |
| 45 |
else: |
| 46 |
return '1' if mesonlib.version_compare(arg[:col_pos], '{}{}'.format(op, arg[col_pos + 1:])) else '0' |
| 47 |
|
| 48 |
def target_property(arg: str) -> str: |
| 49 |
# We can't really support this since we don't have any context |
| 50 |
if ',' not in arg: |
| 51 |
if context_tgt is None: |
| 52 |
return '' |
| 53 |
return ';'.join(context_tgt.properties.get(arg, [])) |
| 54 |
|
| 55 |
args = arg.split(',') |
| 56 |
props = trace.targets[args[0]].properties.get(args[1], []) if args[0] in trace.targets else [] |
| 57 |
return ';'.join(props) |
| 58 |
|
| 59 |
def target_file(arg: str) -> str: |
| 60 |
if arg not in trace.targets: |
| 61 |
mlog.warning(f"Unable to evaluate the cmake variable '$<TARGET_FILE:{arg}>'.") |
| 62 |
return '' |
| 63 |
tgt = trace.targets[arg] |
| 64 |
|
| 65 |
cfgs = [] |
| 66 |
cfg = '' |
| 67 |
|
| 68 |
if 'IMPORTED_CONFIGURATIONS' in tgt.properties: |
| 69 |
cfgs = [x for x in tgt.properties['IMPORTED_CONFIGURATIONS'] if x] |
| 70 |
cfg = cfgs[0] |
| 71 |
|
| 72 |
if cmake_is_debug(trace.env): |
| 73 |
if 'DEBUG' in cfgs: |
| 74 |
cfg = 'DEBUG' |
| 75 |
elif 'RELEASE' in cfgs: |
| 76 |
cfg = 'RELEASE' |
| 77 |
else: |
| 78 |
if 'RELEASE' in cfgs: |
| 79 |
cfg = 'RELEASE' |
| 80 |
|
| 81 |
if f'IMPORTED_IMPLIB_{cfg}' in tgt.properties: |
| 82 |
return ';'.join([x for x in tgt.properties[f'IMPORTED_IMPLIB_{cfg}'] if x]) |
| 83 |
elif 'IMPORTED_IMPLIB' in tgt.properties: |
| 84 |
return ';'.join([x for x in tgt.properties['IMPORTED_IMPLIB'] if x]) |
| 85 |
elif f'IMPORTED_LOCATION_{cfg}' in tgt.properties: |
| 86 |
return ';'.join([x for x in tgt.properties[f'IMPORTED_LOCATION_{cfg}'] if x]) |
| 87 |
elif 'IMPORTED_LOCATION' in tgt.properties: |
| 88 |
return ';'.join([x for x in tgt.properties['IMPORTED_LOCATION'] if x]) |
| 89 |
return '' |
| 90 |
|
| 91 |
supported: T.Dict[str, T.Callable[[str], str]] = { |
| 92 |
# Boolean functions |
| 93 |
'BOOL': lambda x: '0' if x.upper() in {'', '0', 'FALSE', 'OFF', 'N', 'NO', 'IGNORE', 'NOTFOUND'} or x.endswith('-NOTFOUND') else '1', |
| 94 |
'AND': lambda x: '1' if all(y == '1' for y in x.split(',')) else '0', |
| 95 |
'OR': lambda x: '1' if any(y == '1' for y in x.split(',')) else '0', |
| 96 |
'NOT': lambda x: '0' if x == '1' else '1', |
| 97 |
|
| 98 |
'IF': lambda x: x.split(',')[1] if x.split(',')[0] == '1' else x.split(',')[2], |
| 99 |
|
| 100 |
'0': lambda x: '', |
| 101 |
'1': lambda x: x, |
| 102 |
|
| 103 |
# String operations |
| 104 |
'STREQUAL': equal, |
| 105 |
'EQUAL': equal, |
| 106 |
'VERSION_LESS': lambda x: vers_comp('<', x), |
| 107 |
'VERSION_GREATER': lambda x: vers_comp('>', x), |
| 108 |
'VERSION_EQUAL': lambda x: vers_comp('=', x), |
| 109 |
'VERSION_LESS_EQUAL': lambda x: vers_comp('<=', x), |
| 110 |
'VERSION_GREATER_EQUAL': lambda x: vers_comp('>=', x), |
| 111 |
|
| 112 |
# String modification |
| 113 |
'LOWER_CASE': lambda x: x.lower(), |
| 114 |
'UPPER_CASE': lambda x: x.upper(), |
| 115 |
|
| 116 |
# Always assume the BUILD_INTERFACE is valid. |
| 117 |
# INSTALL_INTERFACE is always invalid for subprojects and |
| 118 |
# it should also never appear in CMake config files, used |
| 119 |
# for dependencies |
| 120 |
'INSTALL_INTERFACE': lambda x: '', |
| 121 |
'BUILD_INTERFACE': lambda x: x, |
| 122 |
|
| 123 |
# Constants |
| 124 |
'ANGLE-R': lambda x: '>', |
| 125 |
'COMMA': lambda x: ',', |
| 126 |
'SEMICOLON': lambda x: ';', |
| 127 |
|
| 128 |
# Target related expressions |
| 129 |
'TARGET_EXISTS': lambda x: '1' if x in trace.targets else '0', |
| 130 |
'TARGET_NAME_IF_EXISTS': lambda x: x if x in trace.targets else '', |
| 131 |
'TARGET_PROPERTY': target_property, |
| 132 |
'TARGET_FILE': target_file, |
| 133 |
} |
| 134 |
|
| 135 |
# Recursively evaluate generator expressions |
| 136 |
def eval_generator_expressions() -> str: |
| 137 |
nonlocal i |
| 138 |
i += 2 |
| 139 |
|
| 140 |
func = '' |
| 141 |
args = '' |
| 142 |
res = '' |
| 143 |
exp = '' |
| 144 |
|
| 145 |
# Determine the body of the expression |
| 146 |
while i < len(raw): |
| 147 |
if raw[i] == '>': |
| 148 |
# End of the generator expression |
| 149 |
break |
| 150 |
elif i < len(raw) - 1 and raw[i] == '$' and raw[i + 1] == '<': |
| 151 |
# Nested generator expression |
| 152 |
exp += eval_generator_expressions() |
| 153 |
else: |
| 154 |
# Generator expression body |
| 155 |
exp += raw[i] |
| 156 |
|
| 157 |
i += 1 |
| 158 |
|
| 159 |
# Split the expression into a function and arguments part |
| 160 |
col_pos = exp.find(':') |
| 161 |
if col_pos < 0: |
| 162 |
func = exp |
| 163 |
else: |
| 164 |
func = exp[:col_pos] |
| 165 |
args = exp[col_pos + 1:] |
| 166 |
|
| 167 |
func = func.strip() |
| 168 |
args = args.strip() |
| 169 |
|
| 170 |
# Evaluate the function |
| 171 |
if func in supported: |
| 172 |
res = supported[func](args) |
| 173 |
else: |
| 174 |
mlog.warning(f"Unknown generator expression '$<{func}:{args}>'.", once=True, fatal=False) |
| 175 |
|
| 176 |
return res |
| 177 |
|
| 178 |
while i < len(raw): |
| 179 |
if i < len(raw) - 1 and raw[i] == '$' and raw[i + 1] == '<': |
| 180 |
# Generator expression detected --> try resolving it |
| 181 |
out += eval_generator_expressions() |
| 182 |
else: |
| 183 |
# Normal string, leave unchanged |
| 184 |
out += raw[i] |
| 185 |
|
| 186 |
i += 1 |
| 187 |
|
| 188 |
return out |