Skip to content

Commit

Permalink
CMake: add tests (test_bitcoin) compilation support.
Browse files Browse the repository at this point in the history
  • Loading branch information
aleflm committed Feb 16, 2025
1 parent d62a490 commit 15b4088
Show file tree
Hide file tree
Showing 12 changed files with 290 additions and 140 deletions.
16 changes: 5 additions & 11 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ set(FIRO_DAEMON_NAME firod)
set(FIRO_GUI_NAME firo-qt)
set(FIRO_CLI_NAME firo-cli)
set(FIRO_TX_NAME firo-tx)
set(FIRO_UTIL_NAME firo-util)

set(CLIENT_VERSION_STRING ${PROJECT_VERSION})
if(CLIENT_VERSION_RC GREATER 0)
Expand Down Expand Up @@ -105,14 +104,13 @@ option(BUILD_DAEMON "Build ${FIRO_DAEMON_NAME} executable." ON)
option(BUILD_GUI "Build ${FIRO_GUI_NAME} executable." OFF)
option(BUILD_CLI "Build ${FIRO_CLI_NAME} executable." ON)

option(BUILD_TESTS "Build test_firo executable." ON)
option(BUILD_TX "Build ${FIRO_TX_NAME} executable." ${BUILD_TESTS})
option(BUILD_UTIL "Build ${FIRO_UTIL_NAME} executable and library." ${BUILD_TESTS})

option(BUILD_UTIL_CHAINSTATE "Build experimental firo-chainstate executable." OFF)
option(BUILD_KERNEL_LIB "Build experimental firokernel library." ${BUILD_UTIL_CHAINSTATE})

option(ENABLE_WALLET "Enable wallet." OFF)
cmake_dependent_option(BUILD_TESTS "Build tests." OFF "ENABLE_WALLET" OFF)
option(BUILD_TX "Build ${FIRO_TX_NAME} executable." ${BUILD_TESTS})
option(WITH_BDB "Enable Berkeley DB (BDB) wallet support." OFF)
cmake_dependent_option(WARN_INCOMPATIBLE_BDB "Warn when using a Berkeley DB (BDB) version other than 4.8." ON "WITH_BDB" OFF)
if(WITH_BDB)
Expand All @@ -129,7 +127,6 @@ if(WITH_BDB)
endif()
endif()
endif()
cmake_dependent_option(BUILD_WALLET_TOOL "Build firo-wallet tool." ${BUILD_TESTS} "ENABLE_WALLET" OFF)

option(ENABLE_HARDENING "Attempt to harden the resulting executables." ON)
option(REDUCE_EXPORTS "Attempt to reduce exported symbols in the resulting executables." OFF)
Expand Down Expand Up @@ -162,7 +159,7 @@ endif()
cmake_dependent_option(WITH_DBUS "Enable DBus support." ON "CMAKE_SYSTEM_NAME STREQUAL \"Linux\" AND BUILD_GUI" OFF)


