Add clang-format support
# clang_format([DEPENDENCY] [GLOBAL] [REGEX "..."] TARGETS "..." ["..."]) Adds a clang-format call target for each of the specified targets, which optionally filters files using a special regex (defaults to .c, .cpp, .h, .hpp), with an optional global target, and an optional dependency addition to ensure that formatting is applied before building any of the targets. * DEPENDENCY: Add a dependency to each of the targets onto the clang-format target, to ensure that formatting is applied at build time. * GLOBAL: Create a global target and add the clang-format target as a dependency to it. * REGEX "...": Filter file by a custom regular expression instead of the default filter. Must be a regular expression. * TARGETS "...": Specifies the targets that clang-format is used for.
This commit is contained in:
+65
@@ -0,0 +1,65 @@
|
|||||||
|
function(clang_format)
|
||||||
|
cmake_parse_arguments(
|
||||||
|
PARSE_ARGV 0
|
||||||
|
_CLANG_FORMAT
|
||||||
|
"DEPENDENCY;GLOBAL"
|
||||||
|
"REGEX"
|
||||||
|
"TARGETS"
|
||||||
|
)
|
||||||
|
|
||||||
|
find_program(CLANG_FORMAT_BIN "clang-format" DOC "Path (or name) of the clang-format binary")
|
||||||
|
if(NOT CLANG_FORMAT_BIN)
|
||||||
|
message(WARNING "Clang: Could not find clang-format at path '${CLANG_FORMAT_BIN}'. Disabling clang-format...")
|
||||||
|
return()
|
||||||
|
endif()
|
||||||
|
|
||||||
|
if(NOT _CLANG_FORMAT_FILTER)
|
||||||
|
set(_CLANG_FORMAT_FILTER "\.(h|hpp|c|cpp)$")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
foreach(_target ${_CLANG_FORMAT_TARGETS})
|
||||||
|
# get_target_property(target_name ${_target} NAME)
|
||||||
|
|
||||||
|
get_target_property(target_sources_rel ${_target} SOURCES)
|
||||||
|
set(target_sources "")
|
||||||
|
foreach(source_relative ${target_sources_rel})
|
||||||
|
get_filename_component(source_absolute ${source_relative} ABSOLUTE)
|
||||||
|
list(APPEND target_sources ${source_absolute})
|
||||||
|
endforeach()
|
||||||
|
list(FILTER target_sources INCLUDE REGEX "${_CLANG_FORMAT_FILTER}")
|
||||||
|
unset(target_sources_rel)
|
||||||
|
|
||||||
|
get_target_property(target_source_dir_rel ${_target} SOURCE_DIR)
|
||||||
|
get_filename_component(target_source_dir ${target_source_dir_rel} ABSOLUTE)
|
||||||
|
unset(target_source_dir_rel)
|
||||||
|
|
||||||
|
add_custom_target(${_target}_CLANG-FORMAT
|
||||||
|
COMMAND
|
||||||
|
${CLANG_FORMAT_BIN}
|
||||||
|
-style=file
|
||||||
|
-i
|
||||||
|
${target_sources}
|
||||||
|
COMMENT
|
||||||
|
"clang-format: Formatting ${_target}..."
|
||||||
|
WORKING_DIRECTORY
|
||||||
|
${target_source_dir_rel}
|
||||||
|
)
|
||||||
|
|
||||||
|
if(_CLANG_FORMAT_DEPENDENCY)
|
||||||
|
add_dependencies(${_target} ${_target}_CLANG-FORMAT)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
if(_CLANG_FORMAT_GLOBAL)
|
||||||
|
if(TARGET CLANG-FORMAT)
|
||||||
|
add_dependencies(CLANG-FORMAT ${_target}_CLANG-FORMAT)
|
||||||
|
else()
|
||||||
|
add_custom_target(CLANG-FORMAT
|
||||||
|
DEPENDS
|
||||||
|
${_target}_CLANG-FORMAT
|
||||||
|
COMMENT
|
||||||
|
"clang-format: Formatting..."
|
||||||
|
)
|
||||||
|
endif()
|
||||||
|
endif()
|
||||||
|
endforeach()
|
||||||
|
endfunction()
|
||||||
Reference in New Issue
Block a user