blob: 6690dc51c216286647c92e16526c520220bf9b09 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001# 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
Patrick Williamsc0f7c042017-02-23 20:41:17 -060024
Patrick Williamsc124f4f2015-09-15 14:41:29 -050025import re
26import string
27import logging
28import bb
29import itertools
30from bb import methodpool
31from bb.parse import logger
32
Patrick Williamsc124f4f2015-09-15 14:41:29 -050033class 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
Patrick Williamsc0f7c042017-02-23 20:41:17 -060070class 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
Patrick Williamsc124f4f2015-09-15 14:41:29 -050097class 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:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500110 return data.getVarFlag(key, self.groupd['flag'], expand=False, noweakdefault=True)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500111 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()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500133 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):
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600165 tr_tbl = str.maketrans('/.+-@%&', '_______')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500166
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500167 def __init__(self, filename, lineno, func_name, body, python, fakeroot):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500168 AstNode.__init__(self, filename, lineno)
169 self.func_name = func_name
170 self.body = body
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500171 self.python = python
172 self.fakeroot = fakeroot
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500173
174 def eval(self, data):
175 text = '\n'.join(self.body)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500176 funcname = self.func_name
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500177 if self.func_name == "__anonymous":
178 funcname = ("__anon_%s_%s" % (self.lineno, self.filename.translate(MethodNode.tr_tbl)))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500179 self.python = True
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500180 text = "def %s(d):\n" % (funcname) + text
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500181 bb.methodpool.insert_method(funcname, text, self.filename, self.lineno - len(self.body))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500182 anonfuncs = data.getVar('__BBANONFUNCS', False) or []
183 anonfuncs.append(funcname)
184 data.setVar('__BBANONFUNCS', anonfuncs)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500185 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)))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500198
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)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500211 bb.methodpool.insert_method(self.modulename, text, self.filename, self.lineno - len(self.body) - 1)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500212 data.setVarFlag(self.function, "func", 1)
213 data.setVarFlag(self.function, "python", 1)
214 data.setVar(self.function, text, parsing=True)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500215 data.setVarFlag(self.function, 'filename', self.filename)
216 data.setVarFlag(self.function, 'lineno', str(self.lineno - len(self.body) - 1))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500217
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
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500229 if data.getVar(func, False) and not data.getVarFlag(func, 'export_func', False):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500230 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" ]:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500237 if data.getVarFlag(calledfunc, flag, False):
238 data.setVarFlag(func, flag, data.getVarFlag(calledfunc, flag, False))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500239 for flag in [ "dirs" ]:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500240 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)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500244
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500245 if data.getVarFlag(calledfunc, "python", False):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500246 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
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600297def 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
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500303def handleData(statements, filename, lineno, groupd):
304 statements.append(DataNode(filename, lineno, groupd))
305
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500306def handleMethod(statements, filename, lineno, func_name, body, python, fakeroot):
307 statements.append(MethodNode(filename, lineno, func_name, body, python, fakeroot))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500308
309def handlePythonMethod(statements, filename, lineno, funcname, modulename, body):
310 statements.append(PythonMethodNode(filename, lineno, funcname, modulename, body))
311
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500312def 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
Brad Bishop316dfdd2018-06-25 12:45:53 -0400338def 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
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500344def finalize(fn, d, variant = None):
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600345 saved_handlers = bb.event.get_handlers().copy()
346
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500347 for var in d.getVar('__BBHANDLERS', False) or []:
348 # try to add the handler
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500349 handlerfn = d.getVarFlag(var, "filename", False)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600350 if not handlerfn:
351 bb.fatal("Undefined event handler function '%s'" % var)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500352 handlerln = int(d.getVarFlag(var, "lineno", False))
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500353 bb.event.register(var, d.getVar(var, False), (d.getVarFlag(var, "eventmask") or "").split(), handlerfn, handlerln)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500354
355 bb.event.fire(bb.event.RecipePreFinalise(fn), d)
356
357 bb.data.expandKeys(d)
Brad Bishop316dfdd2018-06-25 12:45:53 -0400358 runAnonFuncs(d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500359
360 tasklist = d.getVar('__BBTASKS', False) or []
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500361 bb.event.fire(bb.event.RecipeTaskPreProcess(fn, list(tasklist)), d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500362 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)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600369 bb.event.set_handlers(saved_handlers)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500370
371def _create_variants(datastores, names, function, onlyfinalise):
372 def create_variant(name, orig_d, arg = None):
373 if onlyfinalise and name not in onlyfinalise:
374 return
375 new_d = bb.data.createCopy(orig_d)
376 function(arg or name, new_d)
377 datastores[name] = new_d
378
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600379 for variant in list(datastores.keys()):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500380 for name in names:
381 if not variant:
382 # Based on main recipe
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600383 create_variant(name, datastores[""])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500384 else:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600385 create_variant("%s-%s" % (variant, name), datastores[variant], name)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500386
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500387def multi_finalize(fn, d):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500388 appends = (d.getVar("__BBAPPEND") or "").split()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500389 for append in appends:
390 logger.debug(1, "Appending .bbappend file %s to %s", append, fn)
391 bb.parse.BBHandler.handle(append, d, True)
392
393 onlyfinalise = d.getVar("__ONLYFINALISE", False)
394
395 safe_d = d
396 d = bb.data.createCopy(safe_d)
397 try:
398 finalize(fn, d)
399 except bb.parse.SkipRecipe as e:
400 d.setVar("__SKIPPED", e.args[0])
401 datastores = {"": safe_d}
402
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500403 extended = d.getVar("BBCLASSEXTEND") or ""
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500404 if extended:
405 # the following is to support bbextends with arguments, for e.g. multilib
406 # an example is as follows:
407 # BBCLASSEXTEND = "multilib:lib32"
408 # it will create foo-lib32, inheriting multilib.bbclass and set
409 # BBEXTENDCURR to "multilib" and BBEXTENDVARIANT to "lib32"
410 extendedmap = {}
411 variantmap = {}
412
413 for ext in extended.split():
414 eext = ext.split(':', 2)
415 if len(eext) > 1:
416 extendedmap[ext] = eext[0]
417 variantmap[ext] = eext[1]
418 else:
419 extendedmap[ext] = ext
420
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500421 pn = d.getVar("PN")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500422 def extendfunc(name, d):
423 if name != extendedmap[name]:
424 d.setVar("BBEXTENDCURR", extendedmap[name])
425 d.setVar("BBEXTENDVARIANT", variantmap[name])
426 else:
427 d.setVar("PN", "%s-%s" % (pn, name))
428 bb.parse.BBHandler.inherit(extendedmap[name], fn, 0, d)
429
430 safe_d.setVar("BBCLASSEXTEND", extended)
431 _create_variants(datastores, extendedmap.keys(), extendfunc, onlyfinalise)
432
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600433 for variant in datastores.keys():
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500434 if variant:
435 try:
436 if not onlyfinalise or variant in onlyfinalise:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600437 finalize(fn, datastores[variant], variant)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500438 except bb.parse.SkipRecipe as e:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600439 datastores[variant].setVar("__SKIPPED", e.args[0])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500440
441 datastores[""] = d
442 return datastores