Skip to content

scripts, .github: check for unused platform headers and macros - #11538

Open
lgirdwood wants to merge 3 commits into
thesofproject:mainfrom
lgirdwood:topic/platform-headers-6
Open

lgirdwood wants to merge 3 commits into
thesofproject:mainfrom
lgirdwood:topic/platform-headers-6

Conversation

@lgirdwood

@lgirdwood lgirdwood commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

Add a CI check that stops new unused platform headers and macros, and document what a platform header has to provide. This is the last part of the platform header cleanup in #11517 step 1, after #11528, #11529, #11532, #11534 and #11537. Without a check, new platforms copy the old templates (trace headers, PM runtime stubs, DMA trace settings, XTOS memory layouts) and the unused code comes back.

Patches

  1. scripts: add a check for unused platform headers and macros. check_platform_headers.py works on the source tree and needs no build; it runs in about 1.5 s.
    • Headers: a platform header is unused when nothing that can be built for its platform can include it. The check follows #include from the platform's sources with the platform's include path.
    • Macros: a platform macro is unused when its name appears nowhere in the code that can be built together with that platform's headers. That code is the generic SOF code, the Zephyr glue, and the platform's own platform and vendor driver directories. For the host library platform it is the host-only code instead of the Zephyr glue.
    • Text matching: it counts every #if branch, so the result is the same for every Kconfig.
    • Not reported: include guards, token-pasted names, the _BASE/_SIZE and _START/_END partners of used macros, mailbox windows with a used member, and the vendor-generated Xtensa core headers in include/arch/.
    • scripts/platform_headers.txt lists the platforms that share header directories (intel/ace, intel/cavs, amd/common), their vendor driver directories, and a [baseline] of today's 105 findings.
    • --strict fails on findings that are not in the baseline, so the baseline can only shrink. Entries that are no longer found are reported, --update-baseline rewrites the list, and --contract prints what generic code uses from each platform header.
  2. platform: document the platform header contract. A table in src/kernel/platform/README.md lists each include name that generic code uses, who includes it, and which macros and functions come from it. It also says what a new platform does not need.
  3. .github: check for new unused platform headers and macros. Runs the check with --strict in the codestyle workflow, next to the kernel include check.

Baseline

The 105 known findings are:

  • AMD and MediaTek macros, which are left until those platforms build again or move to native drivers;
  • isolated entries in hardware register lists that are kept on purpose, such as CPU_LPRO_FREQ_IDX, the sideband IPC fields and the qemu_xtensa trace window.

When a later change removes one of them, the check reports the entry as no longer found but does not fail. The entry can then be dropped with --update-baseline.

Functional change

None. Only a script, a manifest, documentation and a CI job.

Testing

🤖 Generated with Claude Code

@lgirdwood
lgirdwood force-pushed the topic/platform-headers-6 branch from 539fb7f to d93519e Compare November 25, 2026 16:51

@lgirdwood lgirdwood left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, clean source-tree check and contract write-up — fast (~1.5 s), config-independent, and the shrink-only [baseline] (already down to 105!) keeps platform headers from regressing.

