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