diff --git a/cmake/modules/FindEvent.cmake b/cmake/modules/FindEvent.cmake --- a/cmake/modules/FindEvent.cmake +++ b/cmake/modules/FindEvent.cmake @@ -1,42 +1,87 @@ -# Try to find libevent -# EVENT_FOUND - system has libevent -# EVENT_INCLUDE_DIR - the libevent include directory -# EVENT_LIBRARY - Library needed to use libevent -# EVENT_PTHREAD_LIBRARY - Library needed to use libevent_pthread - -if(EVENT_INCLUDE_DIR AND EVENT_LIBRARY) - # Already in cache, be silent - set(EVENT_FIND_QUIETLY TRUE) +# Copyright (c) 2017-2020 The Bitcoin developers +# Distributed under the MIT software license, see the accompanying +# file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#.rst +# FindEvent +# ------------- +# +# Find the Event library. The following components are available:: +# +# event +# pthreads +# +# This will define the following variables:: +# +# Event_FOUND - system has Event lib +# Event_INCLUDE_DIRS - the Event include directories +# Event_LIBRARIES - Libraries needed to use Event +# Event_VERSION - The library version MAJOR.MINOR.PATCH +# +# And the following imported target:: +# +# Event::event +# Event::pthreads + +find_package(PkgConfig) +pkg_check_modules(PC_Event QUIET libevent) + +include(BrewHelper) +find_brew_prefix(BREW_HINT berkeley-db) + +find_path(Event_INCLUDE_DIR + NAMES event.h + PATHS ${PC_Event_INCLUDE_DIRS} + HINTS ${BREW_HINT} +) + +set(Event_INCLUDE_DIRS ${Event_INCLUDE_DIR}) +mark_as_advanced(Event_INCLUDE_DIR) + +if(Event_INCLUDE_DIR) + include(ExternalLibraryHelper) + + find_component(Event event + NAMES event + HINTS "${BREW_HINT}" + INCLUDE_DIRS ${Event_INCLUDE_DIRS} + PATHS ${PC_Event_LIBRARY_DIRS} + ) + + pkg_check_modules(PC_Event_pthreads QUIET event_pthreads libevent_pthreads) + find_component(Event pthreads + NAMES event_pthreads + INCLUDE_DIRS ${Event_INCLUDE_DIRS} + PATHS ${PC_Event_pthreads_LIBRARY_DIRS} + ) endif() -find_path(EVENT_INCLUDE_DIR NAMES event.h) -find_library(EVENT_LIBRARY NAMES event libevent) - -if(NOT TARGET Event) - # Create a library to be used - add_library(Event STATIC IMPORTED) - set_target_properties(Event PROPERTIES - IMPORTED_LOCATION ${EVENT_LIBRARY} - INTERFACE_INCLUDE_DIRECTORIES ${EVENT_INCLUDE_DIR}) - - # On windows, libevent depends on ws2_32 - if(${CMAKE_SYSTEM_NAME} MATCHES "Windows") - find_library(WS2_32_LIBRARY NAMES ws2_32) - set_target_properties(Event PROPERTIES - IMPORTED_LINK_INTERFACE_LIBRARIES ${WS2_32_LIBRARY}) +if(NOT Event_VERSION) + # If pkgconfig found a version number, use it. + if(PC_Event_VERSION) + set(_Event_VERSION ${PC_Event_VERSION}) + elseif(NOT CMAKE_CROSSCOMPILING) + try_run(_Event_CheckVersion_RESULT _Event_CheckVersion_BUILD + "${CMAKE_BINARY_DIR}" + "${CMAKE_SOURCE_DIR}/cmake/utils/EventCheckVersion.cpp" + CMAKE_FLAGS "-DINCLUDE_DIRECTORIES=${Event_INCLUDE_DIRS}" + LINK_LIBRARIES "${Event_event_LIBRARY}" + RUN_OUTPUT_VARIABLE _Event_VERSION + ) else() - find_library(EVENT_PTHREAD_LIBRARY event_pthreads) - set_target_properties(Event PROPERTIES - IMPORTED_LINK_INTERFACE_LIBRARIES ${EVENT_PTHREAD_LIBRARY}) + # There is no way to determine the version. + # Let's assume the user read the doc. + set(_Event_VERSION 99.99.99) endif() -endif() -message(STATUS "libevent: " ${EVENT_LIBRARY}) + set(Event_VERSION ${_Event_VERSION} + CACHE INTERNAL "Event library full version" + ) +endif() include(FindPackageHandleStandardArgs) -find_package_handle_standard_args(Event DEFAULT_MSG EVENT_INCLUDE_DIR EVENT_LIBRARY) - -mark_as_advanced(EVENT_INCLUDE_DIR EVENT_LIBRARY) - -set(Event_LIBRARIES ${EVENT_LIBRARY}) -set(Event_INCLUDE_DIRS ${EVENT_INCLUDE_DIR}) +find_package_handle_standard_args(Event + REQUIRED_VARS Event_INCLUDE_DIR + VERSION_VAR Event_VERSION + HANDLE_COMPONENTS +) diff --git a/cmake/utils/EventCheckVersion.cpp b/cmake/utils/EventCheckVersion.cpp new file mode 100644 --- /dev/null +++ b/cmake/utils/EventCheckVersion.cpp @@ -0,0 +1,12 @@ +#include +#include +#include + +int main(int argc, char** argv) { + uint32_t version = event_get_version_number(); + std::cout << + ((version & 0xff000000) >> 24) << "." << + ((version & 0x00ff0000) >> 16) << "." << + ((version & 0x0000ff00) >> 8); + return 0; +} diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -489,18 +489,24 @@ versionbits.cpp ) -# This require libevent -find_package(Event REQUIRED) - target_include_directories(server PRIVATE leveldb/helpers/memenv) +# This require libevent +set(EVENT_MIN_VERSION 2.0.22) +find_package(Event ${EVENT_MIN_VERSION} REQUIRED COMPONENTS event) + target_link_libraries(server - Event + Event::event bitcoinconsensus leveldb memenv ) +if(NOT ${CMAKE_SYSTEM_NAME} MATCHES "Windows") + find_package(Event ${EVENT_MIN_VERSION} REQUIRED COMPONENTS pthreads) + target_link_libraries(server Event::pthreads) +endif() + if(ENABLE_UPNP) target_include_directories(server PUBLIC ${MINIUPNPC_INCLUDE_DIR}) target_link_libraries(server ${MINIUPNPC_LIBRARY}) @@ -560,7 +566,7 @@ target_sources(bitcoin-cli PRIVATE bitcoin-cli-res.rc) endif() - target_link_libraries(bitcoin-cli common rpcclient Event) + target_link_libraries(bitcoin-cli common rpcclient Event::event) add_to_symbols_check(bitcoin-cli) add_to_security_check(bitcoin-cli) diff --git a/src/wallet/CMakeLists.txt b/src/wallet/CMakeLists.txt --- a/src/wallet/CMakeLists.txt +++ b/src/wallet/CMakeLists.txt @@ -7,7 +7,7 @@ # Add event dependency. This is only required for evhttp_uridecode # in rpcwallet.cpp so it may be worth considering using an alternative. -find_package(Event REQUIRED) +find_package(Event 2.0.22 REQUIRED COMPONENTS event) add_library(wallet ../interfaces/wallet.cpp @@ -25,4 +25,4 @@ walletutil.cpp ) -target_link_libraries(wallet script univalue Event BerkeleyDB::CXX) +target_link_libraries(wallet script univalue Event::event BerkeleyDB::CXX)