-
Notifications
You must be signed in to change notification settings - Fork 2
/
code_coverage.cmake
63 lines (54 loc) · 1.89 KB
/
code_coverage.cmake
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
include_guard(GLOBAL)
if(NOT PROJECT_IS_TOP_LEVEL)
return()
endif()
if(TARGET generate_code_coverage_report)
return()
endif()
if(MSVC)
return()
endif()
get_property(languages GLOBAL PROPERTY ENABLED_LANGUAGES)
include(${CMAKE_CURRENT_LIST_DIR}/build_type.cmake)
add_custom_build_type_like(coverage debug)
foreach(targettype IN ITEMS EXE SHARED STATIC MODULE)
set(CMAKE_${targettype}_LINKER_FLAGS_${build_type}
"${CMAKE_${targettype}_LINKER_FLAGS_${build_type}} --coverage")
endforeach()
foreach(lang IN ITEMS C CXX)
if(CMAKE_${lang}_COMPILER_ID MATCHES "GNU|Clang")
set(CMAKE_${lang}_FLAGS_COVERAGE
"${CMAKE_${lang}_FLAGS_COVERAGE} -g -O0 --coverage")
if(CMAKE_${lang}_COMPILER_ID STREQUAL "GNU")
string(APPEND CMAKE_${lang}_FLAGS_COVERAGE
"${CMAKE_${lang}_FLAGS_COVERAGE} -fprofile-abs-path")
endif()
endif()
endforeach()
add_custom_command(
OUTPUT ${CMAKE_BINARY_DIR}/code_coverage
COMMAND mkdir -p code_coverage
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
add_custom_target(
do_test_for_code_coverage
COMMAND
${CMAKE_COMMAND} -DCMAKE_BUILD_TYPE=coverage
-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} -DDISABLE_RUNTIME_ANALYSIS=ON
-DCMAKE_BUILD_PARALLEL_LEVEL=${CMAKE_BUILD_PARALLEL_LEVEL}
${CMAKE_SOURCE_DIR}
COMMAND ${CMAKE_COMMAND} --build .
COMMAND ${CMAKE_CTEST_COMMAND} -T Test
DEPENDS ${CMAKE_BINARY_DIR}
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
set(gcov-executable "")
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(gcov-executable "--gcov-executable 'llvm-cov gcov'")
endif()
add_custom_target(
generate_code_coverage_report
COMMAND gcovr --root ${CMAKE_SOURCE_DIR} "${gcov-executable}" --html-details
code_coverage_report.html
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
BYPRODUCTS code_coverage_report)
add_dependencies(generate_code_coverage_report do_test_for_code_coverage)