-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindClangFormatCurrent.cmake
100 lines (95 loc) · 3.37 KB
/
FindClangFormatCurrent.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#
#.rst:
# FindClangFormat
# ---------------
#
# The module defines the following variables
#
# ``CLANG_FORMAT_EXECUTABLE``
# Path to clang-format executable
# ``CLANG_FORMAT_FOUND``
# True if the clang-format executable was found.
# ``CLANG_FORMAT_VERSION``
# The version of clang-format found
# ``CLANG_FORMAT_VERSION_MAJOR``
# The clang-format major version if specified, 0 otherwise
# ``CLANG_FORMAT_VERSION_MINOR``
# The clang-format minor version if specified, 0 otherwise
# ``CLANG_FORMAT_VERSION_PATCH``
# The clang-format patch version if specified, 0 otherwise
# ``CLANG_FORMAT_VERSION_COUNT``
# Number of version components reported by clang-format
#
# Example usage:
#
# .. code-block:: cmake
#
# find_package(ClangFormat)
# if(CLANG_FORMAT_FOUND)
# message("clang-format executable found: ${CLANG_FORMAT_EXECUTABLE}\n"
# "version: ${CLANG_FORMAT_VERSION}")
# endif()
find_program(CLANG_FORMAT_EXECUTABLE
NAMES
clang-format-20 clang-format-19
clang-format-18 clang-format-17
clang-format-16 clang-format-15
clang-format-14 clang-format-13
clang-format-12 clang-format-11
clang-format-10 clang-format-9
clang-format-8.0 clang-format-8
clang-format-7.0 clang-format-7
clang-format-6.0 clang-format-6
clang-format clang-format-5.0
clang-format-4.0 clang-format-3.9
clang-format-3.8 clang-format-3.7
clang-format-3.6 clang-format-3.5
clang-format-3.4 clang-format-3.3
DOC "clang-format executable"
)
mark_as_advanced(CLANG_FORMAT_EXECUTABLE)
# Extract version from command "clang-format -version"
if(CLANG_FORMAT_EXECUTABLE)
execute_process(COMMAND sh -c "${CLANG_FORMAT_EXECUTABLE} -version | grep version"
OUTPUT_VARIABLE clang_format_version
# ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE)
if (clang_format_version MATCHES "version .*")
# clang_format_version sample: "clang-format version 3.9.1-4ubuntu3~16.04.1 (tags/RELEASE_391/rc2)"
string(REGEX REPLACE
"clang-format version ([.0-9]+).*" "\\1"
CLANG_FORMAT_VERSION "${clang_format_version}")
# CLANG_FORMAT_VERSION sample: "3.9.1"
# Extract version components
string(REPLACE "." ";" clang_format_version "${CLANG_FORMAT_VERSION}")
list(LENGTH clang_format_version CLANG_FORMAT_VERSION_COUNT)
if(CLANG_FORMAT_VERSION_COUNT GREATER 0)
list(GET clang_format_version 0 CLANG_FORMAT_VERSION_MAJOR)
else()
set(CLANG_FORMAT_VERSION_MAJOR 0)
endif()
if(CLANG_FORMAT_VERSION_COUNT GREATER 1)
list(GET clang_format_version 1 CLANG_FORMAT_VERSION_MINOR)
else()
set(CLANG_FORMAT_VERSION_MINOR 0)
endif()
if(CLANG_FORMAT_VERSION_COUNT GREATER 2)
list(GET clang_format_version 2 CLANG_FORMAT_VERSION_PATCH)
else()
set(CLANG_FORMAT_VERSION_PATCH 0)
endif()
if(CLANG_FORMAT_VERSION_MAJOR LESS 6)
message(FATAL_ERROR "Your installed clang-format version is too old! clang-format v6.0.0 or later is required!")
endif()
else()
message(FATAL_ERROR "Could not detect version of installed clang-format!")
endif()
unset(clang_format_version)
else()
message(FATAL_ERROR "Could not find clang-format! You need to install it first!")
endif()
if(CLANG_FORMAT_EXECUTABLE)
set(CLANG_FORMAT_FOUND TRUE)
else()
set(CLANG_FORMAT_FOUND FALSE)
endif()