]> The Tcpdump Group git mirrors - tcpdump/blob - cmake/Modules/FindPCAP.cmake
3b83c7a79fab45785ea6932df5f92251cab5d0b6
[tcpdump] / cmake / Modules / FindPCAP.cmake
1 #
2 # Try to find libpcap.
3 #
4
5 if(WIN32)
6 #
7 # Building for Windows.
8 #
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
12 # does Npcap.
13 #
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.
19 #
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.
23 #
24 find_path(PCAP_INCLUDE_DIR pcap.h)
25
26 # The 64-bit Packet.lib is located under /x64
27 if(CMAKE_SIZEOF_VOID_P EQUAL 8)
28 #
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
31 # Lib/x64 directory.
32 #
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".
36 #
37 set(CMAKE_LIBRARY_ARCHITECTURE "x64")
38 endif()
39 find_library(PCAP_LIBRARY NAMES pcap wpcap)
40
41 #
42 # Do the standard arg processing, including failing if it's a
43 # required package.
44 #
45 include(FindPackageHandleStandardArgs)
46 find_package_handle_standard_args(PCAP
47 DEFAULT_MSG
48 PCAP_INCLUDE_DIR
49 PCAP_LIBRARY
50 )
51 mark_as_advanced(
52 PCAP_INCLUDE_DIR
53 PCAP_LIBRARY
54 )
55 if(PCAP_FOUND)
56 set(PCAP_LIBRARIES ${PCAP_LIBRARY})
57 set(PCAP_INCLUDE_DIRS ${PCAP_INCLUDE_DIR})
58 endif()
59 else(WIN32)
60 #
61 # Building for UN*X.
62 #
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.
69 #
70 if(PCAP_FIND_QUIETLY)
71 set(_quiet "QUIET")
72 endif()
73
74 #
75 # First, try pkg-config.
76 #
77 find_package(PkgConfig)
78 pkg_search_module(CONFIG_PCAP ${_quiet} libpcap)
79
80 if(NOT CONFIG_PCAP_FOUND)
81 #
82 # That didn't work. Try pcap-config.
83 #
84 find_program(PCAP_CONFIG pcap-config)
85 if(PCAP_CONFIG)
86 #
87 # We have pcap-config; use it.
88 #
89 if(NOT "${_quiet}" STREQUAL "QUIET")
90 message(STATUS "Found pcap-config")
91 endif()
92
93 #
94 # if this is macOS or some other Darwin-based OS, check whether
95 # it's the system-supplied one.
96 #
97 if(APPLE AND "${PCAP_CONFIG}" STREQUAL /usr/bin/pcap-config)
98 #
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.
103 #
104 set(_broken_apple_pcap_config TRUE)
105 endif()
106
107 #
108 # Now get the include directories.
109 #
110 execute_process(COMMAND "${PCAP_CONFIG}" "--cflags"
111 RESULT_VARIABLE PCAP_CONFIG_RESULT
112 OUTPUT_VARIABLE PCAP_CONFIG_OUTPUT
113 OUTPUT_STRIP_TRAILING_WHITESPACE
114 )
115 if(NOT PCAP_CONFIG_RESULT EQUAL 0)
116 message(FATAL_ERROR "pcap-config --cflags failed")
117 endif()
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")
122 #
123 # Extract the directory by removing the -I.
124 #
125 string(REGEX REPLACE "-I" "" _dir ${_arg})
126 #
127 # Work around macOS (and probably other Darwin) brokenness,
128 # by not adding /usr/local/include if it's from the broken
129 # Apple pcap-config.
130 #
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})
135 endif()
136 endif()
137 endforeach()
138
139 #
140 # Now, get the library directories and libraries for dynamic linking.
141 #
142 execute_process(COMMAND "${PCAP_CONFIG}" "--libs"
143 RESULT_VARIABLE PCAP_CONFIG_RESULT
144 OUTPUT_VARIABLE PCAP_CONFIG_OUTPUT
145 OUTPUT_STRIP_TRAILING_WHITESPACE
146 )
147 if(NOT PCAP_CONFIG_RESULT EQUAL 0)
148 message(FATAL_ERROR "pcap-config --libs failed")
149 endif()
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")
155 #
156 # Extract the directory by removing the -L.
157 #
158 string(REGEX REPLACE "-L" "" _dir ${_arg})
159 #
160 # Work around macOS (and probably other Darwin) brokenness,
161 # by not adding /usr/local/lib if it's from the broken
162 # Apple pcap-config.
163 #
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})
168 endif()
169 elseif(_arg MATCHES "^-l")
170 string(REGEX REPLACE "-l" "" _lib ${_arg})
171 list(APPEND CONFIG_PCAP_LIBRARIES ${_lib})
172 endif()
173 endforeach()
174
175 #
176 # Now, get the library directories and libraries for static linking.
177 #
178 execute_process(COMMAND "${PCAP_CONFIG}" "--libs" "--static"
179 RESULT_VARIABLE PCAP_CONFIG_RESULT
180 OUTPUT_VARIABLE PCAP_CONFIG_OUTPUT
181 )
182 if(NOT PCAP_CONFIG_RESULT EQUAL 0)
183 message(FATAL_ERROR "pcap-config --libs --static failed")
184 endif()
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")
190 #
191 # Extract the directory by removing the -L.
192 #
193 string(REGEX REPLACE "-L" "" _dir ${_arg})
194 #
195 # Work around macOS (and probably other Darwin) brokenness,
196 # by not adding /usr/local/lib if it's from the broken
197 # Apple pcap-config.
198 #
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})
203 endif()
204 elseif(_arg MATCHES "^-l")
205 string(REGEX REPLACE "-l" "" _lib ${_arg})
206 #
207 # Try to find that library, so we get its full path, as
208 # we do with dynamic libraries.
209 #
210 list(APPEND CONFIG_PCAP_STATIC_LIBRARIES ${_lib})
211 endif()
212 endforeach()
213
214 #
215 # We've set CONFIG_PCAP_INCLUDE_DIRS, CONFIG_PCAP_LIBRARIES, and
216 # CONFIG_PCAP_STATIC_LIBRARIES above; set CONFIG_PCAP_FOUND.
217 #
218 set(CONFIG_PCAP_FOUND YES)
219 endif()
220 endif()
221
222 #
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.
225 #
226 # If it's not set, we have to look for the libpcap headers and library
227 # ourselves.
228 #
229 if(CONFIG_PCAP_FOUND)
230 #
231 # Use CONFIG_PCAP_INCLUDE_DIRS as the value for PCAP_INCLUDE_DIRS.
232 #
233 set(PCAP_INCLUDE_DIRS "${CONFIG_PCAP_INCLUDE_DIRS}")
234
235 #
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.
240 #
241 foreach(_lib IN LISTS CONFIG_PCAP_LIBRARIES)
242 find_library(_libfullpath ${_lib} HINTS ${CONFIG_PCAP_LIBRARY_DIRS})
243 list(APPEND PCAP_LIBRARIES ${_libfullpath})
244 #
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.
247 #
248 unset(_libfullpath CACHE)
249 endforeach()
250
251 #
252 # Now do the same for the static libraries.
253 #
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})
259 #
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.
262 #
263 unset(_libfullpath CACHE)
264 endforeach()
265 set(CMAKE_FIND_LIBRARY_SUFFIXES "${SAVED_CMAKE_FIND_LIBRARY_SUFFIXES}")
266
267 #
268 # We found libpcap using pkg-config or pcap-config.
269 #
270 set(PCAP_FOUND YES)
271 else(CONFIG_PCAP_FOUND)
272 #
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.
276 #
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.
280 #
281 # Try to find the header file.
282 #
283 find_path(PCAP_INCLUDE_DIR pcap.h)
284
285 #
286 # Try to find the library
287 #
288 find_library(PCAP_LIBRARY pcap)
289
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}")
295
296 #
297 # This will fail if REQUIRED is set and PCAP_INCLUDE_DIR or
298 # PCAP_LIBRARY aren't set.
299 #
300 include(FindPackageHandleStandardArgs)
301 find_package_handle_standard_args(PCAP
302 DEFAULT_MSG
303 PCAP_INCLUDE_DIR
304 PCAP_LIBRARY
305 )
306
307 mark_as_advanced(
308 PCAP_INCLUDE_DIR
309 PCAP_LIBRARY
310 PCAP_STATIC_LIBRARY
311 )
312
313 if(PCAP_FOUND)
314 set(PCAP_INCLUDE_DIRS ${PCAP_INCLUDE_DIR})
315 set(PCAP_LIBRARIES ${PCAP_LIBRARY})
316 set(PCAP_STATIC_LIBRARIES ${PCAP_STATIC_LIBRARY})
317 endif(PCAP_FOUND)
318 endif(CONFIG_PCAP_FOUND)
319 endif(WIN32)