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 this is macOS or some other Darwin-based OS, check whether
90 # it's the system-supplied one.
92 if(APPLE AND "${PCAP_CONFIG}" STREQUAL /usr/bin/pcap-config)
94 # It is - remember that, so that if it provides -I/usr/local/include
95 # with --cflags, or -L/usr/local/lib with --libs, we ignore it;
96 # the macOS pcap-config does that even though the headers aren't
97 # under /usr/local/include and the library isn't in /usr/local/lib.
99 set(_broken_apple_pcap_config TRUE)
103 # Now get the include directories.
105 execute_process(COMMAND "${PCAP_CONFIG}" "--cflags"
106 RESULT_VARIABLE PCAP_CONFIG_RESULT
107 OUTPUT_VARIABLE PCAP_CONFIG_OUTPUT
108 OUTPUT_STRIP_TRAILING_WHITESPACE
110 if(NOT PCAP_CONFIG_RESULT EQUAL 0)
111 message(FATAL_ERROR "pcap-config --cflags failed")
113 separate_arguments(CFLAGS_LIST UNIX_COMMAND ${PCAP_CONFIG_OUTPUT})
114 set(CONFIG_PCAP_INCLUDE_DIRS "")
115 foreach(_arg IN LISTS CFLAGS_LIST)
116 if(_arg MATCHES "^-I")
118 # Extract the directory by removing the -I.
120 string(REGEX REPLACE "-I" "" _dir ${_arg})
122 # Work around macOS (and probably other Darwin) brokenness,
123 # by not adding /usr/local/include if it's from the broken
126 if(NOT _broken_apple_pcap_config OR
127 NOT "${_dir}" STREQUAL /usr/local/include)
128 # Add it to CONFIG_PCAP_INCLUDE_DIRS
129 list(APPEND CONFIG_PCAP_INCLUDE_DIRS ${_dir})
135 # Now, get the library directories and libraries for dynamic linking.
137 execute_process(COMMAND "${PCAP_CONFIG}" "--libs"
138 RESULT_VARIABLE PCAP_CONFIG_RESULT
139 OUTPUT_VARIABLE PCAP_CONFIG_OUTPUT
140 OUTPUT_STRIP_TRAILING_WHITESPACE
142 if(NOT PCAP_CONFIG_RESULT EQUAL 0)
143 message(FATAL_ERROR "pcap-config --libs failed")
145 separate_arguments(LIBS_LIST UNIX_COMMAND ${PCAP_CONFIG_OUTPUT})
146 set(CONFIG_PCAP_LIBRARY_DIRS "")
147 set(CONFIG_PCAP_LIBRARIES "")
148 foreach(_arg IN LISTS LIBS_LIST)
149 if(_arg MATCHES "^-L")
151 # Extract the directory by removing the -L.
153 string(REGEX REPLACE "-L" "" _dir ${_arg})
155 # Work around macOS (and probably other Darwin) brokenness,
156 # by not adding /usr/local/lib if it's from the broken
159 if(NOT _broken_apple_pcap_config OR
160 NOT "${_dir}" STREQUAL /usr/local/lib)
161 # Add this directory to CONFIG_PCAP_LIBRARY_DIRS
162 list(APPEND CONFIG_PCAP_LIBRARY_DIRS ${_dir})
164 elseif(_arg MATCHES "^-l")
165 string(REGEX REPLACE "-l" "" _lib ${_arg})
166 list(APPEND CONFIG_PCAP_LIBRARIES ${_lib})
171 # Now, get the library directories and libraries for static linking.
173 execute_process(COMMAND "${PCAP_CONFIG}" "--libs" "--static"
174 RESULT_VARIABLE PCAP_CONFIG_RESULT
175 OUTPUT_VARIABLE PCAP_CONFIG_OUTPUT
177 if(NOT PCAP_CONFIG_RESULT EQUAL 0)
178 message(FATAL_ERROR "pcap-config --libs --static failed")
180 separate_arguments(LIBS_LIST UNIX_COMMAND ${PCAP_CONFIG_OUTPUT})
181 set(CONFIG_PCAP_STATIC_LIBRARY_DIRS "")
182 set(CONFIG_PCAP_STATIC_LIBRARIES "")
183 foreach(_arg IN LISTS LIBS_LIST)
184 if(_arg MATCHES "^-L")
186 # Extract the directory by removing the -L.
188 string(REGEX REPLACE "-L" "" _dir ${_arg})
190 # Work around macOS (and probably other Darwin) brokenness,
191 # by not adding /usr/local/lib if it's from the broken
194 if(NOT _broken_apple_pcap_config OR
195 NOT "${_dir}" STREQUAL /usr/local/lib)
196 # Add this directory to CONFIG_PCAP_STATIC_LIBRARY_DIRS
197 list(APPEND CONFIG_PCAP_STATIC_LIBRARY_DIRS ${_dir})
199 elseif(_arg MATCHES "^-l")
200 string(REGEX REPLACE "-l" "" _lib ${_arg})
202 # Try to find that library, so we get its full path, as
203 # we do with dynamic libraries.
205 list(APPEND CONFIG_PCAP_STATIC_LIBRARIES ${_lib})
210 # We've set CONFIG_PCAP_INCLUDE_DIRS, CONFIG_PCAP_LIBRARIES, and
211 # CONFIG_PCAP_STATIC_LIBRARIES above; set CONFIG_PCAP_FOUND.
213 set(CONFIG_PCAP_FOUND YES)
218 # If CONFIG_PCAP_FOUND is set, we have information from pkg-config and
219 # pcap-config; we need to convert library names to library full paths.
221 # If it's not set, we have to look for the libpcap headers and library
224 if(CONFIG_PCAP_FOUND)
226 # Use CONFIG_PCAP_INCLUDE_DIRS as the value for PCAP_INCLUDE_DIRS.
228 set(PCAP_INCLUDE_DIRS "${CONFIG_PCAP_INCLUDE_DIRS}")
231 # CMake *really* doesn't like the notion of specifying
232 # "here are the directories in which to look for libraries"
233 # except in find_library() calls; it *really* prefers using
234 # full paths to library files, rather than library names.
236 foreach(_lib IN LISTS CONFIG_PCAP_LIBRARIES)
237 find_library(_libfullpath ${_lib} HINTS ${CONFIG_PCAP_LIBRARY_DIRS})
238 list(APPEND PCAP_LIBRARIES ${_libfullpath})
240 # Remove that from the cache; we're using it as a local variable,
241 # but find_library insists on making it a cache variable.
243 unset(_libfullpath CACHE)
247 # Now do the same for the static libraries.
249 set(SAVED_CMAKE_FIND_LIBRARY_SUFFIXES "${CMAKE_FIND_LIBRARY_SUFFIXES}")
250 set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
251 foreach(_lib IN LISTS CONFIG_PCAP_STATIC_LIBRARIES)
252 find_library(_libfullpath ${_lib} HINTS ${CONFIG_PCAP_LIBRARY_DIRS})
253 list(APPEND PCAP_STATIC_LIBRARIES ${_libfullpath})
255 # Remove that from the cache; we're using it as a local variable,
256 # but find_library insists on making it a cache variable.
258 unset(_libfullpath CACHE)
260 set(CMAKE_FIND_LIBRARY_SUFFIXES "${SAVED_CMAKE_FIND_LIBRARY_SUFFIXES}")
263 # We found libpcap using pkg-config or pcap-config.
266 else(CONFIG_PCAP_FOUND)
268 # We didn't have pkg-config, or we did but it didn't have .pc files
269 # for libpcap, and we don't have pkg-config, so we have to look for
270 # the headers and libraries ourself.
272 # We don't directly set PCAP_INCLUDE_DIRS or PCAP_LIBRARIES, as
273 # they're not supposed to be cache entries, and find_path() and
274 # find_library() set cache entries.
276 # Try to find the header file.
278 find_path(PCAP_INCLUDE_DIR pcap.h)
281 # Try to find the library
283 find_library(PCAP_LIBRARY pcap)
285 # Try to find the static library (XXX - what about AIX?)
286 set(SAVED_CMAKE_FIND_LIBRARY_SUFFIXES "${CMAKE_FIND_LIBRARY_SUFFIXES}")
287 set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
288 find_library(PCAP_STATIC_LIBRARY pcap)
289 set(CMAKE_FIND_LIBRARY_SUFFIXES "${SAVED_CMAKE_FIND_LIBRARY_SUFFIXES}")
292 # This will fail if REQUIRED is set and PCAP_INCLUDE_DIR or
293 # PCAP_LIBRARY aren't set.
295 include(FindPackageHandleStandardArgs)
296 find_package_handle_standard_args(PCAP
309 set(PCAP_INCLUDE_DIRS ${PCAP_INCLUDE_DIR})
310 set(PCAP_LIBRARIES ${PCAP_LIBRARY})
311 set(PCAP_STATIC_LIBRARIES ${PCAP_STATIC_LIBRARY})
313 endif(CONFIG_PCAP_FOUND)