Skip to content

Commit f8a5ebf

Browse files
nordicjmkartben
andcommitted
scripts: ci: check_compliance: Add check for disallowed Kconfigs
Adds a check for Kconfig settings that are disallowed from being used in defconfig files (because it is the complete wrong place for them), with the default entry being PINCTRL Signed-off-by: Jamie McCrae <[email protected]> Co-authored-by: Benjamin Cabé <[email protected]>
1 parent ee07eba commit f8a5ebf

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

scripts/ci/check_compliance.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,7 @@ def run(self):
400400
self.check_no_enable_in_boolean_prompt(kconf)
401401
self.check_soc_name_sync(kconf)
402402
self.check_no_undef_outside_kconfig(kconf)
403+
self.check_disallowed_defconfigs(kconf)
403404

404405
def get_modules(self, modules_file, sysbuild_modules_file, settings_file):
405406
"""
@@ -674,6 +675,84 @@ def get_logging_syms(self, kconf):
674675

675676
return set(kconf_syms)
676677

678+
def check_disallowed_defconfigs(self, kconf):
679+
"""
680+
Checks that there are no disallowed Kconfigs used in board/SoC defconfig files
681+
"""
682+
# Grep for symbol references.
683+
#
684+
# Example output line for a reference to CONFIG_FOO at line 17 of
685+
# foo/bar.c:
686+
#
687+
# foo/bar.c<null>17<null>#ifdef CONFIG_FOO
688+
#
689+
# 'git grep --only-matching' would get rid of the surrounding context
690+
# ('#ifdef '), but it was added fairly recently (second half of 2018),
691+
# so we extract the references from each line ourselves instead.
692+
#
693+
# The regex uses word boundaries (\b) to isolate the reference, and
694+
# negative lookahead to automatically allowlist the following:
695+
#
696+
# - ##, for token pasting (CONFIG_FOO_##X)
697+
#
698+
# - $, e.g. for CMake variable expansion (CONFIG_FOO_${VAR})
699+
#
700+
# - @, e.g. for CMakes's configure_file() (CONFIG_FOO_@VAR@)
701+
#
702+
# - {, e.g. for Python scripts ("CONFIG_FOO_{}_BAR".format(...)")
703+
#
704+
# - *, meant for comments like '#endif /* CONFIG_FOO_* */
705+
706+
disallowed_symbols = {
707+
"PINCTRL": "Drivers requiring PINCTRL must SELECT it instead.",
708+
}
709+
710+
disallowed_regex = "(" + "|".join(disallowed_symbols.keys()) + ")$"
711+
712+
# Warning: Needs to work with both --perl-regexp and the 're' module
713+
regex_boards = r"\bCONFIG_[A-Z0-9_]+\b(?!\s*##|[$@{(.*])"
714+
regex_socs = r"\bconfig\s+[A-Z0-9_]+$"
715+
716+
grep_stdout_boards = git("grep", "--line-number", "-I", "--null",
717+
"--perl-regexp", regex_boards, "--", ":boards",
718+
cwd=Path(GIT_TOP))
719+
grep_stdout_socs = git("grep", "--line-number", "-I", "--null",
720+
"--perl-regexp", regex_socs, "--", ":soc",
721+
cwd=Path(GIT_TOP))
722+
723+
# Board processing
724+
# splitlines() supports various line terminators
725+
for grep_line in grep_stdout_boards.splitlines():
726+
path, lineno, line = grep_line.split("\0")
727+
728+
# Extract symbol references (might be more than one) within the line
729+
for sym_name in re.findall(regex_boards, line):
730+
sym_name = sym_name[len("CONFIG_"):]
731+
# Only check in Kconfig fragment files, references might exist in documentation
732+
if re.match(disallowed_regex, sym_name) and (path[-len("conf"):] == "conf" or
733+
path[-len("defconfig"):] == "defconfig"):
734+
reason = disallowed_symbols.get(sym_name)
735+
self.fmtd_failure("error", "BoardDisallowedKconfigs", path, lineno, desc=f"""
736+
Found disallowed Kconfig symbol in board Kconfig files: CONFIG_{sym_name:35}
737+
{reason}
738+
""")
739+
740+
# SoCs processing
741+
# splitlines() supports various line terminators
742+
for grep_line in grep_stdout_socs.splitlines():
743+
path, lineno, line = grep_line.split("\0")
744+
745+
# Extract symbol references (might be more than one) within the line
746+
for sym_name in re.findall(regex_socs, line):
747+
sym_name = sym_name[len("config"):].strip()
748+
# Only check in Kconfig defconfig files
749+
if re.match(disallowed_regex, sym_name) and "defconfig" in path:
750+
reason = disallowed_symbols.get(sym_name, "Unknown reason")
751+
self.fmtd_failure("error", "SoCDisallowedKconfigs", path, lineno, desc=f"""
752+
Found disallowed Kconfig symbol in SoC Kconfig files: {sym_name:35}
753+
{reason}
754+
""")
755+
677756
def get_defined_syms(self, kconf):
678757
# Returns a set() with the names of all defined Kconfig symbols (with no
679758
# 'CONFIG_' prefix). This is complicated by samples and tests defining

0 commit comments

Comments
 (0)