|
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 |
| 14 | +import re |
13 | 15 |
|
14 | 16 | #Folders to scan |
15 | 17 | folders = [ |
|
34 | 36 | #Files with this extensions will be formatted/ |
35 | 37 | extensions = [".h", ".hpp", ".c", ".cpp"] |
36 | 38 |
|
| 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 | + |
37 | 74 | #processing formatting |
38 | 75 | for folder in folders: |
39 | 76 | print('*****************************************************************************') |
|
0 commit comments