Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | """ |
| 2 | BitBake 'Data' implementations |
| 3 | |
| 4 | Functions for interacting with the data structure used by the |
| 5 | BitBake build tools. |
| 6 | |
Patrick Williams | 7784c42 | 2022-11-17 07:29:11 -0600 | [diff] [blame] | 7 | expandKeys and datastore iteration are the most expensive |
| 8 | operations. Updating overrides is now "on the fly" but still based |
| 9 | on the idea of the cookie monster introduced by zecke: |
| 10 | "At night the cookie monster came by and |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 11 | suggested 'give me cookies on setting the variables and |
| 12 | things will work out'. Taking this suggestion into account |
| 13 | applying the skills from the not yet passed 'Entwurf und |
| 14 | Analyse von Algorithmen' lecture and the cookie |
| 15 | monster seems to be right. We will track setVar more carefully |
Patrick Williams | 7784c42 | 2022-11-17 07:29:11 -0600 | [diff] [blame] | 16 | to have faster datastore operations." |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 17 | |
| 18 | This is a trade-off between speed and memory again but |
| 19 | the speed is more critical here. |
| 20 | """ |
| 21 | |
| 22 | # Copyright (C) 2003, 2004 Chris Larson |
| 23 | # Copyright (C) 2005 Holger Hans Peter Freyther |
| 24 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 25 | # SPDX-License-Identifier: GPL-2.0-only |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 26 | # |
| 27 | # Based on functions from the base bb module, Copyright 2003 Holger Schurig |
| 28 | |
| 29 | import sys, os, re |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 30 | import hashlib |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 31 | if sys.argv[0][-5:] == "pydoc": |
| 32 | path = os.path.dirname(os.path.dirname(sys.argv[1])) |
| 33 | else: |
| 34 | path = os.path.dirname(os.path.dirname(sys.argv[0])) |
| 35 | sys.path.insert(0, path) |
| 36 | from itertools import groupby |
| 37 | |
| 38 | from bb import data_smart |
| 39 | from bb import codeparser |
| 40 | import bb |
| 41 | |
| 42 | logger = data_smart.logger |
| 43 | _dict_type = data_smart.DataSmart |
| 44 | |
| 45 | def init(): |
| 46 | """Return a new object representing the Bitbake data""" |
| 47 | return _dict_type() |
| 48 | |
| 49 | def init_db(parent = None): |
| 50 | """Return a new object representing the Bitbake data, |
| 51 | optionally based on an existing object""" |
| 52 | if parent is not None: |
| 53 | return parent.createCopy() |
| 54 | else: |
| 55 | return _dict_type() |
| 56 | |
| 57 | def createCopy(source): |
| 58 | """Link the source set to the destination |
| 59 | If one does not find the value in the destination set, |
| 60 | search will go on to the source set to get the value. |
| 61 | Value from source are copy-on-write. i.e. any try to |
| 62 | modify one of them will end up putting the modified value |
| 63 | in the destination set. |
| 64 | """ |
| 65 | return source.createCopy() |
| 66 | |
| 67 | def initVar(var, d): |
| 68 | """Non-destructive var init for data structure""" |
| 69 | d.initVar(var) |
| 70 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 71 | def keys(d): |
| 72 | """Return a list of keys in d""" |
| 73 | return d.keys() |
| 74 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 75 | def expand(s, d, varname = None): |
| 76 | """Variable expansion using the data store""" |
| 77 | return d.expand(s, varname) |
| 78 | |
| 79 | def expandKeys(alterdata, readdata = None): |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 80 | if readdata is None: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 81 | readdata = alterdata |
| 82 | |
| 83 | todolist = {} |
| 84 | for key in alterdata: |
| 85 | if not '${' in key: |
| 86 | continue |
| 87 | |
| 88 | ekey = expand(key, readdata) |
| 89 | if key == ekey: |
| 90 | continue |
| 91 | todolist[key] = ekey |
| 92 | |
| 93 | # These two for loops are split for performance to maximise the |
| 94 | # usefulness of the expand cache |
| 95 | for key in sorted(todolist): |
| 96 | ekey = todolist[key] |
| 97 | newval = alterdata.getVar(ekey, False) |
| 98 | if newval is not None: |
| 99 | val = alterdata.getVar(key, False) |
| 100 | if val is not None: |
| 101 | bb.warn("Variable key %s (%s) replaces original key %s (%s)." % (key, val, ekey, newval)) |
| 102 | alterdata.renameVar(key, ekey) |
| 103 | |
| 104 | def inheritFromOS(d, savedenv, permitted): |
| 105 | """Inherit variables from the initial environment.""" |
| 106 | exportlist = bb.utils.preserved_envvars_exported() |
| 107 | for s in savedenv.keys(): |
| 108 | if s in permitted: |
| 109 | try: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 110 | d.setVar(s, savedenv.getVar(s), op = 'from env') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 111 | if s in exportlist: |
| 112 | d.setVarFlag(s, "export", True, op = 'auto env export') |
| 113 | except TypeError: |
| 114 | pass |
| 115 | |
| 116 | def emit_var(var, o=sys.__stdout__, d = init(), all=False): |
| 117 | """Emit a variable to be sourced by a shell.""" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 118 | func = d.getVarFlag(var, "func", False) |
| 119 | if d.getVarFlag(var, 'python', False) and func: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 120 | return False |
| 121 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 122 | export = d.getVarFlag(var, "export", False) |
| 123 | unexport = d.getVarFlag(var, "unexport", False) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 124 | if not all and not export and not unexport and not func: |
| 125 | return False |
| 126 | |
| 127 | try: |
| 128 | if all: |
| 129 | oval = d.getVar(var, False) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 130 | val = d.getVar(var) |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 131 | except (KeyboardInterrupt): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 132 | raise |
| 133 | except Exception as exc: |
| 134 | o.write('# expansion of %s threw %s: %s\n' % (var, exc.__class__.__name__, str(exc))) |
| 135 | return False |
| 136 | |
| 137 | if all: |
| 138 | d.varhistory.emit(var, oval, val, o, d) |
| 139 | |
| 140 | if (var.find("-") != -1 or var.find(".") != -1 or var.find('{') != -1 or var.find('}') != -1 or var.find('+') != -1) and not all: |
| 141 | return False |
| 142 | |
| 143 | varExpanded = d.expand(var) |
| 144 | |
| 145 | if unexport: |
| 146 | o.write('unset %s\n' % varExpanded) |
| 147 | return False |
| 148 | |
| 149 | if val is None: |
| 150 | return False |
| 151 | |
| 152 | val = str(val) |
| 153 | |
| 154 | if varExpanded.startswith("BASH_FUNC_"): |
| 155 | varExpanded = varExpanded[10:-2] |
| 156 | val = val[3:] # Strip off "() " |
| 157 | o.write("%s() %s\n" % (varExpanded, val)) |
| 158 | o.write("export -f %s\n" % (varExpanded)) |
| 159 | return True |
| 160 | |
| 161 | if func: |
Andrew Geissler | 635e0e4 | 2020-08-21 15:58:33 -0500 | [diff] [blame] | 162 | # Write a comment indicating where the shell function came from (line number and filename) to make it easier |
| 163 | # for the user to diagnose task failures. This comment is also used by build.py to determine the metadata |
| 164 | # location of shell functions. |
| 165 | o.write("# line: {0}, file: {1}\n".format( |
| 166 | d.getVarFlag(var, "lineno", False), |
| 167 | d.getVarFlag(var, "filename", False))) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 168 | # NOTE: should probably check for unbalanced {} within the var |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 169 | val = val.rstrip('\n') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 170 | o.write("%s() {\n%s\n}\n" % (varExpanded, val)) |
| 171 | return 1 |
| 172 | |
| 173 | if export: |
| 174 | o.write('export ') |
| 175 | |
| 176 | # if we're going to output this within doublequotes, |
| 177 | # to a shell, we need to escape the quotes in the var |
| 178 | alter = re.sub('"', '\\"', val) |
| 179 | alter = re.sub('\n', ' \\\n', alter) |
| 180 | alter = re.sub('\\$', '\\\\$', alter) |
| 181 | o.write('%s="%s"\n' % (varExpanded, alter)) |
| 182 | return False |
| 183 | |
| 184 | def emit_env(o=sys.__stdout__, d = init(), all=False): |
| 185 | """Emits all items in the data store in a format such that it can be sourced by a shell.""" |
| 186 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 187 | isfunc = lambda key: bool(d.getVarFlag(key, "func", False)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 188 | keys = sorted((key for key in d.keys() if not key.startswith("__")), key=isfunc) |
| 189 | grouped = groupby(keys, isfunc) |
| 190 | for isfunc, keys in grouped: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 191 | for key in sorted(keys): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 192 | emit_var(key, o, d, all and not isfunc) and o.write('\n') |
| 193 | |
| 194 | def exported_keys(d): |
| 195 | return (key for key in d.keys() if not key.startswith('__') and |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 196 | d.getVarFlag(key, 'export', False) and |
| 197 | not d.getVarFlag(key, 'unexport', False)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 198 | |
| 199 | def exported_vars(d): |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 200 | k = list(exported_keys(d)) |
| 201 | for key in k: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 202 | try: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 203 | value = d.getVar(key) |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 204 | except Exception as err: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 205 | bb.warn("%s: Unable to export ${%s}: %s" % (d.getVar("FILE"), key, err)) |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 206 | continue |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 207 | |
| 208 | if value is not None: |
| 209 | yield key, str(value) |
| 210 | |
| 211 | def emit_func(func, o=sys.__stdout__, d = init()): |
| 212 | """Emits all items in the data store in a format such that it can be sourced by a shell.""" |
| 213 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 214 | keys = (key for key in d.keys() if not key.startswith("__") and not d.getVarFlag(key, "func", False)) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 215 | for key in sorted(keys): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 216 | emit_var(key, o, d, False) |
| 217 | |
| 218 | o.write('\n') |
| 219 | emit_var(func, o, d, False) and o.write('\n') |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 220 | newdeps = bb.codeparser.ShellParser(func, logger).parse_shell(d.getVar(func)) |
| 221 | newdeps |= set((d.getVarFlag(func, "vardeps") or "").split()) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 222 | seen = set() |
| 223 | while newdeps: |
| 224 | deps = newdeps |
| 225 | seen |= deps |
| 226 | newdeps = set() |
Patrick Williams | 93c203f | 2021-10-06 16:15:23 -0500 | [diff] [blame] | 227 | for dep in sorted(deps): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 228 | if d.getVarFlag(dep, "func", False) and not d.getVarFlag(dep, "python", False): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 229 | emit_var(dep, o, d, False) and o.write('\n') |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 230 | newdeps |= bb.codeparser.ShellParser(dep, logger).parse_shell(d.getVar(dep)) |
| 231 | newdeps |= set((d.getVarFlag(dep, "vardeps") or "").split()) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 232 | newdeps -= seen |
| 233 | |
| 234 | _functionfmt = """ |
| 235 | def {function}(d): |
| 236 | {body}""" |
| 237 | |
| 238 | def emit_func_python(func, o=sys.__stdout__, d = init()): |
| 239 | """Emits all items in the data store in a format such that it can be sourced by a shell.""" |
| 240 | |
| 241 | def write_func(func, o, call = False): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 242 | body = d.getVar(func, False) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 243 | if not body.startswith("def"): |
| 244 | body = _functionfmt.format(function=func, body=body) |
| 245 | |
| 246 | o.write(body.strip() + "\n\n") |
| 247 | if call: |
| 248 | o.write(func + "(d)" + "\n\n") |
| 249 | |
| 250 | write_func(func, o, True) |
| 251 | pp = bb.codeparser.PythonParser(func, logger) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 252 | pp.parse_python(d.getVar(func, False)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 253 | newdeps = pp.execs |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 254 | newdeps |= set((d.getVarFlag(func, "vardeps") or "").split()) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 255 | seen = set() |
| 256 | while newdeps: |
| 257 | deps = newdeps |
| 258 | seen |= deps |
| 259 | newdeps = set() |
| 260 | for dep in deps: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 261 | if d.getVarFlag(dep, "func", False) and d.getVarFlag(dep, "python", False): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 262 | write_func(dep, o) |
| 263 | pp = bb.codeparser.PythonParser(dep, logger) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 264 | pp.parse_python(d.getVar(dep, False)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 265 | newdeps |= pp.execs |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 266 | newdeps |= set((d.getVarFlag(dep, "vardeps") or "").split()) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 267 | newdeps -= seen |
| 268 | |
Patrick Williams | de0582f | 2022-04-08 10:23:27 -0500 | [diff] [blame] | 269 | def build_dependencies(key, keys, shelldeps, varflagsexcl, ignored_vars, d): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 270 | deps = set() |
| 271 | try: |
| 272 | if key[-1] == ']': |
| 273 | vf = key[:-1].split('[') |
Andrew Geissler | d583833 | 2022-05-27 11:33:10 -0500 | [diff] [blame] | 274 | if vf[1] == "vardepvalueexclude": |
| 275 | return deps, "" |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 276 | value, parser = d.getVarFlag(vf[0], vf[1], False, retparser=True) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 277 | deps |= parser.references |
| 278 | deps = deps | (keys & parser.execs) |
| 279 | return deps, value |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 280 | varflags = d.getVarFlags(key, ["vardeps", "vardepvalue", "vardepsexclude", "exports", "postfuncs", "prefuncs", "lineno", "filename"]) or {} |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 281 | vardeps = varflags.get("vardeps") |
Patrick Williams | de0582f | 2022-04-08 10:23:27 -0500 | [diff] [blame] | 282 | exclusions = varflags.get("vardepsexclude", "").split() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 283 | |
Patrick Williams | de0582f | 2022-04-08 10:23:27 -0500 | [diff] [blame] | 284 | def handle_contains(value, contains, exclusions, d): |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 285 | newvalue = [] |
| 286 | if value: |
| 287 | newvalue.append(str(value)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 288 | for k in sorted(contains): |
Patrick Williams | de0582f | 2022-04-08 10:23:27 -0500 | [diff] [blame] | 289 | if k in exclusions or k in ignored_vars: |
| 290 | continue |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 291 | l = (d.getVar(k) or "").split() |
| 292 | for item in sorted(contains[k]): |
| 293 | for word in item.split(): |
| 294 | if not word in l: |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 295 | newvalue.append("\n%s{%s} = Unset" % (k, item)) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 296 | break |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 297 | else: |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 298 | newvalue.append("\n%s{%s} = Set" % (k, item)) |
| 299 | return "".join(newvalue) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 300 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 301 | def handle_remove(value, deps, removes, d): |
| 302 | for r in sorted(removes): |
| 303 | r2 = d.expandWithRefs(r, None) |
| 304 | value += "\n_remove of %s" % r |
| 305 | deps |= r2.references |
| 306 | deps = deps | (keys & r2.execs) |
| 307 | return value |
| 308 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 309 | if "vardepvalue" in varflags: |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 310 | value = varflags.get("vardepvalue") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 311 | elif varflags.get("func"): |
| 312 | if varflags.get("python"): |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 313 | value = d.getVarFlag(key, "_content", False) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 314 | parser = bb.codeparser.PythonParser(key, logger) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 315 | parser.parse_python(value, filename=varflags.get("filename"), lineno=varflags.get("lineno")) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 316 | deps = deps | parser.references |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 317 | deps = deps | (keys & parser.execs) |
Patrick Williams | de0582f | 2022-04-08 10:23:27 -0500 | [diff] [blame] | 318 | value = handle_contains(value, parser.contains, exclusions, d) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 319 | else: |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 320 | value, parsedvar = d.getVarFlag(key, "_content", False, retparser=True) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 321 | parser = bb.codeparser.ShellParser(key, logger) |
| 322 | parser.parse_shell(parsedvar.value) |
| 323 | deps = deps | shelldeps |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 324 | deps = deps | parsedvar.references |
| 325 | deps = deps | (keys & parser.execs) | (keys & parsedvar.execs) |
Patrick Williams | de0582f | 2022-04-08 10:23:27 -0500 | [diff] [blame] | 326 | value = handle_contains(value, parsedvar.contains, exclusions, d) |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 327 | if hasattr(parsedvar, "removes"): |
| 328 | value = handle_remove(value, deps, parsedvar.removes, d) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 329 | if vardeps is None: |
| 330 | parser.log.flush() |
| 331 | if "prefuncs" in varflags: |
| 332 | deps = deps | set(varflags["prefuncs"].split()) |
| 333 | if "postfuncs" in varflags: |
| 334 | deps = deps | set(varflags["postfuncs"].split()) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 335 | if "exports" in varflags: |
| 336 | deps = deps | set(varflags["exports"].split()) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 337 | else: |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 338 | value, parser = d.getVarFlag(key, "_content", False, retparser=True) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 339 | deps |= parser.references |
| 340 | deps = deps | (keys & parser.execs) |
Patrick Williams | de0582f | 2022-04-08 10:23:27 -0500 | [diff] [blame] | 341 | value = handle_contains(value, parser.contains, exclusions, d) |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 342 | if hasattr(parser, "removes"): |
| 343 | value = handle_remove(value, deps, parser.removes, d) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 344 | |
| 345 | if "vardepvalueexclude" in varflags: |
| 346 | exclude = varflags.get("vardepvalueexclude") |
| 347 | for excl in exclude.split('|'): |
| 348 | if excl: |
| 349 | value = value.replace(excl, '') |
| 350 | |
| 351 | # Add varflags, assuming an exclusion list is set |
| 352 | if varflagsexcl: |
| 353 | varfdeps = [] |
| 354 | for f in varflags: |
| 355 | if f not in varflagsexcl: |
| 356 | varfdeps.append('%s[%s]' % (key, f)) |
| 357 | if varfdeps: |
| 358 | deps |= set(varfdeps) |
| 359 | |
| 360 | deps |= set((vardeps or "").split()) |
Patrick Williams | de0582f | 2022-04-08 10:23:27 -0500 | [diff] [blame] | 361 | deps -= set(exclusions) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 362 | except bb.parse.SkipRecipe: |
| 363 | raise |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 364 | except Exception as e: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 365 | bb.warn("Exception during build_dependencies for %s" % key) |
| 366 | raise |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 367 | return deps, value |
| 368 | #bb.note("Variable %s references %s and calls %s" % (key, str(deps), str(execs))) |
| 369 | #d.setVarFlag(key, "vardeps", deps) |
| 370 | |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 371 | def generate_dependencies(d, ignored_vars): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 372 | |
| 373 | keys = set(key for key in d if not key.startswith("__")) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 374 | shelldeps = set(key for key in d.getVar("__exportlist", False) if d.getVarFlag(key, "export", False) and not d.getVarFlag(key, "unexport", False)) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 375 | varflagsexcl = d.getVar('BB_SIGNATURE_EXCLUDE_FLAGS') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 376 | |
| 377 | deps = {} |
| 378 | values = {} |
| 379 | |
| 380 | tasklist = d.getVar('__BBTASKS', False) or [] |
| 381 | for task in tasklist: |
Patrick Williams | de0582f | 2022-04-08 10:23:27 -0500 | [diff] [blame] | 382 | deps[task], values[task] = build_dependencies(task, keys, shelldeps, varflagsexcl, ignored_vars, d) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 383 | newdeps = deps[task] |
| 384 | seen = set() |
| 385 | while newdeps: |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 386 | nextdeps = newdeps - ignored_vars |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 387 | seen |= nextdeps |
| 388 | newdeps = set() |
| 389 | for dep in nextdeps: |
| 390 | if dep not in deps: |
Patrick Williams | de0582f | 2022-04-08 10:23:27 -0500 | [diff] [blame] | 391 | deps[dep], values[dep] = build_dependencies(dep, keys, shelldeps, varflagsexcl, ignored_vars, d) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 392 | newdeps |= deps[dep] |
| 393 | newdeps -= seen |
| 394 | #print "For %s: %s" % (task, str(deps[task])) |
| 395 | return tasklist, deps, values |
| 396 | |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 397 | def generate_dependency_hash(tasklist, gendeps, lookupcache, ignored_vars, fn): |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 398 | taskdeps = {} |
| 399 | basehash = {} |
| 400 | |
| 401 | for task in tasklist: |
| 402 | data = lookupcache[task] |
| 403 | |
| 404 | if data is None: |
| 405 | bb.error("Task %s from %s seems to be empty?!" % (task, fn)) |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 406 | data = [] |
| 407 | else: |
| 408 | data = [data] |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 409 | |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 410 | gendeps[task] -= ignored_vars |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 411 | newdeps = gendeps[task] |
| 412 | seen = set() |
| 413 | while newdeps: |
| 414 | nextdeps = newdeps |
| 415 | seen |= nextdeps |
| 416 | newdeps = set() |
| 417 | for dep in nextdeps: |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 418 | if dep in ignored_vars: |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 419 | continue |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 420 | gendeps[dep] -= ignored_vars |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 421 | newdeps |= gendeps[dep] |
| 422 | newdeps -= seen |
| 423 | |
| 424 | alldeps = sorted(seen) |
| 425 | for dep in alldeps: |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 426 | data.append(dep) |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 427 | var = lookupcache[dep] |
| 428 | if var is not None: |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 429 | data.append(str(var)) |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 430 | k = fn + ":" + task |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 431 | basehash[k] = hashlib.sha256("".join(data).encode("utf-8")).hexdigest() |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 432 | taskdeps[task] = alldeps |
| 433 | |
| 434 | return taskdeps, basehash |
| 435 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 436 | def inherits_class(klass, d): |
| 437 | val = d.getVar('__inherit_cache', False) or [] |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 438 | needle = '/%s.bbclass' % klass |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 439 | for v in val: |
| 440 | if v.endswith(needle): |
| 441 | return True |
| 442 | return False |