Skip to content

Commit e2ea7ff

Browse files
rugeGerritsenrlubos
authored andcommitted
[nrf fromtree] 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]> (cherry picked from commit e9abaf435ba93c31112bba32861ec8c0a22b9574)
1 parent a52c2f7 commit e2ea7ff

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
@@ -1793,6 +1793,34 @@ def filter_py(root, fnames):
17931793
mime=True) == "text/x-python")]
17941794

17951795

1796+
class CMakeStyle(ComplianceTest):
1797+
"""
1798+
Checks cmake style added/modified files
1799+
"""
1800+
name = "CMakeStyle"
1801+
doc = "See https://docs.zephyrproject.org/latest/contribute/style/cmake.html for more details."
1802+
1803+
def run(self):
1804+
# Loop through added/modified files
1805+
for fname in get_files(filter="d"):
1806+
if fname.endswith(".cmake") or fname == "CMakeLists.txt":
1807+
self.check_style(fname)
1808+
1809+
def check_style(self, fname):
1810+
SPACE_BEFORE_OPEN_BRACKETS_CHECK = re.compile(r"^\s*if\s+\(")
1811+
TAB_INDENTATION_CHECK = re.compile(r"^\t+")
1812+
1813+
with open(fname, encoding="utf-8") as f:
1814+
for line_num, line in enumerate(f.readlines(), start=1):
1815+
if TAB_INDENTATION_CHECK.match(line):
1816+
self.fmtd_failure("error", "CMakeStyle", fname, line_num,
1817+
"Use spaces instead of tabs for indentation")
1818+
1819+
if SPACE_BEFORE_OPEN_BRACKETS_CHECK.match(line):
1820+
self.fmtd_failure("error", "CMakeStyle", fname, line_num,
1821+
"Remove space before '(' in if() statements")
1822+
1823+
17961824
class Identity(ComplianceTest):
17971825
"""
17981826
Checks if Emails of author and signed-off messages are consistent.

0 commit comments

Comments
 (0)