From 2e70ea376b683bc012fabcc9af0564b8b1d03d65 Mon Sep 17 00:00:00 2001 From: Rubin Gerritsen Date: Wed, 1 Oct 2025 11:05:43 +0200 Subject: [PATCH] scripts: compliance: Add basic cmake style checks It is very common to see a comment stating that the "No Space Before Opening Brackets" style guide is not followed. To avoid putting this burdon on reviewers, let the compliance check catch this instead. Checking for tab indentation was also very simple, so also added this. Running this check on all files finds many violations: - Space before opening brackets: 141 violations - Tab indentation: 420 violations Fixing these will not be done as part of this PR. Adding the check will prevent us from adding new violations. Signed-off-by: Rubin Gerritsen --- scripts/ci/check_compliance.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/scripts/ci/check_compliance.py b/scripts/ci/check_compliance.py index a0fa1348bb6e6..2f60f601fd39d 100755 --- a/scripts/ci/check_compliance.py +++ b/scripts/ci/check_compliance.py @@ -1723,6 +1723,34 @@ def filter_py(root, fnames): mime=True) == "text/x-python")] +class CMakeStyle(ComplianceTest): + """ + Checks cmake style added/modified files + """ + name = "CMakeStyle" + doc = "See https://docs.zephyrproject.org/latest/contribute/style/cmake.html for more details." + + def run(self): + # Loop through added/modified files + for fname in get_files(filter="d"): + if fname.endswith(".cmake") or fname == "CMakeLists.txt": + self.check_style(fname) + + def check_style(self, fname): + SPACE_BEFORE_OPEN_BRACKETS_CHECK = re.compile(r"^\s*if\s+\(") + TAB_INDENTATION_CHECK = re.compile(r"^\t+") + + with open(fname, encoding="utf-8") as f: + for line_num, line in enumerate(f.readlines(), start=1): + if TAB_INDENTATION_CHECK.match(line): + self.fmtd_failure("error", "CMakeStyle", fname, line_num, + "Use spaces instead of tabs for indentation") + + if SPACE_BEFORE_OPEN_BRACKETS_CHECK.match(line): + self.fmtd_failure("error", "CMakeStyle", fname, line_num, + "Remove space before '(' in if() statements") + + class Identity(ComplianceTest): """ Checks if Emails of author and signed-off messages are consistent.