4 # To tell this module where to look, a user may set the environment variable
5 # PCAP_ROOT to point cmake to the *root* of a directory with include and
6 # lib subdirectories for pcap.dll (e.g WpdPack or npcap-sdk).
7 # Alternatively, PCAP_ROOT may also be set from cmake command line or GUI
8 # (e.g cmake -DPCAP_ROOT=C:\path\to\pcap [...])
13 # Building for Windows.
15 # libpcap isn't set up to install .pc files or pcap-config on Windows,
16 # and it's not clear that either of them would work without a lot
17 # of additional effort. WinPcap doesn't supply them, and neither
20 # So just search for them directly. Look for both pcap and wpcap.
21 # Don't bother looking for static libraries; unlike most UN*Xes
22 # (with the exception of AIX), where different extensions are used
23 # for shared and static, Windows uses .lib both for import libraries
24 # for DLLs and for static libraries.
26 # We don't directly set PCAP_INCLUDE_DIRS or PCAP_LIBRARIES, as
27 # they're not supposed to be cache entries, and find_path() and
28 # find_library() set cache entries.
30 find_path(PCAP_INCLUDE_DIR pcap.h)
32 # The 64-bit Packet.lib is located under /x64
33 if(CMAKE_SIZEOF_VOID_P EQUAL 8)
35 # For the WinPcap and Npcap SDKs, the Lib subdirectory of the top-level
36 # directory contains 32-bit libraries; the 64-bit libraries are in the
39 # The only way to *FORCE* CMake to look in the Lib/x64 directory
40 # without searching in the Lib directory first appears to be to set
41 # CMAKE_LIBRARY_ARCHITECTURE to "x64".
43 set(CMAKE_LIBRARY_ARCHITECTURE "x64")
45 find_library(PCAP_LIBRARY NAMES pcap wpcap)
48 # Do the standard arg processing, including failing if it's a
51 include(FindPackageHandleStandardArgs)
52 find_package_handle_standard_args(PCAP
62 set(PCAP_LIBRARIES ${PCAP_LIBRARY})
63 set(PCAP_INCLUDE_DIRS ${PCAP_INCLUDE_DIR})
69 # See whether we were handed a QUIET argument, so we can pass it on
70 # to pkg_search_module. Do *NOT* pass on the REQUIRED argument,
71 # because, if pkg-config isn't found, or it is but it has no .pc
72 # files for libpcap, that is *not* necessarily an indication that
73 # libpcap isn't available - not all systems ship pkg-config, and
74 # libpcap didn't have .pc files until libpcap 1.9.0.
81 # First, try pkg-config.
83 find_package(PkgConfig)
84 pkg_search_module(CONFIG_PCAP ${_quiet} libpcap)
86 if(NOT CONFIG_PCAP_FOUND)
88 # That didn't work. Try pcap-config.
90 find_program(PCAP_CONFIG pcap-config)
93 # We have pcap-config; use it.
95 if(NOT "${_quiet}" STREQUAL "QUIET")
96 message(STATUS "Found pcap-config")
100 # if this is macOS or some other Darwin-based OS, check whether
101 # it's the system-supplied one.
103 if(APPLE AND "${PCAP_CONFIG}" STREQUAL /usr/bin/pcap-config)
105 # It is - remember that, so that if it provides -I/usr/local/include
106 # with --cflags, or -L/usr/local/lib with --libs, we ignore it;
107 # the macOS pcap-config does that even though the headers aren't
108 # under /usr/local/include and the library isn't in /usr/local/lib.
110 set(_broken_apple_pcap_config TRUE)
114 # Now get the include directories.
116 execute_process(COMMAND "${PCAP_CONFIG}" "--cflags"
117 RESULT_VARIABLE PCAP_CONFIG_RESULT
118 OUTPUT_VARIABLE PCAP_CONFIG_OUTPUT
119 OUTPUT_STRIP_TRAILING_WHITESPACE
121 if(NOT PCAP_CONFIG_RESULT EQUAL 0)
122 message(FATAL_ERROR "pcap-config --cflags failed")
124 separate_arguments(CFLAGS_LIST UNIX_COMMAND ${PCAP_CONFIG_OUTPUT})
125 set(CONFIG_PCAP_INCLUDE_DIRS "")
126 foreach(_arg IN LISTS CFLAGS_LIST)
127 if(_arg MATCHES "^-I")
129 # Extract the directory by removing the -I.
131 string(REGEX REPLACE "-I" "" _dir ${_arg})
133 # Work around macOS (and probably other Darwin) brokenness,
134 # by not adding /usr/local/include if it's from the broken
137 if(NOT _broken_apple_pcap_config OR
138 NOT "${_dir}" STREQUAL /usr/local/include)
139 # Add it to CONFIG_PCAP_INCLUDE_DIRS
140 list(APPEND CONFIG_PCAP_INCLUDE_DIRS ${_dir})
146 # Now, get the library directories and libraries for dynamic linking.
148 execute_process(COMMAND "${PCAP_CONFIG}" "--libs"
149 RESULT_VARIABLE PCAP_CONFIG_RESULT
150 OUTPUT_VARIABLE PCAP_CONFIG_OUTPUT
151 OUTPUT_STRIP_TRAILING_WHITESPACE
153 if(NOT PCAP_CONFIG_RESULT EQUAL 0)
154 message(FATAL_ERROR "pcap-config --libs failed")
156 separate_arguments(LIBS_LIST UNIX_COMMAND ${PCAP_CONFIG_OUTPUT})
157 set(CONFIG_PCAP_LIBRARY_DIRS "")
158 set(CONFIG_PCAP_LIBRARIES "")
159 foreach(_arg IN LISTS LIBS_LIST)
160 if(_arg MATCHES "^-L")
162 # Extract the directory by removing the -L.
164 string(REGEX REPLACE "-L" "" _dir ${_arg})
166 # Work around macOS (and probably other Darwin) brokenness,
167 # by not adding /usr/local/lib if it's from the broken
170 if(NOT _broken_apple_pcap_config OR
171 NOT "${_dir}" STREQUAL /usr/local/lib)
172 # Add this directory to CONFIG_PCAP_LIBRARY_DIRS
173 list(APPEND CONFIG_PCAP_LIBRARY_DIRS ${_dir})
175 elseif(_arg MATCHES "^-l")
176 string(REGEX REPLACE "-l" "" _lib ${_arg})
177 list(APPEND CONFIG_PCAP_LIBRARIES ${_lib})
182 # Now, get the library directories and libraries for static linking.
184 execute_process(COMMAND "${PCAP_CONFIG}" "--libs" "--static"
185 RESULT_VARIABLE PCAP_CONFIG_RESULT
186 OUTPUT_VARIABLE PCAP_CONFIG_OUTPUT
188 if(NOT PCAP_CONFIG_RESULT EQUAL 0)
189 message(FATAL_ERROR "pcap-config --libs --static failed")
191 separate_arguments(LIBS_LIST UNIX_COMMAND ${PCAP_CONFIG_OUTPUT})
192 set(CONFIG_PCAP_STATIC_LIBRARY_DIRS "")
193 set(CONFIG_PCAP_STATIC_LIBRARIES "")
194 foreach(_arg IN LISTS LIBS_LIST)
195 if(_arg MATCHES "^-L")
197 # Extract the directory by removing the -L.
199 string(REGEX REPLACE "-L" "" _dir ${_arg})
201 # Work around macOS (and probably other Darwin) brokenness,
202 # by not adding /usr/local/lib if it's from the broken
205 if(NOT _broken_apple_pcap_config OR
206 NOT "${_dir}" STREQUAL /usr/local/lib)
207 # Add this directory to CONFIG_PCAP_STATIC_LIBRARY_DIRS
208 list(APPEND CONFIG_PCAP_STATIC_LIBRARY_DIRS ${_dir})
210 elseif(_arg MATCHES "^-l")
211 string(REGEX REPLACE "-l" "" _lib ${_arg})
213 # Try to find that library, so we get its full path, as
214 # we do with dynamic libraries.
216 list(APPEND CONFIG_PCAP_STATIC_LIBRARIES ${_lib})
221 # We've set CONFIG_PCAP_INCLUDE_DIRS, CONFIG_PCAP_LIBRARIES, and
222 # CONFIG_PCAP_STATIC_LIBRARIES above; set CONFIG_PCAP_FOUND.
224 set(CONFIG_PCAP_FOUND YES)
229 # If CONFIG_PCAP_FOUND is set, we have information from pkg-config and
230 # pcap-config; we need to convert library names to library full paths.
232 # If it's not set, we have to look for the libpcap headers and library
235 if(CONFIG_PCAP_FOUND)
237 # Use CONFIG_PCAP_INCLUDE_DIRS as the value for PCAP_INCLUDE_DIRS.
239 set(PCAP_INCLUDE_DIRS "${CONFIG_PCAP_INCLUDE_DIRS}")
242 # CMake *really* doesn't like the notion of specifying
243 # "here are the directories in which to look for libraries"
244 # except in find_library() calls; it *really* prefers using
245 # full paths to library files, rather than library names.
247 foreach(_lib IN LISTS CONFIG_PCAP_LIBRARIES)
248 find_library(_libfullpath ${_lib} HINTS ${CONFIG_PCAP_LIBRARY_DIRS})
249 list(APPEND PCAP_LIBRARIES ${_libfullpath})
251 # Remove that from the cache; we're using it as a local variable,
252 # but find_library insists on making it a cache variable.
254 unset(_libfullpath CACHE)
258 # Now do the same for the static libraries.
260 set(SAVED_CMAKE_FIND_LIBRARY_SUFFIXES "${CMAKE_FIND_LIBRARY_SUFFIXES}")
261 set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
262 foreach(_lib IN LISTS CONFIG_PCAP_STATIC_LIBRARIES)
263 find_library(_libfullpath ${_lib} HINTS ${CONFIG_PCAP_LIBRARY_DIRS})
264 list(APPEND PCAP_STATIC_LIBRARIES ${_libfullpath})
266 # Remove that from the cache; we're using it as a local variable,
267 # but find_library insists on making it a cache variable.
269 unset(_libfullpath CACHE)
271 set(CMAKE_FIND_LIBRARY_SUFFIXES "${SAVED_CMAKE_FIND_LIBRARY_SUFFIXES}")
274 # We found libpcap using pkg-config or pcap-config.
277 else(CONFIG_PCAP_FOUND)
279 # We didn't have pkg-config, or we did but it didn't have .pc files
280 # for libpcap, and we don't have pkg-config, so we have to look for
281 # the headers and libraries ourself.
283 # We don't directly set PCAP_INCLUDE_DIRS or PCAP_LIBRARIES, as
284 # they're not supposed to be cache entries, and find_path() and
285 # find_library() set cache entries.
287 # Try to find the header file.
289 find_path(PCAP_INCLUDE_DIR pcap.h)
292 # Try to find the library
294 find_library(PCAP_LIBRARY pcap)
296 # Try to find the static library (XXX - what about AIX?)
297 set(SAVED_CMAKE_FIND_LIBRARY_SUFFIXES "${CMAKE_FIND_LIBRARY_SUFFIXES}")
298 set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
299 find_library(PCAP_STATIC_LIBRARY pcap)
300 set(CMAKE_FIND_LIBRARY_SUFFIXES "${SAVED_CMAKE_FIND_LIBRARY_SUFFIXES}")
303 # This will fail if REQUIRED is set and PCAP_INCLUDE_DIR or
304 # PCAP_LIBRARY aren't set.
306 include(FindPackageHandleStandardArgs)
307 find_package_handle_standard_args(PCAP
320 set(PCAP_INCLUDE_DIRS ${PCAP_INCLUDE_DIR})
321 set(PCAP_LIBRARIES ${PCAP_LIBRARY})
322 set(PCAP_STATIC_LIBRARIES ${PCAP_STATIC_LIBRARY})
324 endif(CONFIG_PCAP_FOUND)