myx/MyxCMake/lib/uncrustify/Uncrustify.cmake

88 lines
3.3 KiB
CMake
Raw Normal View History

2022-10-02 20:34:32 +00:00
include_guard(GLOBAL)
2022-10-06 12:14:38 +00:00
if(${CMAKE_VERSION} VERSION_LESS "3.17.0")
set(MYX_CMAKE_LIB_UNCRUSTIFY_DIR_BACKPORT "${CMAKE_CURRENT_LIST_DIR}")
2022-10-02 20:34:32 +00:00
endif()
2022-10-06 12:14:38 +00:00
find_program(UNCRUSTIFY_EXE NAMES uncrustify)
2022-10-02 20:34:32 +00:00
2022-10-08 14:23:13 +00:00
function(myx_uncrustify NAME)
2022-10-06 12:14:38 +00:00
if(${CMAKE_VERSION} VERSION_LESS "3.17.0")
set(CMAKE_CURRENT_FUNCTION_LIST_DIR ${MYX_CMAKE_LIB_UNCRUSTIFY_DIR_BACKPORT})
endif()
2022-10-02 20:34:32 +00:00
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")
2022-10-04 07:08:19 +00:00
if(NOT EXISTS ${ARG_CONFIG})
set(ARG_CONFIG "${CMAKE_SOURCE_DIR}/.uncrustify.cfg")
endif()
2022-10-02 20:34:32 +00:00
endif()
2022-10-04 07:08:19 +00:00
if(NOT EXISTS ${ARG_CONFIG})
2022-10-05 07:01:02 +00:00
myx_message_notice("MyxCMake: uncrustify config is not found")
2022-10-02 20:34:32 +00:00
return()
endif()
2022-10-04 07:08:19 +00:00
if(NOT UNCRUSTIFY_EXE)
2022-10-05 07:01:02 +00:00
myx_message_notice("MyxCMake: uncrustify executable is not found")
2022-10-02 20:34:32 +00:00
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()
2022-10-06 08:10:43 +00:00
# Динамически сгенерированные файлы исключаются
2022-10-08 14:23:13 +00:00
get_target_property(target_type ${NAME} TYPE)
if(${target_type} STREQUAL "INTERFACE_LIBRARY")
get_target_property(s ${NAME} INTERFACE_SOURCES)
else()
get_target_property(s ${NAME} SOURCES)
endif()
2022-10-06 08:10:43 +00:00
foreach(iter ${s})
string(FIND ${iter} ${CMAKE_BINARY_DIR} pos)
if(pos EQUAL -1)
list(APPEND src ${iter})
endif()
endforeach()
2022-10-08 14:23:13 +00:00
add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/uncrustify-${NAME}.cfg
2022-10-02 20:34:32 +00:00
COMMAND ${UNCRUSTIFY_EXE} --update-config-with-doc
-c ${ARG_CONFIG}
2022-10-08 14:23:13 +00:00
-o ${PROJECT_BINARY_DIR}/uncrustify-${NAME}.cfg)
list(APPEND UNCRUSTIFY_OPTS -c ${PROJECT_BINARY_DIR}/uncrustify-${NAME}.cfg)
2022-10-02 20:34:32 +00:00
# cmake-format: off
2022-10-08 14:23:13 +00:00
add_custom_target(${NAME}-uncrustify-check
DEPENDS ${PROJECT_BINARY_DIR}/uncrustify-${NAME}.cfg
2022-10-06 08:10:43 +00:00
COMMAND ${UNCRUSTIFY_EXE} ${UNCRUSTIFY_OPTS} --check ${src})
2022-10-02 20:34:32 +00:00
list(APPEND UNCRUSTIFY_OPTS --replace --no-backup)
2022-10-08 14:23:13 +00:00
add_custom_target(${NAME}-uncrustify
DEPENDS ${PROJECT_BINARY_DIR}/uncrustify-${NAME}.cfg
2022-10-06 08:10:43 +00:00
COMMAND ${UNCRUSTIFY_EXE} ${UNCRUSTIFY_OPTS} --mtime ${src})
2022-10-02 20:34:32 +00:00
2022-10-08 14:23:13 +00:00
add_custom_target(${NAME}-uncrustify-append-comments
DEPENDS ${PROJECT_BINARY_DIR}/uncrustify-${NAME}.cfg
2022-10-02 20:34:32 +00:00
COMMAND ${UNCRUSTIFY_EXE} ${UNCRUSTIFY_OPTS}
2022-10-06 12:14:38 +00:00
--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
2022-10-06 08:10:43 +00:00
--set cmt_insert_before_ctor_dtor=true --mtime ${src})
2022-10-02 20:34:32 +00:00
# cmake-format: on
2022-10-08 14:23:13 +00:00
add_dependencies(myx-uncrustify ${NAME}-uncrustify)
add_dependencies(myx-uncrustify-check ${NAME}-uncrustify-check)
add_dependencies(myx-uncrustify-append-comments ${NAME}-uncrustify-append-comments)
2022-10-02 20:34:32 +00:00
endfunction()