diff --git a/ClangFormat.cmake b/ClangFormat.cmake index 789dd7b..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 # @@ -107,7 +121,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 +211,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()