Skip to content

Commit 36c1eb6

Browse files
committed
Coverage support with LCOV and GCOV
This patch adds a `coverage` target that allows coverage statisitics to be retrieved for the project. It requires that lcov and gcov is installed and that the generator is unix makefiles but this can be improved upon in future releases. To make it work use the coverage build type: ``` cmake -DCMAKE_BUILD_TYPE=Coverage . make coverage ```
1 parent 3314aa4 commit 36c1eb6

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
*.pyc
1111
__pycache__
1212

13+
# lcov
14+
*.lcov
15+
/lcov
16+
1317
# cmake files.
1418
/Testing
1519
CMakeCache.txt

CMakeLists.txt

+21
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,27 @@ if (BENCHMARK_ENABLE_LTO)
6464
endif()
6565
endif()
6666

67+
# Coverage build type
68+
set(CMAKE_CXX_FLAGS_COVERAGE "${CMAKE_CXX_FLAGS_DEBUG}" CACHE STRING
69+
"Flags used by the C++ compiler during coverage builds."
70+
FORCE)
71+
set(CMAKE_EXE_LINKER_FLAGS_COVERAGE
72+
"${CMAKE_EXE_LINKER_FLAGS_DEBUG}" CACHE STRING
73+
"Flags used for linking binaries during coverage builds."
74+
FORCE)
75+
set(CMAKE_SHARED_LINKER_FLAGS_COVERAGE
76+
"${CMAKE_SHARED_LINKER_FLAGS_DEBUG}" CACHE STRING
77+
"Flags used by the shared libraries linker during coverage builds."
78+
FORCE)
79+
mark_as_advanced(
80+
CMAKE_CXX_FLAGS_COVERAGE
81+
CMAKE_EXE_LINKER_FLAGS_COVERAGE
82+
CMAKE_SHARED_LINKER_FLAGS_COVERAGE)
83+
set(CMAKE_BUILD_TYPE "${CMAKE_BUILD_TYPE}" CACHE STRING
84+
"Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel Coverage."
85+
FORCE)
86+
add_cxx_compiler_flag(--coverage COVERAGE)
87+
6788
# C++ feature checks
6889
cxx_feature_check(STD_REGEX)
6990
cxx_feature_check(GNU_POSIX_REGEX)

test/CMakeLists.txt

+42
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,45 @@ compile_benchmark_test(cxx03_test)
4343
set_target_properties(cxx03_test
4444
PROPERTIES COMPILE_FLAGS "${CXX03_FLAGS}")
4545
add_test(cxx03 cxx03_test --benchmark_min_time=0.01)
46+
47+
# Add the coverage command(s)
48+
string(TOLOWER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE_LOWER)
49+
if (${CMAKE_BUILD_TYPE_LOWER} MATCHES "coverage")
50+
find_program(GCOV gcov)
51+
find_program(LCOV lcov)
52+
find_program(GENHTML genhtml)
53+
find_program(CTEST ctest)
54+
if (GCOV AND LCOV AND GENHTML AND CTEST AND HAVE_CXX_FLAG_COVERAGE)
55+
add_custom_command(
56+
OUTPUT ${CMAKE_BINARY_DIR}/lcov/index.html
57+
COMMAND ${LCOV} -q -z -d .
58+
COMMAND ${LCOV} -q --no-external -c -b "${CMAKE_SOURCE_DIR}" -d . -o before.lcov -i
59+
COMMAND ${CTEST} --force-new-ctest-process
60+
COMMAND ${LCOV} -q --no-external -c -b "${CMAKE_SOURCE_DIR}" -d . -o after.lcov
61+
COMMAND ${LCOV} -q -a before.lcov -a after.lcov --output-file final.lcov
62+
COMMAND ${LCOV} -q -r final.lcov "'${CMAKE_SOURCE_DIR}/test/*'" -o final.lcov
63+
COMMAND ${GENHTML} final.lcov -o lcov --demangle-cpp --sort -p "${CMAKE_BINARY_DIR}" -t benchmark
64+
DEPENDS filter_test benchmark_test options_test basic_test fixture_test cxx03_test
65+
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
66+
COMMENT "Running LCOV"
67+
)
68+
add_custom_target(coverage
69+
DEPENDS ${CMAKE_BINARY_DIR}/lcov/index.html
70+
COMMENT "LCOV report at lcov/index.html"
71+
)
72+
message(STATUS "Coverage command added")
73+
else()
74+
if (HAVE_CXX_FLAG_COVERAGE)
75+
set(CXX_FLAG_COVERAGE_MESSAGE supported)
76+
else()
77+
set(CXX_FLAG_COVERAGE_MESSAGE unavailable)
78+
endif()
79+
message(WARNING
80+
"Coverage not available:\n"
81+
" gcov: ${GCOV}\n"
82+
" lcov: ${LCOV}\n"
83+
" genhtml: ${GENHTML}\n"
84+
" ctest: ${CTEST}\n"
85+
" --coverage flag: ${CXX_FLAG_COVERAGE_MESSAGE}")
86+
endif()
87+
endif()

0 commit comments

Comments
 (0)