Debian Codegraph @

cmake/Findlibpcap.cmake

raw
1 # SPDX-FileCopyrightText: 1995-2017 The Regents of the University of California
2 # through the Lawrence Berkeley National Laboratory and the
3 # International Computer Science Institute. All rights reserved.
4 # SPDX-License-Identifier: BSD-3-Clause
5
6 # - Try to find libpcap include dirs and libraries
7 #
8 # Usage of this module as follows:
9 #
10 # find_package(libpcap)
11 #
12 # Variables used by this module, they can change the default behaviour and need
13 # to be set before calling find_package:
14 #
15 # PCAP_ROOT_DIR Set this variable to the root installation of
16 # libpcap if the module has problems finding the
17 # proper installation path.
18 #
19 # Variables defined by this module:
20 #
21 # PCAP_FOUND System has libpcap, include and library dirs found
22 # PCAP_INCLUDE_DIR The libpcap include directories.
23 # PCAP_LIBRARY The libpcap library (possibly includes a thread
24 # library e.g. required by pf_ring's libpcap)
25 # HAVE_PF_RING If a found version of libpcap supports PF_RING
26
27 find_path(PCAP_ROOT_DIR
28 NAMES include/pcap.h
29 )
30
31 find_path(PCAP_INCLUDE_DIR
32 NAMES pcap.h
33 HINTS ${PCAP_ROOT_DIR}/include
34 )
35
36 find_library(PCAP_LIBRARY
37 NAMES pcap
38 HINTS ${PCAP_ROOT_DIR}/lib
39 )
40
41 include(FindPackageHandleStandardArgs)
42 find_package_handle_standard_args(libpcap DEFAULT_MSG
43 PCAP_LIBRARY
44 PCAP_INCLUDE_DIR
45 )
46
47 include(CheckCSourceCompiles)
48 set(CMAKE_REQUIRED_LIBRARIES ${PCAP_LIBRARY})
49 check_c_source_compiles("int main() { return 0; }" PCAP_LINKS_SOLO)
50 set(CMAKE_REQUIRED_LIBRARIES)
51
52 # check if linking against libpcap also needs to link against a thread library
53 if (NOT PCAP_LINKS_SOLO)
54 find_package(Threads)
55 if (THREADS_FOUND)
56 set(CMAKE_REQUIRED_LIBRARIES ${PCAP_LIBRARY} ${CMAKE_THREAD_LIBS_INIT})
57 check_c_source_compiles("int main() { return 0; }" PCAP_NEEDS_THREADS)
58 set(CMAKE_REQUIRED_LIBRARIES)
59 endif ()
60 if (THREADS_FOUND AND PCAP_NEEDS_THREADS)
61 set(_tmp ${PCAP_LIBRARY} ${CMAKE_THREAD_LIBS_INIT})
62 list(REMOVE_DUPLICATES _tmp)
63 set(PCAP_LIBRARY ${_tmp}
64 CACHE STRING "Libraries needed to link against libpcap" FORCE)
65 else ()
66 message(FATAL_ERROR "Couldn't determine how to link against libpcap")
67 endif ()
68 endif ()
69
70 include(CheckFunctionExists)
71 set(CMAKE_REQUIRED_LIBRARIES ${PCAP_LIBRARY})
72 check_function_exists(pcap_get_pfring_id HAVE_PF_RING)
73 set(CMAKE_REQUIRED_LIBRARIES)
74
75 mark_as_advanced(
76 PCAP_ROOT_DIR
77 PCAP_INCLUDE_DIR
78 PCAP_LIBRARY
79 )