blob: 04218853062c28898126162a6a9575213a02709e [file] [log] [blame]
Brad Bishopc342db32019-05-15 21:57:59 -04001#
Patrick Williams92b42cb2022-09-03 06:53:57 -05002# Copyright BitBake Contributors
3#
Brad Bishopc342db32019-05-15 21:57:59 -04004# SPDX-License-Identifier: GPL-2.0-only
5#
6
Patrick Williamsc124f4f2015-09-15 14:41:29 -05007import hashlib
8import logging
9import os
10import re
11import tempfile
Patrick Williamsc0f7c042017-02-23 20:41:17 -060012import pickle
Patrick Williamsc124f4f2015-09-15 14:41:29 -050013import bb.data
Brad Bishop6e60e8b2018-02-01 10:27:11 -050014import difflib
15import simplediff
Andrew Geisslereff27472021-10-29 15:35:00 -050016import json
Andrew Geissler517393d2023-01-13 08:55:19 -060017import types
Patrick Williams44b3caf2024-04-12 16:51:14 -050018from contextlib import contextmanager
Andrew Geisslereff27472021-10-29 15:35:00 -050019import bb.compress.zstd
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050020from bb.checksum import FileChecksumCache
Brad Bishop08902b02019-08-20 09:16:51 -040021from bb import runqueue
Brad Bishopa34c0302019-09-23 22:34:48 -040022import hashserv
Andrew Geissler475cb722020-07-10 16:00:51 -050023import hashserv.client
Patrick Williamsc124f4f2015-09-15 14:41:29 -050024
25logger = logging.getLogger('BitBake.SigGen')
Andrew Geissler82c905d2020-04-13 13:39:40 -050026hashequiv_logger = logging.getLogger('BitBake.SigGen.HashEquiv')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050027
Patrick Williams169d7bc2024-01-05 11:33:25 -060028#find_siginfo and find_siginfo_version are set by the metadata siggen
29# The minimum version of the find_siginfo function we need
30find_siginfo_minversion = 2
31
Patrick Williams44b3caf2024-04-12 16:51:14 -050032HASHSERV_ENVVARS = [
33 "SSL_CERT_DIR",
34 "SSL_CERT_FILE",
35 "NO_PROXY",
36 "HTTPS_PROXY",
37 "HTTP_PROXY"
38]
39
Patrick Williams169d7bc2024-01-05 11:33:25 -060040def check_siggen_version(siggen):
41 if not hasattr(siggen, "find_siginfo_version"):
42 bb.fatal("Siggen from metadata (OE-Core?) is too old, please update it (no version found)")
43 if siggen.find_siginfo_version < siggen.find_siginfo_minversion:
44 bb.fatal("Siggen from metadata (OE-Core?) is too old, please update it (%s vs %s)" % (siggen.find_siginfo_version, siggen.find_siginfo_minversion))
45
Andrew Geisslereff27472021-10-29 15:35:00 -050046class SetEncoder(json.JSONEncoder):
47 def default(self, obj):
Andrew Geissler517393d2023-01-13 08:55:19 -060048 if isinstance(obj, set) or isinstance(obj, frozenset):
Andrew Geisslereff27472021-10-29 15:35:00 -050049 return dict(_set_object=list(sorted(obj)))
50 return json.JSONEncoder.default(self, obj)
51
52def SetDecoder(dct):
53 if '_set_object' in dct:
Andrew Geissler517393d2023-01-13 08:55:19 -060054 return frozenset(dct['_set_object'])
Andrew Geisslereff27472021-10-29 15:35:00 -050055 return dct
56
Patrick Williamsc124f4f2015-09-15 14:41:29 -050057def init(d):
Patrick Williamsc0f7c042017-02-23 20:41:17 -060058 siggens = [obj for obj in globals().values()
Patrick Williamsc124f4f2015-09-15 14:41:29 -050059 if type(obj) is type and issubclass(obj, SignatureGenerator)]
60
Brad Bishop6e60e8b2018-02-01 10:27:11 -050061 desired = d.getVar("BB_SIGNATURE_HANDLER") or "noop"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050062 for sg in siggens:
63 if desired == sg.name:
64 return sg(d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050065 else:
66 logger.error("Invalid signature generator '%s', using default 'noop'\n"
67 "Available generators: %s", desired,
68 ', '.join(obj.name for obj in siggens))
69 return SignatureGenerator(d)
70
71class SignatureGenerator(object):
72 """
73 """
74 name = "noop"
75
76 def __init__(self, data):
Brad Bishop37a0e4d2017-12-04 01:01:44 -050077 self.basehash = {}
Patrick Williamsc124f4f2015-09-15 14:41:29 -050078 self.taskhash = {}
Andrew Geissler82c905d2020-04-13 13:39:40 -050079 self.unihash = {}
Patrick Williamsc124f4f2015-09-15 14:41:29 -050080 self.runtaskdeps = {}
81 self.file_checksum_values = {}
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050082 self.taints = {}
Brad Bishop08902b02019-08-20 09:16:51 -040083 self.unitaskhashes = {}
Andrew Geissler82c905d2020-04-13 13:39:40 -050084 self.tidtopn = {}
85 self.setscenetasks = set()
Patrick Williamsc124f4f2015-09-15 14:41:29 -050086
87 def finalise(self, fn, d, varient):
88 return
89
Andrew Geissler82c905d2020-04-13 13:39:40 -050090 def postparsing_clean_cache(self):
91 return
92
Andrew Geissler517393d2023-01-13 08:55:19 -060093 def setup_datacache(self, datacaches):
94 self.datacaches = datacaches
95
96 def setup_datacache_from_datastore(self, mcfn, d):
97 # In task context we have no cache so setup internal data structures
98 # from the fully parsed data store provided
99
100 mc = d.getVar("__BBMULTICONFIG", False) or ""
101 tasks = d.getVar('__BBTASKS', False)
102
103 self.datacaches = {}
104 self.datacaches[mc] = types.SimpleNamespace()
105 setattr(self.datacaches[mc], "stamp", {})
106 self.datacaches[mc].stamp[mcfn] = d.getVar('STAMP')
107 setattr(self.datacaches[mc], "stamp_extrainfo", {})
108 self.datacaches[mc].stamp_extrainfo[mcfn] = {}
109 for t in tasks:
110 flag = d.getVarFlag(t, "stamp-extra-info")
111 if flag:
112 self.datacaches[mc].stamp_extrainfo[mcfn][t] = flag
113
Patrick Williams73bd93f2024-02-20 08:07:48 -0600114 def get_cached_unihash(self, tid):
115 return None
116
Brad Bishop08902b02019-08-20 09:16:51 -0400117 def get_unihash(self, tid):
Patrick Williams73bd93f2024-02-20 08:07:48 -0600118 unihash = self.get_cached_unihash(tid)
119 if unihash:
120 return unihash
Brad Bishop08902b02019-08-20 09:16:51 -0400121 return self.taskhash[tid]
Brad Bishop19323692019-04-05 15:28:33 -0400122
Patrick Williams73bd93f2024-02-20 08:07:48 -0600123 def get_unihashes(self, tids):
124 return {tid: self.get_unihash(tid) for tid in tids}
125
Andrew Geissler5a43b432020-06-13 10:46:56 -0500126 def prep_taskhash(self, tid, deps, dataCaches):
Andrew Geissler82c905d2020-04-13 13:39:40 -0500127 return
128
Andrew Geissler5a43b432020-06-13 10:46:56 -0500129 def get_taskhash(self, tid, deps, dataCaches):
Brad Bishop08902b02019-08-20 09:16:51 -0400130 self.taskhash[tid] = hashlib.sha256(tid.encode("utf-8")).hexdigest()
131 return self.taskhash[tid]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500132
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500133 def writeout_file_checksum_cache(self):
134 """Write/update the file checksum cache onto disk"""
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500135 return
136
Andrew Geissler517393d2023-01-13 08:55:19 -0600137 def stampfile_base(self, mcfn):
138 mc = bb.runqueue.mc_from_tid(mcfn)
139 return self.datacaches[mc].stamp[mcfn]
140
141 def stampfile_mcfn(self, taskname, mcfn, extrainfo=True):
142 mc = bb.runqueue.mc_from_tid(mcfn)
143 stamp = self.datacaches[mc].stamp[mcfn]
144 if not stamp:
145 return
146
147 stamp_extrainfo = ""
148 if extrainfo:
149 taskflagname = taskname
150 if taskname.endswith("_setscene"):
151 taskflagname = taskname.replace("_setscene", "")
152 stamp_extrainfo = self.datacaches[mc].stamp_extrainfo[mcfn].get(taskflagname) or ""
153
154 return self.stampfile(stamp, mcfn, taskname, stamp_extrainfo)
155
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500156 def stampfile(self, stampbase, file_name, taskname, extrainfo):
157 return ("%s.%s.%s" % (stampbase, taskname, extrainfo)).rstrip('.')
158
Andrew Geissler517393d2023-01-13 08:55:19 -0600159 def stampcleanmask_mcfn(self, taskname, mcfn):
160 mc = bb.runqueue.mc_from_tid(mcfn)
161 stamp = self.datacaches[mc].stamp[mcfn]
162 if not stamp:
163 return []
164
165 taskflagname = taskname
166 if taskname.endswith("_setscene"):
167 taskflagname = taskname.replace("_setscene", "")
168 stamp_extrainfo = self.datacaches[mc].stamp_extrainfo[mcfn].get(taskflagname) or ""
169
170 return self.stampcleanmask(stamp, mcfn, taskname, stamp_extrainfo)
171
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500172 def stampcleanmask(self, stampbase, file_name, taskname, extrainfo):
173 return ("%s.%s.%s" % (stampbase, taskname, extrainfo)).rstrip('.')
174
Andrew Geissler517393d2023-01-13 08:55:19 -0600175 def dump_sigtask(self, mcfn, task, stampbase, runtime):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500176 return
177
Andrew Geissler517393d2023-01-13 08:55:19 -0600178 def invalidate_task(self, task, mcfn):
179 mc = bb.runqueue.mc_from_tid(mcfn)
180 stamp = self.datacaches[mc].stamp[mcfn]
181 bb.utils.remove(stamp)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500182
183 def dump_sigs(self, dataCache, options):
184 return
185
186 def get_taskdata(self):
Andrew Geissler82c905d2020-04-13 13:39:40 -0500187 return (self.runtaskdeps, self.taskhash, self.unihash, self.file_checksum_values, self.taints, self.basehash, self.unitaskhashes, self.tidtopn, self.setscenetasks)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500188
189 def set_taskdata(self, data):
Andrew Geissler82c905d2020-04-13 13:39:40 -0500190 self.runtaskdeps, self.taskhash, self.unihash, self.file_checksum_values, self.taints, self.basehash, self.unitaskhashes, self.tidtopn, self.setscenetasks = data
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500191
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500192 def reset(self, data):
193 self.__init__(data)
194
Brad Bishop08902b02019-08-20 09:16:51 -0400195 def get_taskhashes(self):
Andrew Geissler82c905d2020-04-13 13:39:40 -0500196 return self.taskhash, self.unihash, self.unitaskhashes, self.tidtopn
Brad Bishop08902b02019-08-20 09:16:51 -0400197
198 def set_taskhashes(self, hashes):
Andrew Geissler82c905d2020-04-13 13:39:40 -0500199 self.taskhash, self.unihash, self.unitaskhashes, self.tidtopn = hashes
Brad Bishop08902b02019-08-20 09:16:51 -0400200
201 def save_unitaskhashes(self):
202 return
203
Andrew Geissler78b72792022-06-14 06:47:25 -0500204 def copy_unitaskhashes(self, targetdir):
205 return
206
Brad Bishopa34c0302019-09-23 22:34:48 -0400207 def set_setscene_tasks(self, setscene_tasks):
208 return
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500209
Andrew Geissler9aee5002022-03-30 16:27:02 +0000210 def exit(self):
211 return
212
Patrick Williams2a254922023-08-11 09:48:11 -0500213def build_pnid(mc, pn, taskname):
214 if mc:
215 return "mc:" + mc + ":" + pn + ":" + taskname
216 return pn + ":" + taskname
217
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500218class SignatureGeneratorBasic(SignatureGenerator):
219 """
220 """
221 name = "basic"
222
223 def __init__(self, data):
224 self.basehash = {}
225 self.taskhash = {}
Andrew Geissler82c905d2020-04-13 13:39:40 -0500226 self.unihash = {}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500227 self.runtaskdeps = {}
228 self.file_checksum_values = {}
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500229 self.taints = {}
Andrew Geissler82c905d2020-04-13 13:39:40 -0500230 self.setscenetasks = set()
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000231 self.basehash_ignore_vars = set((data.getVar("BB_BASEHASH_IGNORE_VARS") or "").split())
232 self.taskhash_ignore_tasks = None
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500233 self.init_rundepcheck(data)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500234 checksum_cache_file = data.getVar("BB_HASH_CHECKSUM_CACHE_FILE")
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500235 if checksum_cache_file:
236 self.checksum_cache = FileChecksumCache()
237 self.checksum_cache.init_cache(data, checksum_cache_file)
238 else:
239 self.checksum_cache = None
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500240
Andrew Geissler82c905d2020-04-13 13:39:40 -0500241 self.unihash_cache = bb.cache.SimpleCache("3")
Brad Bishop08902b02019-08-20 09:16:51 -0400242 self.unitaskhashes = self.unihash_cache.init_cache(data, "bb_unihashes.dat", {})
Andrew Geissler82c905d2020-04-13 13:39:40 -0500243 self.localdirsexclude = (data.getVar("BB_SIGNATURE_LOCAL_DIRS_EXCLUDE") or "CVS .bzr .git .hg .osc .p4 .repo .svn").split()
244 self.tidtopn = {}
Brad Bishop08902b02019-08-20 09:16:51 -0400245
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500246 def init_rundepcheck(self, data):
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000247 self.taskhash_ignore_tasks = data.getVar("BB_TASKHASH_IGNORE_TASKS") or None
248 if self.taskhash_ignore_tasks:
249 self.twl = re.compile(self.taskhash_ignore_tasks)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500250 else:
251 self.twl = None
252
Andrew Geissler517393d2023-01-13 08:55:19 -0600253 def _build_data(self, mcfn, d):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500254
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500255 ignore_mismatch = ((d.getVar("BB_HASH_IGNORE_MISMATCH") or '') == '1')
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000256 tasklist, gendeps, lookupcache = bb.data.generate_dependencies(d, self.basehash_ignore_vars)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500257
Andrew Geissler517393d2023-01-13 08:55:19 -0600258 taskdeps, basehash = bb.data.generate_dependency_hash(tasklist, gendeps, lookupcache, self.basehash_ignore_vars, mcfn)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500259
260 for task in tasklist:
Andrew Geissler517393d2023-01-13 08:55:19 -0600261 tid = mcfn + ":" + task
Brad Bishop08902b02019-08-20 09:16:51 -0400262 if not ignore_mismatch and tid in self.basehash and self.basehash[tid] != basehash[tid]:
263 bb.error("When reparsing %s, the basehash value changed from %s to %s. The metadata is not deterministic and this needs to be fixed." % (tid, self.basehash[tid], basehash[tid]))
Brad Bishopc342db32019-05-15 21:57:59 -0400264 bb.error("The following commands may help:")
265 cmd = "$ bitbake %s -c%s" % (d.getVar('PN'), task)
266 # Make sure sigdata is dumped before run printdiff
267 bb.error("%s -Snone" % cmd)
268 bb.error("Then:")
269 bb.error("%s -Sprintdiff\n" % cmd)
Brad Bishop08902b02019-08-20 09:16:51 -0400270 self.basehash[tid] = basehash[tid]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500271
Andrew Geissler517393d2023-01-13 08:55:19 -0600272 return taskdeps, gendeps, lookupcache
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500273
Brad Bishopa34c0302019-09-23 22:34:48 -0400274 def set_setscene_tasks(self, setscene_tasks):
Andrew Geissler82c905d2020-04-13 13:39:40 -0500275 self.setscenetasks = set(setscene_tasks)
Brad Bishopa34c0302019-09-23 22:34:48 -0400276
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500277 def finalise(self, fn, d, variant):
278
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600279 mc = d.getVar("__BBMULTICONFIG", False) or ""
Andrew Geissler517393d2023-01-13 08:55:19 -0600280 mcfn = fn
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600281 if variant or mc:
Andrew Geissler517393d2023-01-13 08:55:19 -0600282 mcfn = bb.cache.realfn2virtual(fn, variant, mc)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500283
284 try:
Andrew Geissler517393d2023-01-13 08:55:19 -0600285 taskdeps, gendeps, lookupcache = self._build_data(mcfn, d)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500286 except bb.parse.SkipRecipe:
287 raise
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500288 except:
Andrew Geissler517393d2023-01-13 08:55:19 -0600289 bb.warn("Error during finalise of %s" % mcfn)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500290 raise
291
Andrew Geissler517393d2023-01-13 08:55:19 -0600292 basehashes = {}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500293 for task in taskdeps:
Andrew Geissler517393d2023-01-13 08:55:19 -0600294 basehashes[task] = self.basehash[mcfn + ":" + task]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500295
Andrew Geissler517393d2023-01-13 08:55:19 -0600296 d.setVar("__siggen_basehashes", basehashes)
297 d.setVar("__siggen_gendeps", gendeps)
298 d.setVar("__siggen_varvals", lookupcache)
299 d.setVar("__siggen_taskdeps", taskdeps)
300
Andrew Geissler5082cc72023-09-11 08:41:39 -0400301 #Slow but can be useful for debugging mismatched basehashes
302 #self.setup_datacache_from_datastore(mcfn, d)
303 #for task in taskdeps:
304 # self.dump_sigtask(mcfn, task, d.getVar("STAMP"), False)
305
Andrew Geissler517393d2023-01-13 08:55:19 -0600306 def setup_datacache_from_datastore(self, mcfn, d):
307 super().setup_datacache_from_datastore(mcfn, d)
308
309 mc = bb.runqueue.mc_from_tid(mcfn)
310 for attr in ["siggen_varvals", "siggen_taskdeps", "siggen_gendeps"]:
311 if not hasattr(self.datacaches[mc], attr):
312 setattr(self.datacaches[mc], attr, {})
313 self.datacaches[mc].siggen_varvals[mcfn] = d.getVar("__siggen_varvals")
314 self.datacaches[mc].siggen_taskdeps[mcfn] = d.getVar("__siggen_taskdeps")
315 self.datacaches[mc].siggen_gendeps[mcfn] = d.getVar("__siggen_gendeps")
Andrew Geissler82c905d2020-04-13 13:39:40 -0500316
Andrew Geissler5a43b432020-06-13 10:46:56 -0500317 def rundep_check(self, fn, recipename, task, dep, depname, dataCaches):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500318 # Return True if we should keep the dependency, False to drop it
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000319 # We only manipulate the dependencies for packages not in the ignore
320 # list
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500321 if self.twl and not self.twl.search(recipename):
322 # then process the actual dependencies
323 if self.twl.search(depname):
324 return False
325 return True
326
327 def read_taint(self, fn, task, stampbase):
328 taint = None
329 try:
330 with open(stampbase + '.' + task + '.taint', 'r') as taintf:
331 taint = taintf.read()
332 except IOError:
333 pass
334 return taint
335
Andrew Geissler5a43b432020-06-13 10:46:56 -0500336 def prep_taskhash(self, tid, deps, dataCaches):
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800337
Andrew Geissler517393d2023-01-13 08:55:19 -0600338 (mc, _, task, mcfn) = bb.runqueue.split_tid_mcfn(tid)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800339
Andrew Geissler5a43b432020-06-13 10:46:56 -0500340 self.basehash[tid] = dataCaches[mc].basetaskhash[tid]
Brad Bishop08902b02019-08-20 09:16:51 -0400341 self.runtaskdeps[tid] = []
342 self.file_checksum_values[tid] = []
Andrew Geissler517393d2023-01-13 08:55:19 -0600343 recipename = dataCaches[mc].pkg_fn[mcfn]
Andrew Geissler82c905d2020-04-13 13:39:40 -0500344
345 self.tidtopn[tid] = recipename
Patrick Williams2a254922023-08-11 09:48:11 -0500346 # save hashfn for deps into siginfo?
347 for dep in deps:
348 (depmc, _, deptask, depmcfn) = bb.runqueue.split_tid_mcfn(dep)
349 dep_pn = dataCaches[depmc].pkg_fn[depmcfn]
Andrew Geissler82c905d2020-04-13 13:39:40 -0500350
Patrick Williams2a254922023-08-11 09:48:11 -0500351 if not self.rundep_check(mcfn, recipename, task, dep, dep_pn, dataCaches):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500352 continue
Patrick Williams2a254922023-08-11 09:48:11 -0500353
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500354 if dep not in self.taskhash:
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800355 bb.fatal("%s is not in taskhash, caller isn't calling in dependency order?" % dep)
Patrick Williams2a254922023-08-11 09:48:11 -0500356
357 dep_pnid = build_pnid(depmc, dep_pn, deptask)
358 self.runtaskdeps[tid].append((dep_pnid, dep))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500359
Andrew Geissler517393d2023-01-13 08:55:19 -0600360 if task in dataCaches[mc].file_checksums[mcfn]:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500361 if self.checksum_cache:
Andrew Geissler517393d2023-01-13 08:55:19 -0600362 checksums = self.checksum_cache.get_checksums(dataCaches[mc].file_checksums[mcfn][task], recipename, self.localdirsexclude)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500363 else:
Andrew Geissler517393d2023-01-13 08:55:19 -0600364 checksums = bb.fetch2.get_file_checksums(dataCaches[mc].file_checksums[mcfn][task], recipename, self.localdirsexclude)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500365 for (f,cs) in checksums:
Brad Bishop08902b02019-08-20 09:16:51 -0400366 self.file_checksum_values[tid].append((f,cs))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500367
Andrew Geissler517393d2023-01-13 08:55:19 -0600368 taskdep = dataCaches[mc].task_deps[mcfn]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500369 if 'nostamp' in taskdep and task in taskdep['nostamp']:
370 # Nostamp tasks need an implicit taint so that they force any dependent tasks to run
Andrew Geissler82c905d2020-04-13 13:39:40 -0500371 if tid in self.taints and self.taints[tid].startswith("nostamp:"):
372 # Don't reset taint value upon every call
373 pass
374 else:
375 import uuid
376 taint = str(uuid.uuid4())
377 self.taints[tid] = "nostamp:" + taint
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500378
Andrew Geissler517393d2023-01-13 08:55:19 -0600379 taint = self.read_taint(mcfn, task, dataCaches[mc].stamp[mcfn])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500380 if taint:
Brad Bishop08902b02019-08-20 09:16:51 -0400381 self.taints[tid] = taint
382 logger.warning("%s is tainted from a forced run" % tid)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500383
Andrew Geissler82c905d2020-04-13 13:39:40 -0500384 return
385
Andrew Geissler5a43b432020-06-13 10:46:56 -0500386 def get_taskhash(self, tid, deps, dataCaches):
Andrew Geissler82c905d2020-04-13 13:39:40 -0500387
388 data = self.basehash[tid]
Patrick Williams2a254922023-08-11 09:48:11 -0500389 for dep in sorted(self.runtaskdeps[tid]):
390 data += self.get_unihash(dep[1])
Andrew Geissler82c905d2020-04-13 13:39:40 -0500391
Andrew Geissler5082cc72023-09-11 08:41:39 -0400392 for (f, cs) in sorted(self.file_checksum_values[tid], key=clean_checksum_file_path):
Andrew Geissler82c905d2020-04-13 13:39:40 -0500393 if cs:
Andrew Geissler595f6302022-01-24 19:11:47 +0000394 if "/./" in f:
Patrick Williams864cc432023-02-09 14:54:44 -0600395 data += "./" + f.split("/./")[1]
396 data += cs
Andrew Geissler82c905d2020-04-13 13:39:40 -0500397
398 if tid in self.taints:
399 if self.taints[tid].startswith("nostamp:"):
Patrick Williams864cc432023-02-09 14:54:44 -0600400 data += self.taints[tid][8:]
Andrew Geissler82c905d2020-04-13 13:39:40 -0500401 else:
Patrick Williams864cc432023-02-09 14:54:44 -0600402 data += self.taints[tid]
Andrew Geissler82c905d2020-04-13 13:39:40 -0500403
Brad Bishop19323692019-04-05 15:28:33 -0400404 h = hashlib.sha256(data.encode("utf-8")).hexdigest()
Brad Bishop08902b02019-08-20 09:16:51 -0400405 self.taskhash[tid] = h
Patrick Williams213cb262021-08-07 19:21:33 -0500406 #d.setVar("BB_TASKHASH:task-%s" % task, taskhash[task])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500407 return h
408
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500409 def writeout_file_checksum_cache(self):
410 """Write/update the file checksum cache onto disk"""
411 if self.checksum_cache:
412 self.checksum_cache.save_extras()
413 self.checksum_cache.save_merge()
414 else:
415 bb.fetch2.fetcher_parse_save()
416 bb.fetch2.fetcher_parse_done()
417
Brad Bishop08902b02019-08-20 09:16:51 -0400418 def save_unitaskhashes(self):
419 self.unihash_cache.save(self.unitaskhashes)
420
Andrew Geissler78b72792022-06-14 06:47:25 -0500421 def copy_unitaskhashes(self, targetdir):
422 self.unihash_cache.copyfile(targetdir)
423
Andrew Geissler517393d2023-01-13 08:55:19 -0600424 def dump_sigtask(self, mcfn, task, stampbase, runtime):
425 tid = mcfn + ":" + task
426 mc = bb.runqueue.mc_from_tid(mcfn)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500427 referencestamp = stampbase
428 if isinstance(runtime, str) and runtime.startswith("customfile"):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500429 sigfile = stampbase
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500430 referencestamp = runtime[11:]
Brad Bishop08902b02019-08-20 09:16:51 -0400431 elif runtime and tid in self.taskhash:
Brad Bishop00e122a2019-10-05 11:10:57 -0400432 sigfile = stampbase + "." + task + ".sigdata" + "." + self.get_unihash(tid)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500433 else:
Brad Bishop08902b02019-08-20 09:16:51 -0400434 sigfile = stampbase + "." + task + ".sigbasedata" + "." + self.basehash[tid]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500435
Andrew Geisslerc3d88e42020-10-02 09:45:00 -0500436 with bb.utils.umask(0o002):
437 bb.utils.mkdirhier(os.path.dirname(sigfile))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500438
439 data = {}
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500440 data['task'] = task
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000441 data['basehash_ignore_vars'] = self.basehash_ignore_vars
442 data['taskhash_ignore_tasks'] = self.taskhash_ignore_tasks
Andrew Geissler517393d2023-01-13 08:55:19 -0600443 data['taskdeps'] = self.datacaches[mc].siggen_taskdeps[mcfn][task]
Brad Bishop08902b02019-08-20 09:16:51 -0400444 data['basehash'] = self.basehash[tid]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500445 data['gendeps'] = {}
446 data['varvals'] = {}
Andrew Geissler517393d2023-01-13 08:55:19 -0600447 data['varvals'][task] = self.datacaches[mc].siggen_varvals[mcfn][task]
448 for dep in self.datacaches[mc].siggen_taskdeps[mcfn][task]:
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000449 if dep in self.basehash_ignore_vars:
Andrew Geissler5082cc72023-09-11 08:41:39 -0400450 continue
Andrew Geissler517393d2023-01-13 08:55:19 -0600451 data['gendeps'][dep] = self.datacaches[mc].siggen_gendeps[mcfn][dep]
452 data['varvals'][dep] = self.datacaches[mc].siggen_varvals[mcfn][dep]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500453
Brad Bishop08902b02019-08-20 09:16:51 -0400454 if runtime and tid in self.taskhash:
Patrick Williams2a254922023-08-11 09:48:11 -0500455 data['runtaskdeps'] = [dep[0] for dep in sorted(self.runtaskdeps[tid])]
Andrew Geissler595f6302022-01-24 19:11:47 +0000456 data['file_checksum_values'] = []
Andrew Geissler5082cc72023-09-11 08:41:39 -0400457 for f,cs in sorted(self.file_checksum_values[tid], key=clean_checksum_file_path):
Andrew Geissler595f6302022-01-24 19:11:47 +0000458 if "/./" in f:
459 data['file_checksum_values'].append(("./" + f.split("/./")[1], cs))
460 else:
461 data['file_checksum_values'].append((os.path.basename(f), cs))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500462 data['runtaskhashes'] = {}
Patrick Williams2a254922023-08-11 09:48:11 -0500463 for dep in self.runtaskdeps[tid]:
464 data['runtaskhashes'][dep[0]] = self.get_unihash(dep[1])
Brad Bishop08902b02019-08-20 09:16:51 -0400465 data['taskhash'] = self.taskhash[tid]
Brad Bishop00e122a2019-10-05 11:10:57 -0400466 data['unihash'] = self.get_unihash(tid)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500467
Andrew Geissler517393d2023-01-13 08:55:19 -0600468 taint = self.read_taint(mcfn, task, referencestamp)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500469 if taint:
470 data['taint'] = taint
471
Brad Bishop08902b02019-08-20 09:16:51 -0400472 if runtime and tid in self.taints:
473 if 'nostamp:' in self.taints[tid]:
474 data['taint'] = self.taints[tid]
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500475
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500476 computed_basehash = calc_basehash(data)
Brad Bishop08902b02019-08-20 09:16:51 -0400477 if computed_basehash != self.basehash[tid]:
478 bb.error("Basehash mismatch %s versus %s for %s" % (computed_basehash, self.basehash[tid], tid))
479 if runtime and tid in self.taskhash:
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500480 computed_taskhash = calc_taskhash(data)
Brad Bishop08902b02019-08-20 09:16:51 -0400481 if computed_taskhash != self.taskhash[tid]:
482 bb.error("Taskhash mismatch %s versus %s for %s" % (computed_taskhash, self.taskhash[tid], tid))
483 sigfile = sigfile.replace(self.taskhash[tid], computed_taskhash)
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500484
Patrick Williams92b42cb2022-09-03 06:53:57 -0500485 fd, tmpfile = bb.utils.mkstemp(dir=os.path.dirname(sigfile), prefix="sigtask.")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500486 try:
Andrew Geisslereff27472021-10-29 15:35:00 -0500487 with bb.compress.zstd.open(fd, "wt", encoding="utf-8", num_threads=1) as f:
488 json.dump(data, f, sort_keys=True, separators=(",", ":"), cls=SetEncoder)
489 f.flush()
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600490 os.chmod(tmpfile, 0o664)
Andrew Geisslerc926e172021-05-07 16:11:35 -0500491 bb.utils.rename(tmpfile, sigfile)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500492 except (OSError, IOError) as err:
493 try:
494 os.unlink(tmpfile)
495 except OSError:
496 pass
497 raise err
498
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500499class SignatureGeneratorBasicHash(SignatureGeneratorBasic):
500 name = "basichash"
501
Brad Bishop08902b02019-08-20 09:16:51 -0400502 def get_stampfile_hash(self, tid):
503 if tid in self.taskhash:
504 return self.taskhash[tid]
Brad Bishop19323692019-04-05 15:28:33 -0400505
506 # If task is not in basehash, then error
Brad Bishop08902b02019-08-20 09:16:51 -0400507 return self.basehash[tid]
Brad Bishop19323692019-04-05 15:28:33 -0400508
Andrew Geissler517393d2023-01-13 08:55:19 -0600509 def stampfile(self, stampbase, mcfn, taskname, extrainfo, clean=False):
510 if taskname.endswith("_setscene"):
511 tid = mcfn + ":" + taskname[:-9]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500512 else:
Andrew Geissler517393d2023-01-13 08:55:19 -0600513 tid = mcfn + ":" + taskname
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500514 if clean:
515 h = "*"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500516 else:
Brad Bishop08902b02019-08-20 09:16:51 -0400517 h = self.get_stampfile_hash(tid)
Brad Bishop19323692019-04-05 15:28:33 -0400518
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500519 return ("%s.%s.%s.%s" % (stampbase, taskname, h, extrainfo)).rstrip('.')
520
Andrew Geissler517393d2023-01-13 08:55:19 -0600521 def stampcleanmask(self, stampbase, mcfn, taskname, extrainfo):
522 return self.stampfile(stampbase, mcfn, taskname, extrainfo, clean=True)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800523
Andrew Geissler517393d2023-01-13 08:55:19 -0600524 def invalidate_task(self, task, mcfn):
525 bb.note("Tainting hash to force rebuild of task %s, %s" % (mcfn, task))
526
527 mc = bb.runqueue.mc_from_tid(mcfn)
528 stamp = self.datacaches[mc].stamp[mcfn]
529
530 taintfn = stamp + '.' + task + '.taint'
531
532 import uuid
533 bb.utils.mkdirhier(os.path.dirname(taintfn))
534 # The specific content of the taint file is not really important,
535 # we just need it to be random, so a random UUID is used
536 with open(taintfn, 'w') as taintf:
537 taintf.write(str(uuid.uuid4()))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500538
Brad Bishop08902b02019-08-20 09:16:51 -0400539class SignatureGeneratorUniHashMixIn(object):
Andrew Geissler82c905d2020-04-13 13:39:40 -0500540 def __init__(self, data):
541 self.extramethod = {}
Patrick Williams73bd93f2024-02-20 08:07:48 -0600542 # NOTE: The cache only tracks hashes that exist. Hashes that don't
543 # exist are always queries from the server since it is possible for
544 # hashes to appear over time, but much less likely for them to
545 # disappear
546 self.unihash_exists_cache = set()
Patrick Williams03514f12024-04-05 07:04:11 -0500547 self.username = None
548 self.password = None
Patrick Williams44b3caf2024-04-12 16:51:14 -0500549 self.env = {}
550
551 origenv = data.getVar("BB_ORIGENV")
552 for e in HASHSERV_ENVVARS:
553 value = data.getVar(e)
554 if not value and origenv:
555 value = origenv.getVar(e)
556 if value:
557 self.env[e] = value
Andrew Geissler82c905d2020-04-13 13:39:40 -0500558 super().__init__(data)
559
Brad Bishop08902b02019-08-20 09:16:51 -0400560 def get_taskdata(self):
Patrick Williams44b3caf2024-04-12 16:51:14 -0500561 return (self.server, self.method, self.extramethod, self.max_parallel, self.username, self.password, self.env) + super().get_taskdata()
Brad Bishop08902b02019-08-20 09:16:51 -0400562
563 def set_taskdata(self, data):
Patrick Williams44b3caf2024-04-12 16:51:14 -0500564 self.server, self.method, self.extramethod, self.max_parallel, self.username, self.password, self.env = data[:7]
565 super().set_taskdata(data[7:])
Patrick Williams03514f12024-04-05 07:04:11 -0500566
567 def get_hashserv_creds(self):
568 if self.username and self.password:
569 return {
570 "username": self.username,
571 "password": self.password,
572 }
573
574 return {}
Brad Bishop08902b02019-08-20 09:16:51 -0400575
Patrick Williams44b3caf2024-04-12 16:51:14 -0500576 @contextmanager
577 def _client_env(self):
578 orig_env = os.environ.copy()
579 try:
580 for k, v in self.env.items():
581 os.environ[k] = v
Brad Bishopa34c0302019-09-23 22:34:48 -0400582
Patrick Williams44b3caf2024-04-12 16:51:14 -0500583 yield
584 finally:
585 os.environ = orig_env
586
587 @contextmanager
588 def client(self):
589 with self._client_env():
590 if getattr(self, '_client', None) is None:
591 self._client = hashserv.create_client(self.server, **self.get_hashserv_creds())
592 yield self._client
593
594 @contextmanager
Patrick Williams73bd93f2024-02-20 08:07:48 -0600595 def client_pool(self):
Patrick Williams44b3caf2024-04-12 16:51:14 -0500596 with self._client_env():
597 if getattr(self, '_client_pool', None) is None:
598 self._client_pool = hashserv.client.ClientPool(self.server, self.max_parallel, **self.get_hashserv_creds())
599 yield self._client_pool
Patrick Williams73bd93f2024-02-20 08:07:48 -0600600
Andrew Geissler9aee5002022-03-30 16:27:02 +0000601 def reset(self, data):
Patrick Williams73bd93f2024-02-20 08:07:48 -0600602 self.__close_clients()
Andrew Geissler9aee5002022-03-30 16:27:02 +0000603 return super().reset(data)
604
605 def exit(self):
Patrick Williams73bd93f2024-02-20 08:07:48 -0600606 self.__close_clients()
607 return super().exit()
608
609 def __close_clients(self):
Patrick Williams44b3caf2024-04-12 16:51:14 -0500610 with self._client_env():
611 if getattr(self, '_client', None) is not None:
612 self._client.close()
613 self._client = None
614 if getattr(self, '_client_pool', None) is not None:
615 self._client_pool.close()
616 self._client_pool = None
Andrew Geissler9aee5002022-03-30 16:27:02 +0000617
Brad Bishop08902b02019-08-20 09:16:51 -0400618 def get_stampfile_hash(self, tid):
619 if tid in self.taskhash:
620 # If a unique hash is reported, use it as the stampfile hash. This
621 # ensures that if a task won't be re-run if the taskhash changes,
622 # but it would result in the same output hash
Andrew Geissler82c905d2020-04-13 13:39:40 -0500623 unihash = self._get_unihash(tid)
Brad Bishop08902b02019-08-20 09:16:51 -0400624 if unihash is not None:
625 return unihash
626
627 return super().get_stampfile_hash(tid)
628
629 def set_unihash(self, tid, unihash):
Andrew Geissler82c905d2020-04-13 13:39:40 -0500630 (mc, fn, taskname, taskfn) = bb.runqueue.split_tid_mcfn(tid)
631 key = mc + ":" + self.tidtopn[tid] + ":" + taskname
632 self.unitaskhashes[key] = (self.taskhash[tid], unihash)
633 self.unihash[tid] = unihash
634
635 def _get_unihash(self, tid, checkkey=None):
636 if tid not in self.tidtopn:
637 return None
638 (mc, fn, taskname, taskfn) = bb.runqueue.split_tid_mcfn(tid)
639 key = mc + ":" + self.tidtopn[tid] + ":" + taskname
640 if key not in self.unitaskhashes:
641 return None
642 if not checkkey:
643 checkkey = self.taskhash[tid]
644 (key, unihash) = self.unitaskhashes[key]
645 if key != checkkey:
646 return None
647 return unihash
Brad Bishop08902b02019-08-20 09:16:51 -0400648
Patrick Williams73bd93f2024-02-20 08:07:48 -0600649 def get_cached_unihash(self, tid):
Brad Bishop08902b02019-08-20 09:16:51 -0400650 taskhash = self.taskhash[tid]
651
Brad Bishopa34c0302019-09-23 22:34:48 -0400652 # If its not a setscene task we can return
653 if self.setscenetasks and tid not in self.setscenetasks:
Andrew Geissler82c905d2020-04-13 13:39:40 -0500654 self.unihash[tid] = None
Brad Bishopa34c0302019-09-23 22:34:48 -0400655 return taskhash
656
Brad Bishop08902b02019-08-20 09:16:51 -0400657 # TODO: This cache can grow unbounded. It probably only needs to keep
658 # for each task
Andrew Geissler82c905d2020-04-13 13:39:40 -0500659 unihash = self._get_unihash(tid)
Brad Bishop08902b02019-08-20 09:16:51 -0400660 if unihash is not None:
Andrew Geissler82c905d2020-04-13 13:39:40 -0500661 self.unihash[tid] = unihash
Brad Bishop08902b02019-08-20 09:16:51 -0400662 return unihash
663
Patrick Williams73bd93f2024-02-20 08:07:48 -0600664 return None
Brad Bishop08902b02019-08-20 09:16:51 -0400665
Patrick Williams73bd93f2024-02-20 08:07:48 -0600666 def _get_method(self, tid):
667 method = self.method
668 if tid in self.extramethod:
669 method = method + self.extramethod[tid]
670
671 return method
672
673 def unihashes_exist(self, query):
674 if len(query) == 0:
675 return {}
676
677 uncached_query = {}
678 result = {}
679 for key, unihash in query.items():
680 if unihash in self.unihash_exists_cache:
681 result[key] = True
682 else:
683 uncached_query[key] = unihash
684
685 if self.max_parallel <= 1 or len(uncached_query) <= 1:
686 # No parallelism required. Make the query serially with the single client
Patrick Williams44b3caf2024-04-12 16:51:14 -0500687 with self.client() as client:
688 uncached_result = {
689 key: client.unihash_exists(value) for key, value in uncached_query.items()
690 }
Patrick Williams73bd93f2024-02-20 08:07:48 -0600691 else:
Patrick Williams44b3caf2024-04-12 16:51:14 -0500692 with self.client_pool() as client_pool:
693 uncached_result = client_pool.unihashes_exist(uncached_query)
Patrick Williams73bd93f2024-02-20 08:07:48 -0600694
695 for key, exists in uncached_result.items():
696 if exists:
697 self.unihash_exists_cache.add(query[key])
698 result[key] = exists
699
700 return result
701
702 def get_unihash(self, tid):
703 return self.get_unihashes([tid])[tid]
704
705 def get_unihashes(self, tids):
706 """
707 For a iterable of tids, returns a dictionary that maps each tid to a
708 unihash
709 """
710 result = {}
711 queries = {}
712 query_result = {}
713
714 for tid in tids:
715 unihash = self.get_cached_unihash(tid)
716 if unihash:
717 result[tid] = unihash
718 else:
719 queries[tid] = (self._get_method(tid), self.taskhash[tid])
720
721 if len(queries) == 0:
722 return result
723
724 if self.max_parallel <= 1 or len(queries) <= 1:
725 # No parallelism required. Make the query serially with the single client
Patrick Williams44b3caf2024-04-12 16:51:14 -0500726 with self.client() as client:
727 for tid, args in queries.items():
728 query_result[tid] = client.get_unihash(*args)
Patrick Williams73bd93f2024-02-20 08:07:48 -0600729 else:
Patrick Williams44b3caf2024-04-12 16:51:14 -0500730 with self.client_pool() as client_pool:
731 query_result = client_pool.get_unihashes(queries)
Patrick Williams73bd93f2024-02-20 08:07:48 -0600732
733 for tid, unihash in query_result.items():
734 # In the absence of being able to discover a unique hash from the
735 # server, make it be equivalent to the taskhash. The unique "hash" only
736 # really needs to be a unique string (not even necessarily a hash), but
737 # making it match the taskhash has a few advantages:
738 #
739 # 1) All of the sstate code that assumes hashes can be the same
740 # 2) It provides maximal compatibility with builders that don't use
741 # an equivalency server
742 # 3) The value is easy for multiple independent builders to derive the
743 # same unique hash from the same input. This means that if the
744 # independent builders find the same taskhash, but it isn't reported
745 # to the server, there is a better chance that they will agree on
746 # the unique hash.
747 taskhash = self.taskhash[tid]
748 if unihash:
Brad Bishop08902b02019-08-20 09:16:51 -0400749 # A unique hash equal to the taskhash is not very interesting,
750 # so it is reported it at debug level 2. If they differ, that
751 # is much more interesting, so it is reported at debug level 1
Andrew Geissler6aa7eec2023-03-03 12:41:14 -0600752 hashequiv_logger.bbdebug((1, 2)[unihash == taskhash], 'Found unihash %s in place of %s for %s from %s' % (unihash, taskhash, tid, self.server))
Brad Bishop08902b02019-08-20 09:16:51 -0400753 else:
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600754 hashequiv_logger.debug2('No reported unihash for %s:%s from %s' % (tid, taskhash, self.server))
Patrick Williams73bd93f2024-02-20 08:07:48 -0600755 unihash = taskhash
Brad Bishop08902b02019-08-20 09:16:51 -0400756
Patrick Williams73bd93f2024-02-20 08:07:48 -0600757
758 self.set_unihash(tid, unihash)
759 self.unihash[tid] = unihash
760 result[tid] = unihash
761
762 return result
Brad Bishop08902b02019-08-20 09:16:51 -0400763
764 def report_unihash(self, path, task, d):
Brad Bishop08902b02019-08-20 09:16:51 -0400765 import importlib
766
767 taskhash = d.getVar('BB_TASKHASH')
768 unihash = d.getVar('BB_UNIHASH')
769 report_taskdata = d.getVar('SSTATE_HASHEQUIV_REPORT_TASKDATA') == '1'
770 tempdir = d.getVar('T')
Andrew Geissler517393d2023-01-13 08:55:19 -0600771 mcfn = d.getVar('BB_FILENAME')
772 tid = mcfn + ':do_' + task
Andrew Geissler82c905d2020-04-13 13:39:40 -0500773 key = tid + ':' + taskhash
Brad Bishop00e122a2019-10-05 11:10:57 -0400774
775 if self.setscenetasks and tid not in self.setscenetasks:
776 return
Brad Bishop08902b02019-08-20 09:16:51 -0400777
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000778 # This can happen if locked sigs are in action. Detect and just exit
Andrew Geissler82c905d2020-04-13 13:39:40 -0500779 if taskhash != self.taskhash[tid]:
780 return
781
Brad Bishop08902b02019-08-20 09:16:51 -0400782 # Sanity checks
Andrew Geissler82c905d2020-04-13 13:39:40 -0500783 cache_unihash = self._get_unihash(tid, checkkey=taskhash)
Brad Bishop08902b02019-08-20 09:16:51 -0400784 if cache_unihash is None:
785 bb.fatal('%s not in unihash cache. Please report this error' % key)
786
787 if cache_unihash != unihash:
788 bb.fatal("Cache unihash %s doesn't match BB_UNIHASH %s" % (cache_unihash, unihash))
789
790 sigfile = None
791 sigfile_name = "depsig.do_%s.%d" % (task, os.getpid())
792 sigfile_link = "depsig.do_%s" % task
793
794 try:
795 sigfile = open(os.path.join(tempdir, sigfile_name), 'w+b')
796
797 locs = {'path': path, 'sigfile': sigfile, 'task': task, 'd': d}
798
799 if "." in self.method:
800 (module, method) = self.method.rsplit('.', 1)
801 locs['method'] = getattr(importlib.import_module(module), method)
802 outhash = bb.utils.better_eval('method(path, sigfile, task, d)', locs)
803 else:
804 outhash = bb.utils.better_eval(self.method + '(path, sigfile, task, d)', locs)
805
806 try:
Brad Bishopa34c0302019-09-23 22:34:48 -0400807 extra_data = {}
808
809 owner = d.getVar('SSTATE_HASHEQUIV_OWNER')
810 if owner:
811 extra_data['owner'] = owner
Brad Bishop08902b02019-08-20 09:16:51 -0400812
813 if report_taskdata:
814 sigfile.seek(0)
815
Brad Bishopa34c0302019-09-23 22:34:48 -0400816 extra_data['PN'] = d.getVar('PN')
817 extra_data['PV'] = d.getVar('PV')
818 extra_data['PR'] = d.getVar('PR')
819 extra_data['task'] = task
820 extra_data['outhash_siginfo'] = sigfile.read().decode('utf-8')
Brad Bishop08902b02019-08-20 09:16:51 -0400821
Andrew Geissler82c905d2020-04-13 13:39:40 -0500822 method = self.method
823 if tid in self.extramethod:
824 method = method + self.extramethod[tid]
825
Patrick Williams44b3caf2024-04-12 16:51:14 -0500826 with self.client() as client:
827 data = client.report_unihash(taskhash, method, outhash, unihash, extra_data)
828
Brad Bishopa34c0302019-09-23 22:34:48 -0400829 new_unihash = data['unihash']
Brad Bishop08902b02019-08-20 09:16:51 -0400830
831 if new_unihash != unihash:
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600832 hashequiv_logger.debug('Task %s unihash changed %s -> %s by server %s' % (taskhash, unihash, new_unihash, self.server))
Andrew Geissler517393d2023-01-13 08:55:19 -0600833 bb.event.fire(bb.runqueue.taskUniHashUpdate(mcfn + ':do_' + task, new_unihash), d)
Andrew Geissler82c905d2020-04-13 13:39:40 -0500834 self.set_unihash(tid, new_unihash)
835 d.setVar('BB_UNIHASH', new_unihash)
Brad Bishop08902b02019-08-20 09:16:51 -0400836 else:
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600837 hashequiv_logger.debug('Reported task %s as unihash %s to %s' % (taskhash, unihash, self.server))
Andrew Geisslerc926e172021-05-07 16:11:35 -0500838 except ConnectionError as e:
Brad Bishopa34c0302019-09-23 22:34:48 -0400839 bb.warn('Error contacting Hash Equivalence Server %s: %s' % (self.server, str(e)))
Brad Bishop08902b02019-08-20 09:16:51 -0400840 finally:
841 if sigfile:
842 sigfile.close()
843
844 sigfile_link_path = os.path.join(tempdir, sigfile_link)
845 bb.utils.remove(sigfile_link_path)
846
847 try:
848 os.symlink(sigfile_name, sigfile_link_path)
849 except OSError:
850 pass
851
Andrew Geissler82c905d2020-04-13 13:39:40 -0500852 def report_unihash_equiv(self, tid, taskhash, wanted_unihash, current_unihash, datacaches):
853 try:
854 extra_data = {}
855 method = self.method
856 if tid in self.extramethod:
857 method = method + self.extramethod[tid]
858
Patrick Williams44b3caf2024-04-12 16:51:14 -0500859 with self.client() as client:
860 data = client.report_unihash_equiv(taskhash, method, wanted_unihash, extra_data)
861
Andrew Geissler82c905d2020-04-13 13:39:40 -0500862 hashequiv_logger.verbose('Reported task %s as unihash %s to %s (%s)' % (tid, wanted_unihash, self.server, str(data)))
863
864 if data is None:
865 bb.warn("Server unable to handle unihash report")
866 return False
867
868 finalunihash = data['unihash']
869
870 if finalunihash == current_unihash:
871 hashequiv_logger.verbose('Task %s unihash %s unchanged by server' % (tid, finalunihash))
872 elif finalunihash == wanted_unihash:
873 hashequiv_logger.verbose('Task %s unihash changed %s -> %s as wanted' % (tid, current_unihash, finalunihash))
874 self.set_unihash(tid, finalunihash)
875 return True
876 else:
877 # TODO: What to do here?
878 hashequiv_logger.verbose('Task %s unihash reported as unwanted hash %s' % (tid, finalunihash))
879
Andrew Geisslerc926e172021-05-07 16:11:35 -0500880 except ConnectionError as e:
Andrew Geissler82c905d2020-04-13 13:39:40 -0500881 bb.warn('Error contacting Hash Equivalence Server %s: %s' % (self.server, str(e)))
882
883 return False
Brad Bishop08902b02019-08-20 09:16:51 -0400884
885#
886# Dummy class used for bitbake-selftest
887#
888class SignatureGeneratorTestEquivHash(SignatureGeneratorUniHashMixIn, SignatureGeneratorBasicHash):
889 name = "TestEquivHash"
890 def init_rundepcheck(self, data):
891 super().init_rundepcheck(data)
Brad Bishopa34c0302019-09-23 22:34:48 -0400892 self.server = data.getVar('BB_HASHSERVE')
Brad Bishop08902b02019-08-20 09:16:51 -0400893 self.method = "sstate_output_hash"
Patrick Williams73bd93f2024-02-20 08:07:48 -0600894 self.max_parallel = 1
Brad Bishop08902b02019-08-20 09:16:51 -0400895
Andrew Geissler5082cc72023-09-11 08:41:39 -0400896def clean_checksum_file_path(file_checksum_tuple):
897 f, cs = file_checksum_tuple
898 if "/./" in f:
899 return "./" + f.split("/./")[1]
900 return f
901
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500902def dump_this_task(outfile, d):
903 import bb.parse
Andrew Geissler517393d2023-01-13 08:55:19 -0600904 mcfn = d.getVar("BB_FILENAME")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500905 task = "do_" + d.getVar("BB_CURRENTTASK")
Andrew Geissler517393d2023-01-13 08:55:19 -0600906 referencestamp = bb.parse.siggen.stampfile_base(mcfn)
907 bb.parse.siggen.dump_sigtask(mcfn, task, outfile, "customfile:" + referencestamp)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500908
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500909def init_colors(enable_color):
910 """Initialise colour dict for passing to compare_sigfiles()"""
911 # First set up the colours
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800912 colors = {'color_title': '\033[1m',
913 'color_default': '\033[0m',
914 'color_add': '\033[0;32m',
915 'color_remove': '\033[0;31m',
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500916 }
917 # Leave all keys present but clear the values
918 if not enable_color:
919 for k in colors.keys():
920 colors[k] = ''
921 return colors
922
923def worddiff_str(oldstr, newstr, colors=None):
924 if not colors:
925 colors = init_colors(False)
926 diff = simplediff.diff(oldstr.split(' '), newstr.split(' '))
927 ret = []
928 for change, value in diff:
929 value = ' '.join(value)
930 if change == '=':
931 ret.append(value)
932 elif change == '+':
933 item = '{color_add}{{+{value}+}}{color_default}'.format(value=value, **colors)
934 ret.append(item)
935 elif change == '-':
936 item = '{color_remove}[-{value}-]{color_default}'.format(value=value, **colors)
937 ret.append(item)
938 whitespace_note = ''
939 if oldstr != newstr and ' '.join(oldstr.split()) == ' '.join(newstr.split()):
940 whitespace_note = ' (whitespace changed)'
941 return '"%s"%s' % (' '.join(ret), whitespace_note)
942
943def list_inline_diff(oldlist, newlist, colors=None):
944 if not colors:
945 colors = init_colors(False)
946 diff = simplediff.diff(oldlist, newlist)
947 ret = []
948 for change, value in diff:
949 value = ' '.join(value)
950 if change == '=':
951 ret.append("'%s'" % value)
952 elif change == '+':
953 item = '{color_add}+{value}{color_default}'.format(value=value, **colors)
954 ret.append(item)
955 elif change == '-':
956 item = '{color_remove}-{value}{color_default}'.format(value=value, **colors)
957 ret.append(item)
958 return '[%s]' % (', '.join(ret))
959
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000960# Handled renamed fields
961def handle_renames(data):
962 if 'basewhitelist' in data:
963 data['basehash_ignore_vars'] = data['basewhitelist']
964 del data['basewhitelist']
965 if 'taskwhitelist' in data:
966 data['taskhash_ignore_tasks'] = data['taskwhitelist']
967 del data['taskwhitelist']
968
969
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500970def compare_sigfiles(a, b, recursecb=None, color=False, collapsed=False):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500971 output = []
972
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500973 colors = init_colors(color)
974 def color_format(formatstr, **values):
975 """
976 Return colour formatted string.
977 NOTE: call with the format string, not an already formatted string
978 containing values (otherwise you could have trouble with { and }
979 characters)
980 """
981 if not formatstr.endswith('{color_default}'):
982 formatstr += '{color_default}'
983 # In newer python 3 versions you can pass both of these directly,
984 # but we only require 3.4 at the moment
985 formatparams = {}
986 formatparams.update(colors)
987 formatparams.update(values)
988 return formatstr.format(**formatparams)
989
Patrick Williams73bd93f2024-02-20 08:07:48 -0600990 try:
991 with bb.compress.zstd.open(a, "rt", encoding="utf-8", num_threads=1) as f:
992 a_data = json.load(f, object_hook=SetDecoder)
993 except (TypeError, OSError) as err:
994 bb.error("Failed to open sigdata file '%s': %s" % (a, str(err)))
995 raise err
996 try:
997 with bb.compress.zstd.open(b, "rt", encoding="utf-8", num_threads=1) as f:
998 b_data = json.load(f, object_hook=SetDecoder)
999 except (TypeError, OSError) as err:
1000 bb.error("Failed to open sigdata file '%s': %s" % (b, str(err)))
1001 raise err
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001002
Andrew Geissler7e0e3c02022-02-25 20:34:39 +00001003 for data in [a_data, b_data]:
1004 handle_renames(data)
1005
1006 def dict_diff(a, b, ignored_vars=set()):
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001007 sa = set(a.keys())
1008 sb = set(b.keys())
1009 common = sa & sb
1010 changed = set()
1011 for i in common:
Andrew Geissler7e0e3c02022-02-25 20:34:39 +00001012 if a[i] != b[i] and i not in ignored_vars:
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001013 changed.add(i)
1014 added = sb - sa
1015 removed = sa - sb
1016 return changed, added, removed
1017
1018 def file_checksums_diff(a, b):
1019 from collections import Counter
Andrew Geisslereff27472021-10-29 15:35:00 -05001020
1021 # Convert lists back to tuples
1022 a = [(f[0], f[1]) for f in a]
1023 b = [(f[0], f[1]) for f in b]
1024
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001025 # Compare lists, ensuring we can handle duplicate filenames if they exist
1026 removedcount = Counter(a)
1027 removedcount.subtract(b)
1028 addedcount = Counter(b)
1029 addedcount.subtract(a)
1030 added = []
1031 for x in b:
1032 if addedcount[x] > 0:
1033 addedcount[x] -= 1
1034 added.append(x)
1035 removed = []
1036 changed = []
1037 for x in a:
1038 if removedcount[x] > 0:
1039 removedcount[x] -= 1
1040 for y in added:
1041 if y[0] == x[0]:
1042 changed.append((x[0], x[1], y[1]))
1043 added.remove(y)
1044 break
1045 else:
1046 removed.append(x)
1047 added = [x[0] for x in added]
1048 removed = [x[0] for x in removed]
1049 return changed, added, removed
1050
Andrew Geissler7e0e3c02022-02-25 20:34:39 +00001051 if 'basehash_ignore_vars' in a_data and a_data['basehash_ignore_vars'] != b_data['basehash_ignore_vars']:
1052 output.append(color_format("{color_title}basehash_ignore_vars changed{color_default} from '%s' to '%s'") % (a_data['basehash_ignore_vars'], b_data['basehash_ignore_vars']))
1053 if a_data['basehash_ignore_vars'] and b_data['basehash_ignore_vars']:
1054 output.append("changed items: %s" % a_data['basehash_ignore_vars'].symmetric_difference(b_data['basehash_ignore_vars']))
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001055
Andrew Geissler7e0e3c02022-02-25 20:34:39 +00001056 if 'taskhash_ignore_tasks' in a_data and a_data['taskhash_ignore_tasks'] != b_data['taskhash_ignore_tasks']:
1057 output.append(color_format("{color_title}taskhash_ignore_tasks changed{color_default} from '%s' to '%s'") % (a_data['taskhash_ignore_tasks'], b_data['taskhash_ignore_tasks']))
1058 if a_data['taskhash_ignore_tasks'] and b_data['taskhash_ignore_tasks']:
1059 output.append("changed items: %s" % a_data['taskhash_ignore_tasks'].symmetric_difference(b_data['taskhash_ignore_tasks']))
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001060
1061 if a_data['taskdeps'] != b_data['taskdeps']:
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001062 output.append(color_format("{color_title}Task dependencies changed{color_default} from:\n%s\nto:\n%s") % (sorted(a_data['taskdeps']), sorted(b_data['taskdeps'])))
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001063
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001064 if a_data['basehash'] != b_data['basehash'] and not collapsed:
1065 output.append(color_format("{color_title}basehash changed{color_default} from %s to %s") % (a_data['basehash'], b_data['basehash']))
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001066
Andrew Geissler7e0e3c02022-02-25 20:34:39 +00001067 changed, added, removed = dict_diff(a_data['gendeps'], b_data['gendeps'], a_data['basehash_ignore_vars'] & b_data['basehash_ignore_vars'])
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001068 if changed:
Patrick Williams93c203f2021-10-06 16:15:23 -05001069 for dep in sorted(changed):
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001070 output.append(color_format("{color_title}List of dependencies for variable %s changed from '{color_default}%s{color_title}' to '{color_default}%s{color_title}'") % (dep, a_data['gendeps'][dep], b_data['gendeps'][dep]))
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001071 if a_data['gendeps'][dep] and b_data['gendeps'][dep]:
1072 output.append("changed items: %s" % a_data['gendeps'][dep].symmetric_difference(b_data['gendeps'][dep]))
1073 if added:
Patrick Williams93c203f2021-10-06 16:15:23 -05001074 for dep in sorted(added):
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001075 output.append(color_format("{color_title}Dependency on variable %s was added") % (dep))
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001076 if removed:
Patrick Williams93c203f2021-10-06 16:15:23 -05001077 for dep in sorted(removed):
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001078 output.append(color_format("{color_title}Dependency on Variable %s was removed") % (dep))
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001079
1080
1081 changed, added, removed = dict_diff(a_data['varvals'], b_data['varvals'])
1082 if changed:
Patrick Williams93c203f2021-10-06 16:15:23 -05001083 for dep in sorted(changed):
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001084 oldval = a_data['varvals'][dep]
1085 newval = b_data['varvals'][dep]
1086 if newval and oldval and ('\n' in oldval or '\n' in newval):
1087 diff = difflib.unified_diff(oldval.splitlines(), newval.splitlines(), lineterm='')
1088 # Cut off the first two lines, since we aren't interested in
1089 # the old/new filename (they are blank anyway in this case)
1090 difflines = list(diff)[2:]
1091 if color:
1092 # Add colour to diff output
1093 for i, line in enumerate(difflines):
1094 if line.startswith('+'):
1095 line = color_format('{color_add}{line}', line=line)
1096 difflines[i] = line
1097 elif line.startswith('-'):
1098 line = color_format('{color_remove}{line}', line=line)
1099 difflines[i] = line
1100 output.append(color_format("{color_title}Variable {var} value changed:{color_default}\n{diff}", var=dep, diff='\n'.join(difflines)))
1101 elif newval and oldval and (' ' in oldval or ' ' in newval):
1102 output.append(color_format("{color_title}Variable {var} value changed:{color_default}\n{diff}", var=dep, diff=worddiff_str(oldval, newval, colors)))
1103 else:
1104 output.append(color_format("{color_title}Variable {var} value changed from '{color_default}{oldval}{color_title}' to '{color_default}{newval}{color_title}'{color_default}", var=dep, oldval=oldval, newval=newval))
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001105
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001106 if not 'file_checksum_values' in a_data:
Andrew Geisslereff27472021-10-29 15:35:00 -05001107 a_data['file_checksum_values'] = []
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001108 if not 'file_checksum_values' in b_data:
Andrew Geisslereff27472021-10-29 15:35:00 -05001109 b_data['file_checksum_values'] = []
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001110
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001111 changed, added, removed = file_checksums_diff(a_data['file_checksum_values'], b_data['file_checksum_values'])
1112 if changed:
1113 for f, old, new in changed:
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001114 output.append(color_format("{color_title}Checksum for file %s changed{color_default} from %s to %s") % (f, old, new))
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001115 if added:
1116 for f in added:
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001117 output.append(color_format("{color_title}Dependency on checksum of file %s was added") % (f))
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001118 if removed:
1119 for f in removed:
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001120 output.append(color_format("{color_title}Dependency on checksum of file %s was removed") % (f))
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001121
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001122 if not 'runtaskdeps' in a_data:
1123 a_data['runtaskdeps'] = {}
1124 if not 'runtaskdeps' in b_data:
1125 b_data['runtaskdeps'] = {}
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001126
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001127 if not collapsed:
1128 if len(a_data['runtaskdeps']) != len(b_data['runtaskdeps']):
1129 changed = ["Number of task dependencies changed"]
1130 else:
1131 changed = []
1132 for idx, task in enumerate(a_data['runtaskdeps']):
1133 a = a_data['runtaskdeps'][idx]
1134 b = b_data['runtaskdeps'][idx]
1135 if a_data['runtaskhashes'][a] != b_data['runtaskhashes'][b] and not collapsed:
Patrick Williams2a254922023-08-11 09:48:11 -05001136 changed.append("%s with hash %s\n changed to\n%s with hash %s" % (a, a_data['runtaskhashes'][a], b, b_data['runtaskhashes'][b]))
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001137
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001138 if changed:
Patrick Williams2a254922023-08-11 09:48:11 -05001139 clean_a = a_data['runtaskdeps']
1140 clean_b = b_data['runtaskdeps']
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001141 if clean_a != clean_b:
1142 output.append(color_format("{color_title}runtaskdeps changed:{color_default}\n%s") % list_inline_diff(clean_a, clean_b, colors))
1143 else:
1144 output.append(color_format("{color_title}runtaskdeps changed:"))
1145 output.append("\n".join(changed))
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001146
1147
1148 if 'runtaskhashes' in a_data and 'runtaskhashes' in b_data:
Patrick Williams2a254922023-08-11 09:48:11 -05001149 a = a_data['runtaskhashes']
1150 b = b_data['runtaskhashes']
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001151 changed, added, removed = dict_diff(a, b)
1152 if added:
Patrick Williams93c203f2021-10-06 16:15:23 -05001153 for dep in sorted(added):
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001154 bdep_found = False
1155 if removed:
1156 for bdep in removed:
1157 if b[dep] == a[bdep]:
1158 #output.append("Dependency on task %s was replaced by %s with same hash" % (dep, bdep))
1159 bdep_found = True
1160 if not bdep_found:
Patrick Williams03907ee2022-05-01 06:28:52 -05001161 output.append(color_format("{color_title}Dependency on task %s was added{color_default} with hash %s") % (dep, b[dep]))
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001162 if removed:
Patrick Williams93c203f2021-10-06 16:15:23 -05001163 for dep in sorted(removed):
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001164 adep_found = False
1165 if added:
1166 for adep in added:
1167 if b[adep] == a[dep]:
1168 #output.append("Dependency on task %s was replaced by %s with same hash" % (adep, dep))
1169 adep_found = True
1170 if not adep_found:
Patrick Williams03907ee2022-05-01 06:28:52 -05001171 output.append(color_format("{color_title}Dependency on task %s was removed{color_default} with hash %s") % (dep, a[dep]))
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001172 if changed:
Patrick Williams93c203f2021-10-06 16:15:23 -05001173 for dep in sorted(changed):
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001174 if not collapsed:
Patrick Williams03907ee2022-05-01 06:28:52 -05001175 output.append(color_format("{color_title}Hash for task dependency %s changed{color_default} from %s to %s") % (dep, a[dep], b[dep]))
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001176 if callable(recursecb):
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001177 recout = recursecb(dep, a[dep], b[dep])
1178 if recout:
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001179 if collapsed:
1180 output.extend(recout)
1181 else:
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001182 # If a dependent hash changed, might as well print the line above and then defer to the changes in
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001183 # that hash since in all likelyhood, they're the same changes this task also saw.
1184 output = [output[-1]] + recout
Andrew Geisslerd5838332022-05-27 11:33:10 -05001185 break
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001186
1187 a_taint = a_data.get('taint', None)
1188 b_taint = b_data.get('taint', None)
1189 if a_taint != b_taint:
Brad Bishop96ff1982019-08-19 13:50:42 -04001190 if a_taint and a_taint.startswith('nostamp:'):
Brad Bishopc342db32019-05-15 21:57:59 -04001191 a_taint = a_taint.replace('nostamp:', 'nostamp(uuid4):')
Brad Bishop96ff1982019-08-19 13:50:42 -04001192 if b_taint and b_taint.startswith('nostamp:'):
Brad Bishopc342db32019-05-15 21:57:59 -04001193 b_taint = b_taint.replace('nostamp:', 'nostamp(uuid4):')
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001194 output.append(color_format("{color_title}Taint (by forced/invalidated task) changed{color_default} from %s to %s") % (a_taint, b_taint))
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001195
1196 return output
1197
1198
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05001199def calc_basehash(sigdata):
1200 task = sigdata['task']
1201 basedata = sigdata['varvals'][task]
1202
1203 if basedata is None:
1204 basedata = ''
1205
1206 alldeps = sigdata['taskdeps']
Andrew Geissler517393d2023-01-13 08:55:19 -06001207 for dep in sorted(alldeps):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05001208 basedata = basedata + dep
1209 val = sigdata['varvals'][dep]
1210 if val is not None:
1211 basedata = basedata + str(val)
1212
Brad Bishop19323692019-04-05 15:28:33 -04001213 return hashlib.sha256(basedata.encode("utf-8")).hexdigest()
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05001214
1215def calc_taskhash(sigdata):
1216 data = sigdata['basehash']
1217
1218 for dep in sigdata['runtaskdeps']:
1219 data = data + sigdata['runtaskhashes'][dep]
1220
1221 for c in sigdata['file_checksum_values']:
Brad Bishop37a0e4d2017-12-04 01:01:44 -05001222 if c[1]:
Andrew Geissler595f6302022-01-24 19:11:47 +00001223 if "./" in c[0]:
1224 data = data + c[0]
Brad Bishop37a0e4d2017-12-04 01:01:44 -05001225 data = data + c[1]
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05001226
1227 if 'taint' in sigdata:
1228 if 'nostamp:' in sigdata['taint']:
1229 data = data + sigdata['taint'][8:]
1230 else:
1231 data = data + sigdata['taint']
1232
Brad Bishop19323692019-04-05 15:28:33 -04001233 return hashlib.sha256(data.encode("utf-8")).hexdigest()
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05001234
1235
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001236def dump_sigfile(a):
1237 output = []
1238
Patrick Williams73bd93f2024-02-20 08:07:48 -06001239 try:
1240 with bb.compress.zstd.open(a, "rt", encoding="utf-8", num_threads=1) as f:
1241 a_data = json.load(f, object_hook=SetDecoder)
1242 except (TypeError, OSError) as err:
1243 bb.error("Failed to open sigdata file '%s': %s" % (a, str(err)))
1244 raise err
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001245
Andrew Geissler7e0e3c02022-02-25 20:34:39 +00001246 handle_renames(a_data)
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001247
Andrew Geissler7e0e3c02022-02-25 20:34:39 +00001248 output.append("basehash_ignore_vars: %s" % (sorted(a_data['basehash_ignore_vars'])))
1249
1250 output.append("taskhash_ignore_tasks: %s" % (sorted(a_data['taskhash_ignore_tasks'] or [])))
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001251
1252 output.append("Task dependencies: %s" % (sorted(a_data['taskdeps'])))
1253
1254 output.append("basehash: %s" % (a_data['basehash']))
1255
Andrew Geissler595f6302022-01-24 19:11:47 +00001256 for dep in sorted(a_data['gendeps']):
1257 output.append("List of dependencies for variable %s is %s" % (dep, sorted(a_data['gendeps'][dep])))
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001258
Andrew Geissler595f6302022-01-24 19:11:47 +00001259 for dep in sorted(a_data['varvals']):
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001260 output.append("Variable %s value is %s" % (dep, a_data['varvals'][dep]))
1261
1262 if 'runtaskdeps' in a_data:
Andrew Geissler595f6302022-01-24 19:11:47 +00001263 output.append("Tasks this task depends on: %s" % (sorted(a_data['runtaskdeps'])))
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001264
1265 if 'file_checksum_values' in a_data:
Andrew Geissler595f6302022-01-24 19:11:47 +00001266 output.append("This task depends on the checksums of files: %s" % (sorted(a_data['file_checksum_values'])))
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001267
1268 if 'runtaskhashes' in a_data:
Andrew Geissler595f6302022-01-24 19:11:47 +00001269 for dep in sorted(a_data['runtaskhashes']):
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001270 output.append("Hash for dependent task %s is %s" % (dep, a_data['runtaskhashes'][dep]))
1271
1272 if 'taint' in a_data:
Brad Bishopc342db32019-05-15 21:57:59 -04001273 if a_data['taint'].startswith('nostamp:'):
1274 msg = a_data['taint'].replace('nostamp:', 'nostamp(uuid4):')
1275 else:
1276 msg = a_data['taint']
1277 output.append("Tainted (by forced/invalidated task): %s" % msg)
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001278
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05001279 if 'task' in a_data:
1280 computed_basehash = calc_basehash(a_data)
1281 output.append("Computed base hash is %s and from file %s" % (computed_basehash, a_data['basehash']))
1282 else:
1283 output.append("Unable to compute base hash")
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001284
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05001285 computed_taskhash = calc_taskhash(a_data)
1286 output.append("Computed task hash is %s" % computed_taskhash)
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001287
1288 return output