From 6ce3e63620ebafca0cf4b4e35c8a674bb959ef44 Mon Sep 17 00:00:00 2001 From: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> Date: Fri, 3 Nov 2023 22:36:44 +0000 Subject: [PATCH] cmake: Build `libbitcoinconsensus` library --- CMakeLists.txt | 12 +++++ cmake/bitcoin-config.h.in | 3 ++ cmake/module/GeneratePkgConfigFile.cmake | 13 ++++++ src/CMakeLists.txt | 5 ++ src/bench/CMakeLists.txt | 3 ++ src/script/CMakeLists.txt | 58 ++++++++++++++++++++++++ src/test/CMakeLists.txt | 3 ++ 7 files changed, 97 insertions(+) create mode 100644 cmake/module/GeneratePkgConfigFile.cmake create mode 100644 src/script/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index bbc55f01deddc..a6586283db120 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -40,6 +40,7 @@ option(BUILD_DAEMON "Build bitcoind executable." ON) option(BUILD_CLI "Build bitcoin-cli executable." ON) option(BUILD_TX "Build bitcoin-tx executable." ON) option(BUILD_UTIL "Build bitcoin-util executable." ON) +option(BUILD_BITCOINCONSENSUS_LIB "Build bitcoinconsensus library." ON) option(ASM "Use assembly routines." ON) option(ENABLE_WALLET "Enable wallet." ON) @@ -306,6 +307,10 @@ if(WERROR) unset(werror_flag) endif() +if(BUILD_BITCOINCONSENSUS_LIB) + set(HAVE_CONSENSUS_LIB TRUE) +endif() + find_package(Python3 3.9 COMPONENTS Interpreter) set(PYTHON_COMMAND ${Python3_EXECUTABLE}) @@ -326,6 +331,13 @@ message(" bitcoin-cli ......................... ${BUILD_CLI}") message(" bitcoin-tx .......................... ${BUILD_TX}") message(" bitcoin-util ........................ ${BUILD_UTIL}") message(" bitcoin-wallet ...................... ${BUILD_WALLET_TOOL}") +message("Libraries:") +if(BUILD_SHARED_LIBS) + message(" library type ........................ Shared") +else() + message(" library type ........................ Static") +endif() +message(" libbitcoinconsensus ................. ${BUILD_BITCOINCONSENSUS_LIB}") message("Wallet support:") message(" SQLite, descriptor wallets .......... ${WITH_SQLITE}") message(" Berkeley DB, legacy wallets ......... ${WITH_BDB}") diff --git a/cmake/bitcoin-config.h.in b/cmake/bitcoin-config.h.in index ebbb97562cdb0..c1403f608b911 100644 --- a/cmake/bitcoin-config.h.in +++ b/cmake/bitcoin-config.h.in @@ -46,6 +46,9 @@ /* Define to 1 if you have the header file. */ #cmakedefine HAVE_BYTESWAP_H 1 +/* Define this symbol if the consensus lib has been built */ +#cmakedefine HAVE_CONSENSUS_LIB 1 + /* Define to 1 if you have the declaration of `be16toh', and to 0 if you don't. */ #cmakedefine01 HAVE_DECL_BE16TOH diff --git a/cmake/module/GeneratePkgConfigFile.cmake b/cmake/module/GeneratePkgConfigFile.cmake new file mode 100644 index 0000000000000..39ce264589dc1 --- /dev/null +++ b/cmake/module/GeneratePkgConfigFile.cmake @@ -0,0 +1,13 @@ +# 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(generate_pkg_config_file in_file out_file) + set(prefix ${CMAKE_INSTALL_PREFIX}) + set(exec_prefix "\${prefix}") + set(libdir "\${exec_prefix}/${CMAKE_INSTALL_LIBDIR}") + set(includedir "\${prefix}/${CMAKE_INSTALL_INCLUDEDIR}") + set(PACKAGE_NAME ${PROJECT_NAME}) + set(PACKAGE_VERSION ${PROJECT_VERSION}) + configure_file(${in_file} ${out_file} @ONLY) +endfunction() diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 3f94c0cbcce0e..91a16d58f5194 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -308,6 +308,11 @@ if(BUILD_UTIL) endif() +if(BUILD_BITCOINCONSENSUS_LIB) + add_subdirectory(script) +endif() + + add_subdirectory(test/util) if(BUILD_BENCH) add_subdirectory(bench) diff --git a/src/bench/CMakeLists.txt b/src/bench/CMakeLists.txt index 969748c4280d7..2700d5b4a0305 100644 --- a/src/bench/CMakeLists.txt +++ b/src/bench/CMakeLists.txt @@ -54,6 +54,7 @@ target_link_libraries(bench_bitcoin test_util leveldb univalue + $ Boost::headers ) @@ -68,6 +69,8 @@ if(ENABLE_WALLET) target_link_libraries(bench_bitcoin bitcoin_wallet) endif() +make_bitcoinconsensus_dll_available(bench_bitcoin) + install(TARGETS bench_bitcoin RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} ) diff --git a/src/script/CMakeLists.txt b/src/script/CMakeLists.txt new file mode 100644 index 0000000000000..36582cd20c108 --- /dev/null +++ b/src/script/CMakeLists.txt @@ -0,0 +1,58 @@ +# 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/. + +# Must be included before CMAKE_INSTALL_INCLUDEDIR is used. +include(GNUInstallDirs) + +add_library(bitcoinconsensus + ../support/cleanse.cpp + bitcoinconsensus.cpp + ${bitcoin_crypto_base_sources} + ${bitcoin_consensus_sources} +) +target_compile_definitions(bitcoinconsensus + PRIVATE + BUILD_BITCOIN_INTERNAL +) +target_include_directories(bitcoinconsensus + PUBLIC + $ + $ +) +target_link_libraries(bitcoinconsensus + PRIVATE + core + secp256k1 +) +set_target_properties(bitcoinconsensus PROPERTIES + SOVERSION 0 + VERSION 0.0.0 +) + +install(TARGETS bitcoinconsensus + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} +) +install(FILES bitcoinconsensus.h + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} +) + +include(GeneratePkgConfigFile) +generate_pkg_config_file(${PROJECT_SOURCE_DIR}/libbitcoinconsensus.pc.in libbitcoinconsensus.pc) +install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libbitcoinconsensus.pc + DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig +) + +function(make_bitcoinconsensus_dll_available target) + if(WIN32 AND BUILD_SHARED_LIBS) + # The DLL must reside either in the same folder where the executable is + # or somewhere in PATH. Using the former option. + add_custom_command( + TARGET ${target} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E create_symlink $ $/$ + VERBATIM + ) + endif() +endfunction() diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index c23c6c28d80fc..8a5a1e87b1eff 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -144,6 +144,7 @@ target_link_libraries(test_bitcoin minisketch leveldb univalue + $ Boost::headers libevent::libevent ) @@ -175,6 +176,8 @@ if(ENABLE_WALLET) endif() endif() +make_bitcoinconsensus_dll_available(test_bitcoin) + install(TARGETS test_bitcoin RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} )