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