FindLibevent.cmake raw
1 # Copyright (c) 2024-present The Limenka developers
2 # Distributed under the MIT software license, see the accompanying
3 # file COPYING or https://opensource.org/license/mit/.
4
5 #[=======================================================================[
6 FindLibevent
7 ------------
8
9 Finds the Libevent headers and libraries.
10
11 This is a wrapper around find_package()/pkg_check_modules() commands that:
12 - facilitates searching in various build environments
13 - prints a standard log message
14
15 #]=======================================================================]
16
17 # Check whether evhttp_connection_get_peer expects const char**.
18 # See https://github.com/libevent/libevent/commit/a18301a2bb160ff7c3ffaf5b7653c39ffe27b385
19 function(check_evhttp_connection_get_peer target)
20 include(CMakePushCheckState)
21 cmake_push_check_state(RESET)
22 set(CMAKE_REQUIRED_LIBRARIES ${target})
23 include(CheckCXXSourceCompiles)
24 check_cxx_source_compiles("
25 #include <cstdint>
26 #include <event2/http.h>
27
28 int main()
29 {
30 evhttp_connection* conn = (evhttp_connection*)1;
31 const char* host;
32 uint16_t port;
33 evhttp_connection_get_peer(conn, &host, &port);
34 }
35 " HAVE_EVHTTP_CONNECTION_GET_PEER_CONST_CHAR
36 )
37 cmake_pop_check_state()
38 target_compile_definitions(${target} INTERFACE
39 $<$<BOOL:${HAVE_EVHTTP_CONNECTION_GET_PEER_CONST_CHAR}>:HAVE_EVHTTP_CONNECTION_GET_PEER_CONST_CHAR>
40 )
41 endfunction()
42
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 )
51
52 include(FindPackageHandleStandardArgs)
53 if(Libevent_FOUND)
54 find_package(Libevent ${Libevent_FIND_VERSION} QUIET
55 REQUIRED COMPONENTS ${_libevent_components}
56 NO_MODULE
57 )
58 find_package_handle_standard_args(Libevent
59 REQUIRED_VARS Libevent_DIR
60 VERSION_VAR Libevent_VERSION
61 )
62 check_evhttp_connection_get_peer(libevent::extra)
63 else()
64 find_package(PkgConfig REQUIRED)
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}
70 )
71 if(TARGET PkgConfig::libevent_${component} AND NOT TARGET libevent::${component})
72 add_library(libevent::${component} ALIAS PkgConfig::libevent_${component})
73 endif()
74 endforeach()
75 find_package_handle_standard_args(Libevent
76 REQUIRED_VARS libevent_core_LIBRARY_DIRS
77 VERSION_VAR libevent_core_VERSION
78 )
79 check_evhttp_connection_get_peer(PkgConfig::libevent_extra)
80 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)
87