Skip to content

Commit 5dbe30d

Browse files
committed
cmake: Build test_bitcoin executable
1 parent bad1aec commit 5dbe30d

6 files changed

+246
-0
lines changed

CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ tristate_option(WITH_USDT
6767
AUTO
6868
)
6969

70+
option(BUILD_TESTS "Build test_bitcoin executable." ON)
7071
option(BUILD_BENCH "Build bench_bitcoin executable." ON)
7172

7273
if(CXX20)
@@ -172,6 +173,7 @@ message(" UPnP ................................ ${WITH_MINIUPNPC}")
172173
message(" ZeroMQ .............................. ${WITH_ZMQ}")
173174
message(" USDT tracing ........................ ${WITH_USDT}")
174175
message("Tests:")
176+
message(" test_bitcoin ........................ ${BUILD_TESTS}")
175177
message(" bench_bitcoin ....................... ${BUILD_BENCH}")
176178
message("")
177179
if(CMAKE_CROSSCOMPILING)

cmake/module/GenerateHeaders.cmake

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright (c) 2023 The Bitcoin Core developers
2+
# Distributed under the MIT software license, see the accompanying
3+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
function(generate_header_from_json json_source_relpath)
6+
add_custom_command(
7+
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${json_source_relpath}.h
8+
COMMAND ${CMAKE_COMMAND} -DJSON_SOURCE_PATH=${CMAKE_CURRENT_SOURCE_DIR}/${json_source_relpath} -DHEADER_PATH=${CMAKE_CURRENT_BINARY_DIR}/${json_source_relpath}.h -P ${CMAKE_SOURCE_DIR}/cmake/script/GenerateHeaderFromJson.cmake
9+
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${json_source_relpath}
10+
VERBATIM
11+
)
12+
endfunction()
13+
14+
function(generate_header_from_raw raw_source_relpath)
15+
add_custom_command(
16+
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${raw_source_relpath}.h
17+
COMMAND ${CMAKE_COMMAND} -DRAW_SOURCE_PATH=${CMAKE_CURRENT_SOURCE_DIR}/${raw_source_relpath} -DHEADER_PATH=${CMAKE_CURRENT_BINARY_DIR}/${raw_source_relpath}.h -P ${CMAKE_SOURCE_DIR}/cmake/script/GenerateHeaderFromRaw.cmake
18+
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${raw_source_relpath}
19+
VERBATIM
20+
)
21+
endfunction()
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright (c) 2023 The Bitcoin Core developers
2+
# Distributed under the MIT software license, see the accompanying
3+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
file(READ ${JSON_SOURCE_PATH} hex_content HEX)
6+
string(REGEX MATCHALL "([A-Za-z0-9][A-Za-z0-9])" bytes "${hex_content}")
7+
8+
file(WRITE ${HEADER_PATH} "namespace json_tests{\n")
9+
get_filename_component(json_source_basename ${JSON_SOURCE_PATH} NAME_WE)
10+
file(APPEND ${HEADER_PATH} "static unsigned const char ${json_source_basename}[] = {\n")
11+
12+
set(i 0)
13+
foreach(byte ${bytes})
14+
math(EXPR i "${i} + 1")
15+
math(EXPR remainder "${i} % 8")
16+
if(remainder EQUAL 0)
17+
file(APPEND ${HEADER_PATH} "0x${byte},\n")
18+
else()
19+
file(APPEND ${HEADER_PATH} "0x${byte}, ")
20+
endif()
21+
endforeach()
22+
23+
file(APPEND ${HEADER_PATH} "\n};};")
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Copyright (c) 2023 The Bitcoin Core developers
2+
# Distributed under the MIT software license, see the accompanying
3+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
file(READ ${RAW_SOURCE_PATH} hex_content HEX)
6+
string(REGEX MATCHALL "([A-Za-z0-9][A-Za-z0-9])" bytes "${hex_content}")
7+
8+
get_filename_component(raw_source_basename ${RAW_SOURCE_PATH} NAME_WE)
9+
file(WRITE ${HEADER_PATH} "static unsigned const char ${raw_source_basename}_raw[] = {\n")
10+
11+
set(i 0)
12+
foreach(byte ${bytes})
13+
math(EXPR i "${i} + 1")
14+
math(EXPR remainder "${i} % 8")
15+
if(remainder EQUAL 0)
16+
file(APPEND ${HEADER_PATH} "0x${byte},\n")
17+
else()
18+
file(APPEND ${HEADER_PATH} "0x${byte}, ")
19+
endif()
20+
endforeach()
21+
22+
file(APPEND ${HEADER_PATH} "\n};")

