|
11 | 11 | import subprocess |
12 | 12 | import sys |
13 | 13 | import os |
14 | | -import re |
15 | 14 |
|
16 | 15 | #Folders to scan |
17 | 16 | folders = [ |
|
36 | 35 | #Files with this extensions will be formatted/ |
37 | 36 | extensions = [".h", ".hpp", ".c", ".cpp"] |
38 | 37 |
|
39 | | -# Check that the clang-format is installed and >= the minimum version |
40 | | -clang_format_minimum_version = [10, 0, 0] |
| 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" |
41 | 43 | try: |
42 | 44 | cf = subprocess.Popen( |
43 | | - ["clang-format", "--version"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 45 | + [clang_format_bin, "--version"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
44 | 46 | except FileNotFoundError: |
45 | 47 | print("clang-format is not in $PATH") |
46 | 48 | exit(1) |
|
50 | 52 | clang_format_stderr.decode('utf-8'))) |
51 | 53 | exit(1) |
52 | 54 | 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)) |
| 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") |
56 | 57 | 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 | 58 |
|
74 | 59 | #processing formatting |
75 | 60 | for folder in folders: |
|
0 commit comments