blob: de652449320b590674ffaa99e2db9e36a526b2d1 [file] [log] [blame]
Brad Bishopc342db32019-05-15 21:57:59 -04001#
2# SPDX-License-Identifier: GPL-2.0-only
3#
Patrick Williamsc124f4f2015-09-15 14:41:29 -05004import bb.siggen
Andrew Geisslerd25ed322020-06-27 00:28:28 -05005import bb.runqueue
Brad Bishop316dfdd2018-06-25 12:45:53 -04006import oe
Patrick Williamsc124f4f2015-09-15 14:41:29 -05007
Andrew Geisslerd25ed322020-06-27 00:28:28 -05008def sstate_rundepfilter(siggen, fn, recipename, task, dep, depname, dataCaches):
Patrick Williamsc124f4f2015-09-15 14:41:29 -05009 # Return True if we should keep the dependency, False to drop it
10 def isNative(x):
11 return x.endswith("-native")
12 def isCross(x):
13 return "-cross-" in x
14 def isNativeSDK(x):
15 return x.startswith("nativesdk-")
Andrew Geisslerd25ed322020-06-27 00:28:28 -050016 def isKernel(mc, fn):
17 inherits = " ".join(dataCaches[mc].inherits[fn])
Patrick Williamsc124f4f2015-09-15 14:41:29 -050018 return inherits.find("/module-base.bbclass") != -1 or inherits.find("/linux-kernel-base.bbclass") != -1
Andrew Geisslerd25ed322020-06-27 00:28:28 -050019 def isPackageGroup(mc, fn):
20 inherits = " ".join(dataCaches[mc].inherits[fn])
Patrick Williamsc124f4f2015-09-15 14:41:29 -050021 return "/packagegroup.bbclass" in inherits
Andrew Geisslerd25ed322020-06-27 00:28:28 -050022 def isAllArch(mc, fn):
23 inherits = " ".join(dataCaches[mc].inherits[fn])
Patrick Williamsc124f4f2015-09-15 14:41:29 -050024 return "/allarch.bbclass" in inherits
Andrew Geisslerd25ed322020-06-27 00:28:28 -050025 def isImage(mc, fn):
26 return "/image.bbclass" in " ".join(dataCaches[mc].inherits[fn])
Patrick Williamsdb4c27e2022-08-05 08:10:29 -050027 def isSPDXTask(task):
28 return task in ("do_create_spdx", "do_create_runtime_spdx")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050029
Andrew Geisslerd25ed322020-06-27 00:28:28 -050030 depmc, _, deptaskname, depmcfn = bb.runqueue.split_tid_mcfn(dep)
31 mc, _ = bb.runqueue.split_mc(fn)
32
Patrick Williamsdb4c27e2022-08-05 08:10:29 -050033 # Keep all dependencies between SPDX tasks in the signature. SPDX documents
34 # are linked together by hashes, which means if a dependent document changes,
35 # all downstream documents must be re-written (even if they are "safe"
36 # dependencies).
37 if isSPDXTask(task) and isSPDXTask(deptaskname):
38 return True
39
Andrew Geisslerd25ed322020-06-27 00:28:28 -050040 # (Almost) always include our own inter-task dependencies (unless it comes
41 # from a mcdepends). The exception is the special
42 # do_kernel_configme->do_unpack_and_patch dependency from archiver.bbclass.
43 if recipename == depname and depmc == mc:
44 if task == "do_kernel_configme" and deptaskname == "do_unpack_and_patch":
Brad Bishop6e60e8b2018-02-01 10:27:11 -050045 return False
Patrick Williamsc124f4f2015-09-15 14:41:29 -050046 return True
47
Patrick Williamsc124f4f2015-09-15 14:41:29 -050048 # Exclude well defined recipe->dependency
49 if "%s->%s" % (recipename, depname) in siggen.saferecipedeps:
50 return False
51
Brad Bishop316dfdd2018-06-25 12:45:53 -040052 # Check for special wildcard
53 if "*->%s" % depname in siggen.saferecipedeps and recipename != depname:
54 return False
55
Patrick Williamsc124f4f2015-09-15 14:41:29 -050056 # Don't change native/cross/nativesdk recipe dependencies any further
57 if isNative(recipename) or isCross(recipename) or isNativeSDK(recipename):
58 return True
59
60 # Only target packages beyond here
61
62 # allarch packagegroups are assumed to have well behaved names which don't change between architecures/tunes
Andrew Geisslerd25ed322020-06-27 00:28:28 -050063 if isPackageGroup(mc, fn) and isAllArch(mc, fn) and not isNative(depname):
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080064 return False
Patrick Williamsc124f4f2015-09-15 14:41:29 -050065
66 # Exclude well defined machine specific configurations which don't change ABI
Andrew Geisslerd25ed322020-06-27 00:28:28 -050067 if depname in siggen.abisaferecipes and not isImage(mc, fn):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050068 return False
69
70 # Kernel modules are well namespaced. We don't want to depend on the kernel's checksum
Patrick Williams213cb262021-08-07 19:21:33 -050071 # if we're just doing an RRECOMMENDS:xxx = "kernel-module-*", not least because the checksum
Patrick Williamsc124f4f2015-09-15 14:41:29 -050072 # is machine specific.
73 # Therefore if we're not a kernel or a module recipe (inheriting the kernel classes)
74 # and we reccomend a kernel-module, we exclude the dependency.
Andrew Geisslerd25ed322020-06-27 00:28:28 -050075 if dataCaches and isKernel(depmc, depmcfn) and not isKernel(mc, fn):
76 for pkg in dataCaches[mc].runrecs[fn]:
77 if " ".join(dataCaches[mc].runrecs[fn][pkg]).find("kernel-module-") != -1:
Patrick Williamsc124f4f2015-09-15 14:41:29 -050078 return False
79
80 # Default to keep dependencies
81 return True
82
83def sstate_lockedsigs(d):
84 sigs = {}
Brad Bishop6e60e8b2018-02-01 10:27:11 -050085 types = (d.getVar("SIGGEN_LOCKEDSIGS_TYPES") or "").split()
Patrick Williamsc124f4f2015-09-15 14:41:29 -050086 for t in types:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050087 siggen_lockedsigs_var = "SIGGEN_LOCKEDSIGS_%s" % t
Brad Bishop6e60e8b2018-02-01 10:27:11 -050088 lockedsigs = (d.getVar(siggen_lockedsigs_var) or "").split()
Patrick Williamsc124f4f2015-09-15 14:41:29 -050089 for ls in lockedsigs:
90 pn, task, h = ls.split(":", 2)
91 if pn not in sigs:
92 sigs[pn] = {}
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050093 sigs[pn][task] = [h, siggen_lockedsigs_var]
Patrick Williamsc124f4f2015-09-15 14:41:29 -050094 return sigs
95
96class SignatureGeneratorOEBasic(bb.siggen.SignatureGeneratorBasic):
97 name = "OEBasic"
98 def init_rundepcheck(self, data):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050099 self.abisaferecipes = (data.getVar("SIGGEN_EXCLUDERECIPES_ABISAFE") or "").split()
100 self.saferecipedeps = (data.getVar("SIGGEN_EXCLUDE_SAFE_RECIPE_DEPS") or "").split()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500101 pass
Andrew Geisslerd25ed322020-06-27 00:28:28 -0500102 def rundep_check(self, fn, recipename, task, dep, depname, dataCaches = None):
103 return sstate_rundepfilter(self, fn, recipename, task, dep, depname, dataCaches)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500104
Brad Bishop00e122a2019-10-05 11:10:57 -0400105class SignatureGeneratorOEBasicHashMixIn(object):
Andrew Geisslerd25ed322020-06-27 00:28:28 -0500106 supports_multiconfig_datacaches = True
107
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500108 def init_rundepcheck(self, data):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500109 self.abisaferecipes = (data.getVar("SIGGEN_EXCLUDERECIPES_ABISAFE") or "").split()
110 self.saferecipedeps = (data.getVar("SIGGEN_EXCLUDE_SAFE_RECIPE_DEPS") or "").split()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500111 self.lockedsigs = sstate_lockedsigs(data)
112 self.lockedhashes = {}
113 self.lockedpnmap = {}
114 self.lockedhashfn = {}
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500115 self.machine = data.getVar("MACHINE")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500116 self.mismatch_msgs = []
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500117 self.unlockedrecipes = (data.getVar("SIGGEN_UNLOCKED_RECIPES") or
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500118 "").split()
119 self.unlockedrecipes = { k: "" for k in self.unlockedrecipes }
Andrew Geissler82c905d2020-04-13 13:39:40 -0500120 self._internal = False
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500121 pass
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500122
123 def tasks_resolved(self, virtmap, virtpnmap, dataCache):
124 # Translate virtual/xxx entries to PN values
125 newabisafe = []
126 for a in self.abisaferecipes:
127 if a in virtpnmap:
128 newabisafe.append(virtpnmap[a])
129 else:
130 newabisafe.append(a)
131 self.abisaferecipes = newabisafe
132 newsafedeps = []
133 for a in self.saferecipedeps:
134 a1, a2 = a.split("->")
135 if a1 in virtpnmap:
136 a1 = virtpnmap[a1]
137 if a2 in virtpnmap:
138 a2 = virtpnmap[a2]
139 newsafedeps.append(a1 + "->" + a2)
140 self.saferecipedeps = newsafedeps
141
Andrew Geisslerd25ed322020-06-27 00:28:28 -0500142 def rundep_check(self, fn, recipename, task, dep, depname, dataCaches = None):
143 return sstate_rundepfilter(self, fn, recipename, task, dep, depname, dataCaches)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500144
145 def get_taskdata(self):
Brad Bishop00e122a2019-10-05 11:10:57 -0400146 return (self.lockedpnmap, self.lockedhashfn, self.lockedhashes) + super().get_taskdata()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500147
148 def set_taskdata(self, data):
Brad Bishop00e122a2019-10-05 11:10:57 -0400149 self.lockedpnmap, self.lockedhashfn, self.lockedhashes = data[:3]
150 super().set_taskdata(data[3:])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500151
152 def dump_sigs(self, dataCache, options):
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600153 sigfile = os.getcwd() + "/locked-sigs.inc"
154 bb.plain("Writing locked sigs to %s" % sigfile)
155 self.dump_lockedsigs(sigfile)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500156 return super(bb.siggen.SignatureGeneratorBasicHash, self).dump_sigs(dataCache, options)
157
Andrew Geissler82c905d2020-04-13 13:39:40 -0500158
Andrew Geisslerd25ed322020-06-27 00:28:28 -0500159 def get_taskhash(self, tid, deps, dataCaches):
Andrew Geissler82c905d2020-04-13 13:39:40 -0500160 if tid in self.lockedhashes:
161 if self.lockedhashes[tid]:
162 return self.lockedhashes[tid]
163 else:
Andrew Geisslerd25ed322020-06-27 00:28:28 -0500164 return super().get_taskhash(tid, deps, dataCaches)
Andrew Geissler82c905d2020-04-13 13:39:40 -0500165
Andrew Geisslerd25ed322020-06-27 00:28:28 -0500166 h = super().get_taskhash(tid, deps, dataCaches)
Brad Bishop08902b02019-08-20 09:16:51 -0400167
168 (mc, _, task, fn) = bb.runqueue.split_tid_mcfn(tid)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500169
Andrew Geisslerd25ed322020-06-27 00:28:28 -0500170 recipename = dataCaches[mc].pkg_fn[fn]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500171 self.lockedpnmap[fn] = recipename
Andrew Geisslerd25ed322020-06-27 00:28:28 -0500172 self.lockedhashfn[fn] = dataCaches[mc].hashfn[fn]
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500173
174 unlocked = False
175 if recipename in self.unlockedrecipes:
176 unlocked = True
177 else:
178 def recipename_from_dep(dep):
Andrew Geisslerd25ed322020-06-27 00:28:28 -0500179 (depmc, _, _, depfn) = bb.runqueue.split_tid_mcfn(dep)
180 return dataCaches[depmc].pkg_fn[depfn]
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500181
182 # If any unlocked recipe is in the direct dependencies then the
183 # current recipe should be unlocked as well.
Brad Bishop08902b02019-08-20 09:16:51 -0400184 depnames = [ recipename_from_dep(x) for x in deps if mc == bb.runqueue.mc_from_tid(x)]
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500185 if any(x in y for y in depnames for x in self.unlockedrecipes):
186 self.unlockedrecipes[recipename] = ''
187 unlocked = True
188
189 if not unlocked and recipename in self.lockedsigs:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500190 if task in self.lockedsigs[recipename]:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500191 h_locked = self.lockedsigs[recipename][task][0]
192 var = self.lockedsigs[recipename][task][1]
Brad Bishop08902b02019-08-20 09:16:51 -0400193 self.lockedhashes[tid] = h_locked
Andrew Geissler82c905d2020-04-13 13:39:40 -0500194 self._internal = True
195 unihash = self.get_unihash(tid)
196 self._internal = False
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500197 #bb.warn("Using %s %s %s" % (recipename, task, h))
198
Brad Bishop00e122a2019-10-05 11:10:57 -0400199 if h != h_locked and h_locked != unihash:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500200 self.mismatch_msgs.append('The %s:%s sig is computed to be %s, but the sig is locked to %s in %s'
201 % (recipename, task, h, h_locked, var))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500202
203 return h_locked
Andrew Geissler82c905d2020-04-13 13:39:40 -0500204
205 self.lockedhashes[tid] = False
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500206 #bb.warn("%s %s %s" % (recipename, task, h))
207 return h
208
Andrew Geissler82c905d2020-04-13 13:39:40 -0500209 def get_stampfile_hash(self, tid):
210 if tid in self.lockedhashes and self.lockedhashes[tid]:
211 return self.lockedhashes[tid]
212 return super().get_stampfile_hash(tid)
213
Brad Bishop00e122a2019-10-05 11:10:57 -0400214 def get_unihash(self, tid):
Andrew Geissler82c905d2020-04-13 13:39:40 -0500215 if tid in self.lockedhashes and self.lockedhashes[tid] and not self._internal:
Brad Bishop00e122a2019-10-05 11:10:57 -0400216 return self.lockedhashes[tid]
217 return super().get_unihash(tid)
218
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500219 def dump_sigtask(self, fn, task, stampbase, runtime):
Brad Bishop08902b02019-08-20 09:16:51 -0400220 tid = fn + ":" + task
Andrew Geissler82c905d2020-04-13 13:39:40 -0500221 if tid in self.lockedhashes and self.lockedhashes[tid]:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500222 return
223 super(bb.siggen.SignatureGeneratorBasicHash, self).dump_sigtask(fn, task, stampbase, runtime)
224
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600225 def dump_lockedsigs(self, sigfile, taskfilter=None):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500226 types = {}
Brad Bishop08902b02019-08-20 09:16:51 -0400227 for tid in self.runtaskdeps:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500228 if taskfilter:
Brad Bishop08902b02019-08-20 09:16:51 -0400229 if not tid in taskfilter:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500230 continue
Brad Bishop08902b02019-08-20 09:16:51 -0400231 fn = bb.runqueue.fn_from_tid(tid)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500232 t = self.lockedhashfn[fn].split(" ")[1].split(":")[5]
233 t = 't-' + t.replace('_', '-')
234 if t not in types:
235 types[t] = []
Brad Bishop08902b02019-08-20 09:16:51 -0400236 types[t].append(tid)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500237
238 with open(sigfile, "w") as f:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500239 l = sorted(types)
240 for t in l:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500241 f.write('SIGGEN_LOCKEDSIGS_%s = "\\\n' % t)
242 types[t].sort()
Brad Bishop08902b02019-08-20 09:16:51 -0400243 sortedtid = sorted(types[t], key=lambda tid: self.lockedpnmap[bb.runqueue.fn_from_tid(tid)])
244 for tid in sortedtid:
245 (_, _, task, fn) = bb.runqueue.split_tid_mcfn(tid)
246 if tid not in self.taskhash:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500247 continue
Brad Bishop00e122a2019-10-05 11:10:57 -0400248 f.write(" " + self.lockedpnmap[fn] + ":" + task + ":" + self.get_unihash(tid) + " \\\n")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500249 f.write(' "\n')
Patrick Williams213cb262021-08-07 19:21:33 -0500250 f.write('SIGGEN_LOCKEDSIGS_TYPES:%s = "%s"' % (self.machine, " ".join(l)))
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500251
Andrew Geissler09036742021-06-25 14:25:14 -0500252 def dump_siglist(self, sigfile, path_prefix_strip=None):
253 def strip_fn(fn):
254 nonlocal path_prefix_strip
255 if not path_prefix_strip:
256 return fn
257
258 fn_exp = fn.split(":")
259 if fn_exp[-1].startswith(path_prefix_strip):
260 fn_exp[-1] = fn_exp[-1][len(path_prefix_strip):]
261
262 return ":".join(fn_exp)
263
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500264 with open(sigfile, "w") as f:
265 tasks = []
266 for taskitem in self.taskhash:
Brad Bishop08902b02019-08-20 09:16:51 -0400267 (fn, task) = taskitem.rsplit(":", 1)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500268 pn = self.lockedpnmap[fn]
Andrew Geissler09036742021-06-25 14:25:14 -0500269 tasks.append((pn, task, strip_fn(fn), self.taskhash[taskitem]))
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500270 for (pn, task, fn, taskhash) in sorted(tasks):
Brad Bishop08902b02019-08-20 09:16:51 -0400271 f.write('%s:%s %s %s\n' % (pn, task, fn, taskhash))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500272
Brad Bishop08902b02019-08-20 09:16:51 -0400273 def checkhashes(self, sq_data, missed, found, d):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500274 warn_msgs = []
275 error_msgs = []
276 sstate_missing_msgs = []
277
Brad Bishop08902b02019-08-20 09:16:51 -0400278 for tid in sq_data['hash']:
279 if tid not in found:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500280 for pn in self.lockedsigs:
Brad Bishop08902b02019-08-20 09:16:51 -0400281 taskname = bb.runqueue.taskname_from_tid(tid)
282 if sq_data['hash'][tid] in iter(self.lockedsigs[pn].values()):
283 if taskname == 'do_shared_workdir':
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500284 continue
285 sstate_missing_msgs.append("Locked sig is set for %s:%s (%s) yet not in sstate cache?"
Brad Bishop08902b02019-08-20 09:16:51 -0400286 % (pn, taskname, sq_data['hash'][tid]))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500287
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500288 checklevel = d.getVar("SIGGEN_LOCKEDSIGS_TASKSIG_CHECK")
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500289 if checklevel == 'warn':
290 warn_msgs += self.mismatch_msgs
291 elif checklevel == 'error':
292 error_msgs += self.mismatch_msgs
293
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500294 checklevel = d.getVar("SIGGEN_LOCKEDSIGS_SSTATE_EXISTS_CHECK")
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500295 if checklevel == 'warn':
296 warn_msgs += sstate_missing_msgs
297 elif checklevel == 'error':
298 error_msgs += sstate_missing_msgs
299
300 if warn_msgs:
301 bb.warn("\n".join(warn_msgs))
302 if error_msgs:
303 bb.fatal("\n".join(error_msgs))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500304
Brad Bishop00e122a2019-10-05 11:10:57 -0400305class SignatureGeneratorOEBasicHash(SignatureGeneratorOEBasicHashMixIn, bb.siggen.SignatureGeneratorBasicHash):
306 name = "OEBasicHash"
307
308class SignatureGeneratorOEEquivHash(SignatureGeneratorOEBasicHashMixIn, bb.siggen.SignatureGeneratorUniHashMixIn, bb.siggen.SignatureGeneratorBasicHash):
Brad Bishop19323692019-04-05 15:28:33 -0400309 name = "OEEquivHash"
310
311 def init_rundepcheck(self, data):
312 super().init_rundepcheck(data)
Brad Bishopa34c0302019-09-23 22:34:48 -0400313 self.server = data.getVar('BB_HASHSERVE')
Brad Bishop08902b02019-08-20 09:16:51 -0400314 if not self.server:
Brad Bishopa34c0302019-09-23 22:34:48 -0400315 bb.fatal("OEEquivHash requires BB_HASHSERVE to be set")
Brad Bishop19323692019-04-05 15:28:33 -0400316 self.method = data.getVar('SSTATE_HASHEQUIV_METHOD')
Brad Bishop08902b02019-08-20 09:16:51 -0400317 if not self.method:
318 bb.fatal("OEEquivHash requires SSTATE_HASHEQUIV_METHOD to be set")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500319
320# Insert these classes into siggen's namespace so it can see and select them
321bb.siggen.SignatureGeneratorOEBasic = SignatureGeneratorOEBasic
322bb.siggen.SignatureGeneratorOEBasicHash = SignatureGeneratorOEBasicHash
Brad Bishop19323692019-04-05 15:28:33 -0400323bb.siggen.SignatureGeneratorOEEquivHash = SignatureGeneratorOEEquivHash
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500324
325
326def find_siginfo(pn, taskname, taskhashlist, d):
327 """ Find signature data files for comparison purposes """
328
329 import fnmatch
330 import glob
331
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500332 if not taskname:
333 # We have to derive pn and taskname
334 key = pn
Brad Bishop08902b02019-08-20 09:16:51 -0400335 splitit = key.split('.bb:')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500336 taskname = splitit[1]
337 pn = os.path.basename(splitit[0]).split('_')[0]
338 if key.startswith('virtual:native:'):
339 pn = pn + '-native'
340
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500341 hashfiles = {}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500342 filedates = {}
343
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500344 def get_hashval(siginfo):
345 if siginfo.endswith('.siginfo'):
346 return siginfo.rpartition(':')[2].partition('_')[0]
347 else:
348 return siginfo.rpartition('.')[2]
349
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500350 # First search in stamps dir
351 localdata = d.createCopy()
352 localdata.setVar('MULTIMACH_TARGET_SYS', '*')
353 localdata.setVar('PN', pn)
354 localdata.setVar('PV', '*')
355 localdata.setVar('PR', '*')
356 localdata.setVar('EXTENDPE', '')
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500357 stamp = localdata.getVar('STAMP')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500358 if pn.startswith("gcc-source"):
359 # gcc-source shared workdir is a special case :(
360 stamp = localdata.expand("${STAMPS_DIR}/work-shared/gcc-${PV}-${PR}")
361
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500362 filespec = '%s.%s.sigdata.*' % (stamp, taskname)
363 foundall = False
364 import glob
365 for fullpath in glob.glob(filespec):
366 match = False
367 if taskhashlist:
368 for taskhash in taskhashlist:
369 if fullpath.endswith('.%s' % taskhash):
370 hashfiles[taskhash] = fullpath
371 if len(hashfiles) == len(taskhashlist):
372 foundall = True
373 break
374 else:
375 try:
376 filedates[fullpath] = os.stat(fullpath).st_mtime
377 except OSError:
378 continue
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500379 hashval = get_hashval(fullpath)
380 hashfiles[hashval] = fullpath
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500381
382 if not taskhashlist or (len(filedates) < 2 and not foundall):
383 # That didn't work, look in sstate-cache
Brad Bishop19323692019-04-05 15:28:33 -0400384 hashes = taskhashlist or ['?' * 64]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500385 localdata = bb.data.createCopy(d)
386 for hashval in hashes:
387 localdata.setVar('PACKAGE_ARCH', '*')
388 localdata.setVar('TARGET_VENDOR', '*')
389 localdata.setVar('TARGET_OS', '*')
390 localdata.setVar('PN', pn)
391 localdata.setVar('PV', '*')
392 localdata.setVar('PR', '*')
393 localdata.setVar('BB_TASKHASH', hashval)
Patrick Williams03907ee2022-05-01 06:28:52 -0500394 localdata.setVar('SSTATE_CURRTASK', taskname[3:])
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500395 swspec = localdata.getVar('SSTATE_SWSPEC')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500396 if taskname in ['do_fetch', 'do_unpack', 'do_patch', 'do_populate_lic', 'do_preconfigure'] and swspec:
397 localdata.setVar('SSTATE_PKGSPEC', '${SSTATE_SWSPEC}')
398 elif pn.endswith('-native') or "-cross-" in pn or "-crosssdk-" in pn:
399 localdata.setVar('SSTATE_EXTRAPATH', "${NATIVELSBSTRING}/")
Patrick Williams03907ee2022-05-01 06:28:52 -0500400 filespec = '%s.siginfo' % localdata.getVar('SSTATE_PKG')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500401
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500402 matchedfiles = glob.glob(filespec)
403 for fullpath in matchedfiles:
404 actual_hashval = get_hashval(fullpath)
405 if actual_hashval in hashfiles:
406 continue
407 hashfiles[hashval] = fullpath
408 if not taskhashlist:
409 try:
410 filedates[fullpath] = os.stat(fullpath).st_mtime
411 except:
412 continue
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500413
414 if taskhashlist:
415 return hashfiles
416 else:
417 return filedates
418
419bb.siggen.find_siginfo = find_siginfo
420
421
422def sstate_get_manifest_filename(task, d):
423 """
424 Return the sstate manifest file path for a particular task.
425 Also returns the datastore that can be used to query related variables.
426 """
427 d2 = d.createCopy()
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500428 extrainf = d.getVarFlag("do_" + task, 'stamp-extra-info')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500429 if extrainf:
430 d2.setVar("SSTATE_MANMACH", extrainf)
431 return (d2.expand("${SSTATE_MANFILEPREFIX}.%s" % task), d2)
Brad Bishop316dfdd2018-06-25 12:45:53 -0400432
433def find_sstate_manifest(taskdata, taskdata2, taskname, d, multilibcache):
434 d2 = d
435 variant = ''
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800436 curr_variant = ''
437 if d.getVar("BBEXTENDCURR") == "multilib":
438 curr_variant = d.getVar("BBEXTENDVARIANT")
439 if "virtclass-multilib" not in d.getVar("OVERRIDES"):
440 curr_variant = "invalid"
Brad Bishop316dfdd2018-06-25 12:45:53 -0400441 if taskdata2.startswith("virtual:multilib"):
442 variant = taskdata2.split(":")[2]
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800443 if curr_variant != variant:
Brad Bishop316dfdd2018-06-25 12:45:53 -0400444 if variant not in multilibcache:
445 multilibcache[variant] = oe.utils.get_multilib_datastore(variant, d)
446 d2 = multilibcache[variant]
447
448 if taskdata.endswith("-native"):
Andrew Geissler6ce62a22020-11-30 19:58:47 -0600449 pkgarchs = ["${BUILD_ARCH}", "${BUILD_ARCH}_${ORIGNATIVELSBSTRING}"]
Brad Bishop316dfdd2018-06-25 12:45:53 -0400450 elif taskdata.startswith("nativesdk-"):
451 pkgarchs = ["${SDK_ARCH}_${SDK_OS}", "allarch"]
452 elif "-cross-canadian" in taskdata:
453 pkgarchs = ["${SDK_ARCH}_${SDK_ARCH}-${SDKPKGSUFFIX}"]
454 elif "-cross-" in taskdata:
Andrew Geissler9aee5002022-03-30 16:27:02 +0000455 pkgarchs = ["${BUILD_ARCH}"]
Brad Bishop316dfdd2018-06-25 12:45:53 -0400456 elif "-crosssdk" in taskdata:
457 pkgarchs = ["${BUILD_ARCH}_${SDK_ARCH}_${SDK_OS}"]
458 else:
459 pkgarchs = ['${MACHINE_ARCH}']
460 pkgarchs = pkgarchs + list(reversed(d2.getVar("PACKAGE_EXTRA_ARCHS").split()))
461 pkgarchs.append('allarch')
462 pkgarchs.append('${SDK_ARCH}_${SDK_ARCH}-${SDKPKGSUFFIX}')
463
464 for pkgarch in pkgarchs:
465 manifest = d2.expand("${SSTATE_MANIFESTS}/manifest-%s-%s.%s" % (pkgarch, taskdata, taskname))
466 if os.path.exists(manifest):
467 return manifest, d2
William A. Kennington IIIac69b482021-06-02 12:28:27 -0700468 bb.fatal("Manifest %s not found in %s (variant '%s')?" % (manifest, d2.expand(" ".join(pkgarchs)), variant))
Brad Bishop316dfdd2018-06-25 12:45:53 -0400469 return None, d2
470
Brad Bishop19323692019-04-05 15:28:33 -0400471def OEOuthashBasic(path, sigfile, task, d):
472 """
473 Basic output hash function
474
475 Calculates the output hash of a task by hashing all output file metadata,
476 and file contents.
477 """
478 import hashlib
479 import stat
480 import pwd
481 import grp
Patrick Williams93c203f2021-10-06 16:15:23 -0500482 import re
483 import fnmatch
Brad Bishop19323692019-04-05 15:28:33 -0400484
485 def update_hash(s):
486 s = s.encode('utf-8')
487 h.update(s)
488 if sigfile:
489 sigfile.write(s)
490
491 h = hashlib.sha256()
492 prev_dir = os.getcwd()
Patrick Williams93c203f2021-10-06 16:15:23 -0500493 corebase = d.getVar("COREBASE")
494 tmpdir = d.getVar("TMPDIR")
Brad Bishop19323692019-04-05 15:28:33 -0400495 include_owners = os.environ.get('PSEUDO_DISABLED') == '0'
Andrew Geisslerf0343792020-11-18 10:42:21 -0600496 if "package_write_" in task or task == "package_qa":
497 include_owners = False
Andrew Geissler6ce62a22020-11-30 19:58:47 -0600498 include_timestamps = False
Andrew Geissler5199d832021-09-24 16:47:35 -0500499 include_root = True
Andrew Geissler6ce62a22020-11-30 19:58:47 -0600500 if task == "package":
Andrew Geisslereff27472021-10-29 15:35:00 -0500501 include_timestamps = True
Andrew Geissler5199d832021-09-24 16:47:35 -0500502 include_root = False
Andrew Geissler595f6302022-01-24 19:11:47 +0000503 hash_version = d.getVar('HASHEQUIV_HASH_VERSION')
504 extra_sigdata = d.getVar("HASHEQUIV_EXTRA_SIGDATA")
Brad Bishop19323692019-04-05 15:28:33 -0400505
Patrick Williams93c203f2021-10-06 16:15:23 -0500506 filemaps = {}
507 for m in (d.getVar('SSTATE_HASHEQUIV_FILEMAP') or '').split():
508 entry = m.split(":")
509 if len(entry) != 3 or entry[0] != task:
510 continue
511 filemaps.setdefault(entry[1], [])
512 filemaps[entry[1]].append(entry[2])
513
Brad Bishop19323692019-04-05 15:28:33 -0400514 try:
515 os.chdir(path)
Patrick Williams93c203f2021-10-06 16:15:23 -0500516 basepath = os.path.normpath(path)
Brad Bishop19323692019-04-05 15:28:33 -0400517
518 update_hash("OEOuthashBasic\n")
Andrew Geissler595f6302022-01-24 19:11:47 +0000519 if hash_version:
520 update_hash(hash_version + "\n")
521
522 if extra_sigdata:
523 update_hash(extra_sigdata + "\n")
Brad Bishop19323692019-04-05 15:28:33 -0400524
525 # It is only currently useful to get equivalent hashes for things that
526 # can be restored from sstate. Since the sstate object is named using
527 # SSTATE_PKGSPEC and the task name, those should be included in the
528 # output hash calculation.
529 update_hash("SSTATE_PKGSPEC=%s\n" % d.getVar('SSTATE_PKGSPEC'))
530 update_hash("task=%s\n" % task)
531
532 for root, dirs, files in os.walk('.', topdown=True):
533 # Sort directories to ensure consistent ordering when recursing
534 dirs.sort()
535 files.sort()
536
537 def process(path):
538 s = os.lstat(path)
539
540 if stat.S_ISDIR(s.st_mode):
541 update_hash('d')
542 elif stat.S_ISCHR(s.st_mode):
543 update_hash('c')
544 elif stat.S_ISBLK(s.st_mode):
545 update_hash('b')
546 elif stat.S_ISSOCK(s.st_mode):
547 update_hash('s')
548 elif stat.S_ISLNK(s.st_mode):
549 update_hash('l')
550 elif stat.S_ISFIFO(s.st_mode):
551 update_hash('p')
552 else:
553 update_hash('-')
554
555 def add_perm(mask, on, off='-'):
556 if mask & s.st_mode:
557 update_hash(on)
558 else:
559 update_hash(off)
560
561 add_perm(stat.S_IRUSR, 'r')
562 add_perm(stat.S_IWUSR, 'w')
563 if stat.S_ISUID & s.st_mode:
564 add_perm(stat.S_IXUSR, 's', 'S')
565 else:
566 add_perm(stat.S_IXUSR, 'x')
567
Brad Bishop19323692019-04-05 15:28:33 -0400568 if include_owners:
Andrew Geisslereff27472021-10-29 15:35:00 -0500569 # Group/other permissions are only relevant in pseudo context
570 add_perm(stat.S_IRGRP, 'r')
571 add_perm(stat.S_IWGRP, 'w')
572 if stat.S_ISGID & s.st_mode:
573 add_perm(stat.S_IXGRP, 's', 'S')
574 else:
575 add_perm(stat.S_IXGRP, 'x')
576
577 add_perm(stat.S_IROTH, 'r')
578 add_perm(stat.S_IWOTH, 'w')
579 if stat.S_ISVTX & s.st_mode:
580 update_hash('t')
581 else:
582 add_perm(stat.S_IXOTH, 'x')
583
Andrew Geissler82c905d2020-04-13 13:39:40 -0500584 try:
585 update_hash(" %10s" % pwd.getpwuid(s.st_uid).pw_name)
586 update_hash(" %10s" % grp.getgrgid(s.st_gid).gr_name)
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600587 except KeyError as e:
Andrew Geissler82c905d2020-04-13 13:39:40 -0500588 bb.warn("KeyError in %s" % path)
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600589 msg = ("KeyError: %s\nPath %s is owned by uid %d, gid %d, which doesn't match "
590 "any user/group on target. This may be due to host contamination." % (e, path, s.st_uid, s.st_gid))
591 raise Exception(msg).with_traceback(e.__traceback__)
Brad Bishop19323692019-04-05 15:28:33 -0400592
Andrew Geissler6ce62a22020-11-30 19:58:47 -0600593 if include_timestamps:
594 update_hash(" %10d" % s.st_mtime)
595
Brad Bishop19323692019-04-05 15:28:33 -0400596 update_hash(" ")
597 if stat.S_ISBLK(s.st_mode) or stat.S_ISCHR(s.st_mode):
598 update_hash("%9s" % ("%d.%d" % (os.major(s.st_rdev), os.minor(s.st_rdev))))
599 else:
600 update_hash(" " * 9)
601
Patrick Williams93c203f2021-10-06 16:15:23 -0500602 filterfile = False
603 for entry in filemaps:
604 if fnmatch.fnmatch(path, entry):
605 filterfile = True
606
Brad Bishop19323692019-04-05 15:28:33 -0400607 update_hash(" ")
Patrick Williams93c203f2021-10-06 16:15:23 -0500608 if stat.S_ISREG(s.st_mode) and not filterfile:
Brad Bishop19323692019-04-05 15:28:33 -0400609 update_hash("%10d" % s.st_size)
610 else:
611 update_hash(" " * 10)
612
613 update_hash(" ")
614 fh = hashlib.sha256()
615 if stat.S_ISREG(s.st_mode):
616 # Hash file contents
Patrick Williams93c203f2021-10-06 16:15:23 -0500617 if filterfile:
618 # Need to ignore paths in crossscripts and postinst-useradd files.
619 with open(path, 'rb') as d:
620 chunk = d.read()
621 chunk = chunk.replace(bytes(basepath, encoding='utf8'), b'')
622 for entry in filemaps:
623 if not fnmatch.fnmatch(path, entry):
624 continue
625 for r in filemaps[entry]:
626 if r.startswith("regex-"):
627 chunk = re.sub(bytes(r[6:], encoding='utf8'), b'', chunk)
628 else:
629 chunk = chunk.replace(bytes(r, encoding='utf8'), b'')
Brad Bishop19323692019-04-05 15:28:33 -0400630 fh.update(chunk)
Patrick Williams93c203f2021-10-06 16:15:23 -0500631 else:
632 with open(path, 'rb') as d:
633 for chunk in iter(lambda: d.read(4096), b""):
634 fh.update(chunk)
Brad Bishop19323692019-04-05 15:28:33 -0400635 update_hash(fh.hexdigest())
636 else:
637 update_hash(" " * len(fh.hexdigest()))
638
639 update_hash(" %s" % path)
640
641 if stat.S_ISLNK(s.st_mode):
642 update_hash(" -> %s" % os.readlink(path))
643
644 update_hash("\n")
645
646 # Process this directory and all its child files
Andrew Geissler5199d832021-09-24 16:47:35 -0500647 if include_root or root != ".":
648 process(root)
Brad Bishop19323692019-04-05 15:28:33 -0400649 for f in files:
650 if f == 'fixmepath':
651 continue
652 process(os.path.join(root, f))
653 finally:
654 os.chdir(prev_dir)
655
656 return h.hexdigest()
657
Brad Bishop316dfdd2018-06-25 12:45:53 -0400658