|
5 | 5 | # use clang-format to format source code according to a configuration file. 2 types
|
6 | 6 | # of targets will be created, one for formatting all files in the repository and the
|
7 | 7 | # 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 |
9 | 23 | #
|
10 | 24 | # The created targets have the names
|
11 | 25 | #
|
@@ -107,7 +121,7 @@ endmacro()
|
107 | 121 | function(swift_setup_clang_format)
|
108 | 122 | set(argOption "REQUIRED")
|
109 | 123 | set(argSingle "SCRIPT")
|
110 |
| - set(argMulti "CLANG_FORMAT_NAMES") |
| 124 | + set(argMulti "CLANG_FORMAT_NAMES" "PATTERNS") |
111 | 125 |
|
112 | 126 | cmake_parse_arguments(x "${argOption}" "${argSingle}" "${argMulti}" ${ARGN})
|
113 | 127 |
|
@@ -197,12 +211,16 @@ function(swift_setup_clang_format)
|
197 | 211 | message(STATUS "Using ${CLANG_FORMAT}")
|
198 | 212 | set(${PROJECT_NAME}_CLANG_FORMAT ${CLANG_FORMAT} CACHE STRING "Absolute path to clang-format for ${PROJECT_NAME}")
|
199 | 213 |
|
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() |
202 | 220 |
|
203 | 221 | create_targets(
|
204 | 222 | 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 |
207 | 225 | )
|
208 | 226 | endfunction()
|
0 commit comments