1 # Copyright (c) Facebook, Inc. and its affiliates.
2 # - Find LibEvent (a cross event library)
4 # LIBEVENT_INCLUDE_DIR, where to find LibEvent headers
5 # LIBEVENT_LIB, LibEvent libraries
6 # LibEvent_FOUND, If false, do not try to use libevent
8 set(LibEvent_EXTRA_PREFIXES /usr/local /opt/local "$ENV{HOME}")
9 foreach(prefix ${LibEvent_EXTRA_PREFIXES})
10 list(APPEND LibEvent_INCLUDE_PATHS "${prefix}/include")
11 list(APPEND LibEvent_LIB_PATHS "${prefix}/lib")
14 find_package(Libevent CONFIG QUIET)
16 # Re-export the config under our own names
18 # Somewhat gross, but some vcpkg installed libevents have a relative
19 # `include` path exported into LIBEVENT_INCLUDE_DIRS, which triggers
20 # a cmake error because it resolves to the `include` dir within the
21 # folly repo, which is not something cmake allows to be in the
22 # INTERFACE_INCLUDE_DIRECTORIES. Thankfully on such a system the
23 # actual include directory is already part of the global include
24 # directories, so we can just skip it.
25 if (NOT "${LIBEVENT_INCLUDE_DIRS}" STREQUAL "include")
26 set(LIBEVENT_INCLUDE_DIR ${LIBEVENT_INCLUDE_DIRS})
28 set(LIBEVENT_INCLUDE_DIR)
31 # Unfortunately, with a bare target name `event`, downstream consumers
32 # of the package that depends on `Libevent` located via CONFIG end
33 # up exporting just a bare `event` in their libraries. This is problematic
34 # because this in interpreted as just `-levent` with no library path.
35 # When libevent is not installed in the default installation prefix
36 # this results in linker errors.
37 # To resolve this, we ask cmake to lookup the full path to the library
38 # and use that instead.
41 # Allow reading the LOCATION property
42 cmake_policy(SET CMP0026 OLD)
44 get_target_property(LIBEVENT_LIB event LOCATION)
47 set(LibEvent_FOUND ${Libevent_FOUND})
48 if (NOT LibEvent_FIND_QUIETLY)
49 message(STATUS "Found libevent from package config include=${LIBEVENT_INCLUDE_DIRS} lib=${LIBEVENT_LIB}")
52 find_path(LIBEVENT_INCLUDE_DIR event.h PATHS ${LibEvent_INCLUDE_PATHS})
53 find_library(LIBEVENT_LIB NAMES event PATHS ${LibEvent_LIB_PATHS})
55 if (LIBEVENT_LIB AND LIBEVENT_INCLUDE_DIR)
56 set(LibEvent_FOUND TRUE)
57 set(LIBEVENT_LIB ${LIBEVENT_LIB})
59 set(LibEvent_FOUND FALSE)
63 if (NOT LibEvent_FIND_QUIETLY)
64 message(STATUS "Found libevent: ${LIBEVENT_LIB}")
67 if (LibEvent_FIND_REQUIRED)
68 message(FATAL_ERROR "Could NOT find libevent.")
70 message(STATUS "libevent NOT found.")