5 find_program(PCAP_CONFIG pcap-config)
8 # We have pcap-config; use it.
9 # XXX - what if this is on Windows? If you're using, for example,
10 # MinGW, that might be the right thing to do, *if* pcap-config
11 # were made to work properly on Windows, but what about MSVC?
13 # First, get the include directory.
15 execute_process(COMMAND "${PCAP_CONFIG}" "--cflags"
16 RESULT_VARIABLE PCAP_CONFIG_RESULT
17 OUTPUT_VARIABLE PCAP_CONFIG_OUTPUT
18 OUTPUT_STRIP_TRAILING_WHITESPACE
20 if(NOT PCAP_CONFIG_RESULT EQUAL 0)
21 message(FATAL_ERROR "pcap-config --cflags failed")
24 # XXX - this assumes that there's only one -I flag in the output
25 # of pcap-config --cflags. That *should* be the case.
27 string(REGEX REPLACE "-I" "" _pcap_include_dir "${PCAP_CONFIG_OUTPUT}")
29 # Try to find the header
30 # We use what pcap-config provided as a hint, because the
31 # pcap-config that ships with macOS bogusly supplies
32 # -I/usr/local/include even though the header isn't
33 # there (it may be under /usr/include or it may be
34 # buried in the Xcode app bundle).
35 find_path(PCAP_INCLUDE_DIRS pcap.h HINTS ${_pcap_include_dir})
37 # Now, get the libraries.
38 execute_process(COMMAND "${PCAP_CONFIG}" "--libs"
39 RESULT_VARIABLE PCAP_CONFIG_RESULT
40 OUTPUT_VARIABLE PCAP_CONFIG_OUTPUT
41 OUTPUT_STRIP_TRAILING_WHITESPACE
43 if(NOT PCAP_CONFIG_RESULT EQUAL 0)
44 message(FATAL_ERROR "pcap-config --libs failed")
46 separate_arguments(LIBS_LIST UNIX_COMMAND ${PCAP_CONFIG_OUTPUT})
47 set(_pcap_library_dirs)
49 foreach(_arg IN LISTS LIBS_LIST)
50 if(_arg MATCHES "^-L")
51 # Add this directory to _pcap_library_dirs
52 string(REGEX REPLACE "-L" "" _dir ${_arg})
53 list(APPEND _pcap_library_dirs ${_dir})
54 elseif(_arg MATCHES "^-l")
55 string(REGEX REPLACE "-l" "" _lib ${_arg})
57 # Try to find that library, so we get its full path.
58 # CMake *really* doesn't like the notion of specifying "here are
59 # the directories in which to look for libraries" except in
60 # find_library() calls; it *really* prefers using full paths to
61 # library files, rather than library names.
63 # Furthermore, the pcap-config shipped with macOS reports
64 # -I/usr/local/include for --cflags and -L/usr/local/lib for
65 # --libs, rather than reporting the appropriate system (or
66 # Xcode application) directory.
68 find_library(_libfullpath ${_lib} HINTS ${__pcap_library_dirs})
69 list(APPEND PCAP_LIBRARIES ${_libfullpath})
73 # Now, get the library directories and libraries for static linking.
74 execute_process(COMMAND "${PCAP_CONFIG}" "--libs" "--static"
75 RESULT_VARIABLE PCAP_CONFIG_RESULT
76 OUTPUT_VARIABLE PCAP_CONFIG_OUTPUT
78 if(NOT PCAP_CONFIG_RESULT EQUAL 0)
79 message(FATAL_ERROR "pcap-config --libs --static failed")
81 separate_arguments(LIBS_LIST UNIX_COMMAND ${PCAP_CONFIG_OUTPUT})
82 set(_pcap_static_library_dirs)
83 set(PCAP_STATIC_LIBRARIES)
84 foreach(_arg IN LISTS LIBS_LIST)
85 if(_arg MATCHES "^-L")
86 # Add this directory to _pcap_static_library_dirs
87 string(REGEX REPLACE "-L" "" _dir ${_arg})
88 list(APPEND _pcap_static_library_dirs ${_dir})
89 elseif(_arg MATCHES "^-l")
90 string(REGEX REPLACE "-l" "" _lib ${_arg})
92 # Try to find that library, so we get its full path, as
93 # we do with dynamic libraries.
95 find_library(_libfullpath ${_lib} HINTS ${__pcap_static_library_dirs})
96 list(APPEND PCAP_STATIC_LIBRARIES ${_libfullpath})
101 # We don't have pcap-config.
102 # Try to find the header by just looking for it in whatever
103 # directories find_path() uses by default.
105 find_path(PCAP_INCLUDE_DIRS pcap.h)
107 # Try to find the library
109 # The 64-bit Packet.lib is located under /x64
110 if(CMAKE_SIZEOF_VOID_P EQUAL 8)
112 # For the WinPcap and Npcap SDKs, the Lib subdirectory of the top-level
113 # directory contains 32-bit libraries; the 64-bit libraries are in the
116 # The only way to *FORCE* CMake to look in the Lib/x64 directory
117 # without searching in the Lib directory first appears to be to set
118 # CMAKE_LIBRARY_ARCHITECTURE to "x64".
120 set(CMAKE_LIBRARY_ARCHITECTURE "x64")
124 find_library(PCAP_LIBRAIES pcap)
126 if(NOT PCAP_LIBRARIES)
128 # OK, look for it under the name wpcap.
130 find_library(PCAP_LIBRARIES wpcap)
131 endif(NOT PCAP_LIBRARIES)
134 # Try to find the static library (XXX - what about AIX?)
135 include(CMakePushCheckState)
136 cmake_push_check_state()
137 set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
138 find_library(PCAP_STATIC_LIBRARIES pcap)
139 cmake_pop_check_state()
143 include(FindPackageHandleStandardArgs)
144 find_package_handle_standard_args(PCAP