FindPCAP.cmake 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # - Try to find libpcap include dirs and libraries
  2. #
  3. # Usage of this module as follows:
  4. #
  5. # find_package(PCAP)
  6. #
  7. # Variables used by this module, they can change the default behaviour and need
  8. # to be set before calling find_package:
  9. #
  10. # PCAP_ROOT_DIR Set this variable to the root installation of
  11. # libpcap if the module has problems finding the
  12. # proper installation path.
  13. #
  14. # Variables defined by this module:
  15. #
  16. # PCAP_FOUND System has libpcap, include and library dirs found
  17. # PCAP_INCLUDE_DIR The libpcap include directories.
  18. # PCAP_LIBRARY The libpcap library (possibly includes a thread
  19. # library e.g. required by pf_ring's libpcap)
  20. # HAVE_PF_RING If a found version of libpcap supports PF_RING
  21. # HAVE_PCAP_IMMEDIATE_MODE If the version of libpcap found supports immediate mode
  22. find_path(PCAP_ROOT_DIR
  23. NAMES include/pcap.h
  24. )
  25. find_path(PCAP_INCLUDE_DIR
  26. NAMES pcap.h
  27. HINTS ${PCAP_ROOT_DIR}/include
  28. )
  29. set (HINT_DIR ${PCAP_ROOT_DIR}/Lib)
  30. # On x64 windows, we should look also for the .lib at /lib/x64/
  31. # as this is the default path for the WinPcap developer's pack
  32. if (${CMAKE_SIZEOF_VOID_P} EQUAL 8 AND WIN32)
  33. set (HINT_DIR ${PCAP_ROOT_DIR}/Lib/x64 ${HINT_DIR})
  34. endif ()
  35. find_library(PCAP_LIBRARY
  36. NAMES pcap wpcap
  37. HINTS ${HINT_DIR}
  38. NO_DEFAULT_PATH
  39. )
  40. include(FindPackageHandleStandardArgs)
  41. find_package_handle_standard_args(PCAP DEFAULT_MSG
  42. PCAP_LIBRARY
  43. PCAP_INCLUDE_DIR
  44. )
  45. include(CheckCXXSourceCompiles)
  46. set(CMAKE_REQUIRED_LIBRARIES ${PCAP_LIBRARY})
  47. check_cxx_source_compiles("int main() { return 0; }" PCAP_LINKS_SOLO)
  48. set(CMAKE_REQUIRED_LIBRARIES)
  49. # check if linking against libpcap also needs to link against a thread library
  50. if (NOT PCAP_LINKS_SOLO)
  51. find_package(Threads)
  52. if (THREADS_FOUND)
  53. set(CMAKE_REQUIRED_LIBRARIES ${PCAP_LIBRARY} ${CMAKE_THREAD_LIBS_INIT})
  54. check_cxx_source_compiles("int main() { return 0; }" PCAP_NEEDS_THREADS)
  55. set(CMAKE_REQUIRED_LIBRARIES)
  56. endif (THREADS_FOUND)
  57. if (THREADS_FOUND AND PCAP_NEEDS_THREADS)
  58. set(_tmp ${PCAP_LIBRARY} ${CMAKE_THREAD_LIBS_INIT})
  59. list(REMOVE_DUPLICATES _tmp)
  60. set(PCAP_LIBRARY ${_tmp}
  61. CACHE STRING "Libraries needed to link against libpcap" FORCE)
  62. else (THREADS_FOUND AND PCAP_NEEDS_THREADS)
  63. message(FATAL_ERROR "Couldn't determine how to link against libpcap")
  64. endif (THREADS_FOUND AND PCAP_NEEDS_THREADS)
  65. endif (NOT PCAP_LINKS_SOLO)
  66. include(CheckFunctionExists)
  67. set(CMAKE_REQUIRED_LIBRARIES ${PCAP_LIBRARY})
  68. check_function_exists(pcap_get_pfring_id HAVE_PF_RING)
  69. check_function_exists(pcap_set_immediate_mode HAVE_PCAP_IMMEDIATE_MODE)
  70. check_function_exists(pcap_set_tstamp_precision HAVE_PCAP_TIMESTAMP_PRECISION)
  71. set(CMAKE_REQUIRED_LIBRARIES)
  72. mark_as_advanced(
  73. PCAP_ROOT_DIR
  74. PCAP_INCLUDE_DIR
  75. PCAP_LIBRARY
  76. )