blob: fad10af539843d0eadcc7cad41fb5374b5542ca2 [file] [log] [blame]
Brad Bishopc342db32019-05-15 21:57:59 -04001#
Patrick Williams92b42cb2022-09-03 06:53:57 -05002# Copyright OpenEmbedded Contributors
3#
Brad Bishopc342db32019-05-15 21:57:59 -04004# SPDX-License-Identifier: GPL-2.0-only
5#
Patrick Williamsc124f4f2015-09-15 14:41:29 -05006import bb.siggen
Andrew Geisslerd25ed322020-06-27 00:28:28 -05007import bb.runqueue
Brad Bishop316dfdd2018-06-25 12:45:53 -04008import oe
Patrick Williamsc124f4f2015-09-15 14:41:29 -05009
Andrew Geisslerd25ed322020-06-27 00:28:28 -050010def sstate_rundepfilter(siggen, fn, recipename, task, dep, depname, dataCaches):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050011 # 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 Geisslerd25ed322020-06-27 00:28:28 -050018 def isKernel(mc, fn):
19 inherits = " ".join(dataCaches[mc].inherits[fn])
Patrick Williamsc124f4f2015-09-15 14:41:29 -050020 return inherits.find("/module-base.bbclass") != -1 or inherits.find("/linux-kernel-base.bbclass") != -1
Andrew Geisslerd25ed322020-06-27 00:28:28 -050021 def isPackageGroup(mc, fn):
22 inherits = " ".join(dataCaches[mc].inherits[fn])
Patrick Williamsc124f4f2015-09-15 14:41:29 -050023 return "/packagegroup.bbclass" in inherits
Andrew Geisslerd25ed322020-06-27 00:28:28 -050024 def isAllArch(mc, fn):
25 inherits = " ".join(dataCaches[mc].inherits[fn])
Patrick Williamsc124f4f2015-09-15 14:41:29 -050026 return "/allarch.bbclass" in inherits
Andrew Geisslerd25ed322020-06-27 00:28:28 -050027 def isImage(mc, fn):
28 return "/image.bbclass" in " ".join(dataCaches[mc].inherits[fn])
Patrick Williamsdb4c27e2022-08-05 08:10:29 -050029 def isSPDXTask(task):
30 return task in ("do_create_spdx", "do_create_runtime_spdx")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050031
Andrew Geisslerd25ed322020-06-27 00:28:28 -050032 depmc, _, deptaskname, depmcfn = bb.runqueue.split_tid_mcfn(dep)
33 mc, _ = bb.runqueue.split_mc(fn)
34
Patrick Williamsdb4c27e2022-08-05 08:10:29 -050035 # Keep all dependencies between SPDX tasks in the signature. SPDX documents
36 # are linked together by hashes, which means if a dependent document changes,
37 # all downstream documents must be re-written (even if they are "safe"
38 # dependencies).
39 if isSPDXTask(task) and isSPDXTask(deptaskname):
40 return True
41
Andrew Geisslerd25ed322020-06-27 00:28:28 -050042 # (Almost) always include our own inter-task dependencies (unless it comes
43 # from a mcdepends). The exception is the special
44 # do_kernel_configme->do_unpack_and_patch dependency from archiver.bbclass.
45 if recipename == depname and depmc == mc:
46 if task == "do_kernel_configme" and deptaskname == "do_unpack_and_patch":
Brad Bishop6e60e8b2018-02-01 10:27:11 -050047 return False
Patrick Williamsc124f4f2015-09-15 14:41:29 -050048 return True
49
Patrick Williamsc124f4f2015-09-15 14:41:29 -050050 # Exclude well defined recipe->dependency
51 if "%s->%s" % (recipename, depname) in siggen.saferecipedeps:
52 return False
53
Brad Bishop316dfdd2018-06-25 12:45:53 -040054 # Check for special wildcard
55 if "*->%s" % depname in siggen.saferecipedeps and recipename != depname:
56 return False
57
Patrick Williamsc124f4f2015-09-15 14:41:29 -050058 # Don't change native/cross/nativesdk recipe dependencies any further
59 if isNative(recipename) or isCross(recipename) or isNativeSDK(recipename):
60 return True
61
62 # Only target packages beyond here
63
64 # allarch packagegroups are assumed to have well behaved names which don't change between architecures/tunes
Andrew Geisslerd25ed322020-06-27 00:28:28 -050065 if isPackageGroup(mc, fn) and isAllArch(mc, fn) and not isNative(depname):
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080066 return False
Patrick Williamsc124f4f2015-09-15 14:41:29 -050067
68 # Exclude well defined machine specific configurations which don't change ABI
Andrew Geisslerd25ed322020-06-27 00:28:28 -050069 if depname in siggen.abisaferecipes and not isImage(mc, fn):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050070 return False
71
72 # Kernel modules are well namespaced. We don't want to depend on the kernel's checksum
Patrick Williams213cb262021-08-07 19:21:33 -050073 # if we're just doing an RRECOMMENDS:xxx = "kernel-module-*", not least because the checksum
Patrick Williamsc124f4f2015-09-15 14:41:29 -050074 # is machine specific.
75 # Therefore if we're not a kernel or a module recipe (inheriting the kernel classes)
76 # and we reccomend a kernel-module, we exclude the dependency.
Andrew Geisslerd25ed322020-06-27 00:28:28 -050077 if dataCaches and isKernel(depmc, depmcfn) and not isKernel(mc, fn):
78 for pkg in dataCaches[mc].runrecs[fn]:
79 if " ".join(dataCaches[mc].runrecs[fn][pkg]).find("kernel-module-") != -1:
Patrick Williamsc124f4f2015-09-15 14:41:29 -050080 return False
81
82 # Default to keep dependencies
83 return True
84
85def sstate_lockedsigs(d):
86 sigs = {}
Brad Bishop6e60e8b2018-02-01 10:27:11 -050087 types = (d.getVar("SIGGEN_LOCKEDSIGS_TYPES") or "").split()
Patrick Williamsc124f4f2015-09-15 14:41:29 -050088 for t in types:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050089 siggen_lockedsigs_var = "SIGGEN_LOCKEDSIGS_%s" % t
Brad Bishop6e60e8b2018-02-01 10:27:11 -050090 lockedsigs = (d.getVar(siggen_lockedsigs_var) or "").split()
Patrick Williamsc124f4f2015-09-15 14:41:29 -050091 for ls in lockedsigs:
92 pn, task, h = ls.split(":", 2)
93 if pn not in sigs:
94 sigs[pn] = {}
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050095 sigs[pn][task] = [h, siggen_lockedsigs_var]
Patrick Williamsc124f4f2015-09-15 14:41:29 -050096 return sigs
97
98class SignatureGeneratorOEBasic(bb.siggen.SignatureGeneratorBasic):
99 name = "OEBasic"
100 def init_rundepcheck(self, data):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500101 self.abisaferecipes = (data.getVar("SIGGEN_EXCLUDERECIPES_ABISAFE") or "").split()
102 self.saferecipedeps = (data.getVar("SIGGEN_EXCLUDE_SAFE_RECIPE_DEPS") or "").split()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500103 pass
Andrew Geisslerd25ed322020-06-27 00:28:28 -0500104 def rundep_check(self, fn, recipename, task, dep, depname, dataCaches = None):
105 return sstate_rundepfilter(self, fn, recipename, task, dep, depname, dataCaches)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500106
Brad Bishop00e122a2019-10-05 11:10:57 -0400107class SignatureGeneratorOEBasicHashMixIn(object):
Andrew Geisslerd25ed322020-06-27 00:28:28 -0500108 supports_multiconfig_datacaches = True
109
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500110 def init_rundepcheck(self, data):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500111 self.abisaferecipes = (data.getVar("SIGGEN_EXCLUDERECIPES_ABISAFE") or "").split()
112 self.saferecipedeps = (data.getVar("SIGGEN_EXCLUDE_SAFE_RECIPE_DEPS") or "").split()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500113 self.lockedsigs = sstate_lockedsigs(data)
114 self.lockedhashes = {}
115 self.lockedpnmap = {}
116 self.lockedhashfn = {}
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500117 self.machine = data.getVar("MACHINE")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500118 self.mismatch_msgs = []
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500119 self.unlockedrecipes = (data.getVar("SIGGEN_UNLOCKED_RECIPES") or
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500120 "").split()
121 self.unlockedrecipes = { k: "" for k in self.unlockedrecipes }
Andrew Geissler82c905d2020-04-13 13:39:40 -0500122 self._internal = False
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500123 pass
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500124
125 def tasks_resolved(self, virtmap, virtpnmap, dataCache):
126 # Translate virtual/xxx entries to PN values
127 newabisafe = []
128 for a in self.abisaferecipes:
129 if a in virtpnmap:
130 newabisafe.append(virtpnmap[a])
131 else:
132 newabisafe.append(a)
133 self.abisaferecipes = newabisafe
134 newsafedeps = []
135 for a in self.saferecipedeps:
136 a1, a2 = a.split("->")
137 if a1 in virtpnmap:
138 a1 = virtpnmap[a1]
139 if a2 in virtpnmap:
140 a2 = virtpnmap[a2]
141 newsafedeps.append(a1 + "->" + a2)
142 self.saferecipedeps = newsafedeps
143
Andrew Geisslerd25ed322020-06-27 00:28:28 -0500144 def rundep_check(self, fn, recipename, task, dep, depname, dataCaches = None):
145 return sstate_rundepfilter(self, fn, recipename, task, dep, depname, dataCaches)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500146
147 def get_taskdata(self):
Brad Bishop00e122a2019-10-05 11:10:57 -0400148 return (self.lockedpnmap, self.lockedhashfn, self.lockedhashes) + super().get_taskdata()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500149
150 def set_taskdata(self, data):
Brad Bishop00e122a2019-10-05 11:10:57 -0400151 self.lockedpnmap, self.lockedhashfn, self.lockedhashes = data[:3]
152 super().set_taskdata(data[3:])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500153
154 def dump_sigs(self, dataCache, options):
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600155 sigfile = os.getcwd() + "/locked-sigs.inc"
156 bb.plain("Writing locked sigs to %s" % sigfile)
157 self.dump_lockedsigs(sigfile)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500158 return super(bb.siggen.SignatureGeneratorBasicHash, self).dump_sigs(dataCache, options)
159
Andrew Geissler82c905d2020-04-13 13:39:40 -0500160
Andrew Geisslerd25ed322020-06-27 00:28:28 -0500161 def get_taskhash(self, tid, deps, dataCaches):
Andrew Geissler82c905d2020-04-13 13:39:40 -0500162 if tid in self.lockedhashes:
163 if self.lockedhashes[tid]:
164 return self.lockedhashes[tid]
165 else:
Andrew Geisslerd25ed322020-06-27 00:28:28 -0500166 return super().get_taskhash(tid, deps, dataCaches)
Andrew Geissler82c905d2020-04-13 13:39:40 -0500167
Andrew Geisslerd25ed322020-06-27 00:28:28 -0500168 h = super().get_taskhash(tid, deps, dataCaches)
Brad Bishop08902b02019-08-20 09:16:51 -0400169
170 (mc, _, task, fn) = bb.runqueue.split_tid_mcfn(tid)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500171
Andrew Geisslerd25ed322020-06-27 00:28:28 -0500172 recipename = dataCaches[mc].pkg_fn[fn]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500173 self.lockedpnmap[fn] = recipename
Andrew Geisslerd25ed322020-06-27 00:28:28 -0500174 self.lockedhashfn[fn] = dataCaches[mc].hashfn[fn]
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500175
176 unlocked = False
177 if recipename in self.unlockedrecipes:
178 unlocked = True
179 else:
180 def recipename_from_dep(dep):
Andrew Geisslerd25ed322020-06-27 00:28:28 -0500181 (depmc, _, _, depfn) = bb.runqueue.split_tid_mcfn(dep)
182 return dataCaches[depmc].pkg_fn[depfn]
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500183
184 # If any unlocked recipe is in the direct dependencies then the
185 # current recipe should be unlocked as well.
Brad Bishop08902b02019-08-20 09:16:51 -0400186 depnames = [ recipename_from_dep(x) for x in deps if mc == bb.runqueue.mc_from_tid(x)]
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500187 if any(x in y for y in depnames for x in self.unlockedrecipes):
188 self.unlockedrecipes[recipename] = ''
189 unlocked = True
190
191 if not unlocked and recipename in self.lockedsigs:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500192 if task in self.lockedsigs[recipename]:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500193 h_locked = self.lockedsigs[recipename][task][0]
194 var = self.lockedsigs[recipename][task][1]
Brad Bishop08902b02019-08-20 09:16:51 -0400195 self.lockedhashes[tid] = h_locked
Andrew Geissler82c905d2020-04-13 13:39:40 -0500196 self._internal = True
197 unihash = self.get_unihash(tid)
198 self._internal = False
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500199 #bb.warn("Using %s %s %s" % (recipename, task, h))
200
Brad Bishop00e122a2019-10-05 11:10:57 -0400201 if h != h_locked and h_locked != unihash:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500202 self.mismatch_msgs.append('The %s:%s sig is computed to be %s, but the sig is locked to %s in %s'
203 % (recipename, task, h, h_locked, var))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500204
205 return h_locked
Andrew Geissler82c905d2020-04-13 13:39:40 -0500206
207 self.lockedhashes[tid] = False
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500208 #bb.warn("%s %s %s" % (recipename, task, h))
209 return h
210
Andrew Geissler82c905d2020-04-13 13:39:40 -0500211 def get_stampfile_hash(self, tid):
212 if tid in self.lockedhashes and self.lockedhashes[tid]:
213 return self.lockedhashes[tid]
214 return super().get_stampfile_hash(tid)
215
Brad Bishop00e122a2019-10-05 11:10:57 -0400216 def get_unihash(self, tid):
Andrew Geissler82c905d2020-04-13 13:39:40 -0500217 if tid in self.lockedhashes and self.lockedhashes[tid] and not self._internal:
Brad Bishop00e122a2019-10-05 11:10:57 -0400218 return self.lockedhashes[tid]
219 return super().get_unihash(tid)
220
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500221 def dump_sigtask(self, fn, task, stampbase, runtime):
Brad Bishop08902b02019-08-20 09:16:51 -0400222 tid = fn + ":" + task
Andrew Geissler82c905d2020-04-13 13:39:40 -0500223 if tid in self.lockedhashes and self.lockedhashes[tid]:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500224 return
225 super(bb.siggen.SignatureGeneratorBasicHash, self).dump_sigtask(fn, task, stampbase, runtime)
226
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600227 def dump_lockedsigs(self, sigfile, taskfilter=None):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500228 types = {}
Brad Bishop08902b02019-08-20 09:16:51 -0400229 for tid in self.runtaskdeps:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500230 if taskfilter:
Brad Bishop08902b02019-08-20 09:16:51 -0400231 if not tid in taskfilter:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500232 continue
Brad Bishop08902b02019-08-20 09:16:51 -0400233 fn = bb.runqueue.fn_from_tid(tid)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500234 t = self.lockedhashfn[fn].split(" ")[1].split(":")[5]
235 t = 't-' + t.replace('_', '-')
236 if t not in types:
237 types[t] = []
Brad Bishop08902b02019-08-20 09:16:51 -0400238 types[t].append(tid)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500239
240 with open(sigfile, "w") as f:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500241 l = sorted(types)
242 for t in l:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500243 f.write('SIGGEN_LOCKEDSIGS_%s = "\\\n' % t)
244 types[t].sort()
Brad Bishop08902b02019-08-20 09:16:51 -0400245 sortedtid = sorted(types[t], key=lambda tid: self.lockedpnmap[bb.runqueue.fn_from_tid(tid)])
246 for tid in sortedtid:
247 (_, _, task, fn) = bb.runqueue.split_tid_mcfn(tid)
248 if tid not in self.taskhash:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500249 continue
Brad Bishop00e122a2019-10-05 11:10:57 -0400250 f.write(" " + self.lockedpnmap[fn] + ":" + task + ":" + self.get_unihash(tid) + " \\\n")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500251 f.write(' "\n')
Patrick Williams213cb262021-08-07 19:21:33 -0500252 f.write('SIGGEN_LOCKEDSIGS_TYPES:%s = "%s"' % (self.machine, " ".join(l)))
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500253
Andrew Geissler09036742021-06-25 14:25:14 -0500254 def dump_siglist(self, sigfile, path_prefix_strip=None):
255 def strip_fn(fn):
256 nonlocal path_prefix_strip
257 if not path_prefix_strip:
258 return fn
259
260 fn_exp = fn.split(":")
261 if fn_exp[-1].startswith(path_prefix_strip):
262 fn_exp[-1] = fn_exp[-1][len(path_prefix_strip):]
263
264 return ":".join(fn_exp)
265
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500266 with open(sigfile, "w") as f:
267 tasks = []
268 for taskitem in self.taskhash:
Brad Bishop08902b02019-08-20 09:16:51 -0400269 (fn, task) = taskitem.rsplit(":", 1)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500270 pn = self.lockedpnmap[fn]
Andrew Geissler09036742021-06-25 14:25:14 -0500271 tasks.append((pn, task, strip_fn(fn), self.taskhash[taskitem]))
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500272 for (pn, task, fn, taskhash) in sorted(tasks):
Brad Bishop08902b02019-08-20 09:16:51 -0400273 f.write('%s:%s %s %s\n' % (pn, task, fn, taskhash))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500274
Brad Bishop08902b02019-08-20 09:16:51 -0400275 def checkhashes(self, sq_data, missed, found, d):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500276 warn_msgs = []
277 error_msgs = []
278 sstate_missing_msgs = []
279
Brad Bishop08902b02019-08-20 09:16:51 -0400280 for tid in sq_data['hash']:
281 if tid not in found:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500282 for pn in self.lockedsigs:
Brad Bishop08902b02019-08-20 09:16:51 -0400283 taskname = bb.runqueue.taskname_from_tid(tid)
284 if sq_data['hash'][tid] in iter(self.lockedsigs[pn].values()):
285 if taskname == 'do_shared_workdir':
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500286 continue
287 sstate_missing_msgs.append("Locked sig is set for %s:%s (%s) yet not in sstate cache?"
Brad Bishop08902b02019-08-20 09:16:51 -0400288 % (pn, taskname, sq_data['hash'][tid]))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500289
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500290 checklevel = d.getVar("SIGGEN_LOCKEDSIGS_TASKSIG_CHECK")
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500291 if checklevel == 'warn':
292 warn_msgs += self.mismatch_msgs
293 elif checklevel == 'error':
294 error_msgs += self.mismatch_msgs
295
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500296 checklevel = d.getVar("SIGGEN_LOCKEDSIGS_SSTATE_EXISTS_CHECK")
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500297 if checklevel == 'warn':
298 warn_msgs += sstate_missing_msgs
299 elif checklevel == 'error':
300 error_msgs += sstate_missing_msgs
301
302 if warn_msgs:
303 bb.warn("\n".join(warn_msgs))
304 if error_msgs:
305 bb.fatal("\n".join(error_msgs))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500306
Brad Bishop00e122a2019-10-05 11:10:57 -0400307class SignatureGeneratorOEBasicHash(SignatureGeneratorOEBasicHashMixIn, bb.siggen.SignatureGeneratorBasicHash):
308 name = "OEBasicHash"
309
310class SignatureGeneratorOEEquivHash(SignatureGeneratorOEBasicHashMixIn, bb.siggen.SignatureGeneratorUniHashMixIn, bb.siggen.SignatureGeneratorBasicHash):
Brad Bishop19323692019-04-05 15:28:33 -0400311 name = "OEEquivHash"
312
313 def init_rundepcheck(self, data):
314 super().init_rundepcheck(data)
Brad Bishopa34c0302019-09-23 22:34:48 -0400315 self.server = data.getVar('BB_HASHSERVE')
Brad Bishop08902b02019-08-20 09:16:51 -0400316 if not self.server:
Brad Bishopa34c0302019-09-23 22:34:48 -0400317 bb.fatal("OEEquivHash requires BB_HASHSERVE to be set")
Brad Bishop19323692019-04-05 15:28:33 -0400318 self.method = data.getVar('SSTATE_HASHEQUIV_METHOD')
Brad Bishop08902b02019-08-20 09:16:51 -0400319 if not self.method:
320 bb.fatal("OEEquivHash requires SSTATE_HASHEQUIV_METHOD to be set")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500321
322# Insert these classes into siggen's namespace so it can see and select them
323bb.siggen.SignatureGeneratorOEBasic = SignatureGeneratorOEBasic
324bb.siggen.SignatureGeneratorOEBasicHash = SignatureGeneratorOEBasicHash
Brad Bishop19323692019-04-05 15:28:33 -0400325bb.siggen.SignatureGeneratorOEEquivHash = SignatureGeneratorOEEquivHash
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500326
327
328def find_siginfo(pn, taskname, taskhashlist, d):
329 """ Find signature data files for comparison purposes """
330
331 import fnmatch
332 import glob
333
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500334 if not taskname:
335 # We have to derive pn and taskname
336 key = pn
Brad Bishop08902b02019-08-20 09:16:51 -0400337 splitit = key.split('.bb:')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500338 taskname = splitit[1]
339 pn = os.path.basename(splitit[0]).split('_')[0]
340 if key.startswith('virtual:native:'):
341 pn = pn + '-native'
342
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500343 hashfiles = {}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500344 filedates = {}
345
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500346 def get_hashval(siginfo):
347 if siginfo.endswith('.siginfo'):
348 return siginfo.rpartition(':')[2].partition('_')[0]
349 else:
350 return siginfo.rpartition('.')[2]
351
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500352 # First search in stamps dir
353 localdata = d.createCopy()
354 localdata.setVar('MULTIMACH_TARGET_SYS', '*')
355 localdata.setVar('PN', pn)
356 localdata.setVar('PV', '*')
357 localdata.setVar('PR', '*')
358 localdata.setVar('EXTENDPE', '')
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500359 stamp = localdata.getVar('STAMP')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500360 if pn.startswith("gcc-source"):
361 # gcc-source shared workdir is a special case :(
362 stamp = localdata.expand("${STAMPS_DIR}/work-shared/gcc-${PV}-${PR}")
363
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500364 filespec = '%s.%s.sigdata.*' % (stamp, taskname)
365 foundall = False
366 import glob
367 for fullpath in glob.glob(filespec):
368 match = False
369 if taskhashlist:
370 for taskhash in taskhashlist:
371 if fullpath.endswith('.%s' % taskhash):
372 hashfiles[taskhash] = fullpath
373 if len(hashfiles) == len(taskhashlist):
374 foundall = True
375 break
376 else:
377 try:
378 filedates[fullpath] = os.stat(fullpath).st_mtime
379 except OSError:
380 continue
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500381 hashval = get_hashval(fullpath)
382 hashfiles[hashval] = fullpath
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500383
384 if not taskhashlist or (len(filedates) < 2 and not foundall):
385 # That didn't work, look in sstate-cache
Brad Bishop19323692019-04-05 15:28:33 -0400386 hashes = taskhashlist or ['?' * 64]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500387 localdata = bb.data.createCopy(d)
388 for hashval in hashes:
389 localdata.setVar('PACKAGE_ARCH', '*')
390 localdata.setVar('TARGET_VENDOR', '*')
391 localdata.setVar('TARGET_OS', '*')
392 localdata.setVar('PN', pn)
393 localdata.setVar('PV', '*')
394 localdata.setVar('PR', '*')
395 localdata.setVar('BB_TASKHASH', hashval)
Patrick Williams03907ee2022-05-01 06:28:52 -0500396 localdata.setVar('SSTATE_CURRTASK', taskname[3:])
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500397 swspec = localdata.getVar('SSTATE_SWSPEC')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500398 if taskname in ['do_fetch', 'do_unpack', 'do_patch', 'do_populate_lic', 'do_preconfigure'] and swspec:
399 localdata.setVar('SSTATE_PKGSPEC', '${SSTATE_SWSPEC}')
400 elif pn.endswith('-native') or "-cross-" in pn or "-crosssdk-" in pn:
401 localdata.setVar('SSTATE_EXTRAPATH', "${NATIVELSBSTRING}/")
Patrick Williams03907ee2022-05-01 06:28:52 -0500402 filespec = '%s.siginfo' % localdata.getVar('SSTATE_PKG')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500403
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500404 matchedfiles = glob.glob(filespec)
405 for fullpath in matchedfiles:
406 actual_hashval = get_hashval(fullpath)
407 if actual_hashval in hashfiles:
408 continue
409 hashfiles[hashval] = fullpath
410 if not taskhashlist:
411 try:
412 filedates[fullpath] = os.stat(fullpath).st_mtime
413 except:
414 continue
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500415
416 if taskhashlist:
417 return hashfiles
418 else:
419 return filedates
420
421bb.siggen.find_siginfo = find_siginfo
422
423
424def sstate_get_manifest_filename(task, d):
425 """
426 Return the sstate manifest file path for a particular task.
427 Also returns the datastore that can be used to query related variables.
428 """
429 d2 = d.createCopy()
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500430 extrainf = d.getVarFlag("do_" + task, 'stamp-extra-info')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500431 if extrainf:
432 d2.setVar("SSTATE_MANMACH", extrainf)
433 return (d2.expand("${SSTATE_MANFILEPREFIX}.%s" % task), d2)
Brad Bishop316dfdd2018-06-25 12:45:53 -0400434
435def find_sstate_manifest(taskdata, taskdata2, taskname, d, multilibcache):
436 d2 = d
437 variant = ''
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800438 curr_variant = ''
439 if d.getVar("BBEXTENDCURR") == "multilib":
440 curr_variant = d.getVar("BBEXTENDVARIANT")
441 if "virtclass-multilib" not in d.getVar("OVERRIDES"):
442 curr_variant = "invalid"
Brad Bishop316dfdd2018-06-25 12:45:53 -0400443 if taskdata2.startswith("virtual:multilib"):
444 variant = taskdata2.split(":")[2]
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800445 if curr_variant != variant:
Brad Bishop316dfdd2018-06-25 12:45:53 -0400446 if variant not in multilibcache:
447 multilibcache[variant] = oe.utils.get_multilib_datastore(variant, d)
448 d2 = multilibcache[variant]
449
450 if taskdata.endswith("-native"):
Andrew Geissler6ce62a22020-11-30 19:58:47 -0600451 pkgarchs = ["${BUILD_ARCH}", "${BUILD_ARCH}_${ORIGNATIVELSBSTRING}"]
Brad Bishop316dfdd2018-06-25 12:45:53 -0400452 elif taskdata.startswith("nativesdk-"):
453 pkgarchs = ["${SDK_ARCH}_${SDK_OS}", "allarch"]
454 elif "-cross-canadian" in taskdata:
455 pkgarchs = ["${SDK_ARCH}_${SDK_ARCH}-${SDKPKGSUFFIX}"]
456 elif "-cross-" in taskdata:
Andrew Geissler9aee5002022-03-30 16:27:02 +0000457 pkgarchs = ["${BUILD_ARCH}"]
Brad Bishop316dfdd2018-06-25 12:45:53 -0400458 elif "-crosssdk" in taskdata:
459 pkgarchs = ["${BUILD_ARCH}_${SDK_ARCH}_${SDK_OS}"]
460 else:
461 pkgarchs = ['${MACHINE_ARCH}']
462 pkgarchs = pkgarchs + list(reversed(d2.getVar("PACKAGE_EXTRA_ARCHS").split()))
463 pkgarchs.append('allarch')
464 pkgarchs.append('${SDK_ARCH}_${SDK_ARCH}-${SDKPKGSUFFIX}')
465
466 for pkgarch in pkgarchs:
467 manifest = d2.expand("${SSTATE_MANIFESTS}/manifest-%s-%s.%s" % (pkgarch, taskdata, taskname))
468 if os.path.exists(manifest):
469 return manifest, d2
William A. Kennington IIIac69b482021-06-02 12:28:27 -0700470 bb.fatal("Manifest %s not found in %s (variant '%s')?" % (manifest, d2.expand(" ".join(pkgarchs)), variant))
Brad Bishop316dfdd2018-06-25 12:45:53 -0400471 return None, d2
472
Brad Bishop19323692019-04-05 15:28:33 -0400473def OEOuthashBasic(path, sigfile, task, d):
474 """
475 Basic output hash function
476
477 Calculates the output hash of a task by hashing all output file metadata,
478 and file contents.
479 """
480 import hashlib
481 import stat
482 import pwd
483 import grp
Patrick Williams93c203f2021-10-06 16:15:23 -0500484 import re
485 import fnmatch
Brad Bishop19323692019-04-05 15:28:33 -0400486
487 def update_hash(s):
488 s = s.encode('utf-8')
489 h.update(s)
490 if sigfile:
491 sigfile.write(s)
492
493 h = hashlib.sha256()
494 prev_dir = os.getcwd()
Patrick Williams93c203f2021-10-06 16:15:23 -0500495 corebase = d.getVar("COREBASE")
496 tmpdir = d.getVar("TMPDIR")
Brad Bishop19323692019-04-05 15:28:33 -0400497 include_owners = os.environ.get('PSEUDO_DISABLED') == '0'
Andrew Geisslerf0343792020-11-18 10:42:21 -0600498 if "package_write_" in task or task == "package_qa":
499 include_owners = False
Andrew Geissler6ce62a22020-11-30 19:58:47 -0600500 include_timestamps = False
Andrew Geissler5199d832021-09-24 16:47:35 -0500501 include_root = True
Andrew Geissler6ce62a22020-11-30 19:58:47 -0600502 if task == "package":
Andrew Geisslereff27472021-10-29 15:35:00 -0500503 include_timestamps = True
Andrew Geissler5199d832021-09-24 16:47:35 -0500504 include_root = False
Andrew Geissler595f6302022-01-24 19:11:47 +0000505 hash_version = d.getVar('HASHEQUIV_HASH_VERSION')
506 extra_sigdata = d.getVar("HASHEQUIV_EXTRA_SIGDATA")
Brad Bishop19323692019-04-05 15:28:33 -0400507
Patrick Williams93c203f2021-10-06 16:15:23 -0500508 filemaps = {}
509 for m in (d.getVar('SSTATE_HASHEQUIV_FILEMAP') or '').split():
510 entry = m.split(":")
511 if len(entry) != 3 or entry[0] != task:
512 continue
513 filemaps.setdefault(entry[1], [])
514 filemaps[entry[1]].append(entry[2])
515
Brad Bishop19323692019-04-05 15:28:33 -0400516 try:
517 os.chdir(path)
Patrick Williams93c203f2021-10-06 16:15:23 -0500518 basepath = os.path.normpath(path)
Brad Bishop19323692019-04-05 15:28:33 -0400519
520 update_hash("OEOuthashBasic\n")
Andrew Geissler595f6302022-01-24 19:11:47 +0000521 if hash_version:
522 update_hash(hash_version + "\n")
523
524 if extra_sigdata:
525 update_hash(extra_sigdata + "\n")
Brad Bishop19323692019-04-05 15:28:33 -0400526
527 # It is only currently useful to get equivalent hashes for things that
528 # can be restored from sstate. Since the sstate object is named using
529 # SSTATE_PKGSPEC and the task name, those should be included in the
530 # output hash calculation.
531 update_hash("SSTATE_PKGSPEC=%s\n" % d.getVar('SSTATE_PKGSPEC'))
532 update_hash("task=%s\n" % task)
533
534 for root, dirs, files in os.walk('.', topdown=True):
535 # Sort directories to ensure consistent ordering when recursing
536 dirs.sort()
537 files.sort()
538
539 def process(path):
540 s = os.lstat(path)
541
542 if stat.S_ISDIR(s.st_mode):
543 update_hash('d')
544 elif stat.S_ISCHR(s.st_mode):
545 update_hash('c')
546 elif stat.S_ISBLK(s.st_mode):
547 update_hash('b')
548 elif stat.S_ISSOCK(s.st_mode):
549 update_hash('s')
550 elif stat.S_ISLNK(s.st_mode):
551 update_hash('l')
552 elif stat.S_ISFIFO(s.st_mode):
553 update_hash('p')
554 else:
555 update_hash('-')
556
557 def add_perm(mask, on, off='-'):
558 if mask & s.st_mode:
559 update_hash(on)
560 else:
561 update_hash(off)
562
563 add_perm(stat.S_IRUSR, 'r')
564 add_perm(stat.S_IWUSR, 'w')
565 if stat.S_ISUID & s.st_mode:
566 add_perm(stat.S_IXUSR, 's', 'S')
567 else:
568 add_perm(stat.S_IXUSR, 'x')
569
Brad Bishop19323692019-04-05 15:28:33 -0400570 if include_owners:
Andrew Geisslereff27472021-10-29 15:35:00 -0500571 # Group/other permissions are only relevant in pseudo context
572 add_perm(stat.S_IRGRP, 'r')
573 add_perm(stat.S_IWGRP, 'w')
574 if stat.S_ISGID & s.st_mode:
575 add_perm(stat.S_IXGRP, 's', 'S')
576 else:
577 add_perm(stat.S_IXGRP, 'x')
578
579 add_perm(stat.S_IROTH, 'r')
580 add_perm(stat.S_IWOTH, 'w')
581 if stat.S_ISVTX & s.st_mode:
582 update_hash('t')
583 else:
584 add_perm(stat.S_IXOTH, 'x')
585
Andrew Geissler82c905d2020-04-13 13:39:40 -0500586 try:
587 update_hash(" %10s" % pwd.getpwuid(s.st_uid).pw_name)
588 update_hash(" %10s" % grp.getgrgid(s.st_gid).gr_name)
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600589 except KeyError as e:
Andrew Geissler82c905d2020-04-13 13:39:40 -0500590 bb.warn("KeyError in %s" % path)
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600591 msg = ("KeyError: %s\nPath %s is owned by uid %d, gid %d, which doesn't match "
592 "any user/group on target. This may be due to host contamination." % (e, path, s.st_uid, s.st_gid))
593 raise Exception(msg).with_traceback(e.__traceback__)
Brad Bishop19323692019-04-05 15:28:33 -0400594
Andrew Geissler6ce62a22020-11-30 19:58:47 -0600595 if include_timestamps:
596 update_hash(" %10d" % s.st_mtime)
597
Brad Bishop19323692019-04-05 15:28:33 -0400598 update_hash(" ")
599 if stat.S_ISBLK(s.st_mode) or stat.S_ISCHR(s.st_mode):
600 update_hash("%9s" % ("%d.%d" % (os.major(s.st_rdev), os.minor(s.st_rdev))))
601 else:
602 update_hash(" " * 9)
603
Patrick Williams93c203f2021-10-06 16:15:23 -0500604 filterfile = False
605 for entry in filemaps:
606 if fnmatch.fnmatch(path, entry):
607 filterfile = True
608
Brad Bishop19323692019-04-05 15:28:33 -0400609 update_hash(" ")
Patrick Williams93c203f2021-10-06 16:15:23 -0500610 if stat.S_ISREG(s.st_mode) and not filterfile:
Brad Bishop19323692019-04-05 15:28:33 -0400611 update_hash("%10d" % s.st_size)
612 else:
613 update_hash(" " * 10)
614
615 update_hash(" ")
616 fh = hashlib.sha256()
617 if stat.S_ISREG(s.st_mode):
618 # Hash file contents
Patrick Williams93c203f2021-10-06 16:15:23 -0500619 if filterfile:
620 # Need to ignore paths in crossscripts and postinst-useradd files.
621 with open(path, 'rb') as d:
622 chunk = d.read()
623 chunk = chunk.replace(bytes(basepath, encoding='utf8'), b'')
624 for entry in filemaps:
625 if not fnmatch.fnmatch(path, entry):
626 continue
627 for r in filemaps[entry]:
628 if r.startswith("regex-"):
629 chunk = re.sub(bytes(r[6:], encoding='utf8'), b'', chunk)
630 else:
631 chunk = chunk.replace(bytes(r, encoding='utf8'), b'')
Brad Bishop19323692019-04-05 15:28:33 -0400632 fh.update(chunk)
Patrick Williams93c203f2021-10-06 16:15:23 -0500633 else:
634 with open(path, 'rb') as d:
635 for chunk in iter(lambda: d.read(4096), b""):
636 fh.update(chunk)
Brad Bishop19323692019-04-05 15:28:33 -0400637 update_hash(fh.hexdigest())
638 else:
639 update_hash(" " * len(fh.hexdigest()))
640
641 update_hash(" %s" % path)
642
643 if stat.S_ISLNK(s.st_mode):
644 update_hash(" -> %s" % os.readlink(path))
645
646 update_hash("\n")
647
648 # Process this directory and all its child files
Andrew Geissler5199d832021-09-24 16:47:35 -0500649 if include_root or root != ".":
650 process(root)
Brad Bishop19323692019-04-05 15:28:33 -0400651 for f in files:
652 if f == 'fixmepath':
653 continue
654 process(os.path.join(root, f))
655 finally:
656 os.chdir(prev_dir)
657
658 return h.hexdigest()
659
Brad Bishop316dfdd2018-06-25 12:45:53 -0400660