Skip to content

Commit 6a6a818

Browse files
committed
cmake: Build libbitcoinconsensus library
1 parent b42b187 commit 6a6a818

7 files changed

+103
-0
lines changed

CMakeLists.txt

+17
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)
@@ -258,6 +259,11 @@ if(HARDENING)
258259
try_append_cxx_flags("-fcf-protection=full" TARGET hardening)
259260

260261
if(MINGW)
262+
add_library(link_ssp INTERFACE)
263+
target_link_libraries(link_ssp INTERFACE
264+
$<$<BOOL:${BUILD_SHARED_LIBS}>:ssp>
265+
)
266+
261267
# stack-clash-protection doesn't compile with GCC 10 and earlier.
262268
# In any case, it is a no-op for Windows.
263269
# See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90458 for more details.
@@ -303,6 +309,10 @@ if(WERROR)
303309
unset(werror_flag)
304310
endif()
305311

312+
if(BUILD_BITCOINCONSENSUS_LIB)
313+
set(HAVE_CONSENSUS_LIB ON)
314+
endif()
315+
306316
find_package(Python3 3.9 COMPONENTS Interpreter)
307317
set(PYTHON_COMMAND ${Python3_EXECUTABLE})
308318

@@ -323,6 +333,13 @@ message(" bitcoin-cli ......................... ${BUILD_CLI}")
323333
message(" bitcoin-tx .......................... ${BUILD_TX}")
324334
message(" bitcoin-util ........................ ${BUILD_UTIL}")
325335
message(" bitcoin-wallet ...................... ${BUILD_WALLET_TOOL}")
336+
message("Libraries:")
337+
if(BUILD_SHARED_LIBS)
338+
message(" library type ........................ Shared")
339+
else()
340+
message(" library type ........................ Static")
341+
endif()
342+
message(" libbitcoinconsensus ................. ${BUILD_BITCOINCONSENSUS_LIB}")
326343
message("Wallet support:")
327344
message(" SQLite, descriptor wallets .......... ${WITH_SQLITE}")
328345
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
@@ -302,6 +302,11 @@ if(BUILD_UTIL)
302302
endif()
303303

304304

305+
if(BUILD_BITCOINCONSENSUS_LIB)
306+
add_subdirectory(script)
307+
endif()
308+
309+
305310
add_subdirectory(test/util)
306311
if(BUILD_BENCH)
307312
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+
bitcoinconsensus.cpp
10+
$<TARGET_OBJECTS:bitcoin_consensus>
11+
$<TARGET_OBJECTS:bitcoin_crypto>
12+
../support/cleanse.cpp
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+
$<TARGET_NAME_IF_EXISTS:link_ssp>
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)