Skip to content

Commit f419037

Browse files
Merge pull request #177 from SWISensorHub/pr_run_clang_format_cleanup
run_clang_format.py cleanup
2 parents 313a297 + 6be2172 commit f419037

1 file changed

Lines changed: 39 additions & 24 deletions

File tree

run_clang_format.py

100644100755
Lines changed: 39 additions & 24 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,37 +9,52 @@
99
# $./run_clang_format.py
1010
from __future__ import print_function
1111
import subprocess
12-
import sys,os
12+
import sys
13+
import os
1314

1415
#Folders to scan
15-
folders = []
16-
folders.append("erpc_c");
17-
folders.append("erpcgen/src");
18-
folders.append("erpcsniffer/src");
19-
folders.append("test");
16+
folders = [
17+
"erpc_c",
18+
"erpcgen/src",
19+
"erpcsniffer/src",
20+
"test"]
2021

2122
#Files which will be not formatted
22-
exceptions = []
23-
exceptions.append("test/common/gtest/gtest.h");
24-
exceptions.append("test/common/gtest/gtest.cpp");
25-
exceptions.append("erpcgen/src/cpptemplate/cpptempl.h");
26-
exceptions.append("erpcgen/src/cpptemplate/cpptempl.cpp");
27-
exceptions.append("erpcgen/src/cpptemplate/cpptempl_test.cpp");
23+
exceptions = [
24+
"test/common/gtest/gtest.h",
25+
"test/common/gtest/gtest.cpp",
26+
"erpcgen/src/cpptemplate/cpptempl.h",
27+
"erpcgen/src/cpptemplate/cpptempl.cpp",
28+
"erpcgen/src/cpptemplate/cpptempl_test.cpp"]
2829

2930
#For windows use "\\" instead of "/" path separators.
3031
if os.environ.get('OS','') == 'Windows_NT':
31-
for i, folder in enumerate(folders):
32-
folders[i] = os.path.normpath(folder)
33-
34-
for i, ext in enumerate(exceptions):
35-
exceptions[i] = os.path.normpath(ext)
32+
folders = [os.path.normpath(folder) for folder in folders]
33+
exceptions = [os.path.normpath(e) for e in exceptions]
3634

3735
#Files with this extensions will be formatted/
38-
extensions = []
39-
extensions.append(".h")
40-
extensions.append(".hpp")
41-
extensions.append(".c")
42-
extensions.append(".cpp")
36+
extensions = [".h", ".hpp", ".c", ".cpp"]
37+
38+
# Check that the clang-format is installed and matches the required version. The clang-format binary
39+
# chosen is specified by the environment variable $CLANG_FORMAT_PATH or "clang-format" using the
40+
# regular $PATH resolution mechanism if $CLANG_FORMAT_PATH is undefined.
41+
clang_format_path = os.environ.get("CLANG_FORMAT_PATH")
42+
clang_format_bin = clang_format_path if clang_format_path is not None else "clang-format"
43+
try:
44+
cf = subprocess.Popen(
45+
[clang_format_bin, "--version"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
46+
except FileNotFoundError:
47+
print("clang-format is not in $PATH")
48+
exit(1)
49+
clang_format_stdout, clang_format_stderr = cf.communicate()
50+
if cf.returncode != 0:
51+
print("Failed to determine clang-format version. stderr=\"%s\"" % (
52+
clang_format_stderr.decode('utf-8')))
53+
exit(1)
54+
clang_format_stdout = clang_format_stdout.decode("utf-8")
55+
if not clang_format_stdout.startswith("clang-format version 10.0.0"):
56+
print("clang-format is not the required version: 10.0.0")
57+
exit(1)
4358

4459
#processing formatting
4560
for folder in folders:
@@ -53,5 +68,5 @@
5368
print("Ignored: ", file)
5469
else:
5570
print("Formatting: ", file)
56-
subprocess.call(["clang-format-10.0", "-i", file])
71+
subprocess.call(["clang-format", "-i", file])
5772
print('*****************************************************************************\n')

0 commit comments

Comments
 (0)