Elffuss BraidHub

kikocis/kleidiai public

main · 6.2 KiB · executableRawPermalink
1#!/usr/bin/env python3
2#
3# SPDX-FileCopyrightText: Copyright 2026 Arm Limited and/or its affiliates <open-source-office@arm.com>
4#
5# SPDX-License-Identifier: Apache-2.0
6"""Check the names of all microkernels."""
7import argparse
8import sys
9from pathlib import Path
10
11import naming.documentation as naming_documentation
12import naming.rules as naming
13import utils.git as git_utils
14import utils.microkernels as microkernels
15from naming.grammar import ParseFailure
16from naming.issues import KNOWN_DIRECTORY_PROBLEMS
17from naming.issues import KNOWN_UKERNEL_PROBLEMS
18from naming.issues import KnownIssue
19from utils.annotate import annotate
20
21
22def parse_arguments() -> argparse.Namespace:
23 parser = argparse.ArgumentParser(description=__doc__)
24 repo_root = git_utils.repo_root()
25
26 mode = parser.add_mutually_exclusive_group()
27 mode.add_argument(
28 "--dump-grammar",
29 action="store_true",
30 help="dump the naming grammar and exit",
31 )
32 mode.add_argument(
33 "--generate-documentation",
34 nargs="?",
35 type=Path,
36 const=repo_root / "docs" / "microkernel_names.md",
37 metavar="FILE",
38 help=(
39 "generate micro-kernel naming documentation and exit "
40 "(default: (repo_root)/docs/microkernel_names.md). Using `-` as path writes to stdout."
41 ),
42 )
43 mode.add_argument(
44 "--describe-ukernel",
45 metavar="NAME",
46 help="describe a micro-kernel name and exit",
47 )
48
49 parser.add_argument(
50 "--ukernels-dir",
51 type=Path,
52 help="ukernels directory to scan (default: (repo_root)/kai/ukernels)",
53 default=repo_root / "kai" / "ukernels",
54 )
55
56 parser.add_argument(
57 "--fail-fast",
58 action="store_true",
59 help="stop after the first invalid kernel or directory name",
60 )
61 parser.add_argument(
62 "--report-all",
63 "--report-known-issues",
64 action="store_true",
65 dest="report_known_issues",
66 help="report known invalid names instead of disregarding them",
67 )
68
69 return parser.parse_args()
70
71
72def report_invalid_name(name: str, parse_result: ParseFailure) -> None:
73 failure = parse_result.furthest_failure()
74 assert failure is not None
75
76 print(f" {name}")
77 print(f" {' ' * failure.pos}^")
78 print(f" expected: {failure.expected}")
79 print(f" remaining: {name[failure.pos:]}")
80
81
82def report_known_issue(issue: KnownIssue) -> None:
83 print(f" known issue: {issue.description}")
84 print(f" expected name: {issue.expected}")
85
86
87def report_invalid_kernel(
88 kernel: microkernels.Microkernel, parse_result: ParseFailure
89) -> None:
90 print(
91 "Invalid micro-kernel name in "
92 f"{kernel.operation}/{kernel.directory}: {kernel.name}"
93 )
94 report_invalid_name(kernel.name, parse_result)
95
96
97def report_invalid_directory(
98 kernel: microkernels.Microkernel, parse_result: ParseFailure
99) -> None:
100 print(f"Invalid micro-kernel directory in {kernel.operation}: {kernel.directory}")
101 report_invalid_name(kernel.directory, parse_result)
102
103
104def check_directory_tree(
105 directory: Path, fail_fast: bool, report_known_issues: bool
106) -> int:
107 assert directory.is_dir(), f"{directory} not found"
108 kernels = sorted(microkernels.gather_microkernels(directory))
109 misnamed_kernels = 0
110 misnamed_directories = 0
111 for kernel in kernels:
112 known_ukernel_issue = KNOWN_UKERNEL_PROBLEMS.get(kernel.name)
113 parse_result = naming.parse_kernel_name(kernel.name)
114 if not parse_result.parsed_ok and (
115 known_ukernel_issue is None or report_known_issues
116 ):
117 report_invalid_kernel(kernel, parse_result)
118 if known_ukernel_issue is not None:
119 report_known_issue(known_ukernel_issue)
120 misnamed_kernels += 1
121 if fail_fast:
122 return 1
123 elif known_ukernel_issue is not None and parse_result.parsed_ok:
124 print(
125 f"WARNING: {kernel.name} is listed as a known issue, but passes name check"
126 )
127
128 known_directory_issue = KNOWN_DIRECTORY_PROBLEMS.get(kernel.directory)
129 directory_parse_result = naming.parse_directory_name(kernel.directory)
130 if not directory_parse_result.parsed_ok and (
131 known_directory_issue is None or report_known_issues
132 ):
133 report_invalid_directory(kernel, directory_parse_result)
134 if known_directory_issue is not None:
135 report_known_issue(known_directory_issue)
136 misnamed_directories += 1
137 if fail_fast:
138 return 1
139 elif known_directory_issue is not None and directory_parse_result.parsed_ok:
140 print(
141 f"WARNING: {kernel.directory} is listed as a known issue, but passes name check"
142 )
143
144 # REVISIT, in the future we could compare directory name with microkernel name
145
146 print(f"There are {len(kernels)} checked kernels")
147 print(f"There are {misnamed_kernels} kernels with invalid names")
148 print(f"There are {misnamed_directories} kernels in invalid directories")
149 return 0 if misnamed_kernels == 0 and misnamed_directories == 0 else 1
150
151
152def generate_documentation(path: Path) -> None:
153 if path == Path("-"):
154 sys.stdout.write(naming_documentation.render_documentation())
155 return
156
157 path.write_text(naming_documentation.render_documentation(), encoding="utf-8")
158 print(f"Generated micro-kernel naming documentation: {path}")
159
160
161def describe_ukernel_name(name: str) -> int:
162 parse_result = naming.parse_kernel_name(name)
163 if not parse_result.parsed_ok:
164 print("Invalid micro-kernel name:")
165 report_invalid_name(name, parse_result)
166 return 1
167
168 print(annotate(name, parse_result.descriptions))
169 return 0
170
171
172def main() -> int:
173 args = parse_arguments()
174
175 if args.dump_grammar:
176 print(naming.grammar.to_grammar())
177 return 0
178
179 if args.generate_documentation is not None:
180 generate_documentation(args.generate_documentation)
181 return 0
182
183 if args.describe_ukernel is not None:
184 return describe_ukernel_name(args.describe_ukernel)
185
186 return check_directory_tree(
187 args.ukernels_dir, args.fail_fast, args.report_known_issues
188 )
189
190
191if __name__ == "__main__":
192 raise SystemExit(main())