From c02695e85c4978a9e58bd812c190dc5bc204e40a Mon Sep 17 00:00:00 2001 From: Jeremy Bettis Date: Wed, 29 Mar 2023 23:13:46 +0000 Subject: ec: Fix kconfig_check.py The kconfig_check.py tool was reporting all symbols in the Kconfigs as configs, including things like ``` config PLATFORM_EC_PREINIT_HW_CYCLES_PER_SEC default 100000000 ``` would return CONFIG_PLATFORM_EC_PREINIT_HW_CYCLES_PER_SEC and CONFIG_100000000 as valid configs. Read only Symbol node names instead. Include Kconfigs in platform/ec but outside of platform/ec/zephyr also. Also include Kconfig.zephyr from third_party/zephyr/main, which was previously omitted. This requires setting some environment variables. BRANCH=None BUG=b:272518464 TEST=make buildall Change-Id: I02e86f5c96f1e9943386d1610af1b2ab07550754 Signed-off-by: Jeremy Bettis Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/4383371 Auto-Submit: Jeremy Bettis Tested-by: Jeremy Bettis Commit-Queue: Jeremy Bettis Reviewed-by: Simon Glass --- util/config_allowed.txt | 3 - util/kconfig_check.py | 45 ++++++++++----- util/test_kconfig_check.py | 134 +++++++++++++++++++++++++++++---------------- 3 files changed, 119 insertions(+), 63 deletions(-) (limited to 'util') diff --git a/util/config_allowed.txt b/util/config_allowed.txt index d841c0c7a7..348278277d 100644 --- a/util/config_allowed.txt +++ b/util/config_allowed.txt @@ -406,7 +406,6 @@ CONFIG_FLASH_WRITE_SIZE CONFIG_FMAP CONFIG_FOO CONFIG_FORCE_CONSOLE_RESUME -CONFIG_FPU CONFIG_FPU_WARNINGS CONFIG_FP_SENSOR_ELAN515 CONFIG_FP_SENSOR_ELAN80 @@ -641,12 +640,10 @@ CONFIG_MOTION_MIN_SENSE_WAIT_TIME CONFIG_MOTION_SENSE_RESUME_DELAY_US CONFIG_MOTION_SENSE_SUSPEND_DELAY_US CONFIG_MP4245 -CONFIG_MPU CONFIG_NAME CONFIG_NB7V904M_LPM_OVERRIDE CONFIG_NO_PINHOLD CONFIG_NUM_FIXED_BATTERIES -CONFIG_NUM_IRQS CONFIG_ONEWIRE CONFIG_ONLINE_CALIB CONFIG_ONLINE_CALIB_SPOOF_MODE diff --git a/util/kconfig_check.py b/util/kconfig_check.py index 1b67f0c846..aebaa24cbd 100755 --- a/util/kconfig_check.py +++ b/util/kconfig_check.py @@ -2,6 +2,7 @@ # Copyright 2021 The ChromiumOS Authors # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. + """Kconfig checker Checks that the .config file provided does not introduce any new ad-hoc CONFIG @@ -285,21 +286,37 @@ class KconfigCheck: List of config and menuconfig options found """ if USE_KCONFIGLIB and try_kconfiglib: - os.environ["srctree"] = srcdir - kconf = kconfiglib.Kconfig( - "Kconfig", - warn=False, - search_paths=search_paths, - allow_empty_macros=True, + os.environ.update( + { + "srctree": srcdir, + "SOC_DIR": "soc", + "ARCH_DIR": "arch", + "BOARD_DIR": "boards/*/*", + "ARCH": "*", + } ) - - # There is always a MODULES config, since kconfiglib is designed for - # linux, but we don't want it - kconfigs = [name for name in kconf.syms if name != "MODULES"] - - if prefix: - re_drop_prefix = re.compile(r"^%s" % prefix) - kconfigs = [re_drop_prefix.sub("", name) for name in kconfigs] + kconfigs = [] + for filename in [ + "Kconfig", + os.path.join(os.environ["ZEPHYR_BASE"], "Kconfig.zephyr"), + ]: + kconf = kconfiglib.Kconfig( + filename, + warn=False, + search_paths=search_paths, + allow_empty_macros=True, + ) + + symbols = [ + node.item.name + for node in kconf.node_iter() + if isinstance(node.item, kconfiglib.Symbol) + ] + + if prefix: + re_drop_prefix = re.compile(r"^%s" % prefix) + symbols = [re_drop_prefix.sub("", name) for name in symbols] + kconfigs += symbols else: kconfigs = [] # Remove the prefix if present diff --git a/util/test_kconfig_check.py b/util/test_kconfig_check.py index e30df416fb..12721cc7ef 100644 --- a/util/test_kconfig_check.py +++ b/util/test_kconfig_check.py @@ -1,6 +1,7 @@ # Copyright 2021 The ChromiumOS Authors # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. + """Test for Kconfig checker""" import contextlib @@ -120,6 +121,32 @@ rsource "subdir/Kconfig.wibble" with open(os.path.join(bad_subdir, "Kconfig.bad"), "w") as out: out.write("menuconfig %sBAD_KCONFIG" % PREFIX) + @classmethod + def setup_zephyr_base(cls, zephyr_base): + """Set up some Kconfig files in a directory and subdirs + + Args: + zephyr_base: Directory to write to + """ + with open(os.path.join(zephyr_base, "Kconfig.zephyr"), "w") as out: + out.write( + """config ZCONFIG +\tbool "zephyr kconfig" + +rsource "subdir/Kconfig.wobble" +""" + ) + subdir = os.path.join(zephyr_base, "subdir") + os.mkdir(subdir) + with open(os.path.join(subdir, "Kconfig.wobble"), "w") as out: + out.write("menuconfig WOBBLE_MENU_KCONFIG\n") + + # Add a directory which should be ignored + bad_subdir = os.path.join(subdir, "Kconfig") + os.mkdir(bad_subdir) + with open(os.path.join(bad_subdir, "Kconfig.bad"), "w") as out: + out.write("menuconfig BAD_KCONFIG") + def test_find_kconfigs(self): """Test KconfigCheck.find_kconfigs()""" checker = kconfig_check.KconfigCheck() @@ -133,11 +160,19 @@ rsource "subdir/Kconfig.wibble" """Test KconfigCheck.scan_configs()""" checker = kconfig_check.KconfigCheck() with tempfile.TemporaryDirectory() as srctree: - self.setup_srctree(srctree) - self.assertEqual( - ["MENU_KCONFIG", "MY_KCONFIG"], - checker.scan_kconfigs(srctree, PREFIX), - ) + with tempfile.TemporaryDirectory() as zephyr_path: + self.setup_zephyr_base(zephyr_path) + os.environ["ZEPHYR_BASE"] = str(zephyr_path) + self.setup_srctree(srctree) + self.assertEqual( + [ + "MENU_KCONFIG", + "MY_KCONFIG", + "WOBBLE_MENU_KCONFIG", + "ZCONFIG", + ], + checker.scan_kconfigs(srctree, PREFIX), + ) @classmethod def setup_allowed_and_configs( @@ -166,25 +201,35 @@ rsource "subdir/Kconfig.wibble" self.setup_srctree(srctree) with tempfile.NamedTemporaryFile() as allowed: with tempfile.NamedTemporaryFile() as configs: - self.setup_allowed_and_configs(allowed.name, configs.name) - ( - new_adhoc, - unneeded_adhoc, - updated_adhoc, - ) = checker.check_adhoc_configs( - configs.name, srctree, allowed.name, PREFIX - ) - self.assertEqual(["NEW_ONE"], new_adhoc) - self.assertEqual(["MENU_KCONFIG"], unneeded_adhoc) - self.assertEqual(["OLD_ONE"], updated_adhoc) + with tempfile.TemporaryDirectory() as zephyr_path: + self.setup_zephyr_base(zephyr_path) + os.environ["ZEPHYR_BASE"] = str(zephyr_path) + self.setup_allowed_and_configs( + allowed.name, configs.name + ) + ( + new_adhoc, + unneeded_adhoc, + updated_adhoc, + ) = checker.check_adhoc_configs( + configs.name, srctree, allowed.name, PREFIX + ) + self.assertEqual(["NEW_ONE"], new_adhoc) + self.assertEqual(["MENU_KCONFIG"], unneeded_adhoc) + self.assertEqual(["OLD_ONE"], updated_adhoc) def test_check(self): """Test running the 'check' subcommand""" - with capture_sys_output() as (stdout, stderr): - with tempfile.TemporaryDirectory() as srctree: - self.setup_srctree(srctree) - with tempfile.NamedTemporaryFile() as allowed: - with tempfile.NamedTemporaryFile() as configs: + with capture_sys_output() as ( + stdout, + stderr, + ), tempfile.TemporaryDirectory() as srctree: + with tempfile.NamedTemporaryFile() as allowed: + with tempfile.NamedTemporaryFile() as configs: + with tempfile.TemporaryDirectory() as zephyr_path: + self.setup_srctree(srctree) + self.setup_zephyr_base(zephyr_path) + os.environ["ZEPHYR_BASE"] = str(zephyr_path) self.setup_allowed_and_configs( allowed.name, configs.name ) @@ -228,14 +273,8 @@ rsource "subdir/Kconfig.wibble" # List of things missing from the Kconfig missing = sorted(list(set(adhoc_version) - set(kc_version))) - # Some items are defined in files that are not included - # in all cases, only for particular values of $(ARCH) - self.assertEqual( - [ - "TRAP_UNALIGNED_ACCESS", - ], - missing, - ) + # There should be no differences between adhoc and kconfig versions + self.assertListEqual([], missing) def test_check_unneeded(self): """Test running the 'check' subcommand with unneeded ad-hoc configs""" @@ -244,23 +283,26 @@ rsource "subdir/Kconfig.wibble" self.setup_srctree(srctree) with tempfile.NamedTemporaryFile() as allowed: with tempfile.NamedTemporaryFile() as configs: - self.setup_allowed_and_configs( - allowed.name, configs.name, False - ) - ret_code = kconfig_check.main( - [ - "-c", - configs.name, - "-s", - srctree, - "-a", - allowed.name, - "-p", - PREFIX, - "check", - ] - ) - self.assertEqual(1, ret_code) + with tempfile.TemporaryDirectory() as zephyr_path: + self.setup_zephyr_base(zephyr_path) + os.environ["ZEPHYR_BASE"] = str(zephyr_path) + self.setup_allowed_and_configs( + allowed.name, configs.name, False + ) + ret_code = kconfig_check.main( + [ + "-c", + configs.name, + "-s", + srctree, + "-a", + allowed.name, + "-p", + PREFIX, + "check", + ] + ) + self.assertEqual(1, ret_code) self.assertEqual("", stderr.getvalue()) found = re.findall("(CONFIG_.*)", stdout.getvalue()) self.assertEqual(["CONFIG_MENU_KCONFIG"], found) -- cgit v1.2.1