FindLibevent.cmake 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # Author: Kang Lin ([email protected])
  2. # Try to find Libevent
  3. # Once done, this will define
  4. #
  5. # Libevent_FOUND - system has Libevent
  6. # Libevent_INCLUDE_DIRS - Libevent include directories
  7. # Libevent_LIBRARIES - libraries needed to use Libevent
  8. #
  9. # and the following imported targets
  10. #
  11. # Libevent::core - the core functons of Libevent
  12. # Libevent::extra - extra functions, contains http, dns and rpc
  13. # Libevent::pthreads - multiple threads for Libevent, not exists on Windows
  14. # Libevent::openssl - openssl support for Libevent
  15. macro(no_component_msg _comp)
  16. if(${CMAKE_FIND_PACKAGE_NAME}_FIND_REQUIRED_${_comp})
  17. set(pthreadlib)
  18. if(NOT WIN32)
  19. set(pthreadlib ", pthreads")
  20. endif()
  21. message(FATAL_ERROR "Your libevent library does not contain a ${_comp} component!\n"
  22. "The valid components are core, extra${pthreadlib} and openssl.")
  23. else()
  24. message_if_needed(WARNING "Your libevent library does not contain a ${_comp} component!")
  25. endif()
  26. endmacro()
  27. set(_AVAILABLE_LIBS core extra openssl pthreads)
  28. set(_EVENT_COMPONENTS)
  29. if(${CMAKE_FIND_PACKAGE_NAME}_FIND_COMPONENTS)
  30. foreach(_comp ${${CMAKE_FIND_PACKAGE_NAME}_FIND_COMPONENTS})
  31. list(FIND _AVAILABLE_LIBS ${_comp} _INDEX)
  32. if(_INDEX GREATER -1)
  33. list(APPEND _EVENT_COMPONENTS ${_comp})
  34. else()
  35. no_component_msg(${_comp})
  36. endif()
  37. endforeach()
  38. else()
  39. set(_EVENT_COMPONENTS core extra openssl)
  40. if(NOT WIN32)
  41. list(APPEND _EVENT_COMPONENTS pthreads)
  42. endif()
  43. endif()
  44. foreach(_libevent_comp ${_EVENT_COMPONENTS})
  45. list(APPEND _libevent_comps libevent_${_libevent_comp})
  46. endforeach()
  47. find_package(PkgConfig)
  48. pkg_check_modules(PC_Libevent QUIET ${_libevent_comps})
  49. if(PC_Libevent_FOUND)
  50. set(Libevent_VERSION ${PC_Libevent_VERSION})
  51. else()
  52. foreach(_libevent_comp ${_EVENT_COMPONENTS})
  53. list(APPEND PC_Libevent_LIBRARIES event_${_libevent_comp})
  54. endforeach()
  55. endif()
  56. find_path(Libevent_INCLUDE_DIR
  57. NAMES event2/event.h
  58. HINTS ${Libevent_ROOT} ${PC_Libevent_INCLUDEDIR} ${PC_Libevent_INCLUDE_DIRS} /usr
  59. PATHS $ENV{Libevent_DIR} ${Libevent_DIR}
  60. PATH_SUFFIXES include
  61. )
  62. foreach(Libevent_var ${PC_Libevent_LIBRARIES})
  63. unset(Libevent_lib CACHE)
  64. find_library(
  65. Libevent_lib
  66. NAMES
  67. ${Libevent_var}
  68. HINTS ${Libevent_ROOT} ${PC_Libevent_LIBDIR} ${PC_Libevent_LIBRARY_DIRS}
  69. PATHS $ENV{Libevent_DIR} ${Libevent_DIR}
  70. PATH_SUFFIXES ${CMAKE_INSTALL_LIBDIR} lib)
  71. if(Libevent_lib)
  72. string(REPLACE event_ "" _name ${Libevent_var})
  73. add_library(Libevent::${_name} UNKNOWN IMPORTED)
  74. set_target_properties(Libevent::${_name} PROPERTIES
  75. IMPORTED_LOCATION "${Libevent_lib}"
  76. INTERFACE_INCLUDE_DIRECTORIES "${Libevent_INCLUDE_DIR}")
  77. list(APPEND Libevent_LIBRARY ${Libevent_lib})
  78. endif()
  79. endforeach()
  80. include(FindPackageHandleStandardArgs)
  81. find_package_handle_standard_args(Libevent
  82. REQUIRED_VARS Libevent_LIBRARY Libevent_INCLUDE_DIR)
  83. mark_as_advanced(Libevent_FOUND Libevent_INCLUDE_DIR Libevent_LIBRARY Libevent_lib)
  84. set(Libevent_INCLUDE_DIRS ${Libevent_INCLUDE_DIR})
  85. set(Libevent_LIBRARIES ${Libevent_LIBRARY})
  86. unset(Libevent_INCLUDE_DIR)
  87. unset(Libevent_LIBRARY)