-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbbclasses
More file actions
executable file
·466 lines (389 loc) · 14.3 KB
/
bbclasses
File metadata and controls
executable file
·466 lines (389 loc) · 14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
#!/usr/bin/env python3
"""Generate a Mermaid dependency graph of all files involved in building a BitBake recipe.
Uses bb.tinfoil to parse the recipe and discover nodes (files), then manually parses
those files to discover edges (inherit/require/include relationships).
"""
import argparse
import logging
import os
import re
import sys
def parse_args():
parser = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
)
parser.add_argument(
"recipe",
help="Recipe name (e.g. 'gzip') or path to a .bb file",
)
parser.add_argument(
"--no-filter-base",
"-n",
action="store_true",
help="Disable filtering of base BitBake infrastructure noise",
)
parser.add_argument(
"--group-by-layer",
"-g",
action="store_true",
help="Group files into Mermaid subgraphs by meta-layer",
)
parser.add_argument(
"--log-level",
"-l",
type=str,
default="INFO",
choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
help="Set the logging output level. Defaults to INFO.",
)
parser.add_argument(
"--output",
"-o",
type=str,
default=None,
help="Output file. Default: stdout",
)
return parser.parse_args()
def parse_recipe(tinfoil, recipe):
"""Parse a recipe by name or path, returning its datastore."""
if os.path.exists(recipe):
logging.info("Parsing recipe file: %s", recipe)
d = tinfoil.parse_recipe_file(recipe)
else:
logging.info("Parsing recipe: %s", recipe)
d = tinfoil.parse_recipe(recipe)
if d is None:
sys.exit(f"Failed to parse recipe: {recipe}")
return d
def discover_nodes(tinfoil, d):
"""Collect the set of all files involved in a recipe from the datastore."""
recipe_file = d.getVar("FILE")
logging.info("Recipe file: %s", recipe_file)
inherit_cache = d.getVar("__inherit_cache") or []
logging.info("Found %d inherited bbclasses", len(inherit_cache))
for path in inherit_cache:
logging.debug(" bbclass: %s", path)
depends = d.getVar("__depends") or []
dep_files = [
filepath
for filepath, _mtime in depends
if any(filepath.endswith(ext) for ext in (".bb", ".inc", ".bbappend"))
]
logging.info("Found %d file dependencies", len(dep_files))
for path in dep_files:
logging.debug(" dep: %s", path)
appends = tinfoil.get_file_appends(recipe_file)
logging.info("Found %d appends", len(appends))
for path in appends:
logging.debug(" append: %s", path)
global_inherits = (d.getVar("INHERIT") or "").split()
logging.info("Found %d global INHERIT classes", len(global_inherits))
for name in global_inherits:
logging.debug(" INHERIT: %s", name)
bblayers = (d.getVar("BBLAYERS") or "").split()
common_prefix = os.path.commonpath(bblayers) + "/" if bblayers else "/"
logging.info("Common layer prefix: %s", common_prefix)
all_paths = {recipe_file}
all_paths.update(inherit_cache)
all_paths.update(dep_files)
all_paths.update(appends)
exists = {p for p in all_paths if os.path.isfile(p)}
skipped = all_paths - exists
if skipped:
logging.debug("Skipped %d non-existent files", len(skipped))
for path in sorted(skipped):
logging.debug(" skipped: %s", path)
inherit_cache = [p for p in inherit_cache if p in exists]
dep_files = [p for p in dep_files if p in exists]
appends = [p for p in appends if p in exists]
logging.info("Total node count: %d", len(exists))
return (
exists,
recipe_file,
inherit_cache,
dep_files,
appends,
global_inherits,
bblayers,
common_prefix,
)
# Edge discovery helpers
def preprocess(text):
"""Prepare file contents for directive scanning.
Strips comment lines and joins backslash-continuation lines.
"""
lines = []
for line in text.splitlines():
stripped = line.lstrip()
if stripped.startswith("#"):
continue
lines.append(line)
joined = "\n".join(lines)
return joined.replace("\\\n", " ")
def build_class_lookup(inherit_cache):
"""Build a dict mapping bare class names to full paths.
e.g. {"native": "/path/to/meta/classes/native.bbclass", ...}
"""
lookup = {}
for path in inherit_cache:
basename = os.path.basename(path)
name = basename.removesuffix(".bbclass")
lookup[name] = path
return lookup
def expand_names(token, d):
"""Expand a token that may contain ${...} variable references.
Returns a list of individual names after expansion and splitting.
"""
if "$" not in token:
return [token]
try:
expanded = d.expand(token)
except Exception:
logging.warning("Could not expand variable: %s", token)
return []
return expanded.split()
def resolve_inherit(name, class_lookup):
"""Resolve a single inherit class name to its full path, or None."""
result = class_lookup.get(name)
if result is None:
logging.warning("Could not resolve inherit: %s", name)
return result
def resolve_include(ref, nodes, d):
"""Resolve a require/include path to a node in the set by suffix match, or None."""
if "$" in ref:
try:
ref = d.expand(ref)
except Exception:
logging.warning("Could not expand variable in include: %s", ref)
return None
matches = [n for n in nodes if n.endswith("/" + ref) or n == ref]
if len(matches) == 1:
return matches[0]
if len(matches) > 1:
logging.warning("Ambiguous include ref '%s', matched %d nodes", ref, len(matches))
else:
logging.warning("Could not resolve include: %s", ref)
return None
INHERIT_RE = re.compile(r"^(inherit(?:_defer)?)\s+(.+)", re.MULTILINE)
REQUIRE_INCLUDE_RE = re.compile(r"^(require|include)\s+(.+)", re.MULTILINE)
def split_tokens(text):
"""Split a string on whitespace, respecting ${...} brace nesting."""
tokens = []
current = []
depth = 0
for char in text:
if char == "$" or (depth > 0 and char == "{"):
if char == "{":
depth += 1
current.append(char)
elif depth > 0 and char == "}":
depth -= 1
current.append(char)
elif depth == 0 and char in (" ", "\t"):
if current:
tokens.append("".join(current))
current = []
else:
if char == "{" and current and current[-1] == "$":
depth += 1
current.append(char)
if current:
tokens.append("".join(current))
return tokens
def scan_file(source, nodes, class_lookup, d):
"""Parse a single file for inherit/require/include directives, returning edges."""
edges = []
try:
text = preprocess(open(source).read())
except OSError:
logging.warning("Could not read file: %s", source)
return edges
for match in INHERIT_RE.finditer(text):
edge_type = match.group(1)
for token in split_tokens(match.group(2)):
for name in expand_names(token, d):
target = resolve_inherit(name, class_lookup)
if target and target in nodes:
edges.append((target, source, edge_type))
for match in REQUIRE_INCLUDE_RE.finditer(text):
edge_type = match.group(1)
ref = match.group(2).strip()
target = resolve_include(ref, nodes, d)
if target:
edges.append((target, source, edge_type))
return edges
def discover_edges(nodes, recipe_file, inherit_cache, depends, appends, global_inherits, d):
"""Discover edges by scanning the recipe, its inherited classes, and its dependencies."""
class_lookup = build_class_lookup(inherit_cache)
edges = []
# Global INHERIT classes (from local.conf / bitbake.conf / layer.conf)
for token in global_inherits:
for name in expand_names(token, d):
target = resolve_inherit(name, class_lookup)
if target and target in nodes:
edges.append((target, recipe_file, "INHERIT"))
# Scan the recipe file itself
edges.extend(scan_file(recipe_file, nodes, class_lookup, d))
# Scan each inherited bbclass
for path in inherit_cache:
edges.extend(scan_file(path, nodes, class_lookup, d))
# Scan each file dependency (.bb, .inc, .bbappend)
for path in depends:
edges.extend(scan_file(path, nodes, class_lookup, d))
# Synthetic appends edges
for append in appends:
if append in nodes:
edges.append((recipe_file, append, "appends"))
return edges
BASE_FILTER_BASENAMES = {
"base.bbclass",
"bblayers.conf",
"bitbake.conf",
"insane.bbclass",
"license.bbclass",
"local.conf",
"logging.bbclass",
"metadata_scm.bbclass",
"mirrors.bbclass",
"patch.bbclass",
"sanity.bbclass",
"sstate.bbclass",
"staging.bbclass",
"utility-tasks.bbclass",
"utils.bbclass",
}
def filter_base_infrastructure(nodes, edges):
"""Remove base BitBake infrastructure files and their edges."""
filtered_nodes = set()
for path in nodes:
basename = os.path.basename(path)
if basename in BASE_FILTER_BASENAMES:
logging.debug("Filtering base infrastructure: %s", path)
continue
if path.endswith(".conf"):
logging.debug("Filtering conf file: %s", path)
continue
filtered_nodes.add(path)
filtered_edges = [
(src, tgt, etype)
for src, tgt, etype in edges
if src in filtered_nodes and tgt in filtered_nodes
]
return filtered_nodes, filtered_edges
# Mermaid output helpers
_SANITIZE_RE = re.compile(r"[^a-zA-Z0-9_./\-]")
def sanitize_mermaid_id(path):
"""Convert a file path into a valid Mermaid node ID."""
node_id = _SANITIZE_RE.sub("_", path)
if node_id and node_id[0].isdigit():
node_id = "_" + node_id
return node_id
def escape_label(text):
"""Escape a label for use inside Mermaid quoted strings."""
return text.replace("&", "&").replace('"', """)
def strip_prefix(path, prefix):
"""Strip a common prefix from a path, returning the relative remainder."""
if path.startswith(prefix):
return path[len(prefix) :]
return path
def node_shape(path, label):
"""Return the Mermaid node definition with the appropriate shape."""
escaped = escape_label(label)
if path.endswith(".bb"):
return f'["{escaped}"]'
elif path.endswith(".bbclass"):
return f'{{{{"{escaped}"}}}}'
elif path.endswith(".bbappend"):
return f'(["{escaped}"])'
elif path.endswith(".inc"):
return f'[["{escaped}"]]'
else:
return f'["{escaped}"]'
def classify_layers(nodes, bblayers):
"""Group nodes by layer using longest-prefix match against BBLAYERS."""
layers = {}
for path in sorted(nodes):
best = None
for layer in bblayers:
if path.startswith(layer + "/") and (best is None or len(layer) > len(best)):
best = layer
layer_name = os.path.basename(best) if best else "other"
layers.setdefault(layer_name, []).append(path)
return layers
def emit_node(path, common_prefix, indent, out):
"""Emit a single Mermaid node definition."""
rel = strip_prefix(path, common_prefix)
node_id = sanitize_mermaid_id(rel)
shape = node_shape(rel, label=rel)
out.write(f"{indent}{node_id}{shape}\n")
def emit_mermaid(nodes, edges, common_prefix, bblayers, group_by_layer, out):
"""Write a Mermaid flowchart to the given file object."""
# Normal inheritance diagrams are TB, but LR diagrams render better when there's lots of nodes
out.write("flowchart LR\n")
if group_by_layer:
layers = classify_layers(nodes, bblayers)
for layer_name in sorted(layers):
paths = layers[layer_name]
if len(paths) > 1:
layer_id = sanitize_mermaid_id(layer_name)
out.write(f" subgraph {layer_id}[{layer_name}]\n")
for path in paths:
emit_node(path, common_prefix, " ", out)
out.write(" end\n")
else:
emit_node(paths[0], common_prefix, " ", out)
else:
for path in sorted(nodes):
emit_node(path, common_prefix, " ", out)
for source, target, edge_type in sorted(edges):
src_id = sanitize_mermaid_id(strip_prefix(source, common_prefix))
tgt_id = sanitize_mermaid_id(strip_prefix(target, common_prefix))
escaped_type = escape_label(edge_type)
out.write(f' {src_id} -->|"{escaped_type}"| {tgt_id}\n')
def main(args):
try:
import bb.tinfoil
except ImportError:
sys.exit(
"Could not import bb.tinfoil. "
"Make sure you have sourced the OE build environment "
"(e.g. 'source oe-init-build-env')."
)
saved_stdout = sys.stdout
sys.stdout = sys.stderr
try:
with bb.tinfoil.Tinfoil(output=sys.stderr) as tinfoil:
tinfoil.prepare(config_only=False)
d = parse_recipe(tinfoil, args.recipe)
(
nodes,
recipe_file,
inherit_cache,
depends,
appends,
global_inherits,
bblayers,
common_prefix,
) = discover_nodes(tinfoil, d)
edges = discover_edges(
nodes, recipe_file, inherit_cache, depends, appends, global_inherits, d
)
finally:
sys.stdout = saved_stdout
if not args.no_filter_base:
nodes, edges = filter_base_infrastructure(nodes, edges)
if args.output:
with open(args.output, "w") as f:
emit_mermaid(nodes, edges, common_prefix, bblayers, args.group_by_layer, f)
logging.info("Wrote output to %s", args.output)
else:
emit_mermaid(nodes, edges, common_prefix, bblayers, args.group_by_layer, sys.stdout)
if __name__ == "__main__":
args = parse_args()
logging.basicConfig(
format="%(asctime)s %(module)s %(levelname)s: %(message)s",
datefmt="%Y-%m-%dT%H:%M:%S%z",
level=args.log_level,
stream=sys.stderr,
)
main(args)