File tree 3 files changed +64
-0
lines changed
3 files changed +64
-0
lines changed Original file line number Diff line number Diff line change @@ -33,6 +33,10 @@ endif()
33
33
# Top level project, doesn't really affect anything.
34
34
project (genzh LANGUAGES C CXX)
35
35
36
+ # Set up a format target to do automated clang format checking.
37
+ find_package (ClangFormat)
38
+ include (ClangFormat)
39
+
36
40
# This file handles extra settings wanted/needed for different compilers.
37
41
include (cmake/compilers.cmake)
38
42
Original file line number Diff line number Diff line change
1
+ if (CLANG_FORMAT_FOUND)
2
+ if (CLANG_FORMAT_VERSION VERSION_LESS "19.0.0" )
3
+ message (WARNING "clang-format ${CLANG_FORMAT_VERSION} is older than 19.0.0, formatting may yield unexpected results" )
4
+ elseif (CLANG_FORMAT_VERSION VERSION_GREATER "19.1.1" )
5
+ message (WARNING "clang-format ${CLANG_FORMAT_VERSION} is newer than 19.1.4, formatting may yield unexpected results" )
6
+ else ()
7
+ message (STATUS "Found clang-format ${CLANG_FORMAT_VERSION} , 'format' target enabled" )
8
+ endif ()
9
+
10
+ set (GLOB_PATTERNS
11
+ Generals/*.c
12
+ Generals/*.cpp
13
+ Generals/*.h
14
+ GeneralsMD/*.c
15
+ GeneralsMD/*.cpp
16
+ GeneralsMD/*.h
17
+ )
18
+
19
+ file (GLOB_RECURSE ALL_SOURCE_FILES RELATIVE ${CMAKE_SOURCE_DIR} ${GLOB_PATTERNS} )
20
+
21
+ add_custom_target (format)
22
+ foreach (SOURCE_FILE ${ALL_SOURCE_FILES} )
23
+ add_custom_command (
24
+ TARGET format
25
+ PRE_BUILD
26
+ COMMAND ${CLANG_FORMAT_EXECUTABLE} -style=file -i --verbose \"${SOURCE_FILE} \"
27
+ WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} )
28
+ endforeach ()
29
+ else ()
30
+ message (WARNING "clang-format not found, 'format' target unavailable" )
31
+ endif ()
Original file line number Diff line number Diff line change
1
+ find_program (CLANG_FORMAT_EXECUTABLE
2
+ NAMES clang-format-19
3
+ clang-format
4
+ DOC "clang-format executable" )
5
+ mark_as_advanced (CLANG_FORMAT_EXECUTABLE)
6
+
7
+ if (CLANG_FORMAT_EXECUTABLE)
8
+ if (NOT CLANG_FORMAT_FOUND)
9
+ execute_process (COMMAND ${CLANG_FORMAT_EXECUTABLE} -version
10
+ OUTPUT_VARIABLE clang_format_version
11
+ ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
12
+
13
+ if (clang_format_version MATCHES "^.*clang-format version .*" )
14
+ string (REGEX
15
+ REPLACE ".*clang-format version ([.0-9]+).*"
16
+ "\\ 1"
17
+ clang_format_version_parsed
18
+ "${clang_format_version} " )
19
+
20
+ set (CLANG_FORMAT_VERSION ${clang_format_version_parsed} CACHE INTERNAL "clang-format executable version" )
21
+ set (CLANG_FORMAT_FOUND TRUE CACHE INTERNAL "clang-format executable found" )
22
+ endif ()
23
+
24
+ unset (clang_format_version)
25
+ unset (clang_format_version_parsed)
26
+ endif ()
27
+ else ()
28
+ set (CLANG_FORMAT_FOUND FALSE CACHE INTERNAL "clang-format executable found" )
29
+ endif ()
You can’t perform that action at this time.
0 commit comments