Skip to content

Commit 14d15dd

Browse files
committed
cmake: Build libbitcoinconsensus library
1 parent bff43ea commit 14d15dd

File tree

6 files changed

+123
-0
lines changed

6 files changed

+123
-0
lines changed

CMakeLists.txt

+15
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ 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+
# We do not use CMake's BUILD_SHARED_LIBS option.
44+
option(BUILD_SHARED "Build shared libraries." ON)
45+
option(BUILD_STATIC "Build static libraries." ON)
46+
if(NOT BUILD_SHARED AND NOT BUILD_STATIC)
47+
message(FATAL_ERROR "At least one of BUILD_SHARED and BUILD_STATIC must be enabled.")
48+
endif()
49+
option(BUILD_BITCOINCONSENSUS_LIB "Build bitcoinconsensus library." ON)
4350
option(ASM "Use assembly routines." ON)
4451

4552
option(ENABLE_WALLET "Enable wallet." ON)
@@ -312,6 +319,10 @@ if(WERROR)
312319
endif()
313320
endif()
314321

322+
if(BUILD_BITCOINCONSENSUS_LIB)
323+
set(HAVE_CONSENSUS_LIB ON)
324+
endif()
325+
315326
find_package(Python3 3.8 COMPONENTS Interpreter)
316327
set(PYTHON_COMMAND ${Python3_EXECUTABLE})
317328

@@ -332,6 +343,10 @@ message(" bitcoin-cli ......................... ${BUILD_CLI}")
332343
message(" bitcoin-tx .......................... ${BUILD_TX}")
333344
message(" bitcoin-util ........................ ${BUILD_UTIL}")
334345
message(" bitcoin-wallet ...................... ${BUILD_WALLET_TOOL}")
346+
message("Libraries:")
347+
message(" build shared ........................ ${BUILD_SHARED}")
348+
message(" build static ........................ ${BUILD_STATIC}")
349+
message(" libbitcoinconsensus ................. ${BUILD_BITCOINCONSENSUS_LIB}")
335350
message("Wallet support:")
336351
message(" SQLite, descriptor wallets .......... ${WITH_SQLITE}")
337352
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

src/CMakeLists.txt

+5
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,11 @@ if(BUILD_UTIL)
294294
endif()
295295

296296

297+
if(BUILD_BITCOINCONSENSUS_LIB)
298+
add_subdirectory(script)
299+
endif()
300+
301+
297302
add_subdirectory(test/util)
298303
if(BUILD_BENCH)
299304
add_subdirectory(bench)

src/bench/CMakeLists.txt

+5
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ add_executable(bench_bitcoin
4848
xor.cpp
4949
)
5050

51+
if(TARGET bitcoinconsensus_static)
52+
target_link_libraries(bench_bitcoin bitcoinconsensus_static)
53+
elseif(TARGET bitcoinconsensus AND NOT WIN32)
54+
target_link_libraries(bench_bitcoin bitcoinconsensus)
55+
endif()
5156
target_link_libraries(bench_bitcoin
5257
core
5358
test_util

src/script/CMakeLists.txt

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
set(bitcoinconsensus_src
9+
$<TARGET_OBJECTS:bitcoin_crypto>
10+
interpreter.cpp
11+
script.cpp
12+
script_error.cpp
13+
../arith_uint256.cpp
14+
../consensus/merkle.cpp
15+
../consensus/tx_check.cpp
16+
../hash.cpp
17+
../primitives/block.cpp
18+
../primitives/transaction.cpp
19+
../pubkey.cpp
20+
../uint256.cpp
21+
../util/strencodings.cpp
22+
)
23+
24+
if(BUILD_SHARED)
25+
add_library(bitcoinconsensus SHARED "")
26+
target_sources(bitcoinconsensus
27+
PRIVATE
28+
bitcoinconsensus.cpp
29+
../support/cleanse.cpp
30+
${bitcoinconsensus_src}
31+
)
32+
target_compile_definitions(bitcoinconsensus
33+
PRIVATE
34+
BUILD_BITCOIN_INTERNAL
35+
)
36+
target_include_directories(bitcoinconsensus
37+
PUBLIC
38+
$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/src>
39+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
40+
)
41+
target_link_libraries(bitcoinconsensus
42+
PRIVATE
43+
core
44+
secp256k1
45+
)
46+
set_target_properties(bitcoinconsensus PROPERTIES
47+
SOVERSION 0
48+
VERSION 0.0.0
49+
)
50+
endif()
51+
52+
if(BUILD_STATIC)
53+
add_library(bitcoinconsensus_static STATIC "")
54+
target_sources(bitcoinconsensus_static
55+
PRIVATE
56+
bitcoinconsensus.cpp
57+
../support/cleanse.cpp
58+
${bitcoinconsensus_src}
59+
)
60+
target_compile_definitions(bitcoinconsensus_static
61+
PRIVATE
62+
BUILD_BITCOIN_INTERNAL
63+
)
64+
target_include_directories(bitcoinconsensus_static
65+
PUBLIC
66+
$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/src>
67+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
68+
)
69+
target_link_libraries(bitcoinconsensus_static
70+
PRIVATE
71+
core
72+
secp256k1
73+
)
74+
if(NOT MSVC)
75+
set_target_properties(bitcoinconsensus_static PROPERTIES
76+
OUTPUT_NAME bitcoinconsensus
77+
)
78+
endif()
79+
endif()
80+
81+
install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/bitcoinconsensus.h"
82+
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
83+
)
84+
85+
set(prefix ${CMAKE_INSTALL_PREFIX})
86+
set(exec_prefix "\${prefix}")
87+
set(libdir "\${exec_prefix}/lib")
88+
set(includedir "\${prefix}/include")
89+
configure_file(${CMAKE_SOURCE_DIR}/libbitcoinconsensus.pc.in libbitcoinconsensus.pc @ONLY)
90+
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libbitcoinconsensus.pc DESTINATION lib/pkgconfig)

src/test/CMakeLists.txt

+5
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ add_executable(test_bitcoin
134134
xoroshiro128plusplus_tests.cpp
135135
)
136136

137+
if(TARGET bitcoinconsensus_static)
138+
target_link_libraries(test_bitcoin bitcoinconsensus_static)
139+
elseif(TARGET bitcoinconsensus AND NOT WIN32)
140+
target_link_libraries(test_bitcoin bitcoinconsensus)
141+
endif()
137142
target_link_libraries(test_bitcoin
138143
core
139144
test_util

0 commit comments

Comments
 (0)