Skip to content

Commit

Permalink
Merge #200: cmake: Improve handling libevent dependency
Browse files Browse the repository at this point in the history
b18b727 FIXUP: Switch from `AddLibeventIfNeeded` to `FindLibevent` (Hennadii Stepanov)
2ff3c2d cmake: Add `FindLibevent` module (Hennadii Stepanov)
b568933 FIXUP: Request libevent only when needed (Hennadii Stepanov)

Pull request description:

  The first commit resolves this comment https://github.com/hebasto/bitcoin/blob/05784622feb15252a75dad64cdd5723e612e3f14/cmake/module/AddLibeventIfNeeded.cmake#L31-L33

  ---

  The rest of the PR switches from the `AddLibeventIfNeeded` module to a more "standard" `FindLibevent` one.

  Here are the relevant excerpts from configuration logs:
  1. On Linux, no depends:
  - staging branch:
  ```
  -- Checking for module 'libevent>=2.1.8'
  --   Found libevent, version 2.1.12-stable
  -- Performing Test HAVE_EVHTTP_CONNECTION_GET_PEER_CONST_CHAR
  -- Performing Test HAVE_EVHTTP_CONNECTION_GET_PEER_CONST_CHAR - Failed
  -- Checking for module 'libevent_pthreads>=2.1.8'
  --   Found libevent_pthreads, version 2.1.12-stable
  ```

  - this PR:
  ```
  -- Found Libevent: /usr/lib/x86_64-linux-gnu (found suitable version "2.1.12-stable", minimum required is "2.1.8")
  -- Performing Test HAVE_EVHTTP_CONNECTION_GET_PEER_CONST_CHAR
  -- Performing Test HAVE_EVHTTP_CONNECTION_GET_PEER_CONST_CHAR - Failed
  ```

  2. On Linux, with depends:
  - staging branch:
  ```
  -- Checking for module 'libevent>=2.1.8'
  --   Found libevent, version 2.1.12-stable
  -- Performing Test HAVE_EVHTTP_CONNECTION_GET_PEER_CONST_CHAR
  -- Performing Test HAVE_EVHTTP_CONNECTION_GET_PEER_CONST_CHAR - Failed
  -- Checking for module 'libevent_pthreads>=2.1.8'
  --   Found libevent_pthreads, version 2.1.12-stable
  ```

  - this PR:
  ```
  -- Found Libevent: /home/hebasto/git/bitcoin/depends/x86_64-pc-linux-gnu/lib (found suitable version "2.1.12-stable", minimum required is "2.1.8")
  -- Performing Test HAVE_EVHTTP_CONNECTION_GET_PEER_CONST_CHAR
  -- Performing Test HAVE_EVHTTP_CONNECTION_GET_PEER_CONST_CHAR - Failed
  ```

  3. On Windows, MSVC + vcpkg:
  - staging branch:
  ```
  -- Found libevent include directory: C:/Users/hebasto/source/repos/bitcoin/build/vcpkg_installed/x64-windows/include
  -- Found libevent component: C:/Users/hebasto/source/repos/bitcoin/build/vcpkg_installed/x64-windows/debug/bin/event_extra.dll
  -- Found libevent 2.1.12 in C:/Users/hebasto/source/repos/bitcoin/build/vcpkg_installed/x64-windows
  -- Performing Test HAVE_EVHTTP_CONNECTION_GET_PEER_CONST_CHAR
  -- Performing Test HAVE_EVHTTP_CONNECTION_GET_PEER_CONST_CHAR - Failed
  ```
  - this PR:
  ```
  -- Found Libevent: C:/Users/hebasto/source/repos/bitcoin/build/vcpkg_installed/x64-windows/share/libevent (found suitable version "2.1.12", minimum required is "2.1.8")
  -- Performing Test HAVE_EVHTTP_CONNECTION_GET_PEER_CONST_CHAR
  -- Performing Test HAVE_EVHTTP_CONNECTION_GET_PEER_CONST_CHAR - Failed
  ```

ACKs for top commit:
  pablomartin4btc:
    tACK b18b727

Tree-SHA512: ee4a4d31f18bc60280fbd49d082e3802919de0b21f938baaee57b22dec5e9df3c8da23b519d57376d8108c34470f131018aa7a0627f674a27b237d1dff954c7e
  • Loading branch information
