myx/MyxCMake/lib/uncrustify/Uncrustify.cmake

99 lines
3.5 KiB
CMake
Raw Normal View History

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-15 07:14:08 +00:00
function(myx_uncrustify TARGET_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)
2022-11-27 04:09:09 +00:00
set(ARG_CONFIG "${PROJECT_SOURCE_DIR}/uncrustify.cfg")
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()
get_filename_component(CONFIG_DIR ${ARG_CONFIG} DIRECTORY)
if(NOT "${CMAKE_SOURCE_DIR}" STREQUAL "${CONFIG_DIR}")
myx_message("MyxCMake: skip uncrustify for project ${PROJECT_NAME}")
return()
endif()
2022-10-02 20:34:32 +00:00
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-20 09:45:27 +00:00
get_target_property(__target_type ${TARGET_NAME} TYPE)
2022-10-21 00:15:34 +00:00
if((${__target_type} STREQUAL "INTERFACE_LIBRARY") AND (${CMAKE_VERSION} VERSION_LESS "3.17.0"))
2022-10-20 09:45:27 +00:00
get_target_property(__s1 ${TARGET_NAME} INTERFACE_SOURCES)
if(__s1)
list(APPEND __all_sources ${__s1})
endif()
else()
get_target_property(__s2 ${TARGET_NAME} SOURCES)
if(__s2)
list(APPEND __all_sources ${__s2})
endif()
2022-10-19 11:39:26 +00:00
endif()
foreach(iter ${__all_sources})
2022-10-06 08:10:43 +00:00
string(FIND ${iter} ${CMAKE_BINARY_DIR} pos)
if(pos EQUAL -1)
2022-10-19 11:39:26 +00:00
list(APPEND __sources ${iter})
2022-10-06 08:10:43 +00:00
endif()
endforeach()
2022-10-15 07:14:08 +00:00
2022-11-27 12:31:19 +00:00
target_sources(${TARGET_NAME} PRIVATE ${ARG_CONFIG})
2022-10-15 07:14:08 +00:00
set(__fixed_config ${PROJECT_BINARY_DIR}/uncrustify-${TARGET_NAME}.cfg)
2022-10-21 07:52:33 +00:00
add_custom_command(OUTPUT ${__fixed_config}
DEPENDS ${ARG_CONFIG}
2022-10-02 20:34:32 +00:00
COMMAND ${UNCRUSTIFY_EXE} --update-config-with-doc
2022-10-15 07:14:08 +00:00
-c ${ARG_CONFIG} -o ${__fixed_config})
list(APPEND __options -c ${__fixed_config})
2022-10-02 20:34:32 +00:00
# cmake-format: off
2022-10-15 07:14:08 +00:00
add_custom_target(${TARGET_NAME}-uncrustify-check
DEPENDS ${__fixed_config}
2022-10-19 11:39:26 +00:00
COMMAND ${UNCRUSTIFY_EXE} ${__options} --check ${__sources})
2022-10-02 20:34:32 +00:00
2022-10-15 07:14:08 +00:00
list(APPEND __options --replace --no-backup)
add_custom_target(${TARGET_NAME}-uncrustify
DEPENDS ${__fixed_config}
2022-10-19 11:39:26 +00:00
COMMAND ${UNCRUSTIFY_EXE} ${__options} --mtime ${__sources})
2022-10-02 20:34:32 +00:00
2022-10-15 07:14:08 +00:00
add_custom_target(${TARGET_NAME}-uncrustify-append-comments
DEPENDS ${__fixed_config}
COMMAND ${UNCRUSTIFY_EXE} ${__options}
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-19 11:39:26 +00:00
--set cmt_insert_before_ctor_dtor=true --mtime ${__sources})
2022-10-02 20:34:32 +00:00
# cmake-format: on
2022-10-15 07:14:08 +00:00
add_dependencies(myx-uncrustify ${TARGET_NAME}-uncrustify)
add_dependencies(myx-uncrustify-check ${TARGET_NAME}-uncrustify-check)
add_dependencies(myx-uncrustify-append-comments ${TARGET_NAME}-uncrustify-append-comments)
2022-10-02 20:34:32 +00:00
endfunction()