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