diff --git a/CMakeLists.txt b/CMakeLists.txt index 0f746feb73994..a78e63fa25847 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,12 +43,19 @@ 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) @@ -154,6 +161,10 @@ 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}") 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/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/optional.cmake b/cmake/optional.cmake index 0cb454af3b57d..dc02a6f2f5e26 100644 --- a/cmake/optional.cmake +++ b/cmake/optional.cmake @@ -106,3 +106,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/src/CMakeLists.txt b/src/CMakeLists.txt index 2fbdc68568839..219939115b7a7 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 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()