Skip to content

Commit b32be3c

Browse files
committed
[CMake] Add code for finding clang-format and applying it to the code base. (#493)
1 parent dfec741 commit b32be3c

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ endif()
3333
# Top level project, doesn't really affect anything.
3434
project(genzh LANGUAGES C CXX)
3535

36+
# Set up a format target to do automated clang format checking.
37+
find_package(ClangFormat)
38+
include(ClangFormat)
39+
3640
# This file handles extra settings wanted/needed for different compilers.
3741
include(cmake/compilers.cmake)
3842

cmake/ClangFormat.cmake

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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()

cmake/FindClangFormat.cmake

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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()

0 commit comments

Comments
 (0)