blob: f0911e6fb76fe7b50906182216b9ede0ad723c16 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001"""
2 AbstractSyntaxTree classes for the Bitbake language
3"""
4
5# Copyright (C) 2003, 2004 Chris Larson
6# Copyright (C) 2003, 2004 Phil Blundell
7# Copyright (C) 2009 Holger Hans Peter Freyther
8#
Brad Bishopc342db32019-05-15 21:57:59 -04009# SPDX-License-Identifier: GPL-2.0-only
Patrick Williamsc124f4f2015-09-15 14:41:29 -050010#
Patrick Williamsc0f7c042017-02-23 20:41:17 -060011
Patrick Williamsc124f4f2015-09-15 14:41:29 -050012import re
13import string
14import logging
15import bb
16import itertools
17from bb import methodpool
18from bb.parse import logger
19
Patrick Williamsc124f4f2015-09-15 14:41:29 -050020class StatementGroup(list):
21 def eval(self, data):
22 for statement in self:
23 statement.eval(data)
24
25class AstNode(object):
26 def __init__(self, filename, lineno):
27 self.filename = filename
28 self.lineno = lineno
29
30class IncludeNode(AstNode):
31 def __init__(self, filename, lineno, what_file, force):
32 AstNode.__init__(self, filename, lineno)
33 self.what_file = what_file
34 self.force = force
35
36 def eval(self, data):
37 """
38 Include the file and evaluate the statements
39 """
40 s = data.expand(self.what_file)
41 logger.debug(2, "CONF %s:%s: including %s", self.filename, self.lineno, s)
42
43 # TODO: Cache those includes... maybe not here though
44 if self.force:
45 bb.parse.ConfHandler.include(self.filename, s, self.lineno, data, "include required")
46 else:
47 bb.parse.ConfHandler.include(self.filename, s, self.lineno, data, False)
48
49class ExportNode(AstNode):
50 def __init__(self, filename, lineno, var):
51 AstNode.__init__(self, filename, lineno)
52 self.var = var
53
54 def eval(self, data):
55 data.setVarFlag(self.var, "export", 1, op = 'exported')
56
Patrick Williamsc0f7c042017-02-23 20:41:17 -060057class UnsetNode(AstNode):
58 def __init__(self, filename, lineno, var):
59 AstNode.__init__(self, filename, lineno)
60 self.var = var
61
62 def eval(self, data):
63 loginfo = {
64 'variable': self.var,
65 'file': self.filename,
66 'line': self.lineno,
67 }
68 data.delVar(self.var,**loginfo)
69
70class UnsetFlagNode(AstNode):
71 def __init__(self, filename, lineno, var, flag):
72 AstNode.__init__(self, filename, lineno)
73 self.var = var
74 self.flag = flag
75
76 def eval(self, data):
77 loginfo = {
78 'variable': self.var,
79 'file': self.filename,
80 'line': self.lineno,
81 }
82 data.delVarFlag(self.var, self.flag, **loginfo)
83
Patrick Williamsc124f4f2015-09-15 14:41:29 -050084class DataNode(AstNode):
85 """
86 Various data related updates. For the sake of sanity
87 we have one class doing all this. This means that all
88 this need to be re-evaluated... we might be able to do
89 that faster with multiple classes.
90 """
91 def __init__(self, filename, lineno, groupd):
92 AstNode.__init__(self, filename, lineno)
93 self.groupd = groupd
94
95 def getFunc(self, key, data):
96 if 'flag' in self.groupd and self.groupd['flag'] != None:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050097 return data.getVarFlag(key, self.groupd['flag'], expand=False, noweakdefault=True)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050098 else:
99 return data.getVar(key, False, noweakdefault=True, parsing=True)
100
101 def eval(self, data):
102 groupd = self.groupd
103 key = groupd["var"]
104 loginfo = {
105 'variable': key,
106 'file': self.filename,
107 'line': self.lineno,
108 }
109 if "exp" in groupd and groupd["exp"] != None:
110 data.setVarFlag(key, "export", 1, op = 'exported', **loginfo)
111
112 op = "set"
113 if "ques" in groupd and groupd["ques"] != None:
114 val = self.getFunc(key, data)
115 op = "set?"
116 if val == None:
117 val = groupd["value"]
118 elif "colon" in groupd and groupd["colon"] != None:
119 e = data.createCopy()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500120 op = "immediate"
121 val = e.expand(groupd["value"], key + "[:=]")
122 elif "append" in groupd and groupd["append"] != None:
123 op = "append"
124 val = "%s %s" % ((self.getFunc(key, data) or ""), groupd["value"])
125 elif "prepend" in groupd and groupd["prepend"] != None:
126 op = "prepend"
127 val = "%s %s" % (groupd["value"], (self.getFunc(key, data) or ""))
128 elif "postdot" in groupd and groupd["postdot"] != None:
129 op = "postdot"
130 val = "%s%s" % ((self.getFunc(key, data) or ""), groupd["value"])
131 elif "predot" in groupd and groupd["predot"] != None:
132 op = "predot"
133 val = "%s%s" % (groupd["value"], (self.getFunc(key, data) or ""))
134 else:
135 val = groupd["value"]
136
137 flag = None
138 if 'flag' in groupd and groupd['flag'] != None:
139 flag = groupd['flag']
140 elif groupd["lazyques"]:
141 flag = "_defaultval"
142
143 loginfo['op'] = op
144 loginfo['detail'] = groupd["value"]
145
146 if flag:
147 data.setVarFlag(key, flag, val, **loginfo)
148 else:
149 data.setVar(key, val, parsing=True, **loginfo)
150
151class MethodNode(AstNode):
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600152 tr_tbl = str.maketrans('/.+-@%&', '_______')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500153
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500154 def __init__(self, filename, lineno, func_name, body, python, fakeroot):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500155 AstNode.__init__(self, filename, lineno)
156 self.func_name = func_name
157 self.body = body
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500158 self.python = python
159 self.fakeroot = fakeroot
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500160
161 def eval(self, data):
162 text = '\n'.join(self.body)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500163 funcname = self.func_name
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500164 if self.func_name == "__anonymous":
165 funcname = ("__anon_%s_%s" % (self.lineno, self.filename.translate(MethodNode.tr_tbl)))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500166 self.python = True
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500167 text = "def %s(d):\n" % (funcname) + text
Brad Bishop19323692019-04-05 15:28:33 -0400168 bb.methodpool.insert_method(funcname, text, self.filename, self.lineno - len(self.body) - 1)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500169 anonfuncs = data.getVar('__BBANONFUNCS', False) or []
170 anonfuncs.append(funcname)
171 data.setVar('__BBANONFUNCS', anonfuncs)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500172 if data.getVar(funcname, False):
173 # clean up old version of this piece of metadata, as its
174 # flags could cause problems
175 data.delVarFlag(funcname, 'python')
176 data.delVarFlag(funcname, 'fakeroot')
177 if self.python:
178 data.setVarFlag(funcname, "python", "1")
179 if self.fakeroot:
180 data.setVarFlag(funcname, "fakeroot", "1")
181 data.setVarFlag(funcname, "func", 1)
182 data.setVar(funcname, text, parsing=True)
183 data.setVarFlag(funcname, 'filename', self.filename)
184 data.setVarFlag(funcname, 'lineno', str(self.lineno - len(self.body)))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500185
186class PythonMethodNode(AstNode):
187 def __init__(self, filename, lineno, function, modulename, body):
188 AstNode.__init__(self, filename, lineno)
189 self.function = function
190 self.modulename = modulename
191 self.body = body
192
193 def eval(self, data):
194 # Note we will add root to parsedmethods after having parse
195 # 'this' file. This means we will not parse methods from
196 # bb classes twice
197 text = '\n'.join(self.body)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500198 bb.methodpool.insert_method(self.modulename, text, self.filename, self.lineno - len(self.body) - 1)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500199 data.setVarFlag(self.function, "func", 1)
200 data.setVarFlag(self.function, "python", 1)
201 data.setVar(self.function, text, parsing=True)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500202 data.setVarFlag(self.function, 'filename', self.filename)
203 data.setVarFlag(self.function, 'lineno', str(self.lineno - len(self.body) - 1))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500204
205class ExportFuncsNode(AstNode):
206 def __init__(self, filename, lineno, fns, classname):
207 AstNode.__init__(self, filename, lineno)
208 self.n = fns.split()
209 self.classname = classname
210
211 def eval(self, data):
212
213 for func in self.n:
214 calledfunc = self.classname + "_" + func
215
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500216 if data.getVar(func, False) and not data.getVarFlag(func, 'export_func', False):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500217 continue
218
219 if data.getVar(func, False):
220 data.setVarFlag(func, 'python', None)
221 data.setVarFlag(func, 'func', None)
222
223 for flag in [ "func", "python" ]:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500224 if data.getVarFlag(calledfunc, flag, False):
225 data.setVarFlag(func, flag, data.getVarFlag(calledfunc, flag, False))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500226 for flag in [ "dirs" ]:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500227 if data.getVarFlag(func, flag, False):
228 data.setVarFlag(calledfunc, flag, data.getVarFlag(func, flag, False))
229 data.setVarFlag(func, "filename", "autogenerated")
230 data.setVarFlag(func, "lineno", 1)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500231
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500232 if data.getVarFlag(calledfunc, "python", False):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500233 data.setVar(func, " bb.build.exec_func('" + calledfunc + "', d)\n", parsing=True)
234 else:
235 if "-" in self.classname:
236 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))
237 data.setVar(func, " " + calledfunc + "\n", parsing=True)
238 data.setVarFlag(func, 'export_func', '1')
239
240class AddTaskNode(AstNode):
241 def __init__(self, filename, lineno, func, before, after):
242 AstNode.__init__(self, filename, lineno)
243 self.func = func
244 self.before = before
245 self.after = after
246
247 def eval(self, data):
248 bb.build.addtask(self.func, self.before, self.after, data)
249
250class DelTaskNode(AstNode):
251 def __init__(self, filename, lineno, func):
252 AstNode.__init__(self, filename, lineno)
253 self.func = func
254
255 def eval(self, data):
256 bb.build.deltask(self.func, data)
257
258class BBHandlerNode(AstNode):
259 def __init__(self, filename, lineno, fns):
260 AstNode.__init__(self, filename, lineno)
261 self.hs = fns.split()
262
263 def eval(self, data):
264 bbhands = data.getVar('__BBHANDLERS', False) or []
265 for h in self.hs:
266 bbhands.append(h)
267 data.setVarFlag(h, "handler", 1)
268 data.setVar('__BBHANDLERS', bbhands)
269
270class InheritNode(AstNode):
271 def __init__(self, filename, lineno, classes):
272 AstNode.__init__(self, filename, lineno)
273 self.classes = classes
274
275 def eval(self, data):
276 bb.parse.BBHandler.inherit(self.classes, self.filename, self.lineno, data)
277
278def handleInclude(statements, filename, lineno, m, force):
279 statements.append(IncludeNode(filename, lineno, m.group(1), force))
280
281def handleExport(statements, filename, lineno, m):
282 statements.append(ExportNode(filename, lineno, m.group(1)))
283
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600284def handleUnset(statements, filename, lineno, m):
285 statements.append(UnsetNode(filename, lineno, m.group(1)))
286
287def handleUnsetFlag(statements, filename, lineno, m):
288 statements.append(UnsetFlagNode(filename, lineno, m.group(1), m.group(2)))
289
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500290def handleData(statements, filename, lineno, groupd):
291 statements.append(DataNode(filename, lineno, groupd))
292
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500293def handleMethod(statements, filename, lineno, func_name, body, python, fakeroot):
294 statements.append(MethodNode(filename, lineno, func_name, body, python, fakeroot))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500295
296def handlePythonMethod(statements, filename, lineno, funcname, modulename, body):
297 statements.append(PythonMethodNode(filename, lineno, funcname, modulename, body))
298
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500299def handleExportFuncs(statements, filename, lineno, m, classname):
300 statements.append(ExportFuncsNode(filename, lineno, m.group(1), classname))
301
302def handleAddTask(statements, filename, lineno, m):
303 func = m.group("func")
304 before = m.group("before")
305 after = m.group("after")
306 if func is None:
307 return
308
309 statements.append(AddTaskNode(filename, lineno, func, before, after))
310
311def handleDelTask(statements, filename, lineno, m):
312 func = m.group("func")
313 if func is None:
314 return
315
316 statements.append(DelTaskNode(filename, lineno, func))
317
318def handleBBHandlers(statements, filename, lineno, m):
319 statements.append(BBHandlerNode(filename, lineno, m.group(1)))
320
321def handleInherit(statements, filename, lineno, m):
322 classes = m.group(1)
323 statements.append(InheritNode(filename, lineno, classes))
324
Brad Bishop316dfdd2018-06-25 12:45:53 -0400325def runAnonFuncs(d):
326 code = []
327 for funcname in d.getVar("__BBANONFUNCS", False) or []:
328 code.append("%s(d)" % funcname)
329 bb.utils.better_exec("\n".join(code), {"d": d})
330
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500331def finalize(fn, d, variant = None):
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600332 saved_handlers = bb.event.get_handlers().copy()
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800333 try:
334 for var in d.getVar('__BBHANDLERS', False) or []:
335 # try to add the handler
336 handlerfn = d.getVarFlag(var, "filename", False)
337 if not handlerfn:
338 bb.fatal("Undefined event handler function '%s'" % var)
339 handlerln = int(d.getVarFlag(var, "lineno", False))
340 bb.event.register(var, d.getVar(var, False), (d.getVarFlag(var, "eventmask") or "").split(), handlerfn, handlerln)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600341
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800342 bb.event.fire(bb.event.RecipePreFinalise(fn), d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500343
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800344 bb.data.expandKeys(d)
345 runAnonFuncs(d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500346
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800347 tasklist = d.getVar('__BBTASKS', False) or []
348 bb.event.fire(bb.event.RecipeTaskPreProcess(fn, list(tasklist)), d)
349 bb.build.add_tasks(tasklist, d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500350
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800351 bb.parse.siggen.finalise(fn, d, variant)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500352
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800353 d.setVar('BBINCLUDED', bb.parse.get_file_depends(d))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500354
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800355 bb.event.fire(bb.event.RecipeParsed(fn), d)
356 finally:
357 bb.event.set_handlers(saved_handlers)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500358
359def _create_variants(datastores, names, function, onlyfinalise):
360 def create_variant(name, orig_d, arg = None):
361 if onlyfinalise and name not in onlyfinalise:
362 return
363 new_d = bb.data.createCopy(orig_d)
364 function(arg or name, new_d)
365 datastores[name] = new_d
366
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600367 for variant in list(datastores.keys()):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500368 for name in names:
369 if not variant:
370 # Based on main recipe
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600371 create_variant(name, datastores[""])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500372 else:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600373 create_variant("%s-%s" % (variant, name), datastores[variant], name)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500374
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500375def multi_finalize(fn, d):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500376 appends = (d.getVar("__BBAPPEND") or "").split()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500377 for append in appends:
378 logger.debug(1, "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.SkipRecipe as e:
388 d.setVar("__SKIPPED", e.args[0])
389 datastores = {"": safe_d}
390
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500391 extended = d.getVar("BBCLASSEXTEND") or ""
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500392 if extended:
393 # the following is to support bbextends with arguments, for e.g. multilib
394 # an example is as follows:
395 # BBCLASSEXTEND = "multilib:lib32"
396 # it will create foo-lib32, inheriting multilib.bbclass and set
397 # BBEXTENDCURR to "multilib" and BBEXTENDVARIANT to "lib32"
398 extendedmap = {}
399 variantmap = {}
400
401 for ext in extended.split():
402 eext = ext.split(':', 2)
403 if len(eext) > 1:
404 extendedmap[ext] = eext[0]
405 variantmap[ext] = eext[1]
406 else:
407 extendedmap[ext] = ext
408
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500409 pn = d.getVar("PN")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500410 def extendfunc(name, d):
411 if name != extendedmap[name]:
412 d.setVar("BBEXTENDCURR", extendedmap[name])
413 d.setVar("BBEXTENDVARIANT", variantmap[name])
414 else:
415 d.setVar("PN", "%s-%s" % (pn, name))
416 bb.parse.BBHandler.inherit(extendedmap[name], fn, 0, d)
417
418 safe_d.setVar("BBCLASSEXTEND", extended)
419 _create_variants(datastores, extendedmap.keys(), extendfunc, onlyfinalise)
420
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600421 for variant in datastores.keys():
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500422 if variant:
423 try:
424 if not onlyfinalise or variant in onlyfinalise:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600425 finalize(fn, datastores[variant], variant)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500426 except bb.parse.SkipRecipe as e:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600427 datastores[variant].setVar("__SKIPPED", e.args[0])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500428
429 datastores[""] = d
430 return datastores