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
22find_path(PCAP_ROOT_DIR
23    NAMES include/pcap.h
24)
25
26find_path(PCAP_INCLUDE_DIR
27    NAMES pcap.h
28    HINTS ${PCAP_ROOT_DIR}/include
29)
30
31find_library(PCAP_LIBRARY
32    NAMES pcap
33    HINTS ${PCAP_ROOT_DIR}/lib
34)
35
36include(FindPackageHandleStandardArgs)
37find_package_handle_standard_args(PCAP DEFAULT_MSG
38    PCAP_LIBRARY
39    PCAP_INCLUDE_DIR
40)
41
42include(CheckCSourceCompiles)
43set(CMAKE_REQUIRED_LIBRARIES ${PCAP_LIBRARY})
44check_c_source_compiles("int main() { return 0; }" PCAP_LINKS_SOLO)
45set(CMAKE_REQUIRED_LIBRARIES)
46
47# check if linking against libpcap also needs to link against a thread library
48if (NOT PCAP_LINKS_SOLO)
49    find_package(Threads)
50    if (THREADS_FOUND)
51        set(CMAKE_REQUIRED_LIBRARIES ${PCAP_LIBRARY} ${CMAKE_THREAD_LIBS_INIT})
52        check_c_source_compiles("int main() { return 0; }" PCAP_NEEDS_THREADS)
53        set(CMAKE_REQUIRED_LIBRARIES)
54    endif ()
55    if (THREADS_FOUND AND PCAP_NEEDS_THREADS)
56        set(_tmp ${PCAP_LIBRARY} ${CMAKE_THREAD_LIBS_INIT})
57        list(REMOVE_DUPLICATES _tmp)
58        set(PCAP_LIBRARY ${_tmp}
59            CACHE STRING "Libraries needed to link against libpcap" FORCE)
60    else ()
61        message(FATAL_ERROR "Couldn't determine how to link against libpcap")
62    endif ()
63endif ()
64
65include(CheckFunctionExists)
66set(CMAKE_REQUIRED_LIBRARIES ${PCAP_LIBRARY})
67check_function_exists(pcap_get_pfring_id HAVE_PF_RING)
68check_function_exists(pcap_dump_open_append HAVE_PCAP_DUMP_OPEN_APPEND)
69set(CMAKE_REQUIRED_LIBRARIES)
70
71mark_as_advanced(
72    PCAP_ROOT_DIR
73    PCAP_INCLUDE_DIR
74    PCAP_LIBRARY
75)
76