src/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -287,3 +287,7 @@ add_subdirectory(test/util)
287287
if(BUILD_BENCH)
288288
add_subdirectory(bench)
289289
endif()
290+
291+
if(BUILD_TESTS)
292+
add_subdirectory(test)
293+
endif()

src/test/CMakeLists.txt

+174
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
# Copyright (c) 2023 The Bitcoin Core developers
2+
# Distributed under the MIT software license, see the accompanying
3+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
include(GenerateHeaders)
6+
generate_header_from_json(data/base58_encode_decode.json)
7+
generate_header_from_json(data/bip341_wallet_vectors.json)
8+
generate_header_from_json(data/blockfilters.json)
9+
generate_header_from_json(data/key_io_invalid.json)
10+
generate_header_from_json(data/key_io_valid.json)
11+
generate_header_from_json(data/script_tests.json)
12+
generate_header_from_json(data/sighash.json)
13+
generate_header_from_json(data/tx_invalid.json)
14+
generate_header_from_json(data/tx_valid.json)
15+
generate_header_from_raw(data/asmap.raw)
16+
17+
add_executable(test_bitcoin
18+
main.cpp
19+
$<TARGET_OBJECTS:bitcoin_consensus>
20+
${CMAKE_CURRENT_BINARY_DIR}/data/asmap.raw.h
21+
${CMAKE_CURRENT_BINARY_DIR}/data/base58_encode_decode.json.h
22+
${CMAKE_CURRENT_BINARY_DIR}/data/bip341_wallet_vectors.json.h
23+
${CMAKE_CURRENT_BINARY_DIR}/data/blockfilters.json.h
24+
${CMAKE_CURRENT_BINARY_DIR}/data/key_io_invalid.json.h
25+
${CMAKE_CURRENT_BINARY_DIR}/data/key_io_valid.json.h
26+
${CMAKE_CURRENT_BINARY_DIR}/data/script_tests.json.h
27+
${CMAKE_CURRENT_BINARY_DIR}/data/sighash.json.h
28+
${CMAKE_CURRENT_BINARY_DIR}/data/tx_invalid.json.h
29+
${CMAKE_CURRENT_BINARY_DIR}/data/tx_valid.json.h
30+
# Tests:
31+
addrman_tests.cpp
32+
allocator_tests.cpp
33+
amount_tests.cpp
34+
argsman_tests.cpp
35+
arith_uint256_tests.cpp
36+
banman_tests.cpp
37+
base32_tests.cpp
38+
base58_tests.cpp
39+
base64_tests.cpp
40+
bech32_tests.cpp
41+
bip32_tests.cpp
42+
blockchain_tests.cpp
43+
blockencodings_tests.cpp
44+
blockfilter_index_tests.cpp
45+
blockfilter_tests.cpp
46+
blockmanager_tests.cpp
47+
bloom_tests.cpp
48+
bswap_tests.cpp
49+
checkqueue_tests.cpp
50+
coins_tests.cpp
51+
coinstatsindex_tests.cpp
52+
compilerbug_tests.cpp
53+
compress_tests.cpp
54+
crypto_tests.cpp
55+
cuckoocache_tests.cpp
56+
dbwrapper_tests.cpp
57+
denialofservice_tests.cpp
58+
descriptor_tests.cpp
59+
flatfile_tests.cpp
60+
fs_tests.cpp
61+
getarg_tests.cpp
62+
hash_tests.cpp
63+
headers_sync_chainwork_tests.cpp
64+
httpserver_tests.cpp
65+
i2p_tests.cpp
66+
interfaces_tests.cpp
67+
key_io_tests.cpp
68+
key_tests.cpp
69+
logging_tests.cpp
70+
mempool_tests.cpp
71+
merkle_tests.cpp
72+
merkleblock_tests.cpp
73+
miner_tests.cpp
74+
miniminer_tests.cpp
75+
miniscript_tests.cpp
76+
minisketch_tests.cpp
77+
multisig_tests.cpp
78+
net_peer_eviction_tests.cpp
79+
net_tests.cpp
80+
netbase_tests.cpp
81+
orphanage_tests.cpp
82+
pmt_tests.cpp
83+
policy_fee_tests.cpp
84+
policyestimator_tests.cpp
85+
pool_tests.cpp
86+
pow_tests.cpp
87+
prevector_tests.cpp
88+
raii_event_tests.cpp
89+
random_tests.cpp
90+
rbf_tests.cpp
91+
rest_tests.cpp
92+
result_tests.cpp
93+
reverselock_tests.cpp
94+
rpc_tests.cpp
95+
sanity_tests.cpp
96+
scheduler_tests.cpp
97+
script_p2sh_tests.cpp
98+
script_parse_tests.cpp
99+
script_segwit_tests.cpp
100+
script_standard_tests.cpp
101+
script_tests.cpp
102+
scriptnum_tests.cpp
103+
serfloat_tests.cpp
104+
serialize_tests.cpp
105+
settings_tests.cpp
106+
sighash_tests.cpp
107+
sigopcount_tests.cpp
108+
skiplist_tests.cpp
109+
sock_tests.cpp
110+
streams_tests.cpp
111+
sync_tests.cpp
112+
system_tests.cpp
113+
timedata_tests.cpp
114+
torcontrol_tests.cpp
115+
transaction_tests.cpp
116+
translation_tests.cpp
117+
txindex_tests.cpp
118+
txpackage_tests.cpp
119+
txreconciliation_tests.cpp
120+
txrequest_tests.cpp
121+
txvalidation_tests.cpp
122+
txvalidationcache_tests.cpp
123+
uint256_tests.cpp
124+
util_tests.cpp
125+
util_threadnames_tests.cpp
126+
validation_block_tests.cpp
127+
validation_chainstate_tests.cpp
128+
validation_chainstatemanager_tests.cpp
129+
validation_flush_tests.cpp
130+
validation_tests.cpp
131+
validationinterface_tests.cpp
132+
versionbits_tests.cpp
133+
xoroshiro128plusplus_tests.cpp
134+
)
135+
136+
target_link_libraries(test_bitcoin
137+
test_util
138+
bitcoin_cli
139+
bitcoin_node
140+
bitcoin_common
141+
bitcoin_util
142+
minisketch
143+
leveldb
144+
univalue
145+
Boost::headers
146+
libevent::libevent
147+
)
148+
149+
if(ENABLE_WALLET)
150+
target_sources(test_bitcoin
151+
PRIVATE
152+
../wallet/test/init_test_fixture.cpp
153+
../wallet/test/wallet_test_fixture.cpp
154+
155+
../wallet/test/coinselector_tests.cpp
156+
../wallet/test/feebumper_tests.cpp
157+
../wallet/test/group_outputs_tests.cpp
158+
../wallet/test/init_tests.cpp
159+
../wallet/test/ismine_tests.cpp
160+
../wallet/test/psbt_wallet_tests.cpp
161+
../wallet/test/rpc_util_tests.cpp
162+
../wallet/test/scriptpubkeyman_tests.cpp
163+
../wallet/test/spend_tests.cpp
164+
../wallet/test/wallet_crypto_tests.cpp
165+
../wallet/test/wallet_tests.cpp
166+
../wallet/test/wallet_transaction_tests.cpp
167+
../wallet/test/walletdb_tests.cpp
168+
../wallet/test/walletload_tests.cpp
169+
)
170+
target_link_libraries(test_bitcoin bitcoin_wallet)
171+
if(USE_BDB)
172+
target_sources(test_bitcoin PRIVATE ../wallet/test/db_tests.cpp)
173+
endif()
174+
endif()

0 commit comments

Comments
 (0)