|
1 | 1 | #! /usr/bin/python |
2 | 2 |
|
3 | | -# Copyright 2016-2017 NXP |
| 3 | +# Copyright 2016-2021 NXP |
4 | 4 | # All rights reserved. |
5 | 5 | # |
6 | 6 | # SPDX-License-Identifier: BSD-3-Clause |
|
9 | 9 | # $./run_clang_format.py |
10 | 10 | from __future__ import print_function |
11 | 11 | import subprocess |
12 | | -import sys,os |
| 12 | +import sys |
| 13 | +import os |
13 | 14 |
|
14 | 15 | #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"] |
20 | 21 |
|
21 | 22 | #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"] |
28 | 29 |
|
29 | 30 | #For windows use "\\" instead of "/" path separators. |
30 | 31 | 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] |
36 | 34 |
|
37 | 35 | #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) |
43 | 58 |
|
44 | 59 | #processing formatting |
45 | 60 | for folder in folders: |
|
53 | 68 | print("Ignored: ", file) |
54 | 69 | else: |
55 | 70 | print("Formatting: ", file) |
56 | | - subprocess.call(["clang-format-10.0", "-i", file]) |
| 71 | + subprocess.call(["clang-format", "-i", file]) |
57 | 72 | print('*****************************************************************************\n') |
0 commit comments