blob: ef72c3700f52c7ed478caf650c4274bd81a8d2b5 [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
28from __future__ import absolute_import
29import 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
41__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("addtask\s+(?P<func>\w+)\s*((before\s*(?P<before>((.*(?=after))|(.*))))|(after\s*(?P<after>((.*(?=before))|(.*)))))*")
45__deltask_regexp__ = re.compile("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
Patrick Williamsc124f4f2015-09-15 14:41:29 -050050__infunc__ = []
51__inpython__ = False
52__body__ = []
53__classname__ = ""
54
55cached_statements = {}
56
Patrick Williamsc124f4f2015-09-15 14:41:29 -050057def supports(fn, d):
58 """Return True if fn has a supported extension"""
59 return os.path.splitext(fn)[-1] in [".bb", ".bbclass", ".inc"]
60
61def inherit(files, fn, lineno, d):
62 __inherit_cache = d.getVar('__inherit_cache', False) or []
63 files = d.expand(files).split()
64 for file in files:
65 if not os.path.isabs(file) and not file.endswith(".bbclass"):
66 file = os.path.join('classes', '%s.bbclass' % file)
67
68 if not os.path.isabs(file):
69 bbpath = d.getVar("BBPATH", True)
70 abs_fn, attempts = bb.utils.which(bbpath, file, history=True)
71 for af in attempts:
72 if af != abs_fn:
73 bb.parse.mark_dependency(d, af)
74 if abs_fn:
75 file = abs_fn
76
77 if not file in __inherit_cache:
78 logger.debug(1, "Inheriting %s (from %s:%d)" % (file, fn, lineno))
79 __inherit_cache.append( file )
80 d.setVar('__inherit_cache', __inherit_cache)
81 include(fn, file, lineno, d, "inherit")
82 __inherit_cache = d.getVar('__inherit_cache', False) or []
83
84def get_statements(filename, absolute_filename, base_name):
85 global cached_statements
86
87 try:
88 return cached_statements[absolute_filename]
89 except KeyError:
90 file = open(absolute_filename, 'r')
91 statements = ast.StatementGroup()
92
93 lineno = 0
94 while True:
95 lineno = lineno + 1
96 s = file.readline()
97 if not s: break
98 s = s.rstrip()
99 feeder(lineno, s, filename, base_name, statements)
100 file.close()
101 if __inpython__:
102 # add a blank line to close out any python definition
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500103 feeder(lineno, "", filename, base_name, statements, eof=True)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500104
105 if filename.endswith(".bbclass") or filename.endswith(".inc"):
106 cached_statements[absolute_filename] = statements
107 return statements
108
109def handle(fn, d, include):
110 global __func_start_regexp__, __inherit_regexp__, __export_func_regexp__, __addtask_regexp__, __addhandler_regexp__, __infunc__, __body__, __residue__, __classname__
111 __body__ = []
112 __infunc__ = []
113 __classname__ = ""
114 __residue__ = []
115
116 base_name = os.path.basename(fn)
117 (root, ext) = os.path.splitext(base_name)
118 init(d)
119
120 if ext == ".bbclass":
121 __classname__ = root
122 __inherit_cache = d.getVar('__inherit_cache', False) or []
123 if not fn in __inherit_cache:
124 __inherit_cache.append(fn)
125 d.setVar('__inherit_cache', __inherit_cache)
126
127 if include != 0:
128 oldfile = d.getVar('FILE', False)
129 else:
130 oldfile = None
131
132 abs_fn = resolve_file(fn, d)
133
134 if include:
135 bb.parse.mark_dependency(d, abs_fn)
136
137 # actual loading
138 statements = get_statements(fn, abs_fn, base_name)
139
140 # DONE WITH PARSING... time to evaluate
141 if ext != ".bbclass" and abs_fn != oldfile:
142 d.setVar('FILE', abs_fn)
143
144 try:
145 statements.eval(d)
146 except bb.parse.SkipRecipe:
147 bb.data.setVar("__SKIPPED", True, d)
148 if include == 0:
149 return { "" : d }
150
151 if __infunc__:
152 raise ParseError("Shell function %s is never closed" % __infunc__[0], __infunc__[1], __infunc__[2])
153 if __residue__:
154 raise ParseError("Leftover unparsed (incomplete?) data %s from %s" % __residue__, fn)
155
156 if ext != ".bbclass" and include == 0:
157 return ast.multi_finalize(fn, d)
158
159 if ext != ".bbclass" and oldfile and abs_fn != oldfile:
160 d.setVar("FILE", oldfile)
161
162 return d
163
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500164def feeder(lineno, s, fn, root, statements, eof=False):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500165 global __func_start_regexp__, __inherit_regexp__, __export_func_regexp__, __addtask_regexp__, __addhandler_regexp__, __def_regexp__, __python_func_regexp__, __inpython__, __infunc__, __body__, bb, __residue__, __classname__
166 if __infunc__:
167 if s == '}':
168 __body__.append('')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500169 ast.handleMethod(statements, fn, lineno, __infunc__[0], __body__, __infunc__[3], __infunc__[4])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500170 __infunc__ = []
171 __body__ = []
172 else:
173 __body__.append(s)
174 return
175
176 if __inpython__:
177 m = __python_func_regexp__.match(s)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500178 if m and not eof:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500179 __body__.append(s)
180 return
181 else:
182 ast.handlePythonMethod(statements, fn, lineno, __inpython__,
183 root, __body__)
184 __body__ = []
185 __inpython__ = False
186
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500187 if eof:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500188 return
189
190 if s and s[0] == '#':
191 if len(__residue__) != 0 and __residue__[0][0] != "#":
192 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))
193
194 if len(__residue__) != 0 and __residue__[0][0] == "#" and (not s or s[0] != "#"):
195 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))
196
197 if s and s[-1] == '\\':
198 __residue__.append(s[:-1])
199 return
200
201 s = "".join(__residue__) + s
202 __residue__ = []
203
204 # Skip empty lines
205 if s == '':
206 return
207
208 # Skip comments
209 if s[0] == '#':
210 return
211
212 m = __func_start_regexp__.match(s)
213 if m:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500214 __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 -0500215 return
216
217 m = __def_regexp__.match(s)
218 if m:
219 __body__.append(s)
220 __inpython__ = m.group(1)
221
222 return
223
224 m = __export_func_regexp__.match(s)
225 if m:
226 ast.handleExportFuncs(statements, fn, lineno, m, __classname__)
227 return
228
229 m = __addtask_regexp__.match(s)
230 if m:
231 ast.handleAddTask(statements, fn, lineno, m)
232 return
233
234 m = __deltask_regexp__.match(s)
235 if m:
236 ast.handleDelTask(statements, fn, lineno, m)
237 return
238
239 m = __addhandler_regexp__.match(s)
240 if m:
241 ast.handleBBHandlers(statements, fn, lineno, m)
242 return
243
244 m = __inherit_regexp__.match(s)
245 if m:
246 ast.handleInherit(statements, fn, lineno, m)
247 return
248
249 return ConfHandler.feeder(lineno, s, fn, statements)
250
251# Add us to the handlers list
252from .. import handlers
253handlers.append({'supports': supports, 'handle': handle, 'init': init})
254del handlers