summaryrefslogtreecommitdiffstats
path: root/bitbake/lib/bb/cache.py
blob: 921a9f75891f63d325515f77f05fe1f7042d0316 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/usr/bin/env python
# ex:ts=4:sw=4:sts=4:et
# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
"""
BitBake 'Event' implementation

Caching of bitbake variables before task execution

# Copyright (C) 2006        Richard Purdie

# but small sections based on code from bin/bitbake:
# Copyright (C) 2003, 2004  Chris Larson
# Copyright (C) 2003, 2004  Phil Blundell
# Copyright (C) 2003 - 2005 Michael 'Mickey' Lauer
# Copyright (C) 2005        Holger Hans Peter Freyther
# Copyright (C) 2005        ROAD GmbH

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA. 

"""

import os, re
import bb.data
import bb.utils

try:
    import cPickle as pickle
except ImportError:
    import pickle
    print "NOTE: Importing cPickle failed. Falling back to a very slow implementation."

# __cache_version__ = "123"
__cache_version__ = "124" # changes the __depends structure

class Cache:
    """
    BitBake Cache implementation
    """
    def __init__(self, cooker):


        self.cachedir = bb.data.getVar("CACHE", cooker.configuration.data, True)
        self.clean = {}
        self.depends_cache = {}
        self.data = None
        self.data_fn = None

        if self.cachedir in [None, '']:
            self.has_cache = False
            if cooker.cb is not None:
                print "NOTE: Not using a cache. Set CACHE = <directory> to enable."
        else:
            self.has_cache = True
            self.cachefile = os.path.join(self.cachedir,"bb_cache.dat")
            
            if cooker.cb is not None:
                print "NOTE: Using cache in '%s'" % self.cachedir
            try:
                os.stat( self.cachedir )
            except OSError:
                bb.mkdirhier( self.cachedir )

        if self.has_cache and (self.mtime(self.cachefile)):
            try:
                p = pickle.Unpickler( file(self.cachefile,"rb"))
                self.depends_cache, version_data = p.load()
                if version_data['CACHE_VER'] != __cache_version__:
                    raise ValueError, 'Cache Version Mismatch'
                if version_data['BITBAKE_VER'] != bb.__version__:
                    raise ValueError, 'Bitbake Version Mismatch'
            except (ValueError, KeyError):
                bb.note("Invalid cache found, rebuilding...")
                self.depends_cache = {}

        if self.depends_cache:
            for fn in self.depends_cache.keys():
                self.clean[fn] = ""
                self.cacheValidUpdate(fn)

    def getVar(self, var, fn, exp = 0):
        """
        Gets the value of a variable
        (similar to getVar in the data class)
        
        There are two scenarios:
          1. We have cached data - serve from depends_cache[fn]
          2. We're learning what data to cache - serve from data 
             backend but add a copy of the data to the cache.
        """

        if fn in self.clean:
            return self.depends_cache[fn][var]

        if not fn in self.depends_cache:
            self.depends_cache[fn] = {}

        if fn != self.data_fn:
            # We're trying to access data in the cache which doesn't exist
            # yet setData hasn't been called to setup the right access. Very bad.
            bb.error("Parsing error data_fn %s and fn %s don't match" % (self.data_fn, fn))

        result = bb.data.getVar(var, self.data, exp)
        self.depends_cache[fn][var] = result
        return result

    def setData(self, fn, data):
        """
        Called to prime bb_cache ready to learn which variables to cache.
        Will be followed by calls to self.getVar which aren't cached
        but can be fulfilled from self.data.
        """
        self.data_fn = fn
        self.data = data

        # Make sure __depends makes the depends_cache
        self.getVar("__depends", fn, True)
        self.depends_cache[fn]["CACHETIMESTAMP"] = bb.parse.cached_mtime(fn)

    def loadDataFull(self, fn, cooker):
        """
        Return a complete set of data for fn.
        To do this, we need to parse the file.
        """
        bb_data, skipped = self.load_bbfile(fn, cooker)
        return bb_data

    def loadData(self, fn, cooker):
        """
        Load a subset of data for fn.
        If the cached data is valid we do nothing,
        To do this, we need to parse the file and set the system
        to record the variables accessed.
        Return the cache status and whether the file was skipped when parsed
        """
        if self.cacheValid(fn):
            if "SKIPPED" in self.depends_cache[fn]:
                return True, True
            return True, False

        bb_data, skipped = self.load_bbfile(fn, cooker)
        self.setData(fn, bb_data)
        return False, skipped

    def cacheValid(self, fn):
        """
        Is the cache valid for fn?
        Fast version, no timestamps checked.
        """
        # Is cache enabled?
        if not self.has_cache:
            return False
        if fn in self.clean:
            return True
        return False

    def cacheValidUpdate(self, fn):
        """
        Is the cache valid for fn?
        Make thorough (slower) checks including timestamps.
        """
        # Is cache enabled?
        if not self.has_cache:
            return False

        # Check file still exists
        if self.mtime(fn) == 0:
            bb.debug(2, "Cache: %s not longer exists" % fn)
            self.remove(fn)
            return False

        # File isn't in depends_cache
        if not fn in self.depends_cache:
            bb.debug(2, "Cache: %s is not cached" % fn)
            self.remove(fn)
            return False

        # Check the file's timestamp
        if bb.parse.cached_mtime(fn) > self.getVar("CACHETIMESTAMP", fn, True):
            bb.debug(2, "Cache: %s changed" % fn)
            self.remove(fn)
            return False

        # Check dependencies are still valid
        depends = self.getVar("__depends", fn, True)
        for f,old_mtime in depends:
            new_mtime = bb.parse.cached_mtime(f)
            if (new_mtime > old_mtime):
                bb.debug(2, "Cache: %s's dependency %s changed" % (fn, f))
                self.remove(fn)
                return False

        bb.debug(2, "Depends Cache: %s is clean" % fn)
        if not fn in self.clean:
            self.clean[fn] = ""

        return True

    def skip(self, fn):
        """
        Mark a fn as skipped
        Called from the parser
        """
        if not fn in self.depends_cache:
            self.depends_cache[fn] = {}
        self.depends_cache[fn]["SKIPPED"] = "1"

    def remove(self, fn):
        """
        Remove a fn from the cache
        Called from the parser in error cases
        """
        bb.debug(1, "Removing %s from cache" % fn)
        if fn in self.depends_cache:
            del self.depends_cache[fn]
        if fn in self.clean:
            del self.clean[fn]

    def sync(self):
        """
        Save the cache
        Called from the parser when complete (or exitting)
        """

        if not self.has_cache:
            return

        version_data = {}
        version_data['CACHE_VER'] = __cache_version__
        version_data['BITBAKE_VER'] = bb.__version__

        p = pickle.Pickler(file(self.cachefile, "wb" ), -1 )
        p.dump([self.depends_cache, version_data])

    def mtime(self, cachefile):
        try:
            return os.stat(cachefile)[8]
        except OSError:
            return 0

    def load_bbfile( self, bbfile , cooker):
        """
        Load and parse one .bb build file
        Return the data and whether parsing resulted in the file being skipped
        """

        import bb
        from bb import utils, data, parse, debug, event, fatal

        topdir = data.getVar('TOPDIR', cooker.configuration.data)
        if not topdir:
            topdir = os.path.abspath(os.getcwd())
            # set topdir to here
            data.setVar('TOPDIR', topdir, cooker.configuration)
        bbfile = os.path.abspath(bbfile)
        bbfile_loc = os.path.abspath(os.path.dirname(bbfile))
        # expand tmpdir to include this topdir
        data.setVar('TMPDIR', data.getVar('TMPDIR', cooker.configuration.data, 1) or "", cooker.configuration.data)
        # set topdir to location of .bb file
        topdir = bbfile_loc
        #data.setVar('TOPDIR', topdir, cfg)
        # go there
        oldpath = os.path.abspath(os.getcwd())
        if self.mtime(topdir):
            os.chdir(topdir)
        bb_data = data.init_db(cooker.configuration.data)
        try:
            parse.handle(bbfile, bb_data) # read .bb data
            os.chdir(oldpath)
            return bb_data, False
        except bb.parse.SkipPackage:
            os.chdir(oldpath)
            return bb_data, True
        except:
            os.chdir(oldpath)
            raise

def init(cooker):
    """
    The Objective: Cache the minimum amount of data possible yet get to the 
    stage of building packages (i.e. tryBuild) without reparsing any .bb files.

    To do this, we intercept getVar calls and only cache the variables we see 
    being accessed. We rely on the cache getVar calls being made for all 
    variables bitbake might need to use to reach this stage. For each cached 
    file we need to track:

    * Its mtime
    * The mtimes of all its dependencies
    * Whether it caused a parse.SkipPackage exception

    Files causing parsing errors are evicted from the cache.

    """
    return Cache(cooker)