Skip to content

Commit e9abaf4

Browse files
rugeGerritsennashif
authored andcommitted
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 <[email protected]>
1 parent 28ac3b5 commit e9abaf4

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

scripts/ci/check_compliance.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,6 +1723,34 @@ def filter_py(root, fnames):
17231723
mime=True) == "text/x-python")]
17241724

17251725

1726+
class CMakeStyle(ComplianceTest):
1727+
"""
1728+
Checks cmake style added/modified files
1729+
"""
1730+
name = "CMakeStyle"
1731+
doc = "See https://docs.zephyrproject.org/latest/contribute/style/cmake.html for more details."
1732+
1733+
def run(self):
1734+
# Loop through added/modified files
1735+
for fname in get_files(filter="d"):
1736+
if fname.endswith(".cmake") or fname == "CMakeLists.txt":
1737+
self.check_style(fname)
1738+
1739+
def check_style(self, fname):
1740+
SPACE_BEFORE_OPEN_BRACKETS_CHECK = re.compile(r"^\s*if\s+\(")
1741+
TAB_INDENTATION_CHECK = re.compile(r"^\t+")
1742+
1743+
with open(fname, encoding="utf-8") as f:
1744+
for line_num, line in enumerate(f.readlines(), start=1):
1745+
if TAB_INDENTATION_CHECK.match(line):
1746+
self.fmtd_failure("error", "CMakeStyle", fname, line_num,
1747+
"Use spaces instead of tabs for indentation")
1748+
1749+
if SPACE_BEFORE_OPEN_BRACKETS_CHECK.match(line):
1750+
self.fmtd_failure("error", "CMakeStyle", fname, line_num,
1751+
"Remove space before '(' in if() statements")
1752+
1753+
17261754
class Identity(ComplianceTest):
17271755
"""
17281756
Checks if Emails of author and signed-off messages are consistent.

0 commit comments

Comments
 (0)