Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | """ |
| 2 | BitBake Smart Dictionary Implementation |
| 3 | |
| 4 | Functions for interacting with the data structure used by the |
| 5 | BitBake build tools. |
| 6 | |
| 7 | """ |
| 8 | |
| 9 | # Copyright (C) 2003, 2004 Chris Larson |
| 10 | # Copyright (C) 2004, 2005 Seb Frankengul |
| 11 | # Copyright (C) 2005, 2006 Holger Hans Peter Freyther |
| 12 | # Copyright (C) 2005 Uli Luckas |
| 13 | # Copyright (C) 2005 ROAD GmbH |
| 14 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 15 | # SPDX-License-Identifier: GPL-2.0-only |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 16 | # |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 17 | # Based on functions from the base bb module, Copyright 2003 Holger Schurig |
| 18 | |
| 19 | import copy, re, sys, traceback |
Andrew Geissler | 5199d83 | 2021-09-24 16:47:35 -0500 | [diff] [blame] | 20 | from collections.abc import MutableMapping |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 21 | import logging |
| 22 | import hashlib |
| 23 | import bb, bb.codeparser |
| 24 | from bb import utils |
| 25 | from bb.COW import COWDictBase |
| 26 | |
| 27 | logger = logging.getLogger("BitBake.Data") |
| 28 | |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 29 | __setvar_keyword__ = [":append", ":prepend", ":remove"] |
| 30 | __setvar_regexp__ = re.compile(r'(?P<base>.*?)(?P<keyword>:append|:prepend|:remove)(:(?P<add>[^A-Z]*))?$') |
| 31 | __expand_var_regexp__ = re.compile(r"\${[a-zA-Z0-9\-_+./~:]+?}") |
Patrick Williams | 7784c42 | 2022-11-17 07:29:11 -0600 | [diff] [blame] | 32 | __expand_python_regexp__ = re.compile(r"\${@(?:{.*?}|.)+?}") |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 33 | __whitespace_split__ = re.compile(r'(\s)') |
| 34 | __override_regexp__ = re.compile(r'[a-z0-9]+') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 35 | |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 36 | bitbake_renamed_vars = { |
| 37 | "BB_ENV_WHITELIST": "BB_ENV_PASSTHROUGH", |
| 38 | "BB_ENV_EXTRAWHITE": "BB_ENV_PASSTHROUGH_ADDITIONS", |
| 39 | "BB_HASHBASE_WHITELIST": "BB_BASEHASH_IGNORE_VARS", |
| 40 | "BB_HASHCONFIG_WHITELIST": "BB_HASHCONFIG_IGNORE_VARS", |
| 41 | "BB_HASHTASK_WHITELIST": "BB_TASKHASH_IGNORE_TASKS", |
| 42 | "BB_SETSCENE_ENFORCE_WHITELIST": "BB_SETSCENE_ENFORCE_IGNORE_TASKS", |
| 43 | "MULTI_PROVIDER_WHITELIST": "BB_MULTI_PROVIDER_ALLOWED", |
| 44 | "BB_STAMP_WHITELIST": "is a deprecated variable and support has been removed", |
| 45 | "BB_STAMP_POLICY": "is a deprecated variable and support has been removed", |
| 46 | } |
| 47 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 48 | def infer_caller_details(loginfo, parent = False, varval = True): |
| 49 | """Save the caller the trouble of specifying everything.""" |
| 50 | # Save effort. |
| 51 | if 'ignore' in loginfo and loginfo['ignore']: |
| 52 | return |
| 53 | # If nothing was provided, mark this as possibly unneeded. |
| 54 | if not loginfo: |
| 55 | loginfo['ignore'] = True |
| 56 | return |
| 57 | # Infer caller's likely values for variable (var) and value (value), |
| 58 | # to reduce clutter in the rest of the code. |
| 59 | above = None |
| 60 | def set_above(): |
| 61 | try: |
| 62 | raise Exception |
| 63 | except Exception: |
| 64 | tb = sys.exc_info()[2] |
| 65 | if parent: |
| 66 | return tb.tb_frame.f_back.f_back.f_back |
| 67 | else: |
| 68 | return tb.tb_frame.f_back.f_back |
| 69 | |
| 70 | if varval and ('variable' not in loginfo or 'detail' not in loginfo): |
| 71 | if not above: |
| 72 | above = set_above() |
| 73 | lcls = above.f_locals.items() |
| 74 | for k, v in lcls: |
| 75 | if k == 'value' and 'detail' not in loginfo: |
| 76 | loginfo['detail'] = v |
| 77 | if k == 'var' and 'variable' not in loginfo: |
| 78 | loginfo['variable'] = v |
| 79 | # Infer file/line/function from traceback |
| 80 | # Don't use traceback.extract_stack() since it fills the line contents which |
| 81 | # we don't need and that hits stat syscalls |
| 82 | if 'file' not in loginfo: |
| 83 | if not above: |
| 84 | above = set_above() |
| 85 | f = above.f_back |
| 86 | line = f.f_lineno |
| 87 | file = f.f_code.co_filename |
| 88 | func = f.f_code.co_name |
| 89 | loginfo['file'] = file |
| 90 | loginfo['line'] = line |
| 91 | if func not in loginfo: |
| 92 | loginfo['func'] = func |
| 93 | |
| 94 | class VariableParse: |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame^] | 95 | def __init__(self, varname, d, unexpanded_value = None, val = None): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 96 | self.varname = varname |
| 97 | self.d = d |
| 98 | self.value = val |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame^] | 99 | self.unexpanded_value = unexpanded_value |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 100 | |
| 101 | self.references = set() |
| 102 | self.execs = set() |
| 103 | self.contains = {} |
| 104 | |
| 105 | def var_sub(self, match): |
| 106 | key = match.group()[2:-1] |
| 107 | if self.varname and key: |
| 108 | if self.varname == key: |
| 109 | raise Exception("variable %s references itself!" % self.varname) |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 110 | var = self.d.getVarFlag(key, "_content") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 111 | self.references.add(key) |
| 112 | if var is not None: |
| 113 | return var |
| 114 | else: |
| 115 | return match.group() |
| 116 | |
| 117 | def python_sub(self, match): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 118 | if isinstance(match, str): |
| 119 | code = match |
| 120 | else: |
| 121 | code = match.group()[3:-1] |
| 122 | |
Patrick Williams | 7784c42 | 2022-11-17 07:29:11 -0600 | [diff] [blame] | 123 | # Do not run code that contains one or more unexpanded variables |
| 124 | # instead return the code with the characters we removed put back |
| 125 | if __expand_var_regexp__.findall(code): |
| 126 | return "${@" + code + "}" |
| 127 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 128 | if self.varname: |
| 129 | varname = 'Var <%s>' % self.varname |
| 130 | else: |
| 131 | varname = '<expansion>' |
| 132 | codeobj = compile(code.strip(), varname, "eval") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 133 | |
| 134 | parser = bb.codeparser.PythonParser(self.varname, logger) |
| 135 | parser.parse_python(code) |
| 136 | if self.varname: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 137 | vardeps = self.d.getVarFlag(self.varname, "vardeps") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 138 | if vardeps is None: |
| 139 | parser.log.flush() |
| 140 | else: |
| 141 | parser.log.flush() |
| 142 | self.references |= parser.references |
| 143 | self.execs |= parser.execs |
| 144 | |
| 145 | for k in parser.contains: |
| 146 | if k not in self.contains: |
| 147 | self.contains[k] = parser.contains[k].copy() |
| 148 | else: |
| 149 | self.contains[k].update(parser.contains[k]) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 150 | value = utils.better_eval(codeobj, DataContext(self.d), {'d' : self.d}) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 151 | return str(value) |
| 152 | |
| 153 | |
| 154 | class DataContext(dict): |
| 155 | def __init__(self, metadata, **kwargs): |
| 156 | self.metadata = metadata |
| 157 | dict.__init__(self, **kwargs) |
| 158 | self['d'] = metadata |
| 159 | |
| 160 | def __missing__(self, key): |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 161 | # Skip commonly accessed invalid variables |
| 162 | if key in ['bb', 'oe', 'int', 'bool', 'time', 'str', 'os']: |
| 163 | raise KeyError(key) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 164 | value = self.metadata.getVar(key) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 165 | if value is None or self.metadata.getVarFlag(key, 'func', False): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 166 | raise KeyError(key) |
| 167 | else: |
| 168 | return value |
| 169 | |
| 170 | class ExpansionError(Exception): |
| 171 | def __init__(self, varname, expression, exception): |
| 172 | self.expression = expression |
| 173 | self.variablename = varname |
| 174 | self.exception = exception |
Andrew Geissler | 5199d83 | 2021-09-24 16:47:35 -0500 | [diff] [blame] | 175 | self.varlist = [varname or expression or ""] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 176 | if varname: |
| 177 | if expression: |
| 178 | self.msg = "Failure expanding variable %s, expression was %s which triggered exception %s: %s" % (varname, expression, type(exception).__name__, exception) |
| 179 | else: |
| 180 | self.msg = "Failure expanding variable %s: %s: %s" % (varname, type(exception).__name__, exception) |
| 181 | else: |
| 182 | self.msg = "Failure expanding expression %s which triggered exception %s: %s" % (expression, type(exception).__name__, exception) |
| 183 | Exception.__init__(self, self.msg) |
| 184 | self.args = (varname, expression, exception) |
Andrew Geissler | 5199d83 | 2021-09-24 16:47:35 -0500 | [diff] [blame] | 185 | |
| 186 | def addVar(self, varname): |
| 187 | if varname: |
| 188 | self.varlist.append(varname) |
| 189 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 190 | def __str__(self): |
Andrew Geissler | 5199d83 | 2021-09-24 16:47:35 -0500 | [diff] [blame] | 191 | chain = "\nThe variable dependency chain for the failure is: " + " -> ".join(self.varlist) |
| 192 | return self.msg + chain |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 193 | |
| 194 | class IncludeHistory(object): |
| 195 | def __init__(self, parent = None, filename = '[TOP LEVEL]'): |
| 196 | self.parent = parent |
| 197 | self.filename = filename |
| 198 | self.children = [] |
| 199 | self.current = self |
| 200 | |
| 201 | def copy(self): |
| 202 | new = IncludeHistory(self.parent, self.filename) |
| 203 | for c in self.children: |
| 204 | new.children.append(c) |
| 205 | return new |
| 206 | |
| 207 | def include(self, filename): |
| 208 | newfile = IncludeHistory(self.current, filename) |
| 209 | self.current.children.append(newfile) |
| 210 | self.current = newfile |
| 211 | return self |
| 212 | |
| 213 | def __enter__(self): |
| 214 | pass |
| 215 | |
| 216 | def __exit__(self, a, b, c): |
| 217 | if self.current.parent: |
| 218 | self.current = self.current.parent |
| 219 | else: |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 220 | bb.warn("Include log: Tried to finish '%s' at top level." % self.filename) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 221 | return False |
| 222 | |
| 223 | def emit(self, o, level = 0): |
| 224 | """Emit an include history file, and its children.""" |
| 225 | if level: |
| 226 | spaces = " " * (level - 1) |
| 227 | o.write("# %s%s" % (spaces, self.filename)) |
| 228 | if len(self.children) > 0: |
| 229 | o.write(" includes:") |
| 230 | else: |
| 231 | o.write("#\n# INCLUDE HISTORY:\n#") |
| 232 | level = level + 1 |
| 233 | for child in self.children: |
| 234 | o.write("\n") |
| 235 | child.emit(o, level) |
| 236 | |
| 237 | class VariableHistory(object): |
| 238 | def __init__(self, dataroot): |
| 239 | self.dataroot = dataroot |
| 240 | self.variables = COWDictBase.copy() |
| 241 | |
| 242 | def copy(self): |
| 243 | new = VariableHistory(self.dataroot) |
| 244 | new.variables = self.variables.copy() |
| 245 | return new |
| 246 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 247 | def __getstate__(self): |
| 248 | vardict = {} |
| 249 | for k, v in self.variables.iteritems(): |
| 250 | vardict[k] = v |
| 251 | return {'dataroot': self.dataroot, |
| 252 | 'variables': vardict} |
| 253 | |
| 254 | def __setstate__(self, state): |
| 255 | self.dataroot = state['dataroot'] |
| 256 | self.variables = COWDictBase.copy() |
| 257 | for k, v in state['variables'].items(): |
| 258 | self.variables[k] = v |
| 259 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 260 | def record(self, *kwonly, **loginfo): |
| 261 | if not self.dataroot._tracking: |
| 262 | return |
| 263 | if len(kwonly) > 0: |
| 264 | raise TypeError |
| 265 | infer_caller_details(loginfo, parent = True) |
| 266 | if 'ignore' in loginfo and loginfo['ignore']: |
| 267 | return |
| 268 | if 'op' not in loginfo or not loginfo['op']: |
| 269 | loginfo['op'] = 'set' |
| 270 | if 'detail' in loginfo: |
| 271 | loginfo['detail'] = str(loginfo['detail']) |
| 272 | if 'variable' not in loginfo or 'file' not in loginfo: |
| 273 | raise ValueError("record() missing variable or file.") |
| 274 | var = loginfo['variable'] |
| 275 | |
| 276 | if var not in self.variables: |
| 277 | self.variables[var] = [] |
| 278 | if not isinstance(self.variables[var], list): |
| 279 | return |
| 280 | if 'nodups' in loginfo and loginfo in self.variables[var]: |
| 281 | return |
| 282 | self.variables[var].append(loginfo.copy()) |
| 283 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 284 | def rename_variable_hist(self, oldvar, newvar): |
| 285 | if not self.dataroot._tracking: |
| 286 | return |
| 287 | if oldvar not in self.variables: |
| 288 | return |
| 289 | if newvar not in self.variables: |
| 290 | self.variables[newvar] = [] |
| 291 | for i in self.variables[oldvar]: |
| 292 | self.variables[newvar].append(i.copy()) |
| 293 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 294 | def variable(self, var): |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 295 | varhistory = [] |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 296 | if var in self.variables: |
| 297 | varhistory.extend(self.variables[var]) |
| 298 | return varhistory |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 299 | |
| 300 | def emit(self, var, oval, val, o, d): |
| 301 | history = self.variable(var) |
| 302 | |
| 303 | # Append override history |
| 304 | if var in d.overridedata: |
| 305 | for (r, override) in d.overridedata[var]: |
| 306 | for event in self.variable(r): |
| 307 | loginfo = event.copy() |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 308 | if 'flag' in loginfo and not loginfo['flag'].startswith(("_", ":")): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 309 | continue |
| 310 | loginfo['variable'] = var |
| 311 | loginfo['op'] = 'override[%s]:%s' % (override, loginfo['op']) |
| 312 | history.append(loginfo) |
| 313 | |
| 314 | commentVal = re.sub('\n', '\n#', str(oval)) |
| 315 | if history: |
| 316 | if len(history) == 1: |
| 317 | o.write("#\n# $%s\n" % var) |
| 318 | else: |
| 319 | o.write("#\n# $%s [%d operations]\n" % (var, len(history))) |
| 320 | for event in history: |
| 321 | # o.write("# %s\n" % str(event)) |
| 322 | if 'func' in event: |
| 323 | # If we have a function listed, this is internal |
| 324 | # code, not an operation in a config file, and the |
| 325 | # full path is distracting. |
| 326 | event['file'] = re.sub('.*/', '', event['file']) |
| 327 | display_func = ' [%s]' % event['func'] |
| 328 | else: |
| 329 | display_func = '' |
| 330 | if 'flag' in event: |
| 331 | flag = '[%s] ' % (event['flag']) |
| 332 | else: |
| 333 | flag = '' |
| 334 | o.write("# %s %s:%s%s\n# %s\"%s\"\n" % (event['op'], event['file'], event['line'], display_func, flag, re.sub('\n', '\n# ', event['detail']))) |
| 335 | if len(history) > 1: |
| 336 | o.write("# pre-expansion value:\n") |
| 337 | o.write('# "%s"\n' % (commentVal)) |
| 338 | else: |
| 339 | o.write("#\n# $%s\n# [no history recorded]\n#\n" % var) |
| 340 | o.write('# "%s"\n' % (commentVal)) |
| 341 | |
| 342 | def get_variable_files(self, var): |
| 343 | """Get the files where operations are made on a variable""" |
| 344 | var_history = self.variable(var) |
| 345 | files = [] |
| 346 | for event in var_history: |
| 347 | files.append(event['file']) |
| 348 | return files |
| 349 | |
| 350 | def get_variable_lines(self, var, f): |
| 351 | """Get the line where a operation is made on a variable in file f""" |
| 352 | var_history = self.variable(var) |
| 353 | lines = [] |
| 354 | for event in var_history: |
| 355 | if f== event['file']: |
| 356 | line = event['line'] |
| 357 | lines.append(line) |
| 358 | return lines |
| 359 | |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 360 | def get_variable_refs(self, var): |
| 361 | """Return a dict of file/line references""" |
| 362 | var_history = self.variable(var) |
| 363 | refs = {} |
| 364 | for event in var_history: |
| 365 | if event['file'] not in refs: |
| 366 | refs[event['file']] = [] |
| 367 | refs[event['file']].append(event['line']) |
| 368 | return refs |
| 369 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 370 | def get_variable_items_files(self, var): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 371 | """ |
| 372 | Use variable history to map items added to a list variable and |
| 373 | the files in which they were added. |
| 374 | """ |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 375 | d = self.dataroot |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 376 | history = self.variable(var) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 377 | finalitems = (d.getVar(var) or '').split() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 378 | filemap = {} |
| 379 | isset = False |
| 380 | for event in history: |
| 381 | if 'flag' in event: |
| 382 | continue |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 383 | if event['op'] == ':remove': |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 384 | continue |
| 385 | if isset and event['op'] == 'set?': |
| 386 | continue |
| 387 | isset = True |
| 388 | items = d.expand(event['detail']).split() |
| 389 | for item in items: |
| 390 | # This is a little crude but is belt-and-braces to avoid us |
| 391 | # having to handle every possible operation type specifically |
| 392 | if item in finalitems and not item in filemap: |
| 393 | filemap[item] = event['file'] |
| 394 | return filemap |
| 395 | |
| 396 | def del_var_history(self, var, f=None, line=None): |
| 397 | """If file f and line are not given, the entire history of var is deleted""" |
| 398 | if var in self.variables: |
| 399 | if f and line: |
| 400 | self.variables[var] = [ x for x in self.variables[var] if x['file']!=f and x['line']!=line] |
| 401 | else: |
| 402 | self.variables[var] = [] |
| 403 | |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 404 | def _print_rename_error(var, loginfo, renamedvars, fullvar=None): |
| 405 | info = "" |
| 406 | if "file" in loginfo: |
| 407 | info = " file: %s" % loginfo["file"] |
| 408 | if "line" in loginfo: |
| 409 | info += " line: %s" % loginfo["line"] |
| 410 | if fullvar and fullvar != var: |
| 411 | info += " referenced as: %s" % fullvar |
| 412 | if info: |
| 413 | info = " (%s)" % info.strip() |
| 414 | renameinfo = renamedvars[var] |
| 415 | if " " in renameinfo: |
| 416 | # A space signals a string to display instead of a rename |
| 417 | bb.erroronce('Variable %s %s%s' % (var, renameinfo, info)) |
| 418 | else: |
| 419 | bb.erroronce('Variable %s has been renamed to %s%s' % (var, renameinfo, info)) |
| 420 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 421 | class DataSmart(MutableMapping): |
| 422 | def __init__(self): |
| 423 | self.dict = {} |
| 424 | |
| 425 | self.inchistory = IncludeHistory() |
| 426 | self.varhistory = VariableHistory(self) |
| 427 | self._tracking = False |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 428 | self._var_renames = {} |
| 429 | self._var_renames.update(bitbake_renamed_vars) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 430 | |
| 431 | self.expand_cache = {} |
| 432 | |
| 433 | # cookie monster tribute |
| 434 | # Need to be careful about writes to overridedata as |
| 435 | # its only a shallow copy, could influence other data store |
| 436 | # copies! |
| 437 | self.overridedata = {} |
| 438 | self.overrides = None |
| 439 | self.overridevars = set(["OVERRIDES", "FILE"]) |
| 440 | self.inoverride = False |
| 441 | |
| 442 | def enableTracking(self): |
| 443 | self._tracking = True |
| 444 | |
| 445 | def disableTracking(self): |
| 446 | self._tracking = False |
| 447 | |
| 448 | def expandWithRefs(self, s, varname): |
| 449 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 450 | if not isinstance(s, str): # sanity check |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame^] | 451 | return VariableParse(varname, self, s, s) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 452 | |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame^] | 453 | varparse = VariableParse(varname, self, s) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 454 | |
| 455 | while s.find('${') != -1: |
| 456 | olds = s |
| 457 | try: |
| 458 | s = __expand_var_regexp__.sub(varparse.var_sub, s) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 459 | try: |
| 460 | s = __expand_python_regexp__.sub(varparse.python_sub, s) |
| 461 | except SyntaxError as e: |
| 462 | # Likely unmatched brackets, just don't expand the expression |
Andrew Geissler | 5199d83 | 2021-09-24 16:47:35 -0500 | [diff] [blame] | 463 | if e.msg != "EOL while scanning string literal" and not e.msg.startswith("unterminated string literal"): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 464 | raise |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 465 | if s == olds: |
| 466 | break |
Andrew Geissler | 5199d83 | 2021-09-24 16:47:35 -0500 | [diff] [blame] | 467 | except ExpansionError as e: |
| 468 | e.addVar(varname) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 469 | raise |
| 470 | except bb.parse.SkipRecipe: |
| 471 | raise |
Andrew Geissler | 5199d83 | 2021-09-24 16:47:35 -0500 | [diff] [blame] | 472 | except bb.BBHandledException: |
| 473 | raise |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 474 | except Exception as exc: |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 475 | tb = sys.exc_info()[2] |
| 476 | raise ExpansionError(varname, s, exc).with_traceback(tb) from exc |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 477 | |
| 478 | varparse.value = s |
| 479 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 480 | return varparse |
| 481 | |
| 482 | def expand(self, s, varname = None): |
| 483 | return self.expandWithRefs(s, varname).value |
| 484 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 485 | def need_overrides(self): |
Patrick Williams | d7e9631 | 2015-09-22 08:09:05 -0500 | [diff] [blame] | 486 | if self.overrides is not None: |
| 487 | return |
| 488 | if self.inoverride: |
| 489 | return |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame^] | 490 | overrride_stack = [] |
Patrick Williams | d7e9631 | 2015-09-22 08:09:05 -0500 | [diff] [blame] | 491 | for count in range(5): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 492 | self.inoverride = True |
| 493 | # Can end up here recursively so setup dummy values |
| 494 | self.overrides = [] |
| 495 | self.overridesset = set() |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 496 | self.overrides = (self.getVar("OVERRIDES") or "").split(":") or [] |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame^] | 497 | overrride_stack.append(self.overrides) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 498 | self.overridesset = set(self.overrides) |
| 499 | self.inoverride = False |
| 500 | self.expand_cache = {} |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 501 | newoverrides = (self.getVar("OVERRIDES") or "").split(":") or [] |
Patrick Williams | d7e9631 | 2015-09-22 08:09:05 -0500 | [diff] [blame] | 502 | if newoverrides == self.overrides: |
| 503 | break |
| 504 | self.overrides = newoverrides |
| 505 | self.overridesset = set(self.overrides) |
| 506 | else: |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame^] | 507 | bb.fatal("Overrides could not be expanded into a stable state after 5 iterations, overrides must be being referenced by other overridden variables in some recursive fashion. Please provide your configuration to bitbake-devel so we can laugh, er, I mean try and understand how to make it work. The list of failing override expansions: %s" % "\n".join(str(s) for s in overrride_stack)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 508 | |
| 509 | def initVar(self, var): |
| 510 | self.expand_cache = {} |
| 511 | if not var in self.dict: |
| 512 | self.dict[var] = {} |
| 513 | |
| 514 | def _findVar(self, var): |
| 515 | dest = self.dict |
| 516 | while dest: |
| 517 | if var in dest: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 518 | return dest[var], self.overridedata.get(var, None) |
| 519 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 520 | if "_data" not in dest: |
| 521 | break |
| 522 | dest = dest["_data"] |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 523 | return None, self.overridedata.get(var, None) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 524 | |
| 525 | def _makeShadowCopy(self, var): |
| 526 | if var in self.dict: |
| 527 | return |
| 528 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 529 | local_var, _ = self._findVar(var) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 530 | |
| 531 | if local_var: |
| 532 | self.dict[var] = copy.copy(local_var) |
| 533 | else: |
| 534 | self.initVar(var) |
| 535 | |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 536 | def hasOverrides(self, var): |
| 537 | return var in self.overridedata |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 538 | |
| 539 | def setVar(self, var, value, **loginfo): |
| 540 | #print("var=" + str(var) + " val=" + str(value)) |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 541 | |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 542 | if not var.startswith("__anon_") and ("_append" in var or "_prepend" in var or "_remove" in var): |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 543 | info = "%s" % var |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 544 | if "file" in loginfo: |
| 545 | info += " file: %s" % loginfo["file"] |
| 546 | if "line" in loginfo: |
| 547 | info += " line: %s" % loginfo["line"] |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 548 | bb.fatal("Variable %s contains an operation using the old override syntax. Please convert this layer/metadata before attempting to use with a newer bitbake." % info) |
| 549 | |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 550 | shortvar = var.split(":", 1)[0] |
| 551 | if shortvar in self._var_renames: |
| 552 | _print_rename_error(shortvar, loginfo, self._var_renames, fullvar=var) |
| 553 | # Mark that we have seen a renamed variable |
| 554 | self.setVar("_FAILPARSINGERRORHANDLED", True) |
| 555 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 556 | self.expand_cache = {} |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 557 | parsing=False |
| 558 | if 'parsing' in loginfo: |
| 559 | parsing=True |
| 560 | |
| 561 | if 'op' not in loginfo: |
| 562 | loginfo['op'] = "set" |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 563 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 564 | match = __setvar_regexp__.match(var) |
| 565 | if match and match.group("keyword") in __setvar_keyword__: |
| 566 | base = match.group('base') |
| 567 | keyword = match.group("keyword") |
| 568 | override = match.group('add') |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 569 | l = self.getVarFlag(base, keyword, False) or [] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 570 | l.append([value, override]) |
| 571 | self.setVarFlag(base, keyword, l, ignore=True) |
| 572 | # And cause that to be recorded: |
| 573 | loginfo['detail'] = value |
| 574 | loginfo['variable'] = base |
| 575 | if override: |
| 576 | loginfo['op'] = '%s[%s]' % (keyword, override) |
| 577 | else: |
| 578 | loginfo['op'] = keyword |
| 579 | self.varhistory.record(**loginfo) |
| 580 | # todo make sure keyword is not __doc__ or __module__ |
| 581 | # pay the cookie monster |
| 582 | |
| 583 | # more cookies for the cookie monster |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 584 | if ':' in var: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 585 | self._setvar_update_overrides(base, **loginfo) |
| 586 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 587 | if base in self.overridevars: |
Patrick Williams | d7e9631 | 2015-09-22 08:09:05 -0500 | [diff] [blame] | 588 | self._setvar_update_overridevars(var, value) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 589 | return |
| 590 | |
| 591 | if not var in self.dict: |
| 592 | self._makeShadowCopy(var) |
| 593 | |
| 594 | if not parsing: |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 595 | if ":append" in self.dict[var]: |
| 596 | del self.dict[var][":append"] |
| 597 | if ":prepend" in self.dict[var]: |
| 598 | del self.dict[var][":prepend"] |
| 599 | if ":remove" in self.dict[var]: |
| 600 | del self.dict[var][":remove"] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 601 | if var in self.overridedata: |
| 602 | active = [] |
| 603 | self.need_overrides() |
| 604 | for (r, o) in self.overridedata[var]: |
| 605 | if o in self.overridesset: |
| 606 | active.append(r) |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 607 | elif ":" in o: |
| 608 | if set(o.split(":")).issubset(self.overridesset): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 609 | active.append(r) |
| 610 | for a in active: |
| 611 | self.delVar(a) |
| 612 | del self.overridedata[var] |
| 613 | |
| 614 | # more cookies for the cookie monster |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 615 | if ':' in var: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 616 | self._setvar_update_overrides(var, **loginfo) |
| 617 | |
| 618 | # setting var |
| 619 | self.dict[var]["_content"] = value |
| 620 | self.varhistory.record(**loginfo) |
| 621 | |
| 622 | if var in self.overridevars: |
Patrick Williams | d7e9631 | 2015-09-22 08:09:05 -0500 | [diff] [blame] | 623 | self._setvar_update_overridevars(var, value) |
| 624 | |
| 625 | def _setvar_update_overridevars(self, var, value): |
| 626 | vardata = self.expandWithRefs(value, var) |
| 627 | new = vardata.references |
| 628 | new.update(vardata.contains.keys()) |
| 629 | while not new.issubset(self.overridevars): |
| 630 | nextnew = set() |
| 631 | self.overridevars.update(new) |
| 632 | for i in new: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 633 | vardata = self.expandWithRefs(self.getVar(i), i) |
Patrick Williams | d7e9631 | 2015-09-22 08:09:05 -0500 | [diff] [blame] | 634 | nextnew.update(vardata.references) |
| 635 | nextnew.update(vardata.contains.keys()) |
| 636 | new = nextnew |
Patrick Williams | 7784c42 | 2022-11-17 07:29:11 -0600 | [diff] [blame] | 637 | self.overrides = None |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 638 | |
| 639 | def _setvar_update_overrides(self, var, **loginfo): |
| 640 | # aka pay the cookie monster |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 641 | override = var[var.rfind(':')+1:] |
| 642 | shortvar = var[:var.rfind(':')] |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 643 | while override and __override_regexp__.match(override): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 644 | if shortvar not in self.overridedata: |
| 645 | self.overridedata[shortvar] = [] |
| 646 | if [var, override] not in self.overridedata[shortvar]: |
| 647 | # Force CoW by recreating the list first |
| 648 | self.overridedata[shortvar] = list(self.overridedata[shortvar]) |
| 649 | self.overridedata[shortvar].append([var, override]) |
| 650 | override = None |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 651 | if ":" in shortvar: |
| 652 | override = var[shortvar.rfind(':')+1:] |
| 653 | shortvar = var[:shortvar.rfind(':')] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 654 | if len(shortvar) == 0: |
| 655 | override = None |
| 656 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 657 | def getVar(self, var, expand=True, noweakdefault=False, parsing=False): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 658 | return self.getVarFlag(var, "_content", expand, noweakdefault, parsing) |
| 659 | |
| 660 | def renameVar(self, key, newkey, **loginfo): |
| 661 | """ |
| 662 | Rename the variable key to newkey |
| 663 | """ |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 664 | if key == newkey: |
| 665 | bb.warn("Calling renameVar with equivalent keys (%s) is invalid" % key) |
| 666 | return |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 667 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 668 | val = self.getVar(key, 0, parsing=True) |
| 669 | if val is not None: |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 670 | self.varhistory.rename_variable_hist(key, newkey) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 671 | loginfo['variable'] = newkey |
| 672 | loginfo['op'] = 'rename from %s' % key |
| 673 | loginfo['detail'] = val |
| 674 | self.varhistory.record(**loginfo) |
| 675 | self.setVar(newkey, val, ignore=True, parsing=True) |
| 676 | |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 677 | srcflags = self.getVarFlags(key, False, True) or {} |
| 678 | for i in srcflags: |
| 679 | if i not in (__setvar_keyword__): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 680 | continue |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 681 | src = srcflags[i] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 682 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 683 | dest = self.getVarFlag(newkey, i, False) or [] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 684 | dest.extend(src) |
| 685 | self.setVarFlag(newkey, i, dest, ignore=True) |
| 686 | |
| 687 | if key in self.overridedata: |
| 688 | self.overridedata[newkey] = [] |
| 689 | for (v, o) in self.overridedata[key]: |
| 690 | self.overridedata[newkey].append([v.replace(key, newkey), o]) |
| 691 | self.renameVar(v, v.replace(key, newkey)) |
| 692 | |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 693 | if ':' in newkey and val is None: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 694 | self._setvar_update_overrides(newkey, **loginfo) |
| 695 | |
| 696 | loginfo['variable'] = key |
| 697 | loginfo['op'] = 'rename (to)' |
| 698 | loginfo['detail'] = newkey |
| 699 | self.varhistory.record(**loginfo) |
| 700 | self.delVar(key, ignore=True) |
| 701 | |
| 702 | def appendVar(self, var, value, **loginfo): |
| 703 | loginfo['op'] = 'append' |
| 704 | self.varhistory.record(**loginfo) |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 705 | self.setVar(var + ":append", value, ignore=True, parsing=True) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 706 | |
| 707 | def prependVar(self, var, value, **loginfo): |
| 708 | loginfo['op'] = 'prepend' |
| 709 | self.varhistory.record(**loginfo) |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 710 | self.setVar(var + ":prepend", value, ignore=True, parsing=True) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 711 | |
| 712 | def delVar(self, var, **loginfo): |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 713 | self.expand_cache = {} |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 714 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 715 | loginfo['detail'] = "" |
| 716 | loginfo['op'] = 'del' |
| 717 | self.varhistory.record(**loginfo) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 718 | self.dict[var] = {} |
| 719 | if var in self.overridedata: |
| 720 | del self.overridedata[var] |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 721 | if ':' in var: |
| 722 | override = var[var.rfind(':')+1:] |
| 723 | shortvar = var[:var.rfind(':')] |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame^] | 724 | while override and __override_regexp__.match(override): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 725 | try: |
| 726 | if shortvar in self.overridedata: |
| 727 | # Force CoW by recreating the list first |
| 728 | self.overridedata[shortvar] = list(self.overridedata[shortvar]) |
| 729 | self.overridedata[shortvar].remove([var, override]) |
| 730 | except ValueError as e: |
| 731 | pass |
| 732 | override = None |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 733 | if ":" in shortvar: |
| 734 | override = var[shortvar.rfind(':')+1:] |
| 735 | shortvar = var[:shortvar.rfind(':')] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 736 | if len(shortvar) == 0: |
| 737 | override = None |
| 738 | |
| 739 | def setVarFlag(self, var, flag, value, **loginfo): |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 740 | self.expand_cache = {} |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 741 | |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 742 | if var == "BB_RENAMED_VARIABLES": |
| 743 | self._var_renames[flag] = value |
| 744 | |
| 745 | if var in self._var_renames: |
| 746 | _print_rename_error(var, loginfo, self._var_renames) |
| 747 | # Mark that we have seen a renamed variable |
| 748 | self.setVar("_FAILPARSINGERRORHANDLED", True) |
| 749 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 750 | if 'op' not in loginfo: |
| 751 | loginfo['op'] = "set" |
| 752 | loginfo['flag'] = flag |
| 753 | self.varhistory.record(**loginfo) |
| 754 | if not var in self.dict: |
| 755 | self._makeShadowCopy(var) |
| 756 | self.dict[var][flag] = value |
| 757 | |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 758 | if flag == "_defaultval" and ':' in var: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 759 | self._setvar_update_overrides(var, **loginfo) |
Patrick Williams | d7e9631 | 2015-09-22 08:09:05 -0500 | [diff] [blame] | 760 | if flag == "_defaultval" and var in self.overridevars: |
| 761 | self._setvar_update_overridevars(var, value) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 762 | |
| 763 | if flag == "unexport" or flag == "export": |
| 764 | if not "__exportlist" in self.dict: |
| 765 | self._makeShadowCopy("__exportlist") |
| 766 | if not "_content" in self.dict["__exportlist"]: |
| 767 | self.dict["__exportlist"]["_content"] = set() |
| 768 | self.dict["__exportlist"]["_content"].add(var) |
| 769 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 770 | def getVarFlag(self, var, flag, expand=True, noweakdefault=False, parsing=False, retparser=False): |
| 771 | if flag == "_content": |
| 772 | cachename = var |
| 773 | else: |
| 774 | if not flag: |
| 775 | bb.warn("Calling getVarFlag with flag unset is invalid") |
| 776 | return None |
| 777 | cachename = var + "[" + flag + "]" |
| 778 | |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame^] | 779 | if not expand and retparser and cachename in self.expand_cache: |
| 780 | return self.expand_cache[cachename].unexpanded_value, self.expand_cache[cachename] |
| 781 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 782 | if expand and cachename in self.expand_cache: |
| 783 | return self.expand_cache[cachename].value |
| 784 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 785 | local_var, overridedata = self._findVar(var) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 786 | value = None |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 787 | removes = set() |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 788 | if flag == "_content" and overridedata is not None and not parsing: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 789 | match = False |
| 790 | active = {} |
| 791 | self.need_overrides() |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 792 | for (r, o) in overridedata: |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 793 | # FIXME What about double overrides both with "_" in the name? |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 794 | if o in self.overridesset: |
| 795 | active[o] = r |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 796 | elif ":" in o: |
| 797 | if set(o.split(":")).issubset(self.overridesset): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 798 | active[o] = r |
| 799 | |
| 800 | mod = True |
| 801 | while mod: |
| 802 | mod = False |
| 803 | for o in self.overrides: |
| 804 | for a in active.copy(): |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 805 | if a.endswith(":" + o): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 806 | t = active[a] |
| 807 | del active[a] |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 808 | active[a.replace(":" + o, "")] = t |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 809 | mod = True |
| 810 | elif a == o: |
| 811 | match = active[a] |
| 812 | del active[a] |
| 813 | if match: |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 814 | value, subparser = self.getVarFlag(match, "_content", False, retparser=True) |
| 815 | if hasattr(subparser, "removes"): |
| 816 | # We have to carry the removes from the overridden variable to apply at the |
| 817 | # end of processing |
| 818 | removes = subparser.removes |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 819 | |
| 820 | if local_var is not None and value is None: |
| 821 | if flag in local_var: |
| 822 | value = copy.copy(local_var[flag]) |
| 823 | elif flag == "_content" and "_defaultval" in local_var and not noweakdefault: |
| 824 | value = copy.copy(local_var["_defaultval"]) |
| 825 | |
| 826 | |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 827 | if flag == "_content" and local_var is not None and ":append" in local_var and not parsing: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 828 | self.need_overrides() |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 829 | for (r, o) in local_var[":append"]: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 830 | match = True |
| 831 | if o: |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 832 | for o2 in o.split(":"): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 833 | if not o2 in self.overrides: |
| 834 | match = False |
| 835 | if match: |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 836 | if value is None: |
| 837 | value = "" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 838 | value = value + r |
| 839 | |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 840 | if flag == "_content" and local_var is not None and ":prepend" in local_var and not parsing: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 841 | self.need_overrides() |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 842 | for (r, o) in local_var[":prepend"]: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 843 | |
| 844 | match = True |
| 845 | if o: |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 846 | for o2 in o.split(":"): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 847 | if not o2 in self.overrides: |
| 848 | match = False |
| 849 | if match: |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 850 | if value is None: |
| 851 | value = "" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 852 | value = r + value |
| 853 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 854 | parser = None |
| 855 | if expand or retparser: |
| 856 | parser = self.expandWithRefs(value, cachename) |
| 857 | if expand: |
| 858 | value = parser.value |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 859 | |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 860 | if value and flag == "_content" and local_var is not None and ":remove" in local_var and not parsing: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 861 | self.need_overrides() |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 862 | for (r, o) in local_var[":remove"]: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 863 | match = True |
| 864 | if o: |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 865 | for o2 in o.split(":"): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 866 | if not o2 in self.overrides: |
| 867 | match = False |
| 868 | if match: |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 869 | removes.add(r) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 870 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 871 | if value and flag == "_content" and not parsing: |
| 872 | if removes and parser: |
| 873 | expanded_removes = {} |
| 874 | for r in removes: |
| 875 | expanded_removes[r] = self.expand(r).split() |
| 876 | |
| 877 | parser.removes = set() |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 878 | val = [] |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 879 | for v in __whitespace_split__.split(parser.value): |
| 880 | skip = False |
| 881 | for r in removes: |
| 882 | if v in expanded_removes[r]: |
| 883 | parser.removes.add(r) |
| 884 | skip = True |
| 885 | if skip: |
| 886 | continue |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 887 | val.append(v) |
| 888 | parser.value = "".join(val) |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 889 | if expand: |
| 890 | value = parser.value |
| 891 | |
| 892 | if parser: |
| 893 | self.expand_cache[cachename] = parser |
| 894 | |
| 895 | if retparser: |
| 896 | return value, parser |
| 897 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 898 | return value |
| 899 | |
| 900 | def delVarFlag(self, var, flag, **loginfo): |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 901 | self.expand_cache = {} |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 902 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 903 | local_var, _ = self._findVar(var) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 904 | if not local_var: |
| 905 | return |
| 906 | if not var in self.dict: |
| 907 | self._makeShadowCopy(var) |
| 908 | |
| 909 | if var in self.dict and flag in self.dict[var]: |
| 910 | loginfo['detail'] = "" |
| 911 | loginfo['op'] = 'delFlag' |
| 912 | loginfo['flag'] = flag |
| 913 | self.varhistory.record(**loginfo) |
| 914 | |
| 915 | del self.dict[var][flag] |
| 916 | |
| 917 | def appendVarFlag(self, var, flag, value, **loginfo): |
| 918 | loginfo['op'] = 'append' |
| 919 | loginfo['flag'] = flag |
| 920 | self.varhistory.record(**loginfo) |
| 921 | newvalue = (self.getVarFlag(var, flag, False) or "") + value |
| 922 | self.setVarFlag(var, flag, newvalue, ignore=True) |
| 923 | |
| 924 | def prependVarFlag(self, var, flag, value, **loginfo): |
| 925 | loginfo['op'] = 'prepend' |
| 926 | loginfo['flag'] = flag |
| 927 | self.varhistory.record(**loginfo) |
| 928 | newvalue = value + (self.getVarFlag(var, flag, False) or "") |
| 929 | self.setVarFlag(var, flag, newvalue, ignore=True) |
| 930 | |
| 931 | def setVarFlags(self, var, flags, **loginfo): |
| 932 | self.expand_cache = {} |
| 933 | infer_caller_details(loginfo) |
| 934 | if not var in self.dict: |
| 935 | self._makeShadowCopy(var) |
| 936 | |
| 937 | for i in flags: |
| 938 | if i == "_content": |
| 939 | continue |
| 940 | loginfo['flag'] = i |
| 941 | loginfo['detail'] = flags[i] |
| 942 | self.varhistory.record(**loginfo) |
| 943 | self.dict[var][i] = flags[i] |
| 944 | |
| 945 | def getVarFlags(self, var, expand = False, internalflags=False): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 946 | local_var, _ = self._findVar(var) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 947 | flags = {} |
| 948 | |
| 949 | if local_var: |
| 950 | for i in local_var: |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 951 | if i.startswith(("_", ":")) and not internalflags: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 952 | continue |
| 953 | flags[i] = local_var[i] |
| 954 | if expand and i in expand: |
| 955 | flags[i] = self.expand(flags[i], var + "[" + i + "]") |
| 956 | if len(flags) == 0: |
| 957 | return None |
| 958 | return flags |
| 959 | |
| 960 | |
| 961 | def delVarFlags(self, var, **loginfo): |
| 962 | self.expand_cache = {} |
| 963 | if not var in self.dict: |
| 964 | self._makeShadowCopy(var) |
| 965 | |
| 966 | if var in self.dict: |
| 967 | content = None |
| 968 | |
| 969 | loginfo['op'] = 'delete flags' |
| 970 | self.varhistory.record(**loginfo) |
| 971 | |
| 972 | # try to save the content |
| 973 | if "_content" in self.dict[var]: |
| 974 | content = self.dict[var]["_content"] |
| 975 | self.dict[var] = {} |
| 976 | self.dict[var]["_content"] = content |
| 977 | else: |
| 978 | del self.dict[var] |
| 979 | |
| 980 | def createCopy(self): |
| 981 | """ |
| 982 | Create a copy of self by setting _data to self |
| 983 | """ |
| 984 | # we really want this to be a DataSmart... |
| 985 | data = DataSmart() |
| 986 | data.dict["_data"] = self.dict |
| 987 | data.varhistory = self.varhistory.copy() |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 988 | data.varhistory.dataroot = data |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 989 | data.inchistory = self.inchistory.copy() |
| 990 | |
| 991 | data._tracking = self._tracking |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 992 | data._var_renames = self._var_renames |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 993 | |
| 994 | data.overrides = None |
| 995 | data.overridevars = copy.copy(self.overridevars) |
| 996 | # Should really be a deepcopy but has heavy overhead. |
| 997 | # Instead, we're careful with writes. |
| 998 | data.overridedata = copy.copy(self.overridedata) |
| 999 | |
| 1000 | return data |
| 1001 | |
| 1002 | def expandVarref(self, variable, parents=False): |
| 1003 | """Find all references to variable in the data and expand it |
| 1004 | in place, optionally descending to parent datastores.""" |
| 1005 | |
| 1006 | if parents: |
| 1007 | keys = iter(self) |
| 1008 | else: |
| 1009 | keys = self.localkeys() |
| 1010 | |
| 1011 | ref = '${%s}' % variable |
| 1012 | value = self.getVar(variable, False) |
| 1013 | for key in keys: |
| 1014 | referrervalue = self.getVar(key, False) |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 1015 | if referrervalue and isinstance(referrervalue, str) and ref in referrervalue: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1016 | self.setVar(key, referrervalue.replace(ref, value)) |
| 1017 | |
| 1018 | def localkeys(self): |
| 1019 | for key in self.dict: |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1020 | if key not in ['_data']: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1021 | yield key |
| 1022 | |
| 1023 | def __iter__(self): |
| 1024 | deleted = set() |
| 1025 | overrides = set() |
| 1026 | def keylist(d): |
| 1027 | klist = set() |
| 1028 | for key in d: |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1029 | if key in ["_data"]: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1030 | continue |
| 1031 | if key in deleted: |
| 1032 | continue |
| 1033 | if key in overrides: |
| 1034 | continue |
| 1035 | if not d[key]: |
| 1036 | deleted.add(key) |
| 1037 | continue |
| 1038 | klist.add(key) |
| 1039 | |
| 1040 | if "_data" in d: |
| 1041 | klist |= keylist(d["_data"]) |
| 1042 | |
| 1043 | return klist |
| 1044 | |
| 1045 | self.need_overrides() |
| 1046 | for var in self.overridedata: |
| 1047 | for (r, o) in self.overridedata[var]: |
| 1048 | if o in self.overridesset: |
| 1049 | overrides.add(var) |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 1050 | elif ":" in o: |
| 1051 | if set(o.split(":")).issubset(self.overridesset): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1052 | overrides.add(var) |
| 1053 | |
| 1054 | for k in keylist(self.dict): |
| 1055 | yield k |
| 1056 | |
| 1057 | for k in overrides: |
| 1058 | yield k |
| 1059 | |
| 1060 | def __len__(self): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1061 | return len(frozenset(iter(self))) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1062 | |
| 1063 | def __getitem__(self, item): |
| 1064 | value = self.getVar(item, False) |
| 1065 | if value is None: |
| 1066 | raise KeyError(item) |
| 1067 | else: |
| 1068 | return value |
| 1069 | |
| 1070 | def __setitem__(self, var, value): |
| 1071 | self.setVar(var, value) |
| 1072 | |
| 1073 | def __delitem__(self, var): |
| 1074 | self.delVar(var) |
| 1075 | |
| 1076 | def get_hash(self): |
| 1077 | data = {} |
| 1078 | d = self.createCopy() |
| 1079 | bb.data.expandKeys(d) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1080 | |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 1081 | config_ignore_vars = set((d.getVar("BB_HASHCONFIG_IGNORE_VARS") or "").split()) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1082 | keys = set(key for key in iter(d) if not key.startswith("__")) |
| 1083 | for key in keys: |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 1084 | if key in config_ignore_vars: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1085 | continue |
| 1086 | |
| 1087 | value = d.getVar(key, False) or "" |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1088 | if type(value) is type(self): |
| 1089 | data.update({key:value.get_hash()}) |
| 1090 | else: |
| 1091 | data.update({key:value}) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1092 | |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 1093 | varflags = d.getVarFlags(key, internalflags = True, expand=["vardepvalue"]) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1094 | if not varflags: |
| 1095 | continue |
| 1096 | for f in varflags: |
| 1097 | if f == "_content": |
| 1098 | continue |
| 1099 | data.update({'%s[%s]' % (key, f):varflags[f]}) |
| 1100 | |
| 1101 | for key in ["__BBTASKS", "__BBANONFUNCS", "__BBHANDLERS"]: |
| 1102 | bb_list = d.getVar(key, False) or [] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1103 | data.update({key:str(bb_list)}) |
| 1104 | |
| 1105 | if key == "__BBANONFUNCS": |
| 1106 | for i in bb_list: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 1107 | value = d.getVar(i, False) or "" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1108 | data.update({i:value}) |
| 1109 | |
| 1110 | data_str = str([(k, data[k]) for k in sorted(data.keys())]) |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 1111 | return hashlib.sha256(data_str.encode("utf-8")).hexdigest() |