blob: 2fc4002db51b04c4b9e7ce6c379f2e68f96cdaa6 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001"""
2BitBake Parsers
3
4File parsers for the BitBake build tools.
5
6"""
7
8
9# Copyright (C) 2003, 2004 Chris Larson
10# Copyright (C) 2003, 2004 Phil Blundell
11#
12# This program is free software; you can redistribute it and/or modify
13# it under the terms of the GNU General Public License version 2 as
14# published by the Free Software Foundation.
15#
16# This program is distributed in the hope that it will be useful,
17# but WITHOUT ANY WARRANTY; without even the implied warranty of
18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19# GNU General Public License for more details.
20#
21# You should have received a copy of the GNU General Public License along
22# with this program; if not, write to the Free Software Foundation, Inc.,
23# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24#
25# Based on functions from the base bb module, Copyright 2003 Holger Schurig
26
27handlers = []
28
29import errno
30import logging
31import os
32import stat
33import bb
34import bb.utils
35import bb.siggen
36
37logger = logging.getLogger("BitBake.Parsing")
38
39class ParseError(Exception):
40 """Exception raised when parsing fails"""
41 def __init__(self, msg, filename, lineno=0):
42 self.msg = msg
43 self.filename = filename
44 self.lineno = lineno
45 Exception.__init__(self, msg, filename, lineno)
46
47 def __str__(self):
48 if self.lineno:
49 return "ParseError at %s:%d: %s" % (self.filename, self.lineno, self.msg)
50 else:
51 return "ParseError in %s: %s" % (self.filename, self.msg)
52
53class SkipRecipe(Exception):
54 """Exception raised to skip this recipe"""
55
56class SkipPackage(SkipRecipe):
57 """Exception raised to skip this recipe (use SkipRecipe in new code)"""
58
59__mtime_cache = {}
60def cached_mtime(f):
61 if f not in __mtime_cache:
62 __mtime_cache[f] = os.stat(f)[stat.ST_MTIME]
63 return __mtime_cache[f]
64
65def cached_mtime_noerror(f):
66 if f not in __mtime_cache:
67 try:
68 __mtime_cache[f] = os.stat(f)[stat.ST_MTIME]
69 except OSError:
70 return 0
71 return __mtime_cache[f]
72
73def update_mtime(f):
74 try:
75 __mtime_cache[f] = os.stat(f)[stat.ST_MTIME]
76 except OSError:
77 if f in __mtime_cache:
78 del __mtime_cache[f]
79 return 0
80 return __mtime_cache[f]
81
82def update_cache(f):
83 if f in __mtime_cache:
84 logger.debug(1, "Updating mtime cache for %s" % f)
85 update_mtime(f)
86
Brad Bishopd7bf8c12018-02-25 22:55:05 -050087def clear_cache():
88 global __mtime_cache
89 __mtime_cache = {}
90
Patrick Williamsc124f4f2015-09-15 14:41:29 -050091def mark_dependency(d, f):
92 if f.startswith('./'):
93 f = "%s/%s" % (os.getcwd(), f[2:])
94 deps = (d.getVar('__depends', False) or [])
95 s = (f, cached_mtime_noerror(f))
96 if s not in deps:
97 deps.append(s)
98 d.setVar('__depends', deps)
99
100def check_dependency(d, f):
101 s = (f, cached_mtime_noerror(f))
102 deps = (d.getVar('__depends', False) or [])
103 return s in deps
104
105def supports(fn, data):
106 """Returns true if we have a handler for this file, false otherwise"""
107 for h in handlers:
108 if h['supports'](fn, data):
109 return 1
110 return 0
111
112def handle(fn, data, include = 0):
113 """Call the handler that is appropriate for this file"""
114 for h in handlers:
115 if h['supports'](fn, data):
116 with data.inchistory.include(fn):
117 return h['handle'](fn, data, include)
118 raise ParseError("not a BitBake file", fn)
119
120def init(fn, data):
121 for h in handlers:
122 if h['supports'](fn):
123 return h['init'](data)
124
125def init_parser(d):
126 bb.parse.siggen = bb.siggen.init(d)
127
128def resolve_file(fn, d):
129 if not os.path.isabs(fn):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500130 bbpath = d.getVar("BBPATH")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500131 newfn, attempts = bb.utils.which(bbpath, fn, history=True)
132 for af in attempts:
133 mark_dependency(d, af)
134 if not newfn:
135 raise IOError(errno.ENOENT, "file %s not found in %s" % (fn, bbpath))
136 fn = newfn
137
138 mark_dependency(d, fn)
139 if not os.path.isfile(fn):
140 raise IOError(errno.ENOENT, "file %s not found" % fn)
141
142 return fn
143
144# Used by OpenEmbedded metadata
145__pkgsplit_cache__={}
146def vars_from_file(mypkg, d):
147 if not mypkg or not mypkg.endswith((".bb", ".bbappend")):
148 return (None, None, None)
149 if mypkg in __pkgsplit_cache__:
150 return __pkgsplit_cache__[mypkg]
151
152 myfile = os.path.splitext(os.path.basename(mypkg))
153 parts = myfile[0].split('_')
154 __pkgsplit_cache__[mypkg] = parts
155 if len(parts) > 3:
156 raise ParseError("Unable to generate default variables from filename (too many underscores)", mypkg)
157 exp = 3 - len(parts)
158 tmplist = []
159 while exp != 0:
160 exp -= 1
161 tmplist.append(None)
162 parts.extend(tmplist)
163 return parts
164
165def get_file_depends(d):
166 '''Return the dependent files'''
167 dep_files = []
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500168 depends = d.getVar('__base_depends', False) or []
169 depends = depends + (d.getVar('__depends', False) or [])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500170 for (fn, _) in depends:
171 dep_files.append(os.path.abspath(fn))
172 return " ".join(dep_files)
173
174from bb.parse.parse_py import __version__, ConfHandler, BBHandler