2020-04-18 16:44:16 +00:00
|
|
|
option(CMLIB_ENABLE_CODE_COVERAGE "Enable code coverage support" OFF)
|
2019-10-09 06:12:42 +00:00
|
|
|
find_program(LCOV_EXE NAMES lcov)
|
|
|
|
find_program(GENHTML_EXE NAMES genhtml)
|
|
|
|
|
2020-04-13 16:44:42 +00:00
|
|
|
function(add_code_coverage target)
|
2019-10-08 13:12:18 +00:00
|
|
|
|
2020-04-18 16:44:16 +00:00
|
|
|
if(CMLIB_ENABLE_CODE_COVERAGE)
|
2019-10-09 06:12:42 +00:00
|
|
|
if(CMAKE_CXX_COMPILER_IS_GCC)
|
2020-04-13 16:44:42 +00:00
|
|
|
target_compile_options(${target} PUBLIC "--coverage")
|
2020-06-26 01:09:40 +00:00
|
|
|
set_property(TARGET ${target} APPEND_STRING PROPERTY LINK_FLAGS " --coverage")
|
2019-10-08 13:33:29 +00:00
|
|
|
|
2019-10-09 06:12:42 +00:00
|
|
|
if(LCOV_EXE)
|
2019-12-03 16:23:14 +00:00
|
|
|
add_custom_target(
|
2020-04-13 16:44:42 +00:00
|
|
|
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})
|
2020-04-13 16:44:42 +00:00
|
|
|
add_dependencies(coverage-${target} ${target})
|
2019-10-09 06:12:42 +00:00
|
|
|
|
|
|
|
if(GENHTML_EXE)
|
2020-04-13 16:44:42 +00:00
|
|
|
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})
|
2019-10-09 06:12:42 +00:00
|
|
|
endif()
|
2019-10-08 13:33:29 +00:00
|
|
|
endif()
|
2019-10-09 06:12:42 +00:00
|
|
|
else()
|
|
|
|
message("Only GCC is supported for code coverage")
|
2019-10-08 13:12:18 +00:00
|
|
|
endif()
|
|
|
|
endif()
|
|
|
|
endfunction()
|