Skip to content

Commit

Permalink
Merge bitcoin/bitcoin#31181: cmake: Revamp FindLibevent module
Browse files Browse the repository at this point in the history
5a96767 depends, libevent: Do not install *.pc files and remove patches for them (Hennadii Stepanov)
ffda355 cmake, refactor: Move `HAVE_EVHTTP_...` to `libevent` interface (Hennadii Stepanov)
b619bdc cmake: Revamp `FindLibevent` module (Hennadii Stepanov)

Pull request description:

  This PR generalizes the use of `find_package` / `pkg_check_modules`, prioritizing the former.

  Addresses bitcoin/bitcoin#30903 (comment):
  > We should also follow up with refactoring the libevent module, to more generically use CMake/pkg-config, rather than restricting the CMake usage to `vcpkg`. At that point, we'd likely be able to dump pkg-config for the depends path entirely.

  Similar to bitcoin/bitcoin#30903.

ACKs for top commit:
  fanquake:
    ACK 5a96767

Tree-SHA512: 181020c16ccd2821e718c73f264badcdc5e62980c4a8d9691e759efe2ea00da2326e26308d1dcfdeac01e9e27930428ecace9f36941deee951b751b138d7266c
  • Loading branch information
fanquake committed Nov 11, 2024
2 parents 9a8e5ad + 5a96767 commit 8d340be
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 90 deletions.
3 changes: 0 additions & 3 deletions cmake/bitcoin-build-config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
60 changes: 33 additions & 27 deletions cmake/module/FindLibevent.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -35,46 +35,52 @@ 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
$<$<BOOL:${HAVE_EVHTTP_CONNECTION_GET_PEER_CONST_CHAR}>:HAVE_EVHTTP_CONNECTION_GET_PEER_CONST_CHAR>
)
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)
9 changes: 4 additions & 5 deletions depends/packages/libevent.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
22 changes: 0 additions & 22 deletions depends/patches/libevent/cmake_fixups.patch
Original file line number Diff line number Diff line change
@@ -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 @@
Expand All @@ -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)
25 changes: 0 additions & 25 deletions depends/patches/libevent/fix_mingw_link.patch

This file was deleted.

2 changes: 1 addition & 1 deletion doc/build-unix.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -290,13 +290,14 @@ target_link_libraries(bitcoin_node
core_interface
bitcoin_common
bitcoin_util
$<TARGET_NAME_IF_EXISTS:bitcoin_zmq>
leveldb
minisketch
univalue
Boost::headers
$<TARGET_NAME_IF_EXISTS:libevent::libevent>
libevent::core
libevent::extra
$<TARGET_NAME_IF_EXISTS:libevent::pthreads>
$<TARGET_NAME_IF_EXISTS:bitcoin_zmq>
$<TARGET_NAME_IF_EXISTS:USDT::headers>
)

Expand Down Expand Up @@ -366,7 +367,8 @@ if(BUILD_CLI)
bitcoin_cli
bitcoin_common
bitcoin_util
$<TARGET_NAME_IF_EXISTS:libevent::libevent>
libevent::core
libevent::extra
)
list(APPEND installable_targets bitcoin-cli)
endif()
Expand Down
2 changes: 0 additions & 2 deletions src/httpserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <bitcoin-build-config.h> // IWYU pragma: keep

#include <httpserver.h>

#include <chainparamsbase.h>
Expand Down
2 changes: 1 addition & 1 deletion src/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ target_link_libraries(test_bitcoin
minisketch
secp256k1
Boost::headers
$<TARGET_NAME_IF_EXISTS:libevent::libevent>
libevent::extra
)

if(ENABLE_WALLET)
Expand Down
2 changes: 1 addition & 1 deletion src/test/fuzz/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ target_link_libraries(fuzz
univalue
secp256k1
Boost::headers
$<TARGET_NAME_IF_EXISTS:libevent::libevent>
libevent::extra
)

if(ENABLE_WALLET)
Expand Down

0 comments on commit 8d340be

Please sign in to comment.