hebasto committed May 20, 2024
2 parents 310e2cb + b18b727 commit 40b7172
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 66 deletions.
5 changes: 3 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -359,8 +359,9 @@ target_link_options(sanitizing_interface INTERFACE ${SANITIZER_LDFLAGS})
include(AddBoostIfNeeded)
add_boost_if_needed()

include(AddLibeventIfNeeded)
add_libevent_if_needed()
if(BUILD_DAEMON OR BUILD_GUI OR BUILD_CLI OR BUILD_TESTS OR BUILD_BENCH OR BUILD_FUZZ_BINARY)
find_package(Libevent 2.1.8 MODULE REQUIRED)
endif()

include(cmake/introspection.cmake)

Expand Down
59 changes: 0 additions & 59 deletions cmake/module/AddLibeventIfNeeded.cmake

This file was deleted.

82 changes: 82 additions & 0 deletions cmake/module/FindLibevent.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Copyright (c) 2024-present The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or https://opensource.org/license/mit/.

#[=======================================================================[
FindLibevent
------------

Finds the Libevent headers and libraries.

This is a wrapper around find_package()/pkg_check_modules() commands that:
- facilitates searching in various build environments
- prints a standard log message

#]=======================================================================]

# Check whether evhttp_connection_get_peer expects const char**.
# See https://github.com/libevent/libevent/commit/a18301a2bb160ff7c3ffaf5b7653c39ffe27b385
function(check_evhttp_connection_get_peer target)
include(CMakePushCheckState)
cmake_push_check_state(RESET)
set(CMAKE_REQUIRED_LIBRARIES ${target})
include(CheckCXXSourceCompiles)
check_cxx_source_compiles("
#include <cstdint>
#include <event2/http.h>
int main()
{
evhttp_connection* conn = (evhttp_connection*)1;
const char* host;
uint16_t port;
evhttp_connection_get_peer(conn, &host, &port);
}
" HAVE_EVHTTP_CONNECTION_GET_PEER_CONST_CHAR
)
cmake_pop_check_state()
target_compile_definitions(${target} INTERFACE
$<$<BOOL:${HAVE_EVHTTP_CONNECTION_GET_PEER_CONST_CHAR}>:HAVE_EVHTTP_CONNECTION_GET_PEER_CONST_CHAR>
)
endfunction()


include(FindPackageHandleStandardArgs)
if(VCPKG_TARGET_TRIPLET)
find_package(Libevent ${Libevent_FIND_VERSION} NO_MODULE QUIET
COMPONENTS extra
)
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}
)
list(APPEND _libevent_required_vars libevent_pthreads_FOUND)
endif()
find_package_handle_standard_args(Libevent
REQUIRED_VARS ${_libevent_required_vars}
VERSION_VAR libevent_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()
endif()
6 changes: 3 additions & 3 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,8 @@ target_link_libraries(bitcoin_node
minisketch
univalue
Boost::headers
libevent::libevent
$<TARGET_NAME_IF_EXISTS:PkgConfig::libevent_pthreads>
$<TARGET_NAME_IF_EXISTS:libevent::libevent>
$<TARGET_NAME_IF_EXISTS:libevent::pthreads>
$<TARGET_NAME_IF_EXISTS:NATPMP::NATPMP>
$<TARGET_NAME_IF_EXISTS:MiniUPnPc::MiniUPnPc>
$<TARGET_NAME_IF_EXISTS:bitcoin_zmq>
Expand Down Expand Up @@ -312,7 +312,7 @@ if(BUILD_CLI)
bitcoin_cli
bitcoin_common
bitcoin_util
libevent::libevent
$<TARGET_NAME_IF_EXISTS:libevent::libevent>
)
list(APPEND installable_targets bitcoin-cli)
endif()
Expand Down
2 changes: 1 addition & 1 deletion src/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ target_link_libraries(test_bitcoin
bitcoin_node
minisketch
Boost::headers
libevent::libevent
$<TARGET_NAME_IF_EXISTS:libevent::libevent>
)

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 @@ -130,7 +130,7 @@ target_link_libraries(fuzz
univalue
secp256k1
Boost::headers
libevent::libevent
$<TARGET_NAME_IF_EXISTS:libevent::libevent>
)

if(ENABLE_WALLET)
Expand Down

0 comments on commit 40b7172

Please sign in to comment.