myxx/MyxxCMake/lib/AnalyzeClangTidy.cmake

104 lines
3.2 KiB
CMake
Raw Normal View History

2022-10-05 14:12:28 +00:00
include_guard(GLOBAL)
if(NOT CLANG_TIDY_NAME)
set(CLANG_TIDY_NAMES clang-tidy)
foreach(V RANGE 9 18)
list(INSERT CLANG_TIDY_NAMES 0 "clang-tidy-${V}")
endforeach()
unset(V)
else()
set(CLANG_TIDY_NAMES ${CLANG_TIDY_NAME})
endif()
find_program(CLANG_TIDY_EXE NAMES ${CLANG_TIDY_NAMES})
unset(CLANG_TIDY_NAMES)
2022-10-09 18:15:35 +00:00
function(myxx_analyze_clang_tidy TARGET_NAME)
myx_skip_external_target(${TARGET_NAME})
2022-10-05 14:12:28 +00:00
if(NOT CLANG_TIDY_EXE)
message(STATUS "MyxxCMake: Clang Tidy analyzer is not found")
return()
endif()
2022-10-09 18:15:35 +00:00
if(TARGET ${TARGET_NAME}-analyze-clang-tidy)
message(STATUS "MyxxCMake: target ${TARGET_NAME}-analyze-clang-tidy already exists")
2022-10-05 14:12:28 +00:00
return()
endif()
set(options FIX)
set(oneValueArgs)
set(multiValueArgs EXTRA_ARGS)
cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
2022-10-09 18:15:35 +00:00
set(__cmd_args -extra-arg="-Wno-unknown-warning-option ${ARG_EXTRA_ARGS}")
get_target_property(__sources ${TARGET_NAME} SOURCES)
foreach(iter ${__sources})
2022-10-05 14:12:28 +00:00
string(FIND ${iter} ${CMAKE_BINARY_DIR} pos)
if(pos EQUAL -1)
2022-10-09 18:15:35 +00:00
list(APPEND __filtered_sources ${iter})
2022-10-05 14:12:28 +00:00
endif()
endforeach()
if(ARG_FIX)
2022-10-09 18:15:35 +00:00
list(APPEND __cmd_args "-fix-errors")
2022-10-05 14:12:28 +00:00
endif()
if(NOT TARGET myxx-analyze-clang-tidy)
add_custom_target(myxx-analyze-clang-tidy)
endif()
2022-10-09 18:15:35 +00:00
add_custom_target(${TARGET_NAME}-analyze-clang-tidy
2022-10-05 14:12:28 +00:00
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
2022-10-09 18:15:35 +00:00
COMMAND ${CLANG_TIDY_EXE} ${__cmd_args} -p ${CMAKE_BINARY_DIR} ${__filtered_sources})
add_dependencies(${TARGET_NAME}-analyze-clang-tidy ${TARGET_NAME})
add_dependencies(myxx-analyze-clang-tidy ${TARGET_NAME}-analyze-clang-tidy)
2022-10-05 14:12:28 +00:00
endfunction()
2022-10-15 07:12:14 +00:00
function(myxx_analyze_clang_tidy_naming TARGET_NAME)
myx_skip_external_target(${TARGET_NAME})
if(NOT CLANG_TIDY_EXE)
message(STATUS "MyxxCMake: Clang Tidy analyzer is not found")
return()
endif()
set(options FIX)
set(oneValueArgs CONFIG)
set(multiValueArgs EXTRA_ARGS)
cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
if(NOT ARG_CONFIG)
set(ARG_CONFIG ${PROJECT_SOURCE_DIR}/.clang-tidy-naming)
endif()
if(NOT EXISTS ${ARG_CONFIG})
return()
endif()
if(TARGET ${TARGET_NAME}-analyze-clang-tidy-naming)
message(STATUS "MyxxCMake: target ${TARGET_NAME}-analyze-clang-tidy-naming already exists")
return()
endif()
set(__cmd_args -extra-arg="-Wno-unknown-warning-option ${ARG_EXTRA_ARGS}")
get_target_property(__sources ${TARGET_NAME} SOURCES)
foreach(iter ${__sources})
string(FIND ${iter} ${CMAKE_BINARY_DIR} pos)
if(pos EQUAL -1)
list(APPEND __filtered_sources ${iter})
endif()
endforeach()
if(ARG_FIX)
list(APPEND __cmd_args "-fix-errors")
endif()
if(NOT TARGET myxx-analyze-clang-tidy-naming)
add_custom_target(myxx-analyze-clang-tidy-naming)
endif()
add_custom_target(${TARGET_NAME}-analyze-clang-tidy-naming
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
COMMAND ${CLANG_TIDY_EXE} ${__cmd_args} -p ${CMAKE_BINARY_DIR} ${__filtered_sources})
add_dependencies(${TARGET_NAME}-analyze-clang-tidy-naming ${TARGET_NAME})
add_dependencies(myxx-analyze-clang-tidy-naming ${TARGET_NAME}-analyze-clang-tidy-naming)
endfunction()