]> The Tcpdump Group git mirrors - tcpdump/blob - cmake/Modules/FindPCAP.cmake
855db6517d973b310a1adab2db6ab50da55f6214
[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_QUIET)
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 this is macOS or some other Darwin-based OS, check whether
90 # it's the system-supplied one.
91 #
92 if(APPLE AND "${PCAP_CONFIG}" STREQUAL /usr/bin/pcap-config)
93 #
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.
98 #
99 set(_broken_apple_pcap_config TRUE)
100 endif()
101
102 #
103 # Now get the include directories.
104 #
105 execute_process(COMMAND "${PCAP_CONFIG}" "--cflags"
106 RESULT_VARIABLE PCAP_CONFIG_RESULT
107 OUTPUT_VARIABLE PCAP_CONFIG_OUTPUT
108 OUTPUT_STRIP_TRAILING_WHITESPACE
109 )
110 if(NOT PCAP_CONFIG_RESULT EQUAL 0)
111 message(FATAL_ERROR "pcap-config --cflags failed")
112 endif()
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")
117 #
118 # Extract the directory by removing the -I.
119 #
120 string(REGEX REPLACE "-I" "" _dir ${_arg})
121 #
122 # Work around macOS (and probably other Darwin) brokenness,
123 # by not adding /usr/local/include if it's from the broken
124 # Apple pcap-config.
125 #
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})
130 endif()
131 endif()
132 endforeach()
133
134 #
135 # Now, get the library directories and libraries for dynamic linking.
136 #
137 execute_process(COMMAND "${PCAP_CONFIG}" "--libs"
138 RESULT_VARIABLE PCAP_CONFIG_RESULT
139 OUTPUT_VARIABLE PCAP_CONFIG_OUTPUT
140 OUTPUT_STRIP_TRAILING_WHITESPACE
141 )
142 if(NOT PCAP_CONFIG_RESULT EQUAL 0)
143 message(FATAL_ERROR "pcap-config --libs failed")
144 endif()
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")
150 #
151 # Extract the directory by removing the -L.
152 #
153 string(REGEX REPLACE "-L" "" _dir ${_arg})
154 #
155 # Work around macOS (and probably other Darwin) brokenness,
156 # by not adding /usr/local/lib if it's from the broken
157 # Apple pcap-config.
158 #
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})
163 endif()
164 elseif(_arg MATCHES "^-l")
165 string(REGEX REPLACE "-l" "" _lib ${_arg})
166 list(APPEND CONFIG_PCAP_LIBRARIES ${_lib})
167 endif()
168 endforeach()
169
170 #
171 # Now, get the library directories and libraries for static linking.
172 #
173 execute_process(COMMAND "${PCAP_CONFIG}" "--libs" "--static"
174 RESULT_VARIABLE PCAP_CONFIG_RESULT
175 OUTPUT_VARIABLE PCAP_CONFIG_OUTPUT
176 )
177 if(NOT PCAP_CONFIG_RESULT EQUAL 0)
178 message(FATAL_ERROR "pcap-config --libs --static failed")
179 endif()
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")
185 #
186 # Extract the directory by removing the -L.
187 #
188 string(REGEX REPLACE "-L" "" _dir ${_arg})
189 #
190 # Work around macOS (and probably other Darwin) brokenness,
191 # by not adding /usr/local/lib if it's from the broken
192 # Apple pcap-config.
193 #
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})
198 endif()
199 elseif(_arg MATCHES "^-l")
200 string(REGEX REPLACE "-l" "" _lib ${_arg})
201 #
202 # Try to find that library, so we get its full path, as
203 # we do with dynamic libraries.
204 #
205 list(APPEND CONFIG_PCAP_STATIC_LIBRARIES ${_lib})
206 endif()
207 endforeach()
208
209 #
210 # We've set CONFIG_PCAP_INCLUDE_DIRS, CONFIG_PCAP_LIBRARIES, and
211 # CONFIG_PCAP_STATIC_LIBRARIES above; set CONFIG_PCAP_FOUND.
212 #
213 set(CONFIG_PCAP_FOUND YES)
214 endif()
215 endif()
216
217 #
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.
220 #
221 # If it's not set, we have to look for the libpcap headers and library
222 # ourselves.
223 #
224 if(CONFIG_PCAP_FOUND)
225 #
226 # Use CONFIG_PCAP_INCLUDE_DIRS as the value for PCAP_INCLUDE_DIRS.
227 #
228 set(PCAP_INCLUDE_DIRS "${CONFIG_PCAP_INCLUDE_DIRS}")
229
230 #
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.
235 #
236 foreach(_lib IN LISTS CONFIG_PCAP_LIBRARIES)
237 find_library(_libfullpath ${_lib} HINTS ${CONFIG_PCAP_LIBRARY_DIRS})
238 list(APPEND PCAP_LIBRARIES ${_libfullpath})
239 #
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.
242 #
243 unset(_libfullpath CACHE)
244 endforeach()
245
246 #
247 # Now do the same for the static libraries.
248 #
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})
254 #
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.
257 #
258 unset(_libfullpath CACHE)
259 endforeach()
260 set(CMAKE_FIND_LIBRARY_SUFFIXES "${SAVED_CMAKE_FIND_LIBRARY_SUFFIXES}")
261
262 #
263 # We found libpcap using pkg-config or pcap-config.
264 #
265 set(PCAP_FOUND YES)
266 else(CONFIG_PCAP_FOUND)
267 #
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.
271 #
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.
275 #
276 # Try to find the header file.
277 #
278 find_path(PCAP_INCLUDE_DIR pcap.h)
279
280 #
281 # Try to find the library
282 #
283 find_library(PCAP_LIBRARY pcap)
284
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}")
290
291 #
292 # This will fail if REQUIRED is set and PCAP_INCLUDE_DIR or
293 # PCAP_LIBRARY aren't set.
294 #
295 include(FindPackageHandleStandardArgs)
296 find_package_handle_standard_args(PCAP
297 DEFAULT_MSG
298 PCAP_INCLUDE_DIR
299 PCAP_LIBRARY
300 )
301
302 mark_as_advanced(
303 PCAP_INCLUDE_DIR
304 PCAP_LIBRARY
305 PCAP_STATIC_LIBRARY
306 )
307
308 if(PCAP_FOUND)
309 set(PCAP_INCLUDE_DIRS ${PCAP_INCLUDE_DIR})
310 set(PCAP_LIBRARIES ${PCAP_LIBRARY})
311 set(PCAP_STATIC_LIBRARIES ${PCAP_STATIC_LIBRARY})
312 endif(PCAP_FOUND)
313 endif(CONFIG_PCAP_FOUND)
314 endif(WIN32)