Skip to content

Commit 3c2887c

Browse files
lrgirdwoclaude
andcommitted
scripts: check_kernel_includes: check the sources for kernel headers
The kernel-only headers are moving to src/kernel/include, included as <sof/kernel/...>. Add a --sources mode that checks the source tree, without build directories, for such includes in user-space sources and in the user-space headers listed in the new [user-headers] manifest section: src/include and src/platform, apart from the kernel-only headers that are still there. This is the "no <sof/kernel/ in user-space code" rule of #11306. It also covers code that no checked build configuration compiles, and there is nothing to baseline: no user-space code includes them today. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Liam Girdwood <liam.r.girdwood@linux.intel.com>
1 parent 71d9f0c commit 3c2887c

2 files changed

Lines changed: 70 additions & 1 deletion

File tree

‎scripts/check_kernel_includes.py‎

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,20 @@
2424
checked builds are reported, so that they can be dropped from the list
2525
once they are clean in every configuration.
2626
27+
With --sources it checks the source tree instead of build directories:
28+
kernel-only headers in src/kernel/include are included as
29+
<sof/kernel/...>, and no user-space source or header may include one.
30+
This covers code that no checked build compiles, and has no baseline.
31+
2732
Usage:
2833
check_kernel_includes.py [--strict] [--summary] <build-dir> [<build-dir>...]
34+
check_kernel_includes.py --sources [--strict]
2935
"""
3036

3137
import argparse
3238
import fnmatch
3339
import os
40+
import re
3441
import subprocess
3542
import sys
3643
from collections import defaultdict
@@ -122,9 +129,50 @@ def check_build(build_dir, manifest, sof_root, zephyr_base):
122129
return leaks, built
123130

124131

132+
KERNEL_INCLUDE = re.compile(r'^\s*#\s*include\s*[<"](sof/kernel/[^>"]+)[>"]', re.M)
133+
134+
135+
def source_files(sof_root):
136+
"""Return the files of the SOF source tree, relative to it."""
137+
try:
138+
out = subprocess.run(['git', '-C', sof_root, 'ls-files'],
139+
check=True, capture_output=True, text=True).stdout
140+
return out.split()
141+
except (OSError, subprocess.CalledProcessError):
142+
files = []
143+
for top, _, names in os.walk(sof_root):
144+
files += [os.path.relpath(os.path.join(top, n), sof_root) for n in names]
145+
return files
146+
147+
148+
def check_sources(manifest, sof_root):
149+
"""Return {user-space file: [kernel header includes]} from the source tree."""
150+
user_src = manifest['user-sources']
151+
not_user = manifest['not-user-sources']
152+
user_hdr = manifest['user-headers']
153+
kernel_hdr = manifest['kernel-headers']
154+
155+
found = {}
156+
for rel in source_files(sof_root):
157+
if rel.endswith(SOURCE_EXTS):
158+
if not matches(rel, user_src) or matches(rel, not_user):
159+
continue
160+
elif not (rel.endswith('.h') and matches(rel, user_hdr)) or \
161+
matches(rel, kernel_hdr):
162+
continue
163+
path = os.path.join(sof_root, rel)
164+
if not os.path.isfile(path):
165+
continue
166+
with open(path, encoding='utf-8', errors='replace') as f:
167+
incs = KERNEL_INCLUDE.findall(f.read())
168+
if incs:
169+
found[rel] = incs
170+
return found
171+
172+
125173
def main():
126174
parser = argparse.ArgumentParser(description=__doc__.split('\n', 1)[0])
127-
parser.add_argument('build_dirs', nargs='+', metavar='build-dir')
175+
parser.add_argument('build_dirs', nargs='*', metavar='build-dir')
128176
parser.add_argument('--manifest', default=DEFAULT_MANIFEST)
129177
parser.add_argument('--sof-root', default=SOF_ROOT,
130178
help='SOF source directory the build directories were built from')
@@ -135,9 +183,23 @@ def main():
135183
'includes a kernel-only header')
136184
parser.add_argument('--summary', action='store_true',
137185
help='only print per-header counts, not every source file')
186+
parser.add_argument('--sources', action='store_true',
187+
help='check the source tree for <sof/kernel/...> includes from '
188+
'user-space code instead of build directories')
138189
args = parser.parse_args()
139190

140191
manifest = parse_manifest(args.manifest)
192+
193+
if args.sources:
194+
found = check_sources(manifest, args.sof_root)
195+
for src in sorted(found):
196+
print(f'{src}:')
197+
for inc in found[src]:
198+
print(f'\t{inc}')
199+
print(f'{len(found)} user-space files include headers from src/kernel/include')
200+
return 1 if args.strict and found else 0
201+
if not args.build_dirs:
202+
parser.error('build directories or --sources are required')
141203
zephyr_base = args.zephyr_base if os.path.isdir(args.zephyr_base) else None
142204

143205
leaks = defaultdict(set)

‎scripts/kernel_user_partition.txt‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ src/schedule/dma_single_chan_domain.c
3030
src/schedule/ll_schedule_xtos.c
3131
src/schedule/zephyr_dma_domain.c
3232

33+
# Headers that user-space code includes, checked with --sources: they must
34+
# not include headers from src/kernel/include. The [kernel-headers] below
35+
# are excluded.
36+
[user-headers]
37+
src/include/*
38+
src/platform/*
39+
3340
# SOF headers that only kernel code may include.
3441
# Not listed on purpose: rtos/init.h, which only provides the
3542
# SOF_MODULE_INIT() registration hook used by every module, and

0 commit comments

Comments
 (0)