Skip to content

Commit a80fb45

Browse files
committed
cmake: Build bitcoind executable
1 parent ffd10d1 commit a80fb45

File tree

5 files changed

+279
-0
lines changed

5 files changed

+279
-0
lines changed

CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/module)
3838
# Configurable options.
3939
# When adding a new option, end the <help_text> with a full stop for consistency.
4040
include(CMakeDependentOption)
41+
option(BUILD_DAEMON "Build bitcoind executable." ON)
4142
option(ASM "Use assembly routines." ON)
4243
cmake_dependent_option(CXX20 "Enable compilation in C++20 mode." OFF "NOT MSVC" ON)
4344
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()
100101
include(AddThreadsIfNeeded)
101102
add_threads_if_needed()
102103

104+
include(AddBoostIfNeeded)
105+
add_boost_if_needed()
106+
103107
include(CheckSourceCompilesAndLinks)
108+
109+
include(AddLibeventIfNeeded)
110+
add_libevent_if_needed()
111+
104112
include(cmake/introspection.cmake)
105113

106114
include(cmake/crc32c.cmake)
@@ -116,6 +124,9 @@ add_subdirectory(src)
116124
message("\n")
117125
message("Configure summary")
118126
message("=================")
127+
message("Executables:")
128+
message(" bitcoind ............................ ${BUILD_DAEMON}")
129+
message("")
119130
get_directory_property(definitions COMPILE_DEFINITIONS)
120131
string(REPLACE ";" " " definitions "${definitions}")
121132
message("Preprocessor defined macros ........... ${definitions}")

cmake/introspection.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,3 +263,7 @@ check_cxx_source_compiles("
263263
int main(){}
264264
" HAVE_DLLEXPORT_ATTRIBUTE
265265
)
266+
267+
if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin")
268+
find_program(BREW_COMMAND brew)
269+
endif()
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Copyright (c) 2023 The Bitcoin Core developers
2+
# Distributed under the MIT software license, see the accompanying
3+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
function(add_boost_if_needed)
6+
#[=[
7+
TODO: Not all targets, which will be added in the future, require
8+
Boost. Therefore, a proper check will be appropriate here.
9+
10+
Implementation notes:
11+
Although only Boost headers are used to build Bitcoin Core,
12+
we still leverage a standard CMake's approach to handle
13+
dependencies, i.e., the Boost::headers "library".
14+
A command target_link_libraries(target PRIVATE Boost::headers)
15+
will propagate Boost::headers usage requirements to the target.
16+
For Boost::headers such usage requirements is an include
17+
directory and other added INTERFACE properties.
18+
]=]
19+
20+
if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin" AND BREW_COMMAND)
21+
execute_process(
22+
COMMAND ${BREW_COMMAND} --prefix boost
23+
OUTPUT_VARIABLE BOOST_ROOT
24+
ERROR_QUIET
25+
OUTPUT_STRIP_TRAILING_WHITESPACE
26+
)
27+
endif()
28+
29+
set(Boost_NO_BOOST_CMAKE ON)
30+
find_package(Boost 1.64.0 REQUIRED)
31+
set_target_properties(Boost::boost PROPERTIES IMPORTED_GLOBAL TRUE)
32+
target_compile_definitions(Boost::boost INTERFACE
33+
$<$<CONFIG:Debug>:BOOST_MULTI_INDEX_ENABLE_SAFE_MODE>
34+
)
35+
if(CMAKE_VERSION VERSION_LESS 3.15)
36+
add_library(Boost::headers ALIAS Boost::boost)
37+
endif()
38+
39+
mark_as_advanced(Boost_INCLUDE_DIR)
40+
endfunction()
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Copyright (c) 2023 The Bitcoin Core developers
2+
# Distributed under the MIT software license, see the accompanying
3+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
macro(check_evhttp_connection_get_peer target)
6+
# Check whether evhttp_connection_get_peer expects const char**.
7+
# Fail if neither are available.
8+
check_cxx_source_compiles("
9+
#include <cstdint>
10+
#include <event2/http.h>
11+
12+
int main()
13+
{
14+
evhttp_connection* conn = (evhttp_connection*)1;
15+
const char* host;
16+
uint16_t port;
17+
evhttp_connection_get_peer(conn, &host, &port);
18+
}
19+
" HAVE_EVHTTP_CONNECTION_GET_PEER_CONST_CHAR
20+
)
21+
target_compile_definitions(${target} INTERFACE
22+
$<$<BOOL:${HAVE_EVHTTP_CONNECTION_GET_PEER_CONST_CHAR}>:HAVE_EVHTTP_CONNECTION_GET_PEER_CONST_CHAR=1>
23+
)
24+
endmacro()
25+
26+
function(add_libevent_if_needed)
27+
# TODO: Not all targets, which will be added in the future,
28+
# require libevent. Therefore, a proper check will be
29+
# appropriate here.
30+
31+
set(libevent_minimum_version 2.1.8)
32+
33+
if(MSVC)
34+
find_package(Libevent ${libevent_minimum_version} REQUIRED COMPONENTS extra CONFIG)
35+
check_evhttp_connection_get_peer(libevent::extra)
36+
add_library(libevent::libevent ALIAS libevent::extra)
37+
return()
38+
endif()
39+
40+
find_package(PkgConfig)
41+
pkg_check_modules(libevent REQUIRED libevent>=${libevent_minimum_version} IMPORTED_TARGET GLOBAL)
42+
check_evhttp_connection_get_peer(PkgConfig::libevent)
43+
target_link_libraries(PkgConfig::libevent INTERFACE
44+
$<$<BOOL:${MINGW}>:iphlpapi;ws2_32>
45+
)
46+
add_library(libevent::libevent ALIAS PkgConfig::libevent)
47+
48+
if(NOT WIN32)
49+
pkg_check_modules(libevent_pthreads REQUIRED libevent_pthreads>=${libevent_minimum_version} IMPORTED_TARGET)
50+
endif()
51+
endfunction()

