Skip to content

Commit 48f1190

Browse files
authored
Support custom file patterns in clang-format (#9)
1 parent 0616a35 commit 48f1190

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

ClangFormat.cmake

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,21 @@
55
# use clang-format to format source code according to a configuration file. 2 types
66
# of targets will be created, one for formatting all files in the repository and the
77
# other for formatting only the files which differ from master. The list of files to
8-
# format is generated by git itself.
8+
# format is generated by git itself using a GLOB pattern.
9+
#
10+
# The default pattern will include the following file types
11+
# *.c *.h *.cpp *.cc *.hpp
12+
#
13+
# A project may provide a custom file matching pattern to override the default
14+
#
15+
# swift_setup_clang_format(PATTERNS '*.c')
16+
#
17+
# will format ONLY *.c files. It is possible to provide a path in the pattern
18+
#
19+
# swift_setup_clang_format(PATTERNS 'src/*.c' 'include/*.h')
20+
#
21+
# will format only *.c files under ${PROJECT_SOURCE_DIR}/src and *.h files under
22+
# ${PROJECT_SOURCE_DIR}/include
923
#
1024
# The created targets have the names
1125
#
@@ -107,7 +121,7 @@ endmacro()
107121
function(swift_setup_clang_format)
108122
set(argOption "REQUIRED")
109123
set(argSingle "SCRIPT")
110-
set(argMulti "CLANG_FORMAT_NAMES")
124+
set(argMulti "CLANG_FORMAT_NAMES" "PATTERNS")
111125

112126
cmake_parse_arguments(x "${argOption}" "${argSingle}" "${argMulti}" ${ARGN})
113127

@@ -197,12 +211,16 @@ function(swift_setup_clang_format)
197211
message(STATUS "Using ${CLANG_FORMAT}")
198212
set(${PROJECT_NAME}_CLANG_FORMAT ${CLANG_FORMAT} CACHE STRING "Absolute path to clang-format for ${PROJECT_NAME}")
199213

200-
# Format all source and header files in the repo, use a git command to build the file list
201-
set(default_patterns '*.[ch]' '*.cpp' '*.cc' '*.hpp')
214+
if(x_PATTERNS)
215+
set(patterns ${x_PATTERNS})
216+
else()
217+
# Format all source and header files in the repo, use a git command to build the file list
218+
set(patterns '*.[ch]' '*.cpp' '*.cc' '*.hpp')
219+
endif()
202220

203221
create_targets(
204222
TOP_LEVEL ${top_level_project}
205-
ALL_COMMAND git ls-files ${default_patterns} | xargs ${${PROJECT_NAME}_CLANG_FORMAT} -i
206-
DIFF_COMMAND git diff --diff-filter=ACMRTUXB --name-only master -- ${default_patterns} | xargs ${${PROJECT_NAME}_CLANG_FORMAT} -i
223+
ALL_COMMAND git ls-files ${patterns} | xargs ${${PROJECT_NAME}_CLANG_FORMAT} -i
224+
DIFF_COMMAND git diff --diff-filter=ACMRTUXB --name-only master -- ${patterns} | xargs ${${PROJECT_NAME}_CLANG_FORMAT} -i
207225
)
208226
endfunction()

0 commit comments

Comments
 (0)