myxx/MyxxCMake/lib/AnalyzePvsStudio.cmake
2022-10-09 21:15:35 +03:00

72 lines
2.1 KiB
CMake

find_program(PVS_STUDIO_ANALYZER_EXE pvs-studio-analyzer)
find_program(PVS_STUDIO_CONVERTER_EXE plog-converter)
if(${CMAKE_VERSION} VERSION_LESS "3.17.0")
set(MYXX_CMAKE_LIB_PVS_DIR_BACKPORT ${CMAKE_CURRENT_LIST_DIR})
endif()
function(myxx_analyze_pvs_studio TARGET_NAME)
if(${CMAKE_VERSION} VERSION_LESS "3.17.0")
set(CMAKE_CURRENT_FUNCTION_LIST_DIR ${MYXX_CMAKE_LIB_PVS_DIR_BACKPORT})
endif()
myx_skip_external_target(${TARGET_NAME})
if(NOT PVS_STUDIO_ANALYZER_EXE)
message(STATUS "MyxxCMake: pvs-studio-analyzer not found.")
return()
endif()
if(NOT PVS_STUDIO_CONVERTER_EXE)
message(STATUS "MyxxCMake: pvs-studio-converter not found.")
return()
endif()
if(NOT TARGET myxx-analyze-pvs-studio)
add_custom_target(myxx-analyze-pvs-studio)
endif()
set(options)
set(oneValueArgs HEADER MODE)
set(multiValueArgs ARGS)
cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
if(NOT ARG_HEADER)
set(ARG_HEADER ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/pvs-studio/pvs_studio.hpp.in)
endif()
if(NOT ARG_MODE)
set(ARG_MODE "GA:1,2,3;64:1,2;OP:1,2,3;CS:1,2;OWASP:1,2")
endif()
get_target_property(sources ${TARGET_NAME} SOURCES)
foreach(iter ${sources})
string(FIND ${iter} ${CMAKE_BINARY_DIR} pos)
if(pos EQUAL -1)
list(APPEND srcs ${iter})
endif()
endforeach()
pvs_studio_add_target(TARGET ${TARGET_NAME}-analyze-pvs-studio
ANALYZE ${TARGET_NAME}
COMPILE_COMMANDS
HIDE_HELP
OUTPUT
FORMAT errorfile
ARGS --exclude-path ${CMAKE_CURRENT_BINARY_DIR} ${ARG_ARGS}
MODE ${ARG_MODE})
get_target_property(__target_type ${TARGET_NAME} TYPE)
if(${__target_type} STREQUAL INTERFACE_LIBRARY)
set(__target_type INTERFACE)
else()
set(__target_type PRIVATE)
endif()
if(MSVC)
target_compile_options(${TARGET_NAME} BEFORE ${__target_type} /FI ${ARG_HEADER})
else() # GCC/Clang
target_compile_options(${TARGET_NAME} BEFORE ${__target_type} -include ${ARG_HEADER})
endif()
add_dependencies(${TARGET_NAME}-analyze-pvs-studio ${TARGET_NAME})
add_dependencies(myxx-analyze-pvs-studio ${TARGET_NAME}-analyze-pvs-studio)
endfunction()