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