Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 1 | # |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 2 | # Copyright OpenEmbedded Contributors |
| 3 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 4 | # SPDX-License-Identifier: GPL-2.0-only |
| 5 | # |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 6 | import bb.siggen |
Andrew Geissler | d25ed32 | 2020-06-27 00:28:28 -0500 | [diff] [blame] | 7 | import bb.runqueue |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 8 | import oe |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 9 | |
Andrew Geissler | d25ed32 | 2020-06-27 00:28:28 -0500 | [diff] [blame] | 10 | def sstate_rundepfilter(siggen, fn, recipename, task, dep, depname, dataCaches): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 11 | # Return True if we should keep the dependency, False to drop it |
| 12 | def isNative(x): |
| 13 | return x.endswith("-native") |
| 14 | def isCross(x): |
| 15 | return "-cross-" in x |
| 16 | def isNativeSDK(x): |
| 17 | return x.startswith("nativesdk-") |
Andrew Geissler | d25ed32 | 2020-06-27 00:28:28 -0500 | [diff] [blame] | 18 | def isKernel(mc, fn): |
| 19 | inherits = " ".join(dataCaches[mc].inherits[fn]) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 20 | return inherits.find("/module-base.bbclass") != -1 or inherits.find("/linux-kernel-base.bbclass") != -1 |
Andrew Geissler | d25ed32 | 2020-06-27 00:28:28 -0500 | [diff] [blame] | 21 | def isPackageGroup(mc, fn): |
| 22 | inherits = " ".join(dataCaches[mc].inherits[fn]) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 23 | return "/packagegroup.bbclass" in inherits |
Andrew Geissler | d25ed32 | 2020-06-27 00:28:28 -0500 | [diff] [blame] | 24 | def isAllArch(mc, fn): |
| 25 | inherits = " ".join(dataCaches[mc].inherits[fn]) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 26 | return "/allarch.bbclass" in inherits |
Andrew Geissler | d25ed32 | 2020-06-27 00:28:28 -0500 | [diff] [blame] | 27 | def isImage(mc, fn): |
| 28 | return "/image.bbclass" in " ".join(dataCaches[mc].inherits[fn]) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 29 | |
Andrew Geissler | d25ed32 | 2020-06-27 00:28:28 -0500 | [diff] [blame] | 30 | depmc, _, deptaskname, depmcfn = bb.runqueue.split_tid_mcfn(dep) |
| 31 | mc, _ = bb.runqueue.split_mc(fn) |
| 32 | |
Patrick Williams | 7784c42 | 2022-11-17 07:29:11 -0600 | [diff] [blame] | 33 | # We can skip the rm_work task signature to avoid running the task |
| 34 | # when we remove some tasks from the dependencie chain |
| 35 | # i.e INHERIT:remove = "create-spdx" will trigger the do_rm_work |
| 36 | if task == "do_rm_work": |
| 37 | return False |
| 38 | |
Andrew Geissler | d25ed32 | 2020-06-27 00:28:28 -0500 | [diff] [blame] | 39 | # (Almost) always include our own inter-task dependencies (unless it comes |
| 40 | # from a mcdepends). The exception is the special |
| 41 | # do_kernel_configme->do_unpack_and_patch dependency from archiver.bbclass. |
| 42 | if recipename == depname and depmc == mc: |
| 43 | if task == "do_kernel_configme" and deptaskname == "do_unpack_and_patch": |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 44 | return False |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 45 | return True |
| 46 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 47 | # Exclude well defined recipe->dependency |
| 48 | if "%s->%s" % (recipename, depname) in siggen.saferecipedeps: |
| 49 | return False |
| 50 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 51 | # Check for special wildcard |
| 52 | if "*->%s" % depname in siggen.saferecipedeps and recipename != depname: |
| 53 | return False |
| 54 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 55 | # Don't change native/cross/nativesdk recipe dependencies any further |
| 56 | if isNative(recipename) or isCross(recipename) or isNativeSDK(recipename): |
| 57 | return True |
| 58 | |
| 59 | # Only target packages beyond here |
| 60 | |
| 61 | # allarch packagegroups are assumed to have well behaved names which don't change between architecures/tunes |
Andrew Geissler | d25ed32 | 2020-06-27 00:28:28 -0500 | [diff] [blame] | 62 | if isPackageGroup(mc, fn) and isAllArch(mc, fn) and not isNative(depname): |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 63 | return False |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 64 | |
| 65 | # Exclude well defined machine specific configurations which don't change ABI |
Andrew Geissler | d25ed32 | 2020-06-27 00:28:28 -0500 | [diff] [blame] | 66 | if depname in siggen.abisaferecipes and not isImage(mc, fn): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 67 | return False |
| 68 | |
| 69 | # Kernel modules are well namespaced. We don't want to depend on the kernel's checksum |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 70 | # if we're just doing an RRECOMMENDS:xxx = "kernel-module-*", not least because the checksum |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 71 | # is machine specific. |
| 72 | # Therefore if we're not a kernel or a module recipe (inheriting the kernel classes) |
| 73 | # and we reccomend a kernel-module, we exclude the dependency. |
Andrew Geissler | d25ed32 | 2020-06-27 00:28:28 -0500 | [diff] [blame] | 74 | if dataCaches and isKernel(depmc, depmcfn) and not isKernel(mc, fn): |
| 75 | for pkg in dataCaches[mc].runrecs[fn]: |
| 76 | if " ".join(dataCaches[mc].runrecs[fn][pkg]).find("kernel-module-") != -1: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 77 | return False |
| 78 | |
| 79 | # Default to keep dependencies |
| 80 | return True |
| 81 | |
| 82 | def sstate_lockedsigs(d): |
| 83 | sigs = {} |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 84 | types = (d.getVar("SIGGEN_LOCKEDSIGS_TYPES") or "").split() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 85 | for t in types: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 86 | siggen_lockedsigs_var = "SIGGEN_LOCKEDSIGS_%s" % t |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 87 | lockedsigs = (d.getVar(siggen_lockedsigs_var) or "").split() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 88 | for ls in lockedsigs: |
| 89 | pn, task, h = ls.split(":", 2) |
| 90 | if pn not in sigs: |
| 91 | sigs[pn] = {} |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 92 | sigs[pn][task] = [h, siggen_lockedsigs_var] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 93 | return sigs |
| 94 | |
Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 95 | class SignatureGeneratorOEBasicHashMixIn(object): |
Andrew Geissler | d25ed32 | 2020-06-27 00:28:28 -0500 | [diff] [blame] | 96 | supports_multiconfig_datacaches = True |
| 97 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 98 | def init_rundepcheck(self, data): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 99 | self.abisaferecipes = (data.getVar("SIGGEN_EXCLUDERECIPES_ABISAFE") or "").split() |
| 100 | self.saferecipedeps = (data.getVar("SIGGEN_EXCLUDE_SAFE_RECIPE_DEPS") or "").split() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 101 | self.lockedsigs = sstate_lockedsigs(data) |
| 102 | self.lockedhashes = {} |
| 103 | self.lockedpnmap = {} |
| 104 | self.lockedhashfn = {} |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 105 | self.machine = data.getVar("MACHINE") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 106 | self.mismatch_msgs = [] |
Andrew Geissler | 2013739 | 2023-10-12 04:59:14 -0600 | [diff] [blame] | 107 | self.mismatch_number = 0 |
| 108 | self.lockedsigs_msgs = "" |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 109 | self.unlockedrecipes = (data.getVar("SIGGEN_UNLOCKED_RECIPES") or |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 110 | "").split() |
| 111 | self.unlockedrecipes = { k: "" for k in self.unlockedrecipes } |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 112 | self._internal = False |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 113 | pass |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 114 | |
| 115 | def tasks_resolved(self, virtmap, virtpnmap, dataCache): |
| 116 | # Translate virtual/xxx entries to PN values |
| 117 | newabisafe = [] |
| 118 | for a in self.abisaferecipes: |
| 119 | if a in virtpnmap: |
| 120 | newabisafe.append(virtpnmap[a]) |
| 121 | else: |
| 122 | newabisafe.append(a) |
| 123 | self.abisaferecipes = newabisafe |
| 124 | newsafedeps = [] |
| 125 | for a in self.saferecipedeps: |
| 126 | a1, a2 = a.split("->") |
| 127 | if a1 in virtpnmap: |
| 128 | a1 = virtpnmap[a1] |
| 129 | if a2 in virtpnmap: |
| 130 | a2 = virtpnmap[a2] |
| 131 | newsafedeps.append(a1 + "->" + a2) |
| 132 | self.saferecipedeps = newsafedeps |
| 133 | |
Andrew Geissler | d25ed32 | 2020-06-27 00:28:28 -0500 | [diff] [blame] | 134 | def rundep_check(self, fn, recipename, task, dep, depname, dataCaches = None): |
| 135 | return sstate_rundepfilter(self, fn, recipename, task, dep, depname, dataCaches) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 136 | |
| 137 | def get_taskdata(self): |
Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 138 | return (self.lockedpnmap, self.lockedhashfn, self.lockedhashes) + super().get_taskdata() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 139 | |
| 140 | def set_taskdata(self, data): |
Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 141 | self.lockedpnmap, self.lockedhashfn, self.lockedhashes = data[:3] |
| 142 | super().set_taskdata(data[3:]) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 143 | |
| 144 | def dump_sigs(self, dataCache, options): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 145 | sigfile = os.getcwd() + "/locked-sigs.inc" |
| 146 | bb.plain("Writing locked sigs to %s" % sigfile) |
| 147 | self.dump_lockedsigs(sigfile) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 148 | return super(bb.siggen.SignatureGeneratorBasicHash, self).dump_sigs(dataCache, options) |
| 149 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 150 | |
Andrew Geissler | d25ed32 | 2020-06-27 00:28:28 -0500 | [diff] [blame] | 151 | def get_taskhash(self, tid, deps, dataCaches): |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 152 | if tid in self.lockedhashes: |
| 153 | if self.lockedhashes[tid]: |
| 154 | return self.lockedhashes[tid] |
| 155 | else: |
Andrew Geissler | d25ed32 | 2020-06-27 00:28:28 -0500 | [diff] [blame] | 156 | return super().get_taskhash(tid, deps, dataCaches) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 157 | |
Andrew Geissler | d25ed32 | 2020-06-27 00:28:28 -0500 | [diff] [blame] | 158 | h = super().get_taskhash(tid, deps, dataCaches) |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 159 | |
| 160 | (mc, _, task, fn) = bb.runqueue.split_tid_mcfn(tid) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 161 | |
Andrew Geissler | d25ed32 | 2020-06-27 00:28:28 -0500 | [diff] [blame] | 162 | recipename = dataCaches[mc].pkg_fn[fn] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 163 | self.lockedpnmap[fn] = recipename |
Andrew Geissler | d25ed32 | 2020-06-27 00:28:28 -0500 | [diff] [blame] | 164 | self.lockedhashfn[fn] = dataCaches[mc].hashfn[fn] |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 165 | |
| 166 | unlocked = False |
| 167 | if recipename in self.unlockedrecipes: |
| 168 | unlocked = True |
| 169 | else: |
| 170 | def recipename_from_dep(dep): |
Andrew Geissler | d25ed32 | 2020-06-27 00:28:28 -0500 | [diff] [blame] | 171 | (depmc, _, _, depfn) = bb.runqueue.split_tid_mcfn(dep) |
| 172 | return dataCaches[depmc].pkg_fn[depfn] |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 173 | |
| 174 | # If any unlocked recipe is in the direct dependencies then the |
| 175 | # current recipe should be unlocked as well. |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 176 | depnames = [ recipename_from_dep(x) for x in deps if mc == bb.runqueue.mc_from_tid(x)] |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 177 | if any(x in y for y in depnames for x in self.unlockedrecipes): |
| 178 | self.unlockedrecipes[recipename] = '' |
| 179 | unlocked = True |
| 180 | |
| 181 | if not unlocked and recipename in self.lockedsigs: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 182 | if task in self.lockedsigs[recipename]: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 183 | h_locked = self.lockedsigs[recipename][task][0] |
| 184 | var = self.lockedsigs[recipename][task][1] |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 185 | self.lockedhashes[tid] = h_locked |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 186 | self._internal = True |
| 187 | unihash = self.get_unihash(tid) |
| 188 | self._internal = False |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 189 | #bb.warn("Using %s %s %s" % (recipename, task, h)) |
| 190 | |
Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 191 | if h != h_locked and h_locked != unihash: |
Andrew Geissler | 2013739 | 2023-10-12 04:59:14 -0600 | [diff] [blame] | 192 | self.mismatch_number += 1 |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 193 | self.mismatch_msgs.append('The %s:%s sig is computed to be %s, but the sig is locked to %s in %s' |
| 194 | % (recipename, task, h, h_locked, var)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 195 | |
| 196 | return h_locked |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 197 | |
| 198 | self.lockedhashes[tid] = False |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 199 | #bb.warn("%s %s %s" % (recipename, task, h)) |
| 200 | return h |
| 201 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 202 | def get_stampfile_hash(self, tid): |
| 203 | if tid in self.lockedhashes and self.lockedhashes[tid]: |
| 204 | return self.lockedhashes[tid] |
| 205 | return super().get_stampfile_hash(tid) |
| 206 | |
Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 207 | def get_unihash(self, tid): |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 208 | if tid in self.lockedhashes and self.lockedhashes[tid] and not self._internal: |
Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 209 | return self.lockedhashes[tid] |
| 210 | return super().get_unihash(tid) |
| 211 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 212 | def dump_sigtask(self, fn, task, stampbase, runtime): |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 213 | tid = fn + ":" + task |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 214 | if tid in self.lockedhashes and self.lockedhashes[tid]: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 215 | return |
| 216 | super(bb.siggen.SignatureGeneratorBasicHash, self).dump_sigtask(fn, task, stampbase, runtime) |
| 217 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 218 | def dump_lockedsigs(self, sigfile, taskfilter=None): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 219 | types = {} |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 220 | for tid in self.runtaskdeps: |
Patrick Williams | 2a25492 | 2023-08-11 09:48:11 -0500 | [diff] [blame] | 221 | # Bitbake changed this to a tuple in newer versions |
| 222 | if isinstance(tid, tuple): |
| 223 | tid = tid[1] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 224 | if taskfilter: |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 225 | if not tid in taskfilter: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 226 | continue |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 227 | fn = bb.runqueue.fn_from_tid(tid) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 228 | t = self.lockedhashfn[fn].split(" ")[1].split(":")[5] |
| 229 | t = 't-' + t.replace('_', '-') |
| 230 | if t not in types: |
| 231 | types[t] = [] |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 232 | types[t].append(tid) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 233 | |
| 234 | with open(sigfile, "w") as f: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 235 | l = sorted(types) |
| 236 | for t in l: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 237 | f.write('SIGGEN_LOCKEDSIGS_%s = "\\\n' % t) |
| 238 | types[t].sort() |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 239 | sortedtid = sorted(types[t], key=lambda tid: self.lockedpnmap[bb.runqueue.fn_from_tid(tid)]) |
| 240 | for tid in sortedtid: |
| 241 | (_, _, task, fn) = bb.runqueue.split_tid_mcfn(tid) |
| 242 | if tid not in self.taskhash: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 243 | continue |
Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 244 | f.write(" " + self.lockedpnmap[fn] + ":" + task + ":" + self.get_unihash(tid) + " \\\n") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 245 | f.write(' "\n') |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 246 | f.write('SIGGEN_LOCKEDSIGS_TYPES:%s = "%s"' % (self.machine, " ".join(l))) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 247 | |
Andrew Geissler | 0903674 | 2021-06-25 14:25:14 -0500 | [diff] [blame] | 248 | def dump_siglist(self, sigfile, path_prefix_strip=None): |
| 249 | def strip_fn(fn): |
| 250 | nonlocal path_prefix_strip |
| 251 | if not path_prefix_strip: |
| 252 | return fn |
| 253 | |
| 254 | fn_exp = fn.split(":") |
| 255 | if fn_exp[-1].startswith(path_prefix_strip): |
| 256 | fn_exp[-1] = fn_exp[-1][len(path_prefix_strip):] |
| 257 | |
| 258 | return ":".join(fn_exp) |
| 259 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 260 | with open(sigfile, "w") as f: |
| 261 | tasks = [] |
| 262 | for taskitem in self.taskhash: |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 263 | (fn, task) = taskitem.rsplit(":", 1) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 264 | pn = self.lockedpnmap[fn] |
Andrew Geissler | 0903674 | 2021-06-25 14:25:14 -0500 | [diff] [blame] | 265 | tasks.append((pn, task, strip_fn(fn), self.taskhash[taskitem])) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 266 | for (pn, task, fn, taskhash) in sorted(tasks): |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 267 | f.write('%s:%s %s %s\n' % (pn, task, fn, taskhash)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 268 | |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 269 | def checkhashes(self, sq_data, missed, found, d): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 270 | warn_msgs = [] |
| 271 | error_msgs = [] |
| 272 | sstate_missing_msgs = [] |
Andrew Geissler | 2013739 | 2023-10-12 04:59:14 -0600 | [diff] [blame] | 273 | info_msgs = None |
| 274 | |
| 275 | if self.lockedsigs: |
| 276 | if len(self.lockedsigs) > 10: |
| 277 | self.lockedsigs_msgs = "There are %s recipes with locked tasks (%s task(s) have non matching signature)" % (len(self.lockedsigs), self.mismatch_number) |
| 278 | else: |
| 279 | self.lockedsigs_msgs = "The following recipes have locked tasks:" |
| 280 | for pn in self.lockedsigs: |
| 281 | self.lockedsigs_msgs += " %s" % (pn) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 282 | |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 283 | for tid in sq_data['hash']: |
| 284 | if tid not in found: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 285 | for pn in self.lockedsigs: |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 286 | taskname = bb.runqueue.taskname_from_tid(tid) |
| 287 | if sq_data['hash'][tid] in iter(self.lockedsigs[pn].values()): |
| 288 | if taskname == 'do_shared_workdir': |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 289 | continue |
| 290 | sstate_missing_msgs.append("Locked sig is set for %s:%s (%s) yet not in sstate cache?" |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 291 | % (pn, taskname, sq_data['hash'][tid])) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 292 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 293 | checklevel = d.getVar("SIGGEN_LOCKEDSIGS_TASKSIG_CHECK") |
Andrew Geissler | 2013739 | 2023-10-12 04:59:14 -0600 | [diff] [blame] | 294 | if checklevel == 'info': |
| 295 | info_msgs = self.lockedsigs_msgs |
| 296 | if checklevel == 'warn' or checklevel == 'info': |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 297 | warn_msgs += self.mismatch_msgs |
| 298 | elif checklevel == 'error': |
| 299 | error_msgs += self.mismatch_msgs |
| 300 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 301 | checklevel = d.getVar("SIGGEN_LOCKEDSIGS_SSTATE_EXISTS_CHECK") |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 302 | if checklevel == 'warn': |
| 303 | warn_msgs += sstate_missing_msgs |
| 304 | elif checklevel == 'error': |
| 305 | error_msgs += sstate_missing_msgs |
| 306 | |
Andrew Geissler | 2013739 | 2023-10-12 04:59:14 -0600 | [diff] [blame] | 307 | if info_msgs: |
| 308 | bb.note(info_msgs) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 309 | if warn_msgs: |
| 310 | bb.warn("\n".join(warn_msgs)) |
| 311 | if error_msgs: |
| 312 | bb.fatal("\n".join(error_msgs)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 313 | |
Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 314 | class SignatureGeneratorOEBasicHash(SignatureGeneratorOEBasicHashMixIn, bb.siggen.SignatureGeneratorBasicHash): |
| 315 | name = "OEBasicHash" |
| 316 | |
| 317 | class SignatureGeneratorOEEquivHash(SignatureGeneratorOEBasicHashMixIn, bb.siggen.SignatureGeneratorUniHashMixIn, bb.siggen.SignatureGeneratorBasicHash): |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 318 | name = "OEEquivHash" |
| 319 | |
| 320 | def init_rundepcheck(self, data): |
| 321 | super().init_rundepcheck(data) |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 322 | self.server = data.getVar('BB_HASHSERVE') |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 323 | if not self.server: |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 324 | bb.fatal("OEEquivHash requires BB_HASHSERVE to be set") |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 325 | self.method = data.getVar('SSTATE_HASHEQUIV_METHOD') |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 326 | if not self.method: |
| 327 | bb.fatal("OEEquivHash requires SSTATE_HASHEQUIV_METHOD to be set") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 328 | |
| 329 | # Insert these classes into siggen's namespace so it can see and select them |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 330 | bb.siggen.SignatureGeneratorOEBasicHash = SignatureGeneratorOEBasicHash |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 331 | bb.siggen.SignatureGeneratorOEEquivHash = SignatureGeneratorOEEquivHash |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 332 | |
| 333 | |
| 334 | def find_siginfo(pn, taskname, taskhashlist, d): |
| 335 | """ Find signature data files for comparison purposes """ |
| 336 | |
| 337 | import fnmatch |
| 338 | import glob |
| 339 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 340 | if not taskname: |
| 341 | # We have to derive pn and taskname |
| 342 | key = pn |
Patrick Williams | 2a25492 | 2023-08-11 09:48:11 -0500 | [diff] [blame] | 343 | if key.startswith("mc:"): |
| 344 | # mc:<mc>:<pn>:<task> |
| 345 | _, _, pn, taskname = key.split(':', 3) |
| 346 | else: |
| 347 | # <pn>:<task> |
| 348 | pn, taskname = key.split(':', 1) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 349 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 350 | hashfiles = {} |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 351 | filedates = {} |
| 352 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 353 | def get_hashval(siginfo): |
| 354 | if siginfo.endswith('.siginfo'): |
| 355 | return siginfo.rpartition(':')[2].partition('_')[0] |
| 356 | else: |
| 357 | return siginfo.rpartition('.')[2] |
| 358 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 359 | # First search in stamps dir |
| 360 | localdata = d.createCopy() |
| 361 | localdata.setVar('MULTIMACH_TARGET_SYS', '*') |
| 362 | localdata.setVar('PN', pn) |
| 363 | localdata.setVar('PV', '*') |
| 364 | localdata.setVar('PR', '*') |
| 365 | localdata.setVar('EXTENDPE', '') |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 366 | stamp = localdata.getVar('STAMP') |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 367 | if pn.startswith("gcc-source"): |
| 368 | # gcc-source shared workdir is a special case :( |
| 369 | stamp = localdata.expand("${STAMPS_DIR}/work-shared/gcc-${PV}-${PR}") |
| 370 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 371 | filespec = '%s.%s.sigdata.*' % (stamp, taskname) |
| 372 | foundall = False |
| 373 | import glob |
| 374 | for fullpath in glob.glob(filespec): |
| 375 | match = False |
| 376 | if taskhashlist: |
| 377 | for taskhash in taskhashlist: |
| 378 | if fullpath.endswith('.%s' % taskhash): |
| 379 | hashfiles[taskhash] = fullpath |
| 380 | if len(hashfiles) == len(taskhashlist): |
| 381 | foundall = True |
| 382 | break |
| 383 | else: |
| 384 | try: |
| 385 | filedates[fullpath] = os.stat(fullpath).st_mtime |
| 386 | except OSError: |
| 387 | continue |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 388 | hashval = get_hashval(fullpath) |
| 389 | hashfiles[hashval] = fullpath |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 390 | |
| 391 | if not taskhashlist or (len(filedates) < 2 and not foundall): |
| 392 | # That didn't work, look in sstate-cache |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 393 | hashes = taskhashlist or ['?' * 64] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 394 | localdata = bb.data.createCopy(d) |
| 395 | for hashval in hashes: |
| 396 | localdata.setVar('PACKAGE_ARCH', '*') |
| 397 | localdata.setVar('TARGET_VENDOR', '*') |
| 398 | localdata.setVar('TARGET_OS', '*') |
| 399 | localdata.setVar('PN', pn) |
| 400 | localdata.setVar('PV', '*') |
| 401 | localdata.setVar('PR', '*') |
| 402 | localdata.setVar('BB_TASKHASH', hashval) |
Patrick Williams | 03907ee | 2022-05-01 06:28:52 -0500 | [diff] [blame] | 403 | localdata.setVar('SSTATE_CURRTASK', taskname[3:]) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 404 | swspec = localdata.getVar('SSTATE_SWSPEC') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 405 | if taskname in ['do_fetch', 'do_unpack', 'do_patch', 'do_populate_lic', 'do_preconfigure'] and swspec: |
| 406 | localdata.setVar('SSTATE_PKGSPEC', '${SSTATE_SWSPEC}') |
| 407 | elif pn.endswith('-native') or "-cross-" in pn or "-crosssdk-" in pn: |
| 408 | localdata.setVar('SSTATE_EXTRAPATH', "${NATIVELSBSTRING}/") |
Patrick Williams | 03907ee | 2022-05-01 06:28:52 -0500 | [diff] [blame] | 409 | filespec = '%s.siginfo' % localdata.getVar('SSTATE_PKG') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 410 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 411 | matchedfiles = glob.glob(filespec) |
| 412 | for fullpath in matchedfiles: |
| 413 | actual_hashval = get_hashval(fullpath) |
| 414 | if actual_hashval in hashfiles: |
| 415 | continue |
| 416 | hashfiles[hashval] = fullpath |
| 417 | if not taskhashlist: |
| 418 | try: |
| 419 | filedates[fullpath] = os.stat(fullpath).st_mtime |
| 420 | except: |
| 421 | continue |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 422 | |
| 423 | if taskhashlist: |
| 424 | return hashfiles |
| 425 | else: |
| 426 | return filedates |
| 427 | |
| 428 | bb.siggen.find_siginfo = find_siginfo |
| 429 | |
| 430 | |
| 431 | def sstate_get_manifest_filename(task, d): |
| 432 | """ |
| 433 | Return the sstate manifest file path for a particular task. |
| 434 | Also returns the datastore that can be used to query related variables. |
| 435 | """ |
| 436 | d2 = d.createCopy() |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 437 | extrainf = d.getVarFlag("do_" + task, 'stamp-extra-info') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 438 | if extrainf: |
| 439 | d2.setVar("SSTATE_MANMACH", extrainf) |
| 440 | return (d2.expand("${SSTATE_MANFILEPREFIX}.%s" % task), d2) |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 441 | |
| 442 | def find_sstate_manifest(taskdata, taskdata2, taskname, d, multilibcache): |
| 443 | d2 = d |
| 444 | variant = '' |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 445 | curr_variant = '' |
| 446 | if d.getVar("BBEXTENDCURR") == "multilib": |
| 447 | curr_variant = d.getVar("BBEXTENDVARIANT") |
| 448 | if "virtclass-multilib" not in d.getVar("OVERRIDES"): |
| 449 | curr_variant = "invalid" |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 450 | if taskdata2.startswith("virtual:multilib"): |
| 451 | variant = taskdata2.split(":")[2] |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 452 | if curr_variant != variant: |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 453 | if variant not in multilibcache: |
| 454 | multilibcache[variant] = oe.utils.get_multilib_datastore(variant, d) |
| 455 | d2 = multilibcache[variant] |
| 456 | |
| 457 | if taskdata.endswith("-native"): |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 458 | pkgarchs = ["${BUILD_ARCH}", "${BUILD_ARCH}_${ORIGNATIVELSBSTRING}"] |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 459 | elif taskdata.startswith("nativesdk-"): |
| 460 | pkgarchs = ["${SDK_ARCH}_${SDK_OS}", "allarch"] |
| 461 | elif "-cross-canadian" in taskdata: |
| 462 | pkgarchs = ["${SDK_ARCH}_${SDK_ARCH}-${SDKPKGSUFFIX}"] |
| 463 | elif "-cross-" in taskdata: |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 464 | pkgarchs = ["${BUILD_ARCH}"] |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 465 | elif "-crosssdk" in taskdata: |
| 466 | pkgarchs = ["${BUILD_ARCH}_${SDK_ARCH}_${SDK_OS}"] |
| 467 | else: |
| 468 | pkgarchs = ['${MACHINE_ARCH}'] |
| 469 | pkgarchs = pkgarchs + list(reversed(d2.getVar("PACKAGE_EXTRA_ARCHS").split())) |
| 470 | pkgarchs.append('allarch') |
| 471 | pkgarchs.append('${SDK_ARCH}_${SDK_ARCH}-${SDKPKGSUFFIX}') |
| 472 | |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 473 | searched_manifests = [] |
| 474 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 475 | for pkgarch in pkgarchs: |
| 476 | manifest = d2.expand("${SSTATE_MANIFESTS}/manifest-%s-%s.%s" % (pkgarch, taskdata, taskname)) |
| 477 | if os.path.exists(manifest): |
| 478 | return manifest, d2 |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 479 | searched_manifests.append(manifest) |
| 480 | bb.fatal("The sstate manifest for task '%s:%s' (multilib variant '%s') could not be found.\nThe pkgarchs considered were: %s.\nBut none of these manifests exists:\n %s" |
| 481 | % (taskdata, taskname, variant, d2.expand(", ".join(pkgarchs)),"\n ".join(searched_manifests))) |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 482 | return None, d2 |
| 483 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 484 | def OEOuthashBasic(path, sigfile, task, d): |
| 485 | """ |
| 486 | Basic output hash function |
| 487 | |
| 488 | Calculates the output hash of a task by hashing all output file metadata, |
| 489 | and file contents. |
| 490 | """ |
| 491 | import hashlib |
| 492 | import stat |
| 493 | import pwd |
| 494 | import grp |
Patrick Williams | 93c203f | 2021-10-06 16:15:23 -0500 | [diff] [blame] | 495 | import re |
| 496 | import fnmatch |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 497 | |
| 498 | def update_hash(s): |
| 499 | s = s.encode('utf-8') |
| 500 | h.update(s) |
| 501 | if sigfile: |
| 502 | sigfile.write(s) |
| 503 | |
| 504 | h = hashlib.sha256() |
| 505 | prev_dir = os.getcwd() |
Patrick Williams | 93c203f | 2021-10-06 16:15:23 -0500 | [diff] [blame] | 506 | corebase = d.getVar("COREBASE") |
| 507 | tmpdir = d.getVar("TMPDIR") |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 508 | include_owners = os.environ.get('PSEUDO_DISABLED') == '0' |
Andrew Geissler | f034379 | 2020-11-18 10:42:21 -0600 | [diff] [blame] | 509 | if "package_write_" in task or task == "package_qa": |
| 510 | include_owners = False |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 511 | include_timestamps = False |
Andrew Geissler | 5199d83 | 2021-09-24 16:47:35 -0500 | [diff] [blame] | 512 | include_root = True |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 513 | if task == "package": |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 514 | include_timestamps = True |
Andrew Geissler | 5199d83 | 2021-09-24 16:47:35 -0500 | [diff] [blame] | 515 | include_root = False |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 516 | hash_version = d.getVar('HASHEQUIV_HASH_VERSION') |
| 517 | extra_sigdata = d.getVar("HASHEQUIV_EXTRA_SIGDATA") |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 518 | |
Patrick Williams | 93c203f | 2021-10-06 16:15:23 -0500 | [diff] [blame] | 519 | filemaps = {} |
| 520 | for m in (d.getVar('SSTATE_HASHEQUIV_FILEMAP') or '').split(): |
| 521 | entry = m.split(":") |
| 522 | if len(entry) != 3 or entry[0] != task: |
| 523 | continue |
| 524 | filemaps.setdefault(entry[1], []) |
| 525 | filemaps[entry[1]].append(entry[2]) |
| 526 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 527 | try: |
| 528 | os.chdir(path) |
Patrick Williams | 93c203f | 2021-10-06 16:15:23 -0500 | [diff] [blame] | 529 | basepath = os.path.normpath(path) |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 530 | |
| 531 | update_hash("OEOuthashBasic\n") |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 532 | if hash_version: |
| 533 | update_hash(hash_version + "\n") |
| 534 | |
| 535 | if extra_sigdata: |
| 536 | update_hash(extra_sigdata + "\n") |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 537 | |
| 538 | # It is only currently useful to get equivalent hashes for things that |
| 539 | # can be restored from sstate. Since the sstate object is named using |
| 540 | # SSTATE_PKGSPEC and the task name, those should be included in the |
| 541 | # output hash calculation. |
| 542 | update_hash("SSTATE_PKGSPEC=%s\n" % d.getVar('SSTATE_PKGSPEC')) |
| 543 | update_hash("task=%s\n" % task) |
| 544 | |
| 545 | for root, dirs, files in os.walk('.', topdown=True): |
| 546 | # Sort directories to ensure consistent ordering when recursing |
| 547 | dirs.sort() |
| 548 | files.sort() |
| 549 | |
| 550 | def process(path): |
| 551 | s = os.lstat(path) |
| 552 | |
| 553 | if stat.S_ISDIR(s.st_mode): |
| 554 | update_hash('d') |
| 555 | elif stat.S_ISCHR(s.st_mode): |
| 556 | update_hash('c') |
| 557 | elif stat.S_ISBLK(s.st_mode): |
| 558 | update_hash('b') |
| 559 | elif stat.S_ISSOCK(s.st_mode): |
| 560 | update_hash('s') |
| 561 | elif stat.S_ISLNK(s.st_mode): |
| 562 | update_hash('l') |
| 563 | elif stat.S_ISFIFO(s.st_mode): |
| 564 | update_hash('p') |
| 565 | else: |
| 566 | update_hash('-') |
| 567 | |
| 568 | def add_perm(mask, on, off='-'): |
| 569 | if mask & s.st_mode: |
| 570 | update_hash(on) |
| 571 | else: |
| 572 | update_hash(off) |
| 573 | |
| 574 | add_perm(stat.S_IRUSR, 'r') |
| 575 | add_perm(stat.S_IWUSR, 'w') |
| 576 | if stat.S_ISUID & s.st_mode: |
| 577 | add_perm(stat.S_IXUSR, 's', 'S') |
| 578 | else: |
| 579 | add_perm(stat.S_IXUSR, 'x') |
| 580 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 581 | if include_owners: |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 582 | # Group/other permissions are only relevant in pseudo context |
| 583 | add_perm(stat.S_IRGRP, 'r') |
| 584 | add_perm(stat.S_IWGRP, 'w') |
| 585 | if stat.S_ISGID & s.st_mode: |
| 586 | add_perm(stat.S_IXGRP, 's', 'S') |
| 587 | else: |
| 588 | add_perm(stat.S_IXGRP, 'x') |
| 589 | |
| 590 | add_perm(stat.S_IROTH, 'r') |
| 591 | add_perm(stat.S_IWOTH, 'w') |
| 592 | if stat.S_ISVTX & s.st_mode: |
| 593 | update_hash('t') |
| 594 | else: |
| 595 | add_perm(stat.S_IXOTH, 'x') |
| 596 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 597 | try: |
| 598 | update_hash(" %10s" % pwd.getpwuid(s.st_uid).pw_name) |
| 599 | update_hash(" %10s" % grp.getgrgid(s.st_gid).gr_name) |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 600 | except KeyError as e: |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 601 | bb.warn("KeyError in %s" % path) |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 602 | msg = ("KeyError: %s\nPath %s is owned by uid %d, gid %d, which doesn't match " |
| 603 | "any user/group on target. This may be due to host contamination." % (e, path, s.st_uid, s.st_gid)) |
| 604 | raise Exception(msg).with_traceback(e.__traceback__) |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 605 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 606 | if include_timestamps: |
| 607 | update_hash(" %10d" % s.st_mtime) |
| 608 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 609 | update_hash(" ") |
| 610 | if stat.S_ISBLK(s.st_mode) or stat.S_ISCHR(s.st_mode): |
| 611 | update_hash("%9s" % ("%d.%d" % (os.major(s.st_rdev), os.minor(s.st_rdev)))) |
| 612 | else: |
| 613 | update_hash(" " * 9) |
| 614 | |
Patrick Williams | 93c203f | 2021-10-06 16:15:23 -0500 | [diff] [blame] | 615 | filterfile = False |
| 616 | for entry in filemaps: |
| 617 | if fnmatch.fnmatch(path, entry): |
| 618 | filterfile = True |
| 619 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 620 | update_hash(" ") |
Patrick Williams | 93c203f | 2021-10-06 16:15:23 -0500 | [diff] [blame] | 621 | if stat.S_ISREG(s.st_mode) and not filterfile: |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 622 | update_hash("%10d" % s.st_size) |
| 623 | else: |
| 624 | update_hash(" " * 10) |
| 625 | |
| 626 | update_hash(" ") |
| 627 | fh = hashlib.sha256() |
| 628 | if stat.S_ISREG(s.st_mode): |
| 629 | # Hash file contents |
Patrick Williams | 93c203f | 2021-10-06 16:15:23 -0500 | [diff] [blame] | 630 | if filterfile: |
| 631 | # Need to ignore paths in crossscripts and postinst-useradd files. |
| 632 | with open(path, 'rb') as d: |
| 633 | chunk = d.read() |
| 634 | chunk = chunk.replace(bytes(basepath, encoding='utf8'), b'') |
| 635 | for entry in filemaps: |
| 636 | if not fnmatch.fnmatch(path, entry): |
| 637 | continue |
| 638 | for r in filemaps[entry]: |
| 639 | if r.startswith("regex-"): |
| 640 | chunk = re.sub(bytes(r[6:], encoding='utf8'), b'', chunk) |
| 641 | else: |
| 642 | chunk = chunk.replace(bytes(r, encoding='utf8'), b'') |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 643 | fh.update(chunk) |
Patrick Williams | 93c203f | 2021-10-06 16:15:23 -0500 | [diff] [blame] | 644 | else: |
| 645 | with open(path, 'rb') as d: |
| 646 | for chunk in iter(lambda: d.read(4096), b""): |
| 647 | fh.update(chunk) |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 648 | update_hash(fh.hexdigest()) |
| 649 | else: |
| 650 | update_hash(" " * len(fh.hexdigest())) |
| 651 | |
| 652 | update_hash(" %s" % path) |
| 653 | |
| 654 | if stat.S_ISLNK(s.st_mode): |
| 655 | update_hash(" -> %s" % os.readlink(path)) |
| 656 | |
| 657 | update_hash("\n") |
| 658 | |
| 659 | # Process this directory and all its child files |
Andrew Geissler | 5199d83 | 2021-09-24 16:47:35 -0500 | [diff] [blame] | 660 | if include_root or root != ".": |
| 661 | process(root) |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 662 | for f in files: |
| 663 | if f == 'fixmepath': |
| 664 | continue |
| 665 | process(os.path.join(root, f)) |
Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame] | 666 | |
| 667 | for dir in dirs: |
| 668 | if os.path.islink(os.path.join(root, dir)): |
| 669 | process(os.path.join(root, dir)) |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 670 | finally: |
| 671 | os.chdir(prev_dir) |
| 672 | |
| 673 | return h.hexdigest() |
| 674 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 675 | |