blob: 9dba5f2334aa18f1694bba501ad37e7c32322402 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001#!/usr/bin/env python
2# ex:ts=4:sw=4:sts=4:et
3# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
4"""
5 class for handling .bb files
6
7 Reads a .bb file and obtains its metadata
8
9"""
10
11
12# Copyright (C) 2003, 2004 Chris Larson
13# Copyright (C) 2003, 2004 Phil Blundell
14#
15# This program is free software; you can redistribute it and/or modify
16# it under the terms of the GNU General Public License version 2 as
17# published by the Free Software Foundation.
18#
19# This program is distributed in the hope that it will be useful,
20# but WITHOUT ANY WARRANTY; without even the implied warranty of
21# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22# GNU General Public License for more details.
23#
24# You should have received a copy of the GNU General Public License along
25# with this program; if not, write to the Free Software Foundation, Inc.,
26# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27
Patrick Williamsc0f7c042017-02-23 20:41:17 -060028
Patrick Williamsc124f4f2015-09-15 14:41:29 -050029import re, bb, os
30import logging
31import bb.build, bb.utils
32from bb import data
33
34from . import ConfHandler
35from .. import resolve_file, ast, logger, ParseError
36from .ConfHandler import include, init
37
38# For compatibility
39bb.deprecate_import(__name__, "bb.parse", ["vars_from_file"])
40
Brad Bishop19323692019-04-05 15:28:33 -040041__func_start_regexp__ = re.compile(r"(((?P<py>python)|(?P<fr>fakeroot))\s*)*(?P<func>[\w\.\-\+\{\}\$]+)?\s*\(\s*\)\s*{$" )
42__inherit_regexp__ = re.compile(r"inherit\s+(.+)" )
43__export_func_regexp__ = re.compile(r"EXPORT_FUNCTIONS\s+(.+)" )
44__addtask_regexp__ = re.compile(r"addtask\s+(?P<func>\w+)\s*((before\s*(?P<before>((.*(?=after))|(.*))))|(after\s*(?P<after>((.*(?=before))|(.*)))))*")
45__deltask_regexp__ = re.compile(r"deltask\s+(?P<func>\w+)")
46__addhandler_regexp__ = re.compile(r"addhandler\s+(.+)" )
47__def_regexp__ = re.compile(r"def\s+(\w+).*:" )
48__python_func_regexp__ = re.compile(r"(\s+.*)|(^$)|(^#)" )
49__python_tab_regexp__ = re.compile(r" *\t")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050050
Patrick Williamsc124f4f2015-09-15 14:41:29 -050051__infunc__ = []
52__inpython__ = False
53__body__ = []
54__classname__ = ""
55
56cached_statements = {}
57
Patrick Williamsc124f4f2015-09-15 14:41:29 -050058def supports(fn, d):
59 """Return True if fn has a supported extension"""
60 return os.path.splitext(fn)[-1] in [".bb", ".bbclass", ".inc"]
61
62def inherit(files, fn, lineno, d):
63 __inherit_cache = d.getVar('__inherit_cache', False) or []
64 files = d.expand(files).split()
65 for file in files:
66 if not os.path.isabs(file) and not file.endswith(".bbclass"):
67 file = os.path.join('classes', '%s.bbclass' % file)
68
69 if not os.path.isabs(file):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050070 bbpath = d.getVar("BBPATH")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050071 abs_fn, attempts = bb.utils.which(bbpath, file, history=True)
72 for af in attempts:
73 if af != abs_fn:
74 bb.parse.mark_dependency(d, af)
75 if abs_fn:
76 file = abs_fn
77
78 if not file in __inherit_cache:
79 logger.debug(1, "Inheriting %s (from %s:%d)" % (file, fn, lineno))
80 __inherit_cache.append( file )
81 d.setVar('__inherit_cache', __inherit_cache)
82 include(fn, file, lineno, d, "inherit")
83 __inherit_cache = d.getVar('__inherit_cache', False) or []
84
85def get_statements(filename, absolute_filename, base_name):
86 global cached_statements
87
88 try:
89 return cached_statements[absolute_filename]
90 except KeyError:
Brad Bishop6e60e8b2018-02-01 10:27:11 -050091 with open(absolute_filename, 'r') as f:
92 statements = ast.StatementGroup()
Patrick Williamsc124f4f2015-09-15 14:41:29 -050093
Brad Bishop6e60e8b2018-02-01 10:27:11 -050094 lineno = 0
95 while True:
96 lineno = lineno + 1
97 s = f.readline()
98 if not s: break
99 s = s.rstrip()
100 feeder(lineno, s, filename, base_name, statements)
101
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500102 if __inpython__:
103 # add a blank line to close out any python definition
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500104 feeder(lineno, "", filename, base_name, statements, eof=True)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500105
106 if filename.endswith(".bbclass") or filename.endswith(".inc"):
107 cached_statements[absolute_filename] = statements
108 return statements
109
110def handle(fn, d, include):
111 global __func_start_regexp__, __inherit_regexp__, __export_func_regexp__, __addtask_regexp__, __addhandler_regexp__, __infunc__, __body__, __residue__, __classname__
112 __body__ = []
113 __infunc__ = []
114 __classname__ = ""
115 __residue__ = []
116
117 base_name = os.path.basename(fn)
118 (root, ext) = os.path.splitext(base_name)
119 init(d)
120
121 if ext == ".bbclass":
122 __classname__ = root
123 __inherit_cache = d.getVar('__inherit_cache', False) or []
124 if not fn in __inherit_cache:
125 __inherit_cache.append(fn)
126 d.setVar('__inherit_cache', __inherit_cache)
127
128 if include != 0:
129 oldfile = d.getVar('FILE', False)
130 else:
131 oldfile = None
132
133 abs_fn = resolve_file(fn, d)
134
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500135 # actual loading
136 statements = get_statements(fn, abs_fn, base_name)
137
138 # DONE WITH PARSING... time to evaluate
139 if ext != ".bbclass" and abs_fn != oldfile:
140 d.setVar('FILE', abs_fn)
141
142 try:
143 statements.eval(d)
144 except bb.parse.SkipRecipe:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500145 d.setVar("__SKIPPED", True)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500146 if include == 0:
147 return { "" : d }
148
149 if __infunc__:
150 raise ParseError("Shell function %s is never closed" % __infunc__[0], __infunc__[1], __infunc__[2])
151 if __residue__:
152 raise ParseError("Leftover unparsed (incomplete?) data %s from %s" % __residue__, fn)
153
154 if ext != ".bbclass" and include == 0:
155 return ast.multi_finalize(fn, d)
156
157 if ext != ".bbclass" and oldfile and abs_fn != oldfile:
158 d.setVar("FILE", oldfile)
159
160 return d
161
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500162def feeder(lineno, s, fn, root, statements, eof=False):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500163 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 -0400164
165 # Check tabs in python functions:
166 # - def py_funcname(): covered by __inpython__
167 # - python(): covered by '__anonymous' == __infunc__[0]
168 # - python funcname(): covered by __infunc__[3]
169 if __inpython__ or (__infunc__ and ('__anonymous' == __infunc__[0] or __infunc__[3])):
170 tab = __python_tab_regexp__.match(s)
171 if tab:
172 bb.warn('python should use 4 spaces indentation, but found tabs in %s, line %s' % (root, lineno))
173
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500174 if __infunc__:
175 if s == '}':
176 __body__.append('')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500177 ast.handleMethod(statements, fn, lineno, __infunc__[0], __body__, __infunc__[3], __infunc__[4])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500178 __infunc__ = []
179 __body__ = []
180 else:
181 __body__.append(s)
182 return
183
184 if __inpython__:
185 m = __python_func_regexp__.match(s)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500186 if m and not eof:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500187 __body__.append(s)
188 return
189 else:
190 ast.handlePythonMethod(statements, fn, lineno, __inpython__,
191 root, __body__)
192 __body__ = []
193 __inpython__ = False
194
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500195 if eof:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500196 return
197
198 if s and s[0] == '#':
199 if len(__residue__) != 0 and __residue__[0][0] != "#":
200 bb.fatal("There is a comment on line %s of file %s (%s) which is in the middle of a multiline expression.\nBitbake used to ignore these but no longer does so, please fix your metadata as errors are likely as a result of this change." % (lineno, fn, s))
201
202 if len(__residue__) != 0 and __residue__[0][0] == "#" and (not s or s[0] != "#"):
203 bb.fatal("There is a confusing multiline, partially commented expression on line %s of file %s (%s).\nPlease clarify whether this is all a comment or should be parsed." % (lineno, fn, s))
204
205 if s and s[-1] == '\\':
206 __residue__.append(s[:-1])
207 return
208
209 s = "".join(__residue__) + s
210 __residue__ = []
211
212 # Skip empty lines
213 if s == '':
214 return
215
216 # Skip comments
217 if s[0] == '#':
218 return
219
220 m = __func_start_regexp__.match(s)
221 if m:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500222 __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 -0500223 return
224
225 m = __def_regexp__.match(s)
226 if m:
227 __body__.append(s)
228 __inpython__ = m.group(1)
229
230 return
231
232 m = __export_func_regexp__.match(s)
233 if m:
234 ast.handleExportFuncs(statements, fn, lineno, m, __classname__)
235 return
236
237 m = __addtask_regexp__.match(s)
238 if m:
239 ast.handleAddTask(statements, fn, lineno, m)
240 return
241
242 m = __deltask_regexp__.match(s)
243 if m:
244 ast.handleDelTask(statements, fn, lineno, m)
245 return
246
247 m = __addhandler_regexp__.match(s)
248 if m:
249 ast.handleBBHandlers(statements, fn, lineno, m)
250 return
251
252 m = __inherit_regexp__.match(s)
253 if m:
254 ast.handleInherit(statements, fn, lineno, m)
255 return
256
257 return ConfHandler.feeder(lineno, s, fn, statements)
258
259# Add us to the handlers list
260from .. import handlers
261handlers.append({'supports': supports, 'handle': handle, 'init': init})
262del handlers