Skip to content

Commit

Permalink
cmake: Introduce BUILD_FOR_COVERAGE build option
Browse files Browse the repository at this point in the history
The new `BUILD_FOR_COVERAGE` build option enables instrumentation for
LLVM's Source-based Code Coverage reports.
  • Loading branch information
hebasto committed Nov 28, 2024
1 parent 7590e93 commit 6f1de69
Show file tree
Hide file tree
Showing 13 changed files with 58 additions and 5 deletions.
24 changes: 24 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ option(BUILD_BENCH "Build bench_bitcoin executable." OFF)
option(BUILD_FUZZ_BINARY "Build fuzz binary." OFF)
option(BUILD_FOR_FUZZING "Build for fuzzing. Enabling this will disable all other targets and override BUILD_FUZZ_BINARY." OFF)

option(BUILD_FOR_COVERAGE "Build with Code Coverage instrumentation" OFF)

option(INSTALL_MAN "Install man pages." ON)

set(APPEND_CPPFLAGS "" CACHE STRING "Preprocessor flags that are appended to the command line after all other flags added by the build system. This variable is intended for debugging and special builds.")
Expand Down Expand Up @@ -372,6 +374,27 @@ if(BUILD_FUZZ_BINARY)
)
endif()

add_library(coverage_interface INTERFACE)
if(BUILD_FOR_COVERAGE)
if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
message(FATAL_ERROR "BUILD_FOR_COVERAGE can only be specified when compiling with Clang.")
endif()
if(NOT PROFILE_FILE_PATTERN)
if(NOT PROFILE_DATA_DIR)
file(TO_NATIVE_PATH "${PROJECT_BINARY_DIR}/profiles" PROFILE_DATA_DIR)
endif()
file(TO_NATIVE_PATH "${PROFILE_DATA_DIR}/%m.profraw" PROFILE_FILE_PATTERN)
endif()
target_compile_options(coverage_interface INTERFACE
-fprofile-instr-generate=${PROFILE_FILE_PATTERN}
-fcoverage-mapping
)
target_link_options(coverage_interface INTERFACE
-fprofile-instr-generate=${PROFILE_FILE_PATTERN}
-fcoverage-mapping
)
endif()

include(AddBoostIfNeeded)
add_boost_if_needed()

