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

50 lines
1.7 KiB
CMake

option(MYXX_CODE_COVERAGE "MyxxCMake: enable code coverage" OFF)
if(MYXX_CODE_COVERAGE)
find_program(LCOV_EXE NAMES lcov)
find_program(GENHTML_EXE NAMES genhtml)
endif()
function(myxx_code_coverage TARGET_NAME)
if(NOT MYXX_CODE_COVERAGE)
return()
endif()
myx_skip_external_target(${TARGET_NAME})
set(options)
set(oneValueArgs)
set(multiValueArgs EXCLUDE)
cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
set_property(TARGET ${TARGET_NAME} APPEND PROPERTY
COMPILE_OPTIONS --coverage)
set_property(TARGET ${TARGET_NAME} APPEND PROPERTY LINK_FLAGS --coverage)
endif()
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
set_property(TARGET ${TARGET_NAME} APPEND PROPERTY
COMPILE_OPTIONS -fprofile-instr-generate -fcoverage-mapping)
set_property(TARGET ${TARGET_NAME} APPEND PROPERTY LINK_FLAGS --coverage)
endif()
if(LCOV_EXE)
add_custom_target(${TARGET_NAME}-coverage
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
COMMAND ${LCOV_EXE} --test-name ${TARGET_NAME} --output "${TARGET_NAME}.lcov"
--capture
--no-external
--base-directory ${CMAKE_SOURCE_DIR}
--directory ${CMAKE_BINARY_DIR}
--exclude "/usr/\\\*"
--exclude "${CMAKE_BINARY_DIR}/\\\*")
add_dependencies(${TARGET_NAME}-coverage ${TARGET_NAME})
if(GENHTML_EXE)
add_custom_target(${TARGET_NAME}-coverage-report
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
COMMAND ${GENHTML_EXE} --output-directory "${TARGET_NAME}-coverage-html" "${TARGET_NAME}.lcov")
add_dependencies(${TARGET_NAME}-coverage-report ${TARGET_NAME}-coverage)
endif()
endif()
endfunction()