summaryrefslogtreecommitdiffstats
path: root/bitbake/lib/bb/parse/ast.py
diff options
context:
space:
mode:
Diffstat (limited to 'bitbake/lib/bb/parse/ast.py')
-rw-r--r--bitbake/lib/bb/parse/ast.py478
1 files changed, 478 insertions, 0 deletions
diff --git a/bitbake/lib/bb/parse/ast.py b/bitbake/lib/bb/parse/ast.py
new file mode 100644
index 0000000..d8c141b
--- /dev/null
+++ b/bitbake/lib/bb/parse/ast.py
@@ -0,0 +1,478 @@
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
24from __future__ import absolute_import
25from future_builtins import filter
26import re
27import string
28import logging
29import bb
30import itertools
31from bb import methodpool
32from bb.parse import logger
33
34_bbversions_re = re.compile(r"\[(?P<from>[0-9]+)-(?P<to>[0-9]+)\]")
35
36class StatementGroup(list):
37 def eval(self, data):
38 for statement in self:
39 statement.eval(data)
40
41class AstNode(object):
42 def __init__(self, filename, lineno):
43 self.filename = filename
44 self.lineno = lineno
45
46class IncludeNode(AstNode):
47 def __init__(self, filename, lineno, what_file, force):
48 AstNode.__init__(self, filename, lineno)
49 self.what_file = what_file
50 self.force = force
51
52 def eval(self, data):
53 """
54 Include the file and evaluate the statements
55 """
56 s = data.expand(self.what_file)
57 logger.debug(2, "CONF %s:%s: including %s", self.filename, self.lineno, s)
58
59 # TODO: Cache those includes... maybe not here though
60 if self.force:
61 bb.parse.ConfHandler.include(self.filename, s, self.lineno, data, "include required")
62 else:
63 bb.parse.ConfHandler.include(self.filename, s, self.lineno, data, False)
64
65class ExportNode(AstNode):
66 def __init__(self, filename, lineno, var):
67 AstNode.__init__(self, filename, lineno)
68 self.var = var
69
70 def eval(self, data):
71 data.setVarFlag(self.var, "export", 1, op = 'exported')
72
73class DataNode(AstNode):
74 """
75 Various data related updates. For the sake of sanity
76 we have one class doing all this. This means that all
77 this need to be re-evaluated... we might be able to do
78 that faster with multiple classes.
79 """
80 def __init__(self, filename, lineno, groupd):
81 AstNode.__init__(self, filename, lineno)
82 self.groupd = groupd
83
84 def getFunc(self, key, data):
85 if 'flag' in self.groupd and self.groupd['flag'] != None:
86 return data.getVarFlag(key, self.groupd['flag'], noweakdefault=True)
87 else:
88 return data.getVar(key, noweakdefault=True)
89
90 def eval(self, data):
91 groupd = self.groupd
92 key = groupd["var"]
93 loginfo = {
94 'variable': key,
95 'file': self.filename,
96 'line': self.lineno,
97 }
98 if "exp" in groupd and groupd["exp"] != None:
99 data.setVarFlag(key, "export", 1, op = 'exported', **loginfo)
100
101 op = "set"
102 if "ques" in groupd and groupd["ques"] != None:
103 val = self.getFunc(key, data)
104 op = "set?"
105 if val == None:
106 val = groupd["value"]
107 elif "colon" in groupd and groupd["colon"] != None:
108 e = data.createCopy()
109 bb.data.update_data(e)
110 op = "immediate"
111 val = e.expand(groupd["value"], key + "[:=]")
112 elif "append" in groupd and groupd["append"] != None:
113 op = "append"
114 val = "%s %s" % ((self.getFunc(key, data) or ""), groupd["value"])
115 elif "prepend" in groupd and groupd["prepend"] != None:
116 op = "prepend"
117 val = "%s %s" % (groupd["value"], (self.getFunc(key, data) or ""))
118 elif "postdot" in groupd and groupd["postdot"] != None:
119 op = "postdot"
120 val = "%s%s" % ((self.getFunc(key, data) or ""), groupd["value"])
121 elif "predot" in groupd and groupd["predot"] != None:
122 op = "predot"
123 val = "%s%s" % (groupd["value"], (self.getFunc(key, data) or ""))
124 else:
125 val = groupd["value"]
126
127 flag = None
128 if 'flag' in groupd and groupd['flag'] != None:
129 flag = groupd['flag']
130 elif groupd["lazyques"]:
131 flag = "defaultval"
132
133 loginfo['op'] = op
134 loginfo['detail'] = groupd["value"]
135
136 if flag:
137 data.setVarFlag(key, flag, val, **loginfo)
138 else:
139 data.setVar(key, val, **loginfo)
140
141class MethodNode(AstNode):
142 tr_tbl = string.maketrans('/.+-@%', '______')
143
144 def __init__(self, filename, lineno, func_name, body):
145 AstNode.__init__(self, filename, lineno)
146 self.func_name = func_name
147 self.body = body
148
149 def eval(self, data):
150 text = '\n'.join(self.body)
151 if self.func_name == "__anonymous":
152 funcname = ("__anon_%s_%s" % (self.lineno, self.filename.translate(MethodNode.tr_tbl)))
153 text = "def %s(d):\n" % (funcname) + text
154 bb.methodpool.insert_method(funcname, text, self.filename)
155 anonfuncs = data.getVar('__BBANONFUNCS') or []
156 anonfuncs.append(funcname)
157 data.setVar('__BBANONFUNCS', anonfuncs)
158 data.setVar(funcname, text)
159 else:
160 data.setVarFlag(self.func_name, "func", 1)
161 data.setVar(self.func_name, text)
162
163class PythonMethodNode(AstNode):
164 def __init__(self, filename, lineno, function, modulename, body):
165 AstNode.__init__(self, filename, lineno)
166 self.function = function
167 self.modulename = modulename
168 self.body = body
169
170 def eval(self, data):
171 # Note we will add root to parsedmethods after having parse
172 # 'this' file. This means we will not parse methods from
173 # bb classes twice
174 text = '\n'.join(self.body)
175 bb.methodpool.insert_method(self.modulename, text, self.filename)
176 data.setVarFlag(self.function, "func", 1)
177 data.setVarFlag(self.function, "python", 1)
178 data.setVar(self.function, text)
179
180class MethodFlagsNode(AstNode):
181 def __init__(self, filename, lineno, key, m):
182 AstNode.__init__(self, filename, lineno)
183 self.key = key
184 self.m = m
185
186 def eval(self, data):
187 if data.getVar(self.key):
188 # clean up old version of this piece of metadata, as its
189 # flags could cause problems
190 data.setVarFlag(self.key, 'python', None)
191 data.setVarFlag(self.key, 'fakeroot', None)
192 if self.m.group("py") is not None:
193 data.setVarFlag(self.key, "python", "1")
194 else:
195 data.delVarFlag(self.key, "python")
196 if self.m.group("fr") is not None:
197 data.setVarFlag(self.key, "fakeroot", "1")
198 else:
199 data.delVarFlag(self.key, "fakeroot")
200
201class ExportFuncsNode(AstNode):
202 def __init__(self, filename, lineno, fns, classname):
203 AstNode.__init__(self, filename, lineno)
204 self.n = fns.split()
205 self.classname = classname
206
207 def eval(self, data):
208
209 for func in self.n:
210 calledfunc = self.classname + "_" + func
211
212 if data.getVar(func) and not data.getVarFlag(func, 'export_func'):
213 continue
214
215 if data.getVar(func):
216 data.setVarFlag(func, 'python', None)
217 data.setVarFlag(func, 'func', None)
218
219 for flag in [ "func", "python" ]:
220 if data.getVarFlag(calledfunc, flag):
221 data.setVarFlag(func, flag, data.getVarFlag(calledfunc, flag))
222 for flag in [ "dirs" ]:
223 if data.getVarFlag(func, flag):
224 data.setVarFlag(calledfunc, flag, data.getVarFlag(func, flag))
225
226 if data.getVarFlag(calledfunc, "python"):
227 data.setVar(func, " bb.build.exec_func('" + calledfunc + "', d)\n")
228 else:
229 data.setVar(func, " " + calledfunc + "\n")
230 data.setVarFlag(func, 'export_func', '1')
231
232class AddTaskNode(AstNode):
233 def __init__(self, filename, lineno, func, before, after):
234 AstNode.__init__(self, filename, lineno)
235 self.func = func
236 self.before = before
237 self.after = after
238
239 def eval(self, data):
240 bb.build.addtask(self.func, self.before, self.after, data)
241
242class DelTaskNode(AstNode):
243 def __init__(self, filename, lineno, func):
244 AstNode.__init__(self, filename, lineno)
245 self.func = func
246
247 def eval(self, data):
248 bb.build.deltask(self.func, data)
249
250class BBHandlerNode(AstNode):
251 def __init__(self, filename, lineno, fns):
252 AstNode.__init__(self, filename, lineno)
253 self.hs = fns.split()
254
255 def eval(self, data):
256 bbhands = data.getVar('__BBHANDLERS') or []
257 for h in self.hs:
258 bbhands.append(h)
259 data.setVarFlag(h, "handler", 1)
260 data.setVar('__BBHANDLERS', bbhands)
261
262class InheritNode(AstNode):
263 def __init__(self, filename, lineno, classes):
264 AstNode.__init__(self, filename, lineno)
265 self.classes = classes
266
267 def eval(self, data):
268 bb.parse.BBHandler.inherit(self.classes, self.filename, self.lineno, data)
269
270def handleInclude(statements, filename, lineno, m, force):
271 statements.append(IncludeNode(filename, lineno, m.group(1), force))
272
273def handleExport(statements, filename, lineno, m):
274 statements.append(ExportNode(filename, lineno, m.group(1)))
275
276def handleData(statements, filename, lineno, groupd):
277 statements.append(DataNode(filename, lineno, groupd))
278
279def handleMethod(statements, filename, lineno, func_name, body):
280 statements.append(MethodNode(filename, lineno, func_name, body))
281
282def handlePythonMethod(statements, filename, lineno, funcname, modulename, body):
283 statements.append(PythonMethodNode(filename, lineno, funcname, modulename, body))
284
285def handleMethodFlags(statements, filename, lineno, key, m):
286 statements.append(MethodFlagsNode(filename, lineno, key, m))
287
288def handleExportFuncs(statements, filename, lineno, m, classname):
289 statements.append(ExportFuncsNode(filename, lineno, m.group(1), classname))
290
291def handleAddTask(statements, filename, lineno, m):
292 func = m.group("func")
293 before = m.group("before")
294 after = m.group("after")
295 if func is None:
296 return
297
298 statements.append(AddTaskNode(filename, lineno, func, before, after))
299
300def handleDelTask(statements, filename, lineno, m):
301 func = m.group("func")
302 if func is None:
303 return
304
305 statements.append(DelTaskNode(filename, lineno, func))
306
307def handleBBHandlers(statements, filename, lineno, m):
308 statements.append(BBHandlerNode(filename, lineno, m.group(1)))
309
310def handleInherit(statements, filename, lineno, m):
311 classes = m.group(1)
312 statements.append(InheritNode(filename, lineno, classes))
313
314def finalize(fn, d, variant = None):
315 all_handlers = {}
316 for var in d.getVar('__BBHANDLERS') or []:
317 # try to add the handler
318 bb.event.register(var, d.getVar(var), (d.getVarFlag(var, "eventmask", True) or "").split())
319
320 bb.event.fire(bb.event.RecipePreFinalise(fn), d)
321
322 bb.data.expandKeys(d)
323 bb.data.update_data(d)
324 code = []
325 for funcname in d.getVar("__BBANONFUNCS") or []:
326 code.append("%s(d)" % funcname)
327 bb.utils.better_exec("\n".join(code), {"d": d})
328 bb.data.update_data(d)
329
330 tasklist = d.getVar('__BBTASKS') or []
331 deltasklist = d.getVar('__BBDELTASKS') or []
332 bb.build.add_tasks(tasklist, deltasklist, d)
333
334 bb.parse.siggen.finalise(fn, d, variant)
335
336 d.setVar('BBINCLUDED', bb.parse.get_file_depends(d))
337
338 bb.event.fire(bb.event.RecipeParsed(fn), d)
339
340def _create_variants(datastores, names, function):
341 def create_variant(name, orig_d, arg = None):
342 new_d = bb.data.createCopy(orig_d)
343 function(arg or name, new_d)
344 datastores[name] = new_d
345
346 for variant, variant_d in datastores.items():
347 for name in names:
348 if not variant:
349 # Based on main recipe
350 create_variant(name, variant_d)
351 else:
352 create_variant("%s-%s" % (variant, name), variant_d, name)
353
354def _expand_versions(versions):
355 def expand_one(version, start, end):
356 for i in xrange(start, end + 1):
357 ver = _bbversions_re.sub(str(i), version, 1)
358 yield ver
359
360 versions = iter(versions)
361 while True:
362 try:
363 version = next(versions)
364 except StopIteration:
365 break
366
367 range_ver = _bbversions_re.search(version)
368 if not range_ver:
369 yield version
370 else:
371 newversions = expand_one(version, int(range_ver.group("from")),
372 int(range_ver.group("to")))
373 versions = itertools.chain(newversions, versions)
374
375def multi_finalize(fn, d):
376 appends = (d.getVar("__BBAPPEND", True) or "").split()
377 for append in appends:
378 logger.debug(2, "Appending .bbappend file %s to %s", append, fn)
379 bb.parse.BBHandler.handle(append, d, True)
380
381 onlyfinalise = d.getVar("__ONLYFINALISE", False)
382
383 safe_d = d
384 d = bb.data.createCopy(safe_d)
385 try:
386 finalize(fn, d)
387 except bb.parse.SkipPackage as e:
388 d.setVar("__SKIPPED", e.args[0])
389 datastores = {"": safe_d}
390
391 versions = (d.getVar("BBVERSIONS", True) or "").split()
392 if versions:
393 pv = orig_pv = d.getVar("PV", True)
394 baseversions = {}
395
396 def verfunc(ver, d, pv_d = None):
397 if pv_d is None:
398 pv_d = d
399
400 overrides = d.getVar("OVERRIDES", True).split(":")
401 pv_d.setVar("PV", ver)
402 overrides.append(ver)
403 bpv = baseversions.get(ver) or orig_pv
404 pv_d.setVar("BPV", bpv)
405 overrides.append(bpv)
406 d.setVar("OVERRIDES", ":".join(overrides))
407
408 versions = list(_expand_versions(versions))
409 for pos, version in enumerate(list(versions)):
410 try:
411 pv, bpv = version.split(":", 2)
412 except ValueError:
413 pass
414 else:
415 versions[pos] = pv
416 baseversions[pv] = bpv
417
418 if pv in versions and not baseversions.get(pv):
419 versions.remove(pv)
420 else:
421 pv = versions.pop()
422
423 # This is necessary because our existing main datastore
424 # has already been finalized with the old PV, we need one
425 # that's been finalized with the new PV.
426 d = bb.data.createCopy(safe_d)
427 verfunc(pv, d, safe_d)
428 try:
429 finalize(fn, d)
430 except bb.parse.SkipPackage as e:
431 d.setVar("__SKIPPED", e.args[0])
432
433 _create_variants(datastores, versions, verfunc)
434
435 extended = d.getVar("BBCLASSEXTEND", True) or ""
436 if extended:
437 # the following is to support bbextends with arguments, for e.g. multilib
438 # an example is as follows:
439 # BBCLASSEXTEND = "multilib:lib32"
440 # it will create foo-lib32, inheriting multilib.bbclass and set
441 # BBEXTENDCURR to "multilib" and BBEXTENDVARIANT to "lib32"
442 extendedmap = {}
443 variantmap = {}
444
445 for ext in extended.split():
446 eext = ext.split(':', 2)
447 if len(eext) > 1:
448 extendedmap[ext] = eext[0]
449 variantmap[ext] = eext[1]
450 else:
451 extendedmap[ext] = ext
452
453 pn = d.getVar("PN", True)
454 def extendfunc(name, d):
455 if name != extendedmap[name]:
456 d.setVar("BBEXTENDCURR", extendedmap[name])
457 d.setVar("BBEXTENDVARIANT", variantmap[name])
458 else:
459 d.setVar("PN", "%s-%s" % (pn, name))
460 bb.parse.BBHandler.inherit(extendedmap[name], fn, 0, d)
461
462 safe_d.setVar("BBCLASSEXTEND", extended)
463 _create_variants(datastores, extendedmap.keys(), extendfunc)
464
465 for variant, variant_d in datastores.iteritems():
466 if variant:
467 try:
468 if not onlyfinalise or variant in onlyfinalise:
469 finalize(fn, variant_d, variant)
470 except bb.parse.SkipPackage as e:
471 variant_d.setVar("__SKIPPED", e.args[0])
472
473 if len(datastores) > 1:
474 variants = filter(None, datastores.iterkeys())
475 safe_d.setVar("__VARIANTS", " ".join(variants))
476
477 datastores[""] = d
478 return datastores