Skip to content

Commit dd0ef98

Browse files
committed
cmake: Build libbitcoinconsensus library
1 parent 1e8eee9 commit dd0ef98

7 files changed

+106
-0
lines changed

CMakeLists.txt

+20
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ option(BUILD_DAEMON "Build bitcoind executable." ON)
4040
option(BUILD_CLI "Build bitcoin-cli executable." ON)
4141
option(BUILD_TX "Build bitcoin-tx executable." ON)
4242
option(BUILD_UTIL "Build bitcoin-util executable." ON)
43+
option(BUILD_BITCOINCONSENSUS_LIB "Build bitcoinconsensus library." ON)
4344
option(ASM "Use assembly routines." ON)
4445

4546
option(ENABLE_WALLET "Enable wallet." ON)
@@ -95,9 +96,17 @@ unset(check_pie_output)
9596
# It is intended to be a usage requirement for all other targets.
9697
add_library(core INTERFACE)
9798

99+
# The lib interface library aims to encapsulate build flags, which
100+
# are specific to non-internal libraries.
101+
# It is intended to be a usage requirement for non-internal library
102+
# targets.
103+
add_library(lib INTERFACE)
104+
98105
include(TryAppendCXXFlags)
99106
include(TryAppendLinkerFlag)
100107

108+
try_append_linker_flag("-Wl,--no-undefined" TARGET lib)
109+
101110
if(WIN32)
102111
#[=[
103112
This build system supports two ways to build binaries for Windows.
@@ -306,6 +315,10 @@ if(WERROR)
306315
unset(werror_flag)
307316
endif()
308317

318+
if(BUILD_BITCOINCONSENSUS_LIB)
319+
set(HAVE_CONSENSUS_LIB TRUE)
320+
endif()
321+
309322
find_package(Python3 3.9 COMPONENTS Interpreter)
310323
set(PYTHON_COMMAND ${Python3_EXECUTABLE})
311324

@@ -326,6 +339,13 @@ message(" bitcoin-cli ......................... ${BUILD_CLI}")
326339
message(" bitcoin-tx .......................... ${BUILD_TX}")
327340
message(" bitcoin-util ........................ ${BUILD_UTIL}")
328341
message(" bitcoin-wallet ...................... ${BUILD_WALLET_TOOL}")
342+
message("Libraries:")
343+
if(BUILD_SHARED_LIBS)
344+
message(" library type ........................ Shared")
345+
else()
346+
message(" library type ........................ Static")
347+
endif()
348+
message(" libbitcoinconsensus ................. ${BUILD_BITCOINCONSENSUS_LIB}")
329349
message("Wallet support:")
330350
message(" SQLite, descriptor wallets .......... ${WITH_SQLITE}")
331351
message(" Berkeley DB, legacy wallets ......... ${WITH_BDB}")

cmake/bitcoin-config.h.in

+3
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@
4646
/* Define to 1 if you have the <byteswap.h> header file. */
4747
#cmakedefine HAVE_BYTESWAP_H 1
4848

49+
/* Define this symbol if the consensus lib has been built */
50+
#cmakedefine HAVE_CONSENSUS_LIB 1
51+
4952
/* Define to 1 if you have the declaration of `be16toh', and to 0 if you
5053
don't. */
5154
#cmakedefine01 HAVE_DECL_BE16TOH
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright (c) 2023-present The Bitcoin Core developers
2+
# Distributed under the MIT software license, see the accompanying
3+
# file COPYING or https://opensource.org/license/mit/.
4+
5+
function(generate_pkg_config_file in_file out_file)
6+
set(prefix ${CMAKE_INSTALL_PREFIX})
7+
set(exec_prefix "\${prefix}")
8+
set(libdir "\${exec_prefix}/${CMAKE_INSTALL_LIBDIR}")
9+
set(includedir "\${prefix}/${CMAKE_INSTALL_INCLUDEDIR}")
10+
set(PACKAGE_NAME ${PROJECT_NAME})
11+
set(PACKAGE_VERSION ${PROJECT_VERSION})
12+
configure_file(${in_file} ${out_file} @ONLY)
13+
endfunction()

src/CMakeLists.txt

+5
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,11 @@ if(BUILD_UTIL)
308308
endif()
309309

310310

311+
if(BUILD_BITCOINCONSENSUS_LIB)
312+
add_subdirectory(script)
313+
endif()
314+
315+
311316
add_subdirectory(test/util)
312317
if(BUILD_BENCH)
313318
add_subdirectory(bench)

src/bench/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ target_link_libraries(bench_bitcoin
5454
test_util
5555
leveldb
5656
univalue
57+
$<TARGET_NAME_IF_EXISTS:bitcoinconsensus>
5758
Boost::headers
5859
)
5960

@@ -68,6 +69,8 @@ if(ENABLE_WALLET)
6869
target_link_libraries(bench_bitcoin bitcoin_wallet)
6970
endif()
7071

72+
make_bitcoinconsensus_dll_available(bench_bitcoin)
73+
7174
install(TARGETS bench_bitcoin
7275
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
7376
)

src/script/CMakeLists.txt

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Copyright (c) 2023-present The Bitcoin Core developers
2+
# Distributed under the MIT software license, see the accompanying
3+
# file COPYING or https://opensource.org/license/mit/.
4+
5+
# Must be included before CMAKE_INSTALL_INCLUDEDIR is used.
6+
include(GNUInstallDirs)
7+
8+
add_library(bitcoinconsensus
9+
../support/cleanse.cpp
10+
bitcoinconsensus.cpp
11+
${bitcoin_crypto_base_sources}
12+
${bitcoin_consensus_sources}
13+
)
14+
target_compile_definitions(bitcoinconsensus
15+
PRIVATE
16+
BUILD_BITCOIN_INTERNAL
17+
)
18+
target_include_directories(bitcoinconsensus
19+
PUBLIC
20+
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/src>
21+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
22+
)
23+
target_link_libraries(bitcoinconsensus
24+
PRIVATE
25+
core
26+
lib
27+
secp256k1
28+
)
29+
set_target_properties(bitcoinconsensus PROPERTIES
30+
SOVERSION 0
31+
VERSION 0.0.0
32+
)
33+
34+
install(TARGETS bitcoinconsensus
35+
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
36+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
37+
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
38+
)
39+
install(FILES bitcoinconsensus.h
40+
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
41+
)
42+
43+
include(GeneratePkgConfigFile)
44+
generate_pkg_config_file(${PROJECT_SOURCE_DIR}/libbitcoinconsensus.pc.in libbitcoinconsensus.pc)
45+
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libbitcoinconsensus.pc
46+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig
47+
)
48+
49+
function(make_bitcoinconsensus_dll_available target)
50+
if(WIN32 AND BUILD_SHARED_LIBS)
51+
# The DLL must reside either in the same folder where the executable is
52+
# or somewhere in PATH. Using the former option.
53+
add_custom_command(
54+
TARGET ${target} POST_BUILD
55+
COMMAND ${CMAKE_COMMAND} -E create_symlink $<TARGET_FILE:bitcoinconsensus> $<TARGET_FILE_DIR:${target}>/$<TARGET_FILE_NAME:bitcoinconsensus>
56+
VERBATIM
57+
)
58+
endif()
59+
endfunction()

src/test/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ target_link_libraries(test_bitcoin
144144
minisketch
145145
leveldb
146146
univalue
147+
$<TARGET_NAME_IF_EXISTS:bitcoinconsensus>
147148
Boost::headers
148149
libevent::libevent
149150
)
@@ -175,6 +176,8 @@ if(ENABLE_WALLET)
175176
endif()
176177
endif()
177178

179+
make_bitcoinconsensus_dll_available(test_bitcoin)
180+
178181
install(TARGETS test_bitcoin
179182
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
180183
)

0 commit comments

Comments
 (0)