3 # We need 3.12 or later, so that we can set policy CMP0074; see
5 cmake_minimum_required(VERSION 3.12)
8 # For now, require only 2.8.6, just in case somebody is
9 # configuring with CMake on a "long-term support" version
10 # of some OS and that version supplies an older version of
13 # If this is ever updated to CMake 3.1 or later, remove the
14 # stuff in cmake/Modules/FindPCAP.cmake that appends subdirectories
15 # of directories from CMAKE_PREFIX_PATH to the PKG_CONFIG_PATH
16 # environment variable when running pkg-config, to make sure
17 # it finds any .pc file from there.
19 cmake_minimum_required(VERSION 2.8.6)
23 # We want find_path() and find_library() to honor {packagename}_ROOT,
24 # as that appears to be the standard way to say "hey, look here for
25 # this package" from the command line.
28 cmake_policy(SET CMP0074 NEW)
31 set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake/Modules)
34 # OK, this is a royal pain.
36 # CMake will try to determine the sizes of some data types, including
37 # void *, early in the process of configuration; apparently, it's done
38 # as part of processing the project() command.
40 # At least as of CMake 2.8.6, it does so by checking the size of
41 # "void *" in C, setting CMAKE_C_SIZEOF_DATA_PTR based on that,
42 # setting CMAKE_SIZEOF_VOID_P to that, and then checking the size
43 # of "void *" in C++, setting CMAKE_CXX_SIZEOF_DATA_PTR based on
44 # that, and then setting CMAKE_SIZEOF_VOID_P to *that*.
46 # The compile tests include whatever C flags may have been provided
47 # to CMake in the CFLAGS and CXXFLAGS environment variables.
49 # If you set an architecture flag such as -m32 or -m64 in CFLAGS
50 # but *not* in CXXFLAGS, the size for C++ will win, and hilarity
53 # Or if, at least on Solaris, you have a newer version of GCC
54 # installed, but *not* a newer version of G++, and you have Oracle
55 # Studio installed, it will find GCC, which will default to building
56 # 64-bit, and Oracle Studio's C++ compiler, which will default to
57 # building 32-bit, the size for C++ will win, and, again, hilarity
60 # So we *explicitly* state that only C is used; there is currently no
61 # C++ code in tcpdump.
66 # For checking if a compiler flag works and adding it if it does.
68 include(CheckCCompilerFlag)
69 macro(check_and_add_compiler_option _option)
70 message(STATUS "Checking C compiler flag ${_option}")
71 string(REPLACE "=" "-" _temp_option_variable ${_option})
72 string(REGEX REPLACE "^-" "" _option_variable ${_temp_option_variable})
73 check_c_compiler_flag("${_option}" ${_option_variable})
74 if(${${_option_variable}})
75 set(C_ADDITIONAL_FLAGS "${C_ADDITIONAL_FLAGS} ${_option}")
80 # If we're building with Visual Studio, we require Visual Studio 2015,
81 # in order to get sufficient C99 compatibility. Check for that.
83 # If not, try the appropriate flag for the compiler to enable C99
86 set(C_ADDITIONAL_FLAGS "")
88 if(MSVC_VERSION LESS 1900)
89 message(FATAL_ERROR "Visual Studio 2015 or later is required")
93 # Treat source files as being in UTF-8 with MSVC if it's not using
94 # the Clang front end.
95 # We assume that UTF-8 source is OK with other compilers and with
96 # MSVC if it's using the Clang front end.
98 if(NOT ${CMAKE_C_COMPILER} MATCHES "clang*")
99 set(C_ADDITIONAL_FLAGS "${C_ADDITIONAL_FLAGS} /utf-8")
100 endif(NOT ${CMAKE_C_COMPILER} MATCHES "clang*")
103 # Try to enable as many C99 features as we can.
104 # At minimum, we want C++/C99-style // comments.
106 # Newer versions of compilers might default to supporting C99, but
107 # older versions may require a special flag.
109 # Prior to CMake 3.1, setting CMAKE_C_STANDARD will not have any effect,
110 # so, unless and until we require CMake 3.1 or later, we have to do it
111 # ourselves on pre-3.1 CMake, so we just do it ourselves on all versions
114 # Note: with CMake 3.1 through 3.5, the only compilers for which CMake
115 # handles CMAKE_C_STANDARD are GCC and Clang. 3.6 adds support only
116 # for Intel C; 3.9 adds support for PGI C, Sun C, and IBM XL C, and
117 # 3.10 adds support for Cray C and IAR C, but no version of CMake has
118 # support for HP C. Therefore, even if we use CMAKE_C_STANDARD with
119 # compilers for which CMake supports it, we may still have to do it
120 # ourselves on other compilers.
122 # See the CMake documentation for the CMAKE_<LANG>_COMPILER_ID variables
123 # for a list of compiler IDs.
125 # XXX - this just tests whether the option works and adds it if it does.
126 # We don't test whether it's necessary in order to get the C99 features
127 # that we use; if we ever have a user who tries to compile with a compiler
128 # that can't be made to support those features, we can add a test to make
129 # sure we actually *have* C99 support.
131 if(CMAKE_C_COMPILER_ID MATCHES "GNU" OR
132 CMAKE_C_COMPILER_ID MATCHES "Clang")
133 check_and_add_compiler_option("-std=gnu99")
134 elseif(CMAKE_C_COMPILER_ID MATCHES "XL")
136 # We want support for extensions picked up for GNU C compatibility,
137 # so we use -qlanglvl=extc99.
139 check_and_add_compiler_option("-qlanglvl=extc99")
140 elseif(CMAKE_C_COMPILER_ID MATCHES "HP")
141 check_and_add_compiler_option("-AC99")
142 elseif(CMAKE_C_COMPILER_ID MATCHES "Sun")
143 check_and_add_compiler_option("-xc99")
144 elseif(CMAKE_C_COMPILER_ID MATCHES "Intel")
145 check_and_add_compiler_option("-c99")
149 set(LIBRARY_NAME netdissect)
151 ###################################################################
153 ###################################################################
155 option(WITH_SMI "Build with libsmi, if available" ON)
156 option(WITH_CRYPTO "Build with OpenSSL/libressl libcrypto, if available" ON)
157 option(WITH_CAPSICUM "Build with Capsicum security functions, if available" ON)
158 option(WITH_CAP_NG "Use libcap-ng, if available" ON)
159 option(ENABLE_SMB "Build with the SMB dissector" OFF)
162 # String parameters. Neither of them are set, initially; only if the
163 # user explicitly configures them are they set.
165 # WITH_CHROOT is STRING, not PATH, as the directory need not exist
168 set(WITH_CHROOT CACHE STRING
169 "Directory to which to chroot when dropping privileges")
170 set(WITH_USER CACHE STRING
171 "User to whom to set the UID when dropping privileges")
174 # By default, build universal with the appropriate set of architectures
175 # for the OS on which we're doing the build.
177 if(APPLE AND "${CMAKE_OSX_ARCHITECTURES}" STREQUAL "")
179 # Get the major version of Darwin.
181 string(REGEX MATCH "^([0-9]+)" SYSTEM_VERSION_MAJOR "${CMAKE_SYSTEM_VERSION}")
183 if(SYSTEM_VERSION_MAJOR EQUAL 9)
185 # Leopard. Build for x86 and 32-bit PowerPC, with
186 # x86 first. (That's what Apple does.)
188 set(CMAKE_OSX_ARCHITECTURES "i386;ppc")
189 elseif(SYSTEM_VERSION_MAJOR EQUAL 10)
191 # Snow Leopard. Build for x86-64 and x86, with
192 # x86-64 first. (That's what Apple does.)
194 set(CMAKE_OSX_ARCHITECTURES "x86_64;i386")
198 ###################################################################
200 ###################################################################
202 # Get, parse, format and set tcpdump's version string from
203 # [tcpdump_root]/VERSION for later use.
205 # Get MAJOR, MINOR, PATCH & SUFFIX
206 file(STRINGS ${tcpdump_SOURCE_DIR}/VERSION
208 LIMIT_COUNT 1 # Read only the first line
211 ######################################
213 ######################################
215 add_definitions(-DHAVE_CONFIG_H)
218 ${CMAKE_CURRENT_BINARY_DIR}
219 ${tcpdump_SOURCE_DIR}
223 add_definitions(-D__STDC__)
224 add_definitions(-D_CRT_SECURE_NO_WARNINGS)
229 MESSAGE(STATUS "Use STATIC runtime")
231 set (CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} /MT")
232 set (CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /MT")
233 set (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT")
234 set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd")
236 set (CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL} /MT")
237 set (CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO} /MT")
238 set (CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /MT")
239 set (CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /MTd")
241 MESSAGE(STATUS "Use DYNAMIC runtime")
243 set (CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} /MD")
244 set (CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /MD")
245 set (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MD")
246 set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MDd")
248 set (CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL} /MD")
249 set (CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO} /MD")
250 set (CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /MD")
251 set (CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /MDd")
252 endif (USE_STATIC_RT)
255 ###################################################################
256 # Detect available platform features
257 ###################################################################
259 include(CMakePushCheckState)
260 include(CheckIncludeFile)
261 include(CheckIncludeFiles)
262 include(CheckFunctionExists)
263 include(CheckLibraryExists)
264 include(CheckSymbolExists)
265 include(CheckStructHasMember)
266 include(CheckVariableExists)
267 include(CheckTypeSize)
272 check_include_file(fcntl.h HAVE_FCNTL_H)
273 check_include_file(rpc/rpc.h HAVE_RPC_RPC_H)
274 check_include_file(net/if.h HAVE_NET_IF_H)
276 check_include_files("rpc/rpc.h;rpc/rpcent.h" HAVE_RPC_RPCENT_H)
277 endif(HAVE_RPC_RPC_H)
282 check_function_exists(strlcat HAVE_STRLCAT)
283 check_function_exists(strlcpy HAVE_STRLCPY)
284 check_function_exists(strdup HAVE_STRDUP)
285 check_function_exists(strsep HAVE_STRSEP)
288 # Find library needed for gethostbyaddr.
289 # NOTE: if you hand check_library_exists as its last argument a variable
290 # that's been set, it skips the test, so we need different variables.
292 set(TCPDUMP_LINK_LIBRARIES "")
295 # We need winsock2.h and ws2tcpip.h.
297 cmake_push_check_state()
298 set(CMAKE_REQUIRED_LIBRARIES ws2_32)
299 check_symbol_exists(gethostbyaddr "winsock2.h;ws2tcpip.h" LIBWS2_32_HAS_GETHOSTBYADDR)
300 cmake_pop_check_state()
301 if(LIBWS2_32_HAS_GETHOSTBYADDR)
302 set(TCPDUMP_LINK_LIBRARIES ws2_32 ${TCPDUMP_LINK_LIBRARIES})
303 else(LIBWS2_32_HAS_GETHOSTBYADDR)
304 message(FATAL_ERROR "gethostbyaddr is required, but wasn't found")
305 endif(LIBWS2_32_HAS_GETHOSTBYADDR)
307 check_function_exists(gethostbyaddr STDLIBS_HAVE_GETHOSTBYADDR)
308 if(NOT STDLIBS_HAVE_GETHOSTBYADDR)
309 check_library_exists(socket gethostbyaddr "" LIBSOCKET_HAS_GETHOSTBYADDR)
310 if(LIBSOCKET_HAS_GETHOSTBYADDR)
311 set(TCPDUMP_LINK_LIBRARIES ${TCPDUMP_LINK_LIBRARIES} socket)
312 else(LIBSOCKET_HAS_GETHOSTBYADDR)
313 check_library_exists(nsl gethostbyaddr "" LIBNSL_HAS_GETHOSTBYADDR)
314 if(LIBNSL_HAS_GETHOSTBYADDR)
315 set(TCPDUMP_LINK_LIBRARIES ${TCPDUMP_LINK_LIBRARIES} nsl)
316 else(LIBNSL_HAS_GETHOSTBYADDR)
317 message(FATAL_ERROR "gethostbyaddr is required, but wasn't found")
318 endif(LIBNSL_HAS_GETHOSTBYADDR)
319 endif(LIBSOCKET_HAS_GETHOSTBYADDR)
320 endif(NOT STDLIBS_HAVE_GETHOSTBYADDR)
324 # This may require additional libraries.
326 cmake_push_check_state()
327 set(CMAKE_REQUIRED_LIBRARIES ${TCPDUMP_LINK_LIBRARIES})
328 check_function_exists(getservent STDLIBS_HAVE_GETSERVENT)
329 if(STDLIBS_HAVE_GETSERVENT)
330 set(HAVE_GETSERVENT TRUE)
331 else(STDLIBS_HAVE_GETSERVENT)
333 # Some platforms may need -lsocket for getservent.
335 set(CMAKE_REQUIRED_LIBRARIES socket ${TCPDUMP_LINK_LIBRARIES})
336 check_function_exists(getservent LIBSOCKET_HAS_GETSERVENT)
337 if(LIBSOCKET_HAS_GETSERVENT)
338 set(HAVE_GETSERVENT TRUE)
339 set(TCPDUMP_LINK_LIBRARIES socket ${TCPDUMP_LINK_LIBRARIES})
340 endif(LIBSOCKET_HAS_GETSERVENT)
341 endif(STDLIBS_HAVE_GETSERVENT)
342 cmake_pop_check_state()
345 # Make sure we have vsnprintf() and snprintf(); we require them.
346 # We use check_symbol_exists(), as they aren't necessarily external
347 # functions - in Visual Studio, for example, they're inline functions
348 # calling a common external function.
350 check_symbol_exists(vsnprintf "stdio.h" HAVE_VSNPRINTF)
351 if(NOT HAVE_VSNPRINTF)
352 message(FATAL_ERROR "vsnprintf() is required but wasn't found")
353 endif(NOT HAVE_VSNPRINTF)
354 check_symbol_exists(snprintf "stdio.h" HAVE_SNPRINTF)
355 if(NOT HAVE_SNPRINTF)
356 message(FATAL_ERROR "snprintf() is required but wasn't found")
359 check_function_exists(getopt_long HAVE_GETOPT_LONG)
360 check_function_exists(strftime HAVE_STRFTIME)
361 check_function_exists(setlinebuf HAVE_SETLINEBUF)
363 # For Windows, don't need to waste time checking for fork() or vfork().
366 check_function_exists(fork HAVE_FORK)
367 check_function_exists(vfork HAVE_VFORK)
371 # Some platforms may need -lnsl for getrpcbynumber.
373 cmake_push_check_state()
374 set(CMAKE_REQUIRED_LIBRARIES ${TCPDUMP_LINK_LIBRARIES})
375 check_function_exists(getrpcbynumber STDLIBS_HAVE_GETRPCBYNUMBER)
376 if(STDLIBS_HAVE_GETRPCBYNUMBER)
377 set(HAVE_GETRPCBYNUMBER TRUE)
378 else(STDLIBS_HAVE_GETRPCBYNUMBER)
379 set(CMAKE_REQUIRED_LIBRARIES ${TCPDUMP_LINK_LIBRARIES} nsl)
380 check_function_exists(getrpcbynumber LIBNSL_HAS_GETRPCBYNUMBER)
381 if(LIBNSL_HAS_GETRPCBYNUMBER)
382 set(HAVE_GETRPCBYNUMBER TRUE)
383 set(TCPDUMP_LINK_LIBRARIES ${TCPDUMP_LINK_LIBRARIES} nsl)
384 endif(LIBNSL_HAS_GETRPCBYNUMBER)
385 endif(STDLIBS_HAVE_GETRPCBYNUMBER)
386 cmake_pop_check_state()
389 # This requires the libraries we require, as ether_ntohost might be
390 # in one of those libraries. That means we have to do this after
391 # we check for those libraries.
393 # You are in a twisty little maze of UN*Xes, all different.
394 # Some might not have ether_ntohost().
395 # Some might have it and declare it in <net/ethernet.h>.
396 # Some might have it and declare it in <netinet/ether.h>
397 # Some might have it and declare it in <sys/ethernet.h>.
398 # Some might have it and declare it in <arpa/inet.h>.
399 # Some might have it and declare it in <netinet/if_ether.h>.
400 # Some might have it and not declare it in any header file.
402 # Before you is a C compiler.
404 cmake_push_check_state()
405 set(CMAKE_REQUIRED_LIBRARIES ${TCPDUMP_LINK_LIBRARIES})
406 check_function_exists(ether_ntohost HAVE_ETHER_NTOHOST)
407 if(HAVE_ETHER_NTOHOST)
409 # OK, we have ether_ntohost(). We don't check whether it's buggy,
410 # as we assume any system that has CMake is likely to be new enough
411 # that, if it has ether_ntohost(), whatever bug is checked for in
412 # autotools is fixed; we just decide to use it.
414 set(USE_ETHER_NTOHOST TRUE)
417 # Is it declared in <net/ethernet.h>?
419 # This test fails if we don't have <net/ethernet.h> or if we do
420 # but it doesn't declare ether_ntohost().
422 check_symbol_exists(ether_ntohost net/ethernet.h NET_ETHERNET_H_DECLARES_ETHER_NTOHOST)
423 if(NET_ETHERNET_H_DECLARES_ETHER_NTOHOST)
425 # Yes - we have it declared.
427 set(HAVE_DECL_ETHER_NTOHOST TRUE)
432 if(NOT HAVE_DECL_ETHER_NTOHOST)
434 # No - how about <netinet/ether.h>, as on Linux?
436 # This test fails if we don't have <netinet/ether.h>
437 # or if we do but it doesn't declare ether_ntohost().
439 check_symbol_exists(ether_ntohost netinet/ether.h NETINET_ETHER_H_DECLARES_ETHER_NTOHOST)
440 if(NETINET_ETHER_H_DECLARES_ETHER_NTOHOST)
442 # Yes - we have it declared.
444 set(HAVE_DECL_ETHER_NTOHOST TRUE)
450 if(NOT HAVE_DECL_ETHER_NTOHOST)
452 # No - how about <sys/ethernet.h>, as on Solaris 10 and later?
454 # This test fails if we don't have <sys/ethernet.h>
455 # or if we do but it doesn't declare ether_ntohost().
457 check_symbol_exists(ether_ntohost sys/ethernet.h SYS_ETHERNET_H_DECLARES_ETHER_NTOHOST)
458 if(SYS_ETHERNET_H_DECLARES_ETHER_NTOHOST)
460 # Yes - we have it declared.
462 set(HAVE_DECL_ETHER_NTOHOST TRUE)
468 if(NOT HAVE_DECL_ETHER_NTOHOST)
470 # No, how about <arpa/inet.h>, as on AIX?
472 # This test fails if we don't have <arpa/inet.h>
473 # or if we do but it doesn't declare ether_ntohost().
475 check_symbol_exists(ether_ntohost arpa/inet.h ARPA_INET_H_DECLARES_ETHER_NTOHOST)
476 if(ARPA_INET_H_DECLARES_ETHER_NTOHOST)
478 # Yes - we have it declared.
480 set(HAVE_DECL_ETHER_NTOHOST TRUE)
486 if(NOT HAVE_DECL_ETHER_NTOHOST)
488 # No, how about <netinet/if_ether.h>?
489 # On some platforms, it requires <net/if.h> and
490 # <netinet/in.h>, and we always include it with
491 # both of them, so test it with both of them.
493 # This test fails if we don't have <netinet/if_ether.h>
494 # and the headers we include before it, or if we do but
495 # <netinet/if_ether.h> doesn't declare ether_ntohost().
497 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)
498 if(NETINET_IF_ETHER_H_DECLARES_ETHER_NTOHOST)
500 # Yes - we have it declared.
502 set(HAVE_DECL_ETHER_NTOHOST TRUE)
506 # After all that, is ether_ntohost() declared?
508 if(NOT HAVE_DECL_ETHER_NTOHOST)
510 # No, we'll have to declare it ourselves.
511 # Do we have "struct ether_addr" if we include<netinet/if_ether.h>?
513 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)
516 cmake_pop_check_state()
521 # XXX - there's no check_struct() macro that's like check_struct_has_member()
522 # except that it only checks for the existence of the structure type,
523 # so we use check_struct_has_member() and look for ss_family.
527 # Check for IPv6 support.
528 # We just check for AF_INET6 and struct in6_addr.
530 cmake_push_check_state()
532 set(CMAKE_EXTRA_INCLUDE_FILES sys/types.h ws2tcpip.h)
533 check_symbol_exists(AF_INET6 "sys/types.h;ws2tcpip.h" HAVE_AF_INET6)
535 set(CMAKE_EXTRA_INCLUDE_FILES sys/types.h sys/socket.h netinet/in.h)
536 check_symbol_exists(AF_INET6 "sys/types.h;sys/socket.h;netinet/in.h" HAVE_AF_INET6)
538 check_type_size("struct in6_addr" HAVE_STRUCT_IN6_ADDR)
539 cmake_pop_check_state()
540 if(HAVE_AF_INET6 AND HAVE_STRUCT_IN6_ADDR)
541 set(HAVE_OS_IPV6_SUPPORT TRUE)
542 endif(HAVE_AF_INET6 AND HAVE_STRUCT_IN6_ADDR)
544 ######################################
545 # External dependencies
546 ######################################
549 # libpcap/WinPcap/Npcap.
552 find_package(PCAP REQUIRED)
553 include_directories(${PCAP_INCLUDE_DIRS})
555 cmake_push_check_state()
560 set(CMAKE_REQUIRED_INCLUDES ${PCAP_INCLUDE_DIRS})
563 # Check whether we have pcap/pcap-inttypes.h.
564 # If we do, we use that to get the C99 types defined.
566 check_include_file(pcap/pcap-inttypes.h HAVE_PCAP_PCAP_INTTYPES_H)
569 # Check for various functions in libpcap/WinPcap/Npcap.
571 cmake_push_check_state()
572 set(CMAKE_REQUIRED_LIBRARIES ${PCAP_LIBRARIES})
575 # Check for "pcap_list_datalinks()" and use a substitute version if
576 # it's not present. If it is present, check for "pcap_free_datalinks()";
577 # if it's not present, we don't replace it for now. (We could do so
578 # on UN*X, but not on Windows, where hilarity ensues if a program
579 # built with one version of the MSVC support library tries to free
580 # something allocated by a library built with another version of
581 # the MSVC support library.)
583 check_function_exists(pcap_list_datalinks HAVE_PCAP_LIST_DATALINKS)
584 if(HAVE_PCAP_LIST_DATALINKS)
585 check_function_exists(pcap_free_datalinks HAVE_PCAP_FREE_DATALINKS)
586 endif(HAVE_PCAP_LIST_DATALINKS)
589 # Check for "pcap_datalink_name_to_val()", and use a substitute
590 # version if it's not present. If it is present, check for
591 # "pcap_datalink_val_to_description()", and if we don't have it,
592 # use a substitute version.
594 check_function_exists(pcap_datalink_name_to_val HAVE_PCAP_DATALINK_NAME_TO_VAL)
595 if(HAVE_PCAP_DATALINK_NAME_TO_VAL)
596 check_function_exists(pcap_datalink_val_to_description HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION)
597 endif(HAVE_PCAP_DATALINK_NAME_TO_VAL)
600 # Check for "pcap_set_datalink()"; you can't substitute for it if
601 # it's absent (it has hooks into libpcap), so just define the
602 # HAVE_ value if it's there.
604 check_function_exists(pcap_set_datalink HAVE_PCAP_SET_DATALINK)
607 # Check for "pcap_breakloop()"; you can't substitute for it if
608 # it's absent (it has hooks into the live capture routines),
609 # so just define the HAVE_ value if it's there.
611 check_function_exists(pcap_breakloop HAVE_PCAP_BREAKLOOP)
614 # Check for "pcap_dump_ftell()"; we use a substitute version
615 # if it's not present.
617 check_function_exists(pcap_dump_ftell HAVE_PCAP_DUMP_FTELL)
620 # Do we have the new open API? Check for pcap_create() and for
621 # pcap_statustostr(), and assume that, if we have both of them,
622 # we also have pcap_activate() and the other new routines
623 # introduced in libpcap 1.0.0. (We check for pcap_statustostr()
624 # as well, because WinPcap 4.1.3 screwed up and exported pcap_create()
625 # but not other routines such as pcap_statustostr(), even though it
626 # defined them and even though you really want pcap_statustostr() to
627 # get strings corresponding to some of the status returns from the
630 check_function_exists(pcap_statustostr HAVE_PCAP_STATUSTOSTR)
632 # If we don't have pcap_statustostr(), don't check for pcap_create(),
633 # so we pretend we don't have it.
635 if(HAVE_PCAP_STATUSTOSTR)
636 check_function_exists(pcap_create HAVE_PCAP_CREATE)
637 endif(HAVE_PCAP_STATUSTOSTR)
640 # OK, do we have pcap_set_tstamp_type? If so, assume we have
641 # pcap_list_tstamp_types and pcap_free_tstamp_types as well.
643 check_function_exists(pcap_set_tstamp_type HAVE_PCAP_SET_TSTAMP_TYPE)
646 # And do we have pcap_set_tstamp_precision? If so, we assume
647 # we also have pcap_open_offline_with_tstamp_precision.
649 check_function_exists(pcap_set_tstamp_precision HAVE_PCAP_SET_TSTAMP_PRECISION)
650 endif(HAVE_PCAP_CREATE)
653 # Check for a miscellaneous collection of functions which we use
656 check_function_exists(pcap_findalldevs HAVE_PCAP_FINDALLDEVS)
657 if(HAVE_PCAP_FINDALLDEVS)
659 # Check for libpcap having pcap_findalldevs() but the pcap.h header
660 # not having pcap_if_t; some versions of Mac OS X shipped with pcap.h
661 # from 0.6 and libpcap 0.8, so that libpcap had pcap_findalldevs but
662 # pcap.h didn't have pcap_if_t.
664 cmake_push_check_state()
665 set(CMAKE_REQUIRED_INCLUDES ${PCAP_INCLUDE_DIRS})
666 set(CMAKE_EXTRA_INCLUDE_FILES pcap.h)
667 check_type_size(pcap_if_t PCAP_IF_T)
668 cmake_pop_check_state()
669 endif(HAVE_PCAP_FINDALLDEVS)
670 check_function_exists(pcap_dump_flush HAVE_PCAP_DUMP_FLUSH)
671 check_function_exists(pcap_lib_version HAVE_PCAP_LIB_VERSION)
672 if(NOT HAVE_PCAP_LIB_VERSION)
673 # Check for the pcap_version string variable and set HAVE_PCAP_VERSION
674 endif(NOT HAVE_PCAP_LIB_VERSION)
675 check_function_exists(pcap_setdirection HAVE_PCAP_SETDIRECTION)
676 check_function_exists(pcap_set_immediate_mode HAVE_PCAP_SET_IMMEDIATE_MODE)
677 check_function_exists(pcap_dump_ftell64 HAVE_PCAP_DUMP_FTELL64)
678 check_function_exists(pcap_open HAVE_PCAP_OPEN)
679 check_function_exists(pcap_findalldevs_ex HAVE_PCAP_FINDALLDEVS_EX)
682 # On Windows, check for pcap_wsockinit(); if we don't have it, check for
686 check_function_exists(pcap_wsockinit HAVE_PCAP_WSOCKINIT)
687 if(NOT HAVE_PCAP_WSOCKINIT)
688 check_function_exists(wsockinit HAVE_WSOCKINIT)
689 endif(NOT HAVE_PCAP_WSOCKINIT)
693 # Check for special debugging functions
695 check_function_exists(pcap_set_parser_debug HAVE_PCAP_SET_PARSER_DEBUG)
696 if(NOT HAVE_PCAP_SET_PARSER_DEBUG)
697 # Check whether libpcap defines pcap_debug or yydebug
698 check_variable_exists(pcap_debug HAVE_PCAP_DEBUG)
699 if(NOT HAVE_PCAP_DEBUG)
700 check_variable_exists(yydebug HAVE_YYDEBUG)
701 endif(NOT HAVE_PCAP_DEBUG)
702 endif(NOT HAVE_PCAP_SET_PARSER_DEBUG)
704 check_function_exists(pcap_set_optimizer_debug HAVE_PCAP_SET_OPTIMIZER_DEBUG)
705 check_function_exists(bpf_dump HAVE_BPF_DUMP)
707 cmake_pop_check_state()
712 include_directories(SYSTEM ${PCAP_INCLUDE_DIRS})
713 set(TCPDUMP_LINK_LIBRARIES ${PCAP_LIBRARIES} ${TCPDUMP_LINK_LIBRARIES})
716 # Optional libraries.
725 include_directories(SYSTEM ${SMI_INCLUDE_DIRS})
726 set(TCPDUMP_LINK_LIBRARIES ${TCPDUMP_LINK_LIBRARIES} ${SMI_LIBRARIES})
732 # OpenSSL/libressl libcrypto.
738 # Check for some headers and functions.
740 check_include_file(openssl/evp.h HAVE_OPENSSL_EVP_H)
743 # 1) do we have EVP_CIPHER_CTX_new?
744 # If so, we use it to allocate an EVP_CIPHER_CTX, as
745 # EVP_CIPHER_CTX may be opaque; otherwise, we allocate
748 cmake_push_check_state()
749 set(CMAKE_REQUIRED_LIBRARIES "${CRYPTO_LIBRARIES}")
751 check_function_exists(EVP_CIPHER_CTX_new HAVE_EVP_CIPHER_CTX_NEW)
754 # 2) do we have EVP_DecryptInit_ex()?
755 # If so, we use it, because we need to be able to make two
756 # "initialize the cipher" calls, one with the cipher and key,
757 # and one with the IV, and, as of OpenSSL 1.1, You Can't Do That
758 # with EVP_DecryptInit(), because a call to EVP_DecryptInit() will
759 # unconditionally clear the context, and if you don't supply a
760 # cipher, it'll clear the cipher, rendering the context unusable
761 # and causing a crash.
763 check_function_exists(EVP_DecryptInit_ex HAVE_EVP_DECRYPTINIT_EX)
765 cmake_pop_check_state()
770 include_directories(SYSTEM ${CRYPTO_INCLUDE_DIRS})
771 set(TCPDUMP_LINK_LIBRARIES ${TCPDUMP_LINK_LIBRARIES} ${CRYPTO_LIBRARIES})
772 set(HAVE_LIBCRYPTO ON)
777 # Capsicum sandboxing.
778 # Some of this is in the system library, some of it is in other libraries.
781 check_include_files("sys/capsicum.h" HAVE_SYS_CAPSICUM_H)
782 if(HAVE_SYS_CAPSICUM_H)
783 check_function_exists(cap_enter HAVE_CAP_ENTER)
784 check_function_exists(cap_rights_limit HAVE_CAP_RIGHTS_LIMIT)
785 check_function_exists(cap_ioctls_limit HAVE_CAP_IOCTLS_LIMIT)
786 check_function_exists(openat HAVE_OPENAT)
787 if(HAVE_CAP_ENTER AND HAVE_CAP_RIGHTS_LIMIT AND
788 HAVE_CAP_IOCTLS_LIMIT AND HAVE_OPENAT)
790 # OK, we have the functions we need to support Capsicum.
792 set(HAVE_CAPSICUM TRUE)
795 # OK, can we use Casper?
797 check_library_exists(casper cap_init "" HAVE_CAP_INIT)
799 cmake_push_check_state()
800 set(CMAKE_REQUIRED_LIBRARIES casper)
801 check_library_exists(cap_dns cap_gethostbyaddr "" HAVE_CAP_GETHOSTBYADDR)
802 cmake_pop_check_state()
803 if(HAVE_CAP_GETHOSTBYADDR)
804 set(HAVE_CASPER TRUE)
805 set(TCPDUMP_LINK_LIBRARIES ${TCPDUMP_LINK_LIBRARIES} casper cap_dns)
806 endif(HAVE_CAP_GETHOSTBYADDR)
808 endif(HAVE_CAP_ENTER AND HAVE_CAP_RIGHTS_LIMIT AND
809 HAVE_CAP_IOCTLS_LIMIT AND HAVE_OPENAT)
810 endif(HAVE_SYS_CAPSICUM_H)
817 check_include_file(cap-ng.h HAVE_CAP_NG_H)
818 check_library_exists(cap-ng capng_change_id "" HAVE_LIBCAP_NG)
820 set(TCPDUMP_LINK_LIBRARIES ${TCPDUMP_LINK_LIBRARIES} cap-ng)
821 endif(HAVE_LIBCAP_NG)
824 ###################################################################
826 ###################################################################
829 # Check and add warning options if we have a .devel file.
831 if(EXISTS ${CMAKE_SOURCE_DIR}/.devel OR EXISTS ${CMAKE_BINARY_DIR}/.devel)
835 if(MSVC AND NOT ${CMAKE_C_COMPILER} MATCHES "clang*")
837 # MSVC, with Microsoft's front end and code generator.
838 # "MSVC" is also set for Microsoft's compiler with a Clang
839 # front end and their code generator ("Clang/C2"), so we
840 # check for clang.exe and treat that differently.
842 check_and_add_compiler_option(-Wall)
844 # Disable some pointless warnings that /Wall turns on.
846 # Unfortunately, MSVC does not appear to have an equivalent
847 # to "__attribute__((unused))" to mark a particular function
848 # parameter as being known to be unused, so that the compiler
849 # won't warn about it (for example, the function might have
850 # that parameter because a pointer to it is being used, and
851 # the signature of that function includes that parameter).
852 # C++ lets you give a parameter a type but no name, but C
855 check_and_add_compiler_option(-wd4100)
857 # In theory, we care whether somebody uses f() rather than
858 # f(void) to declare a function with no arguments, but, in
859 # practice, there are places in the Windows header files
860 # that appear to do that, so we squelch that warning.
862 check_and_add_compiler_option(-wd4255)
864 # Windows FD_SET() generates this, so we suppress it.
866 check_and_add_compiler_option(-wd4548)
868 # Perhaps testing something #defined to be 0 with #ifdef is an
869 # error, and it should be tested with #if, but perhaps it's
870 # not, and Microsoft does that in its headers, so we squelch
873 check_and_add_compiler_option(-wd4574)
875 # The Windows headers also test not-defined values in #if, so
876 # we don't want warnings about that, either.
878 check_and_add_compiler_option(-wd4668)
880 # We do *not* care whether some function is, or isn't, going to be
883 check_and_add_compiler_option(-wd4710)
884 check_and_add_compiler_option(-wd4711)
886 # We do *not* care whether we're adding padding bytes after
889 check_and_add_compiler_option(-wd4820)
891 # We do *not* care about every single place the compiler would
892 # have inserted Spectre mitigation if only we had told it to
893 # do so with /Qspectre. I guess the theory is that it's seeing
894 # bounds checks that would prevent out-of-bounds loads and that
895 # those out-of-bounds loads could be done speculatively and that
896 # the Spectre attack could detect the value of the out-of-bounds
897 # data *if* it's within our address space, but unless I'm
898 # missing something I don't see that as being any form of
901 # XXX - add /Qspectre if that is really worth doing.
903 check_and_add_compiler_option(-wd5045)
905 # We do *not* care whether a structure had padding added at
906 # the end because of __declspec(align) - *we* don't use
907 # __declspec(align), because the only structures whose layout
908 # we precisely specify are those that get overlayed on packet
909 # data, and in those every element is an array of octets so
910 # that we have full control over the size and aligmnet, and,
911 # apparently, jmp_buf has such a declaration on x86, meaning
912 # that everything that includes netdissect.h, i.e. almost every
913 # file in tcpdump, gets a warning.
915 check_and_add_compiler_option(-wd4324)
918 # Other compilers, including MSVC with a Clang front end and
919 # Microsoft's code generator. We currently treat them as if
920 # they might support GCC-style -W options.
922 check_and_add_compiler_option(-W)
923 check_and_add_compiler_option(-Wall)
924 check_and_add_compiler_option(-Wassign-enum)
925 check_and_add_compiler_option(-Wcast-qual)
926 check_and_add_compiler_option(-Wmissing-prototypes)
927 check_and_add_compiler_option(-Wmissing-variable-declarations)
928 check_and_add_compiler_option(-Wold-style-definition)
929 check_and_add_compiler_option(-Wpedantic)
930 check_and_add_compiler_option(-Wpointer-arith)
931 check_and_add_compiler_option(-Wpointer-sign)
932 check_and_add_compiler_option(-Wshadow)
933 check_and_add_compiler_option(-Wsign-compare)
934 check_and_add_compiler_option(-Wstrict-prototypes)
935 check_and_add_compiler_option(-Wunreachable-code-return)
936 check_and_add_compiler_option(-Wused-but-marked-unused)
937 check_and_add_compiler_option(-Wwrite-strings)
942 # Extra compiler options for the build matrix scripts to request -Werror or
943 # its equivalent if required. The CMake variable name cannot be CFLAGS
944 # because that is already used for a different purpose in CMake. Example
945 # usage: cmake -DEXTRA_CFLAGS='-Wall -Wextra -Werror' ...
947 if(NOT "${EXTRA_CFLAGS}" STREQUAL "")
948 foreach(_extra_cflag ${EXTRA_CFLAGS})
949 check_and_add_compiler_option("${_extra_cflag}")
950 endforeach(_extra_cflag)
951 message(STATUS "Added extra compile options (${EXTRA_CFLAGS})")
954 ######################################
956 ######################################
960 # We allow the SMB dissector to be omitted.
962 set(LOCALSRC ${LOCALSRC}
967 set(NETDISSECT_SOURCE_LIST_C
1081 print-openflow-1.0.c
1082 print-openflow-1.3.c
1152 # Replace missing functions
1154 foreach(FUNC strlcat strlcpy strdup strsep getservent getopt_long)
1155 string(TOUPPER ${FUNC} FUNC_UPPERCASE)
1156 set(HAVE_FUNC_UPPERCASE HAVE_${FUNC_UPPERCASE})
1157 if(NOT ${HAVE_FUNC_UPPERCASE})
1158 set(NETDISSECT_SOURCE_LIST_C ${NETDISSECT_SOURCE_LIST_C} missing/${FUNC}.c)
1162 add_library(netdissect STATIC
1163 ${NETDISSECT_SOURCE_LIST_C}
1165 if(NOT C_ADDITIONAL_FLAGS STREQUAL "")
1166 set_target_properties(netdissect PROPERTIES COMPILE_FLAGS ${C_ADDITIONAL_FLAGS})
1169 set(TCPDUMP_SOURCE_LIST_C fptype.c tcpdump.c)
1171 if(NOT HAVE_BPF_DUMP)
1172 set(TCPDUMP_SOURCE_LIST_C ${TCPDUMP_SOURCE_LIST_C} bpf_dump.c)
1173 endif(NOT HAVE_BPF_DUMP)
1174 if(NOT HAVE_PCAP_DUMP_FTELL)
1175 set(TCPDUMP_SOURCE_LIST_C ${TCPDUMP_SOURCE_LIST_C} missing/pcap_dump_ftell.c)
1176 endif(NOT HAVE_PCAP_DUMP_FTELL)
1178 if(NOT HAVE_PCAP_LIST_DATALINKS)
1179 set(TCPDUMP_SOURCE_LIST_C ${TCPDUMP_SOURCE_LIST_C} missing/datalinks.c)
1180 endif(NOT HAVE_PCAP_LIST_DATALINKS)
1182 if((NOT HAVE_PCAP_DATALINK_NAME_TO_VAL) OR (NOT HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION))
1183 set(TCPDUMP_SOURCE_LIST_C ${TCPDUMP_SOURCE_LIST_C} missing/dlnames.c)
1184 endif((NOT HAVE_PCAP_DATALINK_NAME_TO_VAL) OR (NOT HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION))
1186 set(PROJECT_SOURCE_LIST_C ${NETDISSECT_SOURCE_LIST_C} ${TCPDUMP_SOURCE_LIST_C})
1188 file(GLOB PROJECT_SOURCE_LIST_H
1192 source_group("Source Files" FILES ${PROJECT_SOURCE_LIST_C})
1193 source_group("Header Files" FILES ${PROJECT_SOURCE_LIST_H})
1195 ######################################
1197 ######################################
1199 add_executable(tcpdump ${TCPDUMP_SOURCE_LIST_C})
1200 if(NOT C_ADDITIONAL_FLAGS STREQUAL "")
1201 set_target_properties(tcpdump PROPERTIES COMPILE_FLAGS ${C_ADDITIONAL_FLAGS})
1203 target_link_libraries(tcpdump netdissect ${TCPDUMP_LINK_LIBRARIES})
1205 ######################################
1206 # Write out the config.h file
1207 ######################################
1209 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmakeconfig.h.in ${CMAKE_CURRENT_BINARY_DIR}/config.h)
1211 ######################################
1212 # Install tcpdump and man pages
1213 ######################################
1216 # "Define GNU standard installation directories", which actually
1217 # are also defined, to some degree, by autotools, and at least
1218 # some of which are general UN*X conventions.
1220 include(GNUInstallDirs)
1222 set(MAN1_EXPAND tcpdump.1.in)
1225 # XXX TODO where to install on Windows?
1227 install(TARGETS tcpdump DESTINATION bin)
1230 # On UN*X, and on Windows when not using MSVC, process man pages and
1231 # arrange that they be installed.
1236 # For each section of the manual for which we have man pages
1237 # that require macro expansion, do the expansion.
1240 foreach(TEMPLATE_MANPAGE ${MAN1_EXPAND})
1241 string(REPLACE ".in" "" MANPAGE ${TEMPLATE_MANPAGE})
1242 configure_file(${CMAKE_SOURCE_DIR}/${TEMPLATE_MANPAGE} ${CMAKE_CURRENT_BINARY_DIR}/${MANPAGE} @ONLY)
1243 set(MAN1 ${MAN1} ${CMAKE_CURRENT_BINARY_DIR}/${MANPAGE})
1244 endforeach(TEMPLATE_MANPAGE)
1245 install(FILES ${MAN1} DESTINATION ${CMAKE_INSTALL_MANDIR}/man1)
1250 "${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in"
1251 "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"
1254 add_custom_target(uninstall
1255 COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)
1259 # We try to find the Perl interpreter and, if we do, we have the check
1260 # rule run tests/TESTrun with it, because just trying to run the TESTrun
1261 # script as a command won't work on Windows.
1263 find_program(PERL perl)
1265 message(STATUS "Found perl at ${PERL}")
1266 add_custom_target(check
1267 COMMAND ${PERL} ${CMAKE_SOURCE_DIR}/tests/TESTrun)
1269 message(STATUS "Didn't find perl")