2019-10-09 06:12:42 +00:00
|
|
|
option(ENABLE_CODE_COVERAGE "Enable code coverage support" OFF)
|
|
|
|
find_program(LCOV_EXE NAMES lcov)
|
|
|
|
find_program(GENHTML_EXE NAMES genhtml)
|
|
|
|
|
2019-10-08 13:12:18 +00:00
|
|
|
function(add_code_coverage)
|
|
|
|
list(GET ARGN 0 _target)
|
|
|
|
|
2019-10-09 06:12:42 +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})
|
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-11 07:12:20 +00:00
|
|
|
coverage-${_target} WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
|
2020-04-01 18:42:59 +00:00
|
|
|
COMMAND ${LCOV_EXE} --test-name ${_target} --output "${_target}.lcov" --capture --directory
|
|
|
|
${CMAKE_BINARY_DIR})
|
2019-10-09 06:12:42 +00:00
|
|
|
add_dependencies(coverage-${_target} ${_target})
|
|
|
|
|
|
|
|
if(GENHTML_EXE)
|
2020-04-11 07:12:20 +00:00
|
|
|
add_custom_target(coverage-report-${_target} WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
|
|
|
|
COMMAND ${GENHTML_EXE} --output-directory "report-${_target}" "${_target}.lcov")
|
2019-10-09 06:12:42 +00:00
|
|
|
add_dependencies(coverage-report-${_target} coverage-${_target})
|
|
|
|
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()
|