blob: e2c93597e53f52f269e1d7758a2ad3b4d8195801 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001"""
2BitBake Smart Dictionary Implementation
3
4Functions for interacting with the data structure used by the
5BitBake 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 Bishopc342db32019-05-15 21:57:59 -040015# SPDX-License-Identifier: GPL-2.0-only
Patrick Williamsc124f4f2015-09-15 14:41:29 -050016#
Patrick Williamsc124f4f2015-09-15 14:41:29 -050017# Based on functions from the base bb module, Copyright 2003 Holger Schurig
18
19import copy, re, sys, traceback
Andrew Geissler5199d832021-09-24 16:47:35 -050020from collections.abc import MutableMapping
Patrick Williamsc124f4f2015-09-15 14:41:29 -050021import logging
22import hashlib
23import bb, bb.codeparser
24from bb import utils
25from bb.COW import COWDictBase
26
27logger = logging.getLogger("BitBake.Data")
28
Patrick Williams213cb262021-08-07 19:21:33 -050029__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 Williams7784c422022-11-17 07:29:11 -060032__expand_python_regexp__ = re.compile(r"\${@(?:{.*?}|.)+?}")
Brad Bishop19323692019-04-05 15:28:33 -040033__whitespace_split__ = re.compile(r'(\s)')
34__override_regexp__ = re.compile(r'[a-z0-9]+')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050035
Andrew Geissler7e0e3c02022-02-25 20:34:39 +000036bitbake_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 Williamsc124f4f2015-09-15 14:41:29 -050048def 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
94class VariableParse:
Andrew Geissler517393d2023-01-13 08:55:19 -060095 def __init__(self, varname, d, unexpanded_value = None, val = None):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050096 self.varname = varname
97 self.d = d
98 self.value = val
Andrew Geissler517393d2023-01-13 08:55:19 -060099 self.unexpanded_value = unexpanded_value
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500100
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 Bishop1a4b7ee2018-12-16 17:11:34 -0800110 var = self.d.getVarFlag(key, "_content")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500111 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 Bishop6e60e8b2018-02-01 10:27:11 -0500118 if isinstance(match, str):
119 code = match
120 else:
121 code = match.group()[3:-1]
122
Patrick Williams7784c422022-11-17 07:29:11 -0600123 # 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 Bishop19323692019-04-05 15:28:33 -0400128 if self.varname:
129 varname = 'Var <%s>' % self.varname
130 else:
131 varname = '<expansion>'
132 codeobj = compile(code.strip(), varname, "eval")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500133
134 parser = bb.codeparser.PythonParser(self.varname, logger)
135 parser.parse_python(code)
136 if self.varname:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500137 vardeps = self.d.getVarFlag(self.varname, "vardeps")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500138 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 Williamsc0f7c042017-02-23 20:41:17 -0600150 value = utils.better_eval(codeobj, DataContext(self.d), {'d' : self.d})
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500151 return str(value)
152
153
154class 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 Geissler9aee5002022-03-30 16:27:02 +0000161 # Skip commonly accessed invalid variables
162 if key in ['bb', 'oe', 'int', 'bool', 'time', 'str', 'os']:
163 raise KeyError(key)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500164 value = self.metadata.getVar(key)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500165 if value is None or self.metadata.getVarFlag(key, 'func', False):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500166 raise KeyError(key)
167 else:
168 return value
169
170class ExpansionError(Exception):
171 def __init__(self, varname, expression, exception):
172 self.expression = expression
173 self.variablename = varname
174 self.exception = exception
Andrew Geissler5199d832021-09-24 16:47:35 -0500175 self.varlist = [varname or expression or ""]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500176 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 Geissler5199d832021-09-24 16:47:35 -0500185
186 def addVar(self, varname):
187 if varname:
188 self.varlist.append(varname)
189
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500190 def __str__(self):
Andrew Geissler5199d832021-09-24 16:47:35 -0500191 chain = "\nThe variable dependency chain for the failure is: " + " -> ".join(self.varlist)
192 return self.msg + chain
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500193
194class 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 Geisslerc9f78652020-09-18 14:11:35 -0500220 bb.warn("Include log: Tried to finish '%s' at top level." % self.filename)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500221 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
237class 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 Bishop6e60e8b2018-02-01 10:27:11 -0500247 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 Williamsc124f4f2015-09-15 14:41:29 -0500260 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 Bishop1a4b7ee2018-12-16 17:11:34 -0800284 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 Williamsc124f4f2015-09-15 14:41:29 -0500294 def variable(self, var):
Andrew Geissler82c905d2020-04-13 13:39:40 -0500295 varhistory = []
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500296 if var in self.variables:
297 varhistory.extend(self.variables[var])
298 return varhistory
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500299
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 Williams213cb262021-08-07 19:21:33 -0500308 if 'flag' in loginfo and not loginfo['flag'].startswith(("_", ":")):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500309 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 Geissler7e0e3c02022-02-25 20:34:39 +0000360 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 Geissler82c905d2020-04-13 13:39:40 -0500370 def get_variable_items_files(self, var):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500371 """
372 Use variable history to map items added to a list variable and
373 the files in which they were added.
374 """
Andrew Geissler82c905d2020-04-13 13:39:40 -0500375 d = self.dataroot
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500376 history = self.variable(var)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500377 finalitems = (d.getVar(var) or '').split()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500378 filemap = {}
379 isset = False
380 for event in history:
381 if 'flag' in event:
382 continue
Patrick Williams213cb262021-08-07 19:21:33 -0500383 if event['op'] == ':remove':
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500384 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 Geissler7e0e3c02022-02-25 20:34:39 +0000404def _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 Williamsc124f4f2015-09-15 14:41:29 -0500421class DataSmart(MutableMapping):
422 def __init__(self):
423 self.dict = {}
424
425 self.inchistory = IncludeHistory()
426 self.varhistory = VariableHistory(self)
427 self._tracking = False
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000428 self._var_renames = {}
429 self._var_renames.update(bitbake_renamed_vars)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500430
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 Williamsc0f7c042017-02-23 20:41:17 -0600450 if not isinstance(s, str): # sanity check
Andrew Geissler517393d2023-01-13 08:55:19 -0600451 return VariableParse(varname, self, s, s)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500452
Andrew Geissler517393d2023-01-13 08:55:19 -0600453 varparse = VariableParse(varname, self, s)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500454
455 while s.find('${') != -1:
456 olds = s
457 try:
458 s = __expand_var_regexp__.sub(varparse.var_sub, s)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500459 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 Geissler5199d832021-09-24 16:47:35 -0500463 if e.msg != "EOL while scanning string literal" and not e.msg.startswith("unterminated string literal"):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500464 raise
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500465 if s == olds:
466 break
Andrew Geissler5199d832021-09-24 16:47:35 -0500467 except ExpansionError as e:
468 e.addVar(varname)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500469 raise
470 except bb.parse.SkipRecipe:
471 raise
Andrew Geissler5199d832021-09-24 16:47:35 -0500472 except bb.BBHandledException:
473 raise
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500474 except Exception as exc:
Brad Bishop19323692019-04-05 15:28:33 -0400475 tb = sys.exc_info()[2]
476 raise ExpansionError(varname, s, exc).with_traceback(tb) from exc
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500477
478 varparse.value = s
479
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500480 return varparse
481
482 def expand(self, s, varname = None):
483 return self.expandWithRefs(s, varname).value
484
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500485 def need_overrides(self):
Patrick Williamsd7e96312015-09-22 08:09:05 -0500486 if self.overrides is not None:
487 return
488 if self.inoverride:
489 return
Andrew Geissler517393d2023-01-13 08:55:19 -0600490 overrride_stack = []
Patrick Williamsd7e96312015-09-22 08:09:05 -0500491 for count in range(5):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500492 self.inoverride = True
493 # Can end up here recursively so setup dummy values
494 self.overrides = []
495 self.overridesset = set()
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500496 self.overrides = (self.getVar("OVERRIDES") or "").split(":") or []
Andrew Geissler517393d2023-01-13 08:55:19 -0600497 overrride_stack.append(self.overrides)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500498 self.overridesset = set(self.overrides)
499 self.inoverride = False
500 self.expand_cache = {}
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500501 newoverrides = (self.getVar("OVERRIDES") or "").split(":") or []
Patrick Williamsd7e96312015-09-22 08:09:05 -0500502 if newoverrides == self.overrides:
503 break
504 self.overrides = newoverrides
505 self.overridesset = set(self.overrides)
506 else:
Andrew Geissler517393d2023-01-13 08:55:19 -0600507 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 Williamsc124f4f2015-09-15 14:41:29 -0500508
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 Bishop6e60e8b2018-02-01 10:27:11 -0500518 return dest[var], self.overridedata.get(var, None)
519
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500520 if "_data" not in dest:
521 break
522 dest = dest["_data"]
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500523 return None, self.overridedata.get(var, None)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500524
525 def _makeShadowCopy(self, var):
526 if var in self.dict:
527 return
528
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500529 local_var, _ = self._findVar(var)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500530
531 if local_var:
532 self.dict[var] = copy.copy(local_var)
533 else:
534 self.initVar(var)
535
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000536 def hasOverrides(self, var):
537 return var in self.overridedata
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500538
539 def setVar(self, var, value, **loginfo):
540 #print("var=" + str(var) + " val=" + str(value))
Patrick Williams213cb262021-08-07 19:21:33 -0500541
Andrew Geissler595f6302022-01-24 19:11:47 +0000542 if not var.startswith("__anon_") and ("_append" in var or "_prepend" in var or "_remove" in var):
Patrick Williams213cb262021-08-07 19:21:33 -0500543 info = "%s" % var
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000544 if "file" in loginfo:
545 info += " file: %s" % loginfo["file"]
546 if "line" in loginfo:
547 info += " line: %s" % loginfo["line"]
Patrick Williams213cb262021-08-07 19:21:33 -0500548 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 Geissler7e0e3c02022-02-25 20:34:39 +0000550 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 Bishop1a4b7ee2018-12-16 17:11:34 -0800556 self.expand_cache = {}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500557 parsing=False
558 if 'parsing' in loginfo:
559 parsing=True
560
561 if 'op' not in loginfo:
562 loginfo['op'] = "set"
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800563
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500564 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 Williamsd8c66bc2016-06-20 12:57:21 -0500569 l = self.getVarFlag(base, keyword, False) or []
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500570 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 Williams213cb262021-08-07 19:21:33 -0500584 if ':' in var:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500585 self._setvar_update_overrides(base, **loginfo)
586
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500587 if base in self.overridevars:
Patrick Williamsd7e96312015-09-22 08:09:05 -0500588 self._setvar_update_overridevars(var, value)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500589 return
590
591 if not var in self.dict:
592 self._makeShadowCopy(var)
593
594 if not parsing:
Patrick Williams213cb262021-08-07 19:21:33 -0500595 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 Williamsc124f4f2015-09-15 14:41:29 -0500601 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 Williams213cb262021-08-07 19:21:33 -0500607 elif ":" in o:
608 if set(o.split(":")).issubset(self.overridesset):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500609 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 Williams213cb262021-08-07 19:21:33 -0500615 if ':' in var:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500616 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 Williamsd7e96312015-09-22 08:09:05 -0500623 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 Bishop6e60e8b2018-02-01 10:27:11 -0500633 vardata = self.expandWithRefs(self.getVar(i), i)
Patrick Williamsd7e96312015-09-22 08:09:05 -0500634 nextnew.update(vardata.references)
635 nextnew.update(vardata.contains.keys())
636 new = nextnew
Patrick Williams7784c422022-11-17 07:29:11 -0600637 self.overrides = None
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500638
639 def _setvar_update_overrides(self, var, **loginfo):
640 # aka pay the cookie monster
Patrick Williams213cb262021-08-07 19:21:33 -0500641 override = var[var.rfind(':')+1:]
642 shortvar = var[:var.rfind(':')]
Brad Bishop19323692019-04-05 15:28:33 -0400643 while override and __override_regexp__.match(override):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500644 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 Williams213cb262021-08-07 19:21:33 -0500651 if ":" in shortvar:
652 override = var[shortvar.rfind(':')+1:]
653 shortvar = var[:shortvar.rfind(':')]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500654 if len(shortvar) == 0:
655 override = None
656
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500657 def getVar(self, var, expand=True, noweakdefault=False, parsing=False):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500658 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 Geissler82c905d2020-04-13 13:39:40 -0500664 if key == newkey:
665 bb.warn("Calling renameVar with equivalent keys (%s) is invalid" % key)
666 return
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500667
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500668 val = self.getVar(key, 0, parsing=True)
669 if val is not None:
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800670 self.varhistory.rename_variable_hist(key, newkey)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500671 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 Geissler9aee5002022-03-30 16:27:02 +0000677 srcflags = self.getVarFlags(key, False, True) or {}
678 for i in srcflags:
679 if i not in (__setvar_keyword__):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500680 continue
Andrew Geissler9aee5002022-03-30 16:27:02 +0000681 src = srcflags[i]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500682
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500683 dest = self.getVarFlag(newkey, i, False) or []
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500684 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 Williams213cb262021-08-07 19:21:33 -0500693 if ':' in newkey and val is None:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500694 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 Williams213cb262021-08-07 19:21:33 -0500705 self.setVar(var + ":append", value, ignore=True, parsing=True)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500706
707 def prependVar(self, var, value, **loginfo):
708 loginfo['op'] = 'prepend'
709 self.varhistory.record(**loginfo)
Patrick Williams213cb262021-08-07 19:21:33 -0500710 self.setVar(var + ":prepend", value, ignore=True, parsing=True)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500711
712 def delVar(self, var, **loginfo):
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800713 self.expand_cache = {}
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500714
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500715 loginfo['detail'] = ""
716 loginfo['op'] = 'del'
717 self.varhistory.record(**loginfo)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500718 self.dict[var] = {}
719 if var in self.overridedata:
720 del self.overridedata[var]
Patrick Williams213cb262021-08-07 19:21:33 -0500721 if ':' in var:
722 override = var[var.rfind(':')+1:]
723 shortvar = var[:var.rfind(':')]
Andrew Geissler517393d2023-01-13 08:55:19 -0600724 while override and __override_regexp__.match(override):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500725 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 Williams213cb262021-08-07 19:21:33 -0500733 if ":" in shortvar:
734 override = var[shortvar.rfind(':')+1:]
735 shortvar = var[:shortvar.rfind(':')]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500736 if len(shortvar) == 0:
737 override = None
738
739 def setVarFlag(self, var, flag, value, **loginfo):
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800740 self.expand_cache = {}
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500741
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000742 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 Williamsc124f4f2015-09-15 14:41:29 -0500750 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 Williams213cb262021-08-07 19:21:33 -0500758 if flag == "_defaultval" and ':' in var:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500759 self._setvar_update_overrides(var, **loginfo)
Patrick Williamsd7e96312015-09-22 08:09:05 -0500760 if flag == "_defaultval" and var in self.overridevars:
761 self._setvar_update_overridevars(var, value)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500762
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 Bishop1a4b7ee2018-12-16 17:11:34 -0800770 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 Geissler517393d2023-01-13 08:55:19 -0600779 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 Bishop1a4b7ee2018-12-16 17:11:34 -0800782 if expand and cachename in self.expand_cache:
783 return self.expand_cache[cachename].value
784
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500785 local_var, overridedata = self._findVar(var)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500786 value = None
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800787 removes = set()
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500788 if flag == "_content" and overridedata is not None and not parsing:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500789 match = False
790 active = {}
791 self.need_overrides()
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500792 for (r, o) in overridedata:
Patrick Williams213cb262021-08-07 19:21:33 -0500793 # FIXME What about double overrides both with "_" in the name?
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500794 if o in self.overridesset:
795 active[o] = r
Patrick Williams213cb262021-08-07 19:21:33 -0500796 elif ":" in o:
797 if set(o.split(":")).issubset(self.overridesset):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500798 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 Williams213cb262021-08-07 19:21:33 -0500805 if a.endswith(":" + o):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500806 t = active[a]
807 del active[a]
Patrick Williams213cb262021-08-07 19:21:33 -0500808 active[a.replace(":" + o, "")] = t
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500809 mod = True
810 elif a == o:
811 match = active[a]
812 del active[a]
813 if match:
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800814 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 Williamsc124f4f2015-09-15 14:41:29 -0500819
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 Williams213cb262021-08-07 19:21:33 -0500827 if flag == "_content" and local_var is not None and ":append" in local_var and not parsing:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500828 self.need_overrides()
Patrick Williams213cb262021-08-07 19:21:33 -0500829 for (r, o) in local_var[":append"]:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500830 match = True
831 if o:
Patrick Williams213cb262021-08-07 19:21:33 -0500832 for o2 in o.split(":"):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500833 if not o2 in self.overrides:
834 match = False
835 if match:
Patrick Williams213cb262021-08-07 19:21:33 -0500836 if value is None:
837 value = ""
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500838 value = value + r
839
Patrick Williams213cb262021-08-07 19:21:33 -0500840 if flag == "_content" and local_var is not None and ":prepend" in local_var and not parsing:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500841 self.need_overrides()
Patrick Williams213cb262021-08-07 19:21:33 -0500842 for (r, o) in local_var[":prepend"]:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500843
844 match = True
845 if o:
Patrick Williams213cb262021-08-07 19:21:33 -0500846 for o2 in o.split(":"):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500847 if not o2 in self.overrides:
848 match = False
849 if match:
Patrick Williams213cb262021-08-07 19:21:33 -0500850 if value is None:
851 value = ""
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500852 value = r + value
853
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800854 parser = None
855 if expand or retparser:
856 parser = self.expandWithRefs(value, cachename)
857 if expand:
858 value = parser.value
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500859
Patrick Williams213cb262021-08-07 19:21:33 -0500860 if value and flag == "_content" and local_var is not None and ":remove" in local_var and not parsing:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500861 self.need_overrides()
Patrick Williams213cb262021-08-07 19:21:33 -0500862 for (r, o) in local_var[":remove"]:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500863 match = True
864 if o:
Patrick Williams213cb262021-08-07 19:21:33 -0500865 for o2 in o.split(":"):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500866 if not o2 in self.overrides:
867 match = False
868 if match:
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800869 removes.add(r)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500870
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800871 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 Geissler595f6302022-01-24 19:11:47 +0000878 val = []
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800879 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 Geissler595f6302022-01-24 19:11:47 +0000887 val.append(v)
888 parser.value = "".join(val)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800889 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 Williamsc124f4f2015-09-15 14:41:29 -0500898 return value
899
900 def delVarFlag(self, var, flag, **loginfo):
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800901 self.expand_cache = {}
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500902
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500903 local_var, _ = self._findVar(var)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500904 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 Bishop6e60e8b2018-02-01 10:27:11 -0500946 local_var, _ = self._findVar(var)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500947 flags = {}
948
949 if local_var:
950 for i in local_var:
Patrick Williams213cb262021-08-07 19:21:33 -0500951 if i.startswith(("_", ":")) and not internalflags:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500952 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 Bishop6e60e8b2018-02-01 10:27:11 -0500988 data.varhistory.dataroot = data
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500989 data.inchistory = self.inchistory.copy()
990
991 data._tracking = self._tracking
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000992 data._var_renames = self._var_renames
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500993
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 Geissler7e0e3c02022-02-25 20:34:39 +00001015 if referrervalue and isinstance(referrervalue, str) and ref in referrervalue:
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001016 self.setVar(key, referrervalue.replace(ref, value))
1017
1018 def localkeys(self):
1019 for key in self.dict:
Andrew Geissler82c905d2020-04-13 13:39:40 -05001020 if key not in ['_data']:
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001021 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 Geissler82c905d2020-04-13 13:39:40 -05001029 if key in ["_data"]:
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001030 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 Williams213cb262021-08-07 19:21:33 -05001050 elif ":" in o:
1051 if set(o.split(":")).issubset(self.overridesset):
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001052 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 Williamsc0f7c042017-02-23 20:41:17 -06001061 return len(frozenset(iter(self)))
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001062
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 Williamsc124f4f2015-09-15 14:41:29 -05001080
Andrew Geissler7e0e3c02022-02-25 20:34:39 +00001081 config_ignore_vars = set((d.getVar("BB_HASHCONFIG_IGNORE_VARS") or "").split())
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001082 keys = set(key for key in iter(d) if not key.startswith("__"))
1083 for key in keys:
Andrew Geissler7e0e3c02022-02-25 20:34:39 +00001084 if key in config_ignore_vars:
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001085 continue
1086
1087 value = d.getVar(key, False) or ""
Andrew Geissler82c905d2020-04-13 13:39:40 -05001088 if type(value) is type(self):
1089 data.update({key:value.get_hash()})
1090 else:
1091 data.update({key:value})
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001092
Andrew Geisslerd1e89492021-02-12 15:35:20 -06001093 varflags = d.getVarFlags(key, internalflags = True, expand=["vardepvalue"])
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001094 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 Williamsc124f4f2015-09-15 14:41:29 -05001103 data.update({key:str(bb_list)})
1104
1105 if key == "__BBANONFUNCS":
1106 for i in bb_list:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05001107 value = d.getVar(i, False) or ""
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001108 data.update({i:value})
1109
1110 data_str = str([(k, data[k]) for k in sorted(data.keys())])
Brad Bishop19323692019-04-05 15:28:33 -04001111 return hashlib.sha256(data_str.encode("utf-8")).hexdigest()