]> The Tcpdump Group git mirrors - tcpdump/blob - cmake/Modules/FindPCAP.cmake
Add a comment about how to tell this where to find pcap.
[tcpdump] / cmake / Modules / FindPCAP.cmake
1 #
2 # Try to find libpcap.
3 #
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 [...])
9 #
10
11 if(WIN32)
12 #
13 # Building for Windows.
14 #
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
18 # does Npcap.
19 #
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.
25 #
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.
29 #
30 find_path(PCAP_INCLUDE_DIR pcap.h)
31
32 # The 64-bit Packet.lib is located under /x64
33 if(CMAKE_SIZEOF_VOID_P EQUAL 8)
34 #
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
37 # Lib/x64 directory.
38 #
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".
42 #
43 set(CMAKE_LIBRARY_ARCHITECTURE "x64")
44 endif()
45 find_library(PCAP_LIBRARY NAMES pcap wpcap)
46
47 #
48 # Do the standard arg processing, including failing if it's a
49 # required package.
50 #
51 include(FindPackageHandleStandardArgs)
52 find_package_handle_standard_args(PCAP
53 DEFAULT_MSG
54 PCAP_INCLUDE_DIR
55 PCAP_LIBRARY
56 )
57 mark_as_advanced(
58 PCAP_INCLUDE_DIR
59 PCAP_LIBRARY
60 )
61 if(PCAP_FOUND)
62 set(PCAP_LIBRARIES ${PCAP_LIBRARY})
63 set(PCAP_INCLUDE_DIRS ${PCAP_INCLUDE_DIR})
64 endif()
65 else(WIN32)
66 #
67 # Building for UN*X.
68 #
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.
75 #
76 if(PCAP_FIND_QUIETLY)
77 set(_quiet "QUIET")
78 endif()
79
80 #
81 # First, try pkg-config.
82 #
83 find_package(PkgConfig)
84 pkg_search_module(CONFIG_PCAP ${_quiet} libpcap)
85
86 if(NOT CONFIG_PCAP_FOUND)
87 #
88 # That didn't work. Try pcap-config.
89 #
90 find_program(PCAP_CONFIG pcap-config)
91 if(PCAP_CONFIG)
92 #
93 # We have pcap-config; use it.
94 #
95 if(NOT "${_quiet}" STREQUAL "QUIET")
96 message(STATUS "Found pcap-config")
97 endif()
98
99 #
100 # if this is macOS or some other Darwin-based OS, check whether
101 # it's the system-supplied one.
102 #
103 if(APPLE AND "${PCAP_CONFIG}" STREQUAL /usr/bin/pcap-config)
104 #
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.
109 #
110 set(_broken_apple_pcap_config TRUE)
111 endif()
112
113 #
114 # Now get the include directories.
115 #
116 execute_process(COMMAND "${PCAP_CONFIG}" "--cflags"
117 RESULT_VARIABLE PCAP_CONFIG_RESULT
118 OUTPUT_VARIABLE PCAP_CONFIG_OUTPUT
119 OUTPUT_STRIP_TRAILING_WHITESPACE
120 )
121 if(NOT PCAP_CONFIG_RESULT EQUAL 0)
122 message(FATAL_ERROR "pcap-config --cflags failed")
123 endif()
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")
128 #
129 # Extract the directory by removing the -I.
130 #
131 string(REGEX REPLACE "-I" "" _dir ${_arg})
132 #
133 # Work around macOS (and probably other Darwin) brokenness,
134 # by not adding /usr/local/include if it's from the broken
135 # Apple pcap-config.
136 #
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})
141 endif()
142 endif()
143 endforeach()
144
145 #
146 # Now, get the library directories and libraries for dynamic linking.
147 #
148 execute_process(COMMAND "${PCAP_CONFIG}" "--libs"
149 RESULT_VARIABLE PCAP_CONFIG_RESULT
150 OUTPUT_VARIABLE PCAP_CONFIG_OUTPUT
151 OUTPUT_STRIP_TRAILING_WHITESPACE
152 )
153 if(NOT PCAP_CONFIG_RESULT EQUAL 0)
154 message(FATAL_ERROR "pcap-config --libs failed")
155 endif()
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")
161 #
162 # Extract the directory by removing the -L.
163 #
164 string(REGEX REPLACE "-L" "" _dir ${_arg})
165 #
166 # Work around macOS (and probably other Darwin) brokenness,
167 # by not adding /usr/local/lib if it's from the broken
168 # Apple pcap-config.
169 #
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})
174 endif()
175 elseif(_arg MATCHES "^-l")
176 string(REGEX REPLACE "-l" "" _lib ${_arg})
177 list(APPEND CONFIG_PCAP_LIBRARIES ${_lib})
178 endif()
179 endforeach()
180
181 #
182 # Now, get the library directories and libraries for static linking.
183 #
184 execute_process(COMMAND "${PCAP_CONFIG}" "--libs" "--static"
185 RESULT_VARIABLE PCAP_CONFIG_RESULT
186 OUTPUT_VARIABLE PCAP_CONFIG_OUTPUT
187 )
188 if(NOT PCAP_CONFIG_RESULT EQUAL 0)
189 message(FATAL_ERROR "pcap-config --libs --static failed")
190 endif()
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")
196 #
197 # Extract the directory by removing the -L.
198 #
199 string(REGEX REPLACE "-L" "" _dir ${_arg})
200 #
201 # Work around macOS (and probably other Darwin) brokenness,
202 # by not adding /usr/local/lib if it's from the broken
203 # Apple pcap-config.
204 #
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})
209 endif()
210 elseif(_arg MATCHES "^-l")
211 string(REGEX REPLACE "-l" "" _lib ${_arg})
212 #
213 # Try to find that library, so we get its full path, as
214 # we do with dynamic libraries.
215 #
216 list(APPEND CONFIG_PCAP_STATIC_LIBRARIES ${_lib})
217 endif()
218 endforeach()
219
220 #
221 # We've set CONFIG_PCAP_INCLUDE_DIRS, CONFIG_PCAP_LIBRARIES, and
222 # CONFIG_PCAP_STATIC_LIBRARIES above; set CONFIG_PCAP_FOUND.
223 #
224 set(CONFIG_PCAP_FOUND YES)
225 endif()
226 endif()
227
228 #
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.
231 #
232 # If it's not set, we have to look for the libpcap headers and library
233 # ourselves.
234 #
235 if(CONFIG_PCAP_FOUND)
236 #
237 # Use CONFIG_PCAP_INCLUDE_DIRS as the value for PCAP_INCLUDE_DIRS.
238 #
239 set(PCAP_INCLUDE_DIRS "${CONFIG_PCAP_INCLUDE_DIRS}")
240
241 #
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.
246 #
247 foreach(_lib IN LISTS CONFIG_PCAP_LIBRARIES)
248 find_library(_libfullpath ${_lib} HINTS ${CONFIG_PCAP_LIBRARY_DIRS})
249 list(APPEND PCAP_LIBRARIES ${_libfullpath})
250 #
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.
253 #
254 unset(_libfullpath CACHE)
255 endforeach()
256
257 #
258 # Now do the same for the static libraries.
259 #
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})
265 #
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.
268 #
269 unset(_libfullpath CACHE)
270 endforeach()
271 set(CMAKE_FIND_LIBRARY_SUFFIXES "${SAVED_CMAKE_FIND_LIBRARY_SUFFIXES}")
272
273 #
274 # We found libpcap using pkg-config or pcap-config.
275 #
276 set(PCAP_FOUND YES)
277 else(CONFIG_PCAP_FOUND)
278 #
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.
282 #
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.
286 #
287 # Try to find the header file.
288 #
289 find_path(PCAP_INCLUDE_DIR pcap.h)
290
291 #
292 # Try to find the library
293 #
294 find_library(PCAP_LIBRARY pcap)
295
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}")
301
302 #
303 # This will fail if REQUIRED is set and PCAP_INCLUDE_DIR or
304 # PCAP_LIBRARY aren't set.
305 #
306 include(FindPackageHandleStandardArgs)
307 find_package_handle_standard_args(PCAP
308 DEFAULT_MSG
309 PCAP_INCLUDE_DIR
310 PCAP_LIBRARY
311 )
312
313 mark_as_advanced(
314 PCAP_INCLUDE_DIR
315 PCAP_LIBRARY
316 PCAP_STATIC_LIBRARY
317 )
318
319 if(PCAP_FOUND)
320 set(PCAP_INCLUDE_DIRS ${PCAP_INCLUDE_DIR})
321 set(PCAP_LIBRARIES ${PCAP_LIBRARY})
322 set(PCAP_STATIC_LIBRARIES ${PCAP_STATIC_LIBRARY})
323 endif(PCAP_FOUND)
324 endif(CONFIG_PCAP_FOUND)
325 endif(WIN32)