1#
2# - Try to find libpcap include dirs and libraries
3#
4# Usage of this module as follows:
5#
6#     find_package(PCAP)
7#
8# Variables defined by this module:
9#
10#  PCAP_FOUND                System has libpcap, include and library dirs found
11#  PCAP_INCLUDE_DIR          The libpcap include directories.
12#  PCAP_LIBRARIES            The libpcap library
13
14
15set(ERROR_MESSAGE
16    "
17    ERROR!  Libpcap library/headers (libpcap.a (or .so)/pcap.h)
18    not found, go get it from http://www.tcpdump.org
19    or use the --with-pcap-* options, if you have it installed
20    in unusual place.  Also check if your libpcap depends on another
21    shared library that may be installed in an unusual place"
22)
23
24# Call find_path twice.  First search custom path, then search standard paths.
25if (PCAP_INCLUDE_DIR_HINT)
26    find_path(PCAP_INCLUDE_DIR pcap.h
27        HINTS ${PCAP_INCLUDE_DIR_HINT}
28        NO_DEFAULT_PATH
29    )
30endif()
31find_path(PCAP_INCLUDE_DIR pcap.h)
32
33# Ditto for the library.
34if (PCAP_LIBRARIES_DIR_HINT)
35    find_library(PCAP_LIBRARIES
36        pcap
37        HINTS ${PCAP_LIBRARIES_DIR_HINT}
38        NO_DEFAULT_PATH
39    )
40endif()
41find_library(PCAP_LIBRARIES pcap)
42
43include(FindPackageHandleStandardArgs)
44find_package_handle_standard_args(PCAP
45    REQUIRED_VARS PCAP_LIBRARIES PCAP_INCLUDE_DIR
46    FAIL_MESSAGE ${ERROR_MESSAGE}
47)
48
49# Check if linking against libpcap also requires linking against a thread library.
50# (lifted from Bro's FindPCAP.cmake)
51include(CheckCSourceCompiles)
52set(CMAKE_REQUIRED_LIBRARIES ${PCAP_LIBRARIES})
53check_c_source_compiles("int main() { return 0; }" PCAP_LINKS_SOLO)
54set(CMAKE_REQUIRED_LIBRARIES)
55
56if (NOT PCAP_LINKS_SOLO)
57    find_package(Threads)
58    if (THREADS_FOUND)
59        set(CMAKE_REQUIRED_LIBRARIES ${PCAP_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
60        check_c_source_compiles("int main() { return 0; }" PCAP_NEEDS_THREADS)
61        set(CMAKE_REQUIRED_LIBRARIES)
62    endif ()
63    if (THREADS_FOUND AND PCAP_NEEDS_THREADS)
64        set(_tmp ${PCAP_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
65        list(REMOVE_DUPLICATES _tmp)
66        set(PCAP_LIBRARIES ${_tmp}
67            CACHE STRING "Libraries needed to link against libpcap" FORCE)
68    else ()
69        message(SEND_ERROR "Couldn't determine how to link against libpcap")
70    endif ()
71endif ()
72
73mark_as_advanced(
74    PCAP_INCLUDE_DIR
75    PCAP_LIBRARIES
76)
77