blob: 4cd82f115b42b4dd5ba203301c137fec38b585be [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#
Brad Bishopc342db32019-05-15 21:57:59 -040012# SPDX-License-Identifier: GPL-2.0-only
Patrick Williamsc124f4f2015-09-15 14:41:29 -050013#
14# Based on functions from the base bb module, Copyright 2003 Holger Schurig
Brad Bishopc342db32019-05-15 21:57:59 -040015#
Patrick Williamsc124f4f2015-09-15 14:41:29 -050016
17handlers = []
18
19import errno
20import logging
21import os
22import stat
23import bb
24import bb.utils
25import bb.siggen
26
27logger = logging.getLogger("BitBake.Parsing")
28
29class ParseError(Exception):
30 """Exception raised when parsing fails"""
31 def __init__(self, msg, filename, lineno=0):
32 self.msg = msg
33 self.filename = filename
34 self.lineno = lineno
35 Exception.__init__(self, msg, filename, lineno)
36
37 def __str__(self):
38 if self.lineno:
39 return "ParseError at %s:%d: %s" % (self.filename, self.lineno, self.msg)
40 else:
41 return "ParseError in %s: %s" % (self.filename, self.msg)
42
43class SkipRecipe(Exception):
44 """Exception raised to skip this recipe"""
45
46class SkipPackage(SkipRecipe):
47 """Exception raised to skip this recipe (use SkipRecipe in new code)"""
48
49__mtime_cache = {}
50def cached_mtime(f):
51 if f not in __mtime_cache:
52 __mtime_cache[f] = os.stat(f)[stat.ST_MTIME]
53 return __mtime_cache[f]
54
55def cached_mtime_noerror(f):
56 if f not in __mtime_cache:
57 try:
58 __mtime_cache[f] = os.stat(f)[stat.ST_MTIME]
59 except OSError:
60 return 0
61 return __mtime_cache[f]
62
63def update_mtime(f):
64 try:
65 __mtime_cache[f] = os.stat(f)[stat.ST_MTIME]
66 except OSError:
67 if f in __mtime_cache:
68 del __mtime_cache[f]
69 return 0
70 return __mtime_cache[f]
71
72def update_cache(f):
73 if f in __mtime_cache:
Andrew Geisslerd1e89492021-02-12 15:35:20 -060074 logger.debug("Updating mtime cache for %s" % f)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050075 update_mtime(f)
76
Brad Bishopd7bf8c12018-02-25 22:55:05 -050077def clear_cache():
78 global __mtime_cache
79 __mtime_cache = {}
80
Patrick Williamsc124f4f2015-09-15 14:41:29 -050081def mark_dependency(d, f):
82 if f.startswith('./'):
83 f = "%s/%s" % (os.getcwd(), f[2:])
84 deps = (d.getVar('__depends', False) or [])
85 s = (f, cached_mtime_noerror(f))
86 if s not in deps:
87 deps.append(s)
88 d.setVar('__depends', deps)
89
90def check_dependency(d, f):
91 s = (f, cached_mtime_noerror(f))
92 deps = (d.getVar('__depends', False) or [])
93 return s in deps
94
95def supports(fn, data):
96 """Returns true if we have a handler for this file, false otherwise"""
97 for h in handlers:
98 if h['supports'](fn, data):
99 return 1
100 return 0
101
Andrew Geissler517393d2023-01-13 08:55:19 -0600102def handle(fn, data, include=0, baseconfig=False):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500103 """Call the handler that is appropriate for this file"""
104 for h in handlers:
105 if h['supports'](fn, data):
106 with data.inchistory.include(fn):
Andrew Geissler517393d2023-01-13 08:55:19 -0600107 return h['handle'](fn, data, include, baseconfig)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500108 raise ParseError("not a BitBake file", fn)
109
110def init(fn, data):
111 for h in handlers:
112 if h['supports'](fn):
113 return h['init'](data)
114
115def init_parser(d):
Patrick Williamsde0582f2022-04-08 10:23:27 -0500116 if hasattr(bb.parse, "siggen"):
117 bb.parse.siggen.exit()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500118 bb.parse.siggen = bb.siggen.init(d)
119
120def resolve_file(fn, d):
121 if not os.path.isabs(fn):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500122 bbpath = d.getVar("BBPATH")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500123 newfn, attempts = bb.utils.which(bbpath, fn, history=True)
124 for af in attempts:
125 mark_dependency(d, af)
126 if not newfn:
127 raise IOError(errno.ENOENT, "file %s not found in %s" % (fn, bbpath))
128 fn = newfn
Brad Bishop316dfdd2018-06-25 12:45:53 -0400129 else:
130 mark_dependency(d, fn)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500131
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500132 if not os.path.isfile(fn):
133 raise IOError(errno.ENOENT, "file %s not found" % fn)
134
135 return fn
136
137# Used by OpenEmbedded metadata
138__pkgsplit_cache__={}
139def vars_from_file(mypkg, d):
140 if not mypkg or not mypkg.endswith((".bb", ".bbappend")):
141 return (None, None, None)
142 if mypkg in __pkgsplit_cache__:
143 return __pkgsplit_cache__[mypkg]
144
145 myfile = os.path.splitext(os.path.basename(mypkg))
146 parts = myfile[0].split('_')
147 __pkgsplit_cache__[mypkg] = parts
148 if len(parts) > 3:
149 raise ParseError("Unable to generate default variables from filename (too many underscores)", mypkg)
150 exp = 3 - len(parts)
151 tmplist = []
152 while exp != 0:
153 exp -= 1
154 tmplist.append(None)
155 parts.extend(tmplist)
156 return parts
157
158def get_file_depends(d):
159 '''Return the dependent files'''
160 dep_files = []
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500161 depends = d.getVar('__base_depends', False) or []
162 depends = depends + (d.getVar('__depends', False) or [])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500163 for (fn, _) in depends:
164 dep_files.append(os.path.abspath(fn))
165 return " ".join(dep_files)
166
167from bb.parse.parse_py import __version__, ConfHandler, BBHandler