Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 1 | # |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 2 | # Copyright BitBake Contributors |
| 3 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 4 | # SPDX-License-Identifier: GPL-2.0-only |
| 5 | # |
| 6 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 7 | import hashlib |
| 8 | import logging |
| 9 | import os |
| 10 | import re |
| 11 | import tempfile |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 12 | import pickle |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 13 | import bb.data |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 14 | import difflib |
| 15 | import simplediff |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 16 | import json |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 17 | import types |
Patrick Williams | 44b3caf | 2024-04-12 16:51:14 -0500 | [diff] [blame] | 18 | from contextlib import contextmanager |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 19 | import bb.compress.zstd |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 20 | from bb.checksum import FileChecksumCache |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 21 | from bb import runqueue |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 22 | import hashserv |
Andrew Geissler | 475cb72 | 2020-07-10 16:00:51 -0500 | [diff] [blame] | 23 | import hashserv.client |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 24 | |
| 25 | logger = logging.getLogger('BitBake.SigGen') |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 26 | hashequiv_logger = logging.getLogger('BitBake.SigGen.HashEquiv') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 27 | |
Patrick Williams | 169d7bc | 2024-01-05 11:33:25 -0600 | [diff] [blame] | 28 | #find_siginfo and find_siginfo_version are set by the metadata siggen |
| 29 | # The minimum version of the find_siginfo function we need |
| 30 | find_siginfo_minversion = 2 |
| 31 | |
Patrick Williams | 44b3caf | 2024-04-12 16:51:14 -0500 | [diff] [blame] | 32 | HASHSERV_ENVVARS = [ |
| 33 | "SSL_CERT_DIR", |
| 34 | "SSL_CERT_FILE", |
| 35 | "NO_PROXY", |
| 36 | "HTTPS_PROXY", |
| 37 | "HTTP_PROXY" |
| 38 | ] |
| 39 | |
Patrick Williams | 169d7bc | 2024-01-05 11:33:25 -0600 | [diff] [blame] | 40 | def 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 Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 46 | class SetEncoder(json.JSONEncoder): |
| 47 | def default(self, obj): |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 48 | if isinstance(obj, set) or isinstance(obj, frozenset): |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 49 | return dict(_set_object=list(sorted(obj))) |
| 50 | return json.JSONEncoder.default(self, obj) |
| 51 | |
| 52 | def SetDecoder(dct): |
| 53 | if '_set_object' in dct: |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 54 | return frozenset(dct['_set_object']) |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 55 | return dct |
| 56 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 57 | def init(d): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 58 | siggens = [obj for obj in globals().values() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 59 | if type(obj) is type and issubclass(obj, SignatureGenerator)] |
| 60 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 61 | desired = d.getVar("BB_SIGNATURE_HANDLER") or "noop" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 62 | for sg in siggens: |
| 63 | if desired == sg.name: |
| 64 | return sg(d) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 65 | 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 | |
| 71 | class SignatureGenerator(object): |
| 72 | """ |
| 73 | """ |
| 74 | name = "noop" |
| 75 | |
| 76 | def __init__(self, data): |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 77 | self.basehash = {} |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 78 | self.taskhash = {} |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 79 | self.unihash = {} |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 80 | self.runtaskdeps = {} |
| 81 | self.file_checksum_values = {} |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 82 | self.taints = {} |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 83 | self.unitaskhashes = {} |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 84 | self.tidtopn = {} |
| 85 | self.setscenetasks = set() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 86 | |
| 87 | def finalise(self, fn, d, varient): |
| 88 | return |
| 89 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 90 | def postparsing_clean_cache(self): |
| 91 | return |
| 92 | |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 93 | 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 Williams | 73bd93f | 2024-02-20 08:07:48 -0600 | [diff] [blame] | 114 | def get_cached_unihash(self, tid): |
| 115 | return None |
| 116 | |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 117 | def get_unihash(self, tid): |
Patrick Williams | 73bd93f | 2024-02-20 08:07:48 -0600 | [diff] [blame] | 118 | unihash = self.get_cached_unihash(tid) |
| 119 | if unihash: |
| 120 | return unihash |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 121 | return self.taskhash[tid] |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 122 | |
Patrick Williams | 73bd93f | 2024-02-20 08:07:48 -0600 | [diff] [blame] | 123 | def get_unihashes(self, tids): |
| 124 | return {tid: self.get_unihash(tid) for tid in tids} |
| 125 | |
Andrew Geissler | 5a43b43 | 2020-06-13 10:46:56 -0500 | [diff] [blame] | 126 | def prep_taskhash(self, tid, deps, dataCaches): |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 127 | return |
| 128 | |
Andrew Geissler | 5a43b43 | 2020-06-13 10:46:56 -0500 | [diff] [blame] | 129 | def get_taskhash(self, tid, deps, dataCaches): |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 130 | self.taskhash[tid] = hashlib.sha256(tid.encode("utf-8")).hexdigest() |
| 131 | return self.taskhash[tid] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 132 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 133 | def writeout_file_checksum_cache(self): |
| 134 | """Write/update the file checksum cache onto disk""" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 135 | return |
| 136 | |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 137 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 156 | def stampfile(self, stampbase, file_name, taskname, extrainfo): |
| 157 | return ("%s.%s.%s" % (stampbase, taskname, extrainfo)).rstrip('.') |
| 158 | |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 159 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 172 | def stampcleanmask(self, stampbase, file_name, taskname, extrainfo): |
| 173 | return ("%s.%s.%s" % (stampbase, taskname, extrainfo)).rstrip('.') |
| 174 | |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 175 | def dump_sigtask(self, mcfn, task, stampbase, runtime): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 176 | return |
| 177 | |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 178 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 182 | |
| 183 | def dump_sigs(self, dataCache, options): |
| 184 | return |
| 185 | |
| 186 | def get_taskdata(self): |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 187 | return (self.runtaskdeps, self.taskhash, self.unihash, self.file_checksum_values, self.taints, self.basehash, self.unitaskhashes, self.tidtopn, self.setscenetasks) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 188 | |
| 189 | def set_taskdata(self, data): |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 190 | self.runtaskdeps, self.taskhash, self.unihash, self.file_checksum_values, self.taints, self.basehash, self.unitaskhashes, self.tidtopn, self.setscenetasks = data |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 191 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 192 | def reset(self, data): |
| 193 | self.__init__(data) |
| 194 | |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 195 | def get_taskhashes(self): |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 196 | return self.taskhash, self.unihash, self.unitaskhashes, self.tidtopn |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 197 | |
| 198 | def set_taskhashes(self, hashes): |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 199 | self.taskhash, self.unihash, self.unitaskhashes, self.tidtopn = hashes |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 200 | |
| 201 | def save_unitaskhashes(self): |
| 202 | return |
| 203 | |
Andrew Geissler | 78b7279 | 2022-06-14 06:47:25 -0500 | [diff] [blame] | 204 | def copy_unitaskhashes(self, targetdir): |
| 205 | return |
| 206 | |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 207 | def set_setscene_tasks(self, setscene_tasks): |
| 208 | return |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 209 | |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 210 | def exit(self): |
| 211 | return |
| 212 | |
Patrick Williams | 2a25492 | 2023-08-11 09:48:11 -0500 | [diff] [blame] | 213 | def build_pnid(mc, pn, taskname): |
| 214 | if mc: |
| 215 | return "mc:" + mc + ":" + pn + ":" + taskname |
| 216 | return pn + ":" + taskname |
| 217 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 218 | class SignatureGeneratorBasic(SignatureGenerator): |
| 219 | """ |
| 220 | """ |
| 221 | name = "basic" |
| 222 | |
| 223 | def __init__(self, data): |
| 224 | self.basehash = {} |
| 225 | self.taskhash = {} |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 226 | self.unihash = {} |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 227 | self.runtaskdeps = {} |
| 228 | self.file_checksum_values = {} |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 229 | self.taints = {} |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 230 | self.setscenetasks = set() |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 231 | self.basehash_ignore_vars = set((data.getVar("BB_BASEHASH_IGNORE_VARS") or "").split()) |
| 232 | self.taskhash_ignore_tasks = None |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 233 | self.init_rundepcheck(data) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 234 | checksum_cache_file = data.getVar("BB_HASH_CHECKSUM_CACHE_FILE") |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 235 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 240 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 241 | self.unihash_cache = bb.cache.SimpleCache("3") |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 242 | self.unitaskhashes = self.unihash_cache.init_cache(data, "bb_unihashes.dat", {}) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 243 | self.localdirsexclude = (data.getVar("BB_SIGNATURE_LOCAL_DIRS_EXCLUDE") or "CVS .bzr .git .hg .osc .p4 .repo .svn").split() |
| 244 | self.tidtopn = {} |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 245 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 246 | def init_rundepcheck(self, data): |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 247 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 250 | else: |
| 251 | self.twl = None |
| 252 | |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 253 | def _build_data(self, mcfn, d): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 254 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 255 | ignore_mismatch = ((d.getVar("BB_HASH_IGNORE_MISMATCH") or '') == '1') |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 256 | tasklist, gendeps, lookupcache = bb.data.generate_dependencies(d, self.basehash_ignore_vars) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 257 | |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 258 | taskdeps, basehash = bb.data.generate_dependency_hash(tasklist, gendeps, lookupcache, self.basehash_ignore_vars, mcfn) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 259 | |
| 260 | for task in tasklist: |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 261 | tid = mcfn + ":" + task |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 262 | 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 Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 264 | 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 Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 270 | self.basehash[tid] = basehash[tid] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 271 | |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 272 | return taskdeps, gendeps, lookupcache |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 273 | |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 274 | def set_setscene_tasks(self, setscene_tasks): |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 275 | self.setscenetasks = set(setscene_tasks) |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 276 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 277 | def finalise(self, fn, d, variant): |
| 278 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 279 | mc = d.getVar("__BBMULTICONFIG", False) or "" |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 280 | mcfn = fn |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 281 | if variant or mc: |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 282 | mcfn = bb.cache.realfn2virtual(fn, variant, mc) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 283 | |
| 284 | try: |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 285 | taskdeps, gendeps, lookupcache = self._build_data(mcfn, d) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 286 | except bb.parse.SkipRecipe: |
| 287 | raise |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 288 | except: |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 289 | bb.warn("Error during finalise of %s" % mcfn) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 290 | raise |
| 291 | |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 292 | basehashes = {} |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 293 | for task in taskdeps: |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 294 | basehashes[task] = self.basehash[mcfn + ":" + task] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 295 | |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 296 | 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 Geissler | 5082cc7 | 2023-09-11 08:41:39 -0400 | [diff] [blame] | 301 | #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 Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 306 | 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 Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 316 | |
Andrew Geissler | 5a43b43 | 2020-06-13 10:46:56 -0500 | [diff] [blame] | 317 | def rundep_check(self, fn, recipename, task, dep, depname, dataCaches): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 318 | # Return True if we should keep the dependency, False to drop it |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 319 | # We only manipulate the dependencies for packages not in the ignore |
| 320 | # list |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 321 | 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 Geissler | 5a43b43 | 2020-06-13 10:46:56 -0500 | [diff] [blame] | 336 | def prep_taskhash(self, tid, deps, dataCaches): |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 337 | |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 338 | (mc, _, task, mcfn) = bb.runqueue.split_tid_mcfn(tid) |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 339 | |
Andrew Geissler | 5a43b43 | 2020-06-13 10:46:56 -0500 | [diff] [blame] | 340 | self.basehash[tid] = dataCaches[mc].basetaskhash[tid] |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 341 | self.runtaskdeps[tid] = [] |
| 342 | self.file_checksum_values[tid] = [] |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 343 | recipename = dataCaches[mc].pkg_fn[mcfn] |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 344 | |
| 345 | self.tidtopn[tid] = recipename |
Patrick Williams | 2a25492 | 2023-08-11 09:48:11 -0500 | [diff] [blame] | 346 | # 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 Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 350 | |
Patrick Williams | 2a25492 | 2023-08-11 09:48:11 -0500 | [diff] [blame] | 351 | if not self.rundep_check(mcfn, recipename, task, dep, dep_pn, dataCaches): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 352 | continue |
Patrick Williams | 2a25492 | 2023-08-11 09:48:11 -0500 | [diff] [blame] | 353 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 354 | if dep not in self.taskhash: |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 355 | bb.fatal("%s is not in taskhash, caller isn't calling in dependency order?" % dep) |
Patrick Williams | 2a25492 | 2023-08-11 09:48:11 -0500 | [diff] [blame] | 356 | |
| 357 | dep_pnid = build_pnid(depmc, dep_pn, deptask) |
| 358 | self.runtaskdeps[tid].append((dep_pnid, dep)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 359 | |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 360 | if task in dataCaches[mc].file_checksums[mcfn]: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 361 | if self.checksum_cache: |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 362 | checksums = self.checksum_cache.get_checksums(dataCaches[mc].file_checksums[mcfn][task], recipename, self.localdirsexclude) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 363 | else: |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 364 | checksums = bb.fetch2.get_file_checksums(dataCaches[mc].file_checksums[mcfn][task], recipename, self.localdirsexclude) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 365 | for (f,cs) in checksums: |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 366 | self.file_checksum_values[tid].append((f,cs)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 367 | |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 368 | taskdep = dataCaches[mc].task_deps[mcfn] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 369 | 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 Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 371 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 378 | |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 379 | taint = self.read_taint(mcfn, task, dataCaches[mc].stamp[mcfn]) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 380 | if taint: |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 381 | self.taints[tid] = taint |
| 382 | logger.warning("%s is tainted from a forced run" % tid) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 383 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 384 | return |
| 385 | |
Andrew Geissler | 5a43b43 | 2020-06-13 10:46:56 -0500 | [diff] [blame] | 386 | def get_taskhash(self, tid, deps, dataCaches): |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 387 | |
| 388 | data = self.basehash[tid] |
Patrick Williams | 2a25492 | 2023-08-11 09:48:11 -0500 | [diff] [blame] | 389 | for dep in sorted(self.runtaskdeps[tid]): |
| 390 | data += self.get_unihash(dep[1]) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 391 | |
Andrew Geissler | 5082cc7 | 2023-09-11 08:41:39 -0400 | [diff] [blame] | 392 | for (f, cs) in sorted(self.file_checksum_values[tid], key=clean_checksum_file_path): |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 393 | if cs: |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 394 | if "/./" in f: |
Patrick Williams | 864cc43 | 2023-02-09 14:54:44 -0600 | [diff] [blame] | 395 | data += "./" + f.split("/./")[1] |
| 396 | data += cs |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 397 | |
| 398 | if tid in self.taints: |
| 399 | if self.taints[tid].startswith("nostamp:"): |
Patrick Williams | 864cc43 | 2023-02-09 14:54:44 -0600 | [diff] [blame] | 400 | data += self.taints[tid][8:] |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 401 | else: |
Patrick Williams | 864cc43 | 2023-02-09 14:54:44 -0600 | [diff] [blame] | 402 | data += self.taints[tid] |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 403 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 404 | h = hashlib.sha256(data.encode("utf-8")).hexdigest() |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 405 | self.taskhash[tid] = h |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 406 | #d.setVar("BB_TASKHASH:task-%s" % task, taskhash[task]) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 407 | return h |
| 408 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 409 | 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 Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 418 | def save_unitaskhashes(self): |
| 419 | self.unihash_cache.save(self.unitaskhashes) |
| 420 | |
Andrew Geissler | 78b7279 | 2022-06-14 06:47:25 -0500 | [diff] [blame] | 421 | def copy_unitaskhashes(self, targetdir): |
| 422 | self.unihash_cache.copyfile(targetdir) |
| 423 | |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 424 | def dump_sigtask(self, mcfn, task, stampbase, runtime): |
| 425 | tid = mcfn + ":" + task |
| 426 | mc = bb.runqueue.mc_from_tid(mcfn) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 427 | referencestamp = stampbase |
| 428 | if isinstance(runtime, str) and runtime.startswith("customfile"): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 429 | sigfile = stampbase |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 430 | referencestamp = runtime[11:] |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 431 | elif runtime and tid in self.taskhash: |
Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 432 | sigfile = stampbase + "." + task + ".sigdata" + "." + self.get_unihash(tid) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 433 | else: |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 434 | sigfile = stampbase + "." + task + ".sigbasedata" + "." + self.basehash[tid] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 435 | |
Andrew Geissler | c3d88e4 | 2020-10-02 09:45:00 -0500 | [diff] [blame] | 436 | with bb.utils.umask(0o002): |
| 437 | bb.utils.mkdirhier(os.path.dirname(sigfile)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 438 | |
| 439 | data = {} |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 440 | data['task'] = task |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 441 | data['basehash_ignore_vars'] = self.basehash_ignore_vars |
| 442 | data['taskhash_ignore_tasks'] = self.taskhash_ignore_tasks |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 443 | data['taskdeps'] = self.datacaches[mc].siggen_taskdeps[mcfn][task] |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 444 | data['basehash'] = self.basehash[tid] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 445 | data['gendeps'] = {} |
| 446 | data['varvals'] = {} |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 447 | data['varvals'][task] = self.datacaches[mc].siggen_varvals[mcfn][task] |
| 448 | for dep in self.datacaches[mc].siggen_taskdeps[mcfn][task]: |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 449 | if dep in self.basehash_ignore_vars: |
Andrew Geissler | 5082cc7 | 2023-09-11 08:41:39 -0400 | [diff] [blame] | 450 | continue |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 451 | data['gendeps'][dep] = self.datacaches[mc].siggen_gendeps[mcfn][dep] |
| 452 | data['varvals'][dep] = self.datacaches[mc].siggen_varvals[mcfn][dep] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 453 | |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 454 | if runtime and tid in self.taskhash: |
Patrick Williams | 2a25492 | 2023-08-11 09:48:11 -0500 | [diff] [blame] | 455 | data['runtaskdeps'] = [dep[0] for dep in sorted(self.runtaskdeps[tid])] |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 456 | data['file_checksum_values'] = [] |
Andrew Geissler | 5082cc7 | 2023-09-11 08:41:39 -0400 | [diff] [blame] | 457 | for f,cs in sorted(self.file_checksum_values[tid], key=clean_checksum_file_path): |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 458 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 462 | data['runtaskhashes'] = {} |
Patrick Williams | 2a25492 | 2023-08-11 09:48:11 -0500 | [diff] [blame] | 463 | for dep in self.runtaskdeps[tid]: |
| 464 | data['runtaskhashes'][dep[0]] = self.get_unihash(dep[1]) |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 465 | data['taskhash'] = self.taskhash[tid] |
Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 466 | data['unihash'] = self.get_unihash(tid) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 467 | |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 468 | taint = self.read_taint(mcfn, task, referencestamp) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 469 | if taint: |
| 470 | data['taint'] = taint |
| 471 | |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 472 | if runtime and tid in self.taints: |
| 473 | if 'nostamp:' in self.taints[tid]: |
| 474 | data['taint'] = self.taints[tid] |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 475 | |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 476 | computed_basehash = calc_basehash(data) |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 477 | 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 Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 480 | computed_taskhash = calc_taskhash(data) |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 481 | 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 Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 484 | |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 485 | fd, tmpfile = bb.utils.mkstemp(dir=os.path.dirname(sigfile), prefix="sigtask.") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 486 | try: |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 487 | 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 Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 490 | os.chmod(tmpfile, 0o664) |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 491 | bb.utils.rename(tmpfile, sigfile) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 492 | except (OSError, IOError) as err: |
| 493 | try: |
| 494 | os.unlink(tmpfile) |
| 495 | except OSError: |
| 496 | pass |
| 497 | raise err |
| 498 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 499 | class SignatureGeneratorBasicHash(SignatureGeneratorBasic): |
| 500 | name = "basichash" |
| 501 | |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 502 | def get_stampfile_hash(self, tid): |
| 503 | if tid in self.taskhash: |
| 504 | return self.taskhash[tid] |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 505 | |
| 506 | # If task is not in basehash, then error |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 507 | return self.basehash[tid] |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 508 | |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 509 | def stampfile(self, stampbase, mcfn, taskname, extrainfo, clean=False): |
| 510 | if taskname.endswith("_setscene"): |
| 511 | tid = mcfn + ":" + taskname[:-9] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 512 | else: |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 513 | tid = mcfn + ":" + taskname |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 514 | if clean: |
| 515 | h = "*" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 516 | else: |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 517 | h = self.get_stampfile_hash(tid) |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 518 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 519 | return ("%s.%s.%s.%s" % (stampbase, taskname, h, extrainfo)).rstrip('.') |
| 520 | |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 521 | def stampcleanmask(self, stampbase, mcfn, taskname, extrainfo): |
| 522 | return self.stampfile(stampbase, mcfn, taskname, extrainfo, clean=True) |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 523 | |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 524 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 538 | |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 539 | class SignatureGeneratorUniHashMixIn(object): |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 540 | def __init__(self, data): |
| 541 | self.extramethod = {} |
Patrick Williams | 73bd93f | 2024-02-20 08:07:48 -0600 | [diff] [blame] | 542 | # 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 Williams | 03514f1 | 2024-04-05 07:04:11 -0500 | [diff] [blame] | 547 | self.username = None |
| 548 | self.password = None |
Patrick Williams | 44b3caf | 2024-04-12 16:51:14 -0500 | [diff] [blame] | 549 | 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 Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 558 | super().__init__(data) |
| 559 | |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 560 | def get_taskdata(self): |
Patrick Williams | 44b3caf | 2024-04-12 16:51:14 -0500 | [diff] [blame] | 561 | return (self.server, self.method, self.extramethod, self.max_parallel, self.username, self.password, self.env) + super().get_taskdata() |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 562 | |
| 563 | def set_taskdata(self, data): |
Patrick Williams | 44b3caf | 2024-04-12 16:51:14 -0500 | [diff] [blame] | 564 | self.server, self.method, self.extramethod, self.max_parallel, self.username, self.password, self.env = data[:7] |
| 565 | super().set_taskdata(data[7:]) |
Patrick Williams | 03514f1 | 2024-04-05 07:04:11 -0500 | [diff] [blame] | 566 | |
| 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 Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 575 | |
Patrick Williams | 44b3caf | 2024-04-12 16:51:14 -0500 | [diff] [blame] | 576 | @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 Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 582 | |
Patrick Williams | 44b3caf | 2024-04-12 16:51:14 -0500 | [diff] [blame] | 583 | yield |
| 584 | finally: |
Patrick Williams | 2f814a6 | 2024-04-16 16:28:03 -0500 | [diff] [blame] | 585 | for k, v in self.env.items(): |
| 586 | if k in orig_env: |
| 587 | os.environ[k] = orig_env[k] |
| 588 | else: |
| 589 | del os.environ[k] |
Patrick Williams | 44b3caf | 2024-04-12 16:51:14 -0500 | [diff] [blame] | 590 | |
| 591 | @contextmanager |
| 592 | def client(self): |
| 593 | with self._client_env(): |
| 594 | if getattr(self, '_client', None) is None: |
| 595 | self._client = hashserv.create_client(self.server, **self.get_hashserv_creds()) |
| 596 | yield self._client |
| 597 | |
| 598 | @contextmanager |
Patrick Williams | 73bd93f | 2024-02-20 08:07:48 -0600 | [diff] [blame] | 599 | def client_pool(self): |
Patrick Williams | 44b3caf | 2024-04-12 16:51:14 -0500 | [diff] [blame] | 600 | with self._client_env(): |
| 601 | if getattr(self, '_client_pool', None) is None: |
| 602 | self._client_pool = hashserv.client.ClientPool(self.server, self.max_parallel, **self.get_hashserv_creds()) |
| 603 | yield self._client_pool |
Patrick Williams | 73bd93f | 2024-02-20 08:07:48 -0600 | [diff] [blame] | 604 | |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 605 | def reset(self, data): |
Patrick Williams | 73bd93f | 2024-02-20 08:07:48 -0600 | [diff] [blame] | 606 | self.__close_clients() |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 607 | return super().reset(data) |
| 608 | |
| 609 | def exit(self): |
Patrick Williams | 73bd93f | 2024-02-20 08:07:48 -0600 | [diff] [blame] | 610 | self.__close_clients() |
| 611 | return super().exit() |
| 612 | |
| 613 | def __close_clients(self): |
Patrick Williams | 44b3caf | 2024-04-12 16:51:14 -0500 | [diff] [blame] | 614 | with self._client_env(): |
| 615 | if getattr(self, '_client', None) is not None: |
| 616 | self._client.close() |
| 617 | self._client = None |
| 618 | if getattr(self, '_client_pool', None) is not None: |
| 619 | self._client_pool.close() |
| 620 | self._client_pool = None |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 621 | |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 622 | def get_stampfile_hash(self, tid): |
| 623 | if tid in self.taskhash: |
| 624 | # If a unique hash is reported, use it as the stampfile hash. This |
| 625 | # ensures that if a task won't be re-run if the taskhash changes, |
| 626 | # but it would result in the same output hash |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 627 | unihash = self._get_unihash(tid) |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 628 | if unihash is not None: |
| 629 | return unihash |
| 630 | |
| 631 | return super().get_stampfile_hash(tid) |
| 632 | |
| 633 | def set_unihash(self, tid, unihash): |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 634 | (mc, fn, taskname, taskfn) = bb.runqueue.split_tid_mcfn(tid) |
| 635 | key = mc + ":" + self.tidtopn[tid] + ":" + taskname |
| 636 | self.unitaskhashes[key] = (self.taskhash[tid], unihash) |
| 637 | self.unihash[tid] = unihash |
| 638 | |
| 639 | def _get_unihash(self, tid, checkkey=None): |
| 640 | if tid not in self.tidtopn: |
| 641 | return None |
| 642 | (mc, fn, taskname, taskfn) = bb.runqueue.split_tid_mcfn(tid) |
| 643 | key = mc + ":" + self.tidtopn[tid] + ":" + taskname |
| 644 | if key not in self.unitaskhashes: |
| 645 | return None |
| 646 | if not checkkey: |
| 647 | checkkey = self.taskhash[tid] |
| 648 | (key, unihash) = self.unitaskhashes[key] |
| 649 | if key != checkkey: |
| 650 | return None |
| 651 | return unihash |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 652 | |
Patrick Williams | 73bd93f | 2024-02-20 08:07:48 -0600 | [diff] [blame] | 653 | def get_cached_unihash(self, tid): |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 654 | taskhash = self.taskhash[tid] |
| 655 | |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 656 | # If its not a setscene task we can return |
| 657 | if self.setscenetasks and tid not in self.setscenetasks: |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 658 | self.unihash[tid] = None |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 659 | return taskhash |
| 660 | |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 661 | # TODO: This cache can grow unbounded. It probably only needs to keep |
| 662 | # for each task |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 663 | unihash = self._get_unihash(tid) |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 664 | if unihash is not None: |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 665 | self.unihash[tid] = unihash |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 666 | return unihash |
| 667 | |
Patrick Williams | 73bd93f | 2024-02-20 08:07:48 -0600 | [diff] [blame] | 668 | return None |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 669 | |
Patrick Williams | 73bd93f | 2024-02-20 08:07:48 -0600 | [diff] [blame] | 670 | def _get_method(self, tid): |
| 671 | method = self.method |
| 672 | if tid in self.extramethod: |
| 673 | method = method + self.extramethod[tid] |
| 674 | |
| 675 | return method |
| 676 | |
| 677 | def unihashes_exist(self, query): |
| 678 | if len(query) == 0: |
| 679 | return {} |
| 680 | |
| 681 | uncached_query = {} |
| 682 | result = {} |
| 683 | for key, unihash in query.items(): |
| 684 | if unihash in self.unihash_exists_cache: |
| 685 | result[key] = True |
| 686 | else: |
| 687 | uncached_query[key] = unihash |
| 688 | |
| 689 | if self.max_parallel <= 1 or len(uncached_query) <= 1: |
| 690 | # No parallelism required. Make the query serially with the single client |
Patrick Williams | 44b3caf | 2024-04-12 16:51:14 -0500 | [diff] [blame] | 691 | with self.client() as client: |
| 692 | uncached_result = { |
| 693 | key: client.unihash_exists(value) for key, value in uncached_query.items() |
| 694 | } |
Patrick Williams | 73bd93f | 2024-02-20 08:07:48 -0600 | [diff] [blame] | 695 | else: |
Patrick Williams | 44b3caf | 2024-04-12 16:51:14 -0500 | [diff] [blame] | 696 | with self.client_pool() as client_pool: |
| 697 | uncached_result = client_pool.unihashes_exist(uncached_query) |
Patrick Williams | 73bd93f | 2024-02-20 08:07:48 -0600 | [diff] [blame] | 698 | |
| 699 | for key, exists in uncached_result.items(): |
| 700 | if exists: |
| 701 | self.unihash_exists_cache.add(query[key]) |
| 702 | result[key] = exists |
| 703 | |
| 704 | return result |
| 705 | |
| 706 | def get_unihash(self, tid): |
| 707 | return self.get_unihashes([tid])[tid] |
| 708 | |
| 709 | def get_unihashes(self, tids): |
| 710 | """ |
| 711 | For a iterable of tids, returns a dictionary that maps each tid to a |
| 712 | unihash |
| 713 | """ |
| 714 | result = {} |
| 715 | queries = {} |
| 716 | query_result = {} |
| 717 | |
| 718 | for tid in tids: |
| 719 | unihash = self.get_cached_unihash(tid) |
| 720 | if unihash: |
| 721 | result[tid] = unihash |
| 722 | else: |
| 723 | queries[tid] = (self._get_method(tid), self.taskhash[tid]) |
| 724 | |
| 725 | if len(queries) == 0: |
| 726 | return result |
| 727 | |
| 728 | if self.max_parallel <= 1 or len(queries) <= 1: |
| 729 | # No parallelism required. Make the query serially with the single client |
Patrick Williams | 44b3caf | 2024-04-12 16:51:14 -0500 | [diff] [blame] | 730 | with self.client() as client: |
| 731 | for tid, args in queries.items(): |
| 732 | query_result[tid] = client.get_unihash(*args) |
Patrick Williams | 73bd93f | 2024-02-20 08:07:48 -0600 | [diff] [blame] | 733 | else: |
Patrick Williams | 44b3caf | 2024-04-12 16:51:14 -0500 | [diff] [blame] | 734 | with self.client_pool() as client_pool: |
| 735 | query_result = client_pool.get_unihashes(queries) |
Patrick Williams | 73bd93f | 2024-02-20 08:07:48 -0600 | [diff] [blame] | 736 | |
| 737 | for tid, unihash in query_result.items(): |
| 738 | # In the absence of being able to discover a unique hash from the |
| 739 | # server, make it be equivalent to the taskhash. The unique "hash" only |
| 740 | # really needs to be a unique string (not even necessarily a hash), but |
| 741 | # making it match the taskhash has a few advantages: |
| 742 | # |
| 743 | # 1) All of the sstate code that assumes hashes can be the same |
| 744 | # 2) It provides maximal compatibility with builders that don't use |
| 745 | # an equivalency server |
| 746 | # 3) The value is easy for multiple independent builders to derive the |
| 747 | # same unique hash from the same input. This means that if the |
| 748 | # independent builders find the same taskhash, but it isn't reported |
| 749 | # to the server, there is a better chance that they will agree on |
| 750 | # the unique hash. |
| 751 | taskhash = self.taskhash[tid] |
| 752 | if unihash: |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 753 | # A unique hash equal to the taskhash is not very interesting, |
| 754 | # so it is reported it at debug level 2. If they differ, that |
| 755 | # is much more interesting, so it is reported at debug level 1 |
Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame] | 756 | hashequiv_logger.bbdebug((1, 2)[unihash == taskhash], 'Found unihash %s in place of %s for %s from %s' % (unihash, taskhash, tid, self.server)) |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 757 | else: |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 758 | hashequiv_logger.debug2('No reported unihash for %s:%s from %s' % (tid, taskhash, self.server)) |
Patrick Williams | 73bd93f | 2024-02-20 08:07:48 -0600 | [diff] [blame] | 759 | unihash = taskhash |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 760 | |
Patrick Williams | 73bd93f | 2024-02-20 08:07:48 -0600 | [diff] [blame] | 761 | |
| 762 | self.set_unihash(tid, unihash) |
| 763 | self.unihash[tid] = unihash |
| 764 | result[tid] = unihash |
| 765 | |
| 766 | return result |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 767 | |
| 768 | def report_unihash(self, path, task, d): |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 769 | import importlib |
| 770 | |
| 771 | taskhash = d.getVar('BB_TASKHASH') |
| 772 | unihash = d.getVar('BB_UNIHASH') |
| 773 | report_taskdata = d.getVar('SSTATE_HASHEQUIV_REPORT_TASKDATA') == '1' |
| 774 | tempdir = d.getVar('T') |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 775 | mcfn = d.getVar('BB_FILENAME') |
| 776 | tid = mcfn + ':do_' + task |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 777 | key = tid + ':' + taskhash |
Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 778 | |
| 779 | if self.setscenetasks and tid not in self.setscenetasks: |
| 780 | return |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 781 | |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 782 | # This can happen if locked sigs are in action. Detect and just exit |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 783 | if taskhash != self.taskhash[tid]: |
| 784 | return |
| 785 | |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 786 | # Sanity checks |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 787 | cache_unihash = self._get_unihash(tid, checkkey=taskhash) |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 788 | if cache_unihash is None: |
| 789 | bb.fatal('%s not in unihash cache. Please report this error' % key) |
| 790 | |
| 791 | if cache_unihash != unihash: |
| 792 | bb.fatal("Cache unihash %s doesn't match BB_UNIHASH %s" % (cache_unihash, unihash)) |
| 793 | |
| 794 | sigfile = None |
| 795 | sigfile_name = "depsig.do_%s.%d" % (task, os.getpid()) |
| 796 | sigfile_link = "depsig.do_%s" % task |
| 797 | |
| 798 | try: |
| 799 | sigfile = open(os.path.join(tempdir, sigfile_name), 'w+b') |
| 800 | |
| 801 | locs = {'path': path, 'sigfile': sigfile, 'task': task, 'd': d} |
| 802 | |
| 803 | if "." in self.method: |
| 804 | (module, method) = self.method.rsplit('.', 1) |
| 805 | locs['method'] = getattr(importlib.import_module(module), method) |
| 806 | outhash = bb.utils.better_eval('method(path, sigfile, task, d)', locs) |
| 807 | else: |
| 808 | outhash = bb.utils.better_eval(self.method + '(path, sigfile, task, d)', locs) |
| 809 | |
| 810 | try: |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 811 | extra_data = {} |
| 812 | |
| 813 | owner = d.getVar('SSTATE_HASHEQUIV_OWNER') |
| 814 | if owner: |
| 815 | extra_data['owner'] = owner |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 816 | |
| 817 | if report_taskdata: |
| 818 | sigfile.seek(0) |
| 819 | |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 820 | extra_data['PN'] = d.getVar('PN') |
| 821 | extra_data['PV'] = d.getVar('PV') |
| 822 | extra_data['PR'] = d.getVar('PR') |
| 823 | extra_data['task'] = task |
| 824 | extra_data['outhash_siginfo'] = sigfile.read().decode('utf-8') |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 825 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 826 | method = self.method |
| 827 | if tid in self.extramethod: |
| 828 | method = method + self.extramethod[tid] |
| 829 | |
Patrick Williams | 44b3caf | 2024-04-12 16:51:14 -0500 | [diff] [blame] | 830 | with self.client() as client: |
| 831 | data = client.report_unihash(taskhash, method, outhash, unihash, extra_data) |
| 832 | |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 833 | new_unihash = data['unihash'] |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 834 | |
| 835 | if new_unihash != unihash: |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 836 | hashequiv_logger.debug('Task %s unihash changed %s -> %s by server %s' % (taskhash, unihash, new_unihash, self.server)) |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 837 | bb.event.fire(bb.runqueue.taskUniHashUpdate(mcfn + ':do_' + task, new_unihash), d) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 838 | self.set_unihash(tid, new_unihash) |
| 839 | d.setVar('BB_UNIHASH', new_unihash) |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 840 | else: |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 841 | hashequiv_logger.debug('Reported task %s as unihash %s to %s' % (taskhash, unihash, self.server)) |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 842 | except ConnectionError as e: |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 843 | bb.warn('Error contacting Hash Equivalence Server %s: %s' % (self.server, str(e))) |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 844 | finally: |
| 845 | if sigfile: |
| 846 | sigfile.close() |
| 847 | |
| 848 | sigfile_link_path = os.path.join(tempdir, sigfile_link) |
| 849 | bb.utils.remove(sigfile_link_path) |
| 850 | |
| 851 | try: |
| 852 | os.symlink(sigfile_name, sigfile_link_path) |
| 853 | except OSError: |
| 854 | pass |
| 855 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 856 | def report_unihash_equiv(self, tid, taskhash, wanted_unihash, current_unihash, datacaches): |
| 857 | try: |
| 858 | extra_data = {} |
| 859 | method = self.method |
| 860 | if tid in self.extramethod: |
| 861 | method = method + self.extramethod[tid] |
| 862 | |
Patrick Williams | 44b3caf | 2024-04-12 16:51:14 -0500 | [diff] [blame] | 863 | with self.client() as client: |
| 864 | data = client.report_unihash_equiv(taskhash, method, wanted_unihash, extra_data) |
| 865 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 866 | hashequiv_logger.verbose('Reported task %s as unihash %s to %s (%s)' % (tid, wanted_unihash, self.server, str(data))) |
| 867 | |
| 868 | if data is None: |
| 869 | bb.warn("Server unable to handle unihash report") |
| 870 | return False |
| 871 | |
| 872 | finalunihash = data['unihash'] |
| 873 | |
| 874 | if finalunihash == current_unihash: |
| 875 | hashequiv_logger.verbose('Task %s unihash %s unchanged by server' % (tid, finalunihash)) |
| 876 | elif finalunihash == wanted_unihash: |
| 877 | hashequiv_logger.verbose('Task %s unihash changed %s -> %s as wanted' % (tid, current_unihash, finalunihash)) |
| 878 | self.set_unihash(tid, finalunihash) |
| 879 | return True |
| 880 | else: |
| 881 | # TODO: What to do here? |
| 882 | hashequiv_logger.verbose('Task %s unihash reported as unwanted hash %s' % (tid, finalunihash)) |
| 883 | |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 884 | except ConnectionError as e: |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 885 | bb.warn('Error contacting Hash Equivalence Server %s: %s' % (self.server, str(e))) |
| 886 | |
| 887 | return False |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 888 | |
| 889 | # |
| 890 | # Dummy class used for bitbake-selftest |
| 891 | # |
| 892 | class SignatureGeneratorTestEquivHash(SignatureGeneratorUniHashMixIn, SignatureGeneratorBasicHash): |
| 893 | name = "TestEquivHash" |
| 894 | def init_rundepcheck(self, data): |
| 895 | super().init_rundepcheck(data) |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 896 | self.server = data.getVar('BB_HASHSERVE') |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 897 | self.method = "sstate_output_hash" |
Patrick Williams | 73bd93f | 2024-02-20 08:07:48 -0600 | [diff] [blame] | 898 | self.max_parallel = 1 |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 899 | |
Andrew Geissler | 5082cc7 | 2023-09-11 08:41:39 -0400 | [diff] [blame] | 900 | def clean_checksum_file_path(file_checksum_tuple): |
| 901 | f, cs = file_checksum_tuple |
| 902 | if "/./" in f: |
| 903 | return "./" + f.split("/./")[1] |
| 904 | return f |
| 905 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 906 | def dump_this_task(outfile, d): |
| 907 | import bb.parse |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 908 | mcfn = d.getVar("BB_FILENAME") |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 909 | task = "do_" + d.getVar("BB_CURRENTTASK") |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 910 | referencestamp = bb.parse.siggen.stampfile_base(mcfn) |
| 911 | bb.parse.siggen.dump_sigtask(mcfn, task, outfile, "customfile:" + referencestamp) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 912 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 913 | def init_colors(enable_color): |
| 914 | """Initialise colour dict for passing to compare_sigfiles()""" |
| 915 | # First set up the colours |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 916 | colors = {'color_title': '\033[1m', |
| 917 | 'color_default': '\033[0m', |
| 918 | 'color_add': '\033[0;32m', |
| 919 | 'color_remove': '\033[0;31m', |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 920 | } |
| 921 | # Leave all keys present but clear the values |
| 922 | if not enable_color: |
| 923 | for k in colors.keys(): |
| 924 | colors[k] = '' |
| 925 | return colors |
| 926 | |
| 927 | def worddiff_str(oldstr, newstr, colors=None): |
| 928 | if not colors: |
| 929 | colors = init_colors(False) |
| 930 | diff = simplediff.diff(oldstr.split(' '), newstr.split(' ')) |
| 931 | ret = [] |
| 932 | for change, value in diff: |
| 933 | value = ' '.join(value) |
| 934 | if change == '=': |
| 935 | ret.append(value) |
| 936 | elif change == '+': |
| 937 | item = '{color_add}{{+{value}+}}{color_default}'.format(value=value, **colors) |
| 938 | ret.append(item) |
| 939 | elif change == '-': |
| 940 | item = '{color_remove}[-{value}-]{color_default}'.format(value=value, **colors) |
| 941 | ret.append(item) |
| 942 | whitespace_note = '' |
| 943 | if oldstr != newstr and ' '.join(oldstr.split()) == ' '.join(newstr.split()): |
| 944 | whitespace_note = ' (whitespace changed)' |
| 945 | return '"%s"%s' % (' '.join(ret), whitespace_note) |
| 946 | |
| 947 | def list_inline_diff(oldlist, newlist, colors=None): |
| 948 | if not colors: |
| 949 | colors = init_colors(False) |
| 950 | diff = simplediff.diff(oldlist, newlist) |
| 951 | ret = [] |
| 952 | for change, value in diff: |
| 953 | value = ' '.join(value) |
| 954 | if change == '=': |
| 955 | ret.append("'%s'" % value) |
| 956 | elif change == '+': |
| 957 | item = '{color_add}+{value}{color_default}'.format(value=value, **colors) |
| 958 | ret.append(item) |
| 959 | elif change == '-': |
| 960 | item = '{color_remove}-{value}{color_default}'.format(value=value, **colors) |
| 961 | ret.append(item) |
| 962 | return '[%s]' % (', '.join(ret)) |
| 963 | |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 964 | # Handled renamed fields |
| 965 | def handle_renames(data): |
| 966 | if 'basewhitelist' in data: |
| 967 | data['basehash_ignore_vars'] = data['basewhitelist'] |
| 968 | del data['basewhitelist'] |
| 969 | if 'taskwhitelist' in data: |
| 970 | data['taskhash_ignore_tasks'] = data['taskwhitelist'] |
| 971 | del data['taskwhitelist'] |
| 972 | |
| 973 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 974 | def compare_sigfiles(a, b, recursecb=None, color=False, collapsed=False): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 975 | output = [] |
| 976 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 977 | colors = init_colors(color) |
| 978 | def color_format(formatstr, **values): |
| 979 | """ |
| 980 | Return colour formatted string. |
| 981 | NOTE: call with the format string, not an already formatted string |
| 982 | containing values (otherwise you could have trouble with { and } |
| 983 | characters) |
| 984 | """ |
| 985 | if not formatstr.endswith('{color_default}'): |
| 986 | formatstr += '{color_default}' |
| 987 | # In newer python 3 versions you can pass both of these directly, |
| 988 | # but we only require 3.4 at the moment |
| 989 | formatparams = {} |
| 990 | formatparams.update(colors) |
| 991 | formatparams.update(values) |
| 992 | return formatstr.format(**formatparams) |
| 993 | |
Patrick Williams | 73bd93f | 2024-02-20 08:07:48 -0600 | [diff] [blame] | 994 | try: |
| 995 | with bb.compress.zstd.open(a, "rt", encoding="utf-8", num_threads=1) as f: |
| 996 | a_data = json.load(f, object_hook=SetDecoder) |
| 997 | except (TypeError, OSError) as err: |
| 998 | bb.error("Failed to open sigdata file '%s': %s" % (a, str(err))) |
| 999 | raise err |
| 1000 | try: |
| 1001 | with bb.compress.zstd.open(b, "rt", encoding="utf-8", num_threads=1) as f: |
| 1002 | b_data = json.load(f, object_hook=SetDecoder) |
| 1003 | except (TypeError, OSError) as err: |
| 1004 | bb.error("Failed to open sigdata file '%s': %s" % (b, str(err))) |
| 1005 | raise err |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1006 | |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 1007 | for data in [a_data, b_data]: |
| 1008 | handle_renames(data) |
| 1009 | |
| 1010 | def dict_diff(a, b, ignored_vars=set()): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1011 | sa = set(a.keys()) |
| 1012 | sb = set(b.keys()) |
| 1013 | common = sa & sb |
| 1014 | changed = set() |
| 1015 | for i in common: |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 1016 | if a[i] != b[i] and i not in ignored_vars: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1017 | changed.add(i) |
| 1018 | added = sb - sa |
| 1019 | removed = sa - sb |
| 1020 | return changed, added, removed |
| 1021 | |
| 1022 | def file_checksums_diff(a, b): |
| 1023 | from collections import Counter |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 1024 | |
| 1025 | # Convert lists back to tuples |
| 1026 | a = [(f[0], f[1]) for f in a] |
| 1027 | b = [(f[0], f[1]) for f in b] |
| 1028 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1029 | # Compare lists, ensuring we can handle duplicate filenames if they exist |
| 1030 | removedcount = Counter(a) |
| 1031 | removedcount.subtract(b) |
| 1032 | addedcount = Counter(b) |
| 1033 | addedcount.subtract(a) |
| 1034 | added = [] |
| 1035 | for x in b: |
| 1036 | if addedcount[x] > 0: |
| 1037 | addedcount[x] -= 1 |
| 1038 | added.append(x) |
| 1039 | removed = [] |
| 1040 | changed = [] |
| 1041 | for x in a: |
| 1042 | if removedcount[x] > 0: |
| 1043 | removedcount[x] -= 1 |
| 1044 | for y in added: |
| 1045 | if y[0] == x[0]: |
| 1046 | changed.append((x[0], x[1], y[1])) |
| 1047 | added.remove(y) |
| 1048 | break |
| 1049 | else: |
| 1050 | removed.append(x) |
| 1051 | added = [x[0] for x in added] |
| 1052 | removed = [x[0] for x in removed] |
| 1053 | return changed, added, removed |
| 1054 | |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 1055 | if 'basehash_ignore_vars' in a_data and a_data['basehash_ignore_vars'] != b_data['basehash_ignore_vars']: |
| 1056 | 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'])) |
| 1057 | if a_data['basehash_ignore_vars'] and b_data['basehash_ignore_vars']: |
| 1058 | output.append("changed items: %s" % a_data['basehash_ignore_vars'].symmetric_difference(b_data['basehash_ignore_vars'])) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1059 | |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 1060 | if 'taskhash_ignore_tasks' in a_data and a_data['taskhash_ignore_tasks'] != b_data['taskhash_ignore_tasks']: |
| 1061 | 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'])) |
| 1062 | if a_data['taskhash_ignore_tasks'] and b_data['taskhash_ignore_tasks']: |
| 1063 | output.append("changed items: %s" % a_data['taskhash_ignore_tasks'].symmetric_difference(b_data['taskhash_ignore_tasks'])) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1064 | |
| 1065 | if a_data['taskdeps'] != b_data['taskdeps']: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1066 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1067 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1068 | if a_data['basehash'] != b_data['basehash'] and not collapsed: |
| 1069 | output.append(color_format("{color_title}basehash changed{color_default} from %s to %s") % (a_data['basehash'], b_data['basehash'])) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1070 | |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 1071 | changed, added, removed = dict_diff(a_data['gendeps'], b_data['gendeps'], a_data['basehash_ignore_vars'] & b_data['basehash_ignore_vars']) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1072 | if changed: |
Patrick Williams | 93c203f | 2021-10-06 16:15:23 -0500 | [diff] [blame] | 1073 | for dep in sorted(changed): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1074 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1075 | if a_data['gendeps'][dep] and b_data['gendeps'][dep]: |
| 1076 | output.append("changed items: %s" % a_data['gendeps'][dep].symmetric_difference(b_data['gendeps'][dep])) |
| 1077 | if added: |
Patrick Williams | 93c203f | 2021-10-06 16:15:23 -0500 | [diff] [blame] | 1078 | for dep in sorted(added): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1079 | output.append(color_format("{color_title}Dependency on variable %s was added") % (dep)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1080 | if removed: |
Patrick Williams | 93c203f | 2021-10-06 16:15:23 -0500 | [diff] [blame] | 1081 | for dep in sorted(removed): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1082 | output.append(color_format("{color_title}Dependency on Variable %s was removed") % (dep)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1083 | |
| 1084 | |
| 1085 | changed, added, removed = dict_diff(a_data['varvals'], b_data['varvals']) |
| 1086 | if changed: |
Patrick Williams | 93c203f | 2021-10-06 16:15:23 -0500 | [diff] [blame] | 1087 | for dep in sorted(changed): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1088 | oldval = a_data['varvals'][dep] |
| 1089 | newval = b_data['varvals'][dep] |
| 1090 | if newval and oldval and ('\n' in oldval or '\n' in newval): |
| 1091 | diff = difflib.unified_diff(oldval.splitlines(), newval.splitlines(), lineterm='') |
| 1092 | # Cut off the first two lines, since we aren't interested in |
| 1093 | # the old/new filename (they are blank anyway in this case) |
| 1094 | difflines = list(diff)[2:] |
| 1095 | if color: |
| 1096 | # Add colour to diff output |
| 1097 | for i, line in enumerate(difflines): |
| 1098 | if line.startswith('+'): |
| 1099 | line = color_format('{color_add}{line}', line=line) |
| 1100 | difflines[i] = line |
| 1101 | elif line.startswith('-'): |
| 1102 | line = color_format('{color_remove}{line}', line=line) |
| 1103 | difflines[i] = line |
| 1104 | output.append(color_format("{color_title}Variable {var} value changed:{color_default}\n{diff}", var=dep, diff='\n'.join(difflines))) |
| 1105 | elif newval and oldval and (' ' in oldval or ' ' in newval): |
| 1106 | output.append(color_format("{color_title}Variable {var} value changed:{color_default}\n{diff}", var=dep, diff=worddiff_str(oldval, newval, colors))) |
| 1107 | else: |
| 1108 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1109 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1110 | if not 'file_checksum_values' in a_data: |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 1111 | a_data['file_checksum_values'] = [] |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1112 | if not 'file_checksum_values' in b_data: |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 1113 | b_data['file_checksum_values'] = [] |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1114 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1115 | changed, added, removed = file_checksums_diff(a_data['file_checksum_values'], b_data['file_checksum_values']) |
| 1116 | if changed: |
| 1117 | for f, old, new in changed: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1118 | output.append(color_format("{color_title}Checksum for file %s changed{color_default} from %s to %s") % (f, old, new)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1119 | if added: |
| 1120 | for f in added: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1121 | output.append(color_format("{color_title}Dependency on checksum of file %s was added") % (f)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1122 | if removed: |
| 1123 | for f in removed: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1124 | output.append(color_format("{color_title}Dependency on checksum of file %s was removed") % (f)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1125 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1126 | if not 'runtaskdeps' in a_data: |
| 1127 | a_data['runtaskdeps'] = {} |
| 1128 | if not 'runtaskdeps' in b_data: |
| 1129 | b_data['runtaskdeps'] = {} |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1130 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1131 | if not collapsed: |
| 1132 | if len(a_data['runtaskdeps']) != len(b_data['runtaskdeps']): |
| 1133 | changed = ["Number of task dependencies changed"] |
| 1134 | else: |
| 1135 | changed = [] |
| 1136 | for idx, task in enumerate(a_data['runtaskdeps']): |
| 1137 | a = a_data['runtaskdeps'][idx] |
| 1138 | b = b_data['runtaskdeps'][idx] |
| 1139 | if a_data['runtaskhashes'][a] != b_data['runtaskhashes'][b] and not collapsed: |
Patrick Williams | 2a25492 | 2023-08-11 09:48:11 -0500 | [diff] [blame] | 1140 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1141 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1142 | if changed: |
Patrick Williams | 2a25492 | 2023-08-11 09:48:11 -0500 | [diff] [blame] | 1143 | clean_a = a_data['runtaskdeps'] |
| 1144 | clean_b = b_data['runtaskdeps'] |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1145 | if clean_a != clean_b: |
| 1146 | output.append(color_format("{color_title}runtaskdeps changed:{color_default}\n%s") % list_inline_diff(clean_a, clean_b, colors)) |
| 1147 | else: |
| 1148 | output.append(color_format("{color_title}runtaskdeps changed:")) |
| 1149 | output.append("\n".join(changed)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1150 | |
| 1151 | |
| 1152 | if 'runtaskhashes' in a_data and 'runtaskhashes' in b_data: |
Patrick Williams | 2a25492 | 2023-08-11 09:48:11 -0500 | [diff] [blame] | 1153 | a = a_data['runtaskhashes'] |
| 1154 | b = b_data['runtaskhashes'] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1155 | changed, added, removed = dict_diff(a, b) |
| 1156 | if added: |
Patrick Williams | 93c203f | 2021-10-06 16:15:23 -0500 | [diff] [blame] | 1157 | for dep in sorted(added): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1158 | bdep_found = False |
| 1159 | if removed: |
| 1160 | for bdep in removed: |
| 1161 | if b[dep] == a[bdep]: |
| 1162 | #output.append("Dependency on task %s was replaced by %s with same hash" % (dep, bdep)) |
| 1163 | bdep_found = True |
| 1164 | if not bdep_found: |
Patrick Williams | 03907ee | 2022-05-01 06:28:52 -0500 | [diff] [blame] | 1165 | output.append(color_format("{color_title}Dependency on task %s was added{color_default} with hash %s") % (dep, b[dep])) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1166 | if removed: |
Patrick Williams | 93c203f | 2021-10-06 16:15:23 -0500 | [diff] [blame] | 1167 | for dep in sorted(removed): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1168 | adep_found = False |
| 1169 | if added: |
| 1170 | for adep in added: |
| 1171 | if b[adep] == a[dep]: |
| 1172 | #output.append("Dependency on task %s was replaced by %s with same hash" % (adep, dep)) |
| 1173 | adep_found = True |
| 1174 | if not adep_found: |
Patrick Williams | 03907ee | 2022-05-01 06:28:52 -0500 | [diff] [blame] | 1175 | output.append(color_format("{color_title}Dependency on task %s was removed{color_default} with hash %s") % (dep, a[dep])) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1176 | if changed: |
Patrick Williams | 93c203f | 2021-10-06 16:15:23 -0500 | [diff] [blame] | 1177 | for dep in sorted(changed): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1178 | if not collapsed: |
Patrick Williams | 03907ee | 2022-05-01 06:28:52 -0500 | [diff] [blame] | 1179 | output.append(color_format("{color_title}Hash for task dependency %s changed{color_default} from %s to %s") % (dep, a[dep], b[dep])) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1180 | if callable(recursecb): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1181 | recout = recursecb(dep, a[dep], b[dep]) |
| 1182 | if recout: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1183 | if collapsed: |
| 1184 | output.extend(recout) |
| 1185 | else: |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 1186 | # If a dependent hash changed, might as well print the line above and then defer to the changes in |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1187 | # that hash since in all likelyhood, they're the same changes this task also saw. |
| 1188 | output = [output[-1]] + recout |
Andrew Geissler | d583833 | 2022-05-27 11:33:10 -0500 | [diff] [blame] | 1189 | break |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1190 | |
| 1191 | a_taint = a_data.get('taint', None) |
| 1192 | b_taint = b_data.get('taint', None) |
| 1193 | if a_taint != b_taint: |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 1194 | if a_taint and a_taint.startswith('nostamp:'): |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 1195 | a_taint = a_taint.replace('nostamp:', 'nostamp(uuid4):') |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 1196 | if b_taint and b_taint.startswith('nostamp:'): |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 1197 | b_taint = b_taint.replace('nostamp:', 'nostamp(uuid4):') |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1198 | output.append(color_format("{color_title}Taint (by forced/invalidated task) changed{color_default} from %s to %s") % (a_taint, b_taint)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1199 | |
| 1200 | return output |
| 1201 | |
| 1202 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 1203 | def calc_basehash(sigdata): |
| 1204 | task = sigdata['task'] |
| 1205 | basedata = sigdata['varvals'][task] |
| 1206 | |
| 1207 | if basedata is None: |
| 1208 | basedata = '' |
| 1209 | |
| 1210 | alldeps = sigdata['taskdeps'] |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 1211 | for dep in sorted(alldeps): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 1212 | basedata = basedata + dep |
| 1213 | val = sigdata['varvals'][dep] |
| 1214 | if val is not None: |
| 1215 | basedata = basedata + str(val) |
| 1216 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 1217 | return hashlib.sha256(basedata.encode("utf-8")).hexdigest() |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 1218 | |
| 1219 | def calc_taskhash(sigdata): |
| 1220 | data = sigdata['basehash'] |
| 1221 | |
| 1222 | for dep in sigdata['runtaskdeps']: |
| 1223 | data = data + sigdata['runtaskhashes'][dep] |
| 1224 | |
| 1225 | for c in sigdata['file_checksum_values']: |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 1226 | if c[1]: |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 1227 | if "./" in c[0]: |
| 1228 | data = data + c[0] |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 1229 | data = data + c[1] |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 1230 | |
| 1231 | if 'taint' in sigdata: |
| 1232 | if 'nostamp:' in sigdata['taint']: |
| 1233 | data = data + sigdata['taint'][8:] |
| 1234 | else: |
| 1235 | data = data + sigdata['taint'] |
| 1236 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 1237 | return hashlib.sha256(data.encode("utf-8")).hexdigest() |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 1238 | |
| 1239 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1240 | def dump_sigfile(a): |
| 1241 | output = [] |
| 1242 | |
Patrick Williams | 73bd93f | 2024-02-20 08:07:48 -0600 | [diff] [blame] | 1243 | try: |
| 1244 | with bb.compress.zstd.open(a, "rt", encoding="utf-8", num_threads=1) as f: |
| 1245 | a_data = json.load(f, object_hook=SetDecoder) |
| 1246 | except (TypeError, OSError) as err: |
| 1247 | bb.error("Failed to open sigdata file '%s': %s" % (a, str(err))) |
| 1248 | raise err |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1249 | |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 1250 | handle_renames(a_data) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1251 | |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 1252 | output.append("basehash_ignore_vars: %s" % (sorted(a_data['basehash_ignore_vars']))) |
| 1253 | |
| 1254 | output.append("taskhash_ignore_tasks: %s" % (sorted(a_data['taskhash_ignore_tasks'] or []))) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1255 | |
| 1256 | output.append("Task dependencies: %s" % (sorted(a_data['taskdeps']))) |
| 1257 | |
| 1258 | output.append("basehash: %s" % (a_data['basehash'])) |
| 1259 | |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 1260 | for dep in sorted(a_data['gendeps']): |
| 1261 | output.append("List of dependencies for variable %s is %s" % (dep, sorted(a_data['gendeps'][dep]))) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1262 | |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 1263 | for dep in sorted(a_data['varvals']): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1264 | output.append("Variable %s value is %s" % (dep, a_data['varvals'][dep])) |
| 1265 | |
| 1266 | if 'runtaskdeps' in a_data: |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 1267 | output.append("Tasks this task depends on: %s" % (sorted(a_data['runtaskdeps']))) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1268 | |
| 1269 | if 'file_checksum_values' in a_data: |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 1270 | output.append("This task depends on the checksums of files: %s" % (sorted(a_data['file_checksum_values']))) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1271 | |
| 1272 | if 'runtaskhashes' in a_data: |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 1273 | for dep in sorted(a_data['runtaskhashes']): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1274 | output.append("Hash for dependent task %s is %s" % (dep, a_data['runtaskhashes'][dep])) |
| 1275 | |
| 1276 | if 'taint' in a_data: |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 1277 | if a_data['taint'].startswith('nostamp:'): |
| 1278 | msg = a_data['taint'].replace('nostamp:', 'nostamp(uuid4):') |
| 1279 | else: |
| 1280 | msg = a_data['taint'] |
| 1281 | output.append("Tainted (by forced/invalidated task): %s" % msg) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1282 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 1283 | if 'task' in a_data: |
| 1284 | computed_basehash = calc_basehash(a_data) |
| 1285 | output.append("Computed base hash is %s and from file %s" % (computed_basehash, a_data['basehash'])) |
| 1286 | else: |
| 1287 | output.append("Unable to compute base hash") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1288 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 1289 | computed_taskhash = calc_taskhash(a_data) |
| 1290 | output.append("Computed task hash is %s" % computed_taskhash) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1291 | |
| 1292 | return output |