blob: 50a88f7da7cbfa090088dd4d6f0c5ea293a3a0bc [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 bb
Patrick Williamsc124f4f2015-09-15 14:41:29 -050013from bb import methodpool
14from bb.parse import logger
15
Patrick Williamsc124f4f2015-09-15 14:41:29 -050016class StatementGroup(list):
17 def eval(self, data):
18 for statement in self:
19 statement.eval(data)
20
21class AstNode(object):
22 def __init__(self, filename, lineno):
23 self.filename = filename
24 self.lineno = lineno
25
26class IncludeNode(AstNode):
27 def __init__(self, filename, lineno, what_file, force):
28 AstNode.__init__(self, filename, lineno)
29 self.what_file = what_file
30 self.force = force
31
32 def eval(self, data):
33 """
34 Include the file and evaluate the statements
35 """
36 s = data.expand(self.what_file)
Andrew Geisslerd1e89492021-02-12 15:35:20 -060037 logger.debug2("CONF %s:%s: including %s", self.filename, self.lineno, s)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050038
39 # TODO: Cache those includes... maybe not here though
40 if self.force:
41 bb.parse.ConfHandler.include(self.filename, s, self.lineno, data, "include required")
42 else:
43 bb.parse.ConfHandler.include(self.filename, s, self.lineno, data, False)
44
45class ExportNode(AstNode):
46 def __init__(self, filename, lineno, var):
47 AstNode.__init__(self, filename, lineno)
48 self.var = var
49
50 def eval(self, data):
51 data.setVarFlag(self.var, "export", 1, op = 'exported')
52
Patrick Williamsc0f7c042017-02-23 20:41:17 -060053class UnsetNode(AstNode):
54 def __init__(self, filename, lineno, var):
55 AstNode.__init__(self, filename, lineno)
56 self.var = var
57
58 def eval(self, data):
59 loginfo = {
60 'variable': self.var,
61 'file': self.filename,
62 'line': self.lineno,
63 }
64 data.delVar(self.var,**loginfo)
65
66class UnsetFlagNode(AstNode):
67 def __init__(self, filename, lineno, var, flag):
68 AstNode.__init__(self, filename, lineno)
69 self.var = var
70 self.flag = flag
71
72 def eval(self, data):
73 loginfo = {
74 'variable': self.var,
75 'file': self.filename,
76 'line': self.lineno,
77 }
78 data.delVarFlag(self.var, self.flag, **loginfo)
79
Patrick Williamsc124f4f2015-09-15 14:41:29 -050080class DataNode(AstNode):
81 """
82 Various data related updates. For the sake of sanity
83 we have one class doing all this. This means that all
84 this need to be re-evaluated... we might be able to do
85 that faster with multiple classes.
86 """
87 def __init__(self, filename, lineno, groupd):
88 AstNode.__init__(self, filename, lineno)
89 self.groupd = groupd
90
91 def getFunc(self, key, data):
Andrew Geissler82c905d2020-04-13 13:39:40 -050092 if 'flag' in self.groupd and self.groupd['flag'] is not None:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050093 return data.getVarFlag(key, self.groupd['flag'], expand=False, noweakdefault=True)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050094 else:
95 return data.getVar(key, False, noweakdefault=True, parsing=True)
96
97 def eval(self, data):
98 groupd = self.groupd
99 key = groupd["var"]
100 loginfo = {
101 'variable': key,
102 'file': self.filename,
103 'line': self.lineno,
104 }
Andrew Geissler82c905d2020-04-13 13:39:40 -0500105 if "exp" in groupd and groupd["exp"] is not None:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500106 data.setVarFlag(key, "export", 1, op = 'exported', **loginfo)
107
108 op = "set"
Andrew Geissler82c905d2020-04-13 13:39:40 -0500109 if "ques" in groupd and groupd["ques"] is not None:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500110 val = self.getFunc(key, data)
111 op = "set?"
Andrew Geissler82c905d2020-04-13 13:39:40 -0500112 if val is None:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500113 val = groupd["value"]
Andrew Geissler82c905d2020-04-13 13:39:40 -0500114 elif "colon" in groupd and groupd["colon"] is not None:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500115 e = data.createCopy()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500116 op = "immediate"
117 val = e.expand(groupd["value"], key + "[:=]")
Andrew Geissler82c905d2020-04-13 13:39:40 -0500118 elif "append" in groupd and groupd["append"] is not None:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500119 op = "append"
120 val = "%s %s" % ((self.getFunc(key, data) or ""), groupd["value"])
Andrew Geissler82c905d2020-04-13 13:39:40 -0500121 elif "prepend" in groupd and groupd["prepend"] is not None:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500122 op = "prepend"
123 val = "%s %s" % (groupd["value"], (self.getFunc(key, data) or ""))
Andrew Geissler82c905d2020-04-13 13:39:40 -0500124 elif "postdot" in groupd and groupd["postdot"] is not None:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500125 op = "postdot"
126 val = "%s%s" % ((self.getFunc(key, data) or ""), groupd["value"])
Andrew Geissler82c905d2020-04-13 13:39:40 -0500127 elif "predot" in groupd and groupd["predot"] is not None:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500128 op = "predot"
129 val = "%s%s" % (groupd["value"], (self.getFunc(key, data) or ""))
130 else:
131 val = groupd["value"]
132
133 flag = None
Andrew Geissler82c905d2020-04-13 13:39:40 -0500134 if 'flag' in groupd and groupd['flag'] is not None:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500135 flag = groupd['flag']
136 elif groupd["lazyques"]:
137 flag = "_defaultval"
138
139 loginfo['op'] = op
140 loginfo['detail'] = groupd["value"]
141
142 if flag:
143 data.setVarFlag(key, flag, val, **loginfo)
144 else:
145 data.setVar(key, val, parsing=True, **loginfo)
146
147class MethodNode(AstNode):
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600148 tr_tbl = str.maketrans('/.+-@%&', '_______')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500149
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500150 def __init__(self, filename, lineno, func_name, body, python, fakeroot):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500151 AstNode.__init__(self, filename, lineno)
152 self.func_name = func_name
153 self.body = body
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500154 self.python = python
155 self.fakeroot = fakeroot
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500156
157 def eval(self, data):
158 text = '\n'.join(self.body)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500159 funcname = self.func_name
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500160 if self.func_name == "__anonymous":
161 funcname = ("__anon_%s_%s" % (self.lineno, self.filename.translate(MethodNode.tr_tbl)))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500162 self.python = True
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500163 text = "def %s(d):\n" % (funcname) + text
Brad Bishop19323692019-04-05 15:28:33 -0400164 bb.methodpool.insert_method(funcname, text, self.filename, self.lineno - len(self.body) - 1)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500165 anonfuncs = data.getVar('__BBANONFUNCS', False) or []
166 anonfuncs.append(funcname)
167 data.setVar('__BBANONFUNCS', anonfuncs)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500168 if data.getVar(funcname, False):
169 # clean up old version of this piece of metadata, as its
170 # flags could cause problems
171 data.delVarFlag(funcname, 'python')
172 data.delVarFlag(funcname, 'fakeroot')
173 if self.python:
174 data.setVarFlag(funcname, "python", "1")
175 if self.fakeroot:
176 data.setVarFlag(funcname, "fakeroot", "1")
177 data.setVarFlag(funcname, "func", 1)
178 data.setVar(funcname, text, parsing=True)
179 data.setVarFlag(funcname, 'filename', self.filename)
180 data.setVarFlag(funcname, 'lineno', str(self.lineno - len(self.body)))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500181
182class PythonMethodNode(AstNode):
183 def __init__(self, filename, lineno, function, modulename, body):
184 AstNode.__init__(self, filename, lineno)
185 self.function = function
186 self.modulename = modulename
187 self.body = body
188
189 def eval(self, data):
190 # Note we will add root to parsedmethods after having parse
191 # 'this' file. This means we will not parse methods from
192 # bb classes twice
193 text = '\n'.join(self.body)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500194 bb.methodpool.insert_method(self.modulename, text, self.filename, self.lineno - len(self.body) - 1)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500195 data.setVarFlag(self.function, "func", 1)
196 data.setVarFlag(self.function, "python", 1)
197 data.setVar(self.function, text, parsing=True)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500198 data.setVarFlag(self.function, 'filename', self.filename)
199 data.setVarFlag(self.function, 'lineno', str(self.lineno - len(self.body) - 1))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500200
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
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500212 if data.getVar(func, False) and not data.getVarFlag(func, 'export_func', False):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500213 continue
214
215 if data.getVar(func, False):
216 data.setVarFlag(func, 'python', None)
217 data.setVarFlag(func, 'func', None)
218
219 for flag in [ "func", "python" ]:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500220 if data.getVarFlag(calledfunc, flag, False):
221 data.setVarFlag(func, flag, data.getVarFlag(calledfunc, flag, False))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500222 for flag in [ "dirs" ]:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500223 if data.getVarFlag(func, flag, False):
224 data.setVarFlag(calledfunc, flag, data.getVarFlag(func, flag, False))
225 data.setVarFlag(func, "filename", "autogenerated")
226 data.setVarFlag(func, "lineno", 1)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500227
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500228 if data.getVarFlag(calledfunc, "python", False):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500229 data.setVar(func, " bb.build.exec_func('" + calledfunc + "', d)\n", parsing=True)
230 else:
231 if "-" in self.classname:
232 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))
233 data.setVar(func, " " + calledfunc + "\n", parsing=True)
234 data.setVarFlag(func, 'export_func', '1')
235
236class AddTaskNode(AstNode):
237 def __init__(self, filename, lineno, func, before, after):
238 AstNode.__init__(self, filename, lineno)
239 self.func = func
240 self.before = before
241 self.after = after
242
243 def eval(self, data):
244 bb.build.addtask(self.func, self.before, self.after, data)
245
246class DelTaskNode(AstNode):
Andrew Geisslerb7d28612020-07-24 16:15:54 -0500247 def __init__(self, filename, lineno, tasks):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500248 AstNode.__init__(self, filename, lineno)
Andrew Geisslerb7d28612020-07-24 16:15:54 -0500249 self.tasks = tasks
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500250
251 def eval(self, data):
Andrew Geisslerb7d28612020-07-24 16:15:54 -0500252 tasks = data.expand(self.tasks).split()
253 for task in tasks:
254 bb.build.deltask(task, data)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500255
256class BBHandlerNode(AstNode):
257 def __init__(self, filename, lineno, fns):
258 AstNode.__init__(self, filename, lineno)
259 self.hs = fns.split()
260
261 def eval(self, data):
262 bbhands = data.getVar('__BBHANDLERS', False) or []
263 for h in self.hs:
264 bbhands.append(h)
265 data.setVarFlag(h, "handler", 1)
266 data.setVar('__BBHANDLERS', bbhands)
267
268class InheritNode(AstNode):
269 def __init__(self, filename, lineno, classes):
270 AstNode.__init__(self, filename, lineno)
271 self.classes = classes
272
273 def eval(self, data):
274 bb.parse.BBHandler.inherit(self.classes, self.filename, self.lineno, data)
275
276def handleInclude(statements, filename, lineno, m, force):
277 statements.append(IncludeNode(filename, lineno, m.group(1), force))
278
279def handleExport(statements, filename, lineno, m):
280 statements.append(ExportNode(filename, lineno, m.group(1)))
281
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600282def handleUnset(statements, filename, lineno, m):
283 statements.append(UnsetNode(filename, lineno, m.group(1)))
284
285def handleUnsetFlag(statements, filename, lineno, m):
286 statements.append(UnsetFlagNode(filename, lineno, m.group(1), m.group(2)))
287
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500288def handleData(statements, filename, lineno, groupd):
289 statements.append(DataNode(filename, lineno, groupd))
290
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500291def handleMethod(statements, filename, lineno, func_name, body, python, fakeroot):
292 statements.append(MethodNode(filename, lineno, func_name, body, python, fakeroot))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500293
294def handlePythonMethod(statements, filename, lineno, funcname, modulename, body):
295 statements.append(PythonMethodNode(filename, lineno, funcname, modulename, body))
296
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500297def handleExportFuncs(statements, filename, lineno, m, classname):
298 statements.append(ExportFuncsNode(filename, lineno, m.group(1), classname))
299
300def handleAddTask(statements, filename, lineno, m):
301 func = m.group("func")
302 before = m.group("before")
303 after = m.group("after")
304 if func is None:
305 return
306
307 statements.append(AddTaskNode(filename, lineno, func, before, after))
308
309def handleDelTask(statements, filename, lineno, m):
Andrew Geisslerb7d28612020-07-24 16:15:54 -0500310 func = m.group(1)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500311 if func is None:
312 return
313
314 statements.append(DelTaskNode(filename, lineno, func))
315
316def handleBBHandlers(statements, filename, lineno, m):
317 statements.append(BBHandlerNode(filename, lineno, m.group(1)))
318
319def handleInherit(statements, filename, lineno, m):
320 classes = m.group(1)
321 statements.append(InheritNode(filename, lineno, classes))
322
Brad Bishop316dfdd2018-06-25 12:45:53 -0400323def runAnonFuncs(d):
324 code = []
325 for funcname in d.getVar("__BBANONFUNCS", False) or []:
326 code.append("%s(d)" % funcname)
327 bb.utils.better_exec("\n".join(code), {"d": d})
328
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500329def finalize(fn, d, variant = None):
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600330 saved_handlers = bb.event.get_handlers().copy()
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800331 try:
332 for var in d.getVar('__BBHANDLERS', False) or []:
333 # try to add the handler
334 handlerfn = d.getVarFlag(var, "filename", False)
335 if not handlerfn:
336 bb.fatal("Undefined event handler function '%s'" % var)
337 handlerln = int(d.getVarFlag(var, "lineno", False))
Andrew Geissler9b4d8b02021-02-19 12:26:16 -0600338 bb.event.register(var, d.getVar(var, False), (d.getVarFlag(var, "eventmask") or "").split(), handlerfn, handlerln, data=d)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600339
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800340 bb.event.fire(bb.event.RecipePreFinalise(fn), d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500341
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800342 bb.data.expandKeys(d)
Andrew Geissler1e34c2d2020-05-29 16:02:59 -0500343
344 bb.event.fire(bb.event.RecipePostKeyExpansion(fn), d)
345
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800346 runAnonFuncs(d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500347
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800348 tasklist = d.getVar('__BBTASKS', False) or []
349 bb.event.fire(bb.event.RecipeTaskPreProcess(fn, list(tasklist)), d)
350 bb.build.add_tasks(tasklist, d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500351
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800352 bb.parse.siggen.finalise(fn, d, variant)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500353
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800354 d.setVar('BBINCLUDED', bb.parse.get_file_depends(d))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500355
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800356 bb.event.fire(bb.event.RecipeParsed(fn), d)
357 finally:
358 bb.event.set_handlers(saved_handlers)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500359
360def _create_variants(datastores, names, function, onlyfinalise):
361 def create_variant(name, orig_d, arg = None):
362 if onlyfinalise and name not in onlyfinalise:
363 return
364 new_d = bb.data.createCopy(orig_d)
365 function(arg or name, new_d)
366 datastores[name] = new_d
367
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600368 for variant in list(datastores.keys()):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500369 for name in names:
370 if not variant:
371 # Based on main recipe
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600372 create_variant(name, datastores[""])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500373 else:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600374 create_variant("%s-%s" % (variant, name), datastores[variant], name)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500375
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500376def multi_finalize(fn, d):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500377 appends = (d.getVar("__BBAPPEND") or "").split()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500378 for append in appends:
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600379 logger.debug("Appending .bbappend file %s to %s", append, fn)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500380 bb.parse.BBHandler.handle(append, d, True)
381
382 onlyfinalise = d.getVar("__ONLYFINALISE", False)
383
384 safe_d = d
385 d = bb.data.createCopy(safe_d)
386 try:
387 finalize(fn, d)
388 except bb.parse.SkipRecipe as e:
389 d.setVar("__SKIPPED", e.args[0])
390 datastores = {"": safe_d}
391
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500392 extended = d.getVar("BBCLASSEXTEND") or ""
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500393 if extended:
394 # the following is to support bbextends with arguments, for e.g. multilib
395 # an example is as follows:
396 # BBCLASSEXTEND = "multilib:lib32"
397 # it will create foo-lib32, inheriting multilib.bbclass and set
398 # BBEXTENDCURR to "multilib" and BBEXTENDVARIANT to "lib32"
399 extendedmap = {}
400 variantmap = {}
401
402 for ext in extended.split():
403 eext = ext.split(':', 2)
404 if len(eext) > 1:
405 extendedmap[ext] = eext[0]
406 variantmap[ext] = eext[1]
407 else:
408 extendedmap[ext] = ext
409
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500410 pn = d.getVar("PN")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500411 def extendfunc(name, d):
412 if name != extendedmap[name]:
413 d.setVar("BBEXTENDCURR", extendedmap[name])
414 d.setVar("BBEXTENDVARIANT", variantmap[name])
415 else:
416 d.setVar("PN", "%s-%s" % (pn, name))
417 bb.parse.BBHandler.inherit(extendedmap[name], fn, 0, d)
418
419 safe_d.setVar("BBCLASSEXTEND", extended)
420 _create_variants(datastores, extendedmap.keys(), extendfunc, onlyfinalise)
421
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600422 for variant in datastores.keys():
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500423 if variant:
424 try:
425 if not onlyfinalise or variant in onlyfinalise:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600426 finalize(fn, datastores[variant], variant)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500427 except bb.parse.SkipRecipe as e:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600428 datastores[variant].setVar("__SKIPPED", e.args[0])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500429
430 datastores[""] = d
431 return datastores