Skip to content

Commit 62a97b7

Browse files
committed
cmake: Add Coverage script
1 parent 8e3ff8d commit 62a97b7

File tree

4 files changed

+80
-7
lines changed

4 files changed

+80
-7
lines changed

CMakeLists.txt

+8
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,14 @@ else()
478478
unset(c_flags_debug_overridden)
479479
endif()
480480

481+
set(CMAKE_C_FLAGS_COVERAGE "-Og --coverage")
482+
set(CMAKE_CXX_FLAGS_COVERAGE "-Og --coverage")
483+
set(CMAKE_OBJCXX_FLAGS_COVERAGE "-Og --coverage")
484+
set(CMAKE_EXE_LINKER_FLAGS_COVERAGE "--coverage")
485+
set(CMAKE_SHARED_LINKER_FLAGS_COVERAGE "--coverage")
486+
487+
configure_file(cmake/script/Coverage.cmake.in Coverage.cmake @ONLY)
488+
481489
include(cmake/optional.cmake)
482490

483491
# Don't allow extended (non-ASCII) symbols in identifiers. This is easier for code review.

cmake/cov_tool_wrapper.sh.in

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Copyright (c) 2024-present The Bitcoin Core developers
2+
# Distributed under the MIT software license, see the accompanying
3+
# file COPYING or https://opensource.org/license/mit/.
4+
5+
exec @COV_TOOL@ "$@"

cmake/script/Coverage.cmake.in

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Copyright (c) 2024-present The Bitcoin Core developers
2+
# Distributed under the MIT software license, see the accompanying
3+
# file COPYING or https://opensource.org/license/mit/.
4+
5+
find_program(LCOV_EXECUTABLE lcov REQUIRED)
6+
find_program(GENHTML_EXECUTABLE genhtml REQUIRED)
7+
8+
if("@CMAKE_CXX_COMPILER_ID@" STREQUAL "Clang")
9+
find_program(LLVM_COV_EXECUTABLE llvm-cov REQUIRED)
10+
set(COV_TOOL "${LLVM_COV_EXECUTABLE} gcov")
11+
else()
12+
find_program(GCOV_EXECUTABLE gcov REQUIRED)
13+
set(COV_TOOL "${GCOV_EXECUTABLE}")
14+
endif()
15+
16+
configure_file(
17+
cmake/cov_tool_wrapper.sh.in ${CMAKE_CURRENT_LIST_DIR}/cov_tool_wrapper.sh
18+
FILE_PERMISSIONS OWNER_READ OWNER_EXECUTE
19+
GROUP_READ GROUP_EXECUTE
20+
WORLD_READ
21+
@ONLY
22+
)
23+
24+
set(LCOV_OPTS)
25+
foreach(setting IN LISTS LCOV_RC)
26+
list(APPEND LCOV_OPTS --rc ${setting})
27+
endforeach()
28+
29+
set(LCOV_COMMAND ${LCOV_EXECUTABLE} --gcov-tool ${CMAKE_CURRENT_LIST_DIR}/cov_tool_wrapper.sh ${LCOV_OPTS})
30+
31+
set(LCOV_FILTER_PATTERN)
32+
list(APPEND LCOV_FILTER_PATTERN -p "/usr/local/")
33+
list(APPEND LCOV_FILTER_PATTERN -p "/usr/include/")
34+
list(APPEND LCOV_FILTER_PATTERN -p "/usr/lib/")
35+
list(APPEND LCOV_FILTER_PATTERN -p "/usr/lib64/")
36+
list(APPEND LCOV_FILTER_PATTERN -p "src/leveldb/")
37+
list(APPEND LCOV_FILTER_PATTERN -p "src/crc32c/")
38+
list(APPEND LCOV_FILTER_PATTERN -p "src/bench/")
39+
list(APPEND LCOV_FILTER_PATTERN -p "src/crypto/ctaes")
40+
list(APPEND LCOV_FILTER_PATTERN -p "src/minisketch")
41+
list(APPEND LCOV_FILTER_PATTERN -p "src/secp256k1")
42+
list(APPEND LCOV_FILTER_PATTERN -p "depends")
43+
44+
if(JOBS)
45+
list(APPEND CMAKE_CTEST_COMMAND -j ${JOBS})
46+
endif()
47+
48+
execute_process(COMMAND ${LCOV_COMMAND} --capture --initial --directory ${CMAKE_CURRENT_LIST_DIR}/src --output-file ${CMAKE_CURRENT_LIST_DIR}/baseline.info)
49+
execute_process(COMMAND contrib/filter-lcov.py ${LCOV_FILTER_PATTERN} ${CMAKE_CURRENT_LIST_DIR}/baseline.info ${CMAKE_CURRENT_LIST_DIR}/baseline_filtered.info)
50+
execute_process(COMMAND ${LCOV_COMMAND} --add-tracefile ${CMAKE_CURRENT_LIST_DIR}/baseline_filtered.info --output-file ${CMAKE_CURRENT_LIST_DIR}/baseline_filtered.info)
51+
execute_process(COMMAND ${CMAKE_CTEST_COMMAND} --build-config Coverage --test-dir ${CMAKE_CURRENT_LIST_DIR})
52+
execute_process(COMMAND ${LCOV_COMMAND} --capture --directory ${CMAKE_CURRENT_LIST_DIR}/src --test-name test_bitcoin --output-file ${CMAKE_CURRENT_LIST_DIR}/test_bitcoin.info)
53+
execute_process(COMMAND ${LCOV_COMMAND} --zerocounters --directory ${CMAKE_CURRENT_LIST_DIR}/src)
54+
execute_process(COMMAND contrib/filter-lcov.py ${LCOV_FILTER_PATTERN} ${CMAKE_CURRENT_LIST_DIR}/test_bitcoin.info ${CMAKE_CURRENT_LIST_DIR}/test_bitcoin_filtered.info)
55+
execute_process(COMMAND ${LCOV_COMMAND} --add-tracefile ${CMAKE_CURRENT_LIST_DIR}/test_bitcoin_filtered.info --output-file ${CMAKE_CURRENT_LIST_DIR}/test_bitcoin_filtered.info)
56+
execute_process(COMMAND ${GENHTML_EXECUTABLE} ${LCOV_OPTS} --show-details ${CMAKE_CURRENT_LIST_DIR}/test_bitcoin_filtered.info --output-directory ${CMAKE_CURRENT_LIST_DIR}/test_bitcoin.coverage)

doc/developer-notes.md

+11-7
Original file line numberDiff line numberDiff line change
@@ -481,20 +481,24 @@ $ ./test/functional/test_runner.py --valgrind
481481

482482
### Compiling for test coverage
483483

484-
LCOV can be used to generate a test coverage report based upon `make check`
484+
LCOV can be used to generate a test coverage report based upon `ctest`
485485
execution. LCOV must be installed on your system (e.g. the `lcov` package
486486
on Debian/Ubuntu).
487487

488488
To enable LCOV report generation during test runs:
489489

490490
```shell
491-
./configure --enable-lcov
492-
make
493-
make cov
491+
cmake -B build -DCMAKE_BUILD_TYPE=Coverage
492+
cmake --build build
493+
cmake -P build/Coverage.cmake
494494

495-
# A coverage report will now be accessible at `./test_bitcoin.coverage/index.html`,
496-
# which covers unit tests, and `./total.coverage/index.html`, which covers
497-
# unit and functional tests.
495+
# A coverage report will now be accessible at `./build/test_bitcoin.coverage/index.html`,
496+
# which covers unit tests.
497+
```
498+
499+
To enable test parallelism and testing branch coverage:
500+
```
501+
cmake -DJOBS=$(nproc) -DLCOV_RC="lcov_branch_coverage=1" -P build/Coverage.cmake
498502
```
499503

500504
### Performance profiling with perf

0 commit comments

Comments
 (0)