blob: 78cdf878f18339c2ff94d77cbfd50132cd3fc129 [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 Williamsc124f4f2015-09-15 14:41:29 -050027
Andrew Geisslerd25ed322020-06-27 00:28:28 -050028 depmc, _, deptaskname, depmcfn = bb.runqueue.split_tid_mcfn(dep)
29 mc, _ = bb.runqueue.split_mc(fn)
30
31 # (Almost) always include our own inter-task dependencies (unless it comes
32 # from a mcdepends). The exception is the special
33 # do_kernel_configme->do_unpack_and_patch dependency from archiver.bbclass.
34 if recipename == depname and depmc == mc:
35 if task == "do_kernel_configme" and deptaskname == "do_unpack_and_patch":
Brad Bishop6e60e8b2018-02-01 10:27:11 -050036 return False
Patrick Williamsc124f4f2015-09-15 14:41:29 -050037 return True
38
Patrick Williamsc124f4f2015-09-15 14:41:29 -050039 # Exclude well defined recipe->dependency
40 if "%s->%s" % (recipename, depname) in siggen.saferecipedeps:
41 return False
42
Brad Bishop316dfdd2018-06-25 12:45:53 -040043 # Check for special wildcard
44 if "*->%s" % depname in siggen.saferecipedeps and recipename != depname:
45 return False
46
Patrick Williamsc124f4f2015-09-15 14:41:29 -050047 # Don't change native/cross/nativesdk recipe dependencies any further
48 if isNative(recipename) or isCross(recipename) or isNativeSDK(recipename):
49 return True
50
51 # Only target packages beyond here
52
53 # allarch packagegroups are assumed to have well behaved names which don't change between architecures/tunes
Andrew Geisslerd25ed322020-06-27 00:28:28 -050054 if isPackageGroup(mc, fn) and isAllArch(mc, fn) and not isNative(depname):
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080055 return False
Patrick Williamsc124f4f2015-09-15 14:41:29 -050056
57 # Exclude well defined machine specific configurations which don't change ABI
Andrew Geisslerd25ed322020-06-27 00:28:28 -050058 if depname in siggen.abisaferecipes and not isImage(mc, fn):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050059 return False
60
61 # Kernel modules are well namespaced. We don't want to depend on the kernel's checksum
Patrick Williams213cb262021-08-07 19:21:33 -050062 # if we're just doing an RRECOMMENDS:xxx = "kernel-module-*", not least because the checksum
Patrick Williamsc124f4f2015-09-15 14:41:29 -050063 # is machine specific.
64 # Therefore if we're not a kernel or a module recipe (inheriting the kernel classes)
65 # and we reccomend a kernel-module, we exclude the dependency.
Andrew Geisslerd25ed322020-06-27 00:28:28 -050066 if dataCaches and isKernel(depmc, depmcfn) and not isKernel(mc, fn):
67 for pkg in dataCaches[mc].runrecs[fn]:
68 if " ".join(dataCaches[mc].runrecs[fn][pkg]).find("kernel-module-") != -1:
Patrick Williamsc124f4f2015-09-15 14:41:29 -050069 return False
70
71 # Default to keep dependencies
72 return True
73
74def sstate_lockedsigs(d):
75 sigs = {}
Brad Bishop6e60e8b2018-02-01 10:27:11 -050076 types = (d.getVar("SIGGEN_LOCKEDSIGS_TYPES") or "").split()
Patrick Williamsc124f4f2015-09-15 14:41:29 -050077 for t in types:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050078 siggen_lockedsigs_var = "SIGGEN_LOCKEDSIGS_%s" % t
Brad Bishop6e60e8b2018-02-01 10:27:11 -050079 lockedsigs = (d.getVar(siggen_lockedsigs_var) or "").split()
Patrick Williamsc124f4f2015-09-15 14:41:29 -050080 for ls in lockedsigs:
81 pn, task, h = ls.split(":", 2)
82 if pn not in sigs:
83 sigs[pn] = {}
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050084 sigs[pn][task] = [h, siggen_lockedsigs_var]
Patrick Williamsc124f4f2015-09-15 14:41:29 -050085 return sigs
86
87class SignatureGeneratorOEBasic(bb.siggen.SignatureGeneratorBasic):
88 name = "OEBasic"
89 def init_rundepcheck(self, data):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050090 self.abisaferecipes = (data.getVar("SIGGEN_EXCLUDERECIPES_ABISAFE") or "").split()
91 self.saferecipedeps = (data.getVar("SIGGEN_EXCLUDE_SAFE_RECIPE_DEPS") or "").split()
Patrick Williamsc124f4f2015-09-15 14:41:29 -050092 pass
Andrew Geisslerd25ed322020-06-27 00:28:28 -050093 def rundep_check(self, fn, recipename, task, dep, depname, dataCaches = None):
94 return sstate_rundepfilter(self, fn, recipename, task, dep, depname, dataCaches)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050095
Brad Bishop00e122a2019-10-05 11:10:57 -040096class SignatureGeneratorOEBasicHashMixIn(object):
Andrew Geisslerd25ed322020-06-27 00:28:28 -050097 supports_multiconfig_datacaches = True
98
Patrick Williamsc124f4f2015-09-15 14:41:29 -050099 def init_rundepcheck(self, data):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500100 self.abisaferecipes = (data.getVar("SIGGEN_EXCLUDERECIPES_ABISAFE") or "").split()
101 self.saferecipedeps = (data.getVar("SIGGEN_EXCLUDE_SAFE_RECIPE_DEPS") or "").split()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500102 self.lockedsigs = sstate_lockedsigs(data)
103 self.lockedhashes = {}
104 self.lockedpnmap = {}
105 self.lockedhashfn = {}
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500106 self.machine = data.getVar("MACHINE")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500107 self.mismatch_msgs = []
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500108 self.unlockedrecipes = (data.getVar("SIGGEN_UNLOCKED_RECIPES") or
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500109 "").split()
110 self.unlockedrecipes = { k: "" for k in self.unlockedrecipes }
Andrew Geissler82c905d2020-04-13 13:39:40 -0500111 self.buildarch = data.getVar('BUILD_ARCH')
112 self._internal = False
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500113 pass
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500114
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 Geisslerd25ed322020-06-27 00:28:28 -0500134 def rundep_check(self, fn, recipename, task, dep, depname, dataCaches = None):
135 return sstate_rundepfilter(self, fn, recipename, task, dep, depname, dataCaches)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500136
137 def get_taskdata(self):
Brad Bishop00e122a2019-10-05 11:10:57 -0400138 return (self.lockedpnmap, self.lockedhashfn, self.lockedhashes) + super().get_taskdata()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500139
140 def set_taskdata(self, data):
Brad Bishop00e122a2019-10-05 11:10:57 -0400141 self.lockedpnmap, self.lockedhashfn, self.lockedhashes = data[:3]
142 super().set_taskdata(data[3:])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500143
144 def dump_sigs(self, dataCache, options):
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600145 sigfile = os.getcwd() + "/locked-sigs.inc"
146 bb.plain("Writing locked sigs to %s" % sigfile)
147 self.dump_lockedsigs(sigfile)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500148 return super(bb.siggen.SignatureGeneratorBasicHash, self).dump_sigs(dataCache, options)
149
Andrew Geisslerd25ed322020-06-27 00:28:28 -0500150 def prep_taskhash(self, tid, deps, dataCaches):
151 super().prep_taskhash(tid, deps, dataCaches)
Andrew Geissler82c905d2020-04-13 13:39:40 -0500152 if hasattr(self, "extramethod"):
Andrew Geisslerd25ed322020-06-27 00:28:28 -0500153 (mc, _, _, fn) = bb.runqueue.split_tid_mcfn(tid)
154 inherits = " ".join(dataCaches[mc].inherits[fn])
Andrew Geissler82c905d2020-04-13 13:39:40 -0500155 if inherits.find("/native.bbclass") != -1 or inherits.find("/cross.bbclass") != -1:
156 self.extramethod[tid] = ":" + self.buildarch
157
Andrew Geisslerd25ed322020-06-27 00:28:28 -0500158 def get_taskhash(self, tid, deps, dataCaches):
Andrew Geissler82c905d2020-04-13 13:39:40 -0500159 if tid in self.lockedhashes:
160 if self.lockedhashes[tid]:
161 return self.lockedhashes[tid]
162 else:
Andrew Geisslerd25ed322020-06-27 00:28:28 -0500163 return super().get_taskhash(tid, deps, dataCaches)
Andrew Geissler82c905d2020-04-13 13:39:40 -0500164
Andrew Geisslerd25ed322020-06-27 00:28:28 -0500165 h = super().get_taskhash(tid, deps, dataCaches)
Brad Bishop08902b02019-08-20 09:16:51 -0400166
167 (mc, _, task, fn) = bb.runqueue.split_tid_mcfn(tid)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500168
Andrew Geisslerd25ed322020-06-27 00:28:28 -0500169 recipename = dataCaches[mc].pkg_fn[fn]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500170 self.lockedpnmap[fn] = recipename
Andrew Geisslerd25ed322020-06-27 00:28:28 -0500171 self.lockedhashfn[fn] = dataCaches[mc].hashfn[fn]
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500172
173 unlocked = False
174 if recipename in self.unlockedrecipes:
175 unlocked = True
176 else:
177 def recipename_from_dep(dep):
Andrew Geisslerd25ed322020-06-27 00:28:28 -0500178 (depmc, _, _, depfn) = bb.runqueue.split_tid_mcfn(dep)
179 return dataCaches[depmc].pkg_fn[depfn]
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500180
181 # If any unlocked recipe is in the direct dependencies then the
182 # current recipe should be unlocked as well.
Brad Bishop08902b02019-08-20 09:16:51 -0400183 depnames = [ recipename_from_dep(x) for x in deps if mc == bb.runqueue.mc_from_tid(x)]
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500184 if any(x in y for y in depnames for x in self.unlockedrecipes):
185 self.unlockedrecipes[recipename] = ''
186 unlocked = True
187
188 if not unlocked and recipename in self.lockedsigs:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500189 if task in self.lockedsigs[recipename]:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500190 h_locked = self.lockedsigs[recipename][task][0]
191 var = self.lockedsigs[recipename][task][1]
Brad Bishop08902b02019-08-20 09:16:51 -0400192 self.lockedhashes[tid] = h_locked
Andrew Geissler82c905d2020-04-13 13:39:40 -0500193 self._internal = True
194 unihash = self.get_unihash(tid)
195 self._internal = False
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500196 #bb.warn("Using %s %s %s" % (recipename, task, h))
197
Brad Bishop00e122a2019-10-05 11:10:57 -0400198 if h != h_locked and h_locked != unihash:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500199 self.mismatch_msgs.append('The %s:%s sig is computed to be %s, but the sig is locked to %s in %s'
200 % (recipename, task, h, h_locked, var))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500201
202 return h_locked
Andrew Geissler82c905d2020-04-13 13:39:40 -0500203
204 self.lockedhashes[tid] = False
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500205 #bb.warn("%s %s %s" % (recipename, task, h))
206 return h
207
Andrew Geissler82c905d2020-04-13 13:39:40 -0500208 def get_stampfile_hash(self, tid):
209 if tid in self.lockedhashes and self.lockedhashes[tid]:
210 return self.lockedhashes[tid]
211 return super().get_stampfile_hash(tid)
212
Brad Bishop00e122a2019-10-05 11:10:57 -0400213 def get_unihash(self, tid):
Andrew Geissler82c905d2020-04-13 13:39:40 -0500214 if tid in self.lockedhashes and self.lockedhashes[tid] and not self._internal:
Brad Bishop00e122a2019-10-05 11:10:57 -0400215 return self.lockedhashes[tid]
216 return super().get_unihash(tid)
217
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500218 def dump_sigtask(self, fn, task, stampbase, runtime):
Brad Bishop08902b02019-08-20 09:16:51 -0400219 tid = fn + ":" + task
Andrew Geissler82c905d2020-04-13 13:39:40 -0500220 if tid in self.lockedhashes and self.lockedhashes[tid]:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500221 return
222 super(bb.siggen.SignatureGeneratorBasicHash, self).dump_sigtask(fn, task, stampbase, runtime)
223
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600224 def dump_lockedsigs(self, sigfile, taskfilter=None):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500225 types = {}
Brad Bishop08902b02019-08-20 09:16:51 -0400226 for tid in self.runtaskdeps:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500227 if taskfilter:
Brad Bishop08902b02019-08-20 09:16:51 -0400228 if not tid in taskfilter:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500229 continue
Brad Bishop08902b02019-08-20 09:16:51 -0400230 fn = bb.runqueue.fn_from_tid(tid)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500231 t = self.lockedhashfn[fn].split(" ")[1].split(":")[5]
232 t = 't-' + t.replace('_', '-')
233 if t not in types:
234 types[t] = []
Brad Bishop08902b02019-08-20 09:16:51 -0400235 types[t].append(tid)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500236
237 with open(sigfile, "w") as f:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500238 l = sorted(types)
239 for t in l:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500240 f.write('SIGGEN_LOCKEDSIGS_%s = "\\\n' % t)
241 types[t].sort()
Brad Bishop08902b02019-08-20 09:16:51 -0400242 sortedtid = sorted(types[t], key=lambda tid: self.lockedpnmap[bb.runqueue.fn_from_tid(tid)])
243 for tid in sortedtid:
244 (_, _, task, fn) = bb.runqueue.split_tid_mcfn(tid)
245 if tid not in self.taskhash:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500246 continue
Brad Bishop00e122a2019-10-05 11:10:57 -0400247 f.write(" " + self.lockedpnmap[fn] + ":" + task + ":" + self.get_unihash(tid) + " \\\n")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500248 f.write(' "\n')
Patrick Williams213cb262021-08-07 19:21:33 -0500249 f.write('SIGGEN_LOCKEDSIGS_TYPES:%s = "%s"' % (self.machine, " ".join(l)))
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500250
Andrew Geissler09036742021-06-25 14:25:14 -0500251 def dump_siglist(self, sigfile, path_prefix_strip=None):
252 def strip_fn(fn):
253 nonlocal path_prefix_strip
254 if not path_prefix_strip:
255 return fn
256
257 fn_exp = fn.split(":")
258 if fn_exp[-1].startswith(path_prefix_strip):
259 fn_exp[-1] = fn_exp[-1][len(path_prefix_strip):]
260
261 return ":".join(fn_exp)
262
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500263 with open(sigfile, "w") as f:
264 tasks = []
265 for taskitem in self.taskhash:
Brad Bishop08902b02019-08-20 09:16:51 -0400266 (fn, task) = taskitem.rsplit(":", 1)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500267 pn = self.lockedpnmap[fn]
Andrew Geissler09036742021-06-25 14:25:14 -0500268 tasks.append((pn, task, strip_fn(fn), self.taskhash[taskitem]))
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500269 for (pn, task, fn, taskhash) in sorted(tasks):
Brad Bishop08902b02019-08-20 09:16:51 -0400270 f.write('%s:%s %s %s\n' % (pn, task, fn, taskhash))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500271
Brad Bishop08902b02019-08-20 09:16:51 -0400272 def checkhashes(self, sq_data, missed, found, d):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500273 warn_msgs = []
274 error_msgs = []
275 sstate_missing_msgs = []
276
Brad Bishop08902b02019-08-20 09:16:51 -0400277 for tid in sq_data['hash']:
278 if tid not in found:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500279 for pn in self.lockedsigs:
Brad Bishop08902b02019-08-20 09:16:51 -0400280 taskname = bb.runqueue.taskname_from_tid(tid)
281 if sq_data['hash'][tid] in iter(self.lockedsigs[pn].values()):
282 if taskname == 'do_shared_workdir':
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500283 continue
284 sstate_missing_msgs.append("Locked sig is set for %s:%s (%s) yet not in sstate cache?"
Brad Bishop08902b02019-08-20 09:16:51 -0400285 % (pn, taskname, sq_data['hash'][tid]))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500286
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500287 checklevel = d.getVar("SIGGEN_LOCKEDSIGS_TASKSIG_CHECK")
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500288 if checklevel == 'warn':
289 warn_msgs += self.mismatch_msgs
290 elif checklevel == 'error':
291 error_msgs += self.mismatch_msgs
292
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500293 checklevel = d.getVar("SIGGEN_LOCKEDSIGS_SSTATE_EXISTS_CHECK")
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500294 if checklevel == 'warn':
295 warn_msgs += sstate_missing_msgs
296 elif checklevel == 'error':
297 error_msgs += sstate_missing_msgs
298
299 if warn_msgs:
300 bb.warn("\n".join(warn_msgs))
301 if error_msgs:
302 bb.fatal("\n".join(error_msgs))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500303
Brad Bishop00e122a2019-10-05 11:10:57 -0400304class SignatureGeneratorOEBasicHash(SignatureGeneratorOEBasicHashMixIn, bb.siggen.SignatureGeneratorBasicHash):
305 name = "OEBasicHash"
306
307class SignatureGeneratorOEEquivHash(SignatureGeneratorOEBasicHashMixIn, bb.siggen.SignatureGeneratorUniHashMixIn, bb.siggen.SignatureGeneratorBasicHash):
Brad Bishop19323692019-04-05 15:28:33 -0400308 name = "OEEquivHash"
309
310 def init_rundepcheck(self, data):
311 super().init_rundepcheck(data)
Brad Bishopa34c0302019-09-23 22:34:48 -0400312 self.server = data.getVar('BB_HASHSERVE')
Brad Bishop08902b02019-08-20 09:16:51 -0400313 if not self.server:
Brad Bishopa34c0302019-09-23 22:34:48 -0400314 bb.fatal("OEEquivHash requires BB_HASHSERVE to be set")
Brad Bishop19323692019-04-05 15:28:33 -0400315 self.method = data.getVar('SSTATE_HASHEQUIV_METHOD')
Brad Bishop08902b02019-08-20 09:16:51 -0400316 if not self.method:
317 bb.fatal("OEEquivHash requires SSTATE_HASHEQUIV_METHOD to be set")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500318
319# Insert these classes into siggen's namespace so it can see and select them
320bb.siggen.SignatureGeneratorOEBasic = SignatureGeneratorOEBasic
321bb.siggen.SignatureGeneratorOEBasicHash = SignatureGeneratorOEBasicHash
Brad Bishop19323692019-04-05 15:28:33 -0400322bb.siggen.SignatureGeneratorOEEquivHash = SignatureGeneratorOEEquivHash
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500323
324
325def find_siginfo(pn, taskname, taskhashlist, d):
326 """ Find signature data files for comparison purposes """
327
328 import fnmatch
329 import glob
330
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500331 if not taskname:
332 # We have to derive pn and taskname
333 key = pn
Brad Bishop08902b02019-08-20 09:16:51 -0400334 splitit = key.split('.bb:')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500335 taskname = splitit[1]
336 pn = os.path.basename(splitit[0]).split('_')[0]
337 if key.startswith('virtual:native:'):
338 pn = pn + '-native'
339
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500340 hashfiles = {}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500341 filedates = {}
342
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500343 def get_hashval(siginfo):
344 if siginfo.endswith('.siginfo'):
345 return siginfo.rpartition(':')[2].partition('_')[0]
346 else:
347 return siginfo.rpartition('.')[2]
348
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500349 # First search in stamps dir
350 localdata = d.createCopy()
351 localdata.setVar('MULTIMACH_TARGET_SYS', '*')
352 localdata.setVar('PN', pn)
353 localdata.setVar('PV', '*')
354 localdata.setVar('PR', '*')
355 localdata.setVar('EXTENDPE', '')
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500356 stamp = localdata.getVar('STAMP')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500357 if pn.startswith("gcc-source"):
358 # gcc-source shared workdir is a special case :(
359 stamp = localdata.expand("${STAMPS_DIR}/work-shared/gcc-${PV}-${PR}")
360
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500361 filespec = '%s.%s.sigdata.*' % (stamp, taskname)
362 foundall = False
363 import glob
364 for fullpath in glob.glob(filespec):
365 match = False
366 if taskhashlist:
367 for taskhash in taskhashlist:
368 if fullpath.endswith('.%s' % taskhash):
369 hashfiles[taskhash] = fullpath
370 if len(hashfiles) == len(taskhashlist):
371 foundall = True
372 break
373 else:
374 try:
375 filedates[fullpath] = os.stat(fullpath).st_mtime
376 except OSError:
377 continue
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500378 hashval = get_hashval(fullpath)
379 hashfiles[hashval] = fullpath
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500380
381 if not taskhashlist or (len(filedates) < 2 and not foundall):
382 # That didn't work, look in sstate-cache
Brad Bishop19323692019-04-05 15:28:33 -0400383 hashes = taskhashlist or ['?' * 64]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500384 localdata = bb.data.createCopy(d)
385 for hashval in hashes:
386 localdata.setVar('PACKAGE_ARCH', '*')
387 localdata.setVar('TARGET_VENDOR', '*')
388 localdata.setVar('TARGET_OS', '*')
389 localdata.setVar('PN', pn)
390 localdata.setVar('PV', '*')
391 localdata.setVar('PR', '*')
392 localdata.setVar('BB_TASKHASH', hashval)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500393 swspec = localdata.getVar('SSTATE_SWSPEC')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500394 if taskname in ['do_fetch', 'do_unpack', 'do_patch', 'do_populate_lic', 'do_preconfigure'] and swspec:
395 localdata.setVar('SSTATE_PKGSPEC', '${SSTATE_SWSPEC}')
396 elif pn.endswith('-native') or "-cross-" in pn or "-crosssdk-" in pn:
397 localdata.setVar('SSTATE_EXTRAPATH', "${NATIVELSBSTRING}/")
398 sstatename = taskname[3:]
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500399 filespec = '%s_%s.*.siginfo' % (localdata.getVar('SSTATE_PKG'), sstatename)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500400
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500401 matchedfiles = glob.glob(filespec)
402 for fullpath in matchedfiles:
403 actual_hashval = get_hashval(fullpath)
404 if actual_hashval in hashfiles:
405 continue
406 hashfiles[hashval] = fullpath
407 if not taskhashlist:
408 try:
409 filedates[fullpath] = os.stat(fullpath).st_mtime
410 except:
411 continue
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500412
413 if taskhashlist:
414 return hashfiles
415 else:
416 return filedates
417
418bb.siggen.find_siginfo = find_siginfo
419
420
421def sstate_get_manifest_filename(task, d):
422 """
423 Return the sstate manifest file path for a particular task.
424 Also returns the datastore that can be used to query related variables.
425 """
426 d2 = d.createCopy()
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500427 extrainf = d.getVarFlag("do_" + task, 'stamp-extra-info')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500428 if extrainf:
429 d2.setVar("SSTATE_MANMACH", extrainf)
430 return (d2.expand("${SSTATE_MANFILEPREFIX}.%s" % task), d2)
Brad Bishop316dfdd2018-06-25 12:45:53 -0400431
432def find_sstate_manifest(taskdata, taskdata2, taskname, d, multilibcache):
433 d2 = d
434 variant = ''
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800435 curr_variant = ''
436 if d.getVar("BBEXTENDCURR") == "multilib":
437 curr_variant = d.getVar("BBEXTENDVARIANT")
438 if "virtclass-multilib" not in d.getVar("OVERRIDES"):
439 curr_variant = "invalid"
Brad Bishop316dfdd2018-06-25 12:45:53 -0400440 if taskdata2.startswith("virtual:multilib"):
441 variant = taskdata2.split(":")[2]
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800442 if curr_variant != variant:
Brad Bishop316dfdd2018-06-25 12:45:53 -0400443 if variant not in multilibcache:
444 multilibcache[variant] = oe.utils.get_multilib_datastore(variant, d)
445 d2 = multilibcache[variant]
446
447 if taskdata.endswith("-native"):
Andrew Geissler6ce62a22020-11-30 19:58:47 -0600448 pkgarchs = ["${BUILD_ARCH}", "${BUILD_ARCH}_${ORIGNATIVELSBSTRING}"]
Brad Bishop316dfdd2018-06-25 12:45:53 -0400449 elif taskdata.startswith("nativesdk-"):
450 pkgarchs = ["${SDK_ARCH}_${SDK_OS}", "allarch"]
451 elif "-cross-canadian" in taskdata:
452 pkgarchs = ["${SDK_ARCH}_${SDK_ARCH}-${SDKPKGSUFFIX}"]
453 elif "-cross-" in taskdata:
454 pkgarchs = ["${BUILD_ARCH}_${TARGET_ARCH}"]
455 elif "-crosssdk" in taskdata:
456 pkgarchs = ["${BUILD_ARCH}_${SDK_ARCH}_${SDK_OS}"]
457 else:
458 pkgarchs = ['${MACHINE_ARCH}']
459 pkgarchs = pkgarchs + list(reversed(d2.getVar("PACKAGE_EXTRA_ARCHS").split()))
460 pkgarchs.append('allarch')
461 pkgarchs.append('${SDK_ARCH}_${SDK_ARCH}-${SDKPKGSUFFIX}')
462
463 for pkgarch in pkgarchs:
464 manifest = d2.expand("${SSTATE_MANIFESTS}/manifest-%s-%s.%s" % (pkgarch, taskdata, taskname))
465 if os.path.exists(manifest):
466 return manifest, d2
William A. Kennington IIIac69b482021-06-02 12:28:27 -0700467 bb.fatal("Manifest %s not found in %s (variant '%s')?" % (manifest, d2.expand(" ".join(pkgarchs)), variant))
Brad Bishop316dfdd2018-06-25 12:45:53 -0400468 return None, d2
469
Brad Bishop19323692019-04-05 15:28:33 -0400470def OEOuthashBasic(path, sigfile, task, d):
471 """
472 Basic output hash function
473
474 Calculates the output hash of a task by hashing all output file metadata,
475 and file contents.
476 """
477 import hashlib
478 import stat
479 import pwd
480 import grp
481
482 def update_hash(s):
483 s = s.encode('utf-8')
484 h.update(s)
485 if sigfile:
486 sigfile.write(s)
487
488 h = hashlib.sha256()
489 prev_dir = os.getcwd()
490 include_owners = os.environ.get('PSEUDO_DISABLED') == '0'
Andrew Geisslerf0343792020-11-18 10:42:21 -0600491 if "package_write_" in task or task == "package_qa":
492 include_owners = False
Andrew Geissler6ce62a22020-11-30 19:58:47 -0600493 include_timestamps = False
494 if task == "package":
495 include_timestamps = d.getVar('BUILD_REPRODUCIBLE_BINARIES') == '1'
Andrew Geissler82c905d2020-04-13 13:39:40 -0500496 extra_content = d.getVar('HASHEQUIV_HASH_VERSION')
Brad Bishop19323692019-04-05 15:28:33 -0400497
498 try:
499 os.chdir(path)
500
501 update_hash("OEOuthashBasic\n")
Andrew Geissler82c905d2020-04-13 13:39:40 -0500502 if extra_content:
503 update_hash(extra_content + "\n")
Brad Bishop19323692019-04-05 15:28:33 -0400504
505 # It is only currently useful to get equivalent hashes for things that
506 # can be restored from sstate. Since the sstate object is named using
507 # SSTATE_PKGSPEC and the task name, those should be included in the
508 # output hash calculation.
509 update_hash("SSTATE_PKGSPEC=%s\n" % d.getVar('SSTATE_PKGSPEC'))
510 update_hash("task=%s\n" % task)
511
512 for root, dirs, files in os.walk('.', topdown=True):
513 # Sort directories to ensure consistent ordering when recursing
514 dirs.sort()
515 files.sort()
516
517 def process(path):
518 s = os.lstat(path)
519
520 if stat.S_ISDIR(s.st_mode):
521 update_hash('d')
522 elif stat.S_ISCHR(s.st_mode):
523 update_hash('c')
524 elif stat.S_ISBLK(s.st_mode):
525 update_hash('b')
526 elif stat.S_ISSOCK(s.st_mode):
527 update_hash('s')
528 elif stat.S_ISLNK(s.st_mode):
529 update_hash('l')
530 elif stat.S_ISFIFO(s.st_mode):
531 update_hash('p')
532 else:
533 update_hash('-')
534
535 def add_perm(mask, on, off='-'):
536 if mask & s.st_mode:
537 update_hash(on)
538 else:
539 update_hash(off)
540
541 add_perm(stat.S_IRUSR, 'r')
542 add_perm(stat.S_IWUSR, 'w')
543 if stat.S_ISUID & s.st_mode:
544 add_perm(stat.S_IXUSR, 's', 'S')
545 else:
546 add_perm(stat.S_IXUSR, 'x')
547
548 add_perm(stat.S_IRGRP, 'r')
549 add_perm(stat.S_IWGRP, 'w')
550 if stat.S_ISGID & s.st_mode:
551 add_perm(stat.S_IXGRP, 's', 'S')
552 else:
553 add_perm(stat.S_IXGRP, 'x')
554
555 add_perm(stat.S_IROTH, 'r')
556 add_perm(stat.S_IWOTH, 'w')
557 if stat.S_ISVTX & s.st_mode:
558 update_hash('t')
559 else:
560 add_perm(stat.S_IXOTH, 'x')
561
562 if include_owners:
Andrew Geissler82c905d2020-04-13 13:39:40 -0500563 try:
564 update_hash(" %10s" % pwd.getpwuid(s.st_uid).pw_name)
565 update_hash(" %10s" % grp.getgrgid(s.st_gid).gr_name)
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600566 except KeyError as e:
Andrew Geissler82c905d2020-04-13 13:39:40 -0500567 bb.warn("KeyError in %s" % path)
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600568 msg = ("KeyError: %s\nPath %s is owned by uid %d, gid %d, which doesn't match "
569 "any user/group on target. This may be due to host contamination." % (e, path, s.st_uid, s.st_gid))
570 raise Exception(msg).with_traceback(e.__traceback__)
Brad Bishop19323692019-04-05 15:28:33 -0400571
Andrew Geissler6ce62a22020-11-30 19:58:47 -0600572 if include_timestamps:
573 update_hash(" %10d" % s.st_mtime)
574
Brad Bishop19323692019-04-05 15:28:33 -0400575 update_hash(" ")
576 if stat.S_ISBLK(s.st_mode) or stat.S_ISCHR(s.st_mode):
577 update_hash("%9s" % ("%d.%d" % (os.major(s.st_rdev), os.minor(s.st_rdev))))
578 else:
579 update_hash(" " * 9)
580
581 update_hash(" ")
582 if stat.S_ISREG(s.st_mode):
583 update_hash("%10d" % s.st_size)
584 else:
585 update_hash(" " * 10)
586
587 update_hash(" ")
588 fh = hashlib.sha256()
589 if stat.S_ISREG(s.st_mode):
590 # Hash file contents
591 with open(path, 'rb') as d:
592 for chunk in iter(lambda: d.read(4096), b""):
593 fh.update(chunk)
594 update_hash(fh.hexdigest())
595 else:
596 update_hash(" " * len(fh.hexdigest()))
597
598 update_hash(" %s" % path)
599
600 if stat.S_ISLNK(s.st_mode):
601 update_hash(" -> %s" % os.readlink(path))
602
603 update_hash("\n")
604
605 # Process this directory and all its child files
606 process(root)
607 for f in files:
608 if f == 'fixmepath':
609 continue
610 process(os.path.join(root, f))
611 finally:
612 os.chdir(prev_dir)
613
614 return h.hexdigest()
615
Brad Bishop316dfdd2018-06-25 12:45:53 -0400616