Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom file patterns in clang-format #9

Merged
merged 2 commits into from
Jul 8, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions ClangFormat.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand Down Expand Up @@ -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})

Expand Down Expand Up @@ -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()