myx-cmake-example-features/cmake/myx/lib/uncrustify/Uncrustify.cmake
2022-10-06 15:22:57 +03:00

85 lines
3.1 KiB
CMake

include_guard(GLOBAL)
if(${CMAKE_VERSION} VERSION_LESS "3.17.0")
set(MYX_CMAKE_LIB_UNCRUSTIFY_DIR_BACKPORT "${CMAKE_CURRENT_LIST_DIR}")
endif()
find_program(UNCRUSTIFY_EXE NAMES uncrustify)
function(myx_uncrustify target)
if(${CMAKE_VERSION} VERSION_LESS "3.17.0")
set(CMAKE_CURRENT_FUNCTION_LIST_DIR ${MYX_CMAKE_LIB_UNCRUSTIFY_DIR_BACKPORT})
endif()
set(options)
set(oneValueArgs CONFIG)
set(multiValueArgs)
cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
if(NOT ARG_CONFIG)
set(ARG_CONFIG "${PROJECT_SOURCE_DIR}/.uncrustify.cfg")
if(NOT EXISTS ${ARG_CONFIG})
set(ARG_CONFIG "${CMAKE_SOURCE_DIR}/.uncrustify.cfg")
endif()
endif()
if(NOT EXISTS ${ARG_CONFIG})
myx_message_notice("MyxCMake: uncrustify config is not found")
return()
endif()
if(NOT UNCRUSTIFY_EXE)
myx_message_notice("MyxCMake: uncrustify executable is not found")
return()
endif()
if(NOT TARGET myx-uncrustify)
add_custom_target(myx-uncrustify)
endif()
if(NOT TARGET myx-uncrustify-check)
add_custom_target(myx-uncrustify-check)
endif()
if(NOT TARGET myx-uncrustify-append-comments)
add_custom_target(myx-uncrustify-append-comments)
endif()
# Динамически сгенерированные файлы исключаются
get_target_property(s ${target} SOURCES)
foreach(iter ${s})
string(FIND ${iter} ${CMAKE_BINARY_DIR} pos)
if(pos EQUAL -1)
list(APPEND src ${iter})
endif()
endforeach()
add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/uncrustify-${target}.cfg
COMMAND ${UNCRUSTIFY_EXE} --update-config-with-doc
-c ${ARG_CONFIG}
-o ${PROJECT_BINARY_DIR}/uncrustify-${target}.cfg)
list(APPEND UNCRUSTIFY_OPTS -c ${PROJECT_BINARY_DIR}/uncrustify-${target}.cfg)
# cmake-format: off
add_custom_target(${target}-uncrustify-check
DEPENDS ${PROJECT_BINARY_DIR}/uncrustify-${target}.cfg
COMMAND ${UNCRUSTIFY_EXE} ${UNCRUSTIFY_OPTS} --check ${src})
list(APPEND UNCRUSTIFY_OPTS --replace --no-backup)
add_custom_target(${target}-uncrustify
DEPENDS ${PROJECT_BINARY_DIR}/uncrustify-${target}.cfg
COMMAND ${UNCRUSTIFY_EXE} ${UNCRUSTIFY_OPTS} --mtime ${src})
add_custom_target(${target}-uncrustify-append-comments
DEPENDS ${PROJECT_BINARY_DIR}/uncrustify-${target}.cfg
COMMAND ${UNCRUSTIFY_EXE} ${UNCRUSTIFY_OPTS}
--set cmt_insert_class_header=${CMAKE_CURRENT_FUNCTION_LIST_DIR}/classheader.txt
--set cmt_insert_file_footer=${CMAKE_CURRENT_FUNCTION_LIST_DIR}/filefooter.txt
--set cmt_insert_file_header=${CMAKE_CURRENT_FUNCTION_LIST_DIR}/fileheader.txt
--set cmt_insert_func_header=${CMAKE_CURRENT_FUNCTION_LIST_DIR}/funcheader.txt
--set cmt_insert_before_ctor_dtor=true --mtime ${src})
# cmake-format: on
add_dependencies(myx-uncrustify ${target}-uncrustify)
add_dependencies(myx-uncrustify-check ${target}-uncrustify-check)
add_dependencies(myx-uncrustify-append-comments ${target}-uncrustify-append-comments)
endfunction()