Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | """ |
| 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 Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 12 | # SPDX-License-Identifier: GPL-2.0-only |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 13 | # |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 14 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 15 | import re, bb, os |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 16 | import bb.build, bb.utils, bb.data_smart |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 17 | |
| 18 | from . import ConfHandler |
| 19 | from .. import resolve_file, ast, logger, ParseError |
| 20 | from .ConfHandler import include, init |
| 21 | |
Andrew Geissler | 5f35090 | 2021-07-23 13:09:54 -0400 | [diff] [blame] | 22 | __func_start_regexp__ = re.compile(r"(((?P<py>python(?=(\s|\()))|(?P<fr>fakeroot(?=\s)))\s*)*(?P<func>[\w\.\-\+\{\}\$:]+)?\s*\(\s*\)\s*{$" ) |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 23 | __inherit_regexp__ = re.compile(r"inherit\s+(.+)" ) |
Patrick Williams | 56b44a9 | 2024-01-19 08:49:29 -0600 | [diff] [blame] | 24 | __inherit_def_regexp__ = re.compile(r"inherit_defer\s+(.+)" ) |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 25 | __export_func_regexp__ = re.compile(r"EXPORT_FUNCTIONS\s+(.+)" ) |
| 26 | __addtask_regexp__ = re.compile(r"addtask\s+(?P<func>\w+)\s*((before\s*(?P<before>((.*(?=after))|(.*))))|(after\s*(?P<after>((.*(?=before))|(.*)))))*") |
Andrew Geissler | b7d2861 | 2020-07-24 16:15:54 -0500 | [diff] [blame] | 27 | __deltask_regexp__ = re.compile(r"deltask\s+(.+)") |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 28 | __addhandler_regexp__ = re.compile(r"addhandler\s+(.+)" ) |
| 29 | __def_regexp__ = re.compile(r"def\s+(\w+).*:" ) |
| 30 | __python_func_regexp__ = re.compile(r"(\s+.*)|(^$)|(^#)" ) |
| 31 | __python_tab_regexp__ = re.compile(r" *\t") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 32 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 33 | __infunc__ = [] |
| 34 | __inpython__ = False |
| 35 | __body__ = [] |
| 36 | __classname__ = "" |
Patrick Williams | 44b3caf | 2024-04-12 16:51:14 -0500 | [diff] [blame^] | 37 | __residue__ = [] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 38 | |
| 39 | cached_statements = {} |
| 40 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 41 | def supports(fn, d): |
| 42 | """Return True if fn has a supported extension""" |
| 43 | return os.path.splitext(fn)[-1] in [".bb", ".bbclass", ".inc"] |
| 44 | |
Patrick Williams | 56b44a9 | 2024-01-19 08:49:29 -0600 | [diff] [blame] | 45 | def inherit(files, fn, lineno, d, deferred=False): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 46 | __inherit_cache = d.getVar('__inherit_cache', False) or [] |
Patrick Williams | 56b44a9 | 2024-01-19 08:49:29 -0600 | [diff] [blame] | 47 | #if "${" in files and not deferred: |
| 48 | # bb.warn("%s:%s has non deferred conditional inherit" % (fn, lineno)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 49 | files = d.expand(files).split() |
| 50 | for file in files: |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 51 | classtype = d.getVar("__bbclasstype", False) |
| 52 | origfile = file |
| 53 | for t in ["classes-" + classtype, "classes"]: |
| 54 | file = origfile |
| 55 | if not os.path.isabs(file) and not file.endswith(".bbclass"): |
| 56 | file = os.path.join(t, '%s.bbclass' % file) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 57 | |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 58 | if not os.path.isabs(file): |
| 59 | bbpath = d.getVar("BBPATH") |
| 60 | abs_fn, attempts = bb.utils.which(bbpath, file, history=True) |
| 61 | for af in attempts: |
| 62 | if af != abs_fn: |
| 63 | bb.parse.mark_dependency(d, af) |
| 64 | if abs_fn: |
| 65 | file = abs_fn |
| 66 | |
| 67 | if os.path.exists(file): |
| 68 | break |
| 69 | |
| 70 | if not os.path.exists(file): |
| 71 | raise ParseError("Could not inherit file %s" % (file), fn, lineno) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 72 | |
| 73 | if not file in __inherit_cache: |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 74 | logger.debug("Inheriting %s (from %s:%d)" % (file, fn, lineno)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 75 | __inherit_cache.append( file ) |
| 76 | d.setVar('__inherit_cache', __inherit_cache) |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 77 | try: |
| 78 | bb.parse.handle(file, d, True) |
| 79 | except (IOError, OSError) as exc: |
| 80 | raise ParseError("Could not inherit file %s: %s" % (fn, exc.strerror), fn, lineno) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 81 | __inherit_cache = d.getVar('__inherit_cache', False) or [] |
| 82 | |
| 83 | def get_statements(filename, absolute_filename, base_name): |
Patrick Williams | 44b3caf | 2024-04-12 16:51:14 -0500 | [diff] [blame^] | 84 | global cached_statements, __residue__, __body__ |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 85 | |
| 86 | try: |
| 87 | return cached_statements[absolute_filename] |
| 88 | except KeyError: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 89 | with open(absolute_filename, 'r') as f: |
| 90 | statements = ast.StatementGroup() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 91 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 92 | lineno = 0 |
| 93 | while True: |
| 94 | lineno = lineno + 1 |
| 95 | s = f.readline() |
| 96 | if not s: break |
| 97 | s = s.rstrip() |
| 98 | feeder(lineno, s, filename, base_name, statements) |
| 99 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 100 | if __inpython__: |
| 101 | # add a blank line to close out any python definition |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 102 | feeder(lineno, "", filename, base_name, statements, eof=True) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 103 | |
Patrick Williams | 44b3caf | 2024-04-12 16:51:14 -0500 | [diff] [blame^] | 104 | if __residue__: |
| 105 | raise ParseError("Unparsed lines %s: %s" % (filename, str(__residue__)), filename, lineno) |
| 106 | if __body__: |
| 107 | raise ParseError("Unparsed lines from unclosed function %s: %s" % (filename, str(__body__)), filename, lineno) |
| 108 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 109 | if filename.endswith(".bbclass") or filename.endswith(".inc"): |
| 110 | cached_statements[absolute_filename] = statements |
| 111 | return statements |
| 112 | |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 113 | def handle(fn, d, include, baseconfig=False): |
| 114 | global __infunc__, __body__, __residue__, __classname__ |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 115 | __body__ = [] |
| 116 | __infunc__ = [] |
| 117 | __classname__ = "" |
| 118 | __residue__ = [] |
| 119 | |
| 120 | base_name = os.path.basename(fn) |
| 121 | (root, ext) = os.path.splitext(base_name) |
| 122 | init(d) |
| 123 | |
| 124 | if ext == ".bbclass": |
| 125 | __classname__ = root |
| 126 | __inherit_cache = d.getVar('__inherit_cache', False) or [] |
| 127 | if not fn in __inherit_cache: |
| 128 | __inherit_cache.append(fn) |
| 129 | d.setVar('__inherit_cache', __inherit_cache) |
| 130 | |
| 131 | if include != 0: |
| 132 | oldfile = d.getVar('FILE', False) |
| 133 | else: |
| 134 | oldfile = None |
| 135 | |
| 136 | abs_fn = resolve_file(fn, d) |
| 137 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 138 | # actual loading |
| 139 | statements = get_statements(fn, abs_fn, base_name) |
| 140 | |
| 141 | # DONE WITH PARSING... time to evaluate |
| 142 | if ext != ".bbclass" and abs_fn != oldfile: |
| 143 | d.setVar('FILE', abs_fn) |
| 144 | |
| 145 | try: |
| 146 | statements.eval(d) |
| 147 | except bb.parse.SkipRecipe: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 148 | d.setVar("__SKIPPED", True) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 149 | if include == 0: |
| 150 | return { "" : d } |
| 151 | |
| 152 | if __infunc__: |
| 153 | raise ParseError("Shell function %s is never closed" % __infunc__[0], __infunc__[1], __infunc__[2]) |
| 154 | if __residue__: |
| 155 | raise ParseError("Leftover unparsed (incomplete?) data %s from %s" % __residue__, fn) |
| 156 | |
| 157 | if ext != ".bbclass" and include == 0: |
| 158 | return ast.multi_finalize(fn, d) |
| 159 | |
| 160 | if ext != ".bbclass" and oldfile and abs_fn != oldfile: |
| 161 | d.setVar("FILE", oldfile) |
| 162 | |
| 163 | return d |
| 164 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 165 | def feeder(lineno, s, fn, root, statements, eof=False): |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 166 | global __inpython__, __infunc__, __body__, __residue__, __classname__ |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 167 | |
| 168 | # Check tabs in python functions: |
| 169 | # - def py_funcname(): covered by __inpython__ |
| 170 | # - python(): covered by '__anonymous' == __infunc__[0] |
| 171 | # - python funcname(): covered by __infunc__[3] |
| 172 | if __inpython__ or (__infunc__ and ('__anonymous' == __infunc__[0] or __infunc__[3])): |
| 173 | tab = __python_tab_regexp__.match(s) |
| 174 | if tab: |
| 175 | bb.warn('python should use 4 spaces indentation, but found tabs in %s, line %s' % (root, lineno)) |
| 176 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 177 | if __infunc__: |
| 178 | if s == '}': |
| 179 | __body__.append('') |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 180 | ast.handleMethod(statements, fn, lineno, __infunc__[0], __body__, __infunc__[3], __infunc__[4]) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 181 | __infunc__ = [] |
| 182 | __body__ = [] |
| 183 | else: |
| 184 | __body__.append(s) |
| 185 | return |
| 186 | |
| 187 | if __inpython__: |
| 188 | m = __python_func_regexp__.match(s) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 189 | if m and not eof: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 190 | __body__.append(s) |
| 191 | return |
| 192 | else: |
| 193 | ast.handlePythonMethod(statements, fn, lineno, __inpython__, |
| 194 | root, __body__) |
| 195 | __body__ = [] |
| 196 | __inpython__ = False |
| 197 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 198 | if eof: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 199 | return |
| 200 | |
| 201 | if s and s[0] == '#': |
| 202 | if len(__residue__) != 0 and __residue__[0][0] != "#": |
Andrew Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame] | 203 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 204 | |
| 205 | if len(__residue__) != 0 and __residue__[0][0] == "#" and (not s or s[0] != "#"): |
Andrew Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame] | 206 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 207 | |
| 208 | if s and s[-1] == '\\': |
| 209 | __residue__.append(s[:-1]) |
| 210 | return |
| 211 | |
| 212 | s = "".join(__residue__) + s |
| 213 | __residue__ = [] |
| 214 | |
| 215 | # Skip empty lines |
| 216 | if s == '': |
| 217 | return |
| 218 | |
| 219 | # Skip comments |
| 220 | if s[0] == '#': |
| 221 | return |
| 222 | |
| 223 | m = __func_start_regexp__.match(s) |
| 224 | if m: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 225 | __infunc__ = [m.group("func") or "__anonymous", fn, lineno, m.group("py") is not None, m.group("fr") is not None] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 226 | return |
| 227 | |
| 228 | m = __def_regexp__.match(s) |
| 229 | if m: |
| 230 | __body__.append(s) |
| 231 | __inpython__ = m.group(1) |
| 232 | |
| 233 | return |
| 234 | |
| 235 | m = __export_func_regexp__.match(s) |
| 236 | if m: |
| 237 | ast.handleExportFuncs(statements, fn, lineno, m, __classname__) |
| 238 | return |
| 239 | |
| 240 | m = __addtask_regexp__.match(s) |
| 241 | if m: |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 242 | if len(m.group().split()) == 2: |
| 243 | # Check and warn for "addtask task1 task2" |
| 244 | m2 = re.match(r"addtask\s+(?P<func>\w+)(?P<ignores>.*)", s) |
| 245 | if m2 and m2.group('ignores'): |
| 246 | logger.warning('addtask ignored: "%s"' % m2.group('ignores')) |
| 247 | |
| 248 | # Check and warn for "addtask task1 before task2 before task3", the |
| 249 | # similar to "after" |
| 250 | taskexpression = s.split() |
| 251 | for word in ('before', 'after'): |
| 252 | if taskexpression.count(word) > 1: |
| 253 | logger.warning("addtask contained multiple '%s' keywords, only one is supported" % word) |
| 254 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 255 | # Check and warn for having task with exprssion as part of task name |
| 256 | for te in taskexpression: |
| 257 | if any( ( "%s_" % keyword ) in te for keyword in bb.data_smart.__setvar_keyword__ ): |
| 258 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 259 | ast.handleAddTask(statements, fn, lineno, m) |
| 260 | return |
| 261 | |
| 262 | m = __deltask_regexp__.match(s) |
| 263 | if m: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 264 | ast.handleDelTask(statements, fn, lineno, m) |
| 265 | return |
| 266 | |
| 267 | m = __addhandler_regexp__.match(s) |
| 268 | if m: |
| 269 | ast.handleBBHandlers(statements, fn, lineno, m) |
| 270 | return |
| 271 | |
| 272 | m = __inherit_regexp__.match(s) |
| 273 | if m: |
| 274 | ast.handleInherit(statements, fn, lineno, m) |
| 275 | return |
| 276 | |
Patrick Williams | 56b44a9 | 2024-01-19 08:49:29 -0600 | [diff] [blame] | 277 | m = __inherit_def_regexp__.match(s) |
| 278 | if m: |
| 279 | ast.handleInheritDeferred(statements, fn, lineno, m) |
| 280 | return |
| 281 | |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 282 | return ConfHandler.feeder(lineno, s, fn, statements, conffile=False) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 283 | |
| 284 | # Add us to the handlers list |
| 285 | from .. import handlers |
| 286 | handlers.append({'supports': supports, 'handle': handle, 'init': init}) |
| 287 | del handlers |