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