| 1 |
# Copyright (c) Facebook, Inc. and its affiliates. |
| 2 |
# - Find LibEvent (a cross event library) |
| 3 |
# This module defines |
| 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 |
| 7 |
|
| 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") |
| 12 |
endforeach() |
| 13 |
|
| 14 |
find_package(Libevent CONFIG QUIET) |
| 15 |
if (TARGET event) |
| 16 |
# Re-export the config under our own names |
| 17 |
|
| 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}) |
| 27 |
else() |
| 28 |
set(LIBEVENT_INCLUDE_DIR) |
| 29 |
endif() |
| 30 |
|
| 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. |
| 39 |
cmake_policy(PUSH) |
| 40 |
if(POLICY CMP0026) |
| 41 |
# Allow reading the LOCATION property |
| 42 |
cmake_policy(SET CMP0026 OLD) |
| 43 |
endif() |
| 44 |
get_target_property(LIBEVENT_LIB event LOCATION) |
| 45 |
cmake_policy(POP) |
| 46 |
|
| 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}") |
| 50 |
endif() |
| 51 |
else() |
| 52 |
find_path(LIBEVENT_INCLUDE_DIR event.h PATHS ${LibEvent_INCLUDE_PATHS}) |
| 53 |
find_library(LIBEVENT_LIB NAMES event PATHS ${LibEvent_LIB_PATHS}) |
| 54 |
|
| 55 |
if (LIBEVENT_LIB AND LIBEVENT_INCLUDE_DIR) |
| 56 |
set(LibEvent_FOUND TRUE) |
| 57 |
set(LIBEVENT_LIB ${LIBEVENT_LIB}) |
| 58 |
else () |
| 59 |
set(LibEvent_FOUND FALSE) |
| 60 |
endif () |
| 61 |
|
| 62 |
if (LibEvent_FOUND) |
| 63 |
if (NOT LibEvent_FIND_QUIETLY) |
| 64 |
message(STATUS "Found libevent: ${LIBEVENT_LIB}") |
| 65 |
endif () |
| 66 |
else () |
| 67 |
if (LibEvent_FIND_REQUIRED) |
| 68 |
message(FATAL_ERROR "Could NOT find libevent.") |
| 69 |
endif () |
| 70 |
message(STATUS "libevent NOT found.") |
| 71 |
endif () |
| 72 |
|
| 73 |
mark_as_advanced( |
| 74 |
LIBEVENT_LIB |
| 75 |
LIBEVENT_INCLUDE_DIR |
| 76 |
) |
| 77 |
endif() |