diff options
author | Jeremy Bettis <jbettis@google.com> | 2023-04-03 18:45:56 +0000 |
---|---|---|
committer | Chromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2023-04-04 17:21:52 +0000 |
commit | 47161f4ecaf63221b2513f088ae0bc9cfdfecaad (patch) | |
tree | 3d1fbf5b8affd8a9ce84a887205c2d99eb7b0fa9 /util/kconfig_check.py | |
parent | 096df9199e102fac34ab23e5cd16ce53f89b2114 (diff) | |
download | chrome-ec-47161f4ecaf63221b2513f088ae0bc9cfdfecaad.tar.gz |
ec: Prevent #undef or #define of zephyr Kconfigs
Block all #undef or #define of Kconfigs in include/config.h. This will
allow removing the #include <autoconf.h> from
zephyr/shim/include/config_chip.h, which will in turn allow removing
all of the #line directives we've sprinkled throughout the headers.
BRANCH=None
BUG=b:272518464
TEST=make buildall_only -j$(nproc), fails before crrev/c/4396143
TEST=CQ, fails before crrev/c/4396143, passes after
Change-Id: I09649cf778e339fb45dc05c6ebdffc9aaa5778d2
Signed-off-by: Jeremy Bettis <jbettis@google.com>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/4396142
Auto-Submit: Jeremy Bettis <jbettis@chromium.org>
Reviewed-by: Keith Short <keithshort@chromium.org>
Commit-Queue: Keith Short <keithshort@chromium.org>
Tested-by: Jeremy Bettis <jbettis@chromium.org>
Diffstat (limited to 'util/kconfig_check.py')
-rwxr-xr-x | util/kconfig_check.py | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/util/kconfig_check.py b/util/kconfig_check.py index aebaa24cbd..48060f6782 100755 --- a/util/kconfig_check.py +++ b/util/kconfig_check.py @@ -22,6 +22,7 @@ Use the -d flag to select the second format. """ import argparse +import glob import os import pathlib import re @@ -119,6 +120,9 @@ a corresponding Kconfig option for Zephyr""" subparsers.add_parser("build", help="Build new list of ad-hoc CONFIGs") subparsers.add_parser("check", help="Check for new ad-hoc CONFIGs") + subparsers.add_parser( + "check_undef", help="Verify #undef directives in ec headers" + ) return parser.parse_args(argv) @@ -496,6 +500,68 @@ update in your CL: print(f"CONFIG_{config}", file=out) print(f"New list is in {NEW_ALLOWED_FNAME}") + def check_undef( + self, + srcdir, + search_paths, + ): + """Parse the ec header files and find zephyr Kconfigs that are + incorrectly undefined or defined to a default value. + + Args: + srcdir: Source directory to scan for Kconfig files + search_paths: List of project paths to search for Kconfig files, in + addition to the current directory + + Returns: + Exit code: 0 if OK, 1 if a problem was found + """ + kconfigs = set(self.scan_kconfigs(srcdir, "", search_paths)) + + if_re = re.compile(r"^\s*#\s*if(ndef CONFIG_ZEPHYR)?") + endif_re = re.compile(r"^\s*#\s*endif") + modify_config_re = re.compile(r"^\s*#\s*(define|undef)\s+CONFIG_(\S*)") + exit_code = 0 + files_to_check = glob.glob( + os.path.join(srcdir, "include/**/*.h"), recursive=True + ) + files_to_check += glob.glob( + os.path.join(srcdir, "common/**/public/*.h"), recursive=True + ) + files_to_check += glob.glob( + os.path.join(srcdir, "driver/**/*.h"), recursive=True + ) + for filename in files_to_check: + with open(filename, "r") as config_h: + depth = 0 + ignore_depth = 0 + line_count = 0 + for line in config_h.readlines(): + line_count += 1 + line = line.strip("\n") + match = if_re.match(line) + if match: + depth += 1 + if match[1] or ignore_depth > 0: + ignore_depth += 1 + if endif_re.match(line): + if depth > 0: + depth -= 1 + if ignore_depth > 0: + ignore_depth -= 1 + if ignore_depth == 0: + match = modify_config_re.match(line) + if match: + if match[2] in kconfigs: + print( + f"ERROR: Modifying CONFIG_{match[2]} " + "outside of #ifndef CONFIG_ZEPHYR not " + f"allowed at {filename}:{line_count}", + file=sys.stderr, + ) + exit_code = 1 + return exit_code + def main(argv): """Main function""" @@ -522,6 +588,11 @@ def main(argv): use_defines=args.use_defines, search_paths=args.search_path, ) + if args.cmd == "check_undef": + return checker.check_undef( + srcdir=args.srctree, + search_paths=args.search_path, + ) return 2 |