Skip to content

Commit 92968f5

Browse files
committed
cmake: Build libbitcoinconsensus library
1 parent 92fa541 commit 92968f5

File tree

7 files changed

+106
-0
lines changed

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.
@@ -320,6 +329,10 @@ if(WERROR)
320329
unset(werror_flag)
321330
endif()
322331

332+
if(BUILD_BITCOINCONSENSUS_LIB)
333+
set(HAVE_CONSENSUS_LIB TRUE)
334+
endif()
335+
323336
find_package(Python3 3.9 COMPONENTS Interpreter)
324337
set(PYTHON_COMMAND ${Python3_EXECUTABLE})
325338

@@ -340,6 +353,13 @@ message(" bitcoin-cli ......................... ${BUILD_CLI}")
340353
message(" bitcoin-tx .......................... ${BUILD_TX}")
341354
message(" bitcoin-util ........................ ${BUILD_UTIL}")
342355
message(" bitcoin-wallet ...................... ${BUILD_WALLET_TOOL}")
356+
message("Libraries:")
357+
if(BUILD_SHARED_LIBS)
358+
message(" library type ........................ Shared")
359+
else()
360+
message(" library type ........................ Static")
361+
endif()
362+
message(" libbitcoinconsensus ................. ${BUILD_BITCOINCONSENSUS_LIB}")
343363
message("Wallet support:")
344364
message(" SQLite, descriptor wallets .......... ${WITH_SQLITE}")
345365
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
@@ -306,6 +306,11 @@ if(BUILD_UTIL)
306306
endif()
307307

308308

309+
if(BUILD_BITCOINCONSENSUS_LIB)
310+
add_subdirectory(script)
311+
endif()
312+
313+
309314
add_subdirectory(test/util)
310315
if(BUILD_BENCH)
311316
add_subdirectory(bench)

src/bench/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ target_link_libraries(bench_bitcoin
5353
core
5454
test_util
5555
bitcoin_node
56+
$<TARGET_NAME_IF_EXISTS:bitcoinconsensus>
5657
Boost::headers
5758
)
5859

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

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

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 copy_if_different $<TARGET_FILE:bitcoinconsensus> $<TARGET_FILE_DIR:${target}>
56+
VERBATIM
57+
)
58+
endif()
59+
endfunction()

src/test/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ target_link_libraries(test_bitcoin
142142
bitcoin_cli
143143
bitcoin_node
144144
minisketch
145+
$<TARGET_NAME_IF_EXISTS:bitcoinconsensus>
145146
Boost::headers
146147
)
147148

@@ -172,6 +173,8 @@ if(ENABLE_WALLET)
172173
endif()
173174
endif()
174175

176+
make_bitcoinconsensus_dll_available(test_bitcoin)
177+
175178
install(TARGETS test_bitcoin
176179
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
177180
)

0 commit comments

Comments
 (0)