7 # Building for Windows.
9 # libpcap isn't set up to install .pc files or pcap-config on Windows,
10 # and it's not clear that either of them would work without a lot
11 # of additional effort. WinPcap doesn't supply them, and neither
14 # So just search for them directly. Look for both pcap and wpcap.
15 # Don't bother looking for static libraries; unlike most UN*Xes
16 # (with the exception of AIX), where different extensions are used
17 # for shared and static, Windows uses .lib both for import libraries
18 # for DLLs and for static libraries.
20 # We don't directly set PCAP_INCLUDE_DIRS or PCAP_LIBRARIES, as
21 # they're not supposed to be cache entries, and find_path() and
22 # find_library() set cache entries.
24 find_path(PCAP_INCLUDE_DIR pcap.h)
26 # The 64-bit Packet.lib is located under /x64
27 if(CMAKE_SIZEOF_VOID_P EQUAL 8)
29 # For the WinPcap and Npcap SDKs, the Lib subdirectory of the top-level
30 # directory contains 32-bit libraries; the 64-bit libraries are in the
33 # The only way to *FORCE* CMake to look in the Lib/x64 directory
34 # without searching in the Lib directory first appears to be to set
35 # CMAKE_LIBRARY_ARCHITECTURE to "x64".
37 set(CMAKE_LIBRARY_ARCHITECTURE "x64")
39 find_library(PCAP_LIBRARY NAMES pcap wpcap)
42 # Do the standard arg processing, including failing if it's a
45 include(FindPackageHandleStandardArgs)
46 find_package_handle_standard_args(PCAP
56 set(PCAP_LIBRARIES ${PCAP_LIBRARY})
57 set(PCAP_INCLUDE_DIRS ${PCAP_INCLUDE_DIR})
63 # See whether we were handed a QUIET argument, so we can pass it on
64 # to pkg_search_module. Do *NOT* pass on the REQUIRED argument,
65 # because, if pkg-config isn't found, or it is but it has no .pc
66 # files for libpcap, that is *not* necessarily an indication that
67 # libpcap isn't available - not all systems ship pkg-config, and
68 # libpcap didn't have .pc files until libpcap 1.9.0.
75 # First, try pkg-config.
77 find_package(PkgConfig)
78 pkg_search_module(CONFIG_PCAP ${_quiet} libpcap)
80 if(NOT CONFIG_PCAP_FOUND)
82 # That didn't work. Try pcap-config.
84 find_program(PCAP_CONFIG pcap-config)
87 # We have pcap-config; use it.
89 if(NOT "${_quiet}" STREQUAL "QUIET")
90 message(STATUS "Found pcap-config")
94 # if this is macOS or some other Darwin-based OS, check whether
95 # it's the system-supplied one.
97 if(APPLE AND "${PCAP_CONFIG}" STREQUAL /usr/bin/pcap-config)
99 # It is - remember that, so that if it provides -I/usr/local/include
100 # with --cflags, or -L/usr/local/lib with --libs, we ignore it;
101 # the macOS pcap-config does that even though the headers aren't
102 # under /usr/local/include and the library isn't in /usr/local/lib.
104 set(_broken_apple_pcap_config TRUE)
108 # Now get the include directories.
110 execute_process(COMMAND "${PCAP_CONFIG}" "--cflags"
111 RESULT_VARIABLE PCAP_CONFIG_RESULT
112 OUTPUT_VARIABLE PCAP_CONFIG_OUTPUT
113 OUTPUT_STRIP_TRAILING_WHITESPACE
115 if(NOT PCAP_CONFIG_RESULT EQUAL 0)
116 message(FATAL_ERROR "pcap-config --cflags failed")
118 separate_arguments(CFLAGS_LIST UNIX_COMMAND ${PCAP_CONFIG_OUTPUT})
119 set(CONFIG_PCAP_INCLUDE_DIRS "")
120 foreach(_arg IN LISTS CFLAGS_LIST)
121 if(_arg MATCHES "^-I")
123 # Extract the directory by removing the -I.
125 string(REGEX REPLACE "-I" "" _dir ${_arg})
127 # Work around macOS (and probably other Darwin) brokenness,
128 # by not adding /usr/local/include if it's from the broken
131 if(NOT _broken_apple_pcap_config OR
132 NOT "${_dir}" STREQUAL /usr/local/include)
133 # Add it to CONFIG_PCAP_INCLUDE_DIRS
134 list(APPEND CONFIG_PCAP_INCLUDE_DIRS ${_dir})
140 # Now, get the library directories and libraries for dynamic linking.
142 execute_process(COMMAND "${PCAP_CONFIG}" "--libs"
143 RESULT_VARIABLE PCAP_CONFIG_RESULT
144 OUTPUT_VARIABLE PCAP_CONFIG_OUTPUT
145 OUTPUT_STRIP_TRAILING_WHITESPACE
147 if(NOT PCAP_CONFIG_RESULT EQUAL 0)
148 message(FATAL_ERROR "pcap-config --libs failed")
150 separate_arguments(LIBS_LIST UNIX_COMMAND ${PCAP_CONFIG_OUTPUT})
151 set(CONFIG_PCAP_LIBRARY_DIRS "")
152 set(CONFIG_PCAP_LIBRARIES "")
153 foreach(_arg IN LISTS LIBS_LIST)
154 if(_arg MATCHES "^-L")
156 # Extract the directory by removing the -L.
158 string(REGEX REPLACE "-L" "" _dir ${_arg})
160 # Work around macOS (and probably other Darwin) brokenness,
161 # by not adding /usr/local/lib if it's from the broken
164 if(NOT _broken_apple_pcap_config OR
165 NOT "${_dir}" STREQUAL /usr/local/lib)
166 # Add this directory to CONFIG_PCAP_LIBRARY_DIRS
167 list(APPEND CONFIG_PCAP_LIBRARY_DIRS ${_dir})
169 elseif(_arg MATCHES "^-l")
170 string(REGEX REPLACE "-l" "" _lib ${_arg})
171 list(APPEND CONFIG_PCAP_LIBRARIES ${_lib})
176 # Now, get the library directories and libraries for static linking.
178 execute_process(COMMAND "${PCAP_CONFIG}" "--libs" "--static"
179 RESULT_VARIABLE PCAP_CONFIG_RESULT
180 OUTPUT_VARIABLE PCAP_CONFIG_OUTPUT
182 if(NOT PCAP_CONFIG_RESULT EQUAL 0)
183 message(FATAL_ERROR "pcap-config --libs --static failed")
185 separate_arguments(LIBS_LIST UNIX_COMMAND ${PCAP_CONFIG_OUTPUT})
186 set(CONFIG_PCAP_STATIC_LIBRARY_DIRS "")
187 set(CONFIG_PCAP_STATIC_LIBRARIES "")
188 foreach(_arg IN LISTS LIBS_LIST)
189 if(_arg MATCHES "^-L")
191 # Extract the directory by removing the -L.
193 string(REGEX REPLACE "-L" "" _dir ${_arg})
195 # Work around macOS (and probably other Darwin) brokenness,
196 # by not adding /usr/local/lib if it's from the broken
199 if(NOT _broken_apple_pcap_config OR
200 NOT "${_dir}" STREQUAL /usr/local/lib)
201 # Add this directory to CONFIG_PCAP_STATIC_LIBRARY_DIRS
202 list(APPEND CONFIG_PCAP_STATIC_LIBRARY_DIRS ${_dir})
204 elseif(_arg MATCHES "^-l")
205 string(REGEX REPLACE "-l" "" _lib ${_arg})
207 # Try to find that library, so we get its full path, as
208 # we do with dynamic libraries.
210 list(APPEND CONFIG_PCAP_STATIC_LIBRARIES ${_lib})
215 # We've set CONFIG_PCAP_INCLUDE_DIRS, CONFIG_PCAP_LIBRARIES, and
216 # CONFIG_PCAP_STATIC_LIBRARIES above; set CONFIG_PCAP_FOUND.
218 set(CONFIG_PCAP_FOUND YES)
223 # If CONFIG_PCAP_FOUND is set, we have information from pkg-config and
224 # pcap-config; we need to convert library names to library full paths.
226 # If it's not set, we have to look for the libpcap headers and library
229 if(CONFIG_PCAP_FOUND)
231 # Use CONFIG_PCAP_INCLUDE_DIRS as the value for PCAP_INCLUDE_DIRS.
233 set(PCAP_INCLUDE_DIRS "${CONFIG_PCAP_INCLUDE_DIRS}")
236 # CMake *really* doesn't like the notion of specifying
237 # "here are the directories in which to look for libraries"
238 # except in find_library() calls; it *really* prefers using
239 # full paths to library files, rather than library names.
241 foreach(_lib IN LISTS CONFIG_PCAP_LIBRARIES)
242 find_library(_libfullpath ${_lib} HINTS ${CONFIG_PCAP_LIBRARY_DIRS})
243 list(APPEND PCAP_LIBRARIES ${_libfullpath})
245 # Remove that from the cache; we're using it as a local variable,
246 # but find_library insists on making it a cache variable.
248 unset(_libfullpath CACHE)
252 # Now do the same for the static libraries.
254 set(SAVED_CMAKE_FIND_LIBRARY_SUFFIXES "${CMAKE_FIND_LIBRARY_SUFFIXES}")
255 set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
256 foreach(_lib IN LISTS CONFIG_PCAP_STATIC_LIBRARIES)
257 find_library(_libfullpath ${_lib} HINTS ${CONFIG_PCAP_LIBRARY_DIRS})
258 list(APPEND PCAP_STATIC_LIBRARIES ${_libfullpath})
260 # Remove that from the cache; we're using it as a local variable,
261 # but find_library insists on making it a cache variable.
263 unset(_libfullpath CACHE)
265 set(CMAKE_FIND_LIBRARY_SUFFIXES "${SAVED_CMAKE_FIND_LIBRARY_SUFFIXES}")
268 # We found libpcap using pkg-config or pcap-config.
271 else(CONFIG_PCAP_FOUND)
273 # We didn't have pkg-config, or we did but it didn't have .pc files
274 # for libpcap, and we don't have pkg-config, so we have to look for
275 # the headers and libraries ourself.
277 # We don't directly set PCAP_INCLUDE_DIRS or PCAP_LIBRARIES, as
278 # they're not supposed to be cache entries, and find_path() and
279 # find_library() set cache entries.
281 # Try to find the header file.
283 find_path(PCAP_INCLUDE_DIR pcap.h)
286 # Try to find the library
288 find_library(PCAP_LIBRARY pcap)
290 # Try to find the static library (XXX - what about AIX?)
291 set(SAVED_CMAKE_FIND_LIBRARY_SUFFIXES "${CMAKE_FIND_LIBRARY_SUFFIXES}")
292 set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
293 find_library(PCAP_STATIC_LIBRARY pcap)
294 set(CMAKE_FIND_LIBRARY_SUFFIXES "${SAVED_CMAKE_FIND_LIBRARY_SUFFIXES}")
297 # This will fail if REQUIRED is set and PCAP_INCLUDE_DIR or
298 # PCAP_LIBRARY aren't set.
300 include(FindPackageHandleStandardArgs)
301 find_package_handle_standard_args(PCAP
314 set(PCAP_INCLUDE_DIRS ${PCAP_INCLUDE_DIR})
315 set(PCAP_LIBRARIES ${PCAP_LIBRARY})
316 set(PCAP_STATIC_LIBRARIES ${PCAP_STATIC_LIBRARY})
318 endif(CONFIG_PCAP_FOUND)