]> The Tcpdump Group git mirrors - tcpdump/blob - CMakeLists.txt
CI: Add warning exemptions for Sun C (suncc-5.14) on Solaris 10
[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 if(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
307 option(WITH_CAPSICUM "Build with Capsicum security functions, if available" ON)
308 else()
309 set(WITH_CAPSICUM OFF)
310 endif()
311 if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
312 option(WITH_CAP_NG "Use libcap-ng, if available" ON)
313 else()
314 set(WITH_CAP_NG OFF)
315 endif()
316 option(ENABLE_SMB "Build with the SMB dissector" OFF)
317
318 #
319 # String parameters. Neither of them are set, initially; only if the
320 # user explicitly configures them are they set.
321 #
322 # WITH_CHROOT is STRING, not PATH, as the directory need not exist
323 # when CMake is run.
324 #
325 set(WITH_CHROOT CACHE STRING
326 "Directory to which to chroot when dropping privileges")
327 set(WITH_USER CACHE STRING
328 "User to whom to set the UID when dropping privileges")
329
330 #
331 # By default, build universal with the appropriate set of architectures
332 # for the OS on which we're doing the build.
333 #
334 if(APPLE AND "${CMAKE_OSX_ARCHITECTURES}" STREQUAL "")
335 #
336 # Get the major version of Darwin.
337 #
338 string(REGEX MATCH "^([0-9]+)" SYSTEM_VERSION_MAJOR "${CMAKE_SYSTEM_VERSION}")
339
340 if(SYSTEM_VERSION_MAJOR EQUAL 9)
341 #
342 # Leopard. Build for x86 and 32-bit PowerPC, with
343 # x86 first. (That's what Apple does.)
344 #
345 set(CMAKE_OSX_ARCHITECTURES "i386;ppc")
346 elseif(SYSTEM_VERSION_MAJOR EQUAL 10)
347 #
348 # Snow Leopard. Build for x86-64 and x86, with
349 # x86-64 first. (That's what Apple does.)
350 #
351 set(CMAKE_OSX_ARCHITECTURES "x86_64;i386")
352 endif()
353 endif()
354
355 ###################################################################
356 # Versioning
357 # (and other variables to keep config.h consistent with Autoconf)
358 ###################################################################
359
360 # Get, parse, format and set tcpdump's version string from
361 # [tcpdump_root]/VERSION for later use.
362
363 # Get MAJOR, MINOR, PATCH & SUFFIX
364 file(STRINGS ${tcpdump_SOURCE_DIR}/VERSION
365 PACKAGE_VERSION
366 LIMIT_COUNT 1 # Read only the first line
367 )
368
369 set(PACKAGE_BUGREPORT "https://github.com/the-tcpdump-group/tcpdump/issues")
370 set(PACKAGE_NAME "tcpdump")
371 set(PACKAGE_STRING "${PACKAGE_NAME} ${PACKAGE_VERSION}")
372 set(PACKAGE_TARNAME "${PACKAGE_NAME}")
373 set(PACKAGE_URL "https://www.tcpdump.org/")
374
375 ######################################
376 # Project settings
377 ######################################
378
379 include_directories(
380 ${CMAKE_CURRENT_BINARY_DIR}
381 ${tcpdump_SOURCE_DIR}
382 )
383
384 if(MSVC)
385 add_definitions(-D__STDC__)
386 add_definitions(-D_CRT_SECURE_NO_WARNINGS)
387 endif(MSVC)
388
389 if(MSVC)
390 if (USE_STATIC_RT)
391 MESSAGE(STATUS "Use STATIC runtime")
392 set(NAME_RT MT)
393 set (CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} /MT")
394 set (CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /MT")
395 set (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT")
396 set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd")
397
398 set (CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL} /MT")
399 set (CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO} /MT")
400 set (CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /MT")
401 set (CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /MTd")
402 else (USE_STATIC_RT)
403 MESSAGE(STATUS "Use DYNAMIC runtime")
404 set(NAME_RT MD)
405 set (CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} /MD")
406 set (CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /MD")
407 set (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MD")
408 set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MDd")
409
410 set (CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL} /MD")
411 set (CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO} /MD")
412 set (CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /MD")
413 set (CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /MDd")
414 endif (USE_STATIC_RT)
415 endif(MSVC)
416
417 #
418 # CMake's definition of "cross-compiling" appears to be "compiling
419 # for an *operating system* other than the one on which the build
420 # is being done*.
421 #
422 # This is an inadequate definition, as people build for the same
423 # operating system but a different instruction set, e.g. building
424 # on an IA-32 or x86-64 Linux box for an Arm embedded Linux box,
425 # or building Arm code on an IA-32 or x86-64 Windows box.
426 #
427 # So just test whether check_c_source_runs() on a trivial program
428 # works; if not, it's probably because the generated code won't
429 # run on the platform on which we're running.
430 #
431 include(CheckCSourceRuns)
432 if (NOT CMAKE_CROSSCOMPILING)
433 check_c_source_runs("
434 int main()
435 {
436 return 0;
437 }
438 "
439 CHECK_C_SOURCE_RUNS_WORKS
440 )
441 if (NOT CHECK_C_SOURCE_RUNS_WORKS)
442 set(CMAKE_CROSSCOMPILING TRUE)
443 endif()
444 endif()
445
446 ###################################################################
447 # Detect available platform features
448 ###################################################################
449
450 include(CMakePushCheckState)
451 include(CheckIncludeFile)
452 include(CheckIncludeFiles)
453 include(CheckFunctionExists)
454 include(CheckLibraryExists)
455 include(CheckSymbolExists)
456 include(CheckStructHasMember)
457 include(CheckVariableExists)
458 include(CheckTypeSize)
459
460 #
461 # Get the size of a time_t, to know whether it's 32-bit or 64-bit. Print it.
462 #
463 cmake_push_check_state()
464 set(CMAKE_EXTRA_INCLUDE_FILES time.h)
465 check_type_size("time_t" SIZEOF_TIME_T)
466 if(SIZEOF_TIME_T EQUAL 4)
467 message(STATUS "32-bit time_t")
468 elseif(SIZEOF_TIME_T EQUAL 8)
469 message(STATUS "64-bit time_t")
470 endif()
471 cmake_pop_check_state()
472
473 #
474 # Test for ONC RPC headers.
475 #
476 # XXX - we should supply copies of these, as I think Sun released them
477 # under a sufficiently permissive license, and that would be one less
478 # place where we rely on OS headers, rather than our own headers, for
479 # a protocol that is specified independent of any particular OS.
480 #
481 check_include_file(rpc/rpc.h HAVE_RPC_RPC_H)
482 if(HAVE_RPC_RPC_H)
483 check_include_files("rpc/rpc.h;rpc/rpcent.h" HAVE_RPC_RPCENT_H)
484 endif(HAVE_RPC_RPC_H)
485
486 #
487 # Functions.
488 #
489 check_function_exists(strlcat HAVE_STRLCAT)
490 check_function_exists(strlcpy HAVE_STRLCPY)
491 check_function_exists(strsep HAVE_STRSEP)
492
493 #
494 # Find library needed for gethostbyaddr.
495 # NOTE: if you hand check_library_exists as its last argument a variable
496 # that's been set, it skips the test, so we need different variables.
497 #
498 set(TCPDUMP_LINK_LIBRARIES "")
499 if(WIN32)
500 #
501 # We need winsock2.h and ws2tcpip.h.
502 #
503 cmake_push_check_state()
504 set(CMAKE_REQUIRED_LIBRARIES ws2_32)
505 check_symbol_exists(gethostbyaddr "winsock2.h;ws2tcpip.h" LIBWS2_32_HAS_GETHOSTBYADDR)
506 cmake_pop_check_state()
507 if(LIBWS2_32_HAS_GETHOSTBYADDR)
508 set(TCPDUMP_LINK_LIBRARIES ws2_32 ${TCPDUMP_LINK_LIBRARIES})
509 else(LIBWS2_32_HAS_GETHOSTBYADDR)
510 message(FATAL_ERROR "gethostbyaddr is required, but wasn't found")
511 endif(LIBWS2_32_HAS_GETHOSTBYADDR)
512 else(WIN32)
513 check_function_exists(gethostbyaddr STDLIBS_HAVE_GETHOSTBYADDR)
514 if(NOT STDLIBS_HAVE_GETHOSTBYADDR)
515 check_library_exists(socket gethostbyaddr "" LIBSOCKET_HAS_GETHOSTBYADDR)
516 if(LIBSOCKET_HAS_GETHOSTBYADDR)
517 set(TCPDUMP_LINK_LIBRARIES ${TCPDUMP_LINK_LIBRARIES} socket)
518 else(LIBSOCKET_HAS_GETHOSTBYADDR)
519 check_library_exists(nsl gethostbyaddr "" LIBNSL_HAS_GETHOSTBYADDR)
520 if(LIBNSL_HAS_GETHOSTBYADDR)
521 set(TCPDUMP_LINK_LIBRARIES ${TCPDUMP_LINK_LIBRARIES} nsl)
522 else(LIBNSL_HAS_GETHOSTBYADDR)
523 check_library_exists(network gethostbyaddr "" LIBNETWORK_HAS_GETHOSTBYADDR)
524 if(LIBNETWORK_HAS_GETHOSTBYADDR)
525 set(TCPDUMP_LINK_LIBRARIES ${TCPDUMP_LINK_LIBRARIES} network)
526 else(LIBNETWORK_HAS_GETHOSTBYADDR)
527 message(FATAL_ERROR "gethostbyaddr is required, but wasn't found")
528 endif(LIBNETWORK_HAS_GETHOSTBYADDR)
529 endif(LIBNSL_HAS_GETHOSTBYADDR)
530 endif(LIBSOCKET_HAS_GETHOSTBYADDR)
531 endif(NOT STDLIBS_HAVE_GETHOSTBYADDR)
532 endif(WIN32)
533
534 #
535 # This may require additional libraries.
536 #
537 cmake_push_check_state()
538 set(CMAKE_REQUIRED_LIBRARIES ${TCPDUMP_LINK_LIBRARIES})
539 check_function_exists(getservent STDLIBS_HAVE_GETSERVENT)
540 if(STDLIBS_HAVE_GETSERVENT)
541 set(HAVE_GETSERVENT TRUE)
542 else(STDLIBS_HAVE_GETSERVENT)
543 #
544 # Some platforms may need -lsocket for getservent.
545 #
546 set(CMAKE_REQUIRED_LIBRARIES socket ${TCPDUMP_LINK_LIBRARIES})
547 check_function_exists(getservent LIBSOCKET_HAS_GETSERVENT)
548 if(LIBSOCKET_HAS_GETSERVENT)
549 set(HAVE_GETSERVENT TRUE)
550 set(TCPDUMP_LINK_LIBRARIES socket ${TCPDUMP_LINK_LIBRARIES})
551 endif(LIBSOCKET_HAS_GETSERVENT)
552 endif(STDLIBS_HAVE_GETSERVENT)
553 cmake_pop_check_state()
554
555 if (NOT CMAKE_CROSSCOMPILING)
556 #
557 # Require a proof of suitable snprintf(3), same as in Autoconf.
558 #
559 check_c_source_runs("
560 #include <stdio.h>
561 #include <string.h>
562 #include <inttypes.h>
563 #include <sys/types.h>
564
565 #if defined(_WIN32) && !defined(_SSIZE_T_DEFINED)
566 /*
567 * On UN*Xes, this is a signed integer type of the same size as size_t.
568 *
569 * It's not defined by Visual Studio; we assume that ptrdiff_t will
570 * be a type that is a signed integer type of the same size as size_t.
571 */
572 typedef ptrdiff_t ssize_t;
573 #endif
574
575 /*
576 * Avoid trying to cast negative values to unsigned types, or doing
577 * shifts of signed types, in order not to have the test program fail
578 * if we're building with undefined-behavior sanitizers enabled.
579 */
580 int main()
581 {
582 char buf[100];
583 unsigned int ui = sizeof(buf);
584 int i = sizeof(buf);
585 int64_t i64 = INT64_C(0x100000000);
586 uint64_t ui64 = UINT64_C(0x100000000);
587
588 snprintf(buf, sizeof(buf), \"%zu\", (size_t)ui);
589 if (strncmp(buf, \"100\", sizeof(buf)))
590 return 1;
591
592 snprintf(buf, sizeof(buf), \"%zd\", (ssize_t)(-i));
593 if (strncmp(buf, \"-100\", sizeof(buf)))
594 return 2;
595
596 snprintf(buf, sizeof(buf), \"%\" PRId64, -i64);
597 if (strncmp(buf, \"-4294967296\", sizeof(buf)))
598 return 3;
599
600 snprintf(buf, sizeof(buf), \"0o%\" PRIo64, ui64);
601 if (strncmp(buf, \"0o40000000000\", sizeof(buf)))
602 return 4;
603
604 snprintf(buf, sizeof(buf), \"0x%\" PRIx64, ui64);
605 if (strncmp(buf, \"0x100000000\", sizeof(buf)))
606 return 5;
607
608 snprintf(buf, sizeof(buf), \"%\" PRIu64, ui64);
609 if (strncmp(buf, \"4294967296\", sizeof(buf)))
610 return 6;
611
612 return 0;
613 }
614
615 "
616 SUITABLE_SNPRINTF
617 )
618 if(NOT SUITABLE_SNPRINTF)
619 message(FATAL_ERROR
620 "The snprintf(3) implementation in this libc is not suitable,
621 tcpdump would not work correctly even if it managed to compile."
622 )
623 endif()
624 else()
625 message(STATUS "Skipped SUITABLE_SNPRINTF because cross-compiling.")
626 endif()
627
628 check_function_exists(getopt_long HAVE_GETOPT_LONG)
629 #
630 # For Windows, don't need to waste time checking for fork() or vfork().
631 #
632 if(NOT WIN32)
633 check_function_exists(fork HAVE_FORK)
634 check_function_exists(vfork HAVE_VFORK)
635 endif(NOT WIN32)
636
637 #
638 # Some platforms may need -lnsl for getrpcbynumber.
639 #
640 cmake_push_check_state()
641 set(CMAKE_REQUIRED_LIBRARIES ${TCPDUMP_LINK_LIBRARIES})
642 check_function_exists(getrpcbynumber STDLIBS_HAVE_GETRPCBYNUMBER)
643 if(STDLIBS_HAVE_GETRPCBYNUMBER)
644 set(HAVE_GETRPCBYNUMBER TRUE)
645 else(STDLIBS_HAVE_GETRPCBYNUMBER)
646 set(CMAKE_REQUIRED_LIBRARIES ${TCPDUMP_LINK_LIBRARIES} nsl)
647 check_function_exists(getrpcbynumber LIBNSL_HAS_GETRPCBYNUMBER)
648 if(LIBNSL_HAS_GETRPCBYNUMBER)
649 set(HAVE_GETRPCBYNUMBER TRUE)
650 set(TCPDUMP_LINK_LIBRARIES ${TCPDUMP_LINK_LIBRARIES} nsl)
651 endif(LIBNSL_HAS_GETRPCBYNUMBER)
652 endif(STDLIBS_HAVE_GETRPCBYNUMBER)
653 cmake_pop_check_state()
654
655 #
656 # This requires the libraries we require, as ether_ntohost might be
657 # in one of those libraries. That means we have to do this after
658 # we check for those libraries.
659 #
660 # You are in a twisty little maze of UN*Xes, all different.
661 # Some might not have ether_ntohost().
662 # Some might have it and declare it in <net/ethernet.h>.
663 # Some might have it and declare it in <netinet/ether.h>
664 # Some might have it and declare it in <sys/ethernet.h>.
665 # Some might have it and declare it in <arpa/inet.h>.
666 # Some might have it and declare it in <netinet/if_ether.h>.
667 # Some might have it and not declare it in any header file.
668 #
669 # Before you is a C compiler.
670 #
671 cmake_push_check_state()
672 set(CMAKE_REQUIRED_LIBRARIES ${TCPDUMP_LINK_LIBRARIES})
673 check_function_exists(ether_ntohost HAVE_ETHER_NTOHOST)
674 if(HAVE_ETHER_NTOHOST)
675 #
676 # OK, we have ether_ntohost(). We don't check whether it's buggy,
677 # as we assume any system that has CMake is likely to be new enough
678 # that, if it has ether_ntohost(), whatever bug is checked for in
679 # autotools is fixed; we just decide to use it.
680 #
681 set(USE_ETHER_NTOHOST TRUE)
682
683 #
684 # Is it declared in <net/ethernet.h>?
685 #
686 # This test fails if we don't have <net/ethernet.h> or if we do
687 # but it doesn't declare ether_ntohost().
688 #
689 check_symbol_exists(ether_ntohost net/ethernet.h NET_ETHERNET_H_DECLARES_ETHER_NTOHOST)
690 if(NET_ETHERNET_H_DECLARES_ETHER_NTOHOST)
691 #
692 # Yes - we have it declared.
693 #
694 set(HAVE_DECL_ETHER_NTOHOST TRUE)
695 endif()
696 #
697 # Did that succeed?
698 #
699 if(NOT HAVE_DECL_ETHER_NTOHOST)
700 #
701 # No - how about <netinet/ether.h>, as on Linux?
702 #
703 # This test fails if we don't have <netinet/ether.h>
704 # or if we do but it doesn't declare ether_ntohost().
705 #
706 check_symbol_exists(ether_ntohost netinet/ether.h NETINET_ETHER_H_DECLARES_ETHER_NTOHOST)
707 if(NETINET_ETHER_H_DECLARES_ETHER_NTOHOST)
708 #
709 # Yes - we have it declared.
710 #
711 set(HAVE_DECL_ETHER_NTOHOST TRUE)
712 endif()
713 endif()
714 #
715 # Did that succeed?
716 #
717 if(NOT HAVE_DECL_ETHER_NTOHOST)
718 #
719 # No - how about <sys/ethernet.h>, as on Solaris 10 and later?
720 #
721 # This test fails if we don't have <sys/ethernet.h>
722 # or if we do but it doesn't declare ether_ntohost().
723 #
724 check_symbol_exists(ether_ntohost sys/ethernet.h SYS_ETHERNET_H_DECLARES_ETHER_NTOHOST)
725 if(SYS_ETHERNET_H_DECLARES_ETHER_NTOHOST)
726 #
727 # Yes - we have it declared.
728 #
729 set(HAVE_DECL_ETHER_NTOHOST TRUE)
730 endif()
731 endif()
732 #
733 # Did that succeed?
734 #
735 if(NOT HAVE_DECL_ETHER_NTOHOST)
736 #
737 # No, how about <arpa/inet.h>, as on AIX?
738 #
739 # This test fails if we don't have <arpa/inet.h>
740 # or if we do but it doesn't declare ether_ntohost().
741 #
742 check_symbol_exists(ether_ntohost arpa/inet.h ARPA_INET_H_DECLARES_ETHER_NTOHOST)
743 if(ARPA_INET_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 # Did that succeed?
752 #
753 if(NOT HAVE_DECL_ETHER_NTOHOST)
754 #
755 # No, how about <netinet/if_ether.h>?
756 # On some platforms, it requires <net/if.h> and
757 # <netinet/in.h>, and we always include it with
758 # both of them, so test it with both of them.
759 #
760 # This test fails if we don't have <netinet/if_ether.h>
761 # and the headers we include before it, or if we do but
762 # <netinet/if_ether.h> doesn't declare ether_ntohost().
763 #
764 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)
765 if(NETINET_IF_ETHER_H_DECLARES_ETHER_NTOHOST)
766 #
767 # Yes - we have it declared.
768 #
769 set(HAVE_DECL_ETHER_NTOHOST TRUE)
770 endif()
771 endif()
772 #
773 # After all that, is ether_ntohost() declared?
774 #
775 if(NOT HAVE_DECL_ETHER_NTOHOST)
776 #
777 # No, we'll have to declare it ourselves.
778 # Do we have "struct ether_addr" if we include<netinet/if_ether.h>?
779 #
780 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)
781 endif()
782 endif()
783 cmake_pop_check_state()
784
785 #
786 # Data types.
787 #
788 # XXX - there's no check_struct() macro that's like check_struct_has_member()
789 # except that it only checks for the existence of the structure type,
790 # so we use check_struct_has_member() and look for ss_family.
791 #
792
793 ######################################
794 # External dependencies
795 ######################################
796
797 #
798 # libpcap/WinPcap/Npcap.
799 # First, find it.
800 #
801 find_package(PCAP REQUIRED)
802 include_directories(${PCAP_INCLUDE_DIRS})
803
804 cmake_push_check_state()
805
806 #
807 # Now check headers.
808 #
809 set(CMAKE_REQUIRED_INCLUDES ${PCAP_INCLUDE_DIRS})
810
811 #
812 # Check whether we have pcap/pcap-inttypes.h.
813 # If we do, we use that to get the C99 types defined.
814 #
815 check_include_file(pcap/pcap-inttypes.h HAVE_PCAP_PCAP_INTTYPES_H)
816
817 #
818 # Check for various functions in libpcap/WinPcap/Npcap.
819 #
820 cmake_push_check_state()
821 set(CMAKE_REQUIRED_LIBRARIES ${PCAP_LIBRARIES})
822
823 #
824 # Do we have the new open API? Check for pcap_create() and for
825 # pcap_statustostr(), and assume that, if we have both of them,
826 # we also have pcap_activate() and the other new routines
827 # introduced in libpcap 1.0.
828 #
829 # We require those routines, so fail if we don't find it.
830 #
831 check_function_exists(pcap_create HAVE_PCAP_CREATE)
832 if(NOT HAVE_PCAP_CREATE)
833 if(WIN32)
834 MESSAGE(FATAL_ERROR "libpcap is too old; 1.0 or later is required")
835 else(WIN32)
836 MESSAGE(FATAL_ERROR "libpcap is too old; 1.0 or later is required - if you're using WinPcap, try Npcap")
837 endif(WIN32)
838 endif(NOT HAVE_PCAP_CREATE)
839
840 if(WIN32)
841 #
842 # We check for pcap_statustostr() as well, because WinPcap 4.1.3
843 # screwed up and exported pcap_create() but not other routines
844 # such as pcap_statustostr(), even though it defined them and
845 # even though you really want pcap_statustostr() to get strings
846 # corresponding to some of the status returns from the new routines.)
847 #
848 check_function_exists(pcap_statustostr HAVE_PCAP_STATUSTOSTR)
849 if(NOT HAVE_PCAP_STATUSTOSTR)
850 MESSAGE(FATAL_ERROR "pcap_create is available, but pcap_statustostr isn't - if you're using WinPcap, try Npcap ")
851 endif(NOT HAVE_PCAP_STATUSTOSTR)
852 endif(WIN32)
853
854 #
855 # OK, do we have pcap_set_tstamp_type? If so, assume we have
856 # pcap_list_tstamp_types and pcap_free_tstamp_types as well.
857 #
858 check_function_exists(pcap_set_tstamp_type HAVE_PCAP_SET_TSTAMP_TYPE)
859
860 #
861 # And do we have pcap_set_tstamp_precision? If so, we assume
862 # we also have pcap_open_offline_with_tstamp_precision.
863 #
864 check_function_exists(pcap_set_tstamp_precision HAVE_PCAP_SET_TSTAMP_PRECISION)
865
866 #
867 # Check for a miscellaneous collection of functions which we use
868 # if we have them.
869 #
870 check_function_exists(pcap_set_immediate_mode HAVE_PCAP_SET_IMMEDIATE_MODE)
871 check_function_exists(pcap_dump_ftell64 HAVE_PCAP_DUMP_FTELL64)
872 #
873 # macOS Sonoma's libpcap includes stub versions of the remote-
874 # capture APIs. They are exported as "weakly linked symbols".
875 #
876 # Xcode 15 offers only a macOS Sonoma SDK, which has a .tbd
877 # file for libpcap that claims it includes those APIs. (Newer
878 # versions of macOS don't provide the system shared libraries,
879 # they only provide the dyld shared cache containing those
880 # libraries, so the OS provides SDKs that include a .tbd file
881 # to use when linking.)
882 #
883 # This means that check_function_exists() will think that
884 # the remote-capture APIs are present, including pcap_open()
885 # and pcap_findalldevs_ex().
886 #
887 # However, they are *not* present in macOS Ventura and earlier,
888 # which means that building on Ventura with Xcode 15 produces
889 # executables that fail to start because one of those APIs
890 # isn't found in the system libpcap.
891 #
892 # Protecting calls to those APIs with __builtin_available()
893 # does not prevent this, because the libpcap header files
894 # in the Sonoma SDK mark them as being first available
895 # in macOS 10.13, just like all the other routines introduced
896 # in libpcap 1.9, even though they're only available if libpcap
897 # is built with remote capture enabled or stub routines are
898 # provided. (A fix to enable this has been checked into the
899 # libpcap repository, and may end up in a later version of
900 # the SDK.)
901 #
902 # Given all that, and given that the versions of the
903 # remote-capture APIs in Sonoma are stubs that always fail,
904 # there doesn't seem to be any point in checking for pcap_open()
905 # and pcap_findalldevs_ex() if we're linking against the Apple libpcap.
906 #
907 # However, if we're *not* linking against the Apple libpcap,
908 # we should check for it, so that we can use it if it's present.
909 #
910 # So we check for pcap_open() and pcap_findalldevs_ex() if 1) this isn't
911 # macOS or 2) the the libpcap we found is not a system library, meaning
912 # that its path begins neither with /usr/lib (meaning it's a system
913 # dylib) nor /Application/Xcode.app (meaning it's a file in
914 # the Xcode SDK).
915 #
916 if(NOT APPLE OR NOT
917 (PCAP_LIBRARIES MATCHES "/usr/lib/.*" OR
918 PCAP_LIBRARIES MATCHES "/Application/Xcode.app/.*"))
919 check_function_exists(pcap_open HAVE_PCAP_OPEN)
920 check_function_exists(pcap_findalldevs_ex HAVE_PCAP_FINDALLDEVS_EX)
921 endif()
922
923 #
924 # On Windows, check for pcap_wsockinit(); if we don't have it, check for
925 # wsockinit().
926 #
927 if(WIN32)
928 check_function_exists(pcap_wsockinit HAVE_PCAP_WSOCKINIT)
929 if(NOT HAVE_PCAP_WSOCKINIT)
930 check_function_exists(wsockinit HAVE_WSOCKINIT)
931 endif(NOT HAVE_PCAP_WSOCKINIT)
932 endif(WIN32)
933
934 #
935 # Check for special debugging functions
936 #
937 check_function_exists(pcap_set_parser_debug HAVE_PCAP_SET_PARSER_DEBUG)
938 if(NOT HAVE_PCAP_SET_PARSER_DEBUG)
939 # Check whether libpcap defines pcap_debug or yydebug
940 check_variable_exists(pcap_debug HAVE_PCAP_DEBUG)
941 if(NOT HAVE_PCAP_DEBUG)
942 check_variable_exists(yydebug HAVE_YYDEBUG)
943 endif(NOT HAVE_PCAP_DEBUG)
944 endif(NOT HAVE_PCAP_SET_PARSER_DEBUG)
945
946 check_function_exists(pcap_set_optimizer_debug HAVE_PCAP_SET_OPTIMIZER_DEBUG)
947
948 #
949 # bpf_dump() moved from tcpdump to libpcap in libpcap 0.6, but some
950 # versions of libpcap didn't pick that change up; the OpenBSD libpcap
951 # picked up most of the new APIs from versions up to 1.0, but didn't
952 # pick up bpf_dump(), so we need to provide it if it's absent.
953 #
954 check_function_exists(bpf_dump HAVE_BPF_DUMP)
955
956 cmake_pop_check_state()
957 cmake_pop_check_state()
958
959 #
960 # We have libpcap.
961 #
962 include_directories(SYSTEM ${PCAP_INCLUDE_DIRS})
963 set(TCPDUMP_LINK_LIBRARIES ${PCAP_LIBRARIES} ${TCPDUMP_LINK_LIBRARIES})
964
965 #
966 # Optional libraries.
967 #
968
969 #
970 # libsmi.
971 #
972 if(WITH_SMI)
973 find_package(SMI)
974 if(SMI_FOUND)
975 include_directories(SYSTEM ${SMI_INCLUDE_DIRS})
976 set(TCPDUMP_LINK_LIBRARIES ${TCPDUMP_LINK_LIBRARIES} ${SMI_LIBRARIES})
977 set(USE_LIBSMI ON)
978 endif(SMI_FOUND)
979 endif(WITH_SMI)
980
981 #
982 # OpenSSL/libressl libcrypto.
983 #
984 if(WITH_CRYPTO)
985 find_package(CRYPTO)
986 if(CRYPTO_FOUND)
987 #
988 # 1) do we have EVP_CIPHER_CTX_new?
989 # If so, we use it to allocate an EVP_CIPHER_CTX, as
990 # EVP_CIPHER_CTX may be opaque; otherwise, we allocate
991 # it ourselves.
992 #
993 cmake_push_check_state()
994 set(CMAKE_REQUIRED_LIBRARIES "${CRYPTO_LIBRARIES}")
995
996 check_function_exists(EVP_CIPHER_CTX_new HAVE_EVP_CIPHER_CTX_NEW)
997
998 #
999 # 2) do we have EVP_DecryptInit_ex()?
1000 # If so, we use it, because we need to be able to make two
1001 # "initialize the cipher" calls, one with the cipher and key,
1002 # and one with the IV, and, as of OpenSSL 1.1, You Can't Do That
1003 # with EVP_DecryptInit(), because a call to EVP_DecryptInit() will
1004 # unconditionally clear the context, and if you don't supply a
1005 # cipher, it'll clear the cipher, rendering the context unusable
1006 # and causing a crash.
1007 #
1008 check_function_exists(EVP_DecryptInit_ex HAVE_EVP_DECRYPTINIT_EX)
1009
1010 cmake_pop_check_state()
1011
1012 #
1013 # We have libcrypto.
1014 #
1015 include_directories(SYSTEM ${CRYPTO_INCLUDE_DIRS})
1016 set(TCPDUMP_LINK_LIBRARIES ${TCPDUMP_LINK_LIBRARIES} ${CRYPTO_LIBRARIES})
1017 set(HAVE_LIBCRYPTO ON)
1018 endif(CRYPTO_FOUND)
1019 endif(WITH_CRYPTO)
1020
1021 #
1022 # Capsicum sandboxing.
1023 # Some of this is in the system library, some of it is in other libraries.
1024 #
1025 if(WITH_CAPSICUM)
1026 check_include_files("sys/capsicum.h" HAVE_SYS_CAPSICUM_H)
1027 if(HAVE_SYS_CAPSICUM_H)
1028 check_function_exists(cap_enter HAVE_CAP_ENTER)
1029 check_function_exists(cap_rights_limit HAVE_CAP_RIGHTS_LIMIT)
1030 check_function_exists(cap_ioctls_limit HAVE_CAP_IOCTLS_LIMIT)
1031 check_function_exists(openat HAVE_OPENAT)
1032 if(HAVE_CAP_ENTER AND HAVE_CAP_RIGHTS_LIMIT AND
1033 HAVE_CAP_IOCTLS_LIMIT AND HAVE_OPENAT)
1034 #
1035 # OK, we have the functions we need to support Capsicum.
1036 #
1037 set(HAVE_CAPSICUM TRUE)
1038
1039 #
1040 # OK, can we use Casper?
1041 #
1042 check_library_exists(casper cap_init "" HAVE_CAP_INIT)
1043 if(HAVE_CAP_INIT)
1044 cmake_push_check_state()
1045 set(CMAKE_REQUIRED_LIBRARIES casper)
1046 check_library_exists(cap_dns cap_gethostbyaddr "" HAVE_CAP_GETHOSTBYADDR)
1047 cmake_pop_check_state()
1048 if(HAVE_CAP_GETHOSTBYADDR)
1049 set(HAVE_CASPER TRUE)
1050 set(TCPDUMP_LINK_LIBRARIES ${TCPDUMP_LINK_LIBRARIES} casper cap_dns)
1051 endif(HAVE_CAP_GETHOSTBYADDR)
1052 endif(HAVE_CAP_INIT)
1053 endif(HAVE_CAP_ENTER AND HAVE_CAP_RIGHTS_LIMIT AND
1054 HAVE_CAP_IOCTLS_LIMIT AND HAVE_OPENAT)
1055 endif(HAVE_SYS_CAPSICUM_H)
1056 endif(WITH_CAPSICUM)
1057
1058 #
1059 # libcap-ng.
1060 #
1061 if(WITH_CAP_NG)
1062 check_include_file(cap-ng.h HAVE_CAP_NG_H)
1063 check_library_exists(cap-ng capng_change_id "" HAVE_LIBCAP_NG)
1064 if(HAVE_LIBCAP_NG)
1065 set(TCPDUMP_LINK_LIBRARIES ${TCPDUMP_LINK_LIBRARIES} cap-ng)
1066 endif(HAVE_LIBCAP_NG)
1067 endif(WITH_CAP_NG)
1068
1069 ###################################################################
1070 # Warning options
1071 ###################################################################
1072
1073 #
1074 # Check and add warning options if we have a .devel file.
1075 #
1076 if(EXISTS ${CMAKE_SOURCE_DIR}/.devel OR EXISTS ${CMAKE_BINARY_DIR}/.devel)
1077 #
1078 # Warning options.
1079 #
1080 if(MSVC AND NOT ${CMAKE_C_COMPILER} MATCHES "clang*")
1081 #
1082 # MSVC, with Microsoft's front end and code generator.
1083 # "MSVC" is also set for Microsoft's compiler with a Clang
1084 # front end and their code generator ("Clang/C2"), so we
1085 # check for clang.exe and treat that differently.
1086 #
1087 check_and_add_compiler_option(-Wall)
1088 #
1089 # Disable some pointless warnings that /Wall turns on.
1090 #
1091 # Unfortunately, MSVC does not appear to have an equivalent
1092 # to "__attribute__((unused))" to mark a particular function
1093 # parameter as being known to be unused, so that the compiler
1094 # won't warn about it (for example, the function might have
1095 # that parameter because a pointer to it is being used, and
1096 # the signature of that function includes that parameter).
1097 # C++ lets you give a parameter a type but no name, but C
1098 # doesn't have that.
1099 #
1100 check_and_add_compiler_option(-wd4100)
1101 #
1102 # In theory, we care whether somebody uses f() rather than
1103 # f(void) to declare a function with no arguments, but, in
1104 # practice, there are places in the Windows header files
1105 # that appear to do that, so we squelch that warning.
1106 #
1107 check_and_add_compiler_option(-wd4255)
1108 #
1109 # Windows FD_SET() generates this, so we suppress it.
1110 #
1111 check_and_add_compiler_option(-wd4548)
1112 #
1113 # Perhaps testing something #defined to be 0 with #ifdef is an
1114 # error, and it should be tested with #if, but perhaps it's
1115 # not, and Microsoft does that in its headers, so we squelch
1116 # that warning.
1117 #
1118 check_and_add_compiler_option(-wd4574)
1119 #
1120 # The Windows headers also test not-defined values in #if, so
1121 # we don't want warnings about that, either.
1122 #
1123 check_and_add_compiler_option(-wd4668)
1124 #
1125 # We do *not* care whether some function is, or isn't, going to be
1126 # expanded inline.
1127 #
1128 check_and_add_compiler_option(-wd4710)
1129 check_and_add_compiler_option(-wd4711)
1130 #
1131 # We do *not* care whether we're adding padding bytes after
1132 # structure members.
1133 #
1134 check_and_add_compiler_option(-wd4820)
1135 #
1136 # We do *not* care about every single place the compiler would
1137 # have inserted Spectre mitigation if only we had told it to
1138 # do so with /Qspectre. I guess the theory is that it's seeing
1139 # bounds checks that would prevent out-of-bounds loads and that
1140 # those out-of-bounds loads could be done speculatively and that
1141 # the Spectre attack could detect the value of the out-of-bounds
1142 # data *if* it's within our address space, but unless I'm
1143 # missing something I don't see that as being any form of
1144 # security hole.
1145 #
1146 # XXX - add /Qspectre if that is really worth doing.
1147 #
1148 check_and_add_compiler_option(-wd5045)
1149 #
1150 # We do *not* care whether a structure had padding added at
1151 # the end because of __declspec(align) - *we* don't use
1152 # __declspec(align), because the only structures whose layout
1153 # we precisely specify are those that get overlaid on packet
1154 # data, and in those every element is an array of octets so
1155 # that we have full control over the size and alignment, and,
1156 # apparently, jmp_buf has such a declaration on x86, meaning
1157 # that everything that includes netdissect.h, i.e. almost every
1158 # file in tcpdump, gets a warning.
1159 #
1160 check_and_add_compiler_option(-wd4324)
1161 #
1162 # Suppress warnings about a "possible loss of performance"
1163 # due to "storing 32-bit float result in memory" in the
1164 # "fetch a 32-bit floating-point variable, in a particular
1165 # byte order, from a packet" routines.
1166 #
1167 # What those routines do is fetch a 32-bit unsigned integral
1168 # value in the appropriate byte order, store it into the
1169 # "32-bit unsigned integer value" member of a union, and then
1170 # return the "32-bit floating-point value" member of that union,
1171 # in order to reinterpret the bits of the value as if it were
1172 # an IEEE floating-point value. (Yes, we know, VAX floating
1173 # point; at least the IBM mainframes have supported IEEE
1174 # floating-point since the G4 S/390s.)
1175 #
1176 # The Microsoft page on that warning:
1177 #
1178 # https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-3-c4738?view=msvc-170
1179 #
1180 # says that
1181 #
1182 # C4738 warns that the result of an assignment, cast, passed
1183 # argument, or other operation may need to be rounded or
1184 # that the operation ran out of registers and needed to
1185 # use memory (spilling). This can result in performance loss.
1186 #
1187 # That suggests that the performance issues would be due to
1188 # conversion between double and float, which we're not doing,
1189 # or due to a calculation somehow running out of registers,
1190 # but we're not doing a calculation. Thus, we just suppress
1191 # the warning.
1192 #
1193 check_and_add_compiler_option(-wd4738)
1194 else()
1195 #
1196 # Other compilers, including MSVC with a Clang front end and
1197 # Microsoft's code generator. We currently treat them as if
1198 # they might support GCC-style -W options.
1199 #
1200 check_and_add_compiler_option(-W)
1201 check_and_add_compiler_option(-Wall)
1202 check_and_add_compiler_option(-Wassign-enum)
1203 check_and_add_compiler_option(-Wcast-qual)
1204 check_and_add_compiler_option(-Wmissing-prototypes)
1205 check_and_add_compiler_option(-Wmissing-variable-declarations)
1206 check_and_add_compiler_option(-Wold-style-definition)
1207 if(NOT CMAKE_C_COMPILER_ID MATCHES "Sun")
1208 # In Sun C versions that implement GCC compatibility "-Wpedantic"
1209 # means the same as "-pedantic". The latter is mutually exclusive
1210 # with several other options. One of those is "-xc99", which has
1211 # already been set for Sun C above.
1212 check_and_add_compiler_option(-Wpedantic)
1213 endif()
1214 check_and_add_compiler_option(-Wpointer-arith)
1215 check_and_add_compiler_option(-Wpointer-sign)
1216 check_and_add_compiler_option(-Wshadow)
1217 check_and_add_compiler_option(-Wsign-compare)
1218 check_and_add_compiler_option(-Wstrict-prototypes)
1219 check_and_add_compiler_option(-Wundef)
1220 check_and_add_compiler_option(-Wunreachable-code-return)
1221 check_and_add_compiler_option(-Wused-but-marked-unused)
1222 check_and_add_compiler_option(-Wwrite-strings)
1223 endif()
1224 endif()
1225
1226 #
1227 # Extra compiler options for the build matrix scripts to request -Werror or
1228 # its equivalent if required. The CMake variable name cannot be CFLAGS
1229 # because that is already used for a different purpose in CMake. Example
1230 # usage: cmake -DEXTRA_CFLAGS='-Wall -Wextra -Werror' ...
1231 #
1232 if(NOT "${EXTRA_CFLAGS}" STREQUAL "")
1233 # The meaning of EXTRA_CFLAGS is "use the exact specified options, or the
1234 # build risks failing to fail", not "try every specified option, omit those
1235 # that do not work and use the rest". Thus use add_compile_options(), not
1236 # foreach()/check_and_add_compiler_option().
1237 string(REPLACE " " ";" _extra_cflags_list ${EXTRA_CFLAGS})
1238 add_compile_options(${_extra_cflags_list})
1239 message(STATUS "Added extra compile options (${EXTRA_CFLAGS})")
1240 endif()
1241
1242 ######################################
1243 # Input files
1244 ######################################
1245
1246 if(ENABLE_SMB)
1247 #
1248 # We allow the SMB dissector to be omitted.
1249 #
1250 set(LOCALSRC ${LOCALSRC}
1251 print-smb.c
1252 smbutil.c)
1253 endif(ENABLE_SMB)
1254
1255 set(NETDISSECT_SOURCE_LIST_C
1256 addrtoname.c
1257 addrtostr.c
1258 af.c
1259 ascii_strcasecmp.c
1260 checksum.c
1261 cpack.c
1262 gmpls.c
1263 in_cksum.c
1264 ipproto.c
1265 l2vpn.c
1266 netdissect.c
1267 netdissect-alloc.c
1268 nlpid.c
1269 ntp.c
1270 oui.c
1271 parsenfsfh.c
1272 print.c
1273 print-802_11.c
1274 print-802_15_4.c
1275 print-ah.c
1276 print-ahcp.c
1277 print-aodv.c
1278 print-aoe.c
1279 print-ap1394.c
1280 print-arcnet.c
1281 print-arista.c
1282 print-arp.c
1283 print-ascii.c
1284 print-atalk.c
1285 print-atm.c
1286 print-babel.c
1287 print-bcm-li.c
1288 print-beep.c
1289 print-bfd.c
1290 print-bgp.c
1291 print-bootp.c
1292 print-brcmtag.c
1293 print-bt.c
1294 print-calm-fast.c
1295 print-carp.c
1296 print-cdp.c
1297 print-cfm.c
1298 print-chdlc.c
1299 print-cip.c
1300 print-cnfp.c
1301 print-dccp.c
1302 print-decnet.c
1303 print-dhcp6.c
1304 print-domain.c
1305 print-dsa.c
1306 print-dtp.c
1307 print-dvmrp.c
1308 print-eap.c
1309 print-egp.c
1310 print-eigrp.c
1311 print-enc.c
1312 print-erspan.c
1313 print-esp.c
1314 print-ether.c
1315 print-fddi.c
1316 print-forces.c
1317 print-fr.c
1318 print-frag6.c
1319 print-ftp.c
1320 print-geneve.c
1321 print-geonet.c
1322 print-gre.c
1323 print-hncp.c
1324 print-hsrp.c
1325 print-http.c
1326 print-icmp.c
1327 print-icmp6.c
1328 print-igmp.c
1329 print-igrp.c
1330 print-ip-demux.c
1331 print-ip.c
1332 print-ip6.c
1333 print-ip6opts.c
1334 print-ipcomp.c
1335 print-ipfc.c
1336 print-ipnet.c
1337 print-ipoib.c
1338 print-ipx.c
1339 print-isakmp.c
1340 print-isoclns.c
1341 print-juniper.c
1342 print-krb.c
1343 print-l2tp.c
1344 print-lane.c
1345 print-ldp.c
1346 print-lisp.c
1347 print-llc.c
1348 print-lldp.c
1349 print-lmp.c
1350 print-loopback.c
1351 print-lspping.c
1352 print-lwapp.c
1353 print-lwres.c
1354 print-m3ua.c
1355 print-macsec.c
1356 print-mobile.c
1357 print-mobility.c
1358 print-mpcp.c
1359 print-mpls.c
1360 print-mptcp.c
1361 print-msdp.c
1362 print-msnlb.c
1363 print-nflog.c
1364 print-nfs.c
1365 print-nhrp.c
1366 print-nsh.c
1367 print-ntp.c
1368 print-null.c
1369 print-olsr.c
1370 print-openflow-1.0.c
1371 print-openflow-1.3.c
1372 print-openflow.c
1373 print-ospf.c
1374 print-ospf6.c
1375 print-otv.c
1376 print-pflog.c
1377 print-pgm.c
1378 print-pim.c
1379 print-pktap.c
1380 print-ppi.c
1381 print-ppp.c
1382 print-pppoe.c
1383 print-pptp.c
1384 print-ptp.c
1385 print-quic.c
1386 print-radius.c
1387 print-raw.c
1388 print-realtek.c
1389 print-resp.c
1390 print-rip.c
1391 print-ripng.c
1392 print-rpki-rtr.c
1393 print-rsvp.c
1394 print-rt6.c
1395 print-rtsp.c
1396 print-rx.c
1397 print-sctp.c
1398 print-sflow.c
1399 print-sip.c
1400 print-sl.c
1401 print-sll.c
1402 print-slow.c
1403 print-smtp.c
1404 print-snmp.c
1405 print-someip.c
1406 print-ssh.c
1407 print-stp.c
1408 print-sunatm.c
1409 print-sunrpc.c
1410 print-symantec.c
1411 print-syslog.c
1412 print-tcp.c
1413 print-telnet.c
1414 print-tftp.c
1415 print-timed.c
1416 print-tipc.c
1417 print-token.c
1418 print-udld.c
1419 print-udp.c
1420 print-unsupported.c
1421 print-usb.c
1422 print-vjc.c
1423 print-vqp.c
1424 print-vrrp.c
1425 print-vsock.c
1426 print-vtp.c
1427 print-vxlan-gpe.c
1428 print-vxlan.c
1429 print-wb.c
1430 print-whois.c
1431 print-zep.c
1432 print-zephyr.c
1433 print-zeromq.c
1434 ${LOCALSRC}
1435 signature.c
1436 strtoaddr.c
1437 util-print.c
1438 )
1439
1440 #
1441 # Replace missing functions
1442 #
1443 foreach(FUNC strlcat strlcpy strsep getservent getopt_long)
1444 string(TOUPPER ${FUNC} FUNC_UPPERCASE)
1445 set(HAVE_FUNC_UPPERCASE HAVE_${FUNC_UPPERCASE})
1446 if(NOT ${HAVE_FUNC_UPPERCASE})
1447 set(NETDISSECT_SOURCE_LIST_C ${NETDISSECT_SOURCE_LIST_C} missing/${FUNC}.c)
1448 endif()
1449 endforeach()
1450
1451 add_library(netdissect STATIC
1452 ${NETDISSECT_SOURCE_LIST_C}
1453 )
1454 if(NOT C_ADDITIONAL_FLAGS STREQUAL "")
1455 set_target_properties(netdissect PROPERTIES COMPILE_FLAGS ${C_ADDITIONAL_FLAGS})
1456 endif()
1457
1458 set(TCPDUMP_SOURCE_LIST_C fptype.c tcpdump.c)
1459
1460 if(NOT HAVE_BPF_DUMP)
1461 set(TCPDUMP_SOURCE_LIST_C ${TCPDUMP_SOURCE_LIST_C} bpf_dump.c)
1462 endif(NOT HAVE_BPF_DUMP)
1463
1464 set(PROJECT_SOURCE_LIST_C ${NETDISSECT_SOURCE_LIST_C} ${TCPDUMP_SOURCE_LIST_C})
1465
1466 file(GLOB PROJECT_SOURCE_LIST_H
1467 *.h
1468 )
1469
1470 #
1471 # Assume, by default, no support for shared libraries and V7/BSD
1472 # convention for man pages (devices in section 4, file formats in
1473 # section 5, miscellaneous info in section 7, administrative commands
1474 # and daemons in section 8). Individual cases can override this.
1475 # Individual cases can override this.
1476 #
1477 set(MAN_FILE_FORMATS 5)
1478 set(MAN_MISC_INFO 7)
1479 if(CMAKE_SYSTEM_NAME STREQUAL "HP-UX")
1480 #
1481 # Use System V conventions for man pages.
1482 #
1483 set(MAN_FILE_FORMATS 4)
1484 set(MAN_MISC_INFO 5)
1485 elseif(CMAKE_SYSTEM_NAME STREQUAL "SunOS" AND CMAKE_SYSTEM_VERSION MATCHES "5[.][0-9.]*")
1486 #
1487 # SunOS 5.x.
1488 #
1489 if(CMAKE_SYSTEM_VERSION STREQUAL "5.12")
1490 else()
1491 #
1492 # Use System V conventions for man pages.
1493 #
1494 set(MAN_FILE_FORMATS 4)
1495 set(MAN_MISC_INFO 5)
1496 endif()
1497 endif()
1498
1499 source_group("Source Files" FILES ${PROJECT_SOURCE_LIST_C})
1500 source_group("Header Files" FILES ${PROJECT_SOURCE_LIST_H})
1501
1502 ######################################
1503 # Register targets
1504 ######################################
1505
1506 add_executable(tcpdump ${TCPDUMP_SOURCE_LIST_C})
1507 if(NOT C_ADDITIONAL_FLAGS STREQUAL "")
1508 set_target_properties(tcpdump PROPERTIES COMPILE_FLAGS ${C_ADDITIONAL_FLAGS})
1509 endif()
1510 if(NOT "${PCAP_LINK_FLAGS}" STREQUAL "")
1511 set_target_properties(tcpdump PROPERTIES LINK_FLAGS ${PCAP_LINK_FLAGS})
1512 endif()
1513 target_link_libraries(tcpdump netdissect ${TCPDUMP_LINK_LIBRARIES})
1514
1515 ######################################
1516 # Write out the config.h file
1517 ######################################
1518
1519 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmakeconfig.h.in ${CMAKE_CURRENT_BINARY_DIR}/config.h)
1520
1521 ######################################
1522 # Install tcpdump and man pages
1523 ######################################
1524
1525 #
1526 # "Define GNU standard installation directories", which actually
1527 # are also defined, to some degree, by autotools, and at least
1528 # some of which are general UN*X conventions.
1529 #
1530 include(GNUInstallDirs)
1531
1532 set(MAN1_EXPAND tcpdump.1.in)
1533
1534 if(WIN32)
1535 # XXX TODO where to install on Windows?
1536 else(WIN32)
1537 install(TARGETS tcpdump DESTINATION bin)
1538 endif(WIN32)
1539
1540 # On UN*X, and on Windows when not using MSVC, process man pages and
1541 # arrange that they be installed.
1542 if(NOT MSVC)
1543 #
1544 # Man pages.
1545 #
1546 # For each section of the manual for which we have man pages
1547 # that require macro expansion, do the expansion.
1548 #
1549 set(MAN1 "")
1550 foreach(TEMPLATE_MANPAGE ${MAN1_EXPAND})
1551 string(REPLACE ".in" "" MANPAGE ${TEMPLATE_MANPAGE})
1552 configure_file(${CMAKE_SOURCE_DIR}/${TEMPLATE_MANPAGE} ${CMAKE_CURRENT_BINARY_DIR}/${MANPAGE} @ONLY)
1553 set(MAN1 ${MAN1} ${CMAKE_CURRENT_BINARY_DIR}/${MANPAGE})
1554 endforeach(TEMPLATE_MANPAGE)
1555 install(FILES ${MAN1} DESTINATION ${CMAKE_INSTALL_MANDIR}/man1)
1556 endif(NOT MSVC)
1557
1558 # uninstall target
1559 configure_file(
1560 "${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in"
1561 "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"
1562 IMMEDIATE @ONLY)
1563
1564 add_custom_target(uninstall
1565 COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)
1566
1567 #
1568 # tcpdump tests
1569 # We try to find the Perl interpreter and, if we do, we have the check
1570 # rule run tests/TESTrun with it, because just trying to run the TESTrun
1571 # script as a command won't work on Windows.
1572 #
1573 find_program(PERL perl)
1574 if(PERL)
1575 message(STATUS "Found perl at ${PERL}")
1576 add_custom_target(check
1577 COMMAND ${PERL} ${CMAKE_SOURCE_DIR}/tests/TESTrun)
1578 else()
1579 message(STATUS "Didn't find perl")
1580 endif()