myx/MyxCMake/lib/Uncrustify.cmake

111 lines
3.7 KiB
CMake
Raw Normal View History

2022-10-02 20:34:32 +00:00
include_guard(GLOBAL)
find_program(UNCRUSTIFY_EXE NAMES uncrustify)
if(UNCRUSTIFY_EXE)
if(NOT EXISTS ${PROJECT_BINARY_DIR}/uncrustify-classheader.txt)
file(GENERATE OUTPUT ${PROJECT_BINARY_DIR}/uncrustify-classheader.txt CONTENT
"/**
* @class $(fclass)
* @brief TODO
* @details TODO
*/")
endif()
if(NOT EXISTS ${PROJECT_BINARY_DIR}/uncrustify-filefooter.txt)
file(GENERATE OUTPUT ${PROJECT_BINARY_DIR}/uncrustify-filefooter.txt CONTENT
"// EOF $(filename)")
endif()
if(NOT EXISTS ${PROJECT_BINARY_DIR}/uncrustify-fileheader.txt)
file(GENERATE OUTPUT ${PROJECT_BINARY_DIR}/uncrustify-fileheader.txt CONTENT
"/**
* @file $(filename)
* @brief TODO
* @details TODO
*/")
endif()
if(NOT EXISTS ${PROJECT_BINARY_DIR}/uncrustify-funcheader.txt)
file(GENERATE OUTPUT ${PROJECT_BINARY_DIR}/uncrustify-funcheader.txt CONTENT
"/**
* @fn $(fclass)::$(function)
* $(javaparam)
* @details TODO
*/")
endif()
endif()
function(myx_uncrustify target)
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
# Динамически сгенерированные файлы исключаются
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()
2022-10-02 20:34:32 +00:00
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
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)
add_custom_target(${target}-uncrustify
DEPENDS ${PROJECT_BINARY_DIR}/uncrustify-${target}.cfg
2022-10-06 08:10:43 +00:00
COMMAND ${UNCRUSTIFY_EXE} ${UNCRUSTIFY_OPTS} --mtime ${src})
2022-10-02 20:34:32 +00:00
add_custom_target(${target}-uncrustify-append-comments
DEPENDS ${PROJECT_BINARY_DIR}/uncrustify-${target}.cfg
COMMAND ${UNCRUSTIFY_EXE} ${UNCRUSTIFY_OPTS}
--set cmt_insert_class_header=${PROJECT_BINARY_DIR}/uncrustify-classheader.txt
--set cmt_insert_file_footer=${PROJECT_BINARY_DIR}/uncrustify-filefooter.txt
--set cmt_insert_file_header=${PROJECT_BINARY_DIR}/uncrustify-fileheader.txt
--set cmt_insert_func_header=${PROJECT_BINARY_DIR}/uncrustify-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
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()