xref: /openbmc/openbmc/poky/bitbake/lib/bb/parse/ast.py (revision fc81e38a)
1# ex:ts=4:sw=4:sts=4:et
2# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
3"""
4 AbstractSyntaxTree classes for the Bitbake language
5"""
6
7# Copyright (C) 2003, 2004 Chris Larson
8# Copyright (C) 2003, 2004 Phil Blundell
9# Copyright (C) 2009 Holger Hans Peter Freyther
10#
11# This program is free software; you can redistribute it and/or modify
12# it under the terms of the GNU General Public License version 2 as
13# published by the Free Software Foundation.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License along
21# with this program; if not, write to the Free Software Foundation, Inc.,
22# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23
24
25import re
26import string
27import logging
28import bb
29import itertools
30from bb import methodpool
31from bb.parse import logger
32
33class StatementGroup(list):
34    def eval(self, data):
35        for statement in self:
36            statement.eval(data)
37
38class AstNode(object):
39    def __init__(self, filename, lineno):
40        self.filename = filename
41        self.lineno = lineno
42
43class IncludeNode(AstNode):
44    def __init__(self, filename, lineno, what_file, force):
45        AstNode.__init__(self, filename, lineno)
46        self.what_file = what_file
47        self.force = force
48
49    def eval(self, data):
50        """
51        Include the file and evaluate the statements
52        """
53        s = data.expand(self.what_file)
54        logger.debug(2, "CONF %s:%s: including %s", self.filename, self.lineno, s)
55
56        # TODO: Cache those includes... maybe not here though
57        if self.force:
58            bb.parse.ConfHandler.include(self.filename, s, self.lineno, data, "include required")
59        else:
60            bb.parse.ConfHandler.include(self.filename, s, self.lineno, data, False)
61
62class ExportNode(AstNode):
63    def __init__(self, filename, lineno, var):
64        AstNode.__init__(self, filename, lineno)
65        self.var = var
66
67    def eval(self, data):
68        data.setVarFlag(self.var, "export", 1, op = 'exported')
69
70class UnsetNode(AstNode):
71    def __init__(self, filename, lineno, var):
72        AstNode.__init__(self, filename, lineno)
73        self.var = var
74
75    def eval(self, data):
76        loginfo = {
77            'variable': self.var,
78            'file': self.filename,
79            'line': self.lineno,
80        }
81        data.delVar(self.var,**loginfo)
82
83class UnsetFlagNode(AstNode):
84    def __init__(self, filename, lineno, var, flag):
85        AstNode.__init__(self, filename, lineno)
86        self.var = var
87        self.flag = flag
88
89    def eval(self, data):
90        loginfo = {
91            'variable': self.var,
92            'file': self.filename,
93            'line': self.lineno,
94        }
95        data.delVarFlag(self.var, self.flag, **loginfo)
96
97class DataNode(AstNode):
98    """
99    Various data related updates. For the sake of sanity
100    we have one class doing all this. This means that all
101    this need to be re-evaluated... we might be able to do
102    that faster with multiple classes.
103    """
104    def __init__(self, filename, lineno, groupd):
105        AstNode.__init__(self, filename, lineno)
106        self.groupd = groupd
107
108    def getFunc(self, key, data):
109        if 'flag' in self.groupd and self.groupd['flag'] != None:
110            return data.getVarFlag(key, self.groupd['flag'], expand=False, noweakdefault=True)
111        else:
112            return data.getVar(key, False, noweakdefault=True, parsing=True)
113
114    def eval(self, data):
115        groupd = self.groupd
116        key = groupd["var"]
117        loginfo = {
118            'variable': key,
119            'file': self.filename,
120            'line': self.lineno,
121        }
122        if "exp" in groupd and groupd["exp"] != None:
123            data.setVarFlag(key, "export", 1, op = 'exported', **loginfo)
124
125        op = "set"
126        if "ques" in groupd and groupd["ques"] != None:
127            val = self.getFunc(key, data)
128            op = "set?"
129            if val == None:
130                val = groupd["value"]
131        elif "colon" in groupd and groupd["colon"] != None:
132            e = data.createCopy()
133            op = "immediate"
134            val = e.expand(groupd["value"], key + "[:=]")
135        elif "append" in groupd and groupd["append"] != None:
136            op = "append"
137            val = "%s %s" % ((self.getFunc(key, data) or ""), groupd["value"])
138        elif "prepend" in groupd and groupd["prepend"] != None:
139            op = "prepend"
140            val = "%s %s" % (groupd["value"], (self.getFunc(key, data) or ""))
141        elif "postdot" in groupd and groupd["postdot"] != None:
142            op = "postdot"
143            val = "%s%s" % ((self.getFunc(key, data) or ""), groupd["value"])
144        elif "predot" in groupd and groupd["predot"] != None:
145            op = "predot"
146            val = "%s%s" % (groupd["value"], (self.getFunc(key, data) or ""))
147        else:
148            val = groupd["value"]
149
150        flag = None
151        if 'flag' in groupd and groupd['flag'] != None:
152            flag = groupd['flag']
153        elif groupd["lazyques"]:
154            flag = "_defaultval"
155
156        loginfo['op'] = op
157        loginfo['detail'] = groupd["value"]
158
159        if flag:
160            data.setVarFlag(key, flag, val, **loginfo)
161        else:
162            data.setVar(key, val, parsing=True, **loginfo)
163
164class MethodNode(AstNode):
165    tr_tbl = str.maketrans('/.+-@%&', '_______')
166
167    def __init__(self, filename, lineno, func_name, body, python, fakeroot):
168        AstNode.__init__(self, filename, lineno)
169        self.func_name = func_name
170        self.body = body
171        self.python = python
172        self.fakeroot = fakeroot
173
174    def eval(self, data):
175        text = '\n'.join(self.body)
176        funcname = self.func_name
177        if self.func_name == "__anonymous":
178            funcname = ("__anon_%s_%s" % (self.lineno, self.filename.translate(MethodNode.tr_tbl)))
179            self.python = True
180            text = "def %s(d):\n" % (funcname) + text
181            bb.methodpool.insert_method(funcname, text, self.filename, self.lineno - len(self.body) - 1)
182            anonfuncs = data.getVar('__BBANONFUNCS', False) or []
183            anonfuncs.append(funcname)
184            data.setVar('__BBANONFUNCS', anonfuncs)
185        if data.getVar(funcname, False):
186            # clean up old version of this piece of metadata, as its
187            # flags could cause problems
188            data.delVarFlag(funcname, 'python')
189            data.delVarFlag(funcname, 'fakeroot')
190        if self.python:
191            data.setVarFlag(funcname, "python", "1")
192        if self.fakeroot:
193            data.setVarFlag(funcname, "fakeroot", "1")
194        data.setVarFlag(funcname, "func", 1)
195        data.setVar(funcname, text, parsing=True)
196        data.setVarFlag(funcname, 'filename', self.filename)
197        data.setVarFlag(funcname, 'lineno', str(self.lineno - len(self.body)))
198
199class PythonMethodNode(AstNode):
200    def __init__(self, filename, lineno, function, modulename, body):
201        AstNode.__init__(self, filename, lineno)
202        self.function = function
203        self.modulename = modulename
204        self.body = body
205
206    def eval(self, data):
207        # Note we will add root to parsedmethods after having parse
208        # 'this' file. This means we will not parse methods from
209        # bb classes twice
210        text = '\n'.join(self.body)
211        bb.methodpool.insert_method(self.modulename, text, self.filename, self.lineno - len(self.body) - 1)
212        data.setVarFlag(self.function, "func", 1)
213        data.setVarFlag(self.function, "python", 1)
214        data.setVar(self.function, text, parsing=True)
215        data.setVarFlag(self.function, 'filename', self.filename)
216        data.setVarFlag(self.function, 'lineno', str(self.lineno - len(self.body) - 1))
217
218class ExportFuncsNode(AstNode):
219    def __init__(self, filename, lineno, fns, classname):
220        AstNode.__init__(self, filename, lineno)
221        self.n = fns.split()
222        self.classname = classname
223
224    def eval(self, data):
225
226        for func in self.n:
227            calledfunc = self.classname + "_" + func
228
229            if data.getVar(func, False) and not data.getVarFlag(func, 'export_func', False):
230                continue
231
232            if data.getVar(func, False):
233                data.setVarFlag(func, 'python', None)
234                data.setVarFlag(func, 'func', None)
235
236            for flag in [ "func", "python" ]:
237                if data.getVarFlag(calledfunc, flag, False):
238                    data.setVarFlag(func, flag, data.getVarFlag(calledfunc, flag, False))
239            for flag in [ "dirs" ]:
240                if data.getVarFlag(func, flag, False):
241                    data.setVarFlag(calledfunc, flag, data.getVarFlag(func, flag, False))
242            data.setVarFlag(func, "filename", "autogenerated")
243            data.setVarFlag(func, "lineno", 1)
244
245            if data.getVarFlag(calledfunc, "python", False):
246                data.setVar(func, "    bb.build.exec_func('" + calledfunc + "', d)\n", parsing=True)
247            else:
248                if "-" in self.classname:
249                   bb.fatal("The classname %s contains a dash character and is calling an sh function %s using EXPORT_FUNCTIONS. Since a dash is illegal in sh function names, this cannot work, please rename the class or don't use EXPORT_FUNCTIONS." % (self.classname, calledfunc))
250                data.setVar(func, "    " + calledfunc + "\n", parsing=True)
251            data.setVarFlag(func, 'export_func', '1')
252
253class AddTaskNode(AstNode):
254    def __init__(self, filename, lineno, func, before, after):
255        AstNode.__init__(self, filename, lineno)
256        self.func = func
257        self.before = before
258        self.after = after
259
260    def eval(self, data):
261        bb.build.addtask(self.func, self.before, self.after, data)
262
263class DelTaskNode(AstNode):
264    def __init__(self, filename, lineno, func):
265        AstNode.__init__(self, filename, lineno)
266        self.func = func
267
268    def eval(self, data):
269        bb.build.deltask(self.func, data)
270
271class BBHandlerNode(AstNode):
272    def __init__(self, filename, lineno, fns):
273        AstNode.__init__(self, filename, lineno)
274        self.hs = fns.split()
275
276    def eval(self, data):
277        bbhands = data.getVar('__BBHANDLERS', False) or []
278        for h in self.hs:
279            bbhands.append(h)
280            data.setVarFlag(h, "handler", 1)
281        data.setVar('__BBHANDLERS', bbhands)
282
283class InheritNode(AstNode):
284    def __init__(self, filename, lineno, classes):
285        AstNode.__init__(self, filename, lineno)
286        self.classes = classes
287
288    def eval(self, data):
289        bb.parse.BBHandler.inherit(self.classes, self.filename, self.lineno, data)
290
291def handleInclude(statements, filename, lineno, m, force):
292    statements.append(IncludeNode(filename, lineno, m.group(1), force))
293
294def handleExport(statements, filename, lineno, m):
295    statements.append(ExportNode(filename, lineno, m.group(1)))
296
297def handleUnset(statements, filename, lineno, m):
298    statements.append(UnsetNode(filename, lineno, m.group(1)))
299
300def handleUnsetFlag(statements, filename, lineno, m):
301    statements.append(UnsetFlagNode(filename, lineno, m.group(1), m.group(2)))
302
303def handleData(statements, filename, lineno, groupd):
304    statements.append(DataNode(filename, lineno, groupd))
305
306def handleMethod(statements, filename, lineno, func_name, body, python, fakeroot):
307    statements.append(MethodNode(filename, lineno, func_name, body, python, fakeroot))
308
309def handlePythonMethod(statements, filename, lineno, funcname, modulename, body):
310    statements.append(PythonMethodNode(filename, lineno, funcname, modulename, body))
311
312def handleExportFuncs(statements, filename, lineno, m, classname):
313    statements.append(ExportFuncsNode(filename, lineno, m.group(1), classname))
314
315def handleAddTask(statements, filename, lineno, m):
316    func = m.group("func")
317    before = m.group("before")
318    after = m.group("after")
319    if func is None:
320        return
321
322    statements.append(AddTaskNode(filename, lineno, func, before, after))
323
324def handleDelTask(statements, filename, lineno, m):
325    func = m.group("func")
326    if func is None:
327        return
328
329    statements.append(DelTaskNode(filename, lineno, func))
330
331def handleBBHandlers(statements, filename, lineno, m):
332    statements.append(BBHandlerNode(filename, lineno, m.group(1)))
333
334def handleInherit(statements, filename, lineno, m):
335    classes = m.group(1)
336    statements.append(InheritNode(filename, lineno, classes))
337
338def runAnonFuncs(d):
339    code = []
340    for funcname in d.getVar("__BBANONFUNCS", False) or []:
341        code.append("%s(d)" % funcname)
342    bb.utils.better_exec("\n".join(code), {"d": d})
343
344def finalize(fn, d, variant = None):
345    saved_handlers = bb.event.get_handlers().copy()
346    try:
347        for var in d.getVar('__BBHANDLERS', False) or []:
348            # try to add the handler
349            handlerfn = d.getVarFlag(var, "filename", False)
350            if not handlerfn:
351                bb.fatal("Undefined event handler function '%s'" % var)
352            handlerln = int(d.getVarFlag(var, "lineno", False))
353            bb.event.register(var, d.getVar(var, False), (d.getVarFlag(var, "eventmask") or "").split(), handlerfn, handlerln)
354
355        bb.event.fire(bb.event.RecipePreFinalise(fn), d)
356
357        bb.data.expandKeys(d)
358        runAnonFuncs(d)
359
360        tasklist = d.getVar('__BBTASKS', False) or []
361        bb.event.fire(bb.event.RecipeTaskPreProcess(fn, list(tasklist)), d)
362        bb.build.add_tasks(tasklist, d)
363
364        bb.parse.siggen.finalise(fn, d, variant)
365
366        d.setVar('BBINCLUDED', bb.parse.get_file_depends(d))
367
368        bb.event.fire(bb.event.RecipeParsed(fn), d)
369    finally:
370        bb.event.set_handlers(saved_handlers)
371
372def _create_variants(datastores, names, function, onlyfinalise):
373    def create_variant(name, orig_d, arg = None):
374        if onlyfinalise and name not in onlyfinalise:
375            return
376        new_d = bb.data.createCopy(orig_d)
377        function(arg or name, new_d)
378        datastores[name] = new_d
379
380    for variant in list(datastores.keys()):
381        for name in names:
382            if not variant:
383                # Based on main recipe
384                create_variant(name, datastores[""])
385            else:
386                create_variant("%s-%s" % (variant, name), datastores[variant], name)
387
388def multi_finalize(fn, d):
389    appends = (d.getVar("__BBAPPEND") or "").split()
390    for append in appends:
391        logger.debug(1, "Appending .bbappend file %s to %s", append, fn)
392        bb.parse.BBHandler.handle(append, d, True)
393
394    onlyfinalise = d.getVar("__ONLYFINALISE", False)
395
396    safe_d = d
397    d = bb.data.createCopy(safe_d)
398    try:
399        finalize(fn, d)
400    except bb.parse.SkipRecipe as e:
401        d.setVar("__SKIPPED", e.args[0])
402    datastores = {"": safe_d}
403
404    extended = d.getVar("BBCLASSEXTEND") or ""
405    if extended:
406        # the following is to support bbextends with arguments, for e.g. multilib
407        # an example is as follows:
408        #   BBCLASSEXTEND = "multilib:lib32"
409        # it will create foo-lib32, inheriting multilib.bbclass and set
410        # BBEXTENDCURR to "multilib" and BBEXTENDVARIANT to "lib32"
411        extendedmap = {}
412        variantmap = {}
413
414        for ext in extended.split():
415            eext = ext.split(':', 2)
416            if len(eext) > 1:
417                extendedmap[ext] = eext[0]
418                variantmap[ext] = eext[1]
419            else:
420                extendedmap[ext] = ext
421
422        pn = d.getVar("PN")
423        def extendfunc(name, d):
424            if name != extendedmap[name]:
425                d.setVar("BBEXTENDCURR", extendedmap[name])
426                d.setVar("BBEXTENDVARIANT", variantmap[name])
427            else:
428                d.setVar("PN", "%s-%s" % (pn, name))
429            bb.parse.BBHandler.inherit(extendedmap[name], fn, 0, d)
430
431        safe_d.setVar("BBCLASSEXTEND", extended)
432        _create_variants(datastores, extendedmap.keys(), extendfunc, onlyfinalise)
433
434    for variant in datastores.keys():
435        if variant:
436            try:
437                if not onlyfinalise or variant in onlyfinalise:
438                    finalize(fn, datastores[variant], variant)
439            except bb.parse.SkipRecipe as e:
440                datastores[variant].setVar("__SKIPPED", e.args[0])
441
442    datastores[""] = d
443    return datastores
444