Skip to content

Commit e3b76c7

Browse files
committed
Build coverage reports using CMake
Understanding which code is covered by our tests may in future enable automation to check whether code modifications are covered by tests.
1 parent 84f150b commit e3b76c7

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

CMakeLists.txt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,44 @@ if(${enable_cbmc_tests})
3939
enable_testing()
4040
endif()
4141

42+
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR
43+
"${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang" OR
44+
"${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU"
45+
)
46+
set(enable_coverage off CACHE BOOL "Build with coverage recording")
47+
set(parallel_tests "1" CACHE STRING "Number of tests to run in parallel")
48+
if(${enable_coverage})
49+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --coverage -g")
50+
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage -g")
51+
if (NOT DEFINED CODE_COVERAGE_OUTPUT_DIR)
52+
set(CODE_COVERAGE_OUTPUT_DIR ${CMAKE_BINARY_DIR}/html)
53+
set(CODE_COVERAGE_INFO_FILE ${CODE_COVERAGE_OUTPUT_DIR}/coverage.info)
54+
endif()
55+
find_program(CODE_COVERAGE_LCOV lcov)
56+
find_program(CODE_COVERAGE_GENHTML genhtml)
57+
add_custom_target(coverage
58+
COMMAND ${CMAKE_COMMAND} -E make_directory ${CODE_COVERAGE_OUTPUT_DIR}
59+
COMMAND ctest -V -L CORE -j${parallel_tests}
60+
COMMAND ${CODE_COVERAGE_LCOV} ${LCOV_FLAGS} --capture --directory ${CMAKE_BINARY_DIR} --output-file ${CODE_COVERAGE_INFO_FILE}
61+
COMMAND ${CODE_COVERAGE_LCOV} ${LCOV_FLAGS} --remove ${CODE_COVERAGE_INFO_FILE} '/usr/*' --output-file ${CODE_COVERAGE_INFO_FILE}
62+
COMMAND ${CODE_COVERAGE_GENHTML} ${CODE_COVERAGE_INFO_FILE} --output-directory ${CODE_COVERAGE_OUTPUT_DIR}
63+
DEPENDS
64+
java-models-library java-unit unit
65+
"$<TARGET_FILE:cbmc>"
66+
"$<TARGET_FILE:driver>"
67+
"$<TARGET_FILE:goto-analyzer>"
68+
"$<TARGET_FILE:goto-cc>"
69+
"$<TARGET_FILE:goto-diff>"
70+
"$<TARGET_FILE:goto-instrument>"
71+
"$<TARGET_FILE:janalyzer>"
72+
"$<TARGET_FILE:jbmc>"
73+
"$<TARGET_FILE:jdiff>"
74+
"$<TARGET_FILE:smt2_solver>"
75+
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
76+
)
77+
endif()
78+
endif()
79+
4280
add_subdirectory(src)
4381
add_subdirectory(regression)
4482
add_subdirectory(unit)

doc/architectural/compilation-and-development.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,23 @@ For more information on the structure of `unit/` and how to tag tests, see
179179
repository](https://github.com/diffblue/cbmc/blob/develop/CODING_STANDARD.md#unit-tests)
180180

181181

182+
\subsection compilation-and-development-subsection-coverage Test coverage
183+
184+
On Unix-style systems you can automatically generate a code coverage report. To
185+
obtain an HTML report for the test and unit tests, first build the dedicated
186+
coverage configuration using CMake (setting `enable_coverage` and building the
187+
`coverage` target):
188+
189+
cmake -H. -Bcov-build -Denable_coverage=1 -Dparallel_tests=2
190+
make -C cov-build coverage
191+
192+
This configures a build environment in the `cov-build/` folder with coverage
193+
recording at runtime enabled. The actual build (using `make` in the above case)
194+
will run the test suite, running `parallel_tests`-many tests concurrently (in
195+
the above case: 2). The HTML report is generated using `lcov` and stored in
196+
`cov-build/html/`.
197+
198+
182199
\subsection compilation-and-development-subsection-sat-solver Using a different SAT solver
183200

184201
By default, CBMC will assume MiniSat 2 as the SAT back-end. Several other

0 commit comments

Comments
 (0)