cmake_dependent_option(BUILD_GUI_TESTS "Build test_firo-qt executable." ON "BUILD_GUI;BUILD_TESTS" OFF)
cmake_dependent_option(BUILD_GUI_TESTS "Build test_bitcoin-qt executable." ON "BUILD_GUI;BUILD_TESTS" OFF)
if(BUILD_GUI)
set(qt_components Core Gui Widgets LinguistTools)
if(ENABLE_WALLET)
Expand Down Expand Up @@ -232,7 +229,6 @@ if(BUILD_FOR_FUZZING)
set(BUILD_UTIL OFF)
set(BUILD_UTIL_CHAINSTATE OFF)
set(BUILD_KERNEL_LIB OFF)
set(BUILD_WALLET_TOOL OFF)
set(BUILD_GUI OFF)
set(ENABLE_EXTERNAL_SIGNER OFF)
set(WITH_ZMQ OFF)
Expand Down Expand Up @@ -735,8 +731,6 @@ message(" firod ............................ ${BUILD_DAEMON}")
message(" firo-qt (GUI) .................... ${BUILD_GUI}")
message(" firo-cli ......................... ${BUILD_CLI}")
message(" firo-tx .......................... ${BUILD_TX}")
message(" firo-util ........................ ${BUILD_UTIL}")
message(" firo-wallet ...................... ${BUILD_WALLET_TOOL}")
message(" firo-chainstate (experimental) ... ${BUILD_UTIL_CHAINSTATE}")
message(" libfirokernel (experimental) ..... ${BUILD_KERNEL_LIB}")
message("Optional features:")
Expand All @@ -750,8 +744,8 @@ message(" USDT tracing ........................ ${WITH_USDT}")
message(" QR code (GUI) ....................... ${WITH_QRENCODE}")
message(" DBus (GUI, Linux only) .............. ${WITH_DBUS}")
message("Tests:")
message(" test_firo ........................ ${BUILD_TESTS}")
message(" test_firo-qt ..................... ${BUILD_GUI_TESTS}")
message(" test_bitcoin ........................ ${BUILD_TESTS}")
message(" test_bitcoin-qt ..................... ${BUILD_GUI_TESTS}")
message(" bench_firo ....................... ${BUILD_BENCH}")
message(" fuzz binary ......................... ${BUILD_FUZZ_BINARY}")
message("")
Expand Down
40 changes: 26 additions & 14 deletions cmake/script/GenerateHeaderFromJson.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,33 @@
# Distributed under the MIT software license, see the accompanying
# file COPYING or https://opensource.org/license/mit/.

cmake_path(GET JSON_SOURCE_PATH STEM json_source_basename)
message(STATUS "Generating header ${HEADER_PATH} from ${JSON_SOURCE_PATH}")

file(READ ${JSON_SOURCE_PATH} hex_content HEX)
string(REGEX REPLACE "................" "\\0\n" formatted_bytes "${hex_content}")
string(REGEX REPLACE "[^\n][^\n]" "0x\\0, " formatted_bytes "${formatted_bytes}")
# Generate a header file from a JSON file containing a hex dump
function(generate_json_header JSON_SOURCE_PATH HEADER_PATH)
# Use get_filename_component instead of cmake_path
get_filename_component(json_source_basename "${JSON_SOURCE_PATH}" NAME_WE)

set(header_content
"#include <string_view>
# Create directory
get_filename_component(header_dir "${HEADER_PATH}" DIRECTORY)
file(MAKE_DIRECTORY "${header_dir}")

# Read and convert to hex
file(READ ${JSON_SOURCE_PATH} hex_content HEX)
string(REGEX REPLACE ".." "0x\\0, " formatted_bytes "${hex_content}")

namespace json_tests {
inline constexpr char detail_${json_source_basename}_bytes[] {
${formatted_bytes}
};
# Generate content
set(header_content
"namespace json_tests{
static unsigned const char ${json_source_basename}[] = {
${formatted_bytes}
};
};"
)

# Atomic write using temporary file
file(WRITE "${HEADER_PATH}.new" "${header_content}")
file(RENAME "${HEADER_PATH}.new" "${HEADER_PATH}")
endfunction()

inline constexpr std::string_view ${json_source_basename}{std::begin(detail_${json_source_basename}_bytes), std::end(detail_${json_source_basename}_bytes)};
}
")
file(WRITE ${HEADER_PATH} "${header_content}")
generate_json_header(${JSON_SOURCE_PATH} ${HEADER_PATH})
3 changes: 1 addition & 2 deletions depends/toolchain.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ set(CMAKE_OBJDUMP "@OBJDUMP@")
set(CMAKE_FIND_USE_CMAKE_ENVIRONMENT_PATH OFF)

