]> The Tcpdump Group git mirrors - tcpdump/blob - CMakeLists.txt
DHCP: Fix a comment about formats for (un)signed longs/shorts data
[tcpdump] / CMakeLists.txt
1 if(WIN32)
2 #
3 # We need 3.12 or later, so that we can set policy CMP0074; see
4 # below.
5 #
6 cmake_minimum_required(VERSION 3.12)
7 else(WIN32)
8 #
9 # For now:
10 #
11 # if this is a version of CMake less than 3.5, require only
12 # 2.8.12, just in case somebody is configuring with CMake
13 # on a "long-term support" version # of some OS and that
14 # version supplies an older version of CMake;
15 #
16 # otherwise, if it's a version less than 3.10, require only
17 # 3.5, just in case somebody is configuring with CMake
18 # on a "long-term support" version # of some OS and that
19 # version supplies an older version of CMake;
20 #
21 # otherwise, require 3.10, so we don't get messages warning
22 # that support for versions of CMake lower than 3.10 is
23 # deprecated.
24 #
25 if(CMAKE_VERSION VERSION_LESS "3.5")
26 cmake_minimum_required(VERSION 2.8.12)
27 elseif(CMAKE_VERSION VERSION_LESS "3.10")
28 cmake_minimum_required(VERSION 3.5)
29 else()
30 cmake_minimum_required(VERSION 3.10)
31 endif()
32 endif(WIN32)
33
34 #
35 # We want find_path() and find_library() to honor {packagename}_ROOT,
36 # as that appears to be the standard way to say "hey, look here for
37 # this package" from the command line.
38 #
39 if(POLICY CMP0074)
40 cmake_policy(SET CMP0074 NEW)
41 endif()
42
43 #
44 # OK, this is a pain.
45 #
46 # When building on NetBSD, with a libpcap installed from pkgsrc,
47 # a -Wl,-rpath,/usr/pkg/lib option is added to the options when
48 # linking tcpdump. This puts /usr/pkg/lib into the run-time path.
49 #
50 # However, by default, CMake adds a rule to the install CMake script
51 # a CMake command (using an undocumented subcommand of file()) that
52 # strips /usr/pkg/lib *out* of the run-time path; the message in the
53 # output for the "install" target is
54 #
55 # -- Set runtime path of "{target-directory}/tcpdump" to ""
56 #
57 # I am not certain what the rationale is for doing this, but a
58 # *consequence* of this is that, when you run the installed tcpdump,
59 # it fails to find libpcap.so:
60 #
61 # $ {target-directory}/tcpdump -h
62 # {target-directory}/tcpdump: Shared object "libpcap.so.0" not found
63 #
64 # It also appears to be the case that, on Ubuntu 22.04, FreeBSD 12,
65 # DragonFly BSD 5.8, OpenBSD 6.6, and Solaris 11.4,
66 #
67 # On Ubuntu and Solaris, even if you have a libpcap in /usr/local, you
68 # have to provide not only -I/usr/local/include and -L/usr/local/lib,
69 # you also must provide -Wl,-rpath,/usr/local/lib in order to have
70 # the run-time linker look in /usr/local/lib for libpcap. If it's not
71 # specified, then, if the shared library major version number of the
72 # libpcap in /usr/lib is the same as the shared major version number
73 # of the libpcap in /usr/local/lib, the run-time linker will find the
74 # libpcap in /usr/lib; if the versions are different, the run-time
75 # linker will fail to find the libpcap in /usr/lib, so the program will
76 # fail to run.
77 #
78 # We suppress this by setting CMAKE_INSTALL_RPATH_USE_LINK_PATH to TRUE;
79 # as the documentation for that variable says:
80 #
81 # Add paths to linker search and installed rpath.
82 #
83 # CMAKE_INSTALL_RPATH_USE_LINK_PATH is a boolean that if set to True
84 # will append to the runtime search path (rpath) of installed
85 # binaries any directories outside the project that are in the linker
86 # search path or contain linked library files. The directories are
87 # appended after the value of the INSTALL_RPATH target property.
88 #
89 # If, for whatever reason, directories in which we search for external
90 # libraries, other than the standard system library directories, are
91 # added to the executable's rpath in the build process, we most
92 # definitely want them in the installed image's rpath if they are
93 # necessary in order to find the libraries at run time.
94 #
95 set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
96
97 set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake/Modules)
98
99 #
100 # We explicitly indicate what languages are used in tcpdump to avoid
101 # checking for a C++ compiler.
102 #
103 # One reason to avoid that check is that there's no need to waste
104 # configuration time performing it.
105 #
106 # Another reason is that:
107 #
108 # CMake will try to determine the sizes of some data types, including
109 # void *, early in the process of configuration; apparently, it's done
110 # as part of processing the project() command.
111 #
112 # At least as of CMake 2.8.6, it does so by checking the size of
113 # "void *" in C, setting CMAKE_C_SIZEOF_DATA_PTR based on that,
114 # setting CMAKE_SIZEOF_VOID_P to that, and then checking the size
115 # of "void *" in C++, setting CMAKE_CXX_SIZEOF_DATA_PTR based on
116 # that, and then setting CMAKE_SIZEOF_VOID_P to *that*.
117 #
118 # The compile tests include whatever C flags may have been provided
119 # to CMake in the CFLAGS and CXXFLAGS environment variables.
120 #
121 # If you set an architecture flag such as -m32 or -m64 in CFLAGS
122 # but *not* in CXXFLAGS, the size for C++ will win, and hilarity
123 # will ensue.
124 #
125 # Or if, at least on Solaris, you have a newer version of GCC
126 # installed, but *not* a newer version of G++, and you have Oracle
127 # Studio installed, it will find GCC, which will default to building
128 # 64-bit, and Oracle Studio's C++ compiler, which will default to
129 # building 32-bit, the size for C++ will win, and, again, hilarity
130 # will ensue.
131 #
132 project(tcpdump C)
133
134 #
135 # Export the size of void * as SIZEOF_VOID_P so that it can be
136 # tested with #if.
137 #
138 set(SIZEOF_VOID_P "${CMAKE_SIZEOF_VOID_P}")
139
140 #
141 # Show the bit width for which we're compiling.
142 # This can help debug problems if you're dealing with a compiler that
143 # defaults to generating 32-bit code even when running on a 64-bit
144 # platform, and where that platform may provide only 64-bit versions of
145 # libraries that we might use (looking at *you*, Oracle Studio!).
146 #
147 if(CMAKE_SIZEOF_VOID_P EQUAL 4)
148 message(STATUS "Building 32-bit")
149 elseif(CMAKE_SIZEOF_VOID_P EQUAL 8)
150 message(STATUS "Building 64-bit")
151 endif()
152
153 #
154 # Solaris pkg-config is annoying. For at least one package (D-Bus, I'm
155 # looking at *you*!), there are separate include files for 32-bit and
156 # 64-bit builds (I guess using "unsigned long long" as a 64-bit integer
157 # type on a 64-bit build is like crossing the beams or something), and
158 # there are two separate .pc files, so if we're doing a 32-bit build we
159 # should make sure we look in /usr/lib/pkgconfig for .pc files and if
160 # we're doing a 64-bit build we should make sure we look in
161 # /usr/lib/amd64/pkgconfig for .pc files.
162 #
163 if(CMAKE_SYSTEM_NAME STREQUAL "SunOS" AND CMAKE_SYSTEM_VERSION MATCHES "5[.][0-9.]*")
164 #
165 # Note: string(REPLACE) does not appear to support using ENV{...}
166 # as an argument, so we set a variable and then use set() to set
167 # the environment variable.
168 #
169 if(CMAKE_SIZEOF_VOID_P EQUAL 8)
170 #
171 # 64-bit build. If /usr/lib/pkgconfig appears in the path,
172 # prepend /usr/lib/amd64/pkgconfig to it; otherwise,
173 # put /usr/lib/amd64 at the end.
174 #
175 if((NOT DEFINED ENV{PKG_CONFIG_PATH}) OR "$ENV{PKG_CONFIG_PATH}" EQUAL "")
176 #
177 # Not set, or empty. Set it to /usr/lib/amd64/pkgconfig.
178 #
179 set(fixed_path "/usr/lib/amd64/pkgconfig")
180 elseif("$ENV{PKG_CONFIG_PATH}" MATCHES "/usr/lib/pkgconfig")
181 #
182 # It contains /usr/lib/pkgconfig. Prepend
183 # /usr/lib/amd64/pkgconfig to /usr/lib/pkgconfig.
184 #
185 string(REPLACE "/usr/lib/pkgconfig"
186 "/usr/lib/amd64/pkgconfig:/usr/lib/pkgconfig"
187 fixed_path "$ENV{PKG_CONFIG_PATH}")
188 else()
189 #
190 # Not empty, but doesn't contain /usr/lib/pkgconfig.
191 # Append /usr/lib/amd64/pkgconfig to it.
192 #
193 set(fixed_path "$ENV{PKG_CONFIG_PATH}:/usr/lib/amd64/pkgconfig")
194 endif()
195 set(ENV{PKG_CONFIG_PATH} "${fixed_path}")
196 elseif(CMAKE_SIZEOF_VOID_P EQUAL 4)
197 #
198 # 32-bit build. If /usr/amd64/lib/pkgconfig appears in the path,
199 # prepend /usr/lib/pkgconfig to it.
200 #
201 if("$ENV{PKG_CONFIG_PATH}" MATCHES "/usr/lib/amd64/pkgconfig")
202 #
203 # It contains /usr/lib/amd64/pkgconfig. Prepend
204 # /usr/lib/pkgconfig to /usr/lib/amd64/pkgconfig.
205 #
206 string(REPLACE "/usr/lib/amd64/pkgconfig"
207 "/usr/lib/pkgconfig:/usr/lib/amd64/pkgconfig"
208 fixed_path "$ENV{PKG_CONFIG_PATH}")
209 set(ENV{PKG_CONFIG_PATH} "${fixed_path}")
210 endif()
211 endif()
212 endif()
213
214 #
215 # For checking if a compiler flag works and adding it if it does.
216 #
217 include(CheckCCompilerFlag)
218 macro(check_and_add_compiler_option _option)
219 message(STATUS "Checking C compiler flag ${_option}")
220 string(REPLACE "=" "-" _temp_option_variable ${_option})
221 string(REGEX REPLACE "^-" "" _option_variable ${_temp_option_variable})
222 check_c_compiler_flag("${_option}" ${_option_variable})
223 if(${${_option_variable}})
224 set(C_ADDITIONAL_FLAGS "${C_ADDITIONAL_FLAGS} ${_option}")
225 endif()
226 endmacro()
227
228 #
229 # If we're building with Visual Studio, we require Visual Studio 2015,
230 # in order to get sufficient C99 compatibility. Check for that.
231 #
232 # If not, try the appropriate flag for the compiler to enable C99
233 # features.
234 #
235 set(C_ADDITIONAL_FLAGS "")
236 if(MSVC)
237 if(MSVC_VERSION LESS 1900)
238 message(FATAL_ERROR "Visual Studio 2015 or later is required")
239 endif()
240
241 #
242 # Treat source files as being in UTF-8 with MSVC if it's not using
243 # the Clang front end.
244 # We assume that UTF-8 source is OK with other compilers and with
245 # MSVC if it's using the Clang front end.
246 #
247 if(NOT ${CMAKE_C_COMPILER} MATCHES "clang*")
248 set(C_ADDITIONAL_FLAGS "${C_ADDITIONAL_FLAGS} /utf-8")
249 endif(NOT ${CMAKE_C_COMPILER} MATCHES "clang*")
250 else(MSVC)
251 #
252 # Try to enable as many C99 features as we can.
253 # At minimum, we want C++/C99-style // comments.
254 #
255 # Newer versions of compilers might default to supporting C99, but
256 # older versions may require a special flag.
257 #
258 # Prior to CMake 3.1, setting CMAKE_C_STANDARD will not have any effect,
259 # so, unless and until we require CMake 3.1 or later, we have to do it
260 # ourselves on pre-3.1 CMake, so we just do it ourselves on all versions
261 # of CMake.
262 #
263 # Note: with CMake 3.1 through 3.5, the only compilers for which CMake
264 # handles CMAKE_C_STANDARD are GCC and Clang. 3.6 adds support only
265 # for Intel C; 3.9 adds support for PGI C, Sun C, and IBM XL C, and
266 # 3.10 adds support for Cray C and IAR C, but no version of CMake has
267 # support for HP C. Therefore, even if we use CMAKE_C_STANDARD with
268 # compilers for which CMake supports it, we may still have to do it
269 # ourselves on other compilers.
270 #
271 # See the CMake documentation for the CMAKE_<LANG>_COMPILER_ID variables
272 # for a list of compiler IDs.
273 #
274 # XXX - this just tests whether the option works and adds it if it does.
275 # We don't test whether it's necessary in order to get the C99 features
276 # that we use; if we ever have a user who tries to compile with a compiler
277 # that can't be made to support those features, we can add a test to make
278 # sure we actually *have* C99 support.
279 #
280 if(CMAKE_C_COMPILER_ID MATCHES "GNU" OR
281 CMAKE_C_COMPILER_ID MATCHES "Clang")
282 check_and_add_compiler_option("-std=gnu99")
283 elseif(CMAKE_C_COMPILER_ID MATCHES "XL")
284 #
285 # We want support for extensions picked up for GNU C compatibility,
286 # so we use -qlanglvl=extc99.
287 #
288 check_and_add_compiler_option("-qlanglvl=extc99")
289 elseif(CMAKE_C_COMPILER_ID MATCHES "HP")
290 check_and_add_compiler_option("-AC99")
291 elseif(CMAKE_C_COMPILER_ID MATCHES "Sun")
292 check_and_add_compiler_option("-xc99")
293 elseif(CMAKE_C_COMPILER_ID MATCHES "Intel")
294 check_and_add_compiler_option("-c99")
295 endif()
296 endif(MSVC)
297
298 set(LIBRARY_NAME netdissect)
299
300 ###################################################################
301 # Parameters
302 ###################################################################
303
304 option(WITH_SMI "Build with libsmi, if available" ON)
305 option(WITH_CRYPTO "Build with OpenSSL/libressl libcrypto, if available" ON)
306 option(WITH_CAPSICUM "Build with Capsicum security functions, if available" ON)
307 option(WITH_CAP_NG "Use libcap-ng, if available" ON)
308 option(ENABLE_SMB "Build with the SMB dissector" OFF)
309
310 #
311 # String parameters. Neither of them are set, initially; only if the
312 # user explicitly configures them are they set.
313 #
314 # WITH_CHROOT is STRING, not PATH, as the directory need not exist
315 # when CMake is run.
316 #
317 set(WITH_CHROOT CACHE STRING
318 "Directory to which to chroot when dropping privileges")
319 set(WITH_USER CACHE STRING
320 "User to whom to set the UID when dropping privileges")
321
322 #
323 # By default, build universal with the appropriate set of architectures
324 # for the OS on which we're doing the build.
325 #
326 if(APPLE AND "${CMAKE_OSX_ARCHITECTURES}" STREQUAL "")
327 #
328 # Get the major version of Darwin.
329 #
330 string(REGEX MATCH "^([0-9]+)" SYSTEM_VERSION_MAJOR "${CMAKE_SYSTEM_VERSION}")
331
332 if(SYSTEM_VERSION_MAJOR EQUAL 9)
333 #
334 # Leopard. Build for x86 and 32-bit PowerPC, with
335 # x86 first. (That's what Apple does.)
336 #
337 set(CMAKE_OSX_ARCHITECTURES "i386;ppc")
338 elseif(SYSTEM_VERSION_MAJOR EQUAL 10)
339 #
340 # Snow Leopard. Build for x86-64 and x86, with
341 # x86-64 first. (That's what Apple does.)
342 #
343 set(CMAKE_OSX_ARCHITECTURES "x86_64;i386")
344 endif()
345 endif()
346
347 ###################################################################
348 # Versioning
349 ###################################################################
350
351 # Get, parse, format and set tcpdump's version string from
352 # [tcpdump_root]/VERSION for later use.
353
354 # Get MAJOR, MINOR, PATCH & SUFFIX
355 file(STRINGS ${tcpdump_SOURCE_DIR}/VERSION
356 PACKAGE_VERSION
357 LIMIT_COUNT 1 # Read only the first line
358 )
359
360 ######################################
361 # Project settings
362 ######################################
363
364 include_directories(
365 ${CMAKE_CURRENT_BINARY_DIR}
366 ${tcpdump_SOURCE_DIR}
367 )
368
369 if(MSVC)
370 add_definitions(-D__STDC__)
371 add_definitions(-D_CRT_SECURE_NO_WARNINGS)
372 endif(MSVC)
373
374 if(MSVC)
375 if (USE_STATIC_RT)
376 MESSAGE(STATUS "Use STATIC runtime")
377 set(NAME_RT MT)
378 set (CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} /MT")
379 set (CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /MT")
380 set (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT")
381 set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd")
382
383 set (CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL} /MT")
384 set (CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO} /MT")
385 set (CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /MT")
386 set (CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /MTd")
387 else (USE_STATIC_RT)
388 MESSAGE(STATUS "Use DYNAMIC runtime")
389 set(NAME_RT MD)
390 set (CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} /MD")
391 set (CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /MD")
392 set (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MD")
393 set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MDd")
394
395 set (CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL} /MD")
396 set (CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO} /MD")
397 set (CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /MD")
398 set (CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /MDd")
399 endif (USE_STATIC_RT)
400 endif(MSVC)
401
402 #
403 # CMake's definition of "cross-compiling" appears to be "compiling
404 # for an *operating system* other than the one on which the build
405 # is being done*.
406 #
407 # This is an inadequate definition, as people build for the same
408 # operating system but a different instruction set, e.g. building
409 # on an IA-32 or x86-64 Linux box for an Arm embedded Linux box,
410 # or building Arm code on an IA-32 or x86-64 Windows box.
411 #
412 # So just test whether check_c_source_runs() on a trivial program
413 # works; if not, it's probably because the generated code won't
414 # run on the platform on which we're running.
415 #
416 include(CheckCSourceRuns)
417 if (NOT CMAKE_CROSSCOMPILING)
418 check_c_source_runs("
419 int main()
420 {
421 return 0;
422 }
423 "
424 CHECK_C_SOURCE_RUNS_WORKS
425 )
426 if (NOT CHECK_C_SOURCE_RUNS_WORKS)
427 set(CMAKE_CROSSCOMPILING TRUE)
428 endif()
429 endif()
430
431 ###################################################################
432 # Detect available platform features
433 ###################################################################
434
435 include(CMakePushCheckState)
436 include(CheckIncludeFile)
437 include(CheckIncludeFiles)
438 include(CheckFunctionExists)
439 include(CheckLibraryExists)
440 include(CheckSymbolExists)
441 include(CheckStructHasMember)
442 include(CheckVariableExists)
443 include(CheckTypeSize)
444
445 #
446 # Get the size of a time_t, to know whether it's 32-bit or 64-bit.
447 #
448 cmake_push_check_state()
449 set(CMAKE_EXTRA_INCLUDE_FILES time.h)
450 check_type_size("time_t" SIZEOF_TIME_T)
451 cmake_pop_check_state()
452
453 #
454 # Header files.
455 #
456 check_include_file(rpc/rpc.h HAVE_RPC_RPC_H)
457 check_include_file(net/if.h HAVE_NET_IF_H)
458 if(HAVE_RPC_RPC_H)
459 check_include_files("rpc/rpc.h;rpc/rpcent.h" HAVE_RPC_RPCENT_H)
460 endif(HAVE_RPC_RPC_H)
461
462 #
463 # Functions.
464 #
465 check_function_exists(strlcat HAVE_STRLCAT)
466 check_function_exists(strlcpy HAVE_STRLCPY)
467 check_function_exists(strdup HAVE_STRDUP)
468 check_function_exists(strsep HAVE_STRSEP)
469
470 #
471 # Find library needed for gethostbyaddr.
472 # NOTE: if you hand check_library_exists as its last argument a variable
473 # that's been set, it skips the test, so we need different variables.
474 #
475 set(TCPDUMP_LINK_LIBRARIES "")
476 if(WIN32)
477 #
478 # We need winsock2.h and ws2tcpip.h.
479 #
480 cmake_push_check_state()
481 set(CMAKE_REQUIRED_LIBRARIES ws2_32)
482 check_symbol_exists(gethostbyaddr "winsock2.h;ws2tcpip.h" LIBWS2_32_HAS_GETHOSTBYADDR)
483 cmake_pop_check_state()
484 if(LIBWS2_32_HAS_GETHOSTBYADDR)
485 set(TCPDUMP_LINK_LIBRARIES ws2_32 ${TCPDUMP_LINK_LIBRARIES})
486 else(LIBWS2_32_HAS_GETHOSTBYADDR)
487 message(FATAL_ERROR "gethostbyaddr is required, but wasn't found")
488 endif(LIBWS2_32_HAS_GETHOSTBYADDR)
489 else(WIN32)
490 check_function_exists(gethostbyaddr STDLIBS_HAVE_GETHOSTBYADDR)
491 if(NOT STDLIBS_HAVE_GETHOSTBYADDR)
492 check_library_exists(socket gethostbyaddr "" LIBSOCKET_HAS_GETHOSTBYADDR)
493 if(LIBSOCKET_HAS_GETHOSTBYADDR)
494 set(TCPDUMP_LINK_LIBRARIES ${TCPDUMP_LINK_LIBRARIES} socket)
495 else(LIBSOCKET_HAS_GETHOSTBYADDR)
496 check_library_exists(nsl gethostbyaddr "" LIBNSL_HAS_GETHOSTBYADDR)
497 if(LIBNSL_HAS_GETHOSTBYADDR)
498 set(TCPDUMP_LINK_LIBRARIES ${TCPDUMP_LINK_LIBRARIES} nsl)
499 else(LIBNSL_HAS_GETHOSTBYADDR)
500 check_library_exists(network gethostbyaddr "" LIBNETWORK_HAS_GETHOSTBYADDR)
501 if(LIBNETWORK_HAS_GETHOSTBYADDR)
502 set(TCPDUMP_LINK_LIBRARIES ${TCPDUMP_LINK_LIBRARIES} network)
503 else(LIBNETWORK_HAS_GETHOSTBYADDR)
504 message(FATAL_ERROR "gethostbyaddr is required, but wasn't found")
505 endif(LIBNETWORK_HAS_GETHOSTBYADDR)
506 endif(LIBNSL_HAS_GETHOSTBYADDR)
507 endif(LIBSOCKET_HAS_GETHOSTBYADDR)
508 endif(NOT STDLIBS_HAVE_GETHOSTBYADDR)
509 endif(WIN32)
510
511 #
512 # This may require additional libraries.
513 #
514 cmake_push_check_state()
515 set(CMAKE_REQUIRED_LIBRARIES ${TCPDUMP_LINK_LIBRARIES})
516 check_function_exists(getservent STDLIBS_HAVE_GETSERVENT)
517 if(STDLIBS_HAVE_GETSERVENT)
518 set(HAVE_GETSERVENT TRUE)
519 else(STDLIBS_HAVE_GETSERVENT)
520 #
521 # Some platforms may need -lsocket for getservent.
522 #
523 set(CMAKE_REQUIRED_LIBRARIES socket ${TCPDUMP_LINK_LIBRARIES})
524 check_function_exists(getservent LIBSOCKET_HAS_GETSERVENT)
525 if(LIBSOCKET_HAS_GETSERVENT)
526 set(HAVE_GETSERVENT TRUE)
527 set(TCPDUMP_LINK_LIBRARIES socket ${TCPDUMP_LINK_LIBRARIES})
528 endif(LIBSOCKET_HAS_GETSERVENT)
529 endif(STDLIBS_HAVE_GETSERVENT)
530 cmake_pop_check_state()
531
532 if (NOT CMAKE_CROSSCOMPILING)
533 #
534 # Require a proof of suitable snprintf(3), same as in Autoconf.
535 #
536 check_c_source_runs("
537 #include <stdio.h>
538 #include <string.h>
539 #include <inttypes.h>
540 #include <sys/types.h>
541
542 #if defined(_WIN32) && !defined(_SSIZE_T_DEFINED)
543 /*
544 * On UN*Xes, this is a signed integer type of the same size as size_t.
545 *
546 * It's not defined by Visual Studio; we assume that ptrdiff_t will
547 * be a type that is a signed integer type of the same size as size_t.
548 */
549 typedef ptrdiff_t ssize_t;
550 #endif
551
552 /*
553 * Avoid trying to cast negative values to unsigned types, or doing
554 * shifts of signed types, in order not to have the test program fail
555 * if we're building with undefined-behavior sanitizers enabled.
556 */
557 int main()
558 {
559 char buf[100];
560 unsigned int ui = sizeof(buf);
561 int i = sizeof(buf);
562 int64_t i64 = INT64_C(0x100000000);
563 uint64_t ui64 = UINT64_C(0x100000000);
564
565 snprintf(buf, sizeof(buf), \"%zu\", (size_t)ui);
566 if (strncmp(buf, \"100\", sizeof(buf)))
567 return 1;
568
569 snprintf(buf, sizeof(buf), \"%zd\", (ssize_t)(-i));
570 if (strncmp(buf, \"-100\", sizeof(buf)))
571 return 2;
572
573 snprintf(buf, sizeof(buf), \"%\" PRId64, -i64);
574 if (strncmp(buf, \"-4294967296\", sizeof(buf)))
575 return 3;
576
577 snprintf(buf, sizeof(buf), \"0o%\" PRIo64, ui64);
578 if (strncmp(buf, \"0o40000000000\", sizeof(buf)))
579 return 4;
580
581 snprintf(buf, sizeof(buf), \"0x%\" PRIx64, ui64);
582 if (strncmp(buf, \"0x100000000\", sizeof(buf)))
583 return 5;
584
585 snprintf(buf, sizeof(buf), \"%\" PRIu64, ui64);
586 if (strncmp(buf, \"4294967296\", sizeof(buf)))
587 return 6;
588
589 return 0;
590 }
591
592 "
593 SUITABLE_SNPRINTF
594 )
595 if(NOT SUITABLE_SNPRINTF)
596 message(FATAL_ERROR
597 "The snprintf(3) implementation in this libc is not suitable,
598 tcpdump would not work correctly even if it managed to compile."
599 )
600 endif()
601 else()
602 message(STATUS "Skipped SUITABLE_SNPRINTF because cross-compiling.")
603 endif()
604
605 check_function_exists(getopt_long HAVE_GETOPT_LONG)
606 check_function_exists(setlinebuf HAVE_SETLINEBUF)
607 #
608 # For Windows, don't need to waste time checking for fork() or vfork().
609 #
610 if(NOT WIN32)
611 check_function_exists(fork HAVE_FORK)
612 check_function_exists(vfork HAVE_VFORK)
613 endif(NOT WIN32)
614
615 #
616 # Some platforms may need -lnsl for getrpcbynumber.
617 #
618 cmake_push_check_state()
619 set(CMAKE_REQUIRED_LIBRARIES ${TCPDUMP_LINK_LIBRARIES})
620 check_function_exists(getrpcbynumber STDLIBS_HAVE_GETRPCBYNUMBER)
621 if(STDLIBS_HAVE_GETRPCBYNUMBER)
622 set(HAVE_GETRPCBYNUMBER TRUE)
623 else(STDLIBS_HAVE_GETRPCBYNUMBER)
624 set(CMAKE_REQUIRED_LIBRARIES ${TCPDUMP_LINK_LIBRARIES} nsl)
625 check_function_exists(getrpcbynumber LIBNSL_HAS_GETRPCBYNUMBER)
626 if(LIBNSL_HAS_GETRPCBYNUMBER)
627 set(HAVE_GETRPCBYNUMBER TRUE)
628 set(TCPDUMP_LINK_LIBRARIES ${TCPDUMP_LINK_LIBRARIES} nsl)
629 endif(LIBNSL_HAS_GETRPCBYNUMBER)
630 endif(STDLIBS_HAVE_GETRPCBYNUMBER)
631 cmake_pop_check_state()
632
633 #
634 # This requires the libraries we require, as ether_ntohost might be
635 # in one of those libraries. That means we have to do this after
636 # we check for those libraries.
637 #
638 # You are in a twisty little maze of UN*Xes, all different.
639 # Some might not have ether_ntohost().
640 # Some might have it and declare it in <net/ethernet.h>.
641 # Some might have it and declare it in <netinet/ether.h>
642 # Some might have it and declare it in <sys/ethernet.h>.
643 # Some might have it and declare it in <arpa/inet.h>.
644 # Some might have it and declare it in <netinet/if_ether.h>.
645 # Some might have it and not declare it in any header file.
646 #
647 # Before you is a C compiler.
648 #
649 cmake_push_check_state()
650 set(CMAKE_REQUIRED_LIBRARIES ${TCPDUMP_LINK_LIBRARIES})
651 check_function_exists(ether_ntohost HAVE_ETHER_NTOHOST)
652 if(HAVE_ETHER_NTOHOST)
653 #
654 # OK, we have ether_ntohost(). We don't check whether it's buggy,
655 # as we assume any system that has CMake is likely to be new enough
656 # that, if it has ether_ntohost(), whatever bug is checked for in
657 # autotools is fixed; we just decide to use it.
658 #
659 set(USE_ETHER_NTOHOST TRUE)
660
661 #
662 # Is it declared in <net/ethernet.h>?
663 #
664 # This test fails if we don't have <net/ethernet.h> or if we do
665 # but it doesn't declare ether_ntohost().
666 #
667 check_symbol_exists(ether_ntohost net/ethernet.h NET_ETHERNET_H_DECLARES_ETHER_NTOHOST)
668 if(NET_ETHERNET_H_DECLARES_ETHER_NTOHOST)
669 #
670 # Yes - we have it declared.
671 #
672 set(HAVE_DECL_ETHER_NTOHOST TRUE)
673 endif()
674 #
675 # Did that succeed?
676 #
677 if(NOT HAVE_DECL_ETHER_NTOHOST)
678 #
679 # No - how about <netinet/ether.h>, as on Linux?
680 #
681 # This test fails if we don't have <netinet/ether.h>
682 # or if we do but it doesn't declare ether_ntohost().
683 #
684 check_symbol_exists(ether_ntohost netinet/ether.h NETINET_ETHER_H_DECLARES_ETHER_NTOHOST)
685 if(NETINET_ETHER_H_DECLARES_ETHER_NTOHOST)
686 #
687 # Yes - we have it declared.
688 #
689 set(HAVE_DECL_ETHER_NTOHOST TRUE)
690 endif()
691 endif()
692 #
693 # Did that succeed?
694 #
695 if(NOT HAVE_DECL_ETHER_NTOHOST)
696 #
697 # No - how about <sys/ethernet.h>, as on Solaris 10 and later?
698 #
699 # This test fails if we don't have <sys/ethernet.h>
700 # or if we do but it doesn't declare ether_ntohost().
701 #
702 check_symbol_exists(ether_ntohost sys/ethernet.h SYS_ETHERNET_H_DECLARES_ETHER_NTOHOST)
703 if(SYS_ETHERNET_H_DECLARES_ETHER_NTOHOST)
704 #
705 # Yes - we have it declared.
706 #
707 set(HAVE_DECL_ETHER_NTOHOST TRUE)
708 endif()
709 endif()
710 #
711 # Did that succeed?
712 #
713 if(NOT HAVE_DECL_ETHER_NTOHOST)
714 #
715 # No, how about <arpa/inet.h>, as on AIX?
716 #
717 # This test fails if we don't have <arpa/inet.h>
718 # or if we do but it doesn't declare ether_ntohost().
719 #
720 check_symbol_exists(ether_ntohost arpa/inet.h ARPA_INET_H_DECLARES_ETHER_NTOHOST)
721 if(ARPA_INET_H_DECLARES_ETHER_NTOHOST)
722 #
723 # Yes - we have it declared.
724 #
725 set(HAVE_DECL_ETHER_NTOHOST TRUE)
726 endif()
727 endif()
728 #
729 # Did that succeed?
730 #
731 if(NOT HAVE_DECL_ETHER_NTOHOST)
732 #
733 # No, how about <netinet/if_ether.h>?
734 # On some platforms, it requires <net/if.h> and
735 # <netinet/in.h>, and we always include it with
736 # both of them, so test it with both of them.
737 #
738 # This test fails if we don't have <netinet/if_ether.h>
739 # and the headers we include before it, or if we do but
740 # <netinet/if_ether.h> doesn't declare ether_ntohost().
741 #
742 check_symbol_exists(ether_ntohost "sys/types.h;sys/socket.h;net/if.h;netinet/in.h;netinet/if_ether.h" NETINET_IF_ETHER_H_DECLARES_ETHER_NTOHOST)
743 if(NETINET_IF_ETHER_H_DECLARES_ETHER_NTOHOST)
744 #
745 # Yes - we have it declared.
746 #
747 set(HAVE_DECL_ETHER_NTOHOST TRUE)
748 endif()
749 endif()
750 #
751 # After all that, is ether_ntohost() declared?
752 #
753 if(NOT HAVE_DECL_ETHER_NTOHOST)
754 #
755 # No, we'll have to declare it ourselves.
756 # Do we have "struct ether_addr" if we include<netinet/if_ether.h>?
757 #
758 check_struct_has_member("struct ether_addr" octet "sys/types.h;sys/socket.h;net/if.h;netinet/in.h;netinet/if_ether.h" HAVE_STRUCT_ETHER_ADDR)
759 endif()
760 endif()
761 cmake_pop_check_state()
762
763 #
764 # Data types.
765 #
766 # XXX - there's no check_struct() macro that's like check_struct_has_member()
767 # except that it only checks for the existence of the structure type,
768 # so we use check_struct_has_member() and look for ss_family.
769 #
770
771 #
772 # Check for IPv6 support.
773 # We just check for AF_INET6 and struct in6_addr.
774 #
775 cmake_push_check_state()
776 if(WIN32)
777 set(CMAKE_EXTRA_INCLUDE_FILES sys/types.h ws2tcpip.h)
778 check_symbol_exists(AF_INET6 "sys/types.h;ws2tcpip.h" HAVE_AF_INET6)
779 else(WIN32)
780 set(CMAKE_EXTRA_INCLUDE_FILES sys/types.h sys/socket.h netinet/in.h)
781 check_symbol_exists(AF_INET6 "sys/types.h;sys/socket.h;netinet/in.h" HAVE_AF_INET6)
782 endif(WIN32)
783 check_type_size("struct in6_addr" HAVE_STRUCT_IN6_ADDR)
784 cmake_pop_check_state()
785 if(HAVE_AF_INET6 AND HAVE_STRUCT_IN6_ADDR)
786 set(HAVE_OS_IPV6_SUPPORT TRUE)
787 endif(HAVE_AF_INET6 AND HAVE_STRUCT_IN6_ADDR)
788
789 ######################################
790 # External dependencies
791 ######################################
792
793 #
794 # libpcap/WinPcap/Npcap.
795 # First, find it.
796 #
797 find_package(PCAP REQUIRED)
798 include_directories(${PCAP_INCLUDE_DIRS})
799
800 cmake_push_check_state()
801
802 #
803 # Now check headers.
804 #
805 set(CMAKE_REQUIRED_INCLUDES ${PCAP_INCLUDE_DIRS})
806
807 #
808 # Check whether we have pcap/pcap-inttypes.h.
809 # If we do, we use that to get the C99 types defined.
810 #
811 check_include_file(pcap/pcap-inttypes.h HAVE_PCAP_PCAP_INTTYPES_H)
812
813 #
814 # At compile time HAVE_PCAP_FINDALLDEVS depends on HAVE_PCAP_IF_T.
815 #
816 cmake_push_check_state()
817 set(CMAKE_EXTRA_INCLUDE_FILES pcap.h)
818 check_type_size(pcap_if_t PCAP_IF_T)
819 cmake_pop_check_state()
820
821 #
822 # Check for various functions in libpcap/WinPcap/Npcap.
823 #
824 cmake_push_check_state()
825 set(CMAKE_REQUIRED_LIBRARIES ${PCAP_LIBRARIES})
826
827 #
828 # Check for "pcap_list_datalinks()" and use a substitute version if
829 # it's not present. If it is present, check for "pcap_free_datalinks()";
830 # if it's not present, we don't replace it for now. (We could do so
831 # on UN*X, but not on Windows, where hilarity ensues if a program
832 # built with one version of the MSVC support library tries to free
833 # something allocated by a library built with another version of
834 # the MSVC support library.)
835 #
836 check_function_exists(pcap_list_datalinks HAVE_PCAP_LIST_DATALINKS)
837 if(HAVE_PCAP_LIST_DATALINKS)
838 check_function_exists(pcap_free_datalinks HAVE_PCAP_FREE_DATALINKS)
839 endif(HAVE_PCAP_LIST_DATALINKS)
840
841 #
842 # Check for "pcap_datalink_name_to_val()", and use a substitute
843 # version if it's not present. If it is present, check for
844 # "pcap_datalink_val_to_description()", and if we don't have it,
845 # use a substitute version.
846 #
847 check_function_exists(pcap_datalink_name_to_val HAVE_PCAP_DATALINK_NAME_TO_VAL)
848 if(HAVE_PCAP_DATALINK_NAME_TO_VAL)
849 check_function_exists(pcap_datalink_val_to_description HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION)
850 endif(HAVE_PCAP_DATALINK_NAME_TO_VAL)
851
852 #
853 # Check for "pcap_set_datalink()"; you can't substitute for it if
854 # it's absent (it has hooks into libpcap), so just define the
855 # HAVE_ value if it's there.
856 #
857 check_function_exists(pcap_set_datalink HAVE_PCAP_SET_DATALINK)
858
859 #
860 # Check for "pcap_breakloop()"; you can't substitute for it if
861 # it's absent (it has hooks into the live capture routines),
862 # so just define the HAVE_ value if it's there.
863 #
864 check_function_exists(pcap_breakloop HAVE_PCAP_BREAKLOOP)
865
866 #
867 # Check for "pcap_dump_ftell()"; we use a substitute version
868 # if it's not present.
869 #
870 check_function_exists(pcap_dump_ftell HAVE_PCAP_DUMP_FTELL)
871
872 #
873 # Do we have the new open API? Check for pcap_create() and for
874 # pcap_statustostr(), and assume that, if we have both of them,
875 # we also have pcap_activate() and the other new routines
876 # introduced in libpcap 1.0.0. (We check for pcap_statustostr()
877 # as well, because WinPcap 4.1.3 screwed up and exported pcap_create()
878 # but not other routines such as pcap_statustostr(), even though it
879 # defined them and even though you really want pcap_statustostr() to
880 # get strings corresponding to some of the status returns from the
881 # new routines.)
882 #
883 check_function_exists(pcap_statustostr HAVE_PCAP_STATUSTOSTR)
884 #
885 # If we don't have pcap_statustostr(), don't check for pcap_create(),
886 # so we pretend we don't have it.
887 #
888 if(HAVE_PCAP_STATUSTOSTR)
889 check_function_exists(pcap_create HAVE_PCAP_CREATE)
890 endif(HAVE_PCAP_STATUSTOSTR)
891 if(HAVE_PCAP_CREATE)
892 #
893 # OK, do we have pcap_set_tstamp_type? If so, assume we have
894 # pcap_list_tstamp_types and pcap_free_tstamp_types as well.
895 #
896 check_function_exists(pcap_set_tstamp_type HAVE_PCAP_SET_TSTAMP_TYPE)
897
898 #
899 # And do we have pcap_set_tstamp_precision? If so, we assume
900 # we also have pcap_open_offline_with_tstamp_precision.
901 #
902 check_function_exists(pcap_set_tstamp_precision HAVE_PCAP_SET_TSTAMP_PRECISION)
903 endif(HAVE_PCAP_CREATE)
904
905 #
906 # Check for a miscellaneous collection of functions which we use
907 # if we have them.
908 #
909 check_function_exists(pcap_findalldevs HAVE_PCAP_FINDALLDEVS)
910 check_function_exists(pcap_dump_flush HAVE_PCAP_DUMP_FLUSH)
911 check_function_exists(pcap_lib_version HAVE_PCAP_LIB_VERSION)
912 if(NOT HAVE_PCAP_LIB_VERSION)
913 # Check for the pcap_version string variable and set HAVE_PCAP_VERSION
914 endif(NOT HAVE_PCAP_LIB_VERSION)
915 check_function_exists(pcap_setdirection HAVE_PCAP_SETDIRECTION)
916 check_function_exists(pcap_set_immediate_mode HAVE_PCAP_SET_IMMEDIATE_MODE)
917 check_function_exists(pcap_dump_ftell64 HAVE_PCAP_DUMP_FTELL64)
918 #
919 # macOS Sonoma's libpcap includes stub versions of the remote-
920 # capture APIs. They are exported as "weakly linked symbols".
921 #
922 # Xcode 15 offers only a macOS Sonoma SDK, which has a .tbd
923 # file for libpcap that claims it includes those APIs. (Newer
924 # versions of macOS don't provide the system shared libraries,
925 # they only provide the dyld shared cache containing those
926 # libraries, so the OS provides SDKs that include a .tbd file
927 # to use when linking.)
928 #
929 # This means that check_function_exists() will think that
930 # the remote-capture APIs are present, including pcap_open()
931 # and pcap_findalldevs_ex().
932 #
933 # However, they are *not* present in macOS Ventura and earlier,
934 # which means that building on Ventura with Xcode 15 produces
935 # executables that fail to start because one of those APIs
936 # isn't found in the system libpcap.
937 #
938 # Protecting calls to those APIs with __builtin_available()
939 # does not prevent this, because the libpcap header files
940 # in the Sonoma SDK mark them as being first available
941 # in macOS 10.13, just like all the other routines introduced
942 # in libpcap 1.9, even though they're only available if libpcap
943 # is built with remote capture enabled or stub routines are
944 # provided. (A fix to enable this has been checked into the
945 # libpcap repository, and may end up in a later version of
946 # the SDK.)
947 #
948 # Given all that, and given that the versions of the
949 # remote-capture APIs in Sonoma are stubs that always fail,
950 # there doesn't seem to be any point in checking for pcap_open()
951 # and pcap_findalldevs_ex() if we're linking against the Apple libpcap.
952 #
953 # However, if we're *not* linking against the Apple libpcap,
954 # we should check for it, so that we can use it if it's present.
955 #
956 # So we check for pcap_open() and pcap_findalldevs_ex() if 1) this isn't
957 # macOS or 2) the the libpcap we found is not a system library, meaning
958 # that its path begins neither with /usr/lib (meaning it's a system
959 # dylib) nor /Application/Xcode.app (meaning it's a file in
960 # the Xcode SDK).
961 #
962 if(NOT APPLE OR NOT
963 (PCAP_LIBRARIES MATCHES "/usr/lib/.*" OR
964 PCAP_LIBRARIES MATCHES "/Application/Xcode.app/.*"))
965 check_function_exists(pcap_open HAVE_PCAP_OPEN)
966 check_function_exists(pcap_findalldevs_ex HAVE_PCAP_FINDALLDEVS_EX)
967 endif()
968
969 #
970 # On Windows, check for pcap_wsockinit(); if we don't have it, check for
971 # wsockinit().
972 #
973 if(WIN32)
974 check_function_exists(pcap_wsockinit HAVE_PCAP_WSOCKINIT)
975 if(NOT HAVE_PCAP_WSOCKINIT)
976 check_function_exists(wsockinit HAVE_WSOCKINIT)
977 endif(NOT HAVE_PCAP_WSOCKINIT)
978 endif(WIN32)
979
980 #
981 # Check for special debugging functions
982 #
983 check_function_exists(pcap_set_parser_debug HAVE_PCAP_SET_PARSER_DEBUG)
984 if(NOT HAVE_PCAP_SET_PARSER_DEBUG)
985 # Check whether libpcap defines pcap_debug or yydebug
986 check_variable_exists(pcap_debug HAVE_PCAP_DEBUG)
987 if(NOT HAVE_PCAP_DEBUG)
988 check_variable_exists(yydebug HAVE_YYDEBUG)
989 endif(NOT HAVE_PCAP_DEBUG)
990 endif(NOT HAVE_PCAP_SET_PARSER_DEBUG)
991
992 check_function_exists(pcap_set_optimizer_debug HAVE_PCAP_SET_OPTIMIZER_DEBUG)
993 check_function_exists(bpf_dump HAVE_BPF_DUMP)
994
995 cmake_pop_check_state()
996
997 #
998 # We have libpcap.
999 #
1000 include_directories(SYSTEM ${PCAP_INCLUDE_DIRS})
1001 set(TCPDUMP_LINK_LIBRARIES ${PCAP_LIBRARIES} ${TCPDUMP_LINK_LIBRARIES})
1002
1003 #
1004 # Optional libraries.
1005 #
1006
1007 #
1008 # libsmi.
1009 #
1010 if(WITH_SMI)
1011 find_package(SMI)
1012 if(SMI_FOUND)
1013 include_directories(SYSTEM ${SMI_INCLUDE_DIRS})
1014 set(TCPDUMP_LINK_LIBRARIES ${TCPDUMP_LINK_LIBRARIES} ${SMI_LIBRARIES})
1015 set(USE_LIBSMI ON)
1016 endif(SMI_FOUND)
1017 endif(WITH_SMI)
1018
1019 #
1020 # OpenSSL/libressl libcrypto.
1021 #
1022 if(WITH_CRYPTO)
1023 find_package(CRYPTO)
1024 if(CRYPTO_FOUND)
1025 #
1026 # 1) do we have EVP_CIPHER_CTX_new?
1027 # If so, we use it to allocate an EVP_CIPHER_CTX, as
1028 # EVP_CIPHER_CTX may be opaque; otherwise, we allocate
1029 # it ourselves.
1030 #
1031 cmake_push_check_state()
1032 set(CMAKE_REQUIRED_LIBRARIES "${CRYPTO_LIBRARIES}")
1033
1034 check_function_exists(EVP_CIPHER_CTX_new HAVE_EVP_CIPHER_CTX_NEW)
1035
1036 #
1037 # 2) do we have EVP_DecryptInit_ex()?
1038 # If so, we use it, because we need to be able to make two
1039 # "initialize the cipher" calls, one with the cipher and key,
1040 # and one with the IV, and, as of OpenSSL 1.1, You Can't Do That
1041 # with EVP_DecryptInit(), because a call to EVP_DecryptInit() will
1042 # unconditionally clear the context, and if you don't supply a
1043 # cipher, it'll clear the cipher, rendering the context unusable
1044 # and causing a crash.
1045 #
1046 check_function_exists(EVP_DecryptInit_ex HAVE_EVP_DECRYPTINIT_EX)
1047
1048 cmake_pop_check_state()
1049
1050 #
1051 # We have libcrypto.
1052 #
1053 include_directories(SYSTEM ${CRYPTO_INCLUDE_DIRS})
1054 set(TCPDUMP_LINK_LIBRARIES ${TCPDUMP_LINK_LIBRARIES} ${CRYPTO_LIBRARIES})
1055 set(HAVE_LIBCRYPTO ON)
1056 endif(CRYPTO_FOUND)
1057 endif(WITH_CRYPTO)
1058
1059 #
1060 # Capsicum sandboxing.
1061 # Some of this is in the system library, some of it is in other libraries.
1062 #
1063 if(WITH_CAPSICUM)
1064 check_include_files("sys/capsicum.h" HAVE_SYS_CAPSICUM_H)
1065 if(HAVE_SYS_CAPSICUM_H)
1066 check_function_exists(cap_enter HAVE_CAP_ENTER)
1067 check_function_exists(cap_rights_limit HAVE_CAP_RIGHTS_LIMIT)
1068 check_function_exists(cap_ioctls_limit HAVE_CAP_IOCTLS_LIMIT)
1069 check_function_exists(openat HAVE_OPENAT)
1070 if(HAVE_CAP_ENTER AND HAVE_CAP_RIGHTS_LIMIT AND
1071 HAVE_CAP_IOCTLS_LIMIT AND HAVE_OPENAT)
1072 #
1073 # OK, we have the functions we need to support Capsicum.
1074 #
1075 set(HAVE_CAPSICUM TRUE)
1076
1077 #
1078 # OK, can we use Casper?
1079 #
1080 check_library_exists(casper cap_init "" HAVE_CAP_INIT)
1081 if(HAVE_CAP_INIT)
1082 cmake_push_check_state()
1083 set(CMAKE_REQUIRED_LIBRARIES casper)
1084 check_library_exists(cap_dns cap_gethostbyaddr "" HAVE_CAP_GETHOSTBYADDR)
1085 cmake_pop_check_state()
1086 if(HAVE_CAP_GETHOSTBYADDR)
1087 set(HAVE_CASPER TRUE)
1088 set(TCPDUMP_LINK_LIBRARIES ${TCPDUMP_LINK_LIBRARIES} casper cap_dns)
1089 endif(HAVE_CAP_GETHOSTBYADDR)
1090 endif(HAVE_CAP_INIT)
1091 endif(HAVE_CAP_ENTER AND HAVE_CAP_RIGHTS_LIMIT AND
1092 HAVE_CAP_IOCTLS_LIMIT AND HAVE_OPENAT)
1093 endif(HAVE_SYS_CAPSICUM_H)
1094 endif(WITH_CAPSICUM)
1095
1096 #
1097 # libcap-ng.
1098 #
1099 if(WITH_CAP_NG)
1100 check_include_file(cap-ng.h HAVE_CAP_NG_H)
1101 check_library_exists(cap-ng capng_change_id "" HAVE_LIBCAP_NG)
1102 if(HAVE_LIBCAP_NG)
1103 set(TCPDUMP_LINK_LIBRARIES ${TCPDUMP_LINK_LIBRARIES} cap-ng)
1104 endif(HAVE_LIBCAP_NG)
1105 endif(WITH_CAP_NG)
1106
1107 ###################################################################
1108 # Warning options
1109 ###################################################################
1110
1111 #
1112 # Check and add warning options if we have a .devel file.
1113 #
1114 if(EXISTS ${CMAKE_SOURCE_DIR}/.devel OR EXISTS ${CMAKE_BINARY_DIR}/.devel)
1115 #
1116 # Warning options.
1117 #
1118 if(MSVC AND NOT ${CMAKE_C_COMPILER} MATCHES "clang*")
1119 #
1120 # MSVC, with Microsoft's front end and code generator.
1121 # "MSVC" is also set for Microsoft's compiler with a Clang
1122 # front end and their code generator ("Clang/C2"), so we
1123 # check for clang.exe and treat that differently.
1124 #
1125 check_and_add_compiler_option(-Wall)
1126 #
1127 # Disable some pointless warnings that /Wall turns on.
1128 #
1129 # Unfortunately, MSVC does not appear to have an equivalent
1130 # to "__attribute__((unused))" to mark a particular function
1131 # parameter as being known to be unused, so that the compiler
1132 # won't warn about it (for example, the function might have
1133 # that parameter because a pointer to it is being used, and
1134 # the signature of that function includes that parameter).
1135 # C++ lets you give a parameter a type but no name, but C
1136 # doesn't have that.
1137 #
1138 check_and_add_compiler_option(-wd4100)
1139 #
1140 # In theory, we care whether somebody uses f() rather than
1141 # f(void) to declare a function with no arguments, but, in
1142 # practice, there are places in the Windows header files
1143 # that appear to do that, so we squelch that warning.
1144 #
1145 check_and_add_compiler_option(-wd4255)
1146 #
1147 # Windows FD_SET() generates this, so we suppress it.
1148 #
1149 check_and_add_compiler_option(-wd4548)
1150 #
1151 # Perhaps testing something #defined to be 0 with #ifdef is an
1152 # error, and it should be tested with #if, but perhaps it's
1153 # not, and Microsoft does that in its headers, so we squelch
1154 # that warning.
1155 #
1156 check_and_add_compiler_option(-wd4574)
1157 #
1158 # The Windows headers also test not-defined values in #if, so
1159 # we don't want warnings about that, either.
1160 #
1161 check_and_add_compiler_option(-wd4668)
1162 #
1163 # We do *not* care whether some function is, or isn't, going to be
1164 # expanded inline.
1165 #
1166 check_and_add_compiler_option(-wd4710)
1167 check_and_add_compiler_option(-wd4711)
1168 #
1169 # We do *not* care whether we're adding padding bytes after
1170 # structure members.
1171 #
1172 check_and_add_compiler_option(-wd4820)
1173 #
1174 # We do *not* care about every single place the compiler would
1175 # have inserted Spectre mitigation if only we had told it to
1176 # do so with /Qspectre. I guess the theory is that it's seeing
1177 # bounds checks that would prevent out-of-bounds loads and that
1178 # those out-of-bounds loads could be done speculatively and that
1179 # the Spectre attack could detect the value of the out-of-bounds
1180 # data *if* it's within our address space, but unless I'm
1181 # missing something I don't see that as being any form of
1182 # security hole.
1183 #
1184 # XXX - add /Qspectre if that is really worth doing.
1185 #
1186 check_and_add_compiler_option(-wd5045)
1187 #
1188 # We do *not* care whether a structure had padding added at
1189 # the end because of __declspec(align) - *we* don't use
1190 # __declspec(align), because the only structures whose layout
1191 # we precisely specify are those that get overlaid on packet
1192 # data, and in those every element is an array of octets so
1193 # that we have full control over the size and alignment, and,
1194 # apparently, jmp_buf has such a declaration on x86, meaning
1195 # that everything that includes netdissect.h, i.e. almost every
1196 # file in tcpdump, gets a warning.
1197 #
1198 check_and_add_compiler_option(-wd4324)
1199 else()
1200 #
1201 # Other compilers, including MSVC with a Clang front end and
1202 # Microsoft's code generator. We currently treat them as if
1203 # they might support GCC-style -W options.
1204 #
1205 check_and_add_compiler_option(-W)
1206 check_and_add_compiler_option(-Wall)
1207 check_and_add_compiler_option(-Wassign-enum)
1208 check_and_add_compiler_option(-Wcast-qual)
1209 check_and_add_compiler_option(-Wmissing-prototypes)
1210 check_and_add_compiler_option(-Wmissing-variable-declarations)
1211 check_and_add_compiler_option(-Wold-style-definition)
1212 if(NOT CMAKE_C_COMPILER_ID MATCHES "Sun")
1213 # In Sun C versions that implement GCC compatibility "-Wpedantic"
1214 # means the same as "-pedantic". The latter is mutually exclusive
1215 # with several other options. One of those is "-xc99", which has
1216 # already been set for Sun C above.
1217 check_and_add_compiler_option(-Wpedantic)
1218 endif()
1219 check_and_add_compiler_option(-Wpointer-arith)
1220 check_and_add_compiler_option(-Wpointer-sign)
1221 check_and_add_compiler_option(-Wshadow)
1222 check_and_add_compiler_option(-Wsign-compare)
1223 check_and_add_compiler_option(-Wstrict-prototypes)
1224 check_and_add_compiler_option(-Wundef)
1225 check_and_add_compiler_option(-Wunreachable-code-return)
1226 check_and_add_compiler_option(-Wused-but-marked-unused)
1227 check_and_add_compiler_option(-Wwrite-strings)
1228 endif()
1229 endif()
1230
1231 #
1232 # Extra compiler options for the build matrix scripts to request -Werror or
1233 # its equivalent if required. The CMake variable name cannot be CFLAGS
1234 # because that is already used for a different purpose in CMake. Example
1235 # usage: cmake -DEXTRA_CFLAGS='-Wall -Wextra -Werror' ...
1236 #
1237 if(NOT "${EXTRA_CFLAGS}" STREQUAL "")
1238 # The meaning of EXTRA_CFLAGS is "use the exact specified options, or the
1239 # build risks failing to fail", not "try every specified option, omit those
1240 # that do not work and use the rest". Thus use add_compile_options(), not
1241 # foreach()/check_and_add_compiler_option(). Another reason to do that is
1242 # that the effect lasts in testprogs/ and testprogs/fuzz/.
1243 string(REPLACE " " ";" _extra_cflags_list ${EXTRA_CFLAGS})
1244 add_compile_options(${_extra_cflags_list})
1245 message(STATUS "Added extra compile options (${EXTRA_CFLAGS})")
1246 endif()
1247
1248 ######################################
1249 # Input files
1250 ######################################
1251
1252 if(ENABLE_SMB)
1253 #
1254 # We allow the SMB dissector to be omitted.
1255 #
1256 set(LOCALSRC ${LOCALSRC}
1257 print-smb.c
1258 smbutil.c)
1259 endif(ENABLE_SMB)
1260
1261 set(NETDISSECT_SOURCE_LIST_C
1262 addrtoname.c
1263 addrtostr.c
1264 af.c
1265 ascii_strcasecmp.c
1266 checksum.c
1267 cpack.c
1268 gmpls.c
1269 in_cksum.c
1270 ipproto.c
1271 l2vpn.c
1272 machdep.c
1273 netdissect.c
1274 netdissect-alloc.c
1275 nlpid.c
1276 oui.c
1277 ntp.c
1278 parsenfsfh.c
1279 print.c
1280 print-802_11.c
1281 print-802_15_4.c
1282 print-ah.c
1283 print-ahcp.c
1284 print-aodv.c
1285 print-aoe.c
1286 print-ap1394.c
1287 print-arcnet.c
1288 print-arista.c
1289 print-arp.c
1290 print-ascii.c
1291 print-atalk.c
1292 print-atm.c
1293 print-babel.c
1294 print-bcm-li.c
1295 print-beep.c
1296 print-bfd.c
1297 print-bgp.c
1298 print-bootp.c
1299 print-brcmtag.c
1300 print-bt.c
1301 print-calm-fast.c
1302 print-carp.c
1303 print-cdp.c
1304 print-cfm.c
1305 print-chdlc.c
1306 print-cip.c
1307 print-cnfp.c
1308 print-dccp.c
1309 print-decnet.c
1310 print-dhcp6.c
1311 print-domain.c
1312 print-dsa.c
1313 print-dtp.c
1314 print-dvmrp.c
1315 print-eap.c
1316 print-egp.c
1317 print-eigrp.c
1318 print-enc.c
1319 print-esp.c
1320 print-ether.c
1321 print-fddi.c
1322 print-forces.c
1323 print-fr.c
1324 print-frag6.c
1325 print-ftp.c
1326 print-geneve.c
1327 print-geonet.c
1328 print-gre.c
1329 print-hncp.c
1330 print-hsrp.c
1331 print-http.c
1332 print-icmp.c
1333 print-icmp6.c
1334 print-igmp.c
1335 print-igrp.c
1336 print-ip-demux.c
1337 print-ip.c
1338 print-ip6.c
1339 print-ip6opts.c
1340 print-ipcomp.c
1341 print-ipfc.c
1342 print-ipnet.c
1343 print-ipoib.c
1344 print-ipx.c
1345 print-isakmp.c
1346 print-isoclns.c
1347 print-juniper.c
1348 print-krb.c
1349 print-l2tp.c
1350 print-lane.c
1351 print-ldp.c
1352 print-lisp.c
1353 print-llc.c
1354 print-lldp.c
1355 print-lmp.c
1356 print-loopback.c
1357 print-lspping.c
1358 print-lwapp.c
1359 print-lwres.c
1360 print-m3ua.c
1361 print-macsec.c
1362 print-mobile.c
1363 print-mobility.c
1364 print-mpcp.c
1365 print-mpls.c
1366 print-mptcp.c
1367 print-msdp.c
1368 print-msnlb.c
1369 print-nflog.c
1370 print-nfs.c
1371 print-nsh.c
1372 print-ntp.c
1373 print-null.c
1374 print-olsr.c
1375 print-openflow-1.0.c
1376 print-openflow-1.3.c
1377 print-openflow.c
1378 print-ospf.c
1379 print-ospf6.c
1380 print-otv.c
1381 print-pflog.c
1382 print-pgm.c
1383 print-pim.c
1384 print-pktap.c
1385 print-ppi.c
1386 print-ppp.c
1387 print-pppoe.c
1388 print-pptp.c
1389 print-ptp.c
1390 print-radius.c
1391 print-raw.c
1392 print-realtek.c
1393 print-resp.c
1394 print-rip.c
1395 print-ripng.c
1396 print-rpki-rtr.c
1397 print-rsvp.c
1398 print-rt6.c
1399 print-rtsp.c
1400 print-rx.c
1401 print-sctp.c
1402 print-sflow.c
1403 print-sip.c
1404 print-sl.c
1405 print-sll.c
1406 print-slow.c
1407 print-smtp.c
1408 print-snmp.c
1409 print-someip.c
1410 print-ssh.c
1411 print-stp.c
1412 print-sunatm.c
1413 print-sunrpc.c
1414 print-symantec.c
1415 print-syslog.c
1416 print-tcp.c
1417 print-telnet.c
1418 print-tftp.c
1419 print-timed.c
1420 print-tipc.c
1421 print-token.c
1422 print-udld.c
1423 print-udp.c
1424 print-unsupported.c
1425 print-usb.c
1426 print-vjc.c
1427 print-vqp.c
1428 print-vrrp.c
1429 print-vsock.c
1430 print-vtp.c
1431 print-vxlan-gpe.c
1432 print-vxlan.c
1433 print-wb.c
1434 print-whois.c
1435 print-zep.c
1436 print-zephyr.c
1437 print-zeromq.c
1438 ${LOCALSRC}
1439 signature.c
1440 strtoaddr.c
1441 util-print.c
1442 )
1443
1444 #
1445 # Replace missing functions
1446 #
1447 foreach(FUNC strlcat strlcpy strdup strsep getservent getopt_long)
1448 string(TOUPPER ${FUNC} FUNC_UPPERCASE)
1449 set(HAVE_FUNC_UPPERCASE HAVE_${FUNC_UPPERCASE})
1450 if(NOT ${HAVE_FUNC_UPPERCASE})
1451 set(NETDISSECT_SOURCE_LIST_C ${NETDISSECT_SOURCE_LIST_C} missing/${FUNC}.c)
1452 endif()
1453 endforeach()
1454
1455 add_library(netdissect STATIC
1456 ${NETDISSECT_SOURCE_LIST_C}
1457 )
1458 if(NOT C_ADDITIONAL_FLAGS STREQUAL "")
1459 set_target_properties(netdissect PROPERTIES COMPILE_FLAGS ${C_ADDITIONAL_FLAGS})
1460 endif()
1461
1462 set(TCPDUMP_SOURCE_LIST_C fptype.c tcpdump.c)
1463
1464 if(NOT HAVE_BPF_DUMP)
1465 set(TCPDUMP_SOURCE_LIST_C ${TCPDUMP_SOURCE_LIST_C} bpf_dump.c)
1466 endif(NOT HAVE_BPF_DUMP)
1467 if(NOT HAVE_PCAP_DUMP_FTELL)
1468 set(TCPDUMP_SOURCE_LIST_C ${TCPDUMP_SOURCE_LIST_C} missing/pcap_dump_ftell.c)
1469 endif(NOT HAVE_PCAP_DUMP_FTELL)
1470
1471 if(NOT HAVE_PCAP_LIST_DATALINKS)
1472 set(TCPDUMP_SOURCE_LIST_C ${TCPDUMP_SOURCE_LIST_C} missing/datalinks.c)
1473 endif(NOT HAVE_PCAP_LIST_DATALINKS)
1474
1475 if((NOT HAVE_PCAP_DATALINK_NAME_TO_VAL) OR (NOT HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION))
1476 set(TCPDUMP_SOURCE_LIST_C ${TCPDUMP_SOURCE_LIST_C} missing/dlnames.c)
1477 endif((NOT HAVE_PCAP_DATALINK_NAME_TO_VAL) OR (NOT HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION))
1478
1479 set(PROJECT_SOURCE_LIST_C ${NETDISSECT_SOURCE_LIST_C} ${TCPDUMP_SOURCE_LIST_C})
1480
1481 file(GLOB PROJECT_SOURCE_LIST_H
1482 *.h
1483 )
1484
1485 #
1486 # Assume, by default, no support for shared libraries and V7/BSD
1487 # convention for man pages (devices in section 4, file formats in
1488 # section 5, miscellaneous info in section 7, administrative commands
1489 # and daemons in section 8). Individual cases can override this.
1490 # Individual cases can override this.
1491 #
1492 set(MAN_FILE_FORMATS 5)
1493 set(MAN_MISC_INFO 7)
1494 if(CMAKE_SYSTEM_NAME STREQUAL "AIX")
1495 # Workaround to enable certain features
1496 set(_SUN TRUE)
1497 elseif(CMAKE_SYSTEM_NAME STREQUAL "HP-UX")
1498 #
1499 # Use System V conventions for man pages.
1500 #
1501 set(MAN_FILE_FORMATS 4)
1502 set(MAN_MISC_INFO 5)
1503 elseif(CMAKE_SYSTEM_NAME STREQUAL "IRIX" OR CMAKE_SYSTEM_NAME STREQUAL "IRIX64")
1504 #
1505 # Use IRIX conventions for man pages; they're the same as the
1506 # System V conventions, except that they use section 8 for
1507 # administrative commands and daemons.
1508 #
1509 set(MAN_FILE_FORMATS 4)
1510 set(MAN_MISC_INFO 5)
1511 elseif(CMAKE_SYSTEM_NAME STREQUAL "OSF1")
1512 #
1513 # DEC OSF/1, a/k/a Digital UNIX, a/k/a Tru64 UNIX.
1514 # Use Tru64 UNIX conventions for man pages; they're the same as the
1515 # System V conventions except that they use section 8 for
1516 # administrative commands and daemons.
1517 #
1518 set(MAN_FILE_FORMATS 4)
1519 set(MAN_MISC_INFO 5)
1520 elseif(CMAKE_SYSTEM_NAME STREQUAL "SunOS" AND CMAKE_SYSTEM_VERSION MATCHES "5[.][0-9.]*")
1521 #
1522 # SunOS 5.x.
1523 #
1524 if(CMAKE_SYSTEM_VERSION STREQUAL "5.12")
1525 else()
1526 #
1527 # Use System V conventions for man pages.
1528 #
1529 set(MAN_FILE_FORMATS 4)
1530 set(MAN_MISC_INFO 5)
1531 endif()
1532 endif()
1533
1534 source_group("Source Files" FILES ${PROJECT_SOURCE_LIST_C})
1535 source_group("Header Files" FILES ${PROJECT_SOURCE_LIST_H})
1536
1537 ######################################
1538 # Register targets
1539 ######################################
1540
1541 add_executable(tcpdump ${TCPDUMP_SOURCE_LIST_C})
1542 if(NOT C_ADDITIONAL_FLAGS STREQUAL "")
1543 set_target_properties(tcpdump PROPERTIES COMPILE_FLAGS ${C_ADDITIONAL_FLAGS})
1544 endif()
1545 if(NOT "${PCAP_LINK_FLAGS}" STREQUAL "")
1546 set_target_properties(tcpdump PROPERTIES LINK_FLAGS ${PCAP_LINK_FLAGS})
1547 endif()
1548 target_link_libraries(tcpdump netdissect ${TCPDUMP_LINK_LIBRARIES})
1549
1550 ######################################
1551 # Write out the config.h file
1552 ######################################
1553
1554 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmakeconfig.h.in ${CMAKE_CURRENT_BINARY_DIR}/config.h)
1555
1556 ######################################
1557 # Install tcpdump and man pages
1558 ######################################
1559
1560 #
1561 # "Define GNU standard installation directories", which actually
1562 # are also defined, to some degree, by autotools, and at least
1563 # some of which are general UN*X conventions.
1564 #
1565 include(GNUInstallDirs)
1566
1567 set(MAN1_EXPAND tcpdump.1.in)
1568
1569 if(WIN32)
1570 # XXX TODO where to install on Windows?
1571 else(WIN32)
1572 install(TARGETS tcpdump DESTINATION bin)
1573 endif(WIN32)
1574
1575 # On UN*X, and on Windows when not using MSVC, process man pages and
1576 # arrange that they be installed.
1577 if(NOT MSVC)
1578 #
1579 # Man pages.
1580 #
1581 # For each section of the manual for which we have man pages
1582 # that require macro expansion, do the expansion.
1583 #
1584 set(MAN1 "")
1585 foreach(TEMPLATE_MANPAGE ${MAN1_EXPAND})
1586 string(REPLACE ".in" "" MANPAGE ${TEMPLATE_MANPAGE})
1587 configure_file(${CMAKE_SOURCE_DIR}/${TEMPLATE_MANPAGE} ${CMAKE_CURRENT_BINARY_DIR}/${MANPAGE} @ONLY)
1588 set(MAN1 ${MAN1} ${CMAKE_CURRENT_BINARY_DIR}/${MANPAGE})
1589 endforeach(TEMPLATE_MANPAGE)
1590 install(FILES ${MAN1} DESTINATION ${CMAKE_INSTALL_MANDIR}/man1)
1591 endif(NOT MSVC)
1592
1593 # uninstall target
1594 configure_file(
1595 "${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in"
1596 "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"
1597 IMMEDIATE @ONLY)
1598
1599 add_custom_target(uninstall
1600 COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)
1601
1602 #
1603 # Tcpdump tests
1604 # We try to find the Perl interpreter and, if we do, we have the check
1605 # rule run tests/TESTrun with it, because just trying to run the TESTrun
1606 # script as a command won't work on Windows.
1607 #
1608 find_program(PERL perl)
1609 if(PERL)
1610 message(STATUS "Found perl at ${PERL}")
1611 add_custom_target(check
1612 COMMAND ${PERL} ${CMAKE_SOURCE_DIR}/tests/TESTrun)
1613 else()
1614 message(STATUS "Didn't find perl")
1615 endif()