2 # ex:ts=4:sw=4:sts=4:et
3 # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
5 BitBake 'Event' implementation
7 Caching of bitbake variables before task execution
9 # Copyright (C) 2006 Richard Purdie
11 # but small sections based on code from bin/bitbake:
12 # Copyright (C) 2003, 2004 Chris Larson
13 # Copyright (C) 2003, 2004 Phil Blundell
14 # Copyright (C) 2003 - 2005 Michael 'Mickey' Lauer
15 # Copyright (C) 2005 Holger Hans Peter Freyther
16 # Copyright (C) 2005 ROAD GmbH
18 This program is free software; you can redistribute it and/or modify it under
19 the terms of the GNU General Public License as published by the Free Software
20 Foundation; either version 2 of the License, or (at your option) any later
23 This program is distributed in the hope that it will be useful, but WITHOUT
24 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
25 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
27 You should have received a copy of the GNU General Public License along with
28 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
29 Place, Suite 330, Boston, MA 02111-1307 USA.
38 import cPickle as pickle
41 print "NOTE: Importing cPickle failed. Falling back to a very slow implementation."
45 def __init__(self, cooker):
47 self.cachedir = bb.data.getVar("CACHE", cooker.configuration.data, True)
48 self.cachefile = os.path.join(self.cachedir,"bb_cache.dat")
50 self.depends_cache = {}
54 if self.cachedir in [None, '']:
55 if cooker.cb is not None:
56 print "NOTE: Not using a cache. Set CACHE = <directory> to enable."
58 if cooker.cb is not None:
59 print "NOTE: Using cache in '%s'" % self.cachedir
61 os.stat( self.cachedir )
63 bb.mkdirhier( self.cachedir )
65 if (self.mtime(self.cachefile)):
66 p = pickle.Unpickler( file(self.cachefile,"rb"))
67 self.depends_cache = p.load()
68 if self.depends_cache:
69 for fn in self.depends_cache.keys():
71 self.cacheValidUpdate(fn)
73 def getVar(self, var, fn, exp = 0):
75 return self.depends_cache[fn][var]
77 if not fn in self.depends_cache:
78 self.depends_cache[fn] = {}
80 if fn != self.data_fn:
81 bb.fatal("Parsing error data_fn %s and fn %s don't match" % (self.data_fn, fn))
83 result = bb.data.getVar(var, self.data, exp)
84 self.depends_cache[fn][var] = result
87 def setData(self, fn, data):
91 # Make sure __depends makes the depends_cache
92 self.getVar("__depends", fn, True)
93 self.depends_cache[fn]["CACHETIMESTAMP"] = bb.parse.cached_mtime(fn)
95 def loadDataFull(self, fn, cooker):
97 bb_data, skipped = self.load_bbfile(fn, cooker)
100 def loadData(self, fn, cooker):
101 if self.cacheValid(fn):
102 if "SKIPPED" in self.depends_cache[fn]:
106 bb_data, skipped = self.load_bbfile(fn, cooker)
107 self.setData(fn, bb_data)
108 return False, skipped
110 def cacheValid(self, fn):
112 if self.cachedir in [None, '']:
118 def cacheValidUpdate(self, fn):
120 if self.cachedir in [None, '']:
123 # File isn't in depends_cache
124 if not fn in self.depends_cache:
125 bb.note("Cache: %s is not cached" % fn)
130 # Check the file's timestamp
131 if bb.parse.cached_mtime(fn) > self.getVar("CACHETIMESTAMP", fn, True):
132 bb.note("Cache: %s changed" % fn)
137 # Check dependencies are still valid
138 depends = self.getVar("__depends", fn, True)
140 deps = depends.split(" ")
142 (f,old_mtime_s) = dep.split("@")
143 old_mtime = int(old_mtime_s)
144 new_mtime = bb.parse.cached_mtime(f)
145 if (new_mtime > old_mtime):
146 bb.note("Cache: %s's dependency %s changed" % (fn, f))
151 #bb.note("Depends Cache: %s is clean" % fn)
152 if not fn in self.clean:
158 if not fn in self.depends_cache:
159 self.depends_cache[fn] = {}
160 self.depends_cache[fn]["SKIPPED"] = "1"
162 def remove(self, fn):
163 bb.note("Removing %s from cache" % fn)
164 if fn in self.depends_cache:
165 del self.depends_cache[fn]
168 p = pickle.Pickler(file(self.cachefile, "wb" ), -1 )
169 p.dump(self.depends_cache)
171 def mtime(self, cachefile):
173 return os.stat(cachefile)[8]
177 def load_bbfile( self, bbfile , cooker):
178 """Load and parse one .bb build file"""
181 from bb import utils, data, parse, debug, event, fatal
183 topdir = data.getVar('TOPDIR', cooker.configuration.data)
185 topdir = os.path.abspath(os.getcwd())
187 data.setVar('TOPDIR', topdir, cooker.configuration)
188 bbfile = os.path.abspath(bbfile)
189 bbfile_loc = os.path.abspath(os.path.dirname(bbfile))
190 # expand tmpdir to include this topdir
191 data.setVar('TMPDIR', data.getVar('TMPDIR', cooker.configuration.data, 1) or "", cooker.configuration.data)
192 # set topdir to location of .bb file
194 #data.setVar('TOPDIR', topdir, cfg)
196 oldpath = os.path.abspath(os.getcwd())
198 bb_data = data.init_db(cooker.configuration.data)
200 parse.handle(bbfile, bb_data) # read .bb data
202 return bb_data, False
203 except bb.parse.SkipPackage: