-
Notifications
You must be signed in to change notification settings - Fork 710
libnvme: add check-public-headers test and fix missing prototypes #3298
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| #!/usr/bin/env python3 | ||
| # SPDX-License-Identifier: LGPL-2.1-or-later | ||
| # | ||
| # This file is part of libnvme. | ||
| # Copyright (c) 2025, Dell Technologies Inc. or its subsidiaries. | ||
| # | ||
| # Authors: Martin Belanger <[email protected]> | ||
| # | ||
| # Verify that every symbol exported in a version script has a prototype | ||
| # declared in one of the installed header files. | ||
| # | ||
| # A __public function that appears in a .ld version script but not in any | ||
| # installed header is technically callable by external code, but callers have | ||
| # no declaration to include — they would need to write their own prototype or | ||
| # use dlsym(), which defeats the purpose of a stable public API. | ||
| # | ||
| # Usage (via meson — preferred, keeps meson.build as single source of truth): | ||
| # python3 tools/check-public-headers.py \ | ||
| # --ld src/libnvme.ld --ld src/accessors.ld [--ld ...] \ | ||
| # --header src/nvme/lib.h --header src/nvme/tree.h [--header ...] | ||
| # | ||
| # Usage (standalone, auto-discovers files from the source root): | ||
| # python3 tools/check-public-headers.py [LIBNVME-SOURCE-ROOT] | ||
| # | ||
| # In auto-discovery mode the script scans src/*.ld for version scripts and | ||
| # src/nvme/*.h (excluding files whose name contains "private") for headers. | ||
| # The source root defaults to the parent directory of this script. | ||
|
|
||
| import argparse | ||
| import re | ||
| import sys | ||
| import pathlib | ||
|
|
||
|
|
||
| def parse_args(): | ||
| parser = argparse.ArgumentParser( | ||
| description='Check that every exported symbol has a prototype in an ' | ||
| 'installed header.') | ||
| parser.add_argument( | ||
| 'root', nargs='?', | ||
| help='libnvme source root for auto-discovery (defaults to the parent ' | ||
| 'of this script); ignored when --ld / --header are given') | ||
| parser.add_argument( | ||
| '--ld', action='append', metavar='FILE', dest='ld_files', | ||
| help='version-script (.ld) file to read exported symbols from ' | ||
| '(may be repeated)') | ||
| parser.add_argument( | ||
| '--header', action='append', metavar='FILE', dest='headers', | ||
| help='installed header file to search for prototypes ' | ||
| '(may be repeated)') | ||
| return parser.parse_args() | ||
|
|
||
|
|
||
| def main(): | ||
| args = parse_args() | ||
|
|
||
| if args.ld_files or args.headers: | ||
| if not args.ld_files or not args.headers: | ||
| sys.exit('error: --ld and --header must both be provided together') | ||
| ld_files = [pathlib.Path(f) for f in args.ld_files] | ||
| headers = [pathlib.Path(f) for f in args.headers] | ||
| else: | ||
| root = pathlib.Path(args.root) if args.root else \ | ||
| pathlib.Path(__file__).resolve().parent.parent | ||
| src = root / 'src' | ||
| ld_files = sorted(src.glob('*.ld')) | ||
| headers = sorted(h for h in (src / 'nvme').glob('*.h') | ||
| if 'private' not in h.name) | ||
|
|
||
| # ----------------------------------------------------------------------- | ||
| # Collect all symbols listed in the version scripts | ||
| # ----------------------------------------------------------------------- | ||
| ld_syms = {} # symbol -> Path of the .ld file that declares it | ||
|
|
||
| for ld_path in ld_files: | ||
| for line in ld_path.read_text().splitlines(): | ||
| m = re.match(r'^\s+([a-z]\w+);', line) | ||
| if m: | ||
| ld_syms[m.group(1)] = ld_path | ||
|
|
||
| # ----------------------------------------------------------------------- | ||
| # Collect all names that appear as a prototype/declaration in installed | ||
| # headers. Match any identifier immediately followed by '(' — this | ||
| # catches both single-line and multi-line function declarations, and macro | ||
| # definitions that alias a function name. The libnvme_*/libnvmf_* | ||
| # namespace is long enough that false positives from comments are not a | ||
| # practical concern. | ||
| # ----------------------------------------------------------------------- | ||
| header_syms = set() | ||
|
|
||
| for hdr_path in headers: | ||
| for m in re.finditer(r'\b([a-z_]\w+)\s*\(', hdr_path.read_text()): | ||
| header_syms.add(m.group(1)) | ||
|
|
||
| # ----------------------------------------------------------------------- | ||
| # Report exported symbols with no prototype in any installed header | ||
| # ----------------------------------------------------------------------- | ||
| errors = 0 | ||
|
|
||
| for sym, ld_path in sorted(ld_syms.items()): | ||
| if sym not in header_syms: | ||
| print(f'ERROR: {sym}() is exported in {ld_path.name} ' | ||
| f'but has no prototype in any installed header') | ||
| errors += 1 | ||
|
|
||
| if errors: | ||
| print(f'\n{errors} error(s) found.') | ||
| sys.exit(1) | ||
|
|
||
| print(f'OK: all {len(ld_syms)} exported symbols have prototypes ' | ||
| f'in installed headers.') | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.