src/CMakeLists.txt

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,176 @@ add_library(bitcoin_consensus OBJECT EXCLUDE_FROM_ALL
2828
util/strencodings.cpp
2929
)
3030
target_link_libraries(bitcoin_consensus PRIVATE secp256k1)
31+
32+
33+
# Home for common functionality shared by different executables and libraries.
34+
# Similar to `bitcoin_util` library, but higher-level.
35+
add_library(bitcoin_common STATIC EXCLUDE_FROM_ALL
36+
base58.cpp
37+
bech32.cpp
38+
chainparams.cpp
39+
coins.cpp
40+
common/bloom.cpp
41+
common/interfaces.cpp
42+
common/run_command.cpp
43+
$<$<TARGET_EXISTS:libevent::libevent>:common/url.cpp>
44+
compressor.cpp
45+
core_read.cpp
46+
core_write.cpp
47+
deploymentinfo.cpp
48+
external_signer.cpp
49+
init/common.cpp
50+
key.cpp
51+
key_io.cpp
52+
merkleblock.cpp
53+
net_types.cpp
54+
netaddress.cpp
55+
netbase.cpp
56+
net_permissions.cpp
57+
outputtype.cpp
58+
policy/feerate.cpp
59+
policy/policy.cpp
60+
protocol.cpp
61+
psbt.cpp
62+
rpc/rawtransaction_util.cpp
63+
rpc/request.cpp
64+
rpc/external_signer.cpp
65+
rpc/util.cpp
66+
scheduler.cpp
67+
script/descriptor.cpp
68+
script/miniscript.cpp
69+
script/sign.cpp
70+
script/signingprovider.cpp
71+
script/standard.cpp
72+
warnings.cpp
73+
)
74+
target_compile_definitions(bitcoin_common
75+
PRIVATE
76+
${THREAD_LOCAL_IF_AVAILABLE}
77+
)
78+
target_link_libraries(bitcoin_common
79+
PRIVATE
80+
bitcoin_consensus
81+
bitcoin_util
82+
univalue
83+
secp256k1
84+
Boost::headers
85+
$<TARGET_NAME_IF_EXISTS:libevent::libevent>
86+
)
87+
88+
89+
# P2P and RPC server functionality used by `bitcoind` and `bitcoin-qt` executables.
90+
add_library(bitcoin_node STATIC EXCLUDE_FROM_ALL
91+
addrdb.cpp
92+
addrman.cpp
93+
banman.cpp
94+
blockencodings.cpp
95+
blockfilter.cpp
96+
chain.cpp
97+
consensus/tx_verify.cpp
98+
dbwrapper.cpp
99+
deploymentstatus.cpp
100+
flatfile.cpp
101+
headerssync.cpp
102+
httprpc.cpp
103+
httpserver.cpp
104+
i2p.cpp
105+
index/base.cpp
106+
index/blockfilterindex.cpp
107+
index/coinstatsindex.cpp
108+
index/txindex.cpp
109+
init.cpp
110+
kernel/chain.cpp
111+
kernel/checks.cpp
112+
kernel/coinstats.cpp
113+
kernel/context.cpp
114+
kernel/cs_main.cpp
115+
kernel/mempool_persist.cpp
116+
mapport.cpp
117+
net.cpp
118+
netgroup.cpp
119+
net_processing.cpp
120+
node/blockstorage.cpp
121+
node/caches.cpp
122+
node/chainstate.cpp
123+
node/chainstatemanager_args.cpp
124+
node/coin.cpp
125+
node/coins_view_args.cpp
126+
node/connection_types.cpp
127+
node/context.cpp
128+
node/database_args.cpp
129+
node/eviction.cpp
130+
node/interface_ui.cpp
131+
node/interfaces.cpp
132+
node/mempool_args.cpp
133+
node/mempool_persist_args.cpp
134+
node/miner.cpp
135+
node/minisketchwrapper.cpp
136+
node/psbt.cpp
137+
node/transaction.cpp
138+
node/txreconciliation.cpp
139+
node/utxo_snapshot.cpp
140+
node/validation_cache_args.cpp
141+
noui.cpp
142+
policy/fees.cpp
143+
policy/fees_args.cpp
144+
policy/packages.cpp
145+
policy/rbf.cpp
146+
policy/settings.cpp
147+
pow.cpp
148+
rest.cpp
149+
rpc/blockchain.cpp
150+
rpc/fees.cpp
151+
rpc/mempool.cpp
152+
rpc/mining.cpp
153+
rpc/net.cpp
154+
rpc/node.cpp
155+
rpc/output_script.cpp
156+
rpc/rawtransaction.cpp
157+
rpc/server.cpp
158+
rpc/server_util.cpp
159+
rpc/signmessage.cpp
160+
rpc/txoutproof.cpp
161+
script/sigcache.cpp
162+
shutdown.cpp
163+
signet.cpp
164+
timedata.cpp
165+
torcontrol.cpp
166+
txdb.cpp
167+
txmempool.cpp
168+
txorphanage.cpp
169+
txrequest.cpp
170+
validation.cpp
171+
validationinterface.cpp
172+
versionbits.cpp
173+
174+
dummywallet.cpp
175+
)
176+
target_link_libraries(bitcoin_node
177+
PRIVATE
178+
bitcoin_common
179+
bitcoin_util
180+
leveldb
181+
minisketch
182+
univalue
183+
Boost::headers
184+
libevent::libevent
185+
$<TARGET_NAME_IF_EXISTS:PkgConfig::libevent_pthreads>
186+
)
187+
188+
189+
# Bitcoin Core bitcoind.
190+
if(BUILD_DAEMON)
191+
add_executable(bitcoind
192+
bitcoind.cpp
193+
init/bitcoind.cpp
194+
)
195+
target_link_libraries(bitcoind
196+
PRIVATE
197+
bitcoin_node
198+
)
199+
target_link_options(bitcoind
200+
PRIVATE
201+
$<$<BOOL:${MINGW}>:-static>
202+
)
203+
endif()

0 commit comments

Comments
 (0)