A few edge cases found while reviewing scripts/check_platform_headers.py and cross-checking --contract against src/kernel/platform/README.md:

  • Tree.contract() drops macros wrapped by generic #defines (enc is not None), so --contract misses PLATFORM_PAGE_TABLE_SIZE (src/kernel/ipc/ipc3/host-page-table.c), HEAP_{SYSTEM,RUNTIME,BUFFER}_SIZE (zephyr/lib/alloc.c), and MAILBOX_DEBUG_BASE (src/include/sof/debug/debug.h).
  • Host source & Zephyr include order (HOST / FW_INCLUDE): src/kernel/drivers/host/ (if(CONFIG_LIBRARY)) is missing from HOST, and FW_INCLUDE puts 'src/include' ahead of 'zephyr/include', causing unreachable() to skip zephyr/include/ wrapper headers when a same-named header exists in src/include/.
  • Include guards separated by blank/comment lines: lines[i - 2] misses #ifndef GUARD when a blank line or /* ... */ comment sits before #define GUARD, while IFNDEF still ignores the #ifndef token, which falsely flags valid include guards as unused.
  • Local working-tree ergonomics: git_files() raises FileNotFoundError on unstaged rm deletions when testing cleanups locally (and scripts/check_platform_headers.py is committed 100644 despite its #!/usr/bin/env python3 shebang).
  • src/kernel/platform/README.md contract table: PLATFORM_PAGE_TABLE_SIZE (platform/platform.h) and SRAM_OUTBOX_SIZE / SRAM_STREAM_BASE / MAILBOX_DEBUG_BASE (platform/lib/mailbox*.h) are used by generic code when IPC3 / CONFIG_LIBRARY_MANAGER / CONFIG_USERSPACE / CONFIG_DEBUG are enabled.

Comment thread scripts/check_platform_headers.py Outdated
Comment on lines +60 to +65
HOST = ('posix/', 'tools/', 'test/cmocka/', 'src/kernel/arch/host/')
CODE_EXTS = ('.c', '.h', '.cpp', '.cc', '.hpp', '.S', '.ld', '.ld.in', '.x.in')
SOURCE_EXTS = ('.c', '.cpp', '.cc', '.S')
# include directories that every build has, besides the platform ones
FW_INCLUDE = ['src/include', 'src/kernel/include', 'zephyr/include', 'third_party/include',
'src/kernel/arch/xtensa/include', 'tools/rimage/src/include']

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two small build-layout fixes here:

  1. src/kernel/drivers/CMakeLists.txt compiles src/kernel/drivers/host/ only under if(CONFIG_LIBRARY) (just like src/kernel/arch/host/). Without 'src/kernel/drivers/host/' in HOST, generic() treats those files as generic firmware code visible to every DSP platform.
  2. Zephyr firmware builds put zephyr/include before src/include so headers like zephyr/include/sof/trace/trace.h override src/include/sof/trace/trace.h (matching how HOST_INCLUDE puts 'posix/include' first). Because unreachable() breaks on the first candidate in self.fileset, listing 'src/include' before 'zephyr/include' skips zephyr/include/ wrapper headers.
Suggested change
HOST = ('posix/', 'tools/', 'test/cmocka/', 'src/kernel/arch/host/')
CODE_EXTS = ('.c', '.h', '.cpp', '.cc', '.hpp', '.S', '.ld', '.ld.in', '.x.in')
SOURCE_EXTS = ('.c', '.cpp', '.cc', '.S')
# include directories that every build has, besides the platform ones
FW_INCLUDE = ['src/include', 'src/kernel/include', 'zephyr/include', 'third_party/include',
'src/kernel/arch/xtensa/include', 'tools/rimage/src/include']
HOST = ('posix/', 'tools/', 'test/cmocka/', 'src/kernel/arch/host/', 'src/kernel/drivers/host/')
CODE_EXTS = ('.c', '.h', '.cpp', '.cc', '.hpp', '.S', '.ld', '.ld.in', '.x.in')
SOURCE_EXTS = ('.c', '.cpp', '.cc', '.S')
# include directories that every build has, besides the platform ones
FW_INCLUDE = ['zephyr/include', 'src/include', 'src/kernel/include', 'third_party/include',
'src/kernel/arch/xtensa/include', 'tools/rimage/src/include']

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in b96bda3, as suggested: src/kernel/drivers/host/ is now host-only, and FW_INCLUDE searches zephyr/include first. I checked the order on a real PTL compile line (zephyr/include is the 10th -I, src/include the 23rd), which matches the zephyr/include/sof/trace/trace.h wrapper including the src/include one. The findings on main are unchanged (105).

Comment thread scripts/check_platform_headers.py Outdated
Comment on lines +96 to +99
def git_files():
out = subprocess.run(['git', 'ls-files'], cwd=SOF_ROOT, check=True,
capture_output=True, text=True).stdout
return [f for f in out.split() if f.endswith(CODE_EXTS)]

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a developer deletes a tracked header/source locally with rm (unstaged) before git rm while testing a cleanup, git ls-files still lists it and Source.__init__ crashes with FileNotFoundError (compare scripts/check_kernel_includes.py, which checks os.path.isfile(...) and uses out.splitlines()).

(Also minor: scripts/check_platform_headers.py has mode 100644 despite its #!/usr/bin/env python3 shebang, unlike 100755 on scripts/check_kernel_includes.py; and posixpath instead of os.path in unreachable() avoids \ vs / mismatches if run on Windows.)

Suggested change
def git_files():
out = subprocess.run(['git', 'ls-files'], cwd=SOF_ROOT, check=True,
capture_output=True, text=True).stdout
return [f for f in out.split() if f.endswith(CODE_EXTS)]
def git_files():
out = subprocess.run(['git', 'ls-files'], cwd=SOF_ROOT, check=True,
capture_output=True, text=True).stdout
return [f for f in out.splitlines()
if f.endswith(CODE_EXTS) and os.path.isfile(os.path.join(SOF_ROOT, f))]

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in b96bda3: git_files() uses splitlines() and skips files missing from the work tree, so deleting a tracked header with rm before git rm no longer crashes (tested). unreachable() builds paths with posixpath, and the script is now mode 100755.

Comment thread scripts/check_platform_headers.py Outdated
Comment on lines +123 to +124
if i >= 2 and re.match(r'\s*#\s*ifndef\s+' + cur + r'\b', lines[i - 2]):
self.guards.add(cur)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a blank line or /* ... */ comment (which line 108 turns into blank lines) sits between #ifndef GUARD and #define GUARD, lines[i - 2] is empty so GUARD is not added to self.guards — while IFNDEF on line 131 + lines 248–250 still ignore the #ifndef GUARD token, causing valid include guards to fail --strict as unused macros. Checking the previous non-blank line avoids the false positive while still catching mismatched #ifndef/#define guard typos:

Suggested change
if i >= 2 and re.match(r'\s*#\s*ifndef\s+' + cur + r'\b', lines[i - 2]):
self.guards.add(cur)
prev = next((l for l in reversed(lines[:i - 1]) if l.strip()), '')
if re.match(r'\s*#\s*ifndef\s+' + cur + r'\b', prev):
self.guards.add(cur)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in b96bda3 with your suggestion: a guard is recognised when the previous non-blank line is its #ifndef. Tested with a temporary header: a guard with a blank line and a comment before its #define is no longer reported, and a misspelled guard (#ifndef __X_H__ / #define __X_H___, like the imx93 clk.h one fixed in #11537) still is. No current platform header has that layout, so the baseline is unchanged.

Comment thread scripts/check_platform_headers.py Outdated
Comment on lines +308 to +312
fw, host = set(), set()
for f in self.files:
if self.generic(f) or f.startswith(HOST):
used = {t for t, enc in self.src(f).uses if enc is None}
(host if f.startswith(HOST) else fw).update(used)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Filtering if enc is None here drops platform macros that generic code uses inside a live #define wrapper (unlike macros(), which computes transitive liveness across enc). As a result, --contract currently misses:

  • PLATFORM_PAGE_TABLE_SIZE (platform/platform.h, used in src/kernel/ipc/ipc3/host-page-table.c inside HOST_PAGE_TABLE_MAX_PAGES),
  • HEAP_BUFFER_SIZE, HEAP_RUNTIME_SIZE, HEAP_SYSTEM_SIZE (platform/lib/memory.h, used in zephyr/lib/alloc.c inside HEAPMEM_SIZE), and
  • MAILBOX_DEBUG_BASE (platform/lib/mailbox.h, used via mailbox_get_debug_base() in src/include/sof/debug/debug.h).

Propagating liveness through enclosing macros keeps --contract accurate without pulling in dead generic wrappers like mailbox_get_exception_base():

Suggested change
fw, host = set(), set()
for f in self.files:
if self.generic(f) or f.startswith(HOST):
used = {t for t, enc in self.src(f).uses if enc is None}
(host if f.startswith(HOST) else fw).update(used)
def alive_in(files):
uses = defaultdict(list)
for f in files:
s = self.src(f)
for t, enc in s.uses:
if enc == '#ifndef':
if t in s.defines:
continue
enc = None
uses[t].append(enc)
alive = {t for t, es in uses.items() if None in es}
changed = True
while changed:
changed = False
for t, es in uses.items():
if t not in alive and any(e in alive for e in es):
alive.add(t)
changed = True
return alive
fw = alive_in([f for f in self.files if self.generic(f)])
host = alive_in([f for f in self.files
if (self.generic(f) and not f.startswith(('zephyr/', 'app/'))) or f.startswith(HOST)])

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in b96bda3 with your alive_in(): uses are followed through the generic macros that wrap them, for firmware and for the host builds. --contract now lists PLATFORM_PAGE_TABLE_SIZE, HEAP_{SYSTEM,RUNTIME,BUFFER}_SIZE and MAILBOX_DEBUG_BASE, and still leaves out dead wrappers such as mailbox_get_exception_base().

Comment thread src/kernel/platform/README.md Outdated

| Include name | Included by | What generic code uses |
|---|---|---|
| `platform/platform.h` | `sof/kernel/platform.h` | `HOST_PAGE_SIZE`, `HW_CFG_VERSION`; `platform_wait_for_interrupt()`, `ipc_platform_send_msg()`, `ipc_platform_send_msg_direct()` |

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

src/kernel/ipc/ipc3/host-page-table.c also uses PLATFORM_PAGE_TABLE_SIZE from platform/platform.h (falling back to HOST_PAGE_SIZE when unset on host builds); --contract missed it due to the enc is None filter in Tree.contract():

Suggested change
| `platform/platform.h` | `sof/kernel/platform.h` | `HOST_PAGE_SIZE`, `HW_CFG_VERSION`; `platform_wait_for_interrupt()`, `ipc_platform_send_msg()`, `ipc_platform_send_msg_direct()` |
| `platform/platform.h` | `sof/kernel/platform.h` | `HOST_PAGE_SIZE`, `PLATFORM_PAGE_TABLE_SIZE`, `HW_CFG_VERSION`; `platform_wait_for_interrupt()`, `ipc_platform_send_msg()`, `ipc_platform_send_msg_direct()` |

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in c5b511c: PLATFORM_PAGE_TABLE_SIZE added to the platform/platform.h row. The fixed --contract now prints it too.

Comment thread src/kernel/platform/README.md Outdated
| `platform/platform.h` | `sof/kernel/platform.h` | `HOST_PAGE_SIZE`, `HW_CFG_VERSION`; `platform_wait_for_interrupt()`, `ipc_platform_send_msg()`, `ipc_platform_send_msg_direct()` |
| `platform/lib/memory.h` | `sof/lib/memory.h` | `PLATFORM_DCACHE_ALIGN`, `HOST_PAGE_SIZE`, `SOF_STACK_SIZE`, `host_to_local()`, `local_to_host()`; where the platform has them: the heap sizes (`HEAPMEM_SIZE`, `HEAP_*_SIZE`), `SRAM_BANK_SIZE`, `LP_SRAM_SIZE`, `IMR_BOOT_LDR_MANIFEST_BASE`, `DMA0_BASE`/`DMA0_SIZE`, `MU_BASE` |
| `platform/lib/clk.h` | `rtos/clk.h` | `NUM_CLOCKS`, `NUM_CPU_FREQ`, `CPU_DEFAULT_IDX`, `CPU_LOWEST_FREQ_IDX`, `CLK_MAX_CPU_HZ`, `CLK_SSP`, `PRIMARY_CORE_BASE_CPS_USAGE`, `SECONDARY_CORE_BASE_CPS_USAGE` |
| `platform/lib/mailbox.h`, `platform/lib/mailbox_layout.h` (in `src/platform`) | `sof/lib/mailbox.h` | the host windows `MAILBOX_{DSPBOX,HOSTBOX,STREAM}_{BASE,SIZE}`, `MAILBOX_SW_REG_{BASE,SIZE}`; `mailbox_sw_reg_read()`, `mailbox_sw_reg_read64()`, `mailbox_sw_reg_write()`, `mailbox_sw_regs_write()` |

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generic code also uses SRAM_OUTBOX_SIZE (src/kernel/library_manager/lib_notification.c with CONFIG_LIBRARY_MANAGER), SRAM_STREAM_BASE (zephyr/lib/userspace_helper.c for IPC3 userspace), and MAILBOX_DEBUG_BASE (src/include/sof/debug/debug.h under CONFIG_DEBUG) from the mailbox/layout headers:

Suggested change
| `platform/lib/mailbox.h`, `platform/lib/mailbox_layout.h` (in `src/platform`) | `sof/lib/mailbox.h` | the host windows `MAILBOX_{DSPBOX,HOSTBOX,STREAM}_{BASE,SIZE}`, `MAILBOX_SW_REG_{BASE,SIZE}`; `mailbox_sw_reg_read()`, `mailbox_sw_reg_read64()`, `mailbox_sw_reg_write()`, `mailbox_sw_regs_write()` |
| `platform/lib/mailbox.h`, `platform/lib/mailbox_layout.h` (in `src/platform`) | `sof/lib/mailbox.h` | the host windows `MAILBOX_{DSPBOX,HOSTBOX,STREAM,DEBUG}_{BASE,SIZE}`, `MAILBOX_SW_REG_{BASE,SIZE}`, and where used `SRAM_OUTBOX_SIZE`, `SRAM_STREAM_BASE`; `mailbox_sw_reg_read()`, `mailbox_sw_reg_read64()`, `mailbox_sw_reg_write()`, `mailbox_sw_regs_write()` |

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in c5b511c, with one change from the suggestion: generic code uses MAILBOX_DEBUG_BASE but not MAILBOX_DEBUG_SIZE (--contract lists only the base), so the row names MAILBOX_DEBUG_BASE on its own instead of adding the whole DEBUG window. SRAM_OUTBOX_SIZE and SRAM_STREAM_BASE are added "where used", as suggested.

lrgirdwo and others added 3 commits November 26, 2026 19:49
The platform headers collected headers and macros that no code uses any
more, and new platforms copy them from the existing ones. Add
check_platform_headers.py to find them in the source tree, without
building:
- a platform header is unused when nothing that can be built for its
  platform can include it;
- a platform macro is unused when its name appears nowhere in the code
  that can be built together with that platform's headers: the generic
  SOF code, the Zephyr glue, and the platform's own platform and vendor
  driver directories (the host-only code for the host library platform).
  Text matching counts every #if branch, so the result does not depend
  on the configuration.

Include guards, token-pasted names, the _BASE/_SIZE and _START/_END
partners of used macros, mailbox windows with a used member and the
vendor-generated Xtensa core headers are not reported.

scripts/platform_headers.txt lists the platforms that share header
directories, their vendor driver directories, and a baseline of the
current 105 findings. With --strict the script fails on findings that
are not in the baseline, so the baseline can only shrink. --contract
prints the macros that generic code uses from each platform header.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Liam Girdwood <liam.r.girdwood@linux.intel.com>
Describe the platform headers that generic code includes, and what it
uses from each of them, so that a new platform only provides what is
needed: no trace or PM runtime header, no DMA trace settings and no
XTOS memory layout. Also describe scripts/check_platform_headers.py.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Liam Girdwood <liam.r.girdwood@linux.intel.com>
Run scripts/check_platform_headers.py --strict in the codestyle
workflow, next to the kernel include check. It fails when a change adds
a platform header or macro that nothing uses, and passes for the known
ones listed in its baseline.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Liam Girdwood <liam.r.girdwood@linux.intel.com>
@lgirdwood
lgirdwood force-pushed the topic/platform-headers-6 branch from d93519e to b30cf95 Compare November 26, 2026 19:50
@lgirdwood

Copy link
Copy Markdown
Member Author

Thanks for the review. I addressed all six points and force-pushed b30cf95 (rebased on d80a052):

  • check_platform_headers.py (b96bda3):
    • src/kernel/drivers/host/ counts as host-only;
    • Zephyr builds search zephyr/include first;
    • files deleted with rm but not yet with git rm are skipped;
    • paths are built with posixpath;
    • a guard is recognised across blank lines and comments;
    • --contract follows uses through the generic macros that wrap them;
    • the script is executable.
  • The README contract table (c5b511c) adds PLATFORM_PAGE_TABLE_SIZE, MAILBOX_DEBUG_BASE, SRAM_OUTBOX_SIZE and SRAM_STREAM_BASE.

The findings on main are unchanged (105, all in the baseline). --strict passes, and it still fails on a new unused macro or header. pyflakes passes, and the script parses as Python 3.8.

@lgirdwood
lgirdwood marked this pull request as ready for review November 26, 2026 19:58
@intel-sofci

Copy link
Copy Markdown

PR 11538: test results

Run date: 2026-11-26 20:26 UTC

Tested commit: b30cf95ac4a02ba9671c64441172e4c2109049da

mtl pass rate lnl pass rate ptl pass rate wcl pass rate nvl pass rate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants