blob: ec097baf73f48523d437cc5c7684a7d365af5dc0 [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
50
51__infunc__ = []
52__inpython__ = False
53__body__ = []
54__classname__ = ""
55
56cached_statements = {}
57
58# We need to indicate EOF to the feeder. This code is so messy that
59# factoring it out to a close_parse_file method is out of question.
60# We will use the IN_PYTHON_EOF as an indicator to just close the method
61#
62# The two parts using it are tightly integrated anyway
63IN_PYTHON_EOF = -9999999999999
64
65
66
67def supports(fn, d):
68 """Return True if fn has a supported extension"""
69 return os.path.splitext(fn)[-1] in [".bb", ".bbclass", ".inc"]
70
71def inherit(files, fn, lineno, d):
72 __inherit_cache = d.getVar('__inherit_cache', False) or []
73 files = d.expand(files).split()
74 for file in files:
75 if not os.path.isabs(file) and not file.endswith(".bbclass"):
76 file = os.path.join('classes', '%s.bbclass' % file)
77
78 if not os.path.isabs(file):
79 bbpath = d.getVar("BBPATH", True)
80 abs_fn, attempts = bb.utils.which(bbpath, file, history=True)
81 for af in attempts:
82 if af != abs_fn:
83 bb.parse.mark_dependency(d, af)
84 if abs_fn:
85 file = abs_fn
86
87 if not file in __inherit_cache:
88 logger.debug(1, "Inheriting %s (from %s:%d)" % (file, fn, lineno))
89 __inherit_cache.append( file )
90 d.setVar('__inherit_cache', __inherit_cache)
91 include(fn, file, lineno, d, "inherit")
92 __inherit_cache = d.getVar('__inherit_cache', False) or []
93
94def get_statements(filename, absolute_filename, base_name):
95 global cached_statements
96
97 try:
98 return cached_statements[absolute_filename]
99 except KeyError:
100 file = open(absolute_filename, 'r')
101 statements = ast.StatementGroup()
102
103 lineno = 0
104 while True:
105 lineno = lineno + 1
106 s = file.readline()
107 if not s: break
108 s = s.rstrip()
109 feeder(lineno, s, filename, base_name, statements)
110 file.close()
111 if __inpython__:
112 # add a blank line to close out any python definition
113 feeder(IN_PYTHON_EOF, "", filename, base_name, statements)
114
115 if filename.endswith(".bbclass") or filename.endswith(".inc"):
116 cached_statements[absolute_filename] = statements
117 return statements
118
119def handle(fn, d, include):
120 global __func_start_regexp__, __inherit_regexp__, __export_func_regexp__, __addtask_regexp__, __addhandler_regexp__, __infunc__, __body__, __residue__, __classname__
121 __body__ = []
122 __infunc__ = []
123 __classname__ = ""
124 __residue__ = []
125
126 base_name = os.path.basename(fn)
127 (root, ext) = os.path.splitext(base_name)
128 init(d)
129
130 if ext == ".bbclass":
131 __classname__ = root
132 __inherit_cache = d.getVar('__inherit_cache', False) or []
133 if not fn in __inherit_cache:
134 __inherit_cache.append(fn)
135 d.setVar('__inherit_cache', __inherit_cache)
136
137 if include != 0:
138 oldfile = d.getVar('FILE', False)
139 else:
140 oldfile = None
141
142 abs_fn = resolve_file(fn, d)
143
144 if include:
145 bb.parse.mark_dependency(d, abs_fn)
146
147 # actual loading
148 statements = get_statements(fn, abs_fn, base_name)
149
150 # DONE WITH PARSING... time to evaluate
151 if ext != ".bbclass" and abs_fn != oldfile:
152 d.setVar('FILE', abs_fn)
153
154 try:
155 statements.eval(d)
156 except bb.parse.SkipRecipe:
157 bb.data.setVar("__SKIPPED", True, d)
158 if include == 0:
159 return { "" : d }
160
161 if __infunc__:
162 raise ParseError("Shell function %s is never closed" % __infunc__[0], __infunc__[1], __infunc__[2])
163 if __residue__:
164 raise ParseError("Leftover unparsed (incomplete?) data %s from %s" % __residue__, fn)
165
166 if ext != ".bbclass" and include == 0:
167 return ast.multi_finalize(fn, d)
168
169 if ext != ".bbclass" and oldfile and abs_fn != oldfile:
170 d.setVar("FILE", oldfile)
171
172 return d
173
174def feeder(lineno, s, fn, root, statements):
175 global __func_start_regexp__, __inherit_regexp__, __export_func_regexp__, __addtask_regexp__, __addhandler_regexp__, __def_regexp__, __python_func_regexp__, __inpython__, __infunc__, __body__, bb, __residue__, __classname__
176 if __infunc__:
177 if s == '}':
178 __body__.append('')
179 ast.handleMethod(statements, fn, lineno, __infunc__[0], __body__)
180 __infunc__ = []
181 __body__ = []
182 else:
183 __body__.append(s)
184 return
185
186 if __inpython__:
187 m = __python_func_regexp__.match(s)
188 if m and lineno != IN_PYTHON_EOF:
189 __body__.append(s)
190 return
191 else:
192 ast.handlePythonMethod(statements, fn, lineno, __inpython__,
193 root, __body__)
194 __body__ = []
195 __inpython__ = False
196
197 if lineno == IN_PYTHON_EOF:
198 return
199
200 if s and s[0] == '#':
201 if len(__residue__) != 0 and __residue__[0][0] != "#":
202 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))
203
204 if len(__residue__) != 0 and __residue__[0][0] == "#" and (not s or s[0] != "#"):
205 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))
206
207 if s and s[-1] == '\\':
208 __residue__.append(s[:-1])
209 return
210
211 s = "".join(__residue__) + s
212 __residue__ = []
213
214 # Skip empty lines
215 if s == '':
216 return
217
218 # Skip comments
219 if s[0] == '#':
220 return
221
222 m = __func_start_regexp__.match(s)
223 if m:
224 __infunc__ = [m.group("func") or "__anonymous", fn, lineno]
225 ast.handleMethodFlags(statements, fn, lineno, __infunc__[0], m)
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:
242 ast.handleAddTask(statements, fn, lineno, m)
243 return
244
245 m = __deltask_regexp__.match(s)
246 if m:
247 ast.handleDelTask(statements, fn, lineno, m)
248 return
249
250 m = __addhandler_regexp__.match(s)
251 if m:
252 ast.handleBBHandlers(statements, fn, lineno, m)
253 return
254
255 m = __inherit_regexp__.match(s)
256 if m:
257 ast.handleInherit(statements, fn, lineno, m)
258 return
259
260 return ConfHandler.feeder(lineno, s, fn, statements)
261
262# Add us to the handlers list
263from .. import handlers
264handlers.append({'supports': supports, 'handle': handle, 'init': init})
265del handlers