Skip to content

Commit b619bdc

Browse files
committed
cmake: Revamp FindLibevent module
This change generalizes the use of `find_package` / `pkg_check_modules`, prioritizing the former.
1 parent 45e2f8f commit b619bdc

File tree

5 files changed

+38
-32
lines changed

5 files changed

+38
-32
lines changed

cmake/module/FindLibevent.cmake

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -38,43 +38,47 @@ function(check_evhttp_connection_get_peer target)
3838
set(HAVE_EVHTTP_CONNECTION_GET_PEER_CONST_CHAR ${HAVE_EVHTTP_CONNECTION_GET_PEER_CONST_CHAR} PARENT_SCOPE)
3939
endfunction()
4040

41+
set(_libevent_components core extra)
42+
if(NOT WIN32)
43+
list(APPEND _libevent_components pthreads)
44+
endif()
45+
46+
find_package(Libevent ${Libevent_FIND_VERSION} QUIET
47+
NO_MODULE
48+
)
4149

4250
include(FindPackageHandleStandardArgs)
43-
if(VCPKG_TARGET_TRIPLET)
44-
find_package(Libevent ${Libevent_FIND_VERSION} NO_MODULE QUIET
45-
COMPONENTS extra
51+
if(Libevent_FOUND)
52+
find_package(Libevent ${Libevent_FIND_VERSION} QUIET
53+
REQUIRED COMPONENTS ${_libevent_components}
54+
NO_MODULE
4655
)
4756
find_package_handle_standard_args(Libevent
4857
REQUIRED_VARS Libevent_DIR
4958
VERSION_VAR Libevent_VERSION
5059
)
5160
check_evhttp_connection_get_peer(libevent::extra)
52-
add_library(libevent::libevent ALIAS libevent::extra)
53-
mark_as_advanced(Libevent_DIR)
54-
mark_as_advanced(_event_h)
55-
mark_as_advanced(_event_lib)
5661
else()
5762
find_package(PkgConfig REQUIRED)
58-
pkg_check_modules(libevent QUIET
59-
IMPORTED_TARGET
60-
libevent>=${Libevent_FIND_VERSION}
61-
)
62-
set(_libevent_required_vars libevent_LIBRARY_DIRS libevent_FOUND)
63-
if(NOT WIN32)
64-
pkg_check_modules(libevent_pthreads QUIET
65-
IMPORTED_TARGET
66-
libevent_pthreads>=${Libevent_FIND_VERSION}
63+
foreach(component IN LISTS _libevent_components)
64+
pkg_check_modules(libevent_${component}
65+
REQUIRED QUIET
66+
IMPORTED_TARGET GLOBAL
67+
libevent_${component}>=${Libevent_FIND_VERSION}
6768
)
68-
list(APPEND _libevent_required_vars libevent_pthreads_FOUND)
69-
endif()
69+
if(TARGET PkgConfig::libevent_${component} AND NOT TARGET libevent::${component})
70+
add_library(libevent::${component} ALIAS PkgConfig::libevent_${component})
71+
endif()
72+
endforeach()
7073
find_package_handle_standard_args(Libevent
71-
REQUIRED_VARS ${_libevent_required_vars}
72-
VERSION_VAR libevent_VERSION
74+
REQUIRED_VARS libevent_core_LIBRARY_DIRS
75+
VERSION_VAR libevent_core_VERSION
7376
)
74-
unset(_libevent_required_vars)
75-
check_evhttp_connection_get_peer(PkgConfig::libevent)
76-
add_library(libevent::libevent ALIAS PkgConfig::libevent)
77-
if(NOT WIN32)
78-
add_library(libevent::pthreads ALIAS PkgConfig::libevent_pthreads)
79-
endif()
77+
check_evhttp_connection_get_peer(PkgConfig::libevent_extra)
8078
endif()
79+
80+
unset(_libevent_components)
81+
82+
mark_as_advanced(Libevent_DIR)
83+
mark_as_advanced(_event_h)
84+
mark_as_advanced(_event_lib)

doc/build-unix.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ Setup and Build Example: Arch Linux
182182
-----------------------------------
183183
This example lists the steps necessary to setup and build a command line only distribution of the latest changes on Arch Linux:
184184

185-
pacman --sync --needed cmake boost gcc git libevent make pkgconf python sqlite
185+
pacman --sync --needed cmake boost gcc git libevent make python sqlite
186186
git clone https://github.com/bitcoin/bitcoin.git
187187
cd bitcoin/
188188
cmake -B build

src/CMakeLists.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,13 +290,14 @@ target_link_libraries(bitcoin_node
290290
core_interface
291291
bitcoin_common
292292
bitcoin_util
293+
$<TARGET_NAME_IF_EXISTS:bitcoin_zmq>
293294
leveldb
294295
minisketch
295296
univalue
296297
Boost::headers
297-
$<TARGET_NAME_IF_EXISTS:libevent::libevent>
298+
libevent::core
299+
libevent::extra
298300
$<TARGET_NAME_IF_EXISTS:libevent::pthreads>
299-
$<TARGET_NAME_IF_EXISTS:bitcoin_zmq>
300301
$<TARGET_NAME_IF_EXISTS:USDT::headers>
301302
)
302303

@@ -366,7 +367,8 @@ if(BUILD_CLI)
366367
bitcoin_cli
367368
bitcoin_common
368369
bitcoin_util
369-
$<TARGET_NAME_IF_EXISTS:libevent::libevent>
370+
libevent::core
371+
libevent::extra
370372
)
371373
list(APPEND installable_targets bitcoin-cli)
372374
endif()

src/test/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ target_link_libraries(test_bitcoin
153153
minisketch
154154
secp256k1
155155
Boost::headers
156-
$<TARGET_NAME_IF_EXISTS:libevent::libevent>
156+
libevent::extra
157157
)
158158

159159
if(ENABLE_WALLET)

src/test/fuzz/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ target_link_libraries(fuzz
140140
univalue
141141
secp256k1
142142
Boost::headers
143-
$<TARGET_NAME_IF_EXISTS:libevent::libevent>
143+
libevent::extra
144144
)
145145

146146
if(ENABLE_WALLET)

0 commit comments

Comments
 (0)