diff --git a/CMakeLists.txt b/CMakeLists.txt index ed3178b269773..9b17db84c99da 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,6 +27,7 @@ project("Bitcoin Core" LANGUAGES CXX ASM ) +set(PACKAGE_NAME ${PROJECT_NAME}) set(CLIENT_VERSION_IS_RELEASE "false") set(COPYRIGHT_YEAR "2023") set(COPYRIGHT_HOLDERS "The %s developers") @@ -39,13 +40,23 @@ list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/module) # When adding a new option, end the with a full stop for consistency. include(CMakeDependentOption) option(BUILD_DAEMON "Build bitcoind executable." ON) +option(BUILD_CLI "Build bitcoin-cli executable." ON) +option(BUILD_TX "Build bitcoin-tx executable." ON) +option(BUILD_UTIL "Build bitcoin-util executable." ON) option(ASM "Use assembly routines." ON) -cmake_dependent_option(CXX20 "Enable compilation in C++20 mode." OFF "NOT MSVC" ON) -option(THREADLOCAL "Enable features that depend on the C++ thread_local keyword (currently just thread names in debug logs)." ON) +option(ENABLE_WALLET "Enable wallet." ON) # TODO: These tri-state options will be removed and most features # will become opt-in by default before merging into master. include(TristateOption) +tristate_option(WITH_SQLITE "Enable SQLite wallet support." "if libsqlite3 is found." AUTO) +tristate_option(WITH_BDB "Enable Berkeley DB (BDB) wallet support." "if libdb_cxx is found." AUTO) +option(WARN_INCOMPATIBLE_BDB "Warn when using a Berkeley DB (BDB) version other than 4.8." ON) +cmake_dependent_option(BUILD_WALLET_TOOL "Build bitcoin-wallet tool." ON "ENABLE_WALLET" OFF) + +cmake_dependent_option(CXX20 "Enable compilation in C++20 mode." OFF "NOT MSVC" ON) +option(THREADLOCAL "Enable features that depend on the C++ thread_local keyword (currently just thread names in debug logs)." ON) + tristate_option(CCACHE "Use ccache for compiling." "if ccache is found." AUTO) tristate_option(WITH_NATPMP "Enable NAT-PMP." "if libnatpmp is found." AUTO) tristate_option(WITH_MINIUPNPC "Enable UPnP." "if libminiupnpc is found." AUTO) @@ -56,6 +67,9 @@ tristate_option(WITH_USDT AUTO ) +option(BUILD_TESTS "Build test_bitcoin executable." ON) +option(BUILD_BENCH "Build bench_bitcoin executable." ON) + if(CXX20) set(CMAKE_CXX_STANDARD 20) else() @@ -141,18 +155,34 @@ else() endif() endif() +find_package(Python3 3.8 COMPONENTS Interpreter) +set(PYTHON_COMMAND ${Python3_EXECUTABLE}) + add_subdirectory(src) +add_subdirectory(test) + +include(cmake/tests.cmake) message("\n") message("Configure summary") message("=================") message("Executables:") message(" bitcoind ............................ ${BUILD_DAEMON}") +message(" bitcoin-cli ......................... ${BUILD_CLI}") +message(" bitcoin-tx .......................... ${BUILD_TX}") +message(" bitcoin-util ........................ ${BUILD_UTIL}") +message(" bitcoin-wallet ...................... ${BUILD_WALLET_TOOL}") +message("Wallet support:") +message(" SQLite, descriptor wallets .......... ${WITH_SQLITE}") +message(" Berkeley DB, legacy wallets ......... ${WITH_BDB}") message("Optional packages:") message(" NAT-PMP ............................. ${WITH_NATPMP}") message(" UPnP ................................ ${WITH_MINIUPNPC}") message(" ZeroMQ .............................. ${WITH_ZMQ}") message(" USDT tracing ........................ ${WITH_USDT}") +message("Tests:") +message(" test_bitcoin ........................ ${BUILD_TESTS}") +message(" bench_bitcoin ....................... ${BUILD_BENCH}") message("") if(CMAKE_CROSSCOMPILING) set(cross_status "TRUE, for ${CMAKE_SYSTEM_NAME}, ${CMAKE_SYSTEM_PROCESSOR}") diff --git a/cmake/bitcoin-config.h.in b/cmake/bitcoin-config.h.in index ed70bb5e8f8c8..ebbb97562cdb0 100644 --- a/cmake/bitcoin-config.h.in +++ b/cmake/bitcoin-config.h.in @@ -206,4 +206,13 @@ significant byte first (like Motorola and SPARC, unlike Intel). */ #cmakedefine WORDS_BIGENDIAN 1 +/* Define to 1 to enable wallet functions. */ +#cmakedefine ENABLE_WALLET 1 + +/* Define if SQLite support should be compiled in. */ +#cmakedefine USE_SQLITE + +/* Define if Berkeley DB (BDB) support should be compiled in. */ +#cmakedefine USE_BDB + #endif //BITCOIN_CONFIG_H diff --git a/cmake/introspection.cmake b/cmake/introspection.cmake index 81a6194e5d4ef..b4126d2dc785a 100644 --- a/cmake/introspection.cmake +++ b/cmake/introspection.cmake @@ -164,7 +164,7 @@ check_cxx_source_compiles(" # Check for posix_fallocate(). check_cxx_source_compiles(" - // same as in src/util/system.cpp + // same as in src/fs_helpers/system.cpp #ifdef __linux__ #ifdef _POSIX_C_SOURCE #undef _POSIX_C_SOURCE diff --git a/cmake/module/CrossPkgConfig.cmake b/cmake/module/CrossPkgConfig.cmake index 4d845a60fe3e3..429fa5fdc2432 100644 --- a/cmake/module/CrossPkgConfig.cmake +++ b/cmake/module/CrossPkgConfig.cmake @@ -4,16 +4,29 @@ find_package(PkgConfig REQUIRED) -macro(cross_pkg_check_modules) +function(remove_isystem_from_include_directories_internal target) + get_target_property(include_directories ${target} INTERFACE_INCLUDE_DIRECTORIES) + if(include_directories) + list(REMOVE_ITEM include_directories -isystem) + set_target_properties(${target} PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${include_directories}") + endif() +endfunction() + +macro(cross_pkg_check_modules prefix) if(CMAKE_CROSSCOMPILING) set(pkg_config_path_saved "$ENV{PKG_CONFIG_PATH}") set(pkg_config_libdir_saved "$ENV{PKG_CONFIG_LIBDIR}") set(ENV{PKG_CONFIG_PATH} ${PKG_CONFIG_PATH}) set(ENV{PKG_CONFIG_LIBDIR} ${PKG_CONFIG_LIBDIR}) - pkg_check_modules(${ARGV}) + pkg_check_modules(${prefix} ${ARGN}) set(ENV{PKG_CONFIG_PATH} ${pkg_config_path_saved}) set(ENV{PKG_CONFIG_LIBDIR} ${pkg_config_libdir_saved}) else() - pkg_check_modules(${ARGV}) + pkg_check_modules(${prefix} ${ARGN}) + endif() + + # A workaround for https://gitlab.kitware.com/cmake/cmake/-/issues/20652. + if(CMAKE_VERSION VERSION_LESS 3.17.3 AND TARGET PkgConfig::${prefix}) + remove_isystem_from_include_directories_internal(PkgConfig::${prefix}) endif() endmacro() diff --git a/cmake/module/FindBerkeleyDB.cmake b/cmake/module/FindBerkeleyDB.cmake new file mode 100644 index 0000000000000..712fcb3decfbd --- /dev/null +++ b/cmake/module/FindBerkeleyDB.cmake @@ -0,0 +1,87 @@ +# Copyright (c) 2023 The Bitcoin Core developers +# Distributed under the MIT software license, see the accompanying +# file COPYING or http://www.opensource.org/licenses/mit-license.php. + +if(CMAKE_HOST_APPLE) + execute_process( + COMMAND brew --prefix berkeley-db@4 + OUTPUT_VARIABLE bdb4_brew_prefix + ERROR_QUIET + OUTPUT_STRIP_TRAILING_WHITESPACE + ) +endif() + +find_path(BerkeleyDB_INCLUDE_DIR + NAMES db.h + HINTS ${bdb4_brew_prefix}/include + PATH_SUFFIXES 4.8 48 4 db4 5 5.3 db5 +) + +if(BerkeleyDB_INCLUDE_DIR) + file( + STRINGS "${BerkeleyDB_INCLUDE_DIR}/db.h" version_strings + REGEX ".*DB_VERSION_(MAJOR|MINOR)[ \t]+[0-9]+.*" + ) + string(REGEX REPLACE ".*DB_VERSION_MAJOR[ \t]+([0-9]+).*" "\\1" BerkeleyDB_VERSION_MAJOR "${version_strings}") + string(REGEX REPLACE ".*DB_VERSION_MINOR[ \t]+([0-9]+).*" "\\1" BerkeleyDB_VERSION_MINOR "${version_strings}") + set(BerkeleyDB_VERSION ${BerkeleyDB_VERSION_MAJOR}.${BerkeleyDB_VERSION_MINOR}) +endif() + +if(MSVC) + cmake_path(GET BerkeleyDB_INCLUDE_DIR PARENT_PATH BerkeleyDB_IMPORTED_PATH) + find_library(BerkeleyDB_LIBRARY_DEBUG + NAMES libdb48 PATHS ${BerkeleyDB_IMPORTED_PATH}/debug/lib + NO_DEFAULT_PATH + ) + find_library(BerkeleyDB_LIBRARY_RELEASE + NAMES libdb48 PATHS ${BerkeleyDB_IMPORTED_PATH}/lib + NO_DEFAULT_PATH + ) + if(BerkeleyDB_LIBRARY_DEBUG OR BerkeleyDB_LIBRARY_RELEASE) + set(BerkeleyDB_required BerkeleyDB_IMPORTED_PATH) + endif() +else() + find_library(BerkeleyDB_LIBRARY + NAMES db_cxx-4.8 libdb48 db4_cxx db_cxx db_cxx-5 + HINTS ${bdb4_brew_prefix}/lib + ) + set(BerkeleyDB_required BerkeleyDB_LIBRARY) +endif() + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(BerkeleyDB + REQUIRED_VARS ${BerkeleyDB_required} BerkeleyDB_INCLUDE_DIR + VERSION_VAR BerkeleyDB_VERSION +) + +if(BerkeleyDB_FOUND AND NOT TARGET BerkeleyDB::BerkeleyDB) + add_library(BerkeleyDB::BerkeleyDB UNKNOWN IMPORTED) + set_target_properties(BerkeleyDB::BerkeleyDB PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES "${BerkeleyDB_INCLUDE_DIR}" + ) + if(MSVC) + if(BerkeleyDB_LIBRARY_DEBUG) + set_property(TARGET BerkeleyDB::BerkeleyDB APPEND PROPERTY IMPORTED_CONFIGURATIONS DEBUG) + set_target_properties(BerkeleyDB::BerkeleyDB PROPERTIES + IMPORTED_LOCATION_DEBUG "${BerkeleyDB_LIBRARY_DEBUG}" + ) + endif() + if(BerkeleyDB_LIBRARY_RELEASE) + set_property(TARGET BerkeleyDB::BerkeleyDB APPEND PROPERTY IMPORTED_CONFIGURATIONS RELEASE) + set_target_properties(BerkeleyDB::BerkeleyDB PROPERTIES + IMPORTED_LOCATION_RELEASE "${BerkeleyDB_LIBRARY_RELEASE}" + ) + endif() + else() + set_target_properties(BerkeleyDB::BerkeleyDB PROPERTIES + IMPORTED_LOCATION "${BerkeleyDB_LIBRARY}" + ) + endif() +endif() + +mark_as_advanced( + BerkeleyDB_INCLUDE_DIR + BerkeleyDB_LIBRARY + BerkeleyDB_LIBRARY_DEBUG + BerkeleyDB_LIBRARY_RELEASE +) diff --git a/cmake/module/GenerateHeaders.cmake b/cmake/module/GenerateHeaders.cmake new file mode 100644 index 0000000000000..13359522f2758 --- /dev/null +++ b/cmake/module/GenerateHeaders.cmake @@ -0,0 +1,21 @@ +# Copyright (c) 2023 The Bitcoin Core developers +# Distributed under the MIT software license, see the accompanying +# file COPYING or http://www.opensource.org/licenses/mit-license.php. + +function(generate_header_from_json json_source_relpath) + add_custom_command( + OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${json_source_relpath}.h + COMMAND ${CMAKE_COMMAND} -DJSON_SOURCE_PATH=${CMAKE_CURRENT_SOURCE_DIR}/${json_source_relpath} -DHEADER_PATH=${CMAKE_CURRENT_BINARY_DIR}/${json_source_relpath}.h -P ${CMAKE_SOURCE_DIR}/cmake/script/GenerateHeaderFromJson.cmake + DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${json_source_relpath} + VERBATIM + ) +endfunction() + +function(generate_header_from_raw raw_source_relpath) + add_custom_command( + OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${raw_source_relpath}.h + COMMAND ${CMAKE_COMMAND} -DRAW_SOURCE_PATH=${CMAKE_CURRENT_SOURCE_DIR}/${raw_source_relpath} -DHEADER_PATH=${CMAKE_CURRENT_BINARY_DIR}/${raw_source_relpath}.h -P ${CMAKE_SOURCE_DIR}/cmake/script/GenerateHeaderFromRaw.cmake + DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${raw_source_relpath} + VERBATIM + ) +endfunction() diff --git a/cmake/optional.cmake b/cmake/optional.cmake index 0cb454af3b57d..e018a47f73725 100644 --- a/cmake/optional.cmake +++ b/cmake/optional.cmake @@ -93,7 +93,9 @@ if(WITH_USDT) int main() { - DTRACE_PROBE(\"context\", \"event\"); + DTRACE_PROBE(context, event); + int a, b, c, d, e, f, g; + DTRACE_PROBE7(context, event, a, b, c, d, e, f, g); } " HAVE_USDT_H ) @@ -106,3 +108,43 @@ if(WITH_USDT) message(FATAL_ERROR "sys/sdt.h requested, but not found.") endif() endif() + +if(ENABLE_WALLET) + if(WITH_SQLITE) + include(CrossPkgConfig) + cross_pkg_check_modules(sqlite sqlite3>=3.7.17 IMPORTED_TARGET) + if(sqlite_FOUND) + set(WITH_SQLITE ON) + set(USE_SQLITE ON) + elseif(WITH_SQLITE STREQUAL "AUTO") + set(WITH_SQLITE OFF) + else() + message(FATAL_ERROR "SQLite requested, but not found.") + endif() + endif() + + if(WITH_BDB) + find_package(BerkeleyDB 4.8 MODULE) + if(BerkeleyDB_FOUND) + set(WITH_BDB ON) + set(USE_BDB ON) + if(NOT BerkeleyDB_VERSION VERSION_EQUAL 4.8) + message(WARNING "Found Berkeley DB (BDB) other than 4.8.") + if(WARN_INCOMPATIBLE_BDB) + message(WARNING "BDB (legacy) wallets opened by this build would not be portable!\n" + "If this is intended, pass \"-DWARN_INCOMPATIBLE_BDB=OFF\".\n" + "Passing \"-DWITH_BDB=OFF\" will suppress this warning.\n") + else() + message(WARNING "BDB (legacy) wallets opened by this build will not be portable!") + endif() + endif() + else() + message(WARNING "Berkeley DB (BDB) required for legacy wallet support, but not found.\n" + "Passing \"-DWITH_BDB=OFF\" will suppress this warning.\n") + set(WITH_BDB OFF) + endif() + endif() +else() + set(WITH_SQLITE OFF) + set(WITH_BDB OFF) +endif() diff --git a/cmake/script/GenerateHeaderFromJson.cmake b/cmake/script/GenerateHeaderFromJson.cmake new file mode 100644 index 0000000000000..e02705dd895e2 --- /dev/null +++ b/cmake/script/GenerateHeaderFromJson.cmake @@ -0,0 +1,23 @@ +# Copyright (c) 2023 The Bitcoin Core developers +# Distributed under the MIT software license, see the accompanying +# file COPYING or http://www.opensource.org/licenses/mit-license.php. + +file(READ ${JSON_SOURCE_PATH} hex_content HEX) +string(REGEX MATCHALL "([A-Za-z0-9][A-Za-z0-9])" bytes "${hex_content}") + +file(WRITE ${HEADER_PATH} "namespace json_tests{\n") +get_filename_component(json_source_basename ${JSON_SOURCE_PATH} NAME_WE) +file(APPEND ${HEADER_PATH} "static unsigned const char ${json_source_basename}[] = {\n") + +set(i 0) +foreach(byte ${bytes}) + math(EXPR i "${i} + 1") + math(EXPR remainder "${i} % 8") + if(remainder EQUAL 0) + file(APPEND ${HEADER_PATH} "0x${byte},\n") + else() + file(APPEND ${HEADER_PATH} "0x${byte}, ") + endif() +endforeach() + +file(APPEND ${HEADER_PATH} "\n};};") diff --git a/cmake/script/GenerateHeaderFromRaw.cmake b/cmake/script/GenerateHeaderFromRaw.cmake new file mode 100644 index 0000000000000..82a3a3c3a3199 --- /dev/null +++ b/cmake/script/GenerateHeaderFromRaw.cmake @@ -0,0 +1,22 @@ +# Copyright (c) 2023 The Bitcoin Core developers +# Distributed under the MIT software license, see the accompanying +# file COPYING or http://www.opensource.org/licenses/mit-license.php. + +file(READ ${RAW_SOURCE_PATH} hex_content HEX) +string(REGEX MATCHALL "([A-Za-z0-9][A-Za-z0-9])" bytes "${hex_content}") + +get_filename_component(raw_source_basename ${RAW_SOURCE_PATH} NAME_WE) +file(WRITE ${HEADER_PATH} "static unsigned const char ${raw_source_basename}_raw[] = {\n") + +set(i 0) +foreach(byte ${bytes}) + math(EXPR i "${i} + 1") + math(EXPR remainder "${i} % 8") + if(remainder EQUAL 0) + file(APPEND ${HEADER_PATH} "0x${byte},\n") + else() + file(APPEND ${HEADER_PATH} "0x${byte}, ") + endif() +endforeach() + +file(APPEND ${HEADER_PATH} "\n};") diff --git a/cmake/secp256k1.cmake b/cmake/secp256k1.cmake index fd1d04198ec09..9d490cdafb487 100644 --- a/cmake/secp256k1.cmake +++ b/cmake/secp256k1.cmake @@ -10,17 +10,19 @@ enable_language(C) set(CMAKE_C_STANDARD 90) set(CMAKE_C_EXTENSIONS OFF) -include(CheckCSourceCompiles) -check_c_source_compiles(" - #include - - int main() - { - uint64_t a = 11, tmp; - __asm__ __volatile__(\"movq $0x100000000,%1; mulq %%rsi\" : \"+a\"(a) : \"S\"(tmp) : \"cc\", \"%rdx\"); - } - " HAVE_64BIT_ASM -) +if(ASM) + include(CheckCSourceCompiles) + check_c_source_compiles(" + #include + + int main() + { + uint64_t a = 11, tmp; + __asm__ __volatile__(\"movq $0x100000000,%1; mulq %%rsi\" : \"+a\"(a) : \"S\"(tmp) : \"cc\", \"%rdx\"); + } + " HAVE_64BIT_ASM + ) +endif() add_library(secp256k1 STATIC EXCLUDE_FROM_ALL ${PROJECT_SOURCE_DIR}/src/secp256k1/src/secp256k1.c diff --git a/cmake/tests.cmake b/cmake/tests.cmake new file mode 100644 index 0000000000000..598af496387d2 --- /dev/null +++ b/cmake/tests.cmake @@ -0,0 +1,55 @@ +# Copyright (c) 2023 The Bitcoin Core developers +# Distributed under the MIT software license, see the accompanying +# file COPYING or http://www.opensource.org/licenses/mit-license.php. + +include(CTest) + +if(TARGET bitcoin-util AND TARGET bitcoin-tx) + add_test(NAME util_test_runner + COMMAND ${CMAKE_COMMAND} -E env BITCOINUTIL=$ BITCOINTX=$ ${PYTHON_COMMAND} ${CMAKE_BINARY_DIR}/test/util/test_runner.py + ) +endif() + +add_test(NAME util_rpcauth_test + COMMAND ${PYTHON_COMMAND} ${CMAKE_BINARY_DIR}/test/util/rpcauth-test.py +) + +if(TARGET bench_bitcoin) + add_test(NAME bench_sanity_check_high_priority + COMMAND bench_bitcoin -sanity-check -priority-level=high + ) +endif() + +if(TARGET test_bitcoin) + function(add_boost_test source_dir source_file) + set(source_file_path ${source_dir}/${source_file}) + if(NOT EXISTS ${source_file_path}) + return() + endif() + + file(READ "${source_file_path}" source_file_content) + string(REGEX + MATCH "(BOOST_FIXTURE_TEST_SUITE|BOOST_AUTO_TEST_SUITE)\\(([A-Za-z0-9_]+)" + test_suite_macro "${source_file_content}" + ) + string(REGEX + REPLACE "(BOOST_FIXTURE_TEST_SUITE|BOOST_AUTO_TEST_SUITE)\\(" "" + test_suite_name "${test_suite_macro}" + ) + if(test_suite_name) + add_test(NAME ${test_suite_name}:${source_file} + COMMAND test_bitcoin --run_test=${test_suite_name} --catch_system_error=no + ) + endif() + endfunction() + + function(add_all_test_targets) + get_target_property(test_source_dir test_bitcoin SOURCE_DIR) + get_target_property(test_sources test_bitcoin SOURCES) + foreach(test_source ${test_sources}) + add_boost_test(${test_source_dir} ${test_source}) + endforeach() + endfunction() + + add_all_test_targets() +endif() diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c821ec44d263a..fd8268070a4b7 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -93,6 +93,25 @@ target_link_libraries(bitcoin_common ) +if(ENABLE_WALLET) + add_subdirectory(wallet) + + if(BUILD_WALLET_TOOL) + add_executable(bitcoin-wallet + bitcoin-wallet.cpp + init/bitcoin-wallet.cpp + wallet/wallettool.cpp + ) + target_link_libraries(bitcoin-wallet + bitcoin_wallet + bitcoin_common + bitcoin_util + Boost::headers + ) + endif() +endif() + + # P2P and RPC server functionality used by `bitcoind` and `bitcoin-qt` executables. add_library(bitcoin_node STATIC EXCLUDE_FROM_ALL addrdb.cpp @@ -178,9 +197,13 @@ add_library(bitcoin_node STATIC EXCLUDE_FROM_ALL validation.cpp validationinterface.cpp versionbits.cpp - - dummywallet.cpp ) +if(ENABLE_WALLET) + target_sources(bitcoin_node PRIVATE wallet/init.cpp) + target_link_libraries(bitcoin_node PRIVATE bitcoin_wallet) +else() + target_sources(bitcoin_node PRIVATE dummywallet.cpp) +endif() target_link_libraries(bitcoin_node PRIVATE bitcoin_common @@ -212,3 +235,54 @@ if(BUILD_DAEMON) $<$:-static> ) endif() + + +add_library(bitcoin_cli STATIC EXCLUDE_FROM_ALL + compat/stdin.cpp + rpc/client.cpp +) +target_link_libraries(bitcoin_cli + PUBLIC + univalue +) + + +# Bitcoin Core RPC client +if(BUILD_CLI) + add_executable(bitcoin-cli bitcoin-cli.cpp) + target_link_libraries(bitcoin-cli + bitcoin_cli + bitcoin_common + bitcoin_util + libevent::libevent + ) +endif() + + +if(BUILD_TX) + add_executable(bitcoin-tx bitcoin-tx.cpp) + target_link_libraries(bitcoin-tx + bitcoin_common + bitcoin_util + univalue + ) +endif() + + +if(BUILD_UTIL) + add_executable(bitcoin-util bitcoin-util.cpp) + target_link_libraries(bitcoin-util + bitcoin_common + bitcoin_util + ) +endif() + + +add_subdirectory(test/util) +if(BUILD_BENCH) + add_subdirectory(bench) +endif() + +if(BUILD_TESTS) + add_subdirectory(test) +endif() diff --git a/src/bench/CMakeLists.txt b/src/bench/CMakeLists.txt new file mode 100644 index 0000000000000..eba9b2535af44 --- /dev/null +++ b/src/bench/CMakeLists.txt @@ -0,0 +1,64 @@ +# Copyright (c) 2023 The Bitcoin Core developers +# Distributed under the MIT software license, see the accompanying +# file COPYING or http://www.opensource.org/licenses/mit-license.php. + +include(GenerateHeaders) +generate_header_from_raw(data/block413567.raw) + +add_executable(bench_bitcoin + bench_bitcoin.cpp + bench.cpp + data.cpp + nanobench.cpp + ${CMAKE_CURRENT_BINARY_DIR}/data/block413567.raw.h +# Benchmarks: + addrman.cpp + base58.cpp + bech32.cpp + block_assemble.cpp + ccoins_caching.cpp + chacha20.cpp + chacha_poly_aead.cpp + checkblock.cpp + checkqueue.cpp + crypto_hash.cpp + descriptors.cpp + duplicate_inputs.cpp + examples.cpp + gcs_filter.cpp + hashpadding.cpp + load_external.cpp + lockedpool.cpp + logging.cpp + mempool_eviction.cpp + mempool_stress.cpp + merkle_root.cpp + peer_eviction.cpp + poly1305.cpp + pool.cpp + prevector.cpp + rollingbloom.cpp + rpc_blockchain.cpp + rpc_mempool.cpp + strencodings.cpp + util_time.cpp + verify_script.cpp +) + +target_link_libraries(bench_bitcoin + test_util + leveldb + univalue + Boost::headers +) + +if(ENABLE_WALLET) + target_sources(bench_bitcoin + PRIVATE + coin_selection.cpp + wallet_balance.cpp + wallet_create_tx.cpp + wallet_loading.cpp + ) + target_link_libraries(bench_bitcoin bitcoin_wallet) +endif() diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt new file mode 100644 index 0000000000000..d1d22901b343e --- /dev/null +++ b/src/test/CMakeLists.txt @@ -0,0 +1,170 @@ +# Copyright (c) 2023 The Bitcoin Core developers +# Distributed under the MIT software license, see the accompanying +# file COPYING or http://www.opensource.org/licenses/mit-license.php. + +include(GenerateHeaders) +generate_header_from_json(data/base58_encode_decode.json) +generate_header_from_json(data/bip341_wallet_vectors.json) +generate_header_from_json(data/blockfilters.json) +generate_header_from_json(data/key_io_invalid.json) +generate_header_from_json(data/key_io_valid.json) +generate_header_from_json(data/script_tests.json) +generate_header_from_json(data/sighash.json) +generate_header_from_json(data/tx_invalid.json) +generate_header_from_json(data/tx_valid.json) +generate_header_from_raw(data/asmap.raw) + +add_executable(test_bitcoin + main.cpp + $ + ${CMAKE_CURRENT_BINARY_DIR}/data/asmap.raw.h + ${CMAKE_CURRENT_BINARY_DIR}/data/base58_encode_decode.json.h + ${CMAKE_CURRENT_BINARY_DIR}/data/bip341_wallet_vectors.json.h + ${CMAKE_CURRENT_BINARY_DIR}/data/blockfilters.json.h + ${CMAKE_CURRENT_BINARY_DIR}/data/key_io_invalid.json.h + ${CMAKE_CURRENT_BINARY_DIR}/data/key_io_valid.json.h + ${CMAKE_CURRENT_BINARY_DIR}/data/script_tests.json.h + ${CMAKE_CURRENT_BINARY_DIR}/data/sighash.json.h + ${CMAKE_CURRENT_BINARY_DIR}/data/tx_invalid.json.h + ${CMAKE_CURRENT_BINARY_DIR}/data/tx_valid.json.h +# Tests: + addrman_tests.cpp + allocator_tests.cpp + amount_tests.cpp + argsman_tests.cpp + arith_uint256_tests.cpp + banman_tests.cpp + base32_tests.cpp + base58_tests.cpp + base64_tests.cpp + bech32_tests.cpp + bip32_tests.cpp + blockchain_tests.cpp + blockencodings_tests.cpp + blockfilter_index_tests.cpp + blockfilter_tests.cpp + blockmanager_tests.cpp + bloom_tests.cpp + bswap_tests.cpp + checkqueue_tests.cpp + coins_tests.cpp + coinstatsindex_tests.cpp + compress_tests.cpp + compilerbug_tests.cpp + crypto_tests.cpp + cuckoocache_tests.cpp + dbwrapper_tests.cpp + denialofservice_tests.cpp + descriptor_tests.cpp + flatfile_tests.cpp + fs_tests.cpp + getarg_tests.cpp + hash_tests.cpp + headers_sync_chainwork_tests.cpp + httpserver_tests.cpp + i2p_tests.cpp + interfaces_tests.cpp + key_io_tests.cpp + key_tests.cpp + logging_tests.cpp + mempool_tests.cpp + merkleblock_tests.cpp + merkle_tests.cpp + miner_tests.cpp + miniscript_tests.cpp + minisketch_tests.cpp + multisig_tests.cpp + netbase_tests.cpp + net_peer_eviction_tests.cpp + net_tests.cpp + orphanage_tests.cpp + pmt_tests.cpp + policy_fee_tests.cpp + policyestimator_tests.cpp + pool_tests.cpp + pow_tests.cpp + prevector_tests.cpp + raii_event_tests.cpp + random_tests.cpp + rbf_tests.cpp + rest_tests.cpp + result_tests.cpp + reverselock_tests.cpp + rpc_tests.cpp + sanity_tests.cpp + scheduler_tests.cpp + scriptnum_tests.cpp + script_p2sh_tests.cpp + script_parse_tests.cpp + script_segwit_tests.cpp + script_standard_tests.cpp + script_tests.cpp + serfloat_tests.cpp + serialize_tests.cpp + settings_tests.cpp + sighash_tests.cpp + sigopcount_tests.cpp + skiplist_tests.cpp + sock_tests.cpp + streams_tests.cpp + sync_tests.cpp + system_tests.cpp + timedata_tests.cpp + torcontrol_tests.cpp + transaction_tests.cpp + translation_tests.cpp + txindex_tests.cpp + txpackage_tests.cpp + txreconciliation_tests.cpp + txrequest_tests.cpp + txvalidationcache_tests.cpp + txvalidation_tests.cpp + uint256_tests.cpp + util_tests.cpp + util_threadnames_tests.cpp + validation_block_tests.cpp + validation_chainstate_tests.cpp + validation_chainstatemanager_tests.cpp + validation_flush_tests.cpp + validation_tests.cpp + validationinterface_tests.cpp + versionbits_tests.cpp + xoroshiro128plusplus_tests.cpp +) + +target_link_libraries(test_bitcoin + test_util + bitcoin_cli + bitcoin_node + bitcoin_common + bitcoin_util + minisketch + leveldb + univalue + Boost::headers + libevent::libevent +) + +if(ENABLE_WALLET) + target_sources(test_bitcoin + PRIVATE + ../wallet/test/coinselector_tests.cpp + ../wallet/test/init_test_fixture.cpp + ../wallet/test/init_tests.cpp + ../wallet/test/ismine_tests.cpp + ../wallet/test/psbt_wallet_tests.cpp + ../wallet/test/rpc_util_tests.cpp + ../wallet/test/scriptpubkeyman_tests.cpp + ../wallet/test/spend_tests.cpp + ../wallet/test/wallet_crypto_tests.cpp + ../wallet/test/wallet_test_fixture.cpp + ../wallet/test/wallet_tests.cpp + ../wallet/test/wallet_transaction_tests.cpp + ../wallet/test/walletdb_tests.cpp + ../wallet/test/walletload_tests.cpp + ) + target_link_libraries(test_bitcoin bitcoin_wallet) + if(USE_BDB) + target_sources(test_bitcoin PRIVATE ../wallet/test/db_tests.cpp) + endif() +endif() diff --git a/src/test/util/CMakeLists.txt b/src/test/util/CMakeLists.txt new file mode 100644 index 0000000000000..3d3792cea4fac --- /dev/null +++ b/src/test/util/CMakeLists.txt @@ -0,0 +1,33 @@ +# Copyright (c) 2023 The Bitcoin Core developers +# Distributed under the MIT software license, see the accompanying +# file COPYING or http://www.opensource.org/licenses/mit-license.php. + +add_library(test_util STATIC EXCLUDE_FROM_ALL + blockfilter.cpp + coins.cpp + json.cpp + logging.cpp + mining.cpp + net.cpp + script.cpp + setup_common.cpp + str.cpp + transaction_utils.cpp + txmempool.cpp + validation.cpp +) +target_link_libraries(test_util + PRIVATE + bitcoin_common + bitcoin_node + leveldb + univalue + Boost::headers +) + +if(ENABLE_WALLET) + target_sources(test_util + PRIVATE + ../../wallet/test/util.cpp + ) +endif() diff --git a/src/wallet/CMakeLists.txt b/src/wallet/CMakeLists.txt new file mode 100644 index 0000000000000..9ff55d505815c --- /dev/null +++ b/src/wallet/CMakeLists.txt @@ -0,0 +1,52 @@ +# Copyright (c) 2023 The Bitcoin Core developers +# Distributed under the MIT software license, see the accompanying +# file COPYING or http://www.opensource.org/licenses/mit-license.php. + +# Wallet functionality used by bitcoind and bitcoin-wallet executables. +add_library(bitcoin_wallet STATIC EXCLUDE_FROM_ALL + coincontrol.cpp + coinselection.cpp + context.cpp + crypter.cpp + db.cpp + dump.cpp + external_signer_scriptpubkeyman.cpp + feebumper.cpp + fees.cpp + interfaces.cpp + load.cpp + receive.cpp + rpc/addresses.cpp + rpc/backup.cpp + rpc/coins.cpp + rpc/encrypt.cpp + rpc/spend.cpp + rpc/signmessage.cpp + rpc/transactions.cpp + rpc/util.cpp + rpc/wallet.cpp + scriptpubkeyman.cpp + spend.cpp + transaction.cpp + wallet.cpp + walletdb.cpp + walletutil.cpp +) +target_link_libraries(bitcoin_wallet + PRIVATE + bitcoin_common + univalue + Boost::headers +) + +if(NOT USE_SQLITE AND NOT USE_BDB) + message(FATAL_ERROR "Wallet functionality requested but no BDB or SQLite support available.") +endif() +if(USE_SQLITE) + target_sources(bitcoin_wallet PRIVATE sqlite.cpp) + target_link_libraries(bitcoin_wallet PRIVATE PkgConfig::sqlite) +endif() +if(USE_BDB) + target_sources(bitcoin_wallet PRIVATE bdb.cpp salvage.cpp) + target_link_libraries(bitcoin_wallet PUBLIC BerkeleyDB::BerkeleyDB) +endif() diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000000000..63423810892b9 --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,52 @@ +# Copyright (c) 2023 The Bitcoin Core developers +# Distributed under the MIT software license, see the accompanying +# file COPYING or http://www.opensource.org/licenses/mit-license.php. + +function(create_test_config) + set(abs_top_srcdir ${PROJECT_SOURCE_DIR}) + set(abs_top_builddir ${PROJECT_BINARY_DIR}) + set(EXEEXT ${CMAKE_EXECUTABLE_SUFFIX}) + + macro(set_configure_variable var conf_var) + if(${var}) + set(${conf_var}_TRUE "") + else() + set(${conf_var}_TRUE "#") + endif() + endmacro() + + set_configure_variable(ENABLE_WALLET ENABLE_WALLET) + set_configure_variable(WITH_SQLITE USE_SQLITE) + set_configure_variable(WITH_BDB USE_BDB) + set_configure_variable(BUILD_CLI BUILD_BITCOIN_CLI) + set_configure_variable(BUILD_UTIL BUILD_BITCOIN_UTIL) + set_configure_variable(BUILD_WALLET_TOOL BUILD_BITCOIN_WALLET) + set_configure_variable(BUILD_DAEMON BUILD_BITCOIND_TRUE) + set_configure_variable(WITH_ZMQ ENABLE_ZMQ) + set_configure_variable(ENABLE_EXTERNAL_SIGNER ENABLE_EXTERNAL_SIGNER) + set_configure_variable(USE_SYSCALL_SANDBOX ENABLE_SYSCALL_SANDBOX) + set_configure_variable(ENABLE_TRACING ENABLE_USDT_TRACEPOINTS) + + configure_file(config.ini.in config.ini @ONLY) +endfunction() + +create_test_config() + +file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/functional) +file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/fuzz) +file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/util) + +function(create_test_script script) + if(MSVC) + file(CREATE_LINK ${CMAKE_CURRENT_SOURCE_DIR}/${script} ${CMAKE_CURRENT_BINARY_DIR}/${script} COPY_ON_ERROR) + elseif(CMAKE_VERSION VERSION_GREATER_EQUAL 3.14) + file(CREATE_LINK ${CMAKE_CURRENT_SOURCE_DIR}/${script} ${CMAKE_CURRENT_BINARY_DIR}/${script} COPY_ON_ERROR SYMBOLIC) + else() + file(REMOVE ${CMAKE_CURRENT_BINARY_DIR}/${script}) + execute_process(COMMAND ln -s ${CMAKE_CURRENT_SOURCE_DIR}/${script} ${CMAKE_CURRENT_BINARY_DIR}/${script}) + endif() +endfunction() + +foreach(script functional/test_runner.py fuzz/test_runner.py util/rpcauth-test.py util/test_runner.py) + create_test_script(${script}) +endforeach() diff --git a/test/functional/test_runner.py b/test/functional/test_runner.py index bcedc0c9af89b..7656e3452392d 100755 --- a/test/functional/test_runner.py +++ b/test/functional/test_runner.py @@ -524,6 +524,12 @@ def run_tests(*, test_list, src_dir, build_dir, tmpdir, jobs=1, enable_coverage= # Test Framework Tests print("Running Unit Tests for Test Framework Modules") + + tests_dir = src_dir + '/test/functional/' + # This allows `test_runner.py` to work from an out-of-source build directory using a symlink, + # a hard link or a copy on any platform. See https://github.com/bitcoin/bitcoin/pull/27561. + sys.path.append(tests_dir) + test_framework_tests = unittest.TestSuite() for module in TEST_FRAMEWORK_MODULES: test_framework_tests.addTest(unittest.TestLoader().loadTestsFromName("test_framework.{}".format(module))) @@ -532,8 +538,6 @@ def run_tests(*, test_list, src_dir, build_dir, tmpdir, jobs=1, enable_coverage= logging.debug("Early exiting after failure in TestFramework unit tests") sys.exit(False) - tests_dir = src_dir + '/test/functional/' - flags = ['--cachedir={}'.format(cache_dir)] + args if enable_coverage: diff --git a/test/util/test_runner.py b/test/util/test_runner.py index e5cdd0bc3ad72..1cd368f6f442a 100755 --- a/test/util/test_runner.py +++ b/test/util/test_runner.py @@ -74,6 +74,11 @@ def bctest(testDir, testObj, buildenv): """ # Get the exec names and arguments execprog = os.path.join(buildenv["BUILDDIR"], "src", testObj["exec"] + buildenv["EXEEXT"]) + if testObj["exec"] == "./bitcoin-util": + execprog = os.getenv("BITCOINUTIL", default=execprog) + elif testObj["exec"] == "./bitcoin-tx": + execprog = os.getenv("BITCOINTX", default=execprog) + execargs = testObj['args'] execrun = [execprog] + execargs