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() if(NOT ${PROJECT_BINARY_DIR} STREQUAL ${CMAKE_BINARY_DIR}) return() endif() 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(__target_sources ${TARGET_NAME} SOURCES) foreach(__iter ${__target_sources}) string(FIND ${__iter} ${CMAKE_BINARY_DIR} __pos) if(__pos EQUAL -1) list(APPEND __filtered_sources ${__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()