Commit 9a29672d authored by Wez Furlong's avatar Wez Furlong Committed by Facebook Github Bot

folly:wangle: fixup libevent detection and export

Summary:
I finally got to the bottom of the weird `-levent` or `event.lib`
linker errors that crop up in the cmake builds.

The issue is that if you have installed a recent libevent it deploys
a cmake CONFIG package that exports just a bare `event` target as the
dep on libevent.  This is unfortunate because it is interpreted as
meaning `-levent` with no library path, which for an installation
of libevent that is not in the default path will result in linker
errors in libraries downstream of folly.

To resolve this, I've given the common `FindLibEvent.cmake` file
(that was derived from the folly library of the same name) a similar
treatment to the recent changes to find glog and taught it how to
locate libevent from a config package and to fall back on a basic
include/library check.

If we find an `event` target then we extract the actual library
path from it and export that.

I've removed folly's and wangle's own FindLibEvent.cmake so that it will pick
up the common library which gets shipit sync'd out to
https://github.com/facebook/folly/blob/master/CMake/FindLibEvent.cmake
and https://github.com/facebook/wangle/blob/master/build/fbcode_builder/CMake/FindLibEvent.cmake

Reviewed By: yfeldblum

Differential Revision: D14702577

fbshipit-source-id: d35d9f741e009dcd77976c0637ba3024a8a4aef3
parent fc3937b5
# - Find LibEvent (a cross event library)
# This module defines
# LIBEVENT_INCLUDE_DIR, where to find LibEvent headers
# LIBEVENT_LIB, LibEvent libraries
# LibEvent_FOUND, If false, do not try to use libevent
set(LibEvent_EXTRA_PREFIXES /usr/local /opt/local "$ENV{HOME}")
foreach(prefix ${LibEvent_EXTRA_PREFIXES})
list(APPEND LibEvent_INCLUDE_PATHS "${prefix}/include")
list(APPEND LibEvent_LIB_PATHS "${prefix}/lib")
endforeach()
find_path(LIBEVENT_INCLUDE_DIR event.h PATHS ${LibEvent_INCLUDE_PATHS})
find_library(LIBEVENT_LIB NAMES event PATHS ${LibEvent_LIB_PATHS})
if (LIBEVENT_LIB AND LIBEVENT_INCLUDE_DIR)
set(LibEvent_FOUND TRUE)
set(LIBEVENT_LIB ${LIBEVENT_LIB})
else ()
set(LibEvent_FOUND FALSE)
endif ()
if (LibEvent_FOUND)
if (NOT LibEvent_FIND_QUIETLY)
message(STATUS "Found libevent: ${LIBEVENT_LIB}")
endif ()
else ()
if (LibEvent_FIND_REQUIRED)
message(FATAL_ERROR "Could NOT find libevent.")
endif ()
message(STATUS "libevent NOT found.")
endif ()
mark_as_advanced(
LIBEVENT_LIB
LIBEVENT_INCLUDE_DIR
)
...@@ -36,25 +36,9 @@ set(FOLLY_HAVE_LIBGLOG ${GLOG_FOUND}) ...@@ -36,25 +36,9 @@ set(FOLLY_HAVE_LIBGLOG ${GLOG_FOUND})
list(APPEND FOLLY_LINK_LIBRARIES ${GLOG_LIBRARY}) list(APPEND FOLLY_LINK_LIBRARIES ${GLOG_LIBRARY})
list(APPEND FOLLY_INCLUDE_DIRECTORIES ${GLOG_INCLUDE_DIR}) list(APPEND FOLLY_INCLUDE_DIRECTORIES ${GLOG_INCLUDE_DIR})
find_package(Libevent CONFIG QUIET) find_package(LibEvent MODULE REQUIRED)
if(TARGET event) list(APPEND FOLLY_LINK_LIBRARIES ${LIBEVENT_LIB})
message(STATUS "Found libevent from package config inc=${LIBEVENT_INCLUDE_DIRS}") list(APPEND FOLLY_INCLUDE_DIRECTORIES ${LIBEVENT_INCLUDE_DIR})
list(APPEND FOLLY_SHINY_DEPENDENCIES event)
# Somewhat gross, but some vcpkg installed libevents have a relative
# `include` path exported into LIBEVENT_INCLUDE_DIRS, which triggers
# a cmake error because it resolves to the `include` dir within the
# folly repo, which is not something cmake allows to be in the
# INTERFACE_INCLUDE_DIRECTORIES. Thankfully on such a system the
# actual include directory is already part of the global include
# directories, so we can just skip it.
if (NOT "${LIBEVENT_INCLUDE_DIRS}" STREQUAL "include")
list(APPEND FOLLY_INCLUDE_DIRECTORIES ${LIBEVENT_INCLUDE_DIRS})
endif()
else()
find_package(LibEvent MODULE REQUIRED)
list(APPEND FOLLY_LINK_LIBRARIES ${LIBEVENT_LIB})
list(APPEND FOLLY_INCLUDE_DIRECTORIES ${LIBEVENT_INCLUDE_DIR})
endif()
find_package(OpenSSL MODULE REQUIRED) find_package(OpenSSL MODULE REQUIRED)
list(APPEND FOLLY_LINK_LIBRARIES ${OPENSSL_LIBRARIES}) list(APPEND FOLLY_LINK_LIBRARIES ${OPENSSL_LIBRARIES})
......
...@@ -11,28 +11,61 @@ foreach(prefix ${LibEvent_EXTRA_PREFIXES}) ...@@ -11,28 +11,61 @@ foreach(prefix ${LibEvent_EXTRA_PREFIXES})
list(APPEND LibEvent_LIB_PATHS "${prefix}/lib") list(APPEND LibEvent_LIB_PATHS "${prefix}/lib")
endforeach() endforeach()
find_path(LIBEVENT_INCLUDE_DIR event.h PATHS ${LibEvent_INCLUDE_PATHS}) find_package(Libevent CONFIG QUIET)
find_library(LIBEVENT_LIB NAMES event PATHS ${LibEvent_LIB_PATHS}) if (TARGET event)
# Re-export the config under our own names
if (LIBEVENT_LIB AND LIBEVENT_INCLUDE_DIR) # Somewhat gross, but some vcpkg installed libevents have a relative
set(LibEvent_FOUND TRUE) # `include` path exported into LIBEVENT_INCLUDE_DIRS, which triggers
set(LIBEVENT_LIB ${LIBEVENT_LIB}) # a cmake error because it resolves to the `include` dir within the
else () # folly repo, which is not something cmake allows to be in the
set(LibEvent_FOUND FALSE) # INTERFACE_INCLUDE_DIRECTORIES. Thankfully on such a system the
endif () # actual include directory is already part of the global include
# directories, so we can just skip it.
if (NOT "${LIBEVENT_INCLUDE_DIRS}" STREQUAL "include")
set(LIBEVENT_INCLUDE_DIR ${LIBEVENT_INCLUDE_DIRS})
else()
set(LIBEVENT_INCLUDE_DIR)
endif()
if (LibEvent_FOUND) # Unfortunately, with a bare target name `event`, downstream consumers
# of the package that depends on `Libevent` located via CONFIG end
# up exporting just a bare `event` in their libraries. This is problematic
# because this in interpreted as just `-levent` with no library path.
# When libevent is not installed in the default installation prefix
# this results in linker errors.
# To resolve this, we ask cmake to lookup the full path to the library
# and use that instead.
get_target_property(LIBEVENT_LIB event LOCATION)
set(LibEvent_FOUND ${Libevent_FOUND})
if (NOT LibEvent_FIND_QUIETLY) if (NOT LibEvent_FIND_QUIETLY)
message(STATUS "Found libevent: ${LIBEVENT_LIB}") message(STATUS "Found libevent from package config include=${LIBEVENT_INCLUDE_DIRS} lib=${LIBEVENT_LIB}")
endif()
else()
find_path(LIBEVENT_INCLUDE_DIR event.h PATHS ${LibEvent_INCLUDE_PATHS})
find_library(LIBEVENT_LIB NAMES event PATHS ${LibEvent_LIB_PATHS})
if (LIBEVENT_LIB AND LIBEVENT_INCLUDE_DIR)
set(LibEvent_FOUND TRUE)
set(LIBEVENT_LIB ${LIBEVENT_LIB})
else ()
set(LibEvent_FOUND FALSE)
endif () endif ()
else ()
if (LibEvent_FIND_REQUIRED) if (LibEvent_FOUND)
message(FATAL_ERROR "Could NOT find libevent.") if (NOT LibEvent_FIND_QUIETLY)
message(STATUS "Found libevent: ${LIBEVENT_LIB}")
endif ()
else ()
if (LibEvent_FIND_REQUIRED)
message(FATAL_ERROR "Could NOT find libevent.")
endif ()
message(STATUS "libevent NOT found.")
endif () endif ()
message(STATUS "libevent NOT found.")
endif ()
mark_as_advanced( mark_as_advanced(
LIBEVENT_LIB LIBEVENT_LIB
LIBEVENT_INCLUDE_DIR LIBEVENT_INCLUDE_DIR
) )
endif()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment