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