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