From e178ae5512fd294acf5475ffbf376ebaae77f294 Mon Sep 17 00:00:00 2001 From: Matt Woodward Date: Mon, 8 Jul 2019 06:12:24 -0700 Subject: [PATCH 1/2] Support custom file patterns in clang-format --- ClangFormat.cmake | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/ClangFormat.cmake b/ClangFormat.cmake index 789dd7b..075b0b1 100644 --- a/ClangFormat.cmake +++ b/ClangFormat.cmake @@ -107,7 +107,7 @@ endmacro() function(swift_setup_clang_format) set(argOption "REQUIRED") set(argSingle "SCRIPT") - set(argMulti "CLANG_FORMAT_NAMES") + set(argMulti "CLANG_FORMAT_NAMES" "PATTERNS") cmake_parse_arguments(x "${argOption}" "${argSingle}" "${argMulti}" ${ARGN}) @@ -197,12 +197,16 @@ function(swift_setup_clang_format) message(STATUS "Using ${CLANG_FORMAT}") set(${PROJECT_NAME}_CLANG_FORMAT ${CLANG_FORMAT} CACHE STRING "Absolute path to clang-format for ${PROJECT_NAME}") - # Format all source and header files in the repo, use a git command to build the file list - set(default_patterns '*.[ch]' '*.cpp' '*.cc' '*.hpp') + if(x_PATTERNS) + set(patterns ${x_PATTERNS}) + else() + # Format all source and header files in the repo, use a git command to build the file list + set(patterns '*.[ch]' '*.cpp' '*.cc' '*.hpp') + endif() create_targets( TOP_LEVEL ${top_level_project} - ALL_COMMAND git ls-files ${default_patterns} | xargs ${${PROJECT_NAME}_CLANG_FORMAT} -i - DIFF_COMMAND git diff --diff-filter=ACMRTUXB --name-only master -- ${default_patterns} | xargs ${${PROJECT_NAME}_CLANG_FORMAT} -i + ALL_COMMAND git ls-files ${patterns} | xargs ${${PROJECT_NAME}_CLANG_FORMAT} -i + DIFF_COMMAND git diff --diff-filter=ACMRTUXB --name-only master -- ${patterns} | xargs ${${PROJECT_NAME}_CLANG_FORMAT} -i ) endfunction() From 8769831fce1266a44b62d1976ea38800ed503ab1 Mon Sep 17 00:00:00 2001 From: Matt Woodward Date: Mon, 8 Jul 2019 09:01:31 -0700 Subject: [PATCH 2/2] Add comments --- ClangFormat.cmake | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/ClangFormat.cmake b/ClangFormat.cmake index 075b0b1..7f2601b 100644 --- a/ClangFormat.cmake +++ b/ClangFormat.cmake @@ -5,7 +5,21 @@ # use clang-format to format source code according to a configuration file. 2 types # of targets will be created, one for formatting all files in the repository and the # other for formatting only the files which differ from master. The list of files to -# format is generated by git itself. +# format is generated by git itself using a GLOB pattern. +# +# The default pattern will include the following file types +# *.c *.h *.cpp *.cc *.hpp +# +# A project may provide a custom file matching pattern to override the default +# +# swift_setup_clang_format(PATTERNS '*.c') +# +# will format ONLY *.c files. It is possible to provide a path in the pattern +# +# swift_setup_clang_format(PATTERNS 'src/*.c' 'include/*.h') +# +# will format only *.c files under ${PROJECT_SOURCE_DIR}/src and *.h files under +# ${PROJECT_SOURCE_DIR}/include # # The created targets have the names #