myxx/MyxxCMake/lib/Coverage.cmake
2022-10-06 21:07:22 +03:00

49 lines
1.6 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)
if(NOT MYXX_CODE_COVERAGE)
return()
endif()
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} APPEND PROPERTY
COMPILE_OPTIONS --coverage)
set_property(TARGET ${target} APPEND PROPERTY LINK_FLAGS --coverage)
endif()
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
set_property(TARGET ${target} APPEND PROPERTY
COMPILE_OPTIONS -fprofile-instr-generate -fcoverage-mapping)
set_property(TARGET ${target} APPEND PROPERTY LINK_FLAGS --coverage)
endif()
if(LCOV_EXE)
add_custom_target(${target}-coverage
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
COMMAND ${LCOV_EXE} --test-name ${target} --output "${target}.lcov"
--capture
--no-external
--base-directory ${CMAKE_SOURCE_DIR}
--directory ${CMAKE_BINARY_DIR}
--exclude "/usr/\\\*"
--exclude "${CMAKE_BINARY_DIR}/\\\*")
add_dependencies(${target}-coverage ${target})
if(GENHTML_EXE)
add_custom_target(${target}-coverage-report
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
COMMAND ${GENHTML_EXE} --output-directory "${target}-coverage-html" "${target}.lcov")
add_dependencies(${target}-coverage-report ${target}-coverage)
endif()
endif()
endfunction()