set(CMAKE_FIND_ROOT_PATH "@depends_prefix@")
list(APPEND CMAKE_PREFIX_PATH "${CMAKE_FIND_ROOT_PATH}/lib/;${CMAKE_FIND_ROOT_PATH}/lib/pkgconfig")
set(ENV{PKG_CONFIG_PATH} "${CMAKE_FIND_ROOT_PATH}/lib/pkgconfig:$ENV{PKG_CONFIG_PATH}")
list(APPEND CMAKE_PREFIX_PATH "${CMAKE_FIND_ROOT_PATH}/lib/")
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
Expand Down
76 changes: 0 additions & 76 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,10 @@ endforeach()
if(DEFINED ENV{CFLAGS})
deduplicate_flags(CMAKE_C_FLAGS)
endif()
set(CMAKE_EXPORT_COMPILE_COMMANDS OFF)
add_subdirectory(secp256k1)
set_target_properties(secp256k1 PROPERTIES
EXCLUDE_FROM_ALL TRUE
)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
string(APPEND CMAKE_C_COMPILE_OBJECT " ${APPEND_CPPFLAGS} ${APPEND_CFLAGS}")

add_library(bitcoin_consensus STATIC EXCLUDE_FROM_ALL
Expand Down Expand Up @@ -249,70 +247,9 @@ target_link_libraries(bitcoin_common
$<$<PLATFORM_ID:Windows>:ws2_32>
)

add_library(bitcoin_wallet STATIC EXCLUDE_FROM_ALL
activemasternode.cpp
hdmint/hdmint.cpp
masternode-sync.cpp
hdmint/mintpool.cpp
hdmint/wallet.cpp
sigma.cpp
lelantus.cpp
spark/state.cpp
wallet/crypter.cpp
wallet/bip39.cpp
wallet/mnemoniccontainer.cpp
wallet/db.cpp
wallet/rpcdump.cpp
wallet/rpcwallet.cpp
wallet/sigmaspendbuilder.cpp
wallet/txbuilder.cpp
wallet/lelantusjoinsplitbuilder.cpp
wallet/walletexcept.cpp
wallet/wallet.cpp
spark/sparkwallet.cpp
spark/primitives.cpp
wallet/walletdb.cpp
wallet/authhelper.cpp
hdmint/tracker.cpp
policy/rbf.cpp
bip47/account.cpp
bip47/paymentchannel.cpp
bip47/bip47utils.cpp
bip47/paymentcode.cpp
bip47/secretpoint.cpp
primitives/mint_spend.cpp
)
target_link_libraries(bitcoin_wallet
PUBLIC
core_interface
bitcoin_util
univalue
secp256k1
Boost::headers
leveldb
)

set(installable_targets)
if(ENABLE_WALLET)
add_subdirectory(wallet)

if(BUILD_WALLET_TOOL)
add_executable(firo-wallet
bitcoin-wallet.cpp
init/bitcoin-wallet.cpp
wallet/wallettool.cpp
)
add_windows_resources(firo-wallet bitcoin-wallet-res.rc)
target_link_libraries(firo-wallet
core_interface
firo_wallet
bitcoin_common
bitcoin_util
bitcoin_wallet
Boost::headers
)
list(APPEND installable_targets firo-wallet)
endif()
endif()


Expand All @@ -322,7 +259,6 @@ add_library(firo_node STATIC EXCLUDE_FROM_ALL
addrman.cpp
blockencodings.cpp
chain.cpp
consensus/tx_verify.cpp
dbwrapper.cpp
httprpc.cpp
httpserver.cpp
Expand Down Expand Up @@ -502,7 +438,6 @@ if(BUILD_DAEMON)
firo_cli
secp256k1
secp256k1pp
bitcoin_wallet
$<TARGET_NAME_IF_EXISTS:firo_wallet>
)
list(APPEND installable_targets firod)
Expand Down Expand Up @@ -592,17 +527,6 @@ if(BUILD_TX)
endif()


if(BUILD_UTIL)
add_executable(bitcoin-util bitcoin-util.cpp)
add_windows_resources(bitcoin-util bitcoin-util-res.rc)
target_link_libraries(bitcoin-util
core_interface
bitcoin_common
bitcoin_util
)
list(APPEND installable_targets bitcoin-util)
endif()


if(BUILD_GUI)
add_subdirectory(qt)
Expand Down
2 changes: 1 addition & 1 deletion src/Makefile.test.include
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ BITCOIN_TESTS += \
wallet/test/wallet_tests.cpp \
wallet/test/crypto_tests.cpp \
wallet/test/lelantus_tests.cpp \
wallet/test/spark_tests.cpp \
wallet/test/spark_wallet_tests.cpp \
wallet/test/sigma_tests.cpp \
wallet/test/mnemonic_tests.cpp \
wallet/test/txbuilder_tests.cpp
Expand Down
16 changes: 11 additions & 5 deletions src/secp256k1/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ target_link_libraries(secp256k1
OpenSSL::Crypto
)

target_include_directories(secp256k1
PUBLIC
${GMP_INCLUDES}
)

#=============================
# secp256k1 Configuration
#=============================
Expand Down Expand Up @@ -54,11 +59,8 @@ target_link_libraries(secp256k1pp
PUBLIC secp256k1 ${GMP_LIBRARIES}
)

target_include_directories(secp256k1pp
PUBLIC ${GMP_INCLUDES}
)

add_library(secp256k1_asm INTERFACE)
target_link_libraries(secp256k1_asm INTERFACE ${GMP_LIBRARIES})
if(SECP256K1_ASM STREQUAL "arm32")
add_library(secp256k1_asm_arm OBJECT EXCLUDE_FROM_ALL)
target_sources(secp256k1_asm_arm PUBLIC
Expand Down Expand Up @@ -133,25 +135,29 @@ if(SECP256K1_BUILD_BENCHMARK)
target_link_libraries(bench_verify PUBLIC secp256k1)
add_executable(bench_internal bench_internal.c)
target_link_libraries(bench_internal PUBLIC secp256k1_asm ${GMP_LIBRARIES})
target_include_directories(bench_internal PUBLIC ${GMP_INCLUDES})
endif()

if(SECP256K1_BUILD_TESTS)
add_executable(noverify_tests tests.c)
target_compile_definitions(noverify_tests PRIVATE VERIFY)
target_link_libraries(noverify_tests secp256k1_asm ${GMP_LIBRARIES})
target_include_directories(noverify_tests PUBLIC ${GMP_INCLUDES})
add_test(NAME secp256k1_noverify_tests COMMAND noverify_tests)
if(NOT CMAKE_BUILD_TYPE STREQUAL "Coverage")
add_executable(tests tests.c)
target_compile_definitions(tests PRIVATE VERIFY)
target_link_libraries(tests secp256k1_asm ${GMP_LIBRARIES})
target_include_directories(tests PUBLIC ${GMP_INCLUDES})
add_test(NAME secp256k1_tests COMMAND tests)
endif()
endif()

if(SECP256K1_BUILD_EXHAUSTIVE_TESTS)
# Note: do not include secp256k1_precomputed in exhaustive_tests (it uses runtime-generated tables).
add_executable(exhaustive_tests tests_exhaustive.c)
target_link_libraries(exhaustive_tests secp256k1_asm ${GMP_LIBRARIES})
target_link_libraries(exhaustive_tests PRIVATE secp256k1_asm ${GMP_LIBRARIES})
target_include_directories(exhaustive_tests PUBLIC ${GMP_INCLUDES})
target_compile_definitions(exhaustive_tests PRIVATE $<$<NOT:$<CONFIG:Coverage>>:VERIFY>)
add_test(NAME secp256k1_exhaustive_tests COMMAND exhaustive_tests)
endif()
Expand Down
Loading

0 comments on commit 15b4088

Please sign in to comment.