Skip to content

Commit

Permalink
cmake: Revamp handling of data files for {test,bench}_bitcoin targets
Browse files Browse the repository at this point in the history
This change introduces a new `target_data_sources()` function.
  • Loading branch information
hebasto committed Sep 14, 2024
1 parent 0c4ff18 commit 0c35be6
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 47 deletions.
21 changes: 0 additions & 21 deletions cmake/module/GenerateHeaders.cmake

This file was deleted.

36 changes: 36 additions & 0 deletions cmake/module/TargetDataSources.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright (c) 2023-present The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or https://opensource.org/license/mit/.

function(target_data_sources target)
set(options "")
set(one_value_args RAW_NAMESPACE)
set(multi_value_keywords JSON_FILES RAW_FILES)
cmake_parse_arguments(
PARSE_ARGV 1
ARG
"${options}" "${one_value_args}" "${multi_value_keywords}"
)

foreach(json_file IN LISTS ARG_JSON_FILES)
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${json_file}.h
COMMAND ${CMAKE_COMMAND} -DJSON_SOURCE_PATH=${CMAKE_CURRENT_SOURCE_DIR}/${json_file} -DHEADER_PATH=${CMAKE_CURRENT_BINARY_DIR}/${json_file}.h -P ${PROJECT_SOURCE_DIR}/cmake/script/GenerateHeaderFromJson.cmake
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${json_file} ${PROJECT_SOURCE_DIR}/cmake/script/GenerateHeaderFromJson.cmake
MAIN_DEPENDENCY ${CMAKE_CURRENT_SOURCE_DIR}/${json_file}
VERBATIM
)
target_sources(${target} PRIVATE ${json_file})
endforeach()

foreach(raw_file IN LISTS ARG_RAW_FILES)
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${raw_file}.h
COMMAND ${CMAKE_COMMAND} -DRAW_SOURCE_PATH=${CMAKE_CURRENT_SOURCE_DIR}/${raw_file} -DHEADER_PATH=${CMAKE_CURRENT_BINARY_DIR}/${raw_file}.h -DRAW_NAMESPACE=${ARG_RAW_NAMESPACE} -P ${PROJECT_SOURCE_DIR}/cmake/script/GenerateHeaderFromRaw.cmake
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${raw_file} ${PROJECT_SOURCE_DIR}/cmake/script/GenerateHeaderFromRaw.cmake
MAIN_DEPENDENCY ${CMAKE_CURRENT_SOURCE_DIR}/${raw_file}
VERBATIM
)
target_sources(${target} PRIVATE ${raw_file})
endforeach()
endfunction()
11 changes: 7 additions & 4 deletions src/bench/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@
# Distributed under the MIT software license, see the accompanying
# file COPYING or https://opensource.org/license/mit/.

include(GenerateHeaders)
generate_header_from_raw(data/block413567.raw benchmark::data)

add_executable(bench_bitcoin
bench_bitcoin.cpp
bench.cpp
nanobench.cpp
${CMAKE_CURRENT_BINARY_DIR}/data/block413567.raw.h
# Benchmarks:
addrman.cpp
base58.cpp
Expand Down Expand Up @@ -55,6 +51,13 @@ add_executable(bench_bitcoin
xor.cpp
)

include(TargetDataSources)
target_data_sources(bench_bitcoin
RAW_FILES
data/block413567.raw
RAW_NAMESPACE benchmark::data
)

target_link_libraries(bench_bitcoin
core_interface
test_util
Expand Down
39 changes: 17 additions & 22 deletions src/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,11 @@
# Distributed under the MIT software license, see the accompanying
# file COPYING or https://opensource.org/license/mit/.

include(GenerateHeaders)
generate_header_from_json(data/base58_encode_decode.json)
generate_header_from_json(data/bip341_wallet_vectors.json)
generate_header_from_json(data/blockfilters.json)
generate_header_from_json(data/key_io_invalid.json)
generate_header_from_json(data/key_io_valid.json)
generate_header_from_json(data/script_tests.json)
generate_header_from_json(data/sighash.json)
generate_header_from_json(data/tx_invalid.json)
generate_header_from_json(data/tx_valid.json)
generate_header_from_raw(data/asmap.raw test::data)

# Do not use generator expressions in test sources because the
# SOURCES property is processed to gather test suite macros.
add_executable(test_bitcoin
main.cpp
$<TARGET_OBJECTS:bitcoin_consensus>
${CMAKE_CURRENT_BINARY_DIR}/data/asmap.raw.h
${CMAKE_CURRENT_BINARY_DIR}/data/base58_encode_decode.json.h
${CMAKE_CURRENT_BINARY_DIR}/data/bip341_wallet_vectors.json.h
${CMAKE_CURRENT_BINARY_DIR}/data/blockfilters.json.h
${CMAKE_CURRENT_BINARY_DIR}/data/key_io_invalid.json.h
${CMAKE_CURRENT_BINARY_DIR}/data/key_io_valid.json.h
${CMAKE_CURRENT_BINARY_DIR}/data/script_tests.json.h
${CMAKE_CURRENT_BINARY_DIR}/data/sighash.json.h
${CMAKE_CURRENT_BINARY_DIR}/data/tx_invalid.json.h
${CMAKE_CURRENT_BINARY_DIR}/data/tx_valid.json.h
addrman_tests.cpp
allocator_tests.cpp
amount_tests.cpp
Expand Down Expand Up @@ -144,6 +122,23 @@ add_executable(test_bitcoin
versionbits_tests.cpp
)

include(TargetDataSources)
target_data_sources(test_bitcoin
JSON_FILES
data/base58_encode_decode.json
data/bip341_wallet_vectors.json
data/blockfilters.json
data/key_io_invalid.json
data/key_io_valid.json
data/script_tests.json
data/sighash.json
data/tx_invalid.json
data/tx_valid.json
RAW_FILES
data/asmap.raw
RAW_NAMESPACE test::data
)

target_link_libraries(test_bitcoin
core_interface
test_util
Expand Down

0 comments on commit 0c35be6

Please sign in to comment.