Repository navigation
Expand file tree
/
Copy pathCodeCoverage.cmake
More file actions
48 lines (43 loc) · 2.31 KB
/
Copy pathCodeCoverage.cmake
File metadata and controls
48 lines (43 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# Code coverage configuration
function(enable_coverage target_name)
option(ENABLE_COVERAGE "Enable code coverage" OFF)
if(ENABLE_COVERAGE)
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
target_compile_options(${target_name} INTERFACE
--coverage
-O0
-g
)
target_link_options(${target_name} INTERFACE --coverage)
message(STATUS "Code coverage enabled for target: ${target_name}")
# Add custom target for generating coverage report
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
find_program(GCOV gcov)
find_program(LCOV lcov)
find_program(GENHTML genhtml)
if(LCOV AND GENHTML)
add_custom_target(coverage
COMMAND ${LCOV} --directory . --zerocounters
COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure
COMMAND ${LCOV} --directory . --capture --output-file coverage_raw.info --ignore-errors mismatch
COMMAND ${LCOV} --remove coverage_raw.info '/usr/*' '*/tests/*' --output-file coverage.info --ignore-errors mismatch,unused
COMMAND ${GENHTML} coverage.info --output-directory coverage_html --ignore-errors mismatch
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
COMMENT "Generating code coverage report"
)
message(STATUS "Coverage report can be generated with: make coverage")
endif()
elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
find_program(LLVM_COV llvm-cov)
find_program(LLVM_PROFDATA llvm-profdata)
if(LLVM_COV AND LLVM_PROFDATA)
message(STATUS "For coverage with Clang, run tests and then:")
message(STATUS " llvm-profdata merge -sparse default.profraw -o default.profdata")
message(STATUS " llvm-cov show <test-executable> -instr-profile=default.profdata")
endif()
endif()
else()
message(WARNING "Code coverage is not supported for ${CMAKE_CXX_COMPILER_ID}")
endif()
endif()
endfunction()