blob: 67ec71f8663e34003ac01eb1bc68b279017fc161 [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
87def mark_dependency(d, f):
88 if f.startswith('./'):
89 f = "%s/%s" % (os.getcwd(), f[2:])
90 deps = (d.getVar('__depends', False) or [])
91 s = (f, cached_mtime_noerror(f))
92 if s not in deps:
93 deps.append(s)
94 d.setVar('__depends', deps)
95
96def check_dependency(d, f):
97 s = (f, cached_mtime_noerror(f))
98 deps = (d.getVar('__depends', False) or [])
99 return s in deps
100
101def supports(fn, data):
102 """Returns true if we have a handler for this file, false otherwise"""
103 for h in handlers:
104 if h['supports'](fn, data):
105 return 1
106 return 0
107
108def handle(fn, data, include = 0):
109 """Call the handler that is appropriate for this file"""
110 for h in handlers:
111 if h['supports'](fn, data):
112 with data.inchistory.include(fn):
113 return h['handle'](fn, data, include)
114 raise ParseError("not a BitBake file", fn)
115
116def init(fn, data):
117 for h in handlers:
118 if h['supports'](fn):
119 return h['init'](data)
120
121def init_parser(d):
122 bb.parse.siggen = bb.siggen.init(d)
123
124def resolve_file(fn, d):
125 if not os.path.isabs(fn):
126 bbpath = d.getVar("BBPATH", True)
127 newfn, attempts = bb.utils.which(bbpath, fn, history=True)
128 for af in attempts:
129 mark_dependency(d, af)
130 if not newfn:
131 raise IOError(errno.ENOENT, "file %s not found in %s" % (fn, bbpath))
132 fn = newfn
133
134 mark_dependency(d, fn)
135 if not os.path.isfile(fn):
136 raise IOError(errno.ENOENT, "file %s not found" % fn)
137
138 return fn
139
140# Used by OpenEmbedded metadata
141__pkgsplit_cache__={}
142def vars_from_file(mypkg, d):
143 if not mypkg or not mypkg.endswith((".bb", ".bbappend")):
144 return (None, None, None)
145 if mypkg in __pkgsplit_cache__:
146 return __pkgsplit_cache__[mypkg]
147
148 myfile = os.path.splitext(os.path.basename(mypkg))
149 parts = myfile[0].split('_')
150 __pkgsplit_cache__[mypkg] = parts
151 if len(parts) > 3:
152 raise ParseError("Unable to generate default variables from filename (too many underscores)", mypkg)
153 exp = 3 - len(parts)
154 tmplist = []
155 while exp != 0:
156 exp -= 1
157 tmplist.append(None)
158 parts.extend(tmplist)
159 return parts
160
161def get_file_depends(d):
162 '''Return the dependent files'''
163 dep_files = []
164 depends = d.getVar('__base_depends', True) or []
165 depends = depends + (d.getVar('__depends', True) or [])
166 for (fn, _) in depends:
167 dep_files.append(os.path.abspath(fn))
168 return " ".join(dep_files)
169
170from bb.parse.parse_py import __version__, ConfHandler, BBHandler