From 15ce0774f84281abff008a30890ac9ca58417ed8 Mon Sep 17 00:00:00 2001 From: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> Date: Sun, 22 Jan 2023 14:27:51 +0000 Subject: [PATCH] cmake: Add `MULTIPROCESS` option --- CMakeLists.txt | 2 ++ cmake/module/GenerateMPCode.cmake | 12 ++++++++++++ cmake/optional.cmake | 14 ++++++++++++++ depends/Makefile | 1 + depends/toolchain.cmake.in | 9 +++++++++ src/CMakeLists.txt | 16 ++++++++++++++++ src/ipc/CMakeLists.txt | 32 +++++++++++++++++++++++++++++++ src/qt/CMakeLists.txt | 22 +++++++++++++++++++-- src/test/CMakeLists.txt | 29 ++++++++++++++++++++++++++++ 9 files changed, 135 insertions(+), 2 deletions(-) create mode 100644 cmake/module/GenerateMPCode.cmake create mode 100644 src/ipc/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index 2c5d2fe2a12f2..53ee75426cd73 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -83,6 +83,7 @@ tristate_option(WITH_EXTERNAL_SIGNER "if Boost.Process is found." AUTO ) +tristate_option(MULTIPROCESS "Build multiprocess bitcoin-node, bitcoin-wallet, and bitcoin-gui executables in addition to monolithic bitcoind and bitcoin-qt executables. Requires libmultiprocess library. Experimental." "if libmultiprocess is found." OFF) option(BUILD_TESTS "Build test_bitcoin executable." ON) option(BUILD_BENCH "Build bench_bitcoin executable." ON) @@ -428,6 +429,7 @@ message(" Berkeley DB, legacy wallets ......... ${WITH_BDB}") message("Optional packages:") message(" GUI ................................. ${WITH_GUI}") message(" external signer ..................... ${WITH_EXTERNAL_SIGNER}") +message(" multiprocess ........................ ${MULTIPROCESS}") message(" NAT-PMP ............................. ${WITH_NATPMP}") message(" UPnP ................................ ${WITH_MINIUPNPC}") message(" ZeroMQ .............................. ${WITH_ZMQ}") diff --git a/cmake/module/GenerateMPCode.cmake b/cmake/module/GenerateMPCode.cmake new file mode 100644 index 0000000000000..8e0e54e3c1c5a --- /dev/null +++ b/cmake/module/GenerateMPCode.cmake @@ -0,0 +1,12 @@ +# 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/. + +macro(generate_from_capnp capnp_relpath) + add_custom_command( + OUTPUT ${capnp_relpath}.c++ ${capnp_relpath}.h ${capnp_relpath}.proxy-client.c++ ${capnp_relpath}.proxy-types.h ${capnp_relpath}.proxy-server.c++ ${capnp_relpath}.proxy-types.c++ ${capnp_relpath}.proxy.h + COMMAND ${MPGEN_PREFIX}/bin/mpgen ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/${capnp_relpath} + DEPENDS ${capnp_relpath} + VERBATIM + ) +endmacro() diff --git a/cmake/optional.cmake b/cmake/optional.cmake index 6bc803fb656f0..0bc9e8c58eaf8 100644 --- a/cmake/optional.cmake +++ b/cmake/optional.cmake @@ -187,3 +187,17 @@ else() set(WITH_SQLITE OFF) set(WITH_BDB OFF) endif() + +if(MULTIPROCESS) + # TODO: Switch to find_package() command. + include(CrossPkgConfig) + cross_pkg_check_modules(libmultiprocess libmultiprocess IMPORTED_TARGET) + if(libmultiprocess_FOUND) + set(MULTIPROCESS ON) + set(MPGEN_PREFIX "${libmultiprocess_PREFIX}" CACHE PATH "libmultiprocess codegen tool prefix.") + elseif(MULTIPROCESS STREQUAL "AUTO") + set(MULTIPROCESS OFF) + else() + message(FATAL_ERROR "\"-DMULTIPROCESS=ON\" specified, but libmultiprocess library was not found.") + endif() +endif() diff --git a/depends/Makefile b/depends/Makefile index ab91b5b72a317..8ca233942cc12 100644 --- a/depends/Makefile +++ b/depends/Makefile @@ -286,6 +286,7 @@ $(host_prefix)/share/toolchain.cmake : toolchain.cmake.in $(host_prefix)/.stamp_ -e 's|@no_natpmp@|$(NO_NATPMP)|' \ -e 's|@no_usdt@|$(NO_USDT)|' \ -e 's|@no_harden@|$(NO_HARDEN)|' \ + -e 's|@multiprocess@|$(MULTIPROCESS)|' \ $< > $@ touch $@ diff --git a/depends/toolchain.cmake.in b/depends/toolchain.cmake.in index 81532f8bcc8a7..f7d30e8496cca 100644 --- a/depends/toolchain.cmake.in +++ b/depends/toolchain.cmake.in @@ -146,3 +146,12 @@ endif() if(NOT HARDENING AND "@no_harden@" STREQUAL "1") set(HARDENING OFF CACHE STRING "") endif() + +if("@multiprocess@" STREQUAL "1") + if(NOT MULTIPROCESS) + set(MULTIPROCESS ON CACHE STRING "") + endif() + if(NOT MPGEN_PREFIX) + set(MPGEN_PREFIX "${CMAKE_FIND_ROOT_PATH}/native" CACHE PATH "") + endif() +endif() diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 85ead14bfed1e..fd23e3835f1e6 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -35,6 +35,9 @@ add_dependencies(bitcoin_clientversion generate_build_info) add_subdirectory(crypto) add_subdirectory(univalue) add_subdirectory(util) +if(MULTIPROCESS) + add_subdirectory(ipc) +endif() add_library(bitcoin_consensus_sources INTERFACE) @@ -277,6 +280,19 @@ if(BUILD_DAEMON) ) list(APPEND installable_targets bitcoind) endif() +if(MULTIPROCESS) + add_executable(bitcoin-node + bitcoind.cpp + init/bitcoin-node.cpp + ) + target_link_libraries(bitcoin-node + core_interface + bitcoin_node + bitcoin_ipc + $ + ) + list(APPEND installable_targets bitcoin-node) +endif() add_library(bitcoin_cli STATIC EXCLUDE_FROM_ALL diff --git a/src/ipc/CMakeLists.txt b/src/ipc/CMakeLists.txt new file mode 100644 index 0000000000000..c722d2c874ebf --- /dev/null +++ b/src/ipc/CMakeLists.txt @@ -0,0 +1,32 @@ +# 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/. + +include(GenerateMPCode) +generate_from_capnp(capnp/echo.capnp) +generate_from_capnp(capnp/init.capnp) + +add_library(bitcoin_ipc STATIC EXCLUDE_FROM_ALL + ${CMAKE_CURRENT_BINARY_DIR}/capnp/echo.capnp.c++ + ${CMAKE_CURRENT_BINARY_DIR}/capnp/echo.capnp.proxy-client.c++ + ${CMAKE_CURRENT_BINARY_DIR}/capnp/echo.capnp.proxy-server.c++ + ${CMAKE_CURRENT_BINARY_DIR}/capnp/echo.capnp.proxy-types.c++ + ${CMAKE_CURRENT_BINARY_DIR}/capnp/init.capnp.c++ + ${CMAKE_CURRENT_BINARY_DIR}/capnp/init.capnp.proxy-client.c++ + ${CMAKE_CURRENT_BINARY_DIR}/capnp/init.capnp.proxy-server.c++ + ${CMAKE_CURRENT_BINARY_DIR}/capnp/init.capnp.proxy-types.c++ + capnp/protocol.cpp + interfaces.cpp + process.cpp +) + +target_include_directories(bitcoin_ipc + PUBLIC + $ +) + +target_link_libraries(bitcoin_ipc + PRIVATE + core_interface + PkgConfig::libmultiprocess +) diff --git a/src/qt/CMakeLists.txt b/src/qt/CMakeLists.txt index d3b7fea9d25a5..1dd2dba142b51 100644 --- a/src/qt/CMakeLists.txt +++ b/src/qt/CMakeLists.txt @@ -179,12 +179,30 @@ target_link_libraries(bitcoin-qt ) import_plugins(bitcoin-qt) - +set(installable_targets bitcoin-qt) if(WIN32) set_target_properties(bitcoin-qt PROPERTIES WIN32_EXECUTABLE TRUE) endif() -install(TARGETS bitcoin-qt +if(MULTIPROCESS) + add_executable(bitcoin-gui + main.cpp + ../init/bitcoin-gui.cpp + ) + target_link_libraries(bitcoin-gui + core_interface + bitcoinqt + bitcoin_node + bitcoin_ipc + ) + import_plugins(bitcoin-gui) + list(APPEND installable_targets bitcoin-gui) + if(WIN32) + set_target_properties(bitcoin-gui PROPERTIES WIN32_EXECUTABLE TRUE) + endif() +endif() + +install(TARGETS ${installable_targets} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT GUI ) diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index c54899aea9d99..20e37a252261c 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -174,6 +174,35 @@ if(ENABLE_WALLET) endif() endif() +if(MULTIPROCESS) + include(GenerateMPCode) + generate_from_capnp(ipc_test.capnp) + + add_library(bitcoin_ipc_test STATIC EXCLUDE_FROM_ALL + ${CMAKE_CURRENT_BINARY_DIR}/ipc_test.capnp.c++ + ${CMAKE_CURRENT_BINARY_DIR}/ipc_test.capnp.proxy-client.c++ + ${CMAKE_CURRENT_BINARY_DIR}/ipc_test.capnp.proxy-server.c++ + ${CMAKE_CURRENT_BINARY_DIR}/ipc_test.capnp.proxy-types.c++ + ipc_test.cpp + ) + target_include_directories(bitcoin_ipc_test + PUBLIC + $ + ) + target_link_libraries(bitcoin_ipc_test + PRIVATE + core_interface + PkgConfig::libmultiprocess + univalue + ) + + target_sources(test_bitcoin + PRIVATE + ipc_tests.cpp + ) + target_link_libraries(test_bitcoin bitcoin_ipc_test) +endif() + install(TARGETS test_bitcoin RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} )