• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# - Try to find libevent
2#.rst
3# FindLibevent
4# ------------
5#
6# Find Libevent include directories and libraries. Invoke as::
7#
8#   find_package(Libevent
9#     [version] [EXACT]   # Minimum or exact version
10#     [REQUIRED]          # Fail if Libevent is not found
11#     [COMPONENT <C>...]) # Libraries to look for
12#
13# Valid components are one or more of:: libevent core extra pthreads openssl.
14# Note that 'libevent' contains both core and extra. You must specify one of
15# them for the other components.
16#
17# This module will define the following variables::
18#
19#  LIBEVENT_FOUND        - True if headers and requested libraries were found
20#  LIBEVENT_INCLUDE_DIRS - Libevent include directories
21#  LIBEVENT_LIBRARIES    - Libevent libraries to be linked
22#  LIBEVENT_<C>_FOUND    - Component <C> was found (<C> is uppercase)
23#  LIBEVENT_<C>_LIBRARY  - Library to be linked for Libevent component <C>.
24
25find_package(PkgConfig QUIET)
26pkg_check_modules(PC_LIBEVENT QUIET libevent)
27
28# Look for the Libevent 2.0 or 1.4 headers
29find_path(LIBEVENT_INCLUDE_DIR
30  NAMES
31    event2/event-config.h
32    event-config.h
33  HINTS
34    ${PC_LIBEVENT_INCLUDE_DIRS}
35)
36
37if(LIBEVENT_INCLUDE_DIR)
38  set(_version_regex "^#define[ \t]+_EVENT_VERSION[ \t]+\"([^\"]+)\".*")
39  if(EXISTS "${LIBEVENT_INCLUDE_DIR}/event2/event-config.h")
40    # Libevent 2.0
41    file(STRINGS "${LIBEVENT_INCLUDE_DIR}/event2/event-config.h"
42      LIBEVENT_VERSION REGEX "${_version_regex}")
43    if("${LIBEVENT_VERSION}" STREQUAL "")
44      set(LIBEVENT_VERSION ${PC_LIBEVENT_VERSION})
45    endif()
46  else()
47    # Libevent 1.4
48    file(STRINGS "${LIBEVENT_INCLUDE_DIR}/event-config.h"
49      LIBEVENT_VERSION REGEX "${_version_regex}")
50  endif()
51  string(REGEX REPLACE "${_version_regex}" "\\1"
52    LIBEVENT_VERSION "${LIBEVENT_VERSION}")
53  unset(_version_regex)
54endif()
55
56set(_LIBEVENT_REQUIRED_VARS)
57foreach(COMPONENT ${Libevent_FIND_COMPONENTS})
58  set(_LIBEVENT_LIBNAME libevent)
59  # Note: compare two variables to avoid a CMP0054 policy warning
60  if(COMPONENT STREQUAL _LIBEVENT_LIBNAME)
61    set(_LIBEVENT_LIBNAME event)
62  else()
63    set(_LIBEVENT_LIBNAME "event_${COMPONENT}")
64  endif()
65  string(TOUPPER "${COMPONENT}" COMPONENT_UPPER)
66  find_library(LIBEVENT_${COMPONENT_UPPER}_LIBRARY
67    NAMES ${_LIBEVENT_LIBNAME}
68    HINTS ${PC_LIBEVENT_LIBRARY_DIRS}
69  )
70  if(LIBEVENT_${COMPONENT_UPPER}_LIBRARY)
71    set(Libevent_${COMPONENT}_FOUND 1)
72  endif()
73  list(APPEND _LIBEVENT_REQUIRED_VARS LIBEVENT_${COMPONENT_UPPER}_LIBRARY)
74endforeach()
75unset(_LIBEVENT_LIBNAME)
76
77include(FindPackageHandleStandardArgs)
78# handle the QUIETLY and REQUIRED arguments and set LIBEVENT_FOUND to TRUE
79# if all listed variables are TRUE and the requested version matches.
80find_package_handle_standard_args(Libevent REQUIRED_VARS
81                                  ${_LIBEVENT_REQUIRED_VARS}
82                                  LIBEVENT_INCLUDE_DIR
83                                  VERSION_VAR LIBEVENT_VERSION
84                                  HANDLE_COMPONENTS)
85
86if(LIBEVENT_FOUND)
87  set(LIBEVENT_INCLUDE_DIRS  ${LIBEVENT_INCLUDE_DIR})
88  set(LIBEVENT_LIBRARIES)
89  foreach(COMPONENT ${Libevent_FIND_COMPONENTS})
90    string(TOUPPER "${COMPONENT}" COMPONENT_UPPER)
91    list(APPEND LIBEVENT_LIBRARIES ${LIBEVENT_${COMPONENT_UPPER}_LIBRARY})
92    set(LIBEVENT_${COMPONENT_UPPER}_FOUND ${Libevent_${COMPONENT}_FOUND})
93  endforeach()
94endif()
95
96mark_as_advanced(LIBEVENT_INCLUDE_DIR ${_LIBEVENT_REQUIRED_VARS})
97unset(_LIBEVENT_REQUIRED_VARS)
98