From 6f1de6925284efca2d0243097c4a7d39cddbae29 Mon Sep 17 00:00:00 2001 From: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> Date: Thu, 28 Nov 2024 17:10:49 +0000 Subject: [PATCH] cmake: Introduce `BUILD_FOR_COVERAGE` build option The new `BUILD_FOR_COVERAGE` build option enables instrumentation for LLVM's Source-based Code Coverage reports. --- CMakeLists.txt | 24 ++++++++++++++++++++++++ src/CMakeLists.txt | 12 ++++++++++++ src/crypto/CMakeLists.txt | 9 +++++---- src/ipc/CMakeLists.txt | 1 + src/kernel/CMakeLists.txt | 1 + src/test/CMakeLists.txt | 2 ++ src/test/fuzz/CMakeLists.txt | 1 + src/test/fuzz/util/CMakeLists.txt | 1 + src/test/util/CMakeLists.txt | 1 + src/univalue/CMakeLists.txt | 8 +++++++- src/util/CMakeLists.txt | 1 + src/wallet/CMakeLists.txt | 1 + src/zmq/CMakeLists.txt | 1 + 13 files changed, 58 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4b21646ca18f5..4b989e807adfc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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.") @@ -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() @@ -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}") diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 22c850e775595..ae433a1d77036 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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) @@ -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 ) @@ -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 @@ -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 @@ -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 $ @@ -312,6 +317,7 @@ if(BUILD_DAEMON) add_windows_resources(bitcoind bitcoind-res.rc) target_link_libraries(bitcoind core_interface + coverage_interface bitcoin_node $ ) @@ -324,6 +330,7 @@ if(WITH_MULTIPROCESS) ) target_link_libraries(bitcoin-node core_interface + coverage_interface bitcoin_node bitcoin_ipc $ @@ -355,6 +362,7 @@ add_library(bitcoin_cli STATIC EXCLUDE_FROM_ALL target_link_libraries(bitcoin_cli PUBLIC core_interface + coverage_interface univalue ) @@ -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 @@ -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 @@ -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 ) @@ -425,6 +436,7 @@ if(BUILD_UTIL_CHAINSTATE) target_link_libraries(bitcoin-chainstate PRIVATE core_interface + coverage_interface bitcoinkernel ) endif() diff --git a/src/crypto/CMakeLists.txt b/src/crypto/CMakeLists.txt index 03c6972dca010..dca40a94ea7d1 100644 --- a/src/crypto/CMakeLists.txt +++ b/src/crypto/CMakeLists.txt @@ -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) @@ -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() @@ -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() @@ -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() @@ -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() diff --git a/src/ipc/CMakeLists.txt b/src/ipc/CMakeLists.txt index 904d72f56e130..667515660e4d7 100644 --- a/src/ipc/CMakeLists.txt +++ b/src/ipc/CMakeLists.txt @@ -19,5 +19,6 @@ target_capnp_sources(bitcoin_ipc ${PROJECT_SOURCE_DIR} target_link_libraries(bitcoin_ipc PRIVATE core_interface + coverage_interface univalue ) diff --git a/src/kernel/CMakeLists.txt b/src/kernel/CMakeLists.txt index 2e07ba042a40a..23403e5c1a437 100644 --- a/src/kernel/CMakeLists.txt +++ b/src/kernel/CMakeLists.txt @@ -81,6 +81,7 @@ add_library(bitcoinkernel target_link_libraries(bitcoinkernel PRIVATE core_interface + coverage_interface bitcoin_clientversion bitcoin_crypto leveldb diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index ed73e44901558..665b847c1378b 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -148,6 +148,7 @@ add_executable(test_bitcoin target_link_libraries(test_bitcoin core_interface + coverage_interface test_util bitcoin_cli bitcoin_node @@ -165,6 +166,7 @@ if(WITH_MULTIPROCESS) target_link_libraries(bitcoin_ipc_test PRIVATE core_interface + coverage_interface univalue ) diff --git a/src/test/fuzz/CMakeLists.txt b/src/test/fuzz/CMakeLists.txt index 1bf05ee4fbd5e..dfceb548f0d22 100644 --- a/src/test/fuzz/CMakeLists.txt +++ b/src/test/fuzz/CMakeLists.txt @@ -134,6 +134,7 @@ add_executable(fuzz ) target_link_libraries(fuzz core_interface + coverage_interface test_fuzz bitcoin_cli bitcoin_common diff --git a/src/test/fuzz/util/CMakeLists.txt b/src/test/fuzz/util/CMakeLists.txt index f73a1a83c2362..f94b29a033c00 100644 --- a/src/test/fuzz/util/CMakeLists.txt +++ b/src/test/fuzz/util/CMakeLists.txt @@ -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 diff --git a/src/test/util/CMakeLists.txt b/src/test/util/CMakeLists.txt index 5d88d1da3ecf6..1fcf83c444bc1 100644 --- a/src/test/util/CMakeLists.txt +++ b/src/test/util/CMakeLists.txt @@ -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 diff --git a/src/univalue/CMakeLists.txt b/src/univalue/CMakeLists.txt index 96733fe07725d..a1903834767c1 100644 --- a/src/univalue/CMakeLists.txt +++ b/src/univalue/CMakeLists.txt @@ -12,7 +12,11 @@ target_include_directories(univalue PUBLIC $ ) -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) @@ -23,6 +27,7 @@ if(BUILD_TESTS) target_link_libraries(unitester PRIVATE core_interface + coverage_interface univalue ) add_test(NAME univalue_test @@ -33,6 +38,7 @@ if(BUILD_TESTS) target_link_libraries(object PRIVATE core_interface + coverage_interface univalue ) add_test(NAME univalue_object_test diff --git a/src/util/CMakeLists.txt b/src/util/CMakeLists.txt index 4999dbf13f040..886f67df8eb61 100644 --- a/src/util/CMakeLists.txt +++ b/src/util/CMakeLists.txt @@ -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 $<$:ws2_32> diff --git a/src/wallet/CMakeLists.txt b/src/wallet/CMakeLists.txt index 121e6e3c83737..5f67fb289ad51 100644 --- a/src/wallet/CMakeLists.txt +++ b/src/wallet/CMakeLists.txt @@ -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 diff --git a/src/zmq/CMakeLists.txt b/src/zmq/CMakeLists.txt index 19ac722b7ae05..26b1370096b07 100644 --- a/src/zmq/CMakeLists.txt +++ b/src/zmq/CMakeLists.txt @@ -16,6 +16,7 @@ target_compile_definitions(bitcoin_zmq target_link_libraries(bitcoin_zmq PRIVATE core_interface + coverage_interface univalue zeromq )