summaryrefslogtreecommitdiffstats
path: root/bitbake/lib/bb/parse/ast.py
blob: f587c6cbd66075185d824f7dbea46af71522b1fb (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
# ex:ts=4:sw=4:sts=4:et
# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
"""
 AbstractSyntaxTree classes for the Bitbake language
"""

# Copyright (C) 2003, 2004 Chris Larson
# Copyright (C) 2003, 2004 Phil Blundell
# Copyright (C) 2009 Holger Hans Peter Freyther
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# 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.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

import bb, re, string

__word__ = re.compile(r"\S+")
__parsed_methods__ = bb.methodpool.get_parsed_dict()

class StatementGroup:
    def __init__(self):
        self.statements = []

    def append(self, statement):
        self.statements.append(statement)

    def eval(self, data):
        """
        Apply each statement on the data... in order
        """
        map(lambda x: x.eval(data), self.statements)

    def __getitem__(self, item):
        return self.statements.__getitem__(item)

class IncludeNode:
    def __init__(self, what_file, fn, lineno, force):
        self.what_file = what_file
        self.from_fn = fn
        self.from_lineno = lineno
        self.force = force

    def eval(self, data):
        """
        Include the file and evaluate the statements
        """
        s = bb.data.expand(self.what_file, data)
        bb.msg.debug(3, bb.msg.domain.Parsing, "CONF %s:%d: including %s" % (self.from_fn, self.from_lineno, s))

        # TODO: Cache those includes...
        statements = StatementGroup()
        if force:
            bb.parse.ConfHandler.include(statements, self.from_fn, s, data, "include required")
        else:
            bb.parse.ConfHandler.include(statements, self.from_fn, s, data, False)
        statements.eval(data)

class ExportNode:
    def __init__(self, var):
        self.var = var

    def eval(self, data):
        bb.data.setVarFlag(self.var, "export", 1, data)

class DataNode:
    """
    Various data related updates. For the sake of sanity
    we have one class doing all this. This means that all
    this need to be re-evaluated... we might be able to do
    that faster with multiple classes.
    """
    def __init__(self, groupd):
        self.groupd = groupd

    def getFunc(self, key, data):
        if 'flag' in self.groupd and self.groupd['flag'] != None:
            return bb.data.getVarFlag(key, self.groupd['flag'], data)
        else:
            return bb.data.getVar(key, data)

    def eval(self, data):
        groupd = self.groupd
        key = groupd["var"]
        if "exp" in groupd and groupd["exp"] != None:
            bb.data.setVarFlag(key, "export", 1, data)
        if "ques" in groupd and groupd["ques"] != None:
            val = self.getFunc(key, data)
            if val == None:
                val = groupd["value"]
        elif "colon" in groupd and groupd["colon"] != None:
            e = data.createCopy()
            bb.data.update_data(e)
            val = bb.data.expand(groupd["value"], e)
        elif "append" in groupd and groupd["append"] != None:
            val = "%s %s" % ((self.getFunc(key, data) or ""), groupd["value"])
        elif "prepend" in groupd and groupd["prepend"] != None:
            val = "%s %s" % (groupd["value"], (self.getFunc(key, data) or ""))
        elif "postdot" in groupd and groupd["postdot"] != None:
            val = "%s%s" % ((self.getFunc(key, data) or ""), groupd["value"])
        elif "predot" in groupd and groupd["predot"] != None:
            val = "%s%s" % (groupd["value"], (self.getFunc(key, data) or ""))
        else:
            val = groupd["value"]
        if 'flag' in groupd and groupd['flag'] != None:
            bb.msg.debug(3, bb.msg.domain.Parsing, "setVarFlag(%s, %s, %s, data)" % (key, groupd['flag'], val))
            bb.data.setVarFlag(key, groupd['flag'], val, data)
        else:
            bb.data.setVar(key, val, data)

        
def handleInclude(statements, m, fn, lineno, data, force):
    # AST handling
    statements.append(IncludeNode(m.group(1), fn, lineno, force))

    s = bb.data.expand(m.group(1), data)
    bb.msg.debug(3, bb.msg.domain.Parsing, "CONF %s:%d: including %s" % (fn, lineno, s))
    if force:
        bb.parse.ConfHandler.include(statements, fn, s, data, "include required")
    else:
        bb.parse.ConfHandler.include(statements, fn, s, data, False)

def handleExport(statements, m, data):
    # AST handling
    statements.append(ExportNode(m.group(1)))
    statements[-1].eval(data)

def handleData(statements, groupd, data):
    # AST handling
    statements.append(DataNode(groupd))
    statements[-1].eval(data)

def handleMethod(statements, func_name, lineno, fn, body, d):
    if func_name == "__anonymous":
        funcname = ("__anon_%s_%s" % (lineno, fn.translate(string.maketrans('/.+-', '____'))))
        if not funcname in bb.methodpool._parsed_fns:
            text = "def %s(d):\n" % (funcname) + '\n'.join(body)
            bb.methodpool.insert_method(funcname, text, fn)
        anonfuncs = bb.data.getVar('__BBANONFUNCS', d) or []
        anonfuncs.append(funcname)
        bb.data.setVar('__BBANONFUNCS', anonfuncs, d)
    else:
        bb.data.setVarFlag(func_name, "func", 1, d)
        bb.data.setVar(func_name, '\n'.join(body), d)

def handlePythonMethod(statements, root, body, fn):
    # Note we will add root to parsedmethods after having parse
    # 'this' file. This means we will not parse methods from
    # bb classes twice
    if not root in __parsed_methods__:
        text = '\n'.join(body)
        bb.methodpool.insert_method(root, text, fn)

def handleMethodFlags(statements, key, m, d):
    if bb.data.getVar(key, d):
        # Clean up old version of this piece of metadata, as its
        # flags could cause problems
        bb.data.setVarFlag(key, 'python', None, d)
        bb.data.setVarFlag(key, 'fakeroot', None, d)
    if m.group("py") is not None:
        bb.data.setVarFlag(key, "python", "1", d)
    else:
        bb.data.delVarFlag(key, "python", d)
    if m.group("fr") is not None:
        bb.data.setVarFlag(key, "fakeroot", "1", d)
    else:
        bb.data.delVarFlag(key, "fakeroot", d)

def handleExportFuncs(statements, m, classes, d):
    fns = m.group(1)
    n = __word__.findall(fns)
    for f in n:
        allvars = []
        allvars.append(f)
        allvars.append(classes[-1] + "_" + f)

        vars = [[ allvars[0], allvars[1] ]]
        if len(classes) > 1 and classes[-2] is not None:
            allvars.append(classes[-2] + "_" + f)
            vars = []
            vars.append([allvars[2], allvars[1]])
            vars.append([allvars[0], allvars[2]])

        for (var, calledvar) in vars:
            if bb.data.getVar(var, d) and not bb.data.getVarFlag(var, 'export_func', d):
                continue

            if bb.data.getVar(var, d):
                bb.data.setVarFlag(var, 'python', None, d)
                bb.data.setVarFlag(var, 'func', None, d)

            for flag in [ "func", "python" ]:
                if bb.data.getVarFlag(calledvar, flag, d):
                    bb.data.setVarFlag(var, flag, bb.data.getVarFlag(calledvar, flag, d), d)
            for flag in [ "dirs" ]:
                if bb.data.getVarFlag(var, flag, d):
                    bb.data.setVarFlag(calledvar, flag, bb.data.getVarFlag(var, flag, d), d)

            if bb.data.getVarFlag(calledvar, "python", d):
                bb.data.setVar(var, "\tbb.build.exec_func('" + calledvar + "', d)\n", d)
            else:
                bb.data.setVar(var, "\t" + calledvar + "\n", d)
            bb.data.setVarFlag(var, 'export_func', '1', d)

def handleAddTask(statements, m, d):
    func = m.group("func")
    before = m.group("before")
    after = m.group("after")
    if func is None:
        return
    if func[:3] != "do_":
        var = "do_" + func

    bb.data.setVarFlag(var, "task", 1, d)

    bbtasks = bb.data.getVar('__BBTASKS', d) or []
    if not var in bbtasks:
        bbtasks.append(var)
    bb.data.setVar('__BBTASKS', bbtasks, d)

    existing = bb.data.getVarFlag(var, "deps", d) or []
    if after is not None:
        # set up deps for function
        for entry in after.split():
            if entry not in existing:
                existing.append(entry)
    bb.data.setVarFlag(var, "deps", existing, d)
    if before is not None:
        # set up things that depend on this func
        for entry in before.split():
            existing = bb.data.getVarFlag(entry, "deps", d) or []
            if var not in existing:
                bb.data.setVarFlag(entry, "deps", [var] + existing, d)

def handleBBHandlers(statements, m, d):
    fns = m.group(1)
    hs = __word__.findall(fns)
    bbhands = bb.data.getVar('__BBHANDLERS', d) or []
    for h in hs:
        bbhands.append(h)
        bb.data.setVarFlag(h, "handler", 1, d)
    bb.data.setVar('__BBHANDLERS', bbhands, d)

def handleInherit(statements, m, d):
    files = m.group(1)
    n = __word__.findall(files)
    bb.parse.BBHandler.inherit(statements, n, d)

def finalise(fn, d):
    bb.data.expandKeys(d)
    bb.data.update_data(d)
    anonqueue = bb.data.getVar("__anonqueue", d, 1) or []
    body = [x['content'] for x in anonqueue]
    flag = { 'python' : 1, 'func' : 1 }
    bb.data.setVar("__anonfunc", "\n".join(body), d)
    bb.data.setVarFlags("__anonfunc", flag, d)
    from bb import build
    try:
        t = bb.data.getVar('T', d)
        bb.data.setVar('T', '${TMPDIR}/anonfunc/', d)
        anonfuncs = bb.data.getVar('__BBANONFUNCS', d) or []
        code = ""
        for f in anonfuncs:
            code = code + "    %s(d)\n" % f
        bb.data.setVar("__anonfunc", code, d)        
        build.exec_func("__anonfunc", d)
        bb.data.delVar('T', d)
        if t:
            bb.data.setVar('T', t, d)
    except Exception, e:
        bb.msg.debug(1, bb.msg.domain.Parsing, "Exception when executing anonymous function: %s" % e)
        raise
    bb.data.delVar("__anonqueue", d)
    bb.data.delVar("__anonfunc", d)
    bb.data.update_data(d)

    all_handlers = {} 
    for var in bb.data.getVar('__BBHANDLERS', d) or []:
        # try to add the handler
        handler = bb.data.getVar(var,d)
        bb.event.register(var, handler)

    tasklist = bb.data.getVar('__BBTASKS', d) or []
    bb.build.add_tasks(tasklist, d)

    bb.event.fire(bb.event.RecipeParsed(fn), d)