From 7ac643cddbe5cb8dfb7c090d1e55214620b03786 Mon Sep 17 00:00:00 2001 From: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> Date: Tue, 28 Feb 2023 21:30:06 +0000 Subject: [PATCH] cmake: Build `bitcoind` executable --- CMakeLists.txt | 11 ++ cmake/introspection.cmake | 4 + cmake/module/AddBoostIfNeeded.cmake | 40 ++++++ cmake/module/AddLibeventIfNeeded.cmake | 51 ++++++++ src/CMakeLists.txt | 173 +++++++++++++++++++++++++ 5 files changed, 279 insertions(+) create mode 100644 cmake/module/AddBoostIfNeeded.cmake create mode 100644 cmake/module/AddLibeventIfNeeded.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 78ea562a965d3..35ea1d0bdbb76 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,6 +38,7 @@ list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/module) # Configurable options. # When adding a new option, end the with a full stop for consistency. include(CMakeDependentOption) +option(BUILD_DAEMON "Build bitcoind 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) @@ -100,7 +101,14 @@ endif() include(AddThreadsIfNeeded) add_threads_if_needed() +include(AddBoostIfNeeded) +add_boost_if_needed() + include(CheckSourceCompilesAndLinks) + +include(AddLibeventIfNeeded) +add_libevent_if_needed() + include(cmake/introspection.cmake) include(cmake/crc32c.cmake) @@ -116,6 +124,9 @@ add_subdirectory(src) message("\n") message("Configure summary") message("=================") +message("Executables:") +message(" bitcoind ............................ ${BUILD_DAEMON}") +message("") get_directory_property(definitions COMPILE_DEFINITIONS) string(REPLACE ";" " " definitions "${definitions}") message("Preprocessor defined macros ........... ${definitions}") diff --git a/cmake/introspection.cmake b/cmake/introspection.cmake index 3e637bc4d04e8..e3f59ce979a26 100644 --- a/cmake/introspection.cmake +++ b/cmake/introspection.cmake @@ -263,3 +263,7 @@ check_cxx_source_compiles(" int main(){} " HAVE_DLLEXPORT_ATTRIBUTE ) + +if(CMAKE_SYSTEM_NAME STREQUAL "Darwin") + find_program(BREW_COMMAND brew) +endif() diff --git a/cmake/module/AddBoostIfNeeded.cmake b/cmake/module/AddBoostIfNeeded.cmake new file mode 100644 index 0000000000000..0a5ddc0d2f9c0 --- /dev/null +++ b/cmake/module/AddBoostIfNeeded.cmake @@ -0,0 +1,40 @@ +# 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(add_boost_if_needed) + #[=[ + TODO: Not all targets, which will be added in the future, require + Boost. Therefore, a proper check will be appropriate here. + + Implementation notes: + Although only Boost headers are used to build Bitcoin Core, + we still leverage a standard CMake's approach to handle + dependencies, i.e., the Boost::headers "library". + A command target_link_libraries(target PRIVATE Boost::headers) + will propagate Boost::headers usage requirements to the target. + For Boost::headers such usage requirements is an include + directory and other added INTERFACE properties. + ]=] + + if(CMAKE_SYSTEM_NAME STREQUAL "Darwin" AND BREW_COMMAND) + execute_process( + COMMAND ${BREW_COMMAND} --prefix boost + OUTPUT_VARIABLE BOOST_ROOT + ERROR_QUIET + OUTPUT_STRIP_TRAILING_WHITESPACE + ) + endif() + + set(Boost_NO_BOOST_CMAKE ON) + find_package(Boost 1.64.0 REQUIRED) + set_target_properties(Boost::boost PROPERTIES IMPORTED_GLOBAL TRUE) + target_compile_definitions(Boost::boost INTERFACE + $<$:BOOST_MULTI_INDEX_ENABLE_SAFE_MODE> + ) + if(CMAKE_VERSION VERSION_LESS 3.15) + add_library(Boost::headers ALIAS Boost::boost) + endif() + + mark_as_advanced(Boost_INCLUDE_DIR) +endfunction() diff --git a/cmake/module/AddLibeventIfNeeded.cmake b/cmake/module/AddLibeventIfNeeded.cmake new file mode 100644 index 0000000000000..186095700b83a --- /dev/null +++ b/cmake/module/AddLibeventIfNeeded.cmake @@ -0,0 +1,51 @@ +# 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. + +macro(check_evhttp_connection_get_peer target) + # Check whether evhttp_connection_get_peer expects const char**. + # Fail if neither are available. + check_cxx_source_compiles(" + #include + #include + + int main() + { + evhttp_connection* conn = (evhttp_connection*)1; + const char* host; + uint16_t port; + evhttp_connection_get_peer(conn, &host, &port); + } + " HAVE_EVHTTP_CONNECTION_GET_PEER_CONST_CHAR + ) + target_compile_definitions(${target} INTERFACE + $<$:HAVE_EVHTTP_CONNECTION_GET_PEER_CONST_CHAR=1> + ) +endmacro() + +function(add_libevent_if_needed) + # TODO: Not all targets, which will be added in the future, + # require libevent. Therefore, a proper check will be + # appropriate here. + + set(libevent_minimum_version 2.1.8) + + if(MSVC) + find_package(Libevent ${libevent_minimum_version} REQUIRED COMPONENTS extra CONFIG) + check_evhttp_connection_get_peer(libevent::extra) + add_library(libevent::libevent ALIAS libevent::extra) + return() + endif() + + find_package(PkgConfig) + pkg_check_modules(libevent REQUIRED libevent>=${libevent_minimum_version} IMPORTED_TARGET GLOBAL) + check_evhttp_connection_get_peer(PkgConfig::libevent) + target_link_libraries(PkgConfig::libevent INTERFACE + $<$:iphlpapi;ws2_32> + ) + add_library(libevent::libevent ALIAS PkgConfig::libevent) + + if(NOT WIN32) + pkg_check_modules(libevent_pthreads REQUIRED libevent_pthreads>=${libevent_minimum_version} IMPORTED_TARGET) + endif() +endfunction() diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8fea2fe3f9622..244ec39a99a68 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -28,3 +28,176 @@ add_library(bitcoin_consensus OBJECT EXCLUDE_FROM_ALL util/strencodings.cpp ) target_link_libraries(bitcoin_consensus PRIVATE secp256k1) + + +# Home for common functionality shared by different executables and libraries. +# Similar to `bitcoin_util` library, but higher-level. +add_library(bitcoin_common STATIC EXCLUDE_FROM_ALL + base58.cpp + bech32.cpp + chainparams.cpp + coins.cpp + common/bloom.cpp + common/interfaces.cpp + common/run_command.cpp + $<$:common/url.cpp> + compressor.cpp + core_read.cpp + core_write.cpp + deploymentinfo.cpp + external_signer.cpp + init/common.cpp + key.cpp + key_io.cpp + merkleblock.cpp + net_types.cpp + netaddress.cpp + netbase.cpp + net_permissions.cpp + outputtype.cpp + policy/feerate.cpp + policy/policy.cpp + protocol.cpp + psbt.cpp + rpc/rawtransaction_util.cpp + rpc/request.cpp + rpc/external_signer.cpp + rpc/util.cpp + scheduler.cpp + script/descriptor.cpp + script/miniscript.cpp + script/sign.cpp + script/signingprovider.cpp + script/standard.cpp + warnings.cpp +) +target_compile_definitions(bitcoin_common + PRIVATE + ${THREAD_LOCAL_IF_AVAILABLE} +) +target_link_libraries(bitcoin_common + PRIVATE + bitcoin_consensus + bitcoin_util + univalue + secp256k1 + Boost::headers + $ +) + + +# P2P and RPC server functionality used by `bitcoind` and `bitcoin-qt` executables. +add_library(bitcoin_node STATIC EXCLUDE_FROM_ALL + addrdb.cpp + addrman.cpp + banman.cpp + blockencodings.cpp + blockfilter.cpp + chain.cpp + consensus/tx_verify.cpp + dbwrapper.cpp + deploymentstatus.cpp + flatfile.cpp + headerssync.cpp + httprpc.cpp + httpserver.cpp + i2p.cpp + index/base.cpp + index/blockfilterindex.cpp + index/coinstatsindex.cpp + index/txindex.cpp + init.cpp + kernel/chain.cpp + kernel/checks.cpp + kernel/coinstats.cpp + kernel/context.cpp + kernel/cs_main.cpp + kernel/mempool_persist.cpp + mapport.cpp + net.cpp + netgroup.cpp + net_processing.cpp + node/blockstorage.cpp + node/caches.cpp + node/chainstate.cpp + node/chainstatemanager_args.cpp + node/coin.cpp + node/coins_view_args.cpp + node/connection_types.cpp + node/context.cpp + node/database_args.cpp + node/eviction.cpp + node/interface_ui.cpp + node/interfaces.cpp + node/mempool_args.cpp + node/mempool_persist_args.cpp + node/miner.cpp + node/minisketchwrapper.cpp + node/psbt.cpp + node/transaction.cpp + node/txreconciliation.cpp + node/utxo_snapshot.cpp + node/validation_cache_args.cpp + noui.cpp + policy/fees.cpp + policy/fees_args.cpp + policy/packages.cpp + policy/rbf.cpp + policy/settings.cpp + pow.cpp + rest.cpp + rpc/blockchain.cpp + rpc/fees.cpp + rpc/mempool.cpp + rpc/mining.cpp + rpc/net.cpp + rpc/node.cpp + rpc/output_script.cpp + rpc/rawtransaction.cpp + rpc/server.cpp + rpc/server_util.cpp + rpc/signmessage.cpp + rpc/txoutproof.cpp + script/sigcache.cpp + shutdown.cpp + signet.cpp + timedata.cpp + torcontrol.cpp + txdb.cpp + txmempool.cpp + txorphanage.cpp + txrequest.cpp + validation.cpp + validationinterface.cpp + versionbits.cpp + + dummywallet.cpp +) +target_link_libraries(bitcoin_node + PRIVATE + bitcoin_common + bitcoin_util + leveldb + minisketch + univalue + Boost::headers + libevent::libevent + $ +) + + +# Bitcoin Core bitcoind. +if(BUILD_DAEMON) + add_executable(bitcoind + bitcoind.cpp + init/bitcoind.cpp + ) + target_link_libraries(bitcoind + PRIVATE + bitcoin_node + ) + target_link_options(bitcoind + PRIVATE + $<$:-static> + ) +endif()