Skip to content

Commit 8d340be

Browse files
committed
Merge bitcoin/bitcoin#31181: cmake: Revamp FindLibevent module
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
2 parents 9a8e5ad + 5a96767 commit 8d340be

10 files changed

+45
-90
lines changed

cmake/bitcoin-build-config.h.in

-3
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,6 @@
7171
*/
7272
#cmakedefine01 HAVE_DECL_SETSID
7373

74-
/* Define this symbol if evhttp_connection_get_peer expects const char** */
75-
#cmakedefine HAVE_EVHTTP_CONNECTION_GET_PEER_CONST_CHAR 1
76-
7774
/* Define to 1 if fdatasync is available. */
7875
#cmakedefine HAVE_FDATASYNC 1
7976

cmake/module/FindLibevent.cmake

+33-27
Original file line numberDiff line numberDiff line change
@@ -35,46 +35,52 @@ function(check_evhttp_connection_get_peer target)
3535
" HAVE_EVHTTP_CONNECTION_GET_PEER_CONST_CHAR
3636
)
3737
cmake_pop_check_state()
38-
set(HAVE_EVHTTP_CONNECTION_GET_PEER_CONST_CHAR ${HAVE_EVHTTP_CONNECTION_GET_PEER_CONST_CHAR} PARENT_SCOPE)
38+
target_compile_definitions(${target} INTERFACE
39+
$<$<BOOL:${HAVE_EVHTTP_CONNECTION_GET_PEER_CONST_CHAR}>:HAVE_EVHTTP_CONNECTION_GET_PEER_CONST_CHAR>
40+
)
3941
endfunction()
4042

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

4252
include(FindPackageHandleStandardArgs)
43-
if(VCPKG_TARGET_TRIPLET)
44-
find_package(Libevent ${Libevent_FIND_VERSION} NO_MODULE QUIET
45-
COMPONENTS extra
53+
if(Libevent_FOUND)
54+
find_package(Libevent ${Libevent_FIND_VERSION} QUIET
55+
REQUIRED COMPONENTS ${_libevent_components}
56+
NO_MODULE
4657
)
4758
find_package_handle_standard_args(Libevent
4859
REQUIRED_VARS Libevent_DIR
4960
VERSION_VAR Libevent_VERSION
5061
)
5162
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)
5663
else()
5764
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}
65+
foreach(component IN LISTS _libevent_components)
66+
pkg_check_modules(libevent_${component}
67+
REQUIRED QUIET
68+
IMPORTED_TARGET GLOBAL
69+
libevent_${component}>=${Libevent_FIND_VERSION}
6770
)
68-
list(APPEND _libevent_required_vars libevent_pthreads_FOUND)
69-
endif()
71+
if(TARGET PkgConfig::libevent_${component} AND NOT TARGET libevent::${component})
72+
add_library(libevent::${component} ALIAS PkgConfig::libevent_${component})
73+
endif()
74+
endforeach()
7075
find_package_handle_standard_args(Libevent
71-
REQUIRED_VARS ${_libevent_required_vars}
72-
VERSION_VAR libevent_VERSION
76+
REQUIRED_VARS libevent_core_LIBRARY_DIRS
77+
VERSION_VAR libevent_core_VERSION
7378
)
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()
79+
check_evhttp_connection_get_peer(PkgConfig::libevent_extra)
8080
endif()
81+
82+
unset(_libevent_components)
83+
84+
mark_as_advanced(Libevent_DIR)
85+
mark_as_advanced(_event_h)
86+
mark_as_advanced(_event_lib)

depends/packages/libevent.mk

+4-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ $(package)_download_path=https://github.com/libevent/libevent/releases/download/
44
$(package)_file_name=$(package)-$($(package)_version).tar.gz
55
$(package)_sha256_hash=92e6de1be9ec176428fd2367677e61ceffc2ee1cb119035037a27d346b0403bb
66
$(package)_patches=cmake_fixups.patch
7-
$(package)_patches+=fix_mingw_link.patch
87
$(package)_build_subdir=build
98

109
# When building for Windows, we set _WIN32_WINNT to target the same Windows
@@ -23,8 +22,7 @@ define $(package)_set_vars
2322
endef
2423

2524
define $(package)_preprocess_cmds
26-
patch -p1 < $($(package)_patch_dir)/cmake_fixups.patch && \
27-
patch -p1 < $($(package)_patch_dir)/fix_mingw_link.patch
25+
patch -p1 < $($(package)_patch_dir)/cmake_fixups.patch
2826
endef
2927

3028
define $(package)_config_cmds
@@ -40,7 +38,8 @@ define $(package)_stage_cmds
4038
endef
4139

4240
define $(package)_postprocess_cmds
43-
rm -rf bin && \
41+
rm -rf bin lib/pkgconfig && \
4442
rm include/ev*.h && \
45-
rm include/event2/*_compat.h
43+
rm include/event2/*_compat.h && \
44+
rm lib/libevent.a
4645
endef
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
cmake: set minimum version to 3.5
22

3-
Fix generated pkg-config files, see
4-
https://github.com/libevent/libevent/pull/1165.
5-
63
--- a/CMakeLists.txt
74
+++ b/CMakeLists.txt
85
@@ -19,7 +19,7 @@
@@ -14,22 +11,3 @@ https://github.com/libevent/libevent/pull/1165.
1411

1512
if (POLICY CMP0054)
1613
cmake_policy(SET CMP0054 NEW)
17-
diff --git a/cmake/AddEventLibrary.cmake b/cmake/AddEventLibrary.cmake
18-
index 04f5837e..d8ea42c4 100644
19-
--- a/cmake/AddEventLibrary.cmake
20-
+++ b/cmake/AddEventLibrary.cmake
21-
@@ -20,12 +20,12 @@ macro(generate_pkgconfig LIB_NAME)
22-
23-
set(LIBS "")
24-
foreach (LIB ${LIB_PLATFORM})
25-
- set(LIBS "${LIBS} -L${LIB}")
26-
+ set(LIBS "${LIBS} -l${LIB}")
27-
endforeach()
28-
29-
set(OPENSSL_LIBS "")
30-
foreach(LIB ${OPENSSL_LIBRARIES})
31-
- set(OPENSSL_LIBS "${OPENSSL_LIBS} -L${LIB}")
32-
+ set(OPENSSL_LIBS "${OPENSSL_LIBS} -l${LIB}")
33-
endforeach()
34-
35-
configure_file("lib${LIB_NAME}.pc.in" "lib${LIB_NAME}.pc" @ONLY)

depends/patches/libevent/fix_mingw_link.patch

-25
This file was deleted.

doc/build-unix.md

+1-1
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

+5-3
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/httpserver.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

5-
#include <bitcoin-build-config.h> // IWYU pragma: keep
6-
75
#include <httpserver.h>
86

97
#include <chainparamsbase.h>

src/test/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ target_link_libraries(test_bitcoin
154154
minisketch
155155
secp256k1
156156
Boost::headers
157-
$<TARGET_NAME_IF_EXISTS:libevent::libevent>
157+
libevent::extra
158158
)
159159

160160
if(ENABLE_WALLET)

src/test/fuzz/CMakeLists.txt

+1-1
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)