Expand Down Expand Up @@ -624,6 +647,7 @@ message(" test_bitcoin ........................ ${BUILD_TESTS}")
message(" test_bitcoin-qt ..................... ${BUILD_GUI_TESTS}")
message(" bench_bitcoin ....................... ${BUILD_BENCH}")
message(" fuzz binary ......................... ${BUILD_FUZZ_BINARY}")
message(" Code Coverage instrumentation ....... ${BUILD_FOR_COVERAGE}")
message("")
if(CMAKE_CROSSCOMPILING)
set(cross_status "TRUE, for ${CMAKE_SYSTEM_NAME}, ${CMAKE_SYSTEM_PROCESSOR}")
Expand Down
12 changes: 12 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ add_library(bitcoin_clientversion OBJECT EXCLUDE_FROM_ALL
target_link_libraries(bitcoin_clientversion
PRIVATE
core_interface
coverage_interface
)
add_dependencies(bitcoin_clientversion generate_build_info)

Expand Down Expand Up @@ -92,6 +93,7 @@ add_library(bitcoin_consensus STATIC EXCLUDE_FROM_ALL
target_link_libraries(bitcoin_consensus
PRIVATE
core_interface
coverage_interface
bitcoin_crypto
secp256k1
)
Expand Down Expand Up @@ -156,6 +158,7 @@ add_library(bitcoin_common STATIC EXCLUDE_FROM_ALL
target_link_libraries(bitcoin_common
PRIVATE
core_interface
coverage_interface
bitcoin_consensus
bitcoin_util
univalue
Expand All @@ -179,6 +182,7 @@ if(ENABLE_WALLET)
add_windows_resources(bitcoin-wallet bitcoin-wallet-res.rc)
target_link_libraries(bitcoin-wallet
core_interface
coverage_interface
bitcoin_wallet
bitcoin_common
bitcoin_util
Expand Down Expand Up @@ -289,6 +293,7 @@ add_library(bitcoin_node STATIC EXCLUDE_FROM_ALL
target_link_libraries(bitcoin_node
PRIVATE
core_interface
coverage_interface
bitcoin_common
bitcoin_util
$<TARGET_NAME_IF_EXISTS:bitcoin_zmq>
Expand All @@ -312,6 +317,7 @@ if(BUILD_DAEMON)
add_windows_resources(bitcoind bitcoind-res.rc)
target_link_libraries(bitcoind
core_interface
coverage_interface
bitcoin_node
$<TARGET_NAME_IF_EXISTS:bitcoin_wallet>
)
Expand All @@ -324,6 +330,7 @@ if(WITH_MULTIPROCESS)
)
target_link_libraries(bitcoin-node
core_interface
coverage_interface
bitcoin_node
bitcoin_ipc
$<TARGET_NAME_IF_EXISTS:bitcoin_wallet>
Expand Down Expand Up @@ -355,6 +362,7 @@ add_library(bitcoin_cli STATIC EXCLUDE_FROM_ALL
target_link_libraries(bitcoin_cli
PUBLIC
core_interface
coverage_interface
univalue
)

Expand All @@ -365,6 +373,7 @@ if(BUILD_CLI)
add_windows_resources(bitcoin-cli bitcoin-cli-res.rc)
target_link_libraries(bitcoin-cli
core_interface
coverage_interface
bitcoin_cli
bitcoin_common
bitcoin_util
Expand All @@ -380,6 +389,7 @@ if(BUILD_TX)
add_windows_resources(bitcoin-tx bitcoin-tx-res.rc)
target_link_libraries(bitcoin-tx
core_interface
coverage_interface
bitcoin_common
bitcoin_util
univalue
Expand All @@ -393,6 +403,7 @@ if(BUILD_UTIL)
add_windows_resources(bitcoin-util bitcoin-util-res.rc)
target_link_libraries(bitcoin-util
core_interface
coverage_interface
bitcoin_common
bitcoin_util
)
Expand Down Expand Up @@ -425,6 +436,7 @@ if(BUILD_UTIL_CHAINSTATE)
target_link_libraries(bitcoin-chainstate
PRIVATE
core_interface
coverage_interface
bitcoinkernel
)
endif()
Expand Down
9 changes: 5 additions & 4 deletions src/crypto/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ add_library(bitcoin_crypto STATIC EXCLUDE_FROM_ALL
target_link_libraries(bitcoin_crypto
PRIVATE
core_interface
coverage_interface
)

if(HAVE_SSE41)
Expand All @@ -33,7 +34,7 @@ if(HAVE_SSE41)
)
target_compile_definitions(bitcoin_crypto_sse41 PUBLIC ENABLE_SSE41)
target_compile_options(bitcoin_crypto_sse41 PRIVATE ${SSE41_CXXFLAGS})
target_link_libraries(bitcoin_crypto_sse41 PRIVATE core_interface)
target_link_libraries(bitcoin_crypto_sse41 PRIVATE core_interface coverage_interface)
target_link_libraries(bitcoin_crypto PRIVATE bitcoin_crypto_sse41)
endif()

Expand All @@ -43,7 +44,7 @@ if(HAVE_AVX2)
)
target_compile_definitions(bitcoin_crypto_avx2 PUBLIC ENABLE_AVX2)
target_compile_options(bitcoin_crypto_avx2 PRIVATE ${AVX2_CXXFLAGS})
target_link_libraries(bitcoin_crypto_avx2 PRIVATE core_interface)
target_link_libraries(bitcoin_crypto_avx2 PRIVATE core_interface coverage_interface)
target_link_libraries(bitcoin_crypto PRIVATE bitcoin_crypto_avx2)
endif()

Expand All @@ -53,7 +54,7 @@ if(HAVE_SSE41 AND HAVE_X86_SHANI)
)
target_compile_definitions(bitcoin_crypto_x86_shani PUBLIC ENABLE_SSE41 ENABLE_X86_SHANI)
target_compile_options(bitcoin_crypto_x86_shani PRIVATE ${X86_SHANI_CXXFLAGS})
target_link_libraries(bitcoin_crypto_x86_shani PRIVATE core_interface)
target_link_libraries(bitcoin_crypto_x86_shani PRIVATE core_interface coverage_interface)
target_link_libraries(bitcoin_crypto PRIVATE bitcoin_crypto_x86_shani)
endif()

Expand All @@ -63,6 +64,6 @@ if(HAVE_ARM_SHANI)
)
target_compile_definitions(bitcoin_crypto_arm_shani PUBLIC ENABLE_ARM_SHANI)
target_compile_options(bitcoin_crypto_arm_shani PRIVATE ${ARM_SHANI_CXXFLAGS})
target_link_libraries(bitcoin_crypto_arm_shani PRIVATE core_interface)
target_link_libraries(bitcoin_crypto_arm_shani PRIVATE core_interface coverage_interface)
target_link_libraries(bitcoin_crypto PRIVATE bitcoin_crypto_arm_shani)
endif()
1 change: 1 addition & 0 deletions src/ipc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ target_capnp_sources(bitcoin_ipc ${PROJECT_SOURCE_DIR}
target_link_libraries(bitcoin_ipc
PRIVATE
core_interface
coverage_interface
univalue
)
1 change: 1 addition & 0 deletions src/kernel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ add_library(bitcoinkernel
target_link_libraries(bitcoinkernel
PRIVATE
core_interface
coverage_interface
bitcoin_clientversion
bitcoin_crypto
leveldb
Expand Down
2 changes: 2 additions & 0 deletions src/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ add_executable(test_bitcoin

target_link_libraries(test_bitcoin
core_interface
coverage_interface
test_util
bitcoin_cli
bitcoin_node
Expand All @@ -165,6 +166,7 @@ if(WITH_MULTIPROCESS)
target_link_libraries(bitcoin_ipc_test
PRIVATE
core_interface
coverage_interface
univalue
)

Expand Down
1 change: 1 addition & 0 deletions src/test/fuzz/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ add_executable(fuzz
)
target_link_libraries(fuzz
core_interface
coverage_interface
test_fuzz
bitcoin_cli
bitcoin_common
Expand Down
1 change: 1 addition & 0 deletions src/test/fuzz/util/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ add_library(test_fuzz STATIC EXCLUDE_FROM_ALL
target_link_libraries(test_fuzz
PRIVATE
core_interface
coverage_interface
test_util
bitcoin_node
Boost::headers
Expand Down
1 change: 1 addition & 0 deletions src/test/util/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ add_library(test_util STATIC EXCLUDE_FROM_ALL
target_link_libraries(test_util
PRIVATE
core_interface
coverage_interface
Boost::headers
PUBLIC
univalue
Expand Down
8 changes: 7 additions & 1 deletion src/univalue/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ target_include_directories(univalue
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
)
target_link_libraries(univalue PRIVATE core_interface)
target_link_libraries(univalue
PRIVATE
core_interface
coverage_interface
)

if(BUILD_TESTS)
add_executable(unitester test/unitester.cpp)
Expand All @@ -23,6 +27,7 @@ if(BUILD_TESTS)
target_link_libraries(unitester
PRIVATE
core_interface
coverage_interface
univalue
)
add_test(NAME univalue_test
Expand All @@ -33,6 +38,7 @@ if(BUILD_TESTS)
target_link_libraries(object
PRIVATE
core_interface
coverage_interface
univalue
)
add_test(NAME univalue_object_test
Expand Down
1 change: 1 addition & 0 deletions src/util/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ add_library(bitcoin_util STATIC EXCLUDE_FROM_ALL
target_link_libraries(bitcoin_util
PRIVATE
core_interface
coverage_interface
bitcoin_clientversion
bitcoin_crypto
$<$<PLATFORM_ID:Windows>:ws2_32>
Expand Down
1 change: 1 addition & 0 deletions src/wallet/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ add_library(bitcoin_wallet STATIC EXCLUDE_FROM_ALL
target_link_libraries(bitcoin_wallet
PRIVATE
core_interface
coverage_interface
bitcoin_common
univalue
Boost::headers
Expand Down
1 change: 1 addition & 0 deletions src/zmq/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ target_compile_definitions(bitcoin_zmq
target_link_libraries(bitcoin_zmq
PRIVATE
core_interface
coverage_interface
univalue
zeromq
)

0 comments on commit 6f1de69

Please sign in to comment.