blob: 18e686838711ffddc95c03053d7e4ffd4cc1f793 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001"""
2 class for handling .bb files
3
4 Reads a .bb file and obtains its metadata
5
6"""
7
8
9# Copyright (C) 2003, 2004 Chris Larson
10# Copyright (C) 2003, 2004 Phil Blundell
11#
Brad Bishopc342db32019-05-15 21:57:59 -040012# SPDX-License-Identifier: GPL-2.0-only
Patrick Williamsc124f4f2015-09-15 14:41:29 -050013#
Patrick Williamsc0f7c042017-02-23 20:41:17 -060014
Patrick Williamsc124f4f2015-09-15 14:41:29 -050015import re, bb, os
Andrew Geissler6ce62a22020-11-30 19:58:47 -060016import bb.build, bb.utils, bb.data_smart
Patrick Williamsc124f4f2015-09-15 14:41:29 -050017
18from . import ConfHandler
19from .. import resolve_file, ast, logger, ParseError
20from .ConfHandler import include, init
21
Andrew Geissler5f350902021-07-23 13:09:54 -040022__func_start_regexp__ = re.compile(r"(((?P<py>python(?=(\s|\()))|(?P<fr>fakeroot(?=\s)))\s*)*(?P<func>[\w\.\-\+\{\}\$:]+)?\s*\(\s*\)\s*{$" )
Brad Bishop19323692019-04-05 15:28:33 -040023__inherit_regexp__ = re.compile(r"inherit\s+(.+)" )
24__export_func_regexp__ = re.compile(r"EXPORT_FUNCTIONS\s+(.+)" )
25__addtask_regexp__ = re.compile(r"addtask\s+(?P<func>\w+)\s*((before\s*(?P<before>((.*(?=after))|(.*))))|(after\s*(?P<after>((.*(?=before))|(.*)))))*")
Andrew Geisslerb7d28612020-07-24 16:15:54 -050026__deltask_regexp__ = re.compile(r"deltask\s+(.+)")
Brad Bishop19323692019-04-05 15:28:33 -040027__addhandler_regexp__ = re.compile(r"addhandler\s+(.+)" )
28__def_regexp__ = re.compile(r"def\s+(\w+).*:" )
29__python_func_regexp__ = re.compile(r"(\s+.*)|(^$)|(^#)" )
30__python_tab_regexp__ = re.compile(r" *\t")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050031
Patrick Williamsc124f4f2015-09-15 14:41:29 -050032__infunc__ = []
33__inpython__ = False
34__body__ = []
35__classname__ = ""
36
37cached_statements = {}
38
Patrick Williamsc124f4f2015-09-15 14:41:29 -050039def supports(fn, d):
40 """Return True if fn has a supported extension"""
41 return os.path.splitext(fn)[-1] in [".bb", ".bbclass", ".inc"]
42
43def inherit(files, fn, lineno, d):
44 __inherit_cache = d.getVar('__inherit_cache', False) or []
45 files = d.expand(files).split()
46 for file in files:
Patrick Williams92b42cb2022-09-03 06:53:57 -050047 classtype = d.getVar("__bbclasstype", False)
48 origfile = file
49 for t in ["classes-" + classtype, "classes"]:
50 file = origfile
51 if not os.path.isabs(file) and not file.endswith(".bbclass"):
52 file = os.path.join(t, '%s.bbclass' % file)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050053
Patrick Williams92b42cb2022-09-03 06:53:57 -050054 if not os.path.isabs(file):
55 bbpath = d.getVar("BBPATH")
56 abs_fn, attempts = bb.utils.which(bbpath, file, history=True)
57 for af in attempts:
58 if af != abs_fn:
59 bb.parse.mark_dependency(d, af)
60 if abs_fn:
61 file = abs_fn
62
63 if os.path.exists(file):
64 break
65
66 if not os.path.exists(file):
67 raise ParseError("Could not inherit file %s" % (file), fn, lineno)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050068
69 if not file in __inherit_cache:
Andrew Geisslerd1e89492021-02-12 15:35:20 -060070 logger.debug("Inheriting %s (from %s:%d)" % (file, fn, lineno))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050071 __inherit_cache.append( file )
72 d.setVar('__inherit_cache', __inherit_cache)
Patrick Williams92b42cb2022-09-03 06:53:57 -050073 try:
74 bb.parse.handle(file, d, True)
75 except (IOError, OSError) as exc:
76 raise ParseError("Could not inherit file %s: %s" % (fn, exc.strerror), fn, lineno)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050077 __inherit_cache = d.getVar('__inherit_cache', False) or []
78
79def get_statements(filename, absolute_filename, base_name):
80 global cached_statements
81
82 try:
83 return cached_statements[absolute_filename]
84 except KeyError:
Brad Bishop6e60e8b2018-02-01 10:27:11 -050085 with open(absolute_filename, 'r') as f:
86 statements = ast.StatementGroup()
Patrick Williamsc124f4f2015-09-15 14:41:29 -050087
Brad Bishop6e60e8b2018-02-01 10:27:11 -050088 lineno = 0
89 while True:
90 lineno = lineno + 1
91 s = f.readline()
92 if not s: break
93 s = s.rstrip()
94 feeder(lineno, s, filename, base_name, statements)
95
Patrick Williamsc124f4f2015-09-15 14:41:29 -050096 if __inpython__:
97 # add a blank line to close out any python definition
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050098 feeder(lineno, "", filename, base_name, statements, eof=True)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050099
100 if filename.endswith(".bbclass") or filename.endswith(".inc"):
101 cached_statements[absolute_filename] = statements
102 return statements
103
104def handle(fn, d, include):
105 global __func_start_regexp__, __inherit_regexp__, __export_func_regexp__, __addtask_regexp__, __addhandler_regexp__, __infunc__, __body__, __residue__, __classname__
106 __body__ = []
107 __infunc__ = []
108 __classname__ = ""
109 __residue__ = []
110
111 base_name = os.path.basename(fn)
112 (root, ext) = os.path.splitext(base_name)
113 init(d)
114
115 if ext == ".bbclass":
116 __classname__ = root
117 __inherit_cache = d.getVar('__inherit_cache', False) or []
118 if not fn in __inherit_cache:
119 __inherit_cache.append(fn)
120 d.setVar('__inherit_cache', __inherit_cache)
121
122 if include != 0:
123 oldfile = d.getVar('FILE', False)
124 else:
125 oldfile = None
126
127 abs_fn = resolve_file(fn, d)
128
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500129 # actual loading
130 statements = get_statements(fn, abs_fn, base_name)
131
132 # DONE WITH PARSING... time to evaluate
133 if ext != ".bbclass" and abs_fn != oldfile:
134 d.setVar('FILE', abs_fn)
135
136 try:
137 statements.eval(d)
138 except bb.parse.SkipRecipe:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500139 d.setVar("__SKIPPED", True)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500140 if include == 0:
141 return { "" : d }
142
143 if __infunc__:
144 raise ParseError("Shell function %s is never closed" % __infunc__[0], __infunc__[1], __infunc__[2])
145 if __residue__:
146 raise ParseError("Leftover unparsed (incomplete?) data %s from %s" % __residue__, fn)
147
148 if ext != ".bbclass" and include == 0:
149 return ast.multi_finalize(fn, d)
150
151 if ext != ".bbclass" and oldfile and abs_fn != oldfile:
152 d.setVar("FILE", oldfile)
153
154 return d
155
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500156def feeder(lineno, s, fn, root, statements, eof=False):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500157 global __func_start_regexp__, __inherit_regexp__, __export_func_regexp__, __addtask_regexp__, __addhandler_regexp__, __def_regexp__, __python_func_regexp__, __inpython__, __infunc__, __body__, bb, __residue__, __classname__
Brad Bishop19323692019-04-05 15:28:33 -0400158
159 # Check tabs in python functions:
160 # - def py_funcname(): covered by __inpython__
161 # - python(): covered by '__anonymous' == __infunc__[0]
162 # - python funcname(): covered by __infunc__[3]
163 if __inpython__ or (__infunc__ and ('__anonymous' == __infunc__[0] or __infunc__[3])):
164 tab = __python_tab_regexp__.match(s)
165 if tab:
166 bb.warn('python should use 4 spaces indentation, but found tabs in %s, line %s' % (root, lineno))
167
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500168 if __infunc__:
169 if s == '}':
170 __body__.append('')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500171 ast.handleMethod(statements, fn, lineno, __infunc__[0], __body__, __infunc__[3], __infunc__[4])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500172 __infunc__ = []
173 __body__ = []
174 else:
175 __body__.append(s)
176 return
177
178 if __inpython__:
179 m = __python_func_regexp__.match(s)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500180 if m and not eof:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500181 __body__.append(s)
182 return
183 else:
184 ast.handlePythonMethod(statements, fn, lineno, __inpython__,
185 root, __body__)
186 __body__ = []
187 __inpython__ = False
188
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500189 if eof:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500190 return
191
192 if s and s[0] == '#':
193 if len(__residue__) != 0 and __residue__[0][0] != "#":
Andrew Geissler615f2f12022-07-15 14:00:58 -0500194 bb.fatal("There is a comment on line %s of file %s:\n'''\n%s\n'''\nwhich is in the middle of a multiline expression. This syntax is invalid, please correct it." % (lineno, fn, s))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500195
196 if len(__residue__) != 0 and __residue__[0][0] == "#" and (not s or s[0] != "#"):
Andrew Geissler615f2f12022-07-15 14:00:58 -0500197 bb.fatal("There is a confusing multiline partially commented expression on line %s of file %s:\n%s\nPlease clarify whether this is all a comment or should be parsed." % (lineno - len(__residue__), fn, "\n".join(__residue__)))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500198
199 if s and s[-1] == '\\':
200 __residue__.append(s[:-1])
201 return
202
203 s = "".join(__residue__) + s
204 __residue__ = []
205
206 # Skip empty lines
207 if s == '':
208 return
209
210 # Skip comments
211 if s[0] == '#':
212 return
213
214 m = __func_start_regexp__.match(s)
215 if m:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500216 __infunc__ = [m.group("func") or "__anonymous", fn, lineno, m.group("py") is not None, m.group("fr") is not None]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500217 return
218
219 m = __def_regexp__.match(s)
220 if m:
221 __body__.append(s)
222 __inpython__ = m.group(1)
223
224 return
225
226 m = __export_func_regexp__.match(s)
227 if m:
228 ast.handleExportFuncs(statements, fn, lineno, m, __classname__)
229 return
230
231 m = __addtask_regexp__.match(s)
232 if m:
Brad Bishopc342db32019-05-15 21:57:59 -0400233 if len(m.group().split()) == 2:
234 # Check and warn for "addtask task1 task2"
235 m2 = re.match(r"addtask\s+(?P<func>\w+)(?P<ignores>.*)", s)
236 if m2 and m2.group('ignores'):
237 logger.warning('addtask ignored: "%s"' % m2.group('ignores'))
238
239 # Check and warn for "addtask task1 before task2 before task3", the
240 # similar to "after"
241 taskexpression = s.split()
242 for word in ('before', 'after'):
243 if taskexpression.count(word) > 1:
244 logger.warning("addtask contained multiple '%s' keywords, only one is supported" % word)
245
Andrew Geissler6ce62a22020-11-30 19:58:47 -0600246 # Check and warn for having task with exprssion as part of task name
247 for te in taskexpression:
248 if any( ( "%s_" % keyword ) in te for keyword in bb.data_smart.__setvar_keyword__ ):
249 raise ParseError("Task name '%s' contains a keyword which is not recommended/supported.\nPlease rename the task not to include the keyword.\n%s" % (te, ("\n".join(map(str, bb.data_smart.__setvar_keyword__)))), fn)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500250 ast.handleAddTask(statements, fn, lineno, m)
251 return
252
253 m = __deltask_regexp__.match(s)
254 if m:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500255 ast.handleDelTask(statements, fn, lineno, m)
256 return
257
258 m = __addhandler_regexp__.match(s)
259 if m:
260 ast.handleBBHandlers(statements, fn, lineno, m)
261 return
262
263 m = __inherit_regexp__.match(s)
264 if m:
265 ast.handleInherit(statements, fn, lineno, m)
266 return
267
268 return ConfHandler.feeder(lineno, s, fn, statements)
269
270# Add us to the handlers list
271from .. import handlers
272handlers.append({'supports': supports, 'handle': handle, 'init': init})
273del handlers