From b619bdc3303217f4415342fe60e586e18fa48308 Mon Sep 17 00:00:00 2001 From: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> Date: Wed, 6 Nov 2024 15:40:34 +0000 Subject: [PATCH 1/3] cmake: Revamp `FindLibevent` module This change generalizes the use of `find_package` / `pkg_check_modules`, prioritizing the former. --- cmake/module/FindLibevent.cmake | 56 ++++++++++++++++++--------------- doc/build-unix.md | 2 +- src/CMakeLists.txt | 8 +++-- src/test/CMakeLists.txt | 2 +- src/test/fuzz/CMakeLists.txt | 2 +- 5 files changed, 38 insertions(+), 32 deletions(-) diff --git a/cmake/module/FindLibevent.cmake b/cmake/module/FindLibevent.cmake index 901a4f3bd41..280e38adf91 100644 --- a/cmake/module/FindLibevent.cmake +++ b/cmake/module/FindLibevent.cmake @@ -38,43 +38,47 @@ function(check_evhttp_connection_get_peer target) set(HAVE_EVHTTP_CONNECTION_GET_PEER_CONST_CHAR ${HAVE_EVHTTP_CONNECTION_GET_PEER_CONST_CHAR} PARENT_SCOPE) endfunction() +set(_libevent_components core extra) +if(NOT WIN32) + list(APPEND _libevent_components pthreads) +endif() + +find_package(Libevent ${Libevent_FIND_VERSION} QUIET + NO_MODULE +) include(FindPackageHandleStandardArgs) -if(VCPKG_TARGET_TRIPLET) - find_package(Libevent ${Libevent_FIND_VERSION} NO_MODULE QUIET - COMPONENTS extra +if(Libevent_FOUND) + find_package(Libevent ${Libevent_FIND_VERSION} QUIET + REQUIRED COMPONENTS ${_libevent_components} + NO_MODULE ) find_package_handle_standard_args(Libevent REQUIRED_VARS Libevent_DIR VERSION_VAR Libevent_VERSION ) check_evhttp_connection_get_peer(libevent::extra) - add_library(libevent::libevent ALIAS libevent::extra) - mark_as_advanced(Libevent_DIR) - mark_as_advanced(_event_h) - mark_as_advanced(_event_lib) else() find_package(PkgConfig REQUIRED) - pkg_check_modules(libevent QUIET - IMPORTED_TARGET - libevent>=${Libevent_FIND_VERSION} - ) - set(_libevent_required_vars libevent_LIBRARY_DIRS libevent_FOUND) - if(NOT WIN32) - pkg_check_modules(libevent_pthreads QUIET - IMPORTED_TARGET - libevent_pthreads>=${Libevent_FIND_VERSION} + foreach(component IN LISTS _libevent_components) + pkg_check_modules(libevent_${component} + REQUIRED QUIET + IMPORTED_TARGET GLOBAL + libevent_${component}>=${Libevent_FIND_VERSION} ) - list(APPEND _libevent_required_vars libevent_pthreads_FOUND) - endif() + if(TARGET PkgConfig::libevent_${component} AND NOT TARGET libevent::${component}) + add_library(libevent::${component} ALIAS PkgConfig::libevent_${component}) + endif() + endforeach() find_package_handle_standard_args(Libevent - REQUIRED_VARS ${_libevent_required_vars} - VERSION_VAR libevent_VERSION + REQUIRED_VARS libevent_core_LIBRARY_DIRS + VERSION_VAR libevent_core_VERSION ) - unset(_libevent_required_vars) - check_evhttp_connection_get_peer(PkgConfig::libevent) - add_library(libevent::libevent ALIAS PkgConfig::libevent) - if(NOT WIN32) - add_library(libevent::pthreads ALIAS PkgConfig::libevent_pthreads) - endif() + check_evhttp_connection_get_peer(PkgConfig::libevent_extra) endif() + +unset(_libevent_components) + +mark_as_advanced(Libevent_DIR) +mark_as_advanced(_event_h) +mark_as_advanced(_event_lib) diff --git a/doc/build-unix.md b/doc/build-unix.md index a5ad4df11de..4f04b4fd9f1 100644 --- a/doc/build-unix.md +++ b/doc/build-unix.md @@ -182,7 +182,7 @@ Setup and Build Example: Arch Linux ----------------------------------- This example lists the steps necessary to setup and build a command line only distribution of the latest changes on Arch Linux: - pacman --sync --needed cmake boost gcc git libevent make pkgconf python sqlite + pacman --sync --needed cmake boost gcc git libevent make python sqlite git clone https://github.com/bitcoin/bitcoin.git cd bitcoin/ cmake -B build diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 0a651644166..488bd3fc74b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -290,13 +290,14 @@ target_link_libraries(bitcoin_node core_interface bitcoin_common bitcoin_util + $ leveldb minisketch univalue Boost::headers - $ + libevent::core + libevent::extra $ - $ $ ) @@ -366,7 +367,8 @@ if(BUILD_CLI) bitcoin_cli bitcoin_common bitcoin_util - $ + libevent::core + libevent::extra ) list(APPEND installable_targets bitcoin-cli) endif() diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index c376c1905a2..38562be6339 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -153,7 +153,7 @@ target_link_libraries(test_bitcoin minisketch secp256k1 Boost::headers - $ + libevent::extra ) if(ENABLE_WALLET) diff --git a/src/test/fuzz/CMakeLists.txt b/src/test/fuzz/CMakeLists.txt index 2d5f93b4f12..f9330286dc7 100644 --- a/src/test/fuzz/CMakeLists.txt +++ b/src/test/fuzz/CMakeLists.txt @@ -140,7 +140,7 @@ target_link_libraries(fuzz univalue secp256k1 Boost::headers - $ + libevent::extra ) if(ENABLE_WALLET) From ffda355b5a2113fa0f7db8015f7b08bf1351e245 Mon Sep 17 00:00:00 2001 From: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> Date: Wed, 30 Oct 2024 07:33:51 +0000 Subject: [PATCH 2/3] cmake, refactor: Move `HAVE_EVHTTP_...` to `libevent` interface --- cmake/bitcoin-build-config.h.in | 3 --- cmake/module/FindLibevent.cmake | 4 +++- src/httpserver.cpp | 2 -- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/cmake/bitcoin-build-config.h.in b/cmake/bitcoin-build-config.h.in index dce9261da2a..56e0519fac1 100644 --- a/cmake/bitcoin-build-config.h.in +++ b/cmake/bitcoin-build-config.h.in @@ -71,9 +71,6 @@ */ #cmakedefine01 HAVE_DECL_SETSID -/* Define this symbol if evhttp_connection_get_peer expects const char** */ -#cmakedefine HAVE_EVHTTP_CONNECTION_GET_PEER_CONST_CHAR 1 - /* Define to 1 if fdatasync is available. */ #cmakedefine HAVE_FDATASYNC 1 diff --git a/cmake/module/FindLibevent.cmake b/cmake/module/FindLibevent.cmake index 280e38adf91..c006b43d604 100644 --- a/cmake/module/FindLibevent.cmake +++ b/cmake/module/FindLibevent.cmake @@ -35,7 +35,9 @@ function(check_evhttp_connection_get_peer target) " HAVE_EVHTTP_CONNECTION_GET_PEER_CONST_CHAR ) cmake_pop_check_state() - set(HAVE_EVHTTP_CONNECTION_GET_PEER_CONST_CHAR ${HAVE_EVHTTP_CONNECTION_GET_PEER_CONST_CHAR} PARENT_SCOPE) + target_compile_definitions(${target} INTERFACE + $<$:HAVE_EVHTTP_CONNECTION_GET_PEER_CONST_CHAR> + ) endfunction() set(_libevent_components core extra) diff --git a/src/httpserver.cpp b/src/httpserver.cpp index b8772ed852f..e37bc21dea6 100644 --- a/src/httpserver.cpp +++ b/src/httpserver.cpp @@ -2,8 +2,6 @@ // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. -#include // IWYU pragma: keep - #include #include From 5a96767e3f531ba9e8a676eec47727421f9f589f Mon Sep 17 00:00:00 2001 From: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> Date: Wed, 30 Oct 2024 07:34:00 +0000 Subject: [PATCH 3/3] depends, libevent: Do not install *.pc files and remove patches for them --- depends/packages/libevent.mk | 9 +++---- depends/patches/libevent/cmake_fixups.patch | 22 ---------------- depends/patches/libevent/fix_mingw_link.patch | 25 ------------------- 3 files changed, 4 insertions(+), 52 deletions(-) delete mode 100644 depends/patches/libevent/fix_mingw_link.patch diff --git a/depends/packages/libevent.mk b/depends/packages/libevent.mk index 4c05e8a0a74..91bc75c1d30 100644 --- a/depends/packages/libevent.mk +++ b/depends/packages/libevent.mk @@ -4,7 +4,6 @@ $(package)_download_path=https://github.com/libevent/libevent/releases/download/ $(package)_file_name=$(package)-$($(package)_version).tar.gz $(package)_sha256_hash=92e6de1be9ec176428fd2367677e61ceffc2ee1cb119035037a27d346b0403bb $(package)_patches=cmake_fixups.patch -$(package)_patches+=fix_mingw_link.patch $(package)_build_subdir=build # When building for Windows, we set _WIN32_WINNT to target the same Windows @@ -23,8 +22,7 @@ define $(package)_set_vars endef define $(package)_preprocess_cmds - patch -p1 < $($(package)_patch_dir)/cmake_fixups.patch && \ - patch -p1 < $($(package)_patch_dir)/fix_mingw_link.patch + patch -p1 < $($(package)_patch_dir)/cmake_fixups.patch endef define $(package)_config_cmds @@ -40,7 +38,8 @@ define $(package)_stage_cmds endef define $(package)_postprocess_cmds - rm -rf bin && \ + rm -rf bin lib/pkgconfig && \ rm include/ev*.h && \ - rm include/event2/*_compat.h + rm include/event2/*_compat.h && \ + rm lib/libevent.a endef diff --git a/depends/patches/libevent/cmake_fixups.patch b/depends/patches/libevent/cmake_fixups.patch index d80c1a94898..a8812afd1e1 100644 --- a/depends/patches/libevent/cmake_fixups.patch +++ b/depends/patches/libevent/cmake_fixups.patch @@ -1,8 +1,5 @@ cmake: set minimum version to 3.5 -Fix generated pkg-config files, see -https://github.com/libevent/libevent/pull/1165. - --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,7 +19,7 @@ @@ -14,22 +11,3 @@ https://github.com/libevent/libevent/pull/1165. if (POLICY CMP0054) cmake_policy(SET CMP0054 NEW) -diff --git a/cmake/AddEventLibrary.cmake b/cmake/AddEventLibrary.cmake -index 04f5837e..d8ea42c4 100644 ---- a/cmake/AddEventLibrary.cmake -+++ b/cmake/AddEventLibrary.cmake -@@ -20,12 +20,12 @@ macro(generate_pkgconfig LIB_NAME) - - set(LIBS "") - foreach (LIB ${LIB_PLATFORM}) -- set(LIBS "${LIBS} -L${LIB}") -+ set(LIBS "${LIBS} -l${LIB}") - endforeach() - - set(OPENSSL_LIBS "") - foreach(LIB ${OPENSSL_LIBRARIES}) -- set(OPENSSL_LIBS "${OPENSSL_LIBS} -L${LIB}") -+ set(OPENSSL_LIBS "${OPENSSL_LIBS} -l${LIB}") - endforeach() - - configure_file("lib${LIB_NAME}.pc.in" "lib${LIB_NAME}.pc" @ONLY) diff --git a/depends/patches/libevent/fix_mingw_link.patch b/depends/patches/libevent/fix_mingw_link.patch deleted file mode 100644 index 41cbd463c91..00000000000 --- a/depends/patches/libevent/fix_mingw_link.patch +++ /dev/null @@ -1,25 +0,0 @@ -commit d108099913c5fdbe518f3f4d711f248f8522bd10 -Author: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> -Date: Mon Apr 22 06:39:35 2024 +0100 - - build: Add `Iphlpapi` to `Libs.private` in `*.pc` files on Windows - - It has been required since https://github.com/libevent/libevent/pull/923 - at least for the `if_nametoindex` call. - - See https://github.com/libevent/libevent/pull/1622. - - -diff --git a/configure.ac b/configure.ac -index d00e063a..cd1fce37 100644 ---- a/CMakeLists.txt -+++ b/CMakeLists.txt -@@ -906,7 +906,7 @@ if(WIN32) - list(APPEND HDR_PRIVATE WIN32-Code/getopt.h) - - set(EVENT__DNS_USE_FTIME_FOR_ID 1) -- set(LIB_PLATFORM ws2_32 shell32 advapi32) -+ set(LIB_PLATFORM ws2_32 shell32 advapi32 iphlpapi) - add_definitions( - -D_CRT_SECURE_NO_WARNINGS - -D_CRT_NONSTDC_NO_DEPRECATE)