myx-cmake/MyxCMake/modules/MyxCMakeCodeCoverage.cmake

48 lines
1.5 KiB
CMake
Raw Normal View History

2022-05-06 09:49:46 +00:00
include_guard(GLOBAL)
2021-12-08 11:32:04 +00:00
option(MYX_CMAKE_CODE_COVERAGE "MyxCMake: enable code coverage" OFF)
if(MYX_CMAKE_CODE_COVERAGE)
find_program(LCOV_EXE NAMES lcov)
find_program(GENHTML_EXE NAMES genhtml)
endif()
function(myx_cmake_code_coverage target)
if(MYX_CMAKE_CODE_COVERAGE)
if(CMAKE_CXX_COMPILER_IS_GCC)
target_compile_options(${target} PUBLIC "--coverage")
set_property(
TARGET ${target}
APPEND_STRING
PROPERTY LINK_FLAGS " --coverage")
endif()
if(CMAKE_CXX_COMPILER_IS_CLANG)
target_compile_options(${target} PUBLIC "-fprofile-instr-generate -fcoverage-mapping")
set_property(
TARGET ${target}
APPEND_STRING
PROPERTY LINK_FLAGS " --coverage")
endif()
2021-12-08 11:32:04 +00:00
if(LCOV_EXE)
2022-01-25 17:49:02 +00:00
add_custom_target(${target}-coverage
2021-12-08 11:32:04 +00:00
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
COMMAND ${LCOV_EXE} --test-name ${target} --output "${target}.lcov" --capture
2022-01-11 16:14:31 +00:00
--no-external
--base-directory ${CMAKE_SOURCE_DIR}
--directory ${CMAKE_BINARY_DIR})
# --exclude "/usr/\\\*"
# --exclude "${CMAKE_BINARY_DIR}/\\\*"
2021-12-08 11:32:04 +00:00
add_dependencies(${target}-coverage ${target})
if(GENHTML_EXE)
2022-01-25 17:49:02 +00:00
add_custom_target(${target}-coverage-report
2021-12-08 11:32:04 +00:00
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
2022-01-25 17:49:02 +00:00
COMMAND ${GENHTML_EXE} --output-directory "${target}-coverage-html" "${target}.lcov")
2021-12-08 11:32:04 +00:00
add_dependencies(${target}-coverage-report ${target}-coverage)
endif()
endif()
endif()
endfunction()