Skip to content

Commit b9e7f12

Browse files
committed
run_clang_format.py: Check clang-format --version
Run "clang-format --version" and parse the results to check that clang-format is at least as new as a minimum required version. Thanks to Michal Princ <michal.princ@nxp.com> for the idea and initial implementation.
1 parent 5ad01d1 commit b9e7f12

1 file changed

Lines changed: 39 additions & 2 deletions

File tree

run_clang_format.py

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#! /usr/bin/python
22

3-
# Copyright 2016-2017 NXP
3+
# Copyright 2016-2021 NXP
44
# All rights reserved.
55
#
66
# SPDX-License-Identifier: BSD-3-Clause
@@ -9,7 +9,9 @@
99
# $./run_clang_format.py
1010
from __future__ import print_function
1111
import subprocess
12-
import sys,os
12+
import sys
13+
import os
14+
import re
1315

1416
#Folders to scan
1517
folders = [
@@ -34,6 +36,41 @@
3436
#Files with this extensions will be formatted/
3537
extensions = [".h", ".hpp", ".c", ".cpp"]
3638

39+
# Check that the clang-format is installed and >= the minimum version
40+
clang_format_minimum_version = [10, 0, 0]
41+
try:
42+
cf = subprocess.Popen(
43+
["clang-format", "--version"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
44+
except FileNotFoundError:
45+
print("clang-format is not in $PATH")
46+
exit(1)
47+
clang_format_stdout, clang_format_stderr = cf.communicate()
48+
if cf.returncode != 0:
49+
print("Failed to determine clang-format version. stderr=\"%s\"" % (
50+
clang_format_stderr.decode('utf-8')))
51+
exit(1)
52+
clang_format_stdout = clang_format_stdout.decode("utf-8")
53+
m = re.match('^clang-format version (\d+\.\d+\.\d+)', clang_format_stdout)
54+
if m is None:
55+
print("clang-format --version output not understood: \"%s\"" % (clang_format_stdout))
56+
exit(1)
57+
clang_format_version = [int(e) for e in m.group(1).split(".")]
58+
59+
if (clang_format_version[0] < clang_format_minimum_version[0]) or \
60+
((clang_format_version[0] == clang_format_minimum_version[0]) and \
61+
((clang_format_version[1] < clang_format_minimum_version[1]) or \
62+
((clang_format_version[1] == clang_format_minimum_version[1]) and \
63+
(clang_format_version[2] < clang_format_minimum_version[2])))):
64+
print(
65+
"Installed clang-format version (%d.%d.%d) is less than the required minimum version (%d.%d.%d)" % (
66+
clang_format_version[0],
67+
clang_format_version[1],
68+
clang_format_version[2],
69+
clang_format_minimum_version[0],
70+
clang_format_minimum_version[1],
71+
clang_format_minimum_version[2]))
72+
exit(1)
73+
3774
#processing formatting
3875
for folder in folders:
3976
print('*****************************************************************************')

0 commit comments

Comments
 (0)