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