cmatrix/cmake/AddAutoformatTarget.cmake

30 lines
899 B
CMake

find_program(_CLANG_FORMAT clang-format)
if(_CLANG_FORMAT)
add_executable(clang-format IMPORTED)
set_target_properties(clang-format PROPERTIES
IMPORTED_LOCATION ${_CLANG_FORMAT})
endif()
unset(_CLANG_FORMAT)
function(add_autoformat_target target)
get_target_property(source_dir ${target} SOURCE_DIR)
get_target_property(sources ${target} SOURCES)
foreach(source ${sources})
get_property(generated SOURCE ${source} PROPERTY GENERATED)
if(NOT ${generated})
list(APPEND non_generated_sources ${source})
endif()
endforeach()
add_custom_target(autoformat_${target}
COMMAND clang-format -i -- ${non_generated_sources}
WORKING_DIRECTORY ${source_dir}
COMMENT "Autoformating sources of target ${target}"
VERBATIM)
if(NOT TARGET autoformat)
add_custom_target(autoformat)
endif()
add_dependencies(autoformat autoformat_${target})
endfunction()