1# - Config file for the Libevent package
2# It defines the following variables
3#  LIBEVENT_FOUND            - true if libevent and all required components found on the system
4#  LIBEVENT_xxx_FOUND        - true if component xxx(see available components) found on the system
5#  LIBEVENT_VERSION          - libevent version in format Major.Minor.Patch
6#  LIBEVENT_INCLUDE_DIRS     - directories where libevent header is located.
7#  LIBEVENT_INCLUDE_DIR      - same as DIRS
8#  LIBEVENT_LIBRARIES        - libevent library to link against.
9#  LIBEVENT_LIBRARY          - same as LIBRARIES
10#
11# These variables are deprecated, don't use them.
12#  LIBEVENT_STATIC_LIBRARIES - libraries to link against (archive/static)
13#  LIBEVENT_SHARED_LIBRARIES - libraries to link against (shared)
14#
15# When you try to locate the libevent libraries, you should specify which components you want to use.
16# The following table lists all available components. If none is given, all imported targets will used.
17#  core        - the core functons of libevent
18#  extra       - extra functions, contains http, dns and rpc
19#  pthreads    - multiple threads for libevent, not exists on Windows
20#  openssl     - openssl support for libevent
21#
22# By default, the shared libraries of libevent will be found. To find the static ones instead,
23# you must set the LIBEVENT_STATIC_LINK variable to TRUE before calling find_package(Libevent ...).
24# If no component provided, all components will be used.
25# example:
26#  set(LIBEVENT_STATIC_LINK TRUE)
27#  find_package(Libevent 2.2 REQUIRED COMPONENTS core)
28#  include_directories(${LIBEVENT_INCLUDE_DIRS})  # Can be omitted
29#  target_link_libraries(myapp ${LIBEVENT_LIBRARIES})
30#    or target_link_libraries(myapp libevent::core)
31#
32# find_package() can handle dependencies automatically. For example, given the 'openssl' component,
33# all dependencies (libevent_core, libssl, libcrypto and openssl include directories) will be found.
34
35set(CONFIG_FOR_INSTALL_TREE @CONFIG_FOR_INSTALL_TREE@)
36
37set(LIBEVENT_VERSION @EVENT_PACKAGE_VERSION@)
38
39# IMPORTED targets from LibeventTargets.cmake
40set(LIBEVENT_STATIC_LIBRARIES "@LIBEVENT_STATIC_LIBRARIES@")
41set(LIBEVENT_SHARED_LIBRARIES "@LIBEVENT_SHARED_LIBRARIES@")
42
43# Default to the same type as libevent was built:
44if(NOT DEFINED LIBEVENT_STATIC_LINK)
45    set(LIBEVENT_STATIC_LINK NOT @EVENT_LIBRARY_SHARED@)
46endif()
47
48set(CMAKE_FIND_LIBRARY_SUFFIXES_SAVE "${CMAKE_FIND_LIBRARY_SUFFIXES}")
49if(${LIBEVENT_STATIC_LINK})
50    set(_LIB_TYPE static)
51    set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_STATIC_LIBRARY_SUFFIX})
52    set(_AVAILABLE_LIBS "${LIBEVENT_STATIC_LIBRARIES}")
53else()
54    set(_LIB_TYPE shared)
55    set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_SHARED_LIBRARY_SUFFIX})
56    set(_AVAILABLE_LIBS "${LIBEVENT_SHARED_LIBRARIES}")
57endif()
58
59# Get the path of the current file.
60get_filename_component(LIBEVENT_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
61get_filename_component(_INSTALL_PREFIX "${LIBEVENT_CMAKE_DIR}/../../.." ABSOLUTE)
62
63macro(message_if_needed _flag _msg)
64    if (NOT ${CMAKE_FIND_PACKAGE_NAME}_FIND_QUIETLY)
65        message(${_flag} "${_msg}")
66    endif()
67endmacro()
68
69macro(no_component_msg _comp)
70    if(${CMAKE_FIND_PACKAGE_NAME}_FIND_REQUIRED_${_comp})
71        set(pthreadlib)
72        if(NOT WIN32)
73            set(pthreadlib ", pthreads")
74        endif()
75        message(FATAL_ERROR "Your libevent library does not contain a ${_comp} component!\n"
76                "The valid components are core, extra${pthreadlib} and openssl.")
77    else()
78        message_if_needed(WARNING "Your libevent library does not contain a ${_comp} component!")
79    endif()
80endmacro()
81
82set(_EVENT_COMPONENTS)
83if(${CMAKE_FIND_PACKAGE_NAME}_FIND_COMPONENTS)
84    list(REMOVE_DUPLICATES ${CMAKE_FIND_PACKAGE_NAME}_FIND_COMPONENTS)
85    foreach(_comp ${${CMAKE_FIND_PACKAGE_NAME}_FIND_COMPONENTS})
86        list(FIND _AVAILABLE_LIBS ${_comp} _INDEX)
87        if(_INDEX GREATER -1)
88            list(APPEND _EVENT_COMPONENTS ${_comp})
89        else()
90            no_component_msg(${_comp})
91        endif()
92    endforeach()
93else()
94    set(_EVENT_COMPONENTS ${_AVAILABLE_LIBS})
95endif()
96
97set(_POSSIBLE_PKG_NAMES)
98list(APPEND _POSSIBLE_PKG_NAMES ${CMAKE_FIND_PACKAGE_NAME} LIBEVENT Libevent libevent)
99list(REMOVE_DUPLICATES _POSSIBLE_PKG_NAMES)
100
101macro(set_case_insensitive_found _comp)
102    foreach(name ${_POSSIBLE_PKG_NAMES})
103        if("${_comp}" STREQUAL "")
104            set(${name}_FOUND TRUE)
105            set(${name}_NOTFOUND FALSE)
106        else()
107            set(${name}_${_comp}_FOUND TRUE)
108            set(${name}_${_comp}_NOTFOUND FALSE)
109        endif()
110    endforeach()
111endmacro()
112
113if(CONFIG_FOR_INSTALL_TREE)
114    ## Config for install tree ----------------------------------------
115    # Find includes
116    unset(_event_h CACHE)
117    find_path(_event_h
118              NAMES event2/event.h
119              PATHS "${_INSTALL_PREFIX}/include"
120              NO_DEFAULT_PATH)
121    if(_event_h)
122        set(LIBEVENT_INCLUDE_DIRS "${_event_h}")
123        message_if_needed(STATUS "Found libevent include directory: ${_event_h}")
124    else()
125        message_if_needed(WARNING "Your libevent library does not contain header files!")
126    endif()
127
128    # Find libraries
129    macro(find_event_lib _comp)
130        unset(_event_lib CACHE)
131        find_library(_event_lib
132                    NAMES "event_${_comp}"
133                    PATHS "${_INSTALL_PREFIX}/lib"
134                    NO_DEFAULT_PATH)
135        if(_event_lib)
136            list(APPEND LIBEVENT_LIBRARIES "libevent::${_comp}")
137            set_case_insensitive_found(${_comp})
138            message_if_needed(STATUS "Found libevent component: ${_event_lib}")
139        else()
140            no_component_msg(${_comp})
141        endif()
142    endmacro()
143
144    foreach(comp ${_EVENT_COMPONENTS})
145        find_event_lib(${comp})
146    endforeach()
147else()
148    ## Config for build tree ----------------------------------------
149    set(LIBEVENT_INCLUDE_DIRS "@EVENT__INCLUDE_DIRS@")
150    foreach(_comp ${_EVENT_COMPONENTS})
151        list(APPEND LIBEVENT_LIBRARIES "libevent::${_comp}")
152        set_case_insensitive_found(${_comp})
153    endforeach()
154endif()
155
156set(LIBEVENT_INCLUDE_DIR ${LIBEVENT_INCLUDE_DIRS})
157if(LIBEVENT_LIBRARIES)
158    set(LIBEVENT_LIBRARY ${LIBEVENT_LIBRARIES})
159    if(CONFIG_FOR_INSTALL_TREE)
160        message_if_needed(STATUS "Found libevent ${LIBEVENT_VERSION} in ${_INSTALL_PREFIX}")
161    else()
162        message_if_needed(STATUS "Found libevent ${LIBEVENT_VERSION} in ${LIBEVENT_CMAKE_DIR}")
163    endif()
164
165    # Avoid including targets more than one times
166    if(NOT TARGET event_core_${_LIB_TYPE})
167        # Include the project Targets file, this contains definitions for IMPORTED targets.
168        include(${LIBEVENT_CMAKE_DIR}/LibeventTargets-${_LIB_TYPE}.cmake)
169    endif()
170else()
171    if(${CMAKE_FIND_PACKAGE_NAME}_FIND_REQUIRED)
172        message(FATAL_ERROR "Can not find any libraries for libevent.")
173    else()
174        message_if_needed(WARNING "Can not find any libraries for libevent.")
175    endif()
176endif()
177
178set(CMAKE_FIND_LIBRARY_SUFFIXES "${CMAKE_FIND_LIBRARY_SUFFIXES_SAVE}")
179unset(_LIB_TYPE)
180unset(_AVAILABLE_LIBS)
181unset(_EVENT_COMPONENTS)
182unset(_POSSIBLE_PKG_NAMES)
183unset(_INSTALL_PREFIX)
184