cmlib/CMLibCodeCoverage.cmake

32 lines
1.2 KiB
CMake
Raw Normal View History

option(ENABLE_CODE_COVERAGE "Enable code coverage support" OFF)
find_program(LCOV_EXE NAMES lcov)
find_program(GENHTML_EXE NAMES genhtml)
function(add_code_coverage target)
2019-10-08 13:12:18 +00:00
if(ENABLE_CODE_COVERAGE)
if(CMAKE_CXX_COMPILER_IS_GCC)
target_compile_options(${target} PUBLIC "--coverage")
get_target_property(LF ${target} LINK_FLAGS)
string(APPEND LF " --coverage")
set_target_properties(${target} PROPERTIES LINK_FLAGS ${LF})
if(LCOV_EXE)
2019-12-03 16:23:14 +00:00
add_custom_target(
coverage-${target} WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
COMMAND ${LCOV_EXE} --test-name ${target} --output "${target}.lcov" --capture --directory
2020-04-01 18:42:59 +00:00
${CMAKE_BINARY_DIR})
add_dependencies(coverage-${target} ${target})
if(GENHTML_EXE)
add_custom_target(coverage-report-${target} WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
COMMAND ${GENHTML_EXE} --output-directory "report-${target}" "${target}.lcov")
add_dependencies(coverage-report-${target} coverage-${target})
endif()
endif()
else()
message("Only GCC is supported for code coverage")
2019-10-08 13:12:18 +00:00
endif()
endif()
endfunction()