Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | # |
| 2 | # BitBake Cache implementation |
| 3 | # |
| 4 | # Caching of bitbake variables before task execution |
| 5 | |
| 6 | # Copyright (C) 2006 Richard Purdie |
| 7 | # Copyright (C) 2012 Intel Corporation |
| 8 | |
| 9 | # but small sections based on code from bin/bitbake: |
| 10 | # Copyright (C) 2003, 2004 Chris Larson |
| 11 | # Copyright (C) 2003, 2004 Phil Blundell |
| 12 | # Copyright (C) 2003 - 2005 Michael 'Mickey' Lauer |
| 13 | # Copyright (C) 2005 Holger Hans Peter Freyther |
| 14 | # Copyright (C) 2005 ROAD GmbH |
| 15 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 16 | # SPDX-License-Identifier: GPL-2.0-only |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 17 | # |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 18 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 19 | import os |
| 20 | import logging |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 21 | import pickle |
Andrew Geissler | 5199d83 | 2021-09-24 16:47:35 -0500 | [diff] [blame] | 22 | from collections import defaultdict |
| 23 | from collections.abc import Mapping |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 24 | import bb.utils |
Andrew Geissler | 5a43b43 | 2020-06-13 10:46:56 -0500 | [diff] [blame] | 25 | from bb import PrefixLoggerAdapter |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 26 | import re |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 27 | |
| 28 | logger = logging.getLogger("BitBake.Cache") |
| 29 | |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 30 | __cache_version__ = "154" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 31 | |
Andrew Geissler | 5a43b43 | 2020-06-13 10:46:56 -0500 | [diff] [blame] | 32 | def getCacheFile(path, filename, mc, data_hash): |
| 33 | mcspec = '' |
| 34 | if mc: |
| 35 | mcspec = ".%s" % mc |
| 36 | return os.path.join(path, filename + mcspec + "." + data_hash) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 37 | |
| 38 | # RecipeInfoCommon defines common data retrieving methods |
| 39 | # from meta data for caches. CoreRecipeInfo as well as other |
| 40 | # Extra RecipeInfo needs to inherit this class |
| 41 | class RecipeInfoCommon(object): |
| 42 | |
| 43 | @classmethod |
| 44 | def listvar(cls, var, metadata): |
| 45 | return cls.getvar(var, metadata).split() |
| 46 | |
| 47 | @classmethod |
| 48 | def intvar(cls, var, metadata): |
| 49 | return int(cls.getvar(var, metadata) or 0) |
| 50 | |
| 51 | @classmethod |
| 52 | def depvar(cls, var, metadata): |
| 53 | return bb.utils.explode_deps(cls.getvar(var, metadata)) |
| 54 | |
| 55 | @classmethod |
| 56 | def pkgvar(cls, var, packages, metadata): |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 57 | return dict((pkg, cls.depvar("%s:%s" % (var, pkg), metadata)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 58 | for pkg in packages) |
| 59 | |
| 60 | @classmethod |
| 61 | def taskvar(cls, var, tasks, metadata): |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 62 | return dict((task, cls.getvar("%s:task-%s" % (var, task), metadata)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 63 | for task in tasks) |
| 64 | |
| 65 | @classmethod |
| 66 | def flaglist(cls, flag, varlist, metadata, squash=False): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 67 | out_dict = dict((var, metadata.getVarFlag(var, flag)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 68 | for var in varlist) |
| 69 | if squash: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 70 | return dict((k,v) for (k,v) in out_dict.items() if v) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 71 | else: |
| 72 | return out_dict |
| 73 | |
| 74 | @classmethod |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 75 | def getvar(cls, var, metadata, expand = True): |
| 76 | return metadata.getVar(var, expand) or '' |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 77 | |
| 78 | |
| 79 | class CoreRecipeInfo(RecipeInfoCommon): |
| 80 | __slots__ = () |
| 81 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 82 | cachefile = "bb_cache.dat" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 83 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 84 | def __init__(self, filename, metadata): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 85 | self.file_depends = metadata.getVar('__depends', False) |
| 86 | self.timestamp = bb.parse.cached_mtime(filename) |
| 87 | self.variants = self.listvar('__VARIANTS', metadata) + [''] |
| 88 | self.appends = self.listvar('__BBAPPEND', metadata) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 89 | self.nocache = self.getvar('BB_DONT_CACHE', metadata) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 90 | |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 91 | self.provides = self.depvar('PROVIDES', metadata) |
| 92 | self.rprovides = self.depvar('RPROVIDES', metadata) |
| 93 | self.pn = self.getvar('PN', metadata) or bb.parse.vars_from_file(filename,metadata)[0] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 94 | self.packages = self.listvar('PACKAGES', metadata) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 95 | if not self.packages: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 96 | self.packages.append(self.pn) |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 97 | self.packages_dynamic = self.listvar('PACKAGES_DYNAMIC', metadata) |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 98 | self.rprovides_pkg = self.pkgvar('RPROVIDES', self.packages, metadata) |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 99 | |
| 100 | self.skipreason = self.getvar('__SKIPPED', metadata) |
| 101 | if self.skipreason: |
| 102 | self.skipped = True |
| 103 | return |
| 104 | |
| 105 | self.tasks = metadata.getVar('__BBTASKS', False) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 106 | |
| 107 | self.basetaskhashes = self.taskvar('BB_BASEHASH', self.tasks, metadata) |
| 108 | self.hashfilename = self.getvar('BB_HASHFILENAME', metadata) |
| 109 | |
| 110 | self.task_deps = metadata.getVar('_task_deps', False) or {'tasks': [], 'parents': {}} |
| 111 | |
| 112 | self.skipped = False |
| 113 | self.pe = self.getvar('PE', metadata) |
| 114 | self.pv = self.getvar('PV', metadata) |
| 115 | self.pr = self.getvar('PR', metadata) |
| 116 | self.defaultpref = self.intvar('DEFAULT_PREFERENCE', metadata) |
| 117 | self.not_world = self.getvar('EXCLUDE_FROM_WORLD', metadata) |
| 118 | self.stamp = self.getvar('STAMP', metadata) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 119 | self.stampclean = self.getvar('STAMPCLEAN', metadata) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 120 | self.stamp_extrainfo = self.flaglist('stamp-extra-info', self.tasks, metadata) |
| 121 | self.file_checksums = self.flaglist('file-checksums', self.tasks, metadata, True) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 122 | self.depends = self.depvar('DEPENDS', metadata) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 123 | self.rdepends = self.depvar('RDEPENDS', metadata) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 124 | self.rrecommends = self.depvar('RRECOMMENDS', metadata) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 125 | self.rdepends_pkg = self.pkgvar('RDEPENDS', self.packages, metadata) |
| 126 | self.rrecommends_pkg = self.pkgvar('RRECOMMENDS', self.packages, metadata) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 127 | self.inherits = self.getvar('__inherit_cache', metadata, expand=False) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 128 | self.fakerootenv = self.getvar('FAKEROOTENV', metadata) |
| 129 | self.fakerootdirs = self.getvar('FAKEROOTDIRS', metadata) |
Andrew Geissler | 95ac1b8 | 2021-03-31 14:34:31 -0500 | [diff] [blame] | 130 | self.fakerootlogs = self.getvar('FAKEROOTLOGS', metadata) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 131 | self.fakerootnoenv = self.getvar('FAKEROOTNOENV', metadata) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 132 | self.extradepsfunc = self.getvar('calculate_extra_depends', metadata) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 133 | |
| 134 | @classmethod |
| 135 | def init_cacheData(cls, cachedata): |
| 136 | # CacheData in Core RecipeInfo Class |
| 137 | cachedata.task_deps = {} |
| 138 | cachedata.pkg_fn = {} |
| 139 | cachedata.pkg_pn = defaultdict(list) |
| 140 | cachedata.pkg_pepvpr = {} |
| 141 | cachedata.pkg_dp = {} |
| 142 | |
| 143 | cachedata.stamp = {} |
| 144 | cachedata.stampclean = {} |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 145 | cachedata.stamp_extrainfo = {} |
| 146 | cachedata.file_checksums = {} |
| 147 | cachedata.fn_provides = {} |
| 148 | cachedata.pn_provides = defaultdict(list) |
| 149 | cachedata.all_depends = [] |
| 150 | |
| 151 | cachedata.deps = defaultdict(list) |
| 152 | cachedata.packages = defaultdict(list) |
| 153 | cachedata.providers = defaultdict(list) |
| 154 | cachedata.rproviders = defaultdict(list) |
| 155 | cachedata.packages_dynamic = defaultdict(list) |
| 156 | |
| 157 | cachedata.rundeps = defaultdict(lambda: defaultdict(list)) |
| 158 | cachedata.runrecs = defaultdict(lambda: defaultdict(list)) |
| 159 | cachedata.possible_world = [] |
| 160 | cachedata.universe_target = [] |
| 161 | cachedata.hashfn = {} |
| 162 | |
| 163 | cachedata.basetaskhash = {} |
| 164 | cachedata.inherits = {} |
| 165 | cachedata.fakerootenv = {} |
| 166 | cachedata.fakerootnoenv = {} |
| 167 | cachedata.fakerootdirs = {} |
Andrew Geissler | 95ac1b8 | 2021-03-31 14:34:31 -0500 | [diff] [blame] | 168 | cachedata.fakerootlogs = {} |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 169 | cachedata.extradepsfunc = {} |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 170 | |
| 171 | def add_cacheData(self, cachedata, fn): |
| 172 | cachedata.task_deps[fn] = self.task_deps |
| 173 | cachedata.pkg_fn[fn] = self.pn |
| 174 | cachedata.pkg_pn[self.pn].append(fn) |
| 175 | cachedata.pkg_pepvpr[fn] = (self.pe, self.pv, self.pr) |
| 176 | cachedata.pkg_dp[fn] = self.defaultpref |
| 177 | cachedata.stamp[fn] = self.stamp |
| 178 | cachedata.stampclean[fn] = self.stampclean |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 179 | cachedata.stamp_extrainfo[fn] = self.stamp_extrainfo |
| 180 | cachedata.file_checksums[fn] = self.file_checksums |
| 181 | |
| 182 | provides = [self.pn] |
| 183 | for provide in self.provides: |
| 184 | if provide not in provides: |
| 185 | provides.append(provide) |
| 186 | cachedata.fn_provides[fn] = provides |
| 187 | |
| 188 | for provide in provides: |
| 189 | cachedata.providers[provide].append(fn) |
| 190 | if provide not in cachedata.pn_provides[self.pn]: |
| 191 | cachedata.pn_provides[self.pn].append(provide) |
| 192 | |
| 193 | for dep in self.depends: |
| 194 | if dep not in cachedata.deps[fn]: |
| 195 | cachedata.deps[fn].append(dep) |
| 196 | if dep not in cachedata.all_depends: |
| 197 | cachedata.all_depends.append(dep) |
| 198 | |
| 199 | rprovides = self.rprovides |
| 200 | for package in self.packages: |
| 201 | cachedata.packages[package].append(fn) |
| 202 | rprovides += self.rprovides_pkg[package] |
| 203 | |
| 204 | for rprovide in rprovides: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 205 | if fn not in cachedata.rproviders[rprovide]: |
| 206 | cachedata.rproviders[rprovide].append(fn) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 207 | |
| 208 | for package in self.packages_dynamic: |
| 209 | cachedata.packages_dynamic[package].append(fn) |
| 210 | |
| 211 | # Build hash of runtime depends and recommends |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 212 | for package in self.packages: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 213 | cachedata.rundeps[fn][package] = list(self.rdepends) + self.rdepends_pkg[package] |
| 214 | cachedata.runrecs[fn][package] = list(self.rrecommends) + self.rrecommends_pkg[package] |
| 215 | |
| 216 | # Collect files we may need for possible world-dep |
| 217 | # calculations |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 218 | if not self.not_world: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 219 | cachedata.possible_world.append(fn) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 220 | #else: |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 221 | # logger.debug2("EXCLUDE FROM WORLD: %s", fn) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 222 | |
| 223 | # create a collection of all targets for sanity checking |
| 224 | # tasks, such as upstream versions, license, and tools for |
| 225 | # task and image creation. |
| 226 | cachedata.universe_target.append(self.pn) |
| 227 | |
| 228 | cachedata.hashfn[fn] = self.hashfilename |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 229 | for task, taskhash in self.basetaskhashes.items(): |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 230 | identifier = '%s:%s' % (fn, task) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 231 | cachedata.basetaskhash[identifier] = taskhash |
| 232 | |
| 233 | cachedata.inherits[fn] = self.inherits |
| 234 | cachedata.fakerootenv[fn] = self.fakerootenv |
| 235 | cachedata.fakerootnoenv[fn] = self.fakerootnoenv |
| 236 | cachedata.fakerootdirs[fn] = self.fakerootdirs |
Andrew Geissler | 95ac1b8 | 2021-03-31 14:34:31 -0500 | [diff] [blame] | 237 | cachedata.fakerootlogs[fn] = self.fakerootlogs |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 238 | cachedata.extradepsfunc[fn] = self.extradepsfunc |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 239 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 240 | def virtualfn2realfn(virtualfn): |
| 241 | """ |
| 242 | Convert a virtual file name to a real one + the associated subclass keyword |
| 243 | """ |
| 244 | mc = "" |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 245 | if virtualfn.startswith('mc:') and virtualfn.count(':') >= 2: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 246 | elems = virtualfn.split(':') |
| 247 | mc = elems[1] |
| 248 | virtualfn = ":".join(elems[2:]) |
| 249 | |
| 250 | fn = virtualfn |
| 251 | cls = "" |
| 252 | if virtualfn.startswith('virtual:'): |
| 253 | elems = virtualfn.split(':') |
| 254 | cls = ":".join(elems[1:-1]) |
| 255 | fn = elems[-1] |
| 256 | |
| 257 | return (fn, cls, mc) |
| 258 | |
| 259 | def realfn2virtual(realfn, cls, mc): |
| 260 | """ |
| 261 | Convert a real filename + the associated subclass keyword to a virtual filename |
| 262 | """ |
| 263 | if cls: |
| 264 | realfn = "virtual:" + cls + ":" + realfn |
| 265 | if mc: |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 266 | realfn = "mc:" + mc + ":" + realfn |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 267 | return realfn |
| 268 | |
| 269 | def variant2virtual(realfn, variant): |
| 270 | """ |
| 271 | Convert a real filename + the associated subclass keyword to a virtual filename |
| 272 | """ |
| 273 | if variant == "": |
| 274 | return realfn |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 275 | if variant.startswith("mc:") and variant.count(':') >= 2: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 276 | elems = variant.split(":") |
| 277 | if elems[2]: |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 278 | return "mc:" + elems[1] + ":virtual:" + ":".join(elems[2:]) + ":" + realfn |
| 279 | return "mc:" + elems[1] + ":" + realfn |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 280 | return "virtual:" + variant + ":" + realfn |
| 281 | |
| 282 | def parse_recipe(bb_data, bbfile, appends, mc=''): |
| 283 | """ |
| 284 | Parse a recipe |
| 285 | """ |
| 286 | |
| 287 | chdir_back = False |
| 288 | |
| 289 | bb_data.setVar("__BBMULTICONFIG", mc) |
| 290 | |
| 291 | # expand tmpdir to include this topdir |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 292 | bb_data.setVar('TMPDIR', bb_data.getVar('TMPDIR') or "") |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 293 | bbfile_loc = os.path.abspath(os.path.dirname(bbfile)) |
| 294 | oldpath = os.path.abspath(os.getcwd()) |
| 295 | bb.parse.cached_mtime_noerror(bbfile_loc) |
| 296 | |
| 297 | # The ConfHandler first looks if there is a TOPDIR and if not |
| 298 | # then it would call getcwd(). |
| 299 | # Previously, we chdir()ed to bbfile_loc, called the handler |
| 300 | # and finally chdir()ed back, a couple of thousand times. We now |
| 301 | # just fill in TOPDIR to point to bbfile_loc if there is no TOPDIR yet. |
| 302 | if not bb_data.getVar('TOPDIR', False): |
| 303 | chdir_back = True |
| 304 | bb_data.setVar('TOPDIR', bbfile_loc) |
| 305 | try: |
| 306 | if appends: |
| 307 | bb_data.setVar('__BBAPPEND', " ".join(appends)) |
| 308 | bb_data = bb.parse.handle(bbfile, bb_data) |
| 309 | if chdir_back: |
| 310 | os.chdir(oldpath) |
| 311 | return bb_data |
| 312 | except: |
| 313 | if chdir_back: |
| 314 | os.chdir(oldpath) |
| 315 | raise |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 316 | |
| 317 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 318 | |
| 319 | class NoCache(object): |
| 320 | |
| 321 | def __init__(self, databuilder): |
| 322 | self.databuilder = databuilder |
| 323 | self.data = databuilder.data |
| 324 | |
| 325 | def loadDataFull(self, virtualfn, appends): |
| 326 | """ |
| 327 | Return a complete set of data for fn. |
| 328 | To do this, we need to parse the file. |
| 329 | """ |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 330 | logger.debug("Parsing %s (full)" % virtualfn) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 331 | (fn, virtual, mc) = virtualfn2realfn(virtualfn) |
| 332 | bb_data = self.load_bbfile(virtualfn, appends, virtonly=True) |
| 333 | return bb_data[virtual] |
| 334 | |
Andrew Geissler | 5a43b43 | 2020-06-13 10:46:56 -0500 | [diff] [blame] | 335 | def load_bbfile(self, bbfile, appends, virtonly = False, mc=None): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 336 | """ |
| 337 | Load and parse one .bb build file |
| 338 | Return the data and whether parsing resulted in the file being skipped |
| 339 | """ |
| 340 | |
| 341 | if virtonly: |
| 342 | (bbfile, virtual, mc) = virtualfn2realfn(bbfile) |
| 343 | bb_data = self.databuilder.mcdata[mc].createCopy() |
| 344 | bb_data.setVar("__ONLYFINALISE", virtual or "default") |
| 345 | datastores = parse_recipe(bb_data, bbfile, appends, mc) |
| 346 | return datastores |
| 347 | |
Andrew Geissler | 5a43b43 | 2020-06-13 10:46:56 -0500 | [diff] [blame] | 348 | if mc is not None: |
| 349 | bb_data = self.databuilder.mcdata[mc].createCopy() |
| 350 | return parse_recipe(bb_data, bbfile, appends, mc) |
| 351 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 352 | bb_data = self.data.createCopy() |
| 353 | datastores = parse_recipe(bb_data, bbfile, appends) |
| 354 | |
| 355 | for mc in self.databuilder.mcdata: |
| 356 | if not mc: |
| 357 | continue |
| 358 | bb_data = self.databuilder.mcdata[mc].createCopy() |
| 359 | newstores = parse_recipe(bb_data, bbfile, appends, mc) |
| 360 | for ns in newstores: |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 361 | datastores["mc:%s:%s" % (mc, ns)] = newstores[ns] |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 362 | |
| 363 | return datastores |
| 364 | |
| 365 | class Cache(NoCache): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 366 | """ |
| 367 | BitBake Cache implementation |
| 368 | """ |
Andrew Geissler | 5a43b43 | 2020-06-13 10:46:56 -0500 | [diff] [blame] | 369 | def __init__(self, databuilder, mc, data_hash, caches_array): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 370 | super().__init__(databuilder) |
| 371 | data = databuilder.data |
| 372 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 373 | # Pass caches_array information into Cache Constructor |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 374 | # It will be used later for deciding whether we |
| 375 | # need extra cache file dump/load support |
Andrew Geissler | 5a43b43 | 2020-06-13 10:46:56 -0500 | [diff] [blame] | 376 | self.mc = mc |
| 377 | self.logger = PrefixLoggerAdapter("Cache: %s: " % (mc if mc else "default"), logger) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 378 | self.caches_array = caches_array |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 379 | self.cachedir = data.getVar("CACHE") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 380 | self.clean = set() |
| 381 | self.checked = set() |
| 382 | self.depends_cache = {} |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 383 | self.data_fn = None |
| 384 | self.cacheclean = True |
| 385 | self.data_hash = data_hash |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 386 | self.filelist_regex = re.compile(r'(?:(?<=:True)|(?<=:False))\s+') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 387 | |
| 388 | if self.cachedir in [None, '']: |
| 389 | self.has_cache = False |
Andrew Geissler | 5a43b43 | 2020-06-13 10:46:56 -0500 | [diff] [blame] | 390 | self.logger.info("Not using a cache. " |
| 391 | "Set CACHE = <directory> to enable.") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 392 | return |
| 393 | |
| 394 | self.has_cache = True |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 395 | |
Andrew Geissler | 5a43b43 | 2020-06-13 10:46:56 -0500 | [diff] [blame] | 396 | def getCacheFile(self, cachefile): |
| 397 | return getCacheFile(self.cachedir, cachefile, self.mc, self.data_hash) |
| 398 | |
| 399 | def prepare_cache(self, progress): |
| 400 | if not self.has_cache: |
| 401 | return 0 |
| 402 | |
| 403 | loaded = 0 |
| 404 | |
| 405 | self.cachefile = self.getCacheFile("bb_cache.dat") |
| 406 | |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 407 | self.logger.debug("Cache dir: %s", self.cachedir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 408 | bb.utils.mkdirhier(self.cachedir) |
| 409 | |
| 410 | cache_ok = True |
| 411 | if self.caches_array: |
| 412 | for cache_class in self.caches_array: |
Andrew Geissler | 5a43b43 | 2020-06-13 10:46:56 -0500 | [diff] [blame] | 413 | cachefile = self.getCacheFile(cache_class.cachefile) |
| 414 | cache_exists = os.path.exists(cachefile) |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 415 | self.logger.debug2("Checking if %s exists: %r", cachefile, cache_exists) |
Andrew Geissler | 5a43b43 | 2020-06-13 10:46:56 -0500 | [diff] [blame] | 416 | cache_ok = cache_ok and cache_exists |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 417 | cache_class.init_cacheData(self) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 418 | if cache_ok: |
Andrew Geissler | 5a43b43 | 2020-06-13 10:46:56 -0500 | [diff] [blame] | 419 | loaded = self.load_cachefile(progress) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 420 | elif os.path.isfile(self.cachefile): |
Andrew Geissler | 5a43b43 | 2020-06-13 10:46:56 -0500 | [diff] [blame] | 421 | self.logger.info("Out of date cache found, rebuilding...") |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 422 | else: |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 423 | self.logger.debug("Cache file %s not found, building..." % self.cachefile) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 424 | |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 425 | # We don't use the symlink, its just for debugging convinience |
Andrew Geissler | 5a43b43 | 2020-06-13 10:46:56 -0500 | [diff] [blame] | 426 | if self.mc: |
| 427 | symlink = os.path.join(self.cachedir, "bb_cache.dat.%s" % self.mc) |
| 428 | else: |
| 429 | symlink = os.path.join(self.cachedir, "bb_cache.dat") |
| 430 | |
Brad Bishop | 96ff198 | 2019-08-19 13:50:42 -0400 | [diff] [blame] | 431 | if os.path.exists(symlink): |
| 432 | bb.utils.remove(symlink) |
| 433 | try: |
| 434 | os.symlink(os.path.basename(self.cachefile), symlink) |
| 435 | except OSError: |
| 436 | pass |
| 437 | |
Andrew Geissler | 5a43b43 | 2020-06-13 10:46:56 -0500 | [diff] [blame] | 438 | return loaded |
| 439 | |
| 440 | def cachesize(self): |
| 441 | if not self.has_cache: |
| 442 | return 0 |
| 443 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 444 | cachesize = 0 |
Andrew Geissler | 5a43b43 | 2020-06-13 10:46:56 -0500 | [diff] [blame] | 445 | for cache_class in self.caches_array: |
| 446 | cachefile = self.getCacheFile(cache_class.cachefile) |
| 447 | try: |
| 448 | with open(cachefile, "rb") as cachefile: |
| 449 | cachesize += os.fstat(cachefile.fileno()).st_size |
| 450 | except FileNotFoundError: |
| 451 | pass |
| 452 | |
| 453 | return cachesize |
| 454 | |
| 455 | def load_cachefile(self, progress): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 456 | previous_progress = 0 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 457 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 458 | for cache_class in self.caches_array: |
Andrew Geissler | 5a43b43 | 2020-06-13 10:46:56 -0500 | [diff] [blame] | 459 | cachefile = self.getCacheFile(cache_class.cachefile) |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 460 | self.logger.debug('Loading cache file: %s' % cachefile) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 461 | with open(cachefile, "rb") as cachefile: |
| 462 | pickled = pickle.Unpickler(cachefile) |
| 463 | # Check cache version information |
| 464 | try: |
| 465 | cache_ver = pickled.load() |
| 466 | bitbake_ver = pickled.load() |
| 467 | except Exception: |
Andrew Geissler | 5a43b43 | 2020-06-13 10:46:56 -0500 | [diff] [blame] | 468 | self.logger.info('Invalid cache, rebuilding...') |
| 469 | return 0 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 470 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 471 | if cache_ver != __cache_version__: |
Andrew Geissler | 5a43b43 | 2020-06-13 10:46:56 -0500 | [diff] [blame] | 472 | self.logger.info('Cache version mismatch, rebuilding...') |
| 473 | return 0 |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 474 | elif bitbake_ver != bb.__version__: |
Andrew Geissler | 5a43b43 | 2020-06-13 10:46:56 -0500 | [diff] [blame] | 475 | self.logger.info('Bitbake version mismatch, rebuilding...') |
| 476 | return 0 |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 477 | |
| 478 | # Load the rest of the cache file |
| 479 | current_progress = 0 |
| 480 | while cachefile: |
| 481 | try: |
| 482 | key = pickled.load() |
| 483 | value = pickled.load() |
| 484 | except Exception: |
| 485 | break |
| 486 | if not isinstance(key, str): |
| 487 | bb.warn("%s from extras cache is not a string?" % key) |
| 488 | break |
| 489 | if not isinstance(value, RecipeInfoCommon): |
| 490 | bb.warn("%s from extras cache is not a RecipeInfoCommon class?" % value) |
| 491 | break |
| 492 | |
| 493 | if key in self.depends_cache: |
| 494 | self.depends_cache[key].append(value) |
| 495 | else: |
| 496 | self.depends_cache[key] = [value] |
| 497 | # only fire events on even percentage boundaries |
| 498 | current_progress = cachefile.tell() + previous_progress |
Andrew Geissler | 5a43b43 | 2020-06-13 10:46:56 -0500 | [diff] [blame] | 499 | progress(cachefile.tell() + previous_progress) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 500 | |
| 501 | previous_progress += current_progress |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 502 | |
Andrew Geissler | 5a43b43 | 2020-06-13 10:46:56 -0500 | [diff] [blame] | 503 | return len(self.depends_cache) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 504 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 505 | def parse(self, filename, appends): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 506 | """Parse the specified filename, returning the recipe information""" |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 507 | self.logger.debug("Parsing %s", filename) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 508 | infos = [] |
Andrew Geissler | 5a43b43 | 2020-06-13 10:46:56 -0500 | [diff] [blame] | 509 | datastores = self.load_bbfile(filename, appends, mc=self.mc) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 510 | depends = [] |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 511 | variants = [] |
| 512 | # Process the "real" fn last so we can store variants list |
| 513 | for variant, data in sorted(datastores.items(), |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 514 | key=lambda i: i[0], |
| 515 | reverse=True): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 516 | virtualfn = variant2virtual(filename, variant) |
| 517 | variants.append(variant) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 518 | depends = depends + (data.getVar("__depends", False) or []) |
| 519 | if depends and not variant: |
| 520 | data.setVar("__depends", depends) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 521 | if virtualfn == filename: |
| 522 | data.setVar("__VARIANTS", " ".join(variants)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 523 | info_array = [] |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 524 | for cache_class in self.caches_array: |
| 525 | info = cache_class(filename, data) |
| 526 | info_array.append(info) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 527 | infos.append((virtualfn, info_array)) |
| 528 | |
| 529 | return infos |
| 530 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 531 | def load(self, filename, appends): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 532 | """Obtain the recipe information for the specified filename, |
| 533 | using cached values if available, otherwise parsing. |
| 534 | |
| 535 | Note that if it does parse to obtain the info, it will not |
| 536 | automatically add the information to the cache or to your |
| 537 | CacheData. Use the add or add_info method to do so after |
| 538 | running this, or use loadData instead.""" |
| 539 | cached = self.cacheValid(filename, appends) |
| 540 | if cached: |
| 541 | infos = [] |
| 542 | # info_array item is a list of [CoreRecipeInfo, XXXRecipeInfo] |
| 543 | info_array = self.depends_cache[filename] |
| 544 | for variant in info_array[0].variants: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 545 | virtualfn = variant2virtual(filename, variant) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 546 | infos.append((virtualfn, self.depends_cache[virtualfn])) |
| 547 | else: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 548 | return self.parse(filename, appends, configdata, self.caches_array) |
| 549 | |
| 550 | return cached, infos |
| 551 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 552 | def loadData(self, fn, appends, cacheData): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 553 | """Load the recipe info for the specified filename, |
| 554 | parsing and adding to the cache if necessary, and adding |
| 555 | the recipe information to the supplied CacheData instance.""" |
| 556 | skipped, virtuals = 0, 0 |
| 557 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 558 | cached, infos = self.load(fn, appends) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 559 | for virtualfn, info_array in infos: |
| 560 | if info_array[0].skipped: |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 561 | self.logger.debug("Skipping %s: %s", virtualfn, info_array[0].skipreason) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 562 | skipped += 1 |
| 563 | else: |
| 564 | self.add_info(virtualfn, info_array, cacheData, not cached) |
| 565 | virtuals += 1 |
| 566 | |
| 567 | return cached, skipped, virtuals |
| 568 | |
| 569 | def cacheValid(self, fn, appends): |
| 570 | """ |
| 571 | Is the cache valid for fn? |
| 572 | Fast version, no timestamps checked. |
| 573 | """ |
| 574 | if fn not in self.checked: |
| 575 | self.cacheValidUpdate(fn, appends) |
| 576 | |
| 577 | # Is cache enabled? |
| 578 | if not self.has_cache: |
| 579 | return False |
| 580 | if fn in self.clean: |
| 581 | return True |
| 582 | return False |
| 583 | |
| 584 | def cacheValidUpdate(self, fn, appends): |
| 585 | """ |
| 586 | Is the cache valid for fn? |
| 587 | Make thorough (slower) checks including timestamps. |
| 588 | """ |
| 589 | # Is cache enabled? |
| 590 | if not self.has_cache: |
| 591 | return False |
| 592 | |
| 593 | self.checked.add(fn) |
| 594 | |
| 595 | # File isn't in depends_cache |
| 596 | if not fn in self.depends_cache: |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 597 | self.logger.debug2("%s is not cached", fn) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 598 | return False |
| 599 | |
| 600 | mtime = bb.parse.cached_mtime_noerror(fn) |
| 601 | |
| 602 | # Check file still exists |
| 603 | if mtime == 0: |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 604 | self.logger.debug2("%s no longer exists", fn) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 605 | self.remove(fn) |
| 606 | return False |
| 607 | |
| 608 | info_array = self.depends_cache[fn] |
| 609 | # Check the file's timestamp |
| 610 | if mtime != info_array[0].timestamp: |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 611 | self.logger.debug2("%s changed", fn) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 612 | self.remove(fn) |
| 613 | return False |
| 614 | |
| 615 | # Check dependencies are still valid |
| 616 | depends = info_array[0].file_depends |
| 617 | if depends: |
| 618 | for f, old_mtime in depends: |
| 619 | fmtime = bb.parse.cached_mtime_noerror(f) |
| 620 | # Check if file still exists |
| 621 | if old_mtime != 0 and fmtime == 0: |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 622 | self.logger.debug2("%s's dependency %s was removed", |
Andrew Geissler | 5a43b43 | 2020-06-13 10:46:56 -0500 | [diff] [blame] | 623 | fn, f) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 624 | self.remove(fn) |
| 625 | return False |
| 626 | |
| 627 | if (fmtime != old_mtime): |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 628 | self.logger.debug2("%s's dependency %s changed", |
Andrew Geissler | 5a43b43 | 2020-06-13 10:46:56 -0500 | [diff] [blame] | 629 | fn, f) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 630 | self.remove(fn) |
| 631 | return False |
| 632 | |
| 633 | if hasattr(info_array[0], 'file_checksums'): |
| 634 | for _, fl in info_array[0].file_checksums.items(): |
Patrick Williams | d7e9631 | 2015-09-22 08:09:05 -0500 | [diff] [blame] | 635 | fl = fl.strip() |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 636 | if not fl: |
| 637 | continue |
| 638 | # Have to be careful about spaces and colons in filenames |
| 639 | flist = self.filelist_regex.split(fl) |
| 640 | for f in flist: |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 641 | if not f: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 642 | continue |
| 643 | f, exist = f.split(":") |
| 644 | if (exist == "True" and not os.path.exists(f)) or (exist == "False" and os.path.exists(f)): |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 645 | self.logger.debug2("%s's file checksum list file %s changed", |
Andrew Geissler | 5a43b43 | 2020-06-13 10:46:56 -0500 | [diff] [blame] | 646 | fn, f) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 647 | self.remove(fn) |
| 648 | return False |
| 649 | |
Andrew Geissler | 5a43b43 | 2020-06-13 10:46:56 -0500 | [diff] [blame] | 650 | if tuple(appends) != tuple(info_array[0].appends): |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 651 | self.logger.debug2("appends for %s changed", fn) |
| 652 | self.logger.debug2("%s to %s" % (str(appends), str(info_array[0].appends))) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 653 | self.remove(fn) |
| 654 | return False |
| 655 | |
| 656 | invalid = False |
| 657 | for cls in info_array[0].variants: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 658 | virtualfn = variant2virtual(fn, cls) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 659 | self.clean.add(virtualfn) |
| 660 | if virtualfn not in self.depends_cache: |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 661 | self.logger.debug2("%s is not cached", virtualfn) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 662 | invalid = True |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 663 | elif len(self.depends_cache[virtualfn]) != len(self.caches_array): |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 664 | self.logger.debug2("Extra caches missing for %s?" % virtualfn) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 665 | invalid = True |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 666 | |
| 667 | # If any one of the variants is not present, mark as invalid for all |
| 668 | if invalid: |
| 669 | for cls in info_array[0].variants: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 670 | virtualfn = variant2virtual(fn, cls) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 671 | if virtualfn in self.clean: |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 672 | self.logger.debug2("Removing %s from cache", virtualfn) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 673 | self.clean.remove(virtualfn) |
| 674 | if fn in self.clean: |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 675 | self.logger.debug2("Marking %s as not clean", fn) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 676 | self.clean.remove(fn) |
| 677 | return False |
| 678 | |
| 679 | self.clean.add(fn) |
| 680 | return True |
| 681 | |
| 682 | def remove(self, fn): |
| 683 | """ |
| 684 | Remove a fn from the cache |
| 685 | Called from the parser in error cases |
| 686 | """ |
| 687 | if fn in self.depends_cache: |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 688 | self.logger.debug("Removing %s from cache", fn) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 689 | del self.depends_cache[fn] |
| 690 | if fn in self.clean: |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 691 | self.logger.debug("Marking %s as unclean", fn) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 692 | self.clean.remove(fn) |
| 693 | |
| 694 | def sync(self): |
| 695 | """ |
| 696 | Save the cache |
| 697 | Called from the parser when complete (or exiting) |
| 698 | """ |
| 699 | |
| 700 | if not self.has_cache: |
| 701 | return |
| 702 | |
| 703 | if self.cacheclean: |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 704 | self.logger.debug2("Cache is clean, not saving.") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 705 | return |
| 706 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 707 | for cache_class in self.caches_array: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 708 | cache_class_name = cache_class.__name__ |
Andrew Geissler | 5a43b43 | 2020-06-13 10:46:56 -0500 | [diff] [blame] | 709 | cachefile = self.getCacheFile(cache_class.cachefile) |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 710 | self.logger.debug2("Writing %s", cachefile) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 711 | with open(cachefile, "wb") as f: |
| 712 | p = pickle.Pickler(f, pickle.HIGHEST_PROTOCOL) |
| 713 | p.dump(__cache_version__) |
| 714 | p.dump(bb.__version__) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 715 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 716 | for key, info_array in self.depends_cache.items(): |
| 717 | for info in info_array: |
| 718 | if isinstance(info, RecipeInfoCommon) and info.__class__.__name__ == cache_class_name: |
| 719 | p.dump(key) |
| 720 | p.dump(info) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 721 | |
| 722 | del self.depends_cache |
| 723 | |
| 724 | @staticmethod |
| 725 | def mtime(cachefile): |
| 726 | return bb.parse.cached_mtime_noerror(cachefile) |
| 727 | |
| 728 | def add_info(self, filename, info_array, cacheData, parsed=None, watcher=None): |
Andrew Geissler | 5a43b43 | 2020-06-13 10:46:56 -0500 | [diff] [blame] | 729 | if self.mc is not None: |
| 730 | (fn, cls, mc) = virtualfn2realfn(filename) |
| 731 | if mc: |
Andrew Geissler | d25ed32 | 2020-06-27 00:28:28 -0500 | [diff] [blame] | 732 | self.logger.error("Unexpected multiconfig %s", filename) |
Andrew Geissler | 5a43b43 | 2020-06-13 10:46:56 -0500 | [diff] [blame] | 733 | return |
| 734 | |
| 735 | vfn = realfn2virtual(fn, cls, self.mc) |
| 736 | else: |
| 737 | vfn = filename |
| 738 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 739 | if isinstance(info_array[0], CoreRecipeInfo) and (not info_array[0].skipped): |
Andrew Geissler | 5a43b43 | 2020-06-13 10:46:56 -0500 | [diff] [blame] | 740 | cacheData.add_from_recipeinfo(vfn, info_array) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 741 | |
| 742 | if watcher: |
| 743 | watcher(info_array[0].file_depends) |
| 744 | |
| 745 | if not self.has_cache: |
| 746 | return |
| 747 | |
| 748 | if (info_array[0].skipped or 'SRCREVINACTION' not in info_array[0].pv) and not info_array[0].nocache: |
| 749 | if parsed: |
| 750 | self.cacheclean = False |
| 751 | self.depends_cache[filename] = info_array |
| 752 | |
| 753 | def add(self, file_name, data, cacheData, parsed=None): |
| 754 | """ |
| 755 | Save data we need into the cache |
| 756 | """ |
| 757 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 758 | realfn = virtualfn2realfn(file_name)[0] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 759 | |
| 760 | info_array = [] |
| 761 | for cache_class in self.caches_array: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 762 | info_array.append(cache_class(realfn, data)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 763 | self.add_info(file_name, info_array, cacheData, parsed) |
| 764 | |
Andrew Geissler | 5a43b43 | 2020-06-13 10:46:56 -0500 | [diff] [blame] | 765 | class MulticonfigCache(Mapping): |
| 766 | def __init__(self, databuilder, data_hash, caches_array): |
| 767 | def progress(p): |
| 768 | nonlocal current_progress |
| 769 | nonlocal previous_progress |
| 770 | nonlocal previous_percent |
| 771 | nonlocal cachesize |
| 772 | |
| 773 | current_progress = previous_progress + p |
| 774 | |
| 775 | if current_progress > cachesize: |
| 776 | # we might have calculated incorrect total size because a file |
| 777 | # might've been written out just after we checked its size |
| 778 | cachesize = current_progress |
| 779 | current_percent = 100 * current_progress / cachesize |
| 780 | if current_percent > previous_percent: |
| 781 | previous_percent = current_percent |
| 782 | bb.event.fire(bb.event.CacheLoadProgress(current_progress, cachesize), |
| 783 | databuilder.data) |
| 784 | |
| 785 | |
| 786 | cachesize = 0 |
| 787 | current_progress = 0 |
| 788 | previous_progress = 0 |
| 789 | previous_percent = 0 |
| 790 | self.__caches = {} |
| 791 | |
| 792 | for mc, mcdata in databuilder.mcdata.items(): |
| 793 | self.__caches[mc] = Cache(databuilder, mc, data_hash, caches_array) |
| 794 | |
| 795 | cachesize += self.__caches[mc].cachesize() |
| 796 | |
| 797 | bb.event.fire(bb.event.CacheLoadStarted(cachesize), databuilder.data) |
| 798 | loaded = 0 |
| 799 | |
| 800 | for c in self.__caches.values(): |
| 801 | loaded += c.prepare_cache(progress) |
| 802 | previous_progress = current_progress |
| 803 | |
| 804 | # Note: depends cache number is corresponding to the parsing file numbers. |
| 805 | # The same file has several caches, still regarded as one item in the cache |
| 806 | bb.event.fire(bb.event.CacheLoadCompleted(cachesize, loaded), databuilder.data) |
| 807 | |
| 808 | def __len__(self): |
| 809 | return len(self.__caches) |
| 810 | |
| 811 | def __getitem__(self, key): |
| 812 | return self.__caches[key] |
| 813 | |
| 814 | def __contains__(self, key): |
| 815 | return key in self.__caches |
| 816 | |
| 817 | def __iter__(self): |
| 818 | for k in self.__caches: |
| 819 | yield k |
| 820 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 821 | def init(cooker): |
| 822 | """ |
| 823 | The Objective: Cache the minimum amount of data possible yet get to the |
| 824 | stage of building packages (i.e. tryBuild) without reparsing any .bb files. |
| 825 | |
| 826 | To do this, we intercept getVar calls and only cache the variables we see |
| 827 | being accessed. We rely on the cache getVar calls being made for all |
| 828 | variables bitbake might need to use to reach this stage. For each cached |
| 829 | file we need to track: |
| 830 | |
| 831 | * Its mtime |
| 832 | * The mtimes of all its dependencies |
| 833 | * Whether it caused a parse.SkipRecipe exception |
| 834 | |
| 835 | Files causing parsing errors are evicted from the cache. |
| 836 | |
| 837 | """ |
| 838 | return Cache(cooker.configuration.data, cooker.configuration.data_hash) |
| 839 | |
| 840 | |
| 841 | class CacheData(object): |
| 842 | """ |
| 843 | The data structures we compile from the cached data |
| 844 | """ |
| 845 | |
| 846 | def __init__(self, caches_array): |
| 847 | self.caches_array = caches_array |
| 848 | for cache_class in self.caches_array: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 849 | if not issubclass(cache_class, RecipeInfoCommon): |
| 850 | bb.error("Extra cache data class %s should subclass RecipeInfoCommon class" % cache_class) |
| 851 | cache_class.init_cacheData(self) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 852 | |
| 853 | # Direct cache variables |
| 854 | self.task_queues = {} |
| 855 | self.preferred = {} |
| 856 | self.tasks = {} |
| 857 | # Indirect Cache variables (set elsewhere) |
| 858 | self.ignored_dependencies = [] |
| 859 | self.world_target = set() |
| 860 | self.bbfile_priority = {} |
| 861 | |
| 862 | def add_from_recipeinfo(self, fn, info_array): |
| 863 | for info in info_array: |
| 864 | info.add_cacheData(self, fn) |
| 865 | |
| 866 | class MultiProcessCache(object): |
| 867 | """ |
| 868 | BitBake multi-process cache implementation |
| 869 | |
| 870 | Used by the codeparser & file checksum caches |
| 871 | """ |
| 872 | |
| 873 | def __init__(self): |
| 874 | self.cachefile = None |
| 875 | self.cachedata = self.create_cachedata() |
| 876 | self.cachedata_extras = self.create_cachedata() |
| 877 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 878 | def init_cache(self, d, cache_file_name=None): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 879 | cachedir = (d.getVar("PERSISTENT_DIR") or |
| 880 | d.getVar("CACHE")) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 881 | if cachedir in [None, '']: |
| 882 | return |
| 883 | bb.utils.mkdirhier(cachedir) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 884 | self.cachefile = os.path.join(cachedir, |
| 885 | cache_file_name or self.__class__.cache_file_name) |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 886 | logger.debug("Using cache in '%s'", self.cachefile) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 887 | |
| 888 | glf = bb.utils.lockfile(self.cachefile + ".lock") |
| 889 | |
| 890 | try: |
| 891 | with open(self.cachefile, "rb") as f: |
| 892 | p = pickle.Unpickler(f) |
| 893 | data, version = p.load() |
| 894 | except: |
| 895 | bb.utils.unlockfile(glf) |
| 896 | return |
| 897 | |
| 898 | bb.utils.unlockfile(glf) |
| 899 | |
| 900 | if version != self.__class__.CACHE_VERSION: |
| 901 | return |
| 902 | |
| 903 | self.cachedata = data |
| 904 | |
| 905 | def create_cachedata(self): |
| 906 | data = [{}] |
| 907 | return data |
| 908 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 909 | def save_extras(self): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 910 | if not self.cachefile: |
| 911 | return |
| 912 | |
| 913 | glf = bb.utils.lockfile(self.cachefile + ".lock", shared=True) |
| 914 | |
| 915 | i = os.getpid() |
| 916 | lf = None |
| 917 | while not lf: |
| 918 | lf = bb.utils.lockfile(self.cachefile + ".lock." + str(i), retry=False) |
| 919 | if not lf or os.path.exists(self.cachefile + "-" + str(i)): |
| 920 | if lf: |
| 921 | bb.utils.unlockfile(lf) |
| 922 | lf = None |
| 923 | i = i + 1 |
| 924 | continue |
| 925 | |
| 926 | with open(self.cachefile + "-" + str(i), "wb") as f: |
| 927 | p = pickle.Pickler(f, -1) |
| 928 | p.dump([self.cachedata_extras, self.__class__.CACHE_VERSION]) |
| 929 | |
| 930 | bb.utils.unlockfile(lf) |
| 931 | bb.utils.unlockfile(glf) |
| 932 | |
| 933 | def merge_data(self, source, dest): |
| 934 | for j in range(0,len(dest)): |
| 935 | for h in source[j]: |
| 936 | if h not in dest[j]: |
| 937 | dest[j][h] = source[j][h] |
| 938 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 939 | def save_merge(self): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 940 | if not self.cachefile: |
| 941 | return |
| 942 | |
| 943 | glf = bb.utils.lockfile(self.cachefile + ".lock") |
| 944 | |
| 945 | data = self.cachedata |
| 946 | |
| 947 | for f in [y for y in os.listdir(os.path.dirname(self.cachefile)) if y.startswith(os.path.basename(self.cachefile) + '-')]: |
| 948 | f = os.path.join(os.path.dirname(self.cachefile), f) |
| 949 | try: |
| 950 | with open(f, "rb") as fd: |
| 951 | p = pickle.Unpickler(fd) |
| 952 | extradata, version = p.load() |
| 953 | except (IOError, EOFError): |
| 954 | os.unlink(f) |
| 955 | continue |
| 956 | |
| 957 | if version != self.__class__.CACHE_VERSION: |
| 958 | os.unlink(f) |
| 959 | continue |
| 960 | |
| 961 | self.merge_data(extradata, data) |
| 962 | os.unlink(f) |
| 963 | |
| 964 | with open(self.cachefile, "wb") as f: |
| 965 | p = pickle.Pickler(f, -1) |
| 966 | p.dump([data, self.__class__.CACHE_VERSION]) |
| 967 | |
| 968 | bb.utils.unlockfile(glf) |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 969 | |
| 970 | |
| 971 | class SimpleCache(object): |
| 972 | """ |
| 973 | BitBake multi-process cache implementation |
| 974 | |
| 975 | Used by the codeparser & file checksum caches |
| 976 | """ |
| 977 | |
| 978 | def __init__(self, version): |
| 979 | self.cachefile = None |
| 980 | self.cachedata = None |
| 981 | self.cacheversion = version |
| 982 | |
| 983 | def init_cache(self, d, cache_file_name=None, defaultdata=None): |
| 984 | cachedir = (d.getVar("PERSISTENT_DIR") or |
| 985 | d.getVar("CACHE")) |
| 986 | if not cachedir: |
| 987 | return defaultdata |
| 988 | |
| 989 | bb.utils.mkdirhier(cachedir) |
| 990 | self.cachefile = os.path.join(cachedir, |
| 991 | cache_file_name or self.__class__.cache_file_name) |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 992 | logger.debug("Using cache in '%s'", self.cachefile) |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 993 | |
| 994 | glf = bb.utils.lockfile(self.cachefile + ".lock") |
| 995 | |
| 996 | try: |
| 997 | with open(self.cachefile, "rb") as f: |
| 998 | p = pickle.Unpickler(f) |
| 999 | data, version = p.load() |
| 1000 | except: |
| 1001 | bb.utils.unlockfile(glf) |
| 1002 | return defaultdata |
| 1003 | |
| 1004 | bb.utils.unlockfile(glf) |
| 1005 | |
| 1006 | if version != self.cacheversion: |
| 1007 | return defaultdata |
| 1008 | |
| 1009 | return data |
| 1010 | |
| 1011 | def save(self, data): |
| 1012 | if not self.cachefile: |
| 1013 | return |
| 1014 | |
| 1015 | glf = bb.utils.lockfile(self.cachefile + ".lock") |
| 1016 | |
| 1017 | with open(self.cachefile, "wb") as f: |
| 1018 | p = pickle.Pickler(f, -1) |
| 1019 | p.dump([data, self.cacheversion]) |
| 1020 | |
| 1021 | bb.utils.unlockfile(glf) |