Skip to content

Commit 6ce3e63

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

7 files changed

+97
-0
lines changed

CMakeLists.txt

+12
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)
@@ -306,6 +307,10 @@ if(WERROR)
306307
unset(werror_flag)
307308
endif()
308309

310+
if(BUILD_BITCOINCONSENSUS_LIB)
311+
set(HAVE_CONSENSUS_LIB TRUE)
312+
endif()
313+
309314
find_package(Python3 3.9 COMPONENTS Interpreter)
310315
set(PYTHON_COMMAND ${Python3_EXECUTABLE})
311316

@@ -326,6 +331,13 @@ message(" bitcoin-cli ......................... ${BUILD_CLI}")
326331
message(" bitcoin-tx .......................... ${BUILD_TX}")
327332
message(" bitcoin-util ........................ ${BUILD_UTIL}")
328333
message(" bitcoin-wallet ...................... ${BUILD_WALLET_TOOL}")
334+
message("Libraries:")
335+
if(BUILD_SHARED_LIBS)
336+
message(" library type ........................ Shared")
337+
else()
338+
message(" library type ........................ Static")
339+
endif()
340+
message(" libbitcoinconsensus ................. ${BUILD_BITCOINCONSENSUS_LIB}")
329341
message("Wallet support:")
330342
message(" SQLite, descriptor wallets .......... ${WITH_SQLITE}")
331343
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

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
secp256k1
27+
)
28+
set_target_properties(bitcoinconsensus PROPERTIES
29+
SOVERSION 0
30+
VERSION 0.0.0
31+
)
32+
33+
install(TARGETS bitcoinconsensus
34+
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
35+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
36+
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
37+
)
38+
install(FILES bitcoinconsensus.h
39+
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
40+
)
41+
42+
include(GeneratePkgConfigFile)
43+
generate_pkg_config_file(${PROJECT_SOURCE_DIR}/libbitcoinconsensus.pc.in libbitcoinconsensus.pc)
44+
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libbitcoinconsensus.pc
45+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig
46+
)
47+
48+
function(make_bitcoinconsensus_dll_available target)
49+
if(WIN32 AND BUILD_SHARED_LIBS)
50+
# The DLL must reside either in the same folder where the executable is
51+
# or somewhere in PATH. Using the former option.
52+
add_custom_command(
53+
TARGET ${target} POST_BUILD
54+
COMMAND ${CMAKE_COMMAND} -E create_symlink $<TARGET_FILE:bitcoinconsensus> $<TARGET_FILE_DIR:${target}>/$<TARGET_FILE_NAME:bitcoinconsensus>
55+
VERBATIM
56+
)
57+
endif()
58+
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)