1 cmake_minimum_required(VERSION 2.8.6)
3 set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake/Modules)
8 # For checking if a compiler flag works and adding it if it does.
10 include(CheckCCompilerFlag)
11 macro(check_and_add_compiler_option _option)
12 message(STATUS "Checking C compiler flag ${_option}")
13 string(REPLACE "=" "-" _temp_option_variable ${_option})
14 string(REGEX REPLACE "^-" "" _option_variable ${_temp_option_variable})
15 check_c_compiler_flag("${_option}" ${_option_variable})
16 if(${${_option_variable}})
17 set(C_ADDITIONAL_FLAGS "${C_ADDITIONAL_FLAGS} ${_option}")
22 # If we're building with Visual Studio, we require Visual Studio 2015,
23 # in order to get sufficient C99 compatibility. Check for that.
25 # If not, try the appropriate flag for the compiler to enable C99
28 set(C_ADDITIONAL_FLAGS "")
30 if(MSVC_VERSION LESS 1900)
31 message(FATAL_ERROR "Visual Studio 2015 or later is required")
35 # Treat source files as being in UTF-8 with MSVC if it's not using
36 # the Clang front end.
37 # We assume that UTF-8 source is OK with other compilers and with
38 # MSVC if it's using the Clang front end.
40 if(NOT ${CMAKE_C_COMPILER} MATCHES "clang*")
41 set(C_ADDITIONAL_FLAGS "${C_ADDITIONAL_FLAGS} /utf-8")
42 endif(NOT ${CMAKE_C_COMPILER} MATCHES "clang*")
45 # Try to enable as many C99 features as we can.
46 # At minimum, we want C++/C99-style // comments.
48 # Newer versions of compilers might default to supporting C99, but
49 # older versions may require a special flag.
51 # Prior to CMake 3.1, setting CMAKE_C_STANDARD will not have any effect,
52 # so, unless and until we require CMake 3.1 or later, we have to do it
53 # ourselves on pre-3.1 CMake, so we just do it ourselves on all versions
56 # Note: with CMake 3.1 through 3.5, the only compilers for which CMake
57 # handles CMAKE_C_STANDARD are GCC and Clang. 3.6 adds support only
58 # for Intel C; 3.9 adds support for PGI C, Sun C, and IBM XL C, and
59 # 3.10 adds support for Cray C and IAR C, but no version of CMake has
60 # support for HP C. Therefore, even if we use CMAKE_C_STANDARD with
61 # compilers for which CMake supports it, we may still have to do it
62 # ourselves on other compilers.
64 # See the CMake documentation for the CMAKE_<LANG>_COMPILER_ID variables
65 # for a list of compiler IDs.
67 # XXX - this just tests whether the option works and adds it if it does.
68 # We don't test whether it's necessary in order to get the C99 features
69 # that we use; if we ever have a user who tries to compile with a compiler
70 # that can't be made to support those features, we can add a test to make
71 # sure we actually *have* C99 support.
73 if(CMAKE_C_COMPILER_ID MATCHES "GNU" OR
74 CMAKE_C_COMPILER_ID MATCHES "Clang")
75 check_and_add_compiler_option("-std=gnu99")
76 elseif(CMAKE_C_COMPILER_ID MATCHES "XL")
78 # We want support for extensions picked up for GNU C compatibility,
79 # so we use -qlanglvl=extc99.
81 check_and_add_compiler_option("-qlanglvl=extc99")
82 elseif(CMAKE_C_COMPILER_ID MATCHES "HP")
83 check_and_add_compiler_option("-AC99")
84 elseif(CMAKE_C_COMPILER_ID MATCHES "Sun")
85 check_and_add_compiler_option("-xc99")
86 elseif(CMAKE_C_COMPILER_ID MATCHES "Intel")
87 check_and_add_compiler_option("-c99")
91 set(LIBRARY_NAME netdissect)
93 ###################################################################
95 ###################################################################
97 option(WITH_SMI "Build with libsmi, if available" ON)
98 option(WITH_CRYPTO "Build with OpenSSL/libressl libcrypto, if available" ON)
99 option(WITH_CAPSICUM "Build with Capsicum security functions, if available" ON)
100 option(WITH_CAP_NG "Use libcap-ng, if available" ON)
101 option(ENABLE_SMB "Build with the SMB dissector" ON)
104 # String parameters. Neither of them are set, initially; only if the
105 # user explicitly configures them are they set.
107 # WITH_CHROOT is STRING, not PATH, as the directory need not exist
110 set(WITH_CHROOT CACHE STRING
111 "Directory to which to chroot when dropping privileges")
112 set(WITH_USER CACHE STRING
113 "User to whom to set the UID when dropping privileges")
116 # By default, build universal with the appropriate set of architectures
117 # for the OS on which we're doing the build.
119 if(APPLE AND "${CMAKE_OSX_ARCHITECTURES}" STREQUAL "")
121 # Get the major version of Darwin.
123 string(REGEX MATCH "^([0-9]+)" SYSTEM_VERSION_MAJOR "${CMAKE_SYSTEM_VERSION}")
125 if(SYSTEM_VERSION_MAJOR EQUAL 9)
127 # Leopard. Build for x86 and 32-bit PowerPC, with
128 # x86 first. (That's what Apple does.)
130 set(CMAKE_OSX_ARCHITECTURES "i386;ppc")
131 elseif(SYSTEM_VERSION_MAJOR EQUAL 10)
133 # Snow Leopard. Build for x86-64 and x86, with
134 # x86-64 first. (That's what Apple does.)
136 set(CMAKE_OSX_ARCHITECTURES "x86_64;i386")
140 ###################################################################
142 ###################################################################
144 # Get, parse, format and set tcpdump's version string from
145 # [tcpdump_root]/VERSION for later use.
147 # Get MAJOR, MINOR, PATCH & SUFFIX
148 file(STRINGS ${tcpdump_SOURCE_DIR}/VERSION
150 LIMIT_COUNT 1 # Read only the first line
153 ######################################
155 ######################################
157 add_definitions(-DHAVE_CONFIG_H)
160 ${CMAKE_CURRENT_BINARY_DIR}
161 ${tcpdump_SOURCE_DIR}
165 add_definitions(-D__STDC__)
166 add_definitions(-D_CRT_SECURE_NO_WARNINGS)
171 MESSAGE(STATUS "Use STATIC runtime")
173 set (CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} /MT")
174 set (CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /MT")
175 set (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT")
176 set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd")
178 set (CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL} /MT")
179 set (CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO} /MT")
180 set (CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /MT")
181 set (CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /MTd")
183 MESSAGE(STATUS "Use DYNAMIC runtime")
185 set (CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} /MD")
186 set (CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /MD")
187 set (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MD")
188 set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MDd")
190 set (CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL} /MD")
191 set (CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO} /MD")
192 set (CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /MD")
193 set (CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /MDd")
194 endif (USE_STATIC_RT)
197 ###################################################################
198 # Detect available platform features
199 ###################################################################
201 include(CMakePushCheckState)
202 include(CheckIncludeFile)
203 include(CheckIncludeFiles)
204 include(CheckFunctionExists)
205 include(CheckLibraryExists)
206 include(CheckSymbolExists)
207 include(CheckStructHasMember)
208 include(CheckVariableExists)
209 include(CheckTypeSize)
214 check_include_file(fcntl.h HAVE_FCNTL_H)
215 check_include_file(rpc/rpc.h HAVE_RPC_RPC_H)
216 check_include_file(net/if.h HAVE_NET_IF_H)
218 check_include_files("rpc/rpc.h;rpc/rpcent.h" HAVE_RPC_RPCENT_H)
219 endif(HAVE_RPC_RPC_H)
221 check_include_files("sys/types.h;sys/socket.h;net/if.h;net/pfvar.h" HAVE_NET_PFVAR_H)
223 check_include_files("sys/types.h;sys/socket.h;net/if.h;net/pfvar.h;net/if_pflog.h" HAVE_NET_IF_PFLOG_H)
224 if(HAVE_NET_IF_PFLOG_H)
225 set(LOCALSRC print-pflog.c ${LOCALSRC})
226 endif(HAVE_NET_IF_PFLOG_H)
227 endif(HAVE_NET_PFVAR_H)
233 check_function_exists(strlcat HAVE_STRLCAT)
234 check_function_exists(strlcpy HAVE_STRLCPY)
235 check_function_exists(strdup HAVE_STRDUP)
236 check_function_exists(strsep HAVE_STRSEP)
239 # Find library needed for gethostbyaddr.
240 # NOTE: if you hand check_library_exists as its last argument a variable
241 # that's been set, it skips the test, so we need different variables.
243 set(TCPDUMP_LINK_LIBRARIES "")
246 # We need winsock2.h and ws2tcpip.h.
248 cmake_push_check_state()
249 set(CMAKE_REQUIRED_LIBRARIES ws2_32)
250 check_symbol_exists(gethostbyaddr "winsock2.h;ws2tcpip.h" LIBWS2_32_HAS_GETHOSTBYADDR)
251 cmake_pop_check_state()
252 if(LIBWS2_32_HAS_GETHOSTBYADDR)
253 set(TCPDUMP_LINK_LIBRARIES ws2_32 ${TCPDUMP_LINK_LIBRARIES})
254 else(LIBWS2_32_HAS_GETHOSTBYADDR)
255 message(FATAL_ERROR "gethostbyaddr is required, but wasn't found")
256 endif(LIBWS2_32_HAS_GETHOSTBYADDR)
258 check_function_exists(gethostbyaddr STDLIBS_HAVE_GETHOSTBYADDR)
259 if(NOT STDLIBS_HAVE_GETHOSTBYADDR)
260 check_library_exists(socket gethostbyaddr "" LIBSOCKET_HAS_GETHOSTBYADDR)
261 if(LIBSOCKET_HAS_GETHOSTBYADDR)
262 set(TCPDUMP_LINK_LIBRARIES ${TCPDUMP_LINK_LIBRARIES} socket)
263 else(LIBSOCKET_HAS_GETHOSTBYADDR)
264 check_library_exists(nsl gethostbyaddr "" LIBNSL_HAS_GETHOSTBYADDR)
265 if(LIBNSL_HAS_GETHOSTBYADDR)
266 set(TCPDUMP_LINK_LIBRARIES ${TCPDUMP_LINK_LIBRARIES} nsl)
267 else(LIBNSL_HAS_GETHOSTBYADDR)
268 message(FATAL_ERROR "gethostbyaddr is required, but wasn't found")
269 endif(LIBNSL_HAS_GETHOSTBYADDR)
270 endif(LIBSOCKET_HAS_GETHOSTBYADDR)
271 endif(NOT STDLIBS_HAVE_GETHOSTBYADDR)
275 # This may require additional libraries.
277 cmake_push_check_state()
278 set(CMAKE_REQUIRED_LIBRARIES ${TCPDUMP_LINK_LIBRARIES})
279 check_function_exists(getservent STDLIBS_HAVE_GETSERVENT)
280 if(STDLIBS_HAVE_GETSERVENT)
281 set(HAVE_GETSERVENT TRUE)
282 else(STDLIBS_HAVE_GETSERVENT)
284 # Some platforms may need -lsocket for getservent.
286 set(CMAKE_REQUIRED_LIBRARIES socket ${TCPDUMP_LINK_LIBRARIES})
287 check_function_exists(getservent LIBSOCKET_HAS_GETSERVENT)
288 if(LIBSOCKET_HAS_GETSERVENT)
289 set(HAVE_GETSERVENT TRUE)
290 set(TCPDUMP_LINK_LIBRARIES socket ${TCPDUMP_LINK_LIBRARIES})
291 endif(LIBSOCKET_HAS_GETSERVENT)
292 endif(STDLIBS_HAVE_GETSERVENT)
293 cmake_pop_check_state()
296 # Make sure we have vsnprintf() and snprintf(); we require them.
297 # We use check_symbol_exists(), as they aren't necessarily external
298 # functions - in Visual Studio, for example, they're inline functions
299 # calling a common external function.
301 check_symbol_exists(vsnprintf "stdio.h" HAVE_VSNPRINTF)
302 if(NOT HAVE_VSNPRINTF)
303 message(FATAL_ERROR "vsnprintf() is required but wasn't found")
304 endif(NOT HAVE_VSNPRINTF)
305 check_symbol_exists(snprintf "stdio.h" HAVE_SNPRINTF)
306 if(NOT HAVE_SNPRINTF)
307 message(FATAL_ERROR "snprintf() is required but wasn't found")
310 check_function_exists(getopt_long HAVE_GETOPT_LONG)
311 check_function_exists(strftime HAVE_STRFTIME)
312 check_function_exists(setlinebuf HAVE_SETLINEBUF)
314 # For Windows, don't need to waste time checking for fork() or vfork().
317 check_function_exists(fork HAVE_FORK)
318 check_function_exists(vfork HAVE_VFORK)
322 # Some platforms may need -lnsl for getrpcbynumber.
324 cmake_push_check_state()
325 set(CMAKE_REQUIRED_LIBRARIES ${TCPDUMP_LINK_LIBRARIES})
326 check_function_exists(getrpcbynumber STDLIBS_HAVE_GETRPCBYNUMBER)
327 if(STDLIBS_HAVE_GETRPCBYNUMBER)
328 set(HAVE_GETRPCBYNUMBER TRUE)
329 else(STDLIBS_HAVE_GETRPCBYNUMBER)
330 set(CMAKE_REQUIRED_LIBRARIES ${TCPDUMP_LINK_LIBRARIES} nsl)
331 check_function_exists(getrpcbynumber LIBNSL_HAS_GETRPCBYNUMBER)
332 if(LIBNSL_HAS_GETRPCBYNUMBER)
333 set(HAVE_GETRPCBYNUMBER TRUE)
334 set(TCPDUMP_LINK_LIBRARIES ${TCPDUMP_LINK_LIBRARIES} nsl)
335 endif(LIBNSL_HAS_GETRPCBYNUMBER)
336 endif(STDLIBS_HAVE_GETRPCBYNUMBER)
337 cmake_pop_check_state()
340 # This requires the libraries we require, as ether_ntohost might be
341 # in one of those libraries. That means we have to do this after
342 # we check for those libraries.
344 # You are in a twisty little maze of UN*Xes, all different.
345 # Some might not have ether_ntohost().
346 # Some might have it and declare it in <net/ethernet.h>.
347 # Some might have it and declare it in <netinet/ether.h>
348 # Some might have it and declare it in <sys/ethernet.h>.
349 # Some might have it and declare it in <arpa/inet.h>.
350 # Some might have it and declare it in <netinet/if_ether.h>.
351 # Some might have it and not declare it in any header file.
353 # Before you is a C compiler.
355 cmake_push_check_state()
356 set(CMAKE_REQUIRED_LIBRARIES ${TCPDUMP_LINK_LIBRARIES})
357 check_function_exists(ether_ntohost HAVE_ETHER_NTOHOST)
358 if(HAVE_ETHER_NTOHOST)
360 # OK, we have ether_ntohost(). We don't check whether it's buggy,
361 # as we assume any system that has CMake is likely to be new enough
362 # that, if it has ether_ntohost(), whatever bug is checked for in
363 # autotools is fixed; we just decide to use it.
365 set(USE_ETHER_NTOHOST TRUE)
368 # Is it declared in <net/ethernet.h>?
370 # This test fails if we don't have <net/ethernet.h> or if we do
371 # but it doesn't declare ether_ntohost().
373 check_symbol_exists(ether_ntohost net/ethernet.h NET_ETHERNET_H_DECLARES_ETHER_NTOHOST)
374 if(NET_ETHERNET_H_DECLARES_ETHER_NTOHOST)
376 # Yes - we have it declared.
378 set(HAVE_DECL_ETHER_NTOHOST TRUE)
383 if(NOT HAVE_DECL_ETHER_NTOHOST)
385 # No - how about <netinet/ether.h>, as on Linux?
387 # This test fails if we don't have <netinet/ether.h>
388 # or if we do but it doesn't declare ether_ntohost().
390 check_symbol_exists(ether_ntohost netinet/ether.h NETINET_ETHER_H_DECLARES_ETHER_NTOHOST)
391 if(NETINET_ETHER_H_DECLARES_ETHER_NTOHOST)
393 # Yes - we have it declared.
395 set(HAVE_DECL_ETHER_NTOHOST TRUE)
401 if(NOT HAVE_DECL_ETHER_NTOHOST)
403 # No - how about <sys/ethernet.h>, as on Solaris 10 and later?
405 # This test fails if we don't have <sys/ethernet.h>
406 # or if we do but it doesn't declare ether_ntohost().
408 check_symbol_exists(ether_ntohost sys/ethernet.h SYS_ETHERNET_H_DECLARES_ETHER_NTOHOST)
409 if(SYS_ETHERNET_H_DECLARES_ETHER_NTOHOST)
411 # Yes - we have it declared.
413 set(HAVE_DECL_ETHER_NTOHOST TRUE)
419 if(NOT HAVE_DECL_ETHER_NTOHOST)
421 # No, how about <arpa/inet.h>, as on AIX?
423 # This test fails if we don't have <arpa/inet.h>
424 # or if we do but it doesn't declare ether_ntohost().
426 check_symbol_exists(ether_ntohost arpa/inet.h ARPA_INET_H_DECLARES_ETHER_NTOHOST)
427 if(ARPA_INET_H_DECLARES_ETHER_NTOHOST)
429 # Yes - we have it declared.
431 set(HAVE_DECL_ETHER_NTOHOST TRUE)
437 if(NOT HAVE_DECL_ETHER_NTOHOST)
439 # No, how about <netinet/if_ether.h>?
440 # On some platforms, it requires <net/if.h> and
441 # <netinet/in.h>, and we always include it with
442 # both of them, so test it with both of them.
444 # This test fails if we don't have <netinet/if_ether.h>
445 # and the headers we include before it, or if we do but
446 # <netinet/if_ether.h> doesn't declare ether_ntohost().
448 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)
449 if(NETINET_IF_ETHER_H_DECLARES_ETHER_NTOHOST)
451 # Yes - we have it declared.
453 set(HAVE_DECL_ETHER_NTOHOST TRUE)
457 # After all that, is ether_ntohost() declared?
459 if(NOT HAVE_DECL_ETHER_NTOHOST)
461 # No, we'll have to declare it ourselves.
462 # Do we have "struct ether_addr" if we include<netinet/if_ether.h>?
464 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)
467 cmake_pop_check_state()
469 check_function_exists(dnet_htoa STDLIBS_HAVE_DNET_HTOA)
470 if(STDLIBS_HAVE_DNET_HTOA)
471 set(HAVE_DNET_HTOA TRUE)
472 else(STDLIBS_HAVE_DNET_HTOA)
473 check_library_exists(dnet dnet_htoa "" HAVE_DNET_HTOA)
475 set(TCPDUMP_LINK_LIBRARIES dnet ${TCPDUMP_LINK_LIBRARIES})
476 endif(HAVE_DNET_HTOA)
477 endif(STDLIBS_HAVE_DNET_HTOA)
480 # OK, we have dnet_htoa(). Do we have netdnet/dnetdb.h?
482 check_include_files("sys/types.h;netdnet/dnetdb.h" HAVE_NETDNET_DNETDB_H)
483 if(HAVE_NETDNET_DNETDB_H)
485 # Yes. Does it declare dnet_htoa()?
487 cmake_push_check_state()
488 set(CMAKE_REQUIRED_LIBRARIES dnet)
489 check_symbol_exists(dnet_htoa "sys/types.h;netdnet/dnetdb.h" NETDNET_DNETDB_H_DECLARES_DNET_HTOA)
490 cmake_pop_check_state()
491 endif(HAVE_NETDNET_DNETDB_H)
494 # Do we have netdnet/dn.h?
496 check_include_file(netdnet/dn.h HAVE_NETDNET_DN_H)
497 if(HAVE_NETDNET_DN_H)
499 # Yes. Does it declare struct dn_naddr?
501 cmake_push_check_state()
502 set(CMAKE_EXTRA_INCLUDE_FILES netdnet/dn.h)
503 check_type_size("struct dn_naddr" STRUCT_DN_NADDR)
504 cmake_pop_check_state()
505 endif(HAVE_NETDNET_DN_H)
506 endif(HAVE_DNET_HTOA)
511 # XXX - there's no check_struct() macro that's like check_struct_has_member()
512 # except that it only checks for the existence of the structure type,
513 # so we use check_struct_has_member() and look for ss_family.
517 # Check for IPv6 support.
518 # We just check for AF_INET6 and struct in6_addr.
520 cmake_push_check_state()
522 set(CMAKE_EXTRA_INCLUDE_FILES sys/types.h ws2tcpip.h)
523 check_symbol_exists(AF_INET6 "sys/types.h;ws2tcpip.h" HAVE_AF_INET6)
525 set(CMAKE_EXTRA_INCLUDE_FILES sys/types.h sys/socket.h netinet/in.h)
526 check_symbol_exists(AF_INET6 "sys/types.h;sys/socket.h;netinet/in.h" HAVE_AF_INET6)
528 check_type_size("struct in6_addr" HAVE_STRUCT_IN6_ADDR)
529 cmake_pop_check_state()
530 if(HAVE_AF_INET6 AND HAVE_STRUCT_IN6_ADDR)
531 set(HAVE_OS_IPV6_SUPPORT TRUE)
532 endif(HAVE_AF_INET6 AND HAVE_STRUCT_IN6_ADDR)
534 ######################################
535 # External dependencies
536 ######################################
539 # libpcap/WinPcap/Npcap.
542 find_package(PCAP REQUIRED)
543 include_directories(${PCAP_INCLUDE_DIRS})
545 cmake_push_check_state()
550 set(CMAKE_REQUIRED_INCLUDES ${PCAP_INCLUDE_DIRS})
553 # Check whether we have pcap/pcap-inttypes.h.
554 # If we do, we use that to get the C99 types defined.
556 check_include_file(pcap/pcap-inttypes.h HAVE_PCAP_PCAP_INTTYPES_H)
559 # Check for various functions in libpcap/WinPcap/Npcap.
561 cmake_push_check_state()
562 set(CMAKE_REQUIRED_LIBRARIES ${PCAP_LIBRARIES})
565 # Check for "pcap_list_datalinks()" and use a substitute version if
566 # it's not present. If it is present, check for "pcap_free_datalinks()";
567 # if it's not present, we don't replace it for now. (We could do so
568 # on UN*X, but not on Windows, where hilarity ensues if a program
569 # built with one version of the MSVC support library tries to free
570 # something allocated by a library built with another version of
571 # the MSVC support library.)
573 check_function_exists(pcap_list_datalinks HAVE_PCAP_LIST_DATALINKS)
574 if(HAVE_PCAP_LIST_DATALINKS)
575 check_function_exists(pcap_free_datalinks HAVE_PCAP_FREE_DATALINKS)
576 endif(HAVE_PCAP_LIST_DATALINKS)
579 # Check for "pcap_datalink_name_to_val()", and use a substitute
580 # version if it's not present. If it is present, check for
581 # "pcap_datalink_val_to_description()", and if we don't have it,
582 # use a substitute version.
584 check_function_exists(pcap_datalink_name_to_val HAVE_PCAP_DATALINK_NAME_TO_VAL)
585 if(HAVE_PCAP_DATALINK_NAME_TO_VAL)
586 check_function_exists(pcap_datalink_val_to_description HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION)
587 endif(HAVE_PCAP_DATALINK_NAME_TO_VAL)
590 # Check for "pcap_set_datalink()"; you can't substitute for it if
591 # it's absent (it has hooks into libpcap), so just define the
592 # HAVE_ value if it's there.
594 check_function_exists(pcap_set_datalink HAVE_PCAP_SET_DATALINK)
597 # Check for "pcap_breakloop()"; you can't substitute for it if
598 # it's absent (it has hooks into the live capture routines),
599 # so just define the HAVE_ value if it's there.
601 check_function_exists(pcap_breakloop HAVE_PCAP_BREAKLOOP)
604 # Check for "pcap_dump_ftell()"; we use a substitute version
605 # if it's not present.
607 check_function_exists(pcap_dump_ftell HAVE_PCAP_DUMP_FTELL)
610 # Do we have the new open API? Check for pcap_create() and for
611 # pcap_statustostr(), and assume that, if we have both of them,
612 # we also have pcap_activate() and the other new routines
613 # introduced in libpcap 1.0.0. (We check for pcap_statustostr()
614 # as well, because WinPcap 4.1.3 screwed up and exported pcap_create()
615 # but not other routines such as pcap_statustostr(), even though it
616 # defined them and even though you really want pcap_statustostr() to
617 # get strings corresponding to some of the status returns from the
620 check_function_exists(pcap_statustostr HAVE_PCAP_STATUSTOSTR)
622 # If we don't have pcap_statustostr(), don't check for pcap_create(),
623 # so we pretend we don't have it.
625 if(HAVE_PCAP_STATUSTOSTR)
626 check_function_exists(pcap_create HAVE_PCAP_CREATE)
627 endif(HAVE_PCAP_STATUSTOSTR)
630 # OK, do we have pcap_set_tstamp_type? If so, assume we have
631 # pcap_list_tstamp_types and pcap_free_tstamp_types as well.
633 check_function_exists(pcap_set_tstamp_type HAVE_PCAP_SET_TSTAMP_TYPE)
636 # And do we have pcap_set_tstamp_precision? If so, we assume
637 # we also have pcap_open_offline_with_tstamp_precision.
639 check_function_exists(pcap_set_tstamp_precision HAVE_PCAP_SET_TSTAMP_PRECISION)
640 endif(HAVE_PCAP_CREATE)
643 # Check for a miscellaneous collection of functions which we use
646 check_function_exists(pcap_findalldevs HAVE_PCAP_FINDALLDEVS)
647 if(HAVE_PCAP_FINDALLDEVS)
649 # Check for libpcap having pcap_findalldevs() but the pcap.h header
650 # not having pcap_if_t; some versions of Mac OS X shipped with pcap.h
651 # from 0.6 and libpcap 0.8, so that libpcap had pcap_findalldevs but
652 # pcap.h didn't have pcap_if_t.
654 cmake_push_check_state()
655 set(CMAKE_REQUIRED_INCLUDES ${PCAP_INCLUDE_DIRS})
656 set(CMAKE_EXTRA_INCLUDE_FILES pcap.h)
657 check_type_size(pcap_if_t PCAP_IF_T)
658 cmake_pop_check_state()
659 endif(HAVE_PCAP_FINDALLDEVS)
660 check_function_exists(pcap_dump_flush HAVE_PCAP_DUMP_FLUSH)
661 check_function_exists(pcap_lib_version HAVE_PCAP_LIB_VERSION)
662 if(NOT HAVE_PCAP_LIB_VERSION)
663 # Check for the pcap_version string variable and set HAVE_PCAP_VERSION
664 endif(NOT HAVE_PCAP_LIB_VERSION)
665 check_function_exists(pcap_setdirection HAVE_PCAP_SETDIRECTION)
666 check_function_exists(pcap_set_immediate_mode HAVE_PCAP_SET_IMMEDIATE_MODE)
667 check_function_exists(pcap_dump_ftell64 HAVE_PCAP_DUMP_FTELL64)
668 check_function_exists(pcap_open HAVE_PCAP_OPEN)
669 check_function_exists(pcap_findalldevs_ex HAVE_PCAP_FINDALLDEVS_EX)
672 # On Windows, check for pcap_wsockinit(); if we don't have it, check for
676 check_function_exists(pcap_wsockinit HAVE_PCAP_WSOCKINIT)
677 if(NOT HAVE_PCAP_WSOCKINIT)
678 check_function_exists(wsockinit HAVE_WSOCKINIT)
679 endif(NOT HAVE_PCAP_WSOCKINIT)
683 # Check for special debugging functions
685 check_function_exists(pcap_set_parser_debug HAVE_PCAP_SET_PARSER_DEBUG)
686 if(NOT HAVE_PCAP_SET_PARSER_DEBUG)
687 # Check whether libpcap defines pcap_debug or yydebug
688 check_variable_exists(pcap_debug HAVE_PCAP_DEBUG)
689 if(NOT HAVE_PCAP_DEBUG)
690 check_variable_exists(yydebug HAVE_YYDEBUG)
691 endif(NOT HAVE_PCAP_DEBUG)
692 endif(NOT HAVE_PCAP_SET_PARSER_DEBUG)
694 check_function_exists(pcap_set_optimizer_debug HAVE_PCAP_SET_OPTIMIZER_DEBUG)
695 check_function_exists(bpf_dump HAVE_BPF_DUMP)
697 cmake_pop_check_state()
702 include_directories(SYSTEM ${PCAP_INCLUDE_DIRS})
703 set(TCPDUMP_LINK_LIBRARIES ${PCAP_LIBRARIES} ${TCPDUMP_LINK_LIBRARIES})
706 # Optional libraries.
715 include_directories(SYSTEM ${SMI_INCLUDE_DIRS})
716 set(TCPDUMP_LINK_LIBRARIES ${TCPDUMP_LINK_LIBRARIES} ${SMI_LIBRARIES})
722 # OpenSSL/libressl libcrypto.
728 # Check for some headers and functions.
730 cmake_push_check_state()
731 set(CMAKE_REQUIRED_LIBRARIES crypto)
733 check_include_file(openssl/evp.h HAVE_OPENSSL_EVP_H)
736 # 1) do we have EVP_CIPHER_CTX_new?
737 # If so, we use it to allocate an EVP_CIPHER_CTX, as
738 # EVP_CIPHER_CTX may be opaque; otherwise, we allocate
741 check_function_exists(EVP_CIPHER_CTX_new HAVE_EVP_CIPHER_CTX_NEW)
744 # 2) do we have EVP_CipherInit_ex()?
745 # If so, we use it, because we need to be able to make two
746 # "initialize the cipher" calls, one with the cipher and key,
747 # and one with the IV, and, as of OpenSSL 1.1, You Can't Do That
748 # with EVP_CipherInit(), because a call to EVP_CipherInit() will
749 # unconditionally clear the context, and if you don't supply a
750 # cipher, it'll clear the cipher, rendering the context unusable
751 # and causing a crash.
753 check_function_exists(EVP_CipherInit_ex HAVE_EVP_CIPHERINIT_EX)
755 cmake_pop_check_state()
760 include_directories(SYSTEM ${CRYPTO_INCLUDE_DIRS})
761 set(TCPDUMP_LINK_LIBRARIES ${TCPDUMP_LINK_LIBRARIES} ${CRYPTO_LIBRARIES})
762 set(HAVE_LIBCRYPTO ON)
767 # Capsicum sandboxing.
768 # Some of this is in the system library, some of it is in other libraries.
771 check_include_files("sys/capsicum.h" HAVE_SYS_CAPSICUM_H)
772 if(HAVE_SYS_CAPSICUM_H)
773 check_function_exists(cap_enter HAVE_CAP_ENTER)
774 check_function_exists(cap_rights_limit HAVE_CAP_RIGHTS_LIMIT)
775 check_function_exists(cap_ioctls_limit HAVE_CAP_IOCTLS_LIMIT)
776 check_function_exists(openat HAVE_OPENAT)
777 if(HAVE_CAP_ENTER AND HAVE_CAP_RIGHTS_LIMIT AND
778 HAVE_CAP_IOCTLS_LIMIT AND HAVE_OPENAT)
780 # OK, we have the functions we need to support Capsicum.
782 set(HAVE_CAPSICUM TRUE)
785 # OK, can we use Casper?
787 check_library_exists(casper cap_init "" HAVE_CAP_INIT)
789 cmake_push_check_state()
790 set(CMAKE_REQUIRED_LIBRARIES casper)
791 check_library_exists(cap_dns cap_gethostbyaddr "" HAVE_CAP_GETHOSTBYADDR)
792 cmake_pop_check_state()
793 if(HAVE_CAP_GETHOSTBYADDR)
794 set(HAVE_CASPER TRUE)
795 set(TCPDUMP_LINK_LIBRARIES ${TCPDUMP_LINK_LIBRARIES} casper cap_dns)
796 endif(HAVE_CAP_GETHOSTBYADDR)
798 endif(HAVE_CAP_ENTER AND HAVE_CAP_RIGHTS_LIMIT AND
799 HAVE_CAP_IOCTLS_LIMIT AND HAVE_OPENAT)
800 endif(HAVE_SYS_CAPSICUM_H)
807 check_include_file(cap-ng.h HAVE_CAP_NG_H)
808 check_library_exists(cap-ng capng_change_id "" HAVE_LIBCAP_NG)
810 set(TCPDUMP_LINK_LIBRARIES ${TCPDUMP_LINK_LIBRARIES} cap-ng)
811 endif(HAVE_LIBCAP_NG)
814 ###################################################################
816 ###################################################################
819 # Check and add warning options if we have a .devel file.
821 if(EXISTS ${CMAKE_SOURCE_DIR}/.devel OR EXISTS ${CMAKE_BINARY_DIR}/.devel)
825 if(MSVC AND NOT ${CMAKE_C_COMPILER} MATCHES "clang*")
827 # MSVC, with Microsoft's front end and code generator.
828 # "MSVC" is also set for Microsoft's compiler with a Clang
829 # front end and their code generator ("Clang/C2"), so we
830 # check for clang.exe and treat that differently.
832 check_and_add_compiler_option(-Wall)
834 # Disable some pointless warnings that /Wall turns on.
836 # Unfortunately, MSVC does not appear to have an equivalent
837 # to "__attribute__((unused))" to mark a particular function
838 # parameter as being known to be unused, so that the compiler
839 # won't warn about it (for example, the function might have
840 # that parameter because a pointer to it is being used, and
841 # the signature of that function includes that parameter).
842 # C++ lets you give a parameter a type but no name, but C
845 check_and_add_compiler_option(-wd4100)
847 # In theory, we care whether somebody uses f() rather than
848 # f(void) to declare a function with no arguments, but, in
849 # practice, there are places in the Windows header files
850 # that appear to do that, so we squelch that warning.
852 check_and_add_compiler_option(-wd4255)
854 # Windows FD_SET() generates this, so we suppress it.
856 check_and_add_compiler_option(-wd4548)
858 # Perhaps testing something #defined to be 0 with #ifdef is an
859 # error, and it should be tested with #if, but perhaps it's
860 # not, and Microsoft does that in its headers, so we squelch
863 check_and_add_compiler_option(-wd4574)
865 # The Windows headers also test not-defined values in #if, so
866 # we don't want warnings about that, either.
868 check_and_add_compiler_option(-wd4668)
870 # We do *not* care whether some function is, or isn't, going to be
873 check_and_add_compiler_option(-wd4710)
874 check_and_add_compiler_option(-wd4711)
876 # We do *not* care whether we're adding padding bytes after
879 check_and_add_compiler_option(-wd4820)
881 # We do *not* care about every single place the compiler would
882 # have inserted Spectre mitigation if only we had told it to
883 # do so with /Qspectre. I guess the theory is that it's seeing
884 # bounds checks that would prevent out-of-bounds loads and that
885 # those out-of-bounds loads could be done speculatively and that
886 # the Spectre attack could detect the value of the out-of-bounds
887 # data *if* it's within our address space, but unless I'm
888 # missing something I don't see that as being any form of
891 # XXX - add /Qspectre if that is really worth doing.
893 check_and_add_compiler_option(-wd5045)
895 # We do *not* care whether a structure had padding added at
896 # the end because of __declspec(align) - *we* don't use
897 # __declspec(align), because the only structures whose layout
898 # we precisely specify are those that get overlayed on packet
899 # data, and in those every element is an array of octets so
900 # that we have full control over the size and aligmnet, and,
901 # apparently, jmp_buf has such a declaration on x86, meaning
902 # that everything that includes netdissect.h, i.e. almost every
903 # file in tcpdump, gets a warning.
905 check_and_add_compiler_option(-wd4324)
908 # Other compilers, including MSVC with a Clang front end and
909 # Microsoft's code generator. We currently treat them as if
910 # they might support GCC-style -W options.
912 check_and_add_compiler_option(-W)
913 check_and_add_compiler_option(-Wall)
914 check_and_add_compiler_option(-Wassign-enum)
915 check_and_add_compiler_option(-Wcast-qual)
916 check_and_add_compiler_option(-Wmissing-prototypes)
917 check_and_add_compiler_option(-Wold-style-definition)
918 check_and_add_compiler_option(-Wpedantic)
919 check_and_add_compiler_option(-Wpointer-arith)
920 check_and_add_compiler_option(-Wpointer-sign)
921 check_and_add_compiler_option(-Wshadow)
922 check_and_add_compiler_option(-Wsign-compare)
923 check_and_add_compiler_option(-Wstrict-prototypes)
924 check_and_add_compiler_option(-Wunreachable-code-return)
925 check_and_add_compiler_option(-Wused-but-marked-unused)
926 check_and_add_compiler_option(-Wwrite-strings)
930 ######################################
932 ######################################
936 # We allow the SMB dissector to be omitted.
938 set(LOCALSRC ${LOCALSRC}
943 set(NETDISSECT_SOURCE_LIST_C
1054 print-openflow-1.0.c
1118 # Replace missing functions
1120 foreach(FUNC strlcat strlcpy strdup strsep getservent getopt_long)
1121 string(TOUPPER ${FUNC} FUNC_UPPERCASE)
1122 set(HAVE_FUNC_UPPERCASE HAVE_${FUNC_UPPERCASE})
1123 if(NOT ${HAVE_FUNC_UPPERCASE})
1124 set(NETDISSECT_SOURCE_LIST_C ${NETDISSECT_SOURCE_LIST_C} missing/${FUNC}.c)
1128 add_library(netdissect STATIC
1129 ${NETDISSECT_SOURCE_LIST_C}
1131 if(NOT C_ADDITIONAL_FLAGS STREQUAL "")
1132 set_target_properties(netdissect PROPERTIES COMPILE_FLAGS ${C_ADDITIONAL_FLAGS})
1135 set(TCPDUMP_SOURCE_LIST_C tcpdump.c)
1137 if(NOT HAVE_BPF_DUMP)
1138 set(TCPDUMP_SOURCE_LIST_C ${TCPDUMP_SOURCE_LIST_C} bpf_dump.c)
1139 endif(NOT HAVE_BPF_DUMP)
1140 if(NOT HAVE_PCAP_DUMP_FTELL)
1141 set(TCPDUMP_SOURCE_LIST_C ${TCPDUMP_SOURCE_LIST_C} missing/pcap_dump_ftell.c)
1142 endif(NOT HAVE_PCAP_DUMP_FTELL)
1144 if(NOT HAVE_PCAP_LIST_DATALINKS)
1145 set(TCPDUMP_SOURCE_LIST_C ${TCPDUMP_SOURCE_LIST_C} missing/datalinks.c)
1146 endif(NOT HAVE_PCAP_LIST_DATALINKS)
1148 if((NOT HAVE_PCAP_DATALINK_NAME_TO_VAL) OR (NOT HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION))
1149 set(TCPDUMP_SOURCE_LIST_C ${TCPDUMP_SOURCE_LIST_C} missing/dlnames.c)
1150 endif((NOT HAVE_PCAP_DATALINK_NAME_TO_VAL) OR (NOT HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION))
1152 set(PROJECT_SOURCE_LIST_C ${NETDISSECT_SOURCE_LIST_C} ${TCPDUMP_SOURCE_LIST_C})
1154 file(GLOB PROJECT_SOURCE_LIST_H
1158 source_group("Source Files" FILES ${PROJECT_SOURCE_LIST_C})
1159 source_group("Header Files" FILES ${PROJECT_SOURCE_LIST_H})
1161 ######################################
1163 ######################################
1165 add_executable(tcpdump ${TCPDUMP_SOURCE_LIST_C})
1166 if(NOT C_ADDITIONAL_FLAGS STREQUAL "")
1167 set_target_properties(tcpdump PROPERTIES COMPILE_FLAGS ${C_ADDITIONAL_FLAGS})
1169 target_link_libraries(tcpdump netdissect ${TCPDUMP_LINK_LIBRARIES})
1171 ######################################
1172 # Write out the config.h file
1173 ######################################
1175 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmakeconfig.h.in ${CMAKE_CURRENT_BINARY_DIR}/config.h)
1177 ######################################
1178 # Install tcpdump and man pages
1179 ######################################
1182 # "Define GNU standard installation directories", which actually
1183 # are also defined, to some degree, by autotools, and at least
1184 # some of which are general UN*X conventions.
1186 include(GNUInstallDirs)
1188 set(MAN1_EXPAND tcpdump.1.in)
1191 # XXX TODO where to install on Windows?
1193 install(TARGETS tcpdump DESTINATION sbin)
1196 # On UN*X, and on Windows when not using MSVC, process man pages and
1197 # arrange that they be installed.
1202 # For each section of the manual for which we have man pages
1203 # that require macro expansion, do the expansion.
1206 foreach(TEMPLATE_MANPAGE ${MAN1_EXPAND})
1207 string(REPLACE ".in" "" MANPAGE ${TEMPLATE_MANPAGE})
1208 configure_file(${CMAKE_SOURCE_DIR}/${TEMPLATE_MANPAGE} ${CMAKE_CURRENT_BINARY_DIR}/${MANPAGE} @ONLY)
1209 set(MAN1 ${MAN1} ${CMAKE_CURRENT_BINARY_DIR}/${MANPAGE})
1210 endforeach(TEMPLATE_MANPAGE)
1211 install(FILES ${MAN1} DESTINATION ${CMAKE_INSTALL_MANDIR}/man1)
1216 "${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in"
1217 "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"
1220 add_custom_target(uninstall
1221 COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)
1226 add_custom_target(check
1227 COMMAND ./TESTrun.sh
1228 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/tests)