3 # We need 3.12 or later, so that we can set policy CMP0074; see
6 cmake_minimum_required(VERSION 3.12)
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;
16 # otherwise, if it's a version less than 3.10, require only
17 # 3.5, just in case somebody is configuring with CMake
18 # on a "long-term support" version # of some OS and that
19 # version supplies an older version of CMake;
21 # otherwise, require 3.10, so we don't get messages warning
22 # that support for versions of CMake lower than 3.10 is
25 if(CMAKE_VERSION VERSION_LESS "3.5")
26 cmake_minimum_required(VERSION 2.8.12)
27 elseif(CMAKE_VERSION VERSION_LESS "3.10")
28 cmake_minimum_required(VERSION 3.5)
30 cmake_minimum_required(VERSION 3.10)
35 # Apple doesn't build with an install_name starting with @rpath, and
36 # neither do we with autotools; don't do so with CMake, either, and
37 # suppress warnings about that.
39 # Setting CMAKE_MACOSX_RPATH to FALSE uses the old behavior,
40 # but removes the POLICY CMP0042 OLD deprecated warning.
41 # See https://cmake.org/cmake/help/latest/policy/CMP0042.html
43 if (NOT DEFINED CMAKE_MACOSX_RPATH)
44 set(CMAKE_MACOSX_RPATH FALSE)
47 cmake_policy(SET CMP0042 NEW)
51 # Squelch noise about quoted strings in if() statements.
52 # WE KNOW WHAT WE'RE DOING, WE'RE DOING EVERYTHING THE WAY THAT NEWER
53 # VERSIONS OF CMAKE EXPECT BY DEFAULT, DON'T WASTE OUR TIME WITH NOISE.
56 cmake_policy(SET CMP0054 NEW)
60 # We want find_file() and find_library() to honor {packagename}_ROOT,
61 # as that appears to be the only way, with the Visual Studio 2019 IDE
62 # and its CMake support, to tell CMake where to look for the Npcap
66 cmake_policy(SET CMP0074 NEW)
70 # We want check_include_file() to honor CMAKE_REQUIRED_LIBRARIES; see
71 # the big comment before the check_include_file() test for
72 # infiniband/verbs.h for the reason.
75 cmake_policy(SET CMP0075 NEW)
78 set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules)
81 # We explicitly indicate what languages are used in libpcap to avoid
82 # checking for a C++ compiler.
84 # One reason to avoid that check is that there's no need to waste
85 # configuration time performing it.
87 # Another reason is that:
89 # CMake will try to determine the sizes of some data types, including
90 # void *, early in the process of configuration; apparently, it's done
91 # as part of processing the project() command.
93 # At least as of CMake 2.8.6, it does so by checking the size of
94 # "void *" in C, setting CMAKE_C_SIZEOF_DATA_PTR based on that,
95 # setting CMAKE_SIZEOF_VOID_P to that, and then checking the size
96 # of "void *" in C++, setting CMAKE_CXX_SIZEOF_DATA_PTR based on
97 # that, and then setting CMAKE_SIZEOF_VOID_P to *that*.
99 # The compile tests include whatever C flags may have been provided
100 # to CMake in the CFLAGS and CXXFLAGS environment variables.
102 # If you set an architecture flag such as -m32 or -m64 in CFLAGS
103 # but *not* in CXXFLAGS, the size for C++ will win, and hilarity
106 # Or if, at least on Solaris, you have a newer version of GCC
107 # installed, but *not* a newer version of G++, and you have Oracle
108 # Studio installed, it will find GCC, which will default to building
109 # 64-bit, and Oracle Studio's C++ compiler, which will default to
110 # building 32-bit, the size for C++ will win, and, again, hilarity
116 # Export the size of void * as SIZEOF_VOID_P so that it can be
119 set(SIZEOF_VOID_P "${CMAKE_SIZEOF_VOID_P}")
122 # Setting CMAKE_MACOSX_RPATH to FALSE causes the installed
123 # libpcap.A.dylib to have just libpcap.A.dylib as the install
124 # name; Apple built libpcap with an install_name of /usr/lib/libpcap.A.dylib
125 # (back when they still shipped individual system dylibs rather than
126 # shipping a pre-built shared library cache, at least), and we do the
127 # same with autotools; do the same with CMake.
129 if (NOT DEFINED CMAKE_INSTALL_NAME_DIR)
130 set(CMAKE_INSTALL_NAME_DIR ${CMAKE_INSTALL_PREFIX}/lib)
134 # For getting raw lists of --libs and --libs --static information from a
137 # In CMake up to 2.8.12, pkg_check_modules() sets:
139 # <XPREFIX>_LIBRARIES, which is a list of library names to which, on
140 # a UN*X, -l can be prefixed - i.e., names, without extensions,
141 # rather than full paths to the file.
142 # <XPREFIX>_LIBRARY_DIRS, which is a list of paths to directories
143 # containing the libraries, to which, on a UN*X, -L can be
145 # <XPREFIX>_LDFLAGS, which is a list of *all* required linker flags
146 # <XPREFIX>_LDFLAGS_OTHER, which is a list of all linker flags other
147 # than -l and -L flags
149 # In 3.0 (at least as of 3.0.2), it also sets:
151 # <XPREFIX>_LINK_LIBRARIES, which is a list of full paths to the
154 # but if <XPREFIX> is <PREFIX>_STATIC, <XPREFIX>_LINK_LIBRARIES is
155 # currently not set by CMake.
157 # Unfortunately, pkg_check_modules() sets the
158 # PKG_CONFIG_ALLOW_SYSTEM_LIBS environment variable when running
159 # pkg-config, so the output of --libs, etc. may include a -L for the
160 # system library, which we do *NOT* want to put in our libpcap.pc and
163 # So we just run pkg-config ourselves, so that we get its output
164 # directly without any processing by CMake.
166 macro(pkg_get_link_info _prefix _package)
167 if (PKG_CONFIG_EXECUTABLE)
169 # Get the --libs information.
171 # We force PKG_CONFIG_ALLOW_SYSTEM_LIBS to be undefined, as
172 # at least some versions of CMake appear to define it in
173 # pkg_check_modules() before running pkg-config and *not* undefine
174 # it after running it.
176 unset(ENV{PKG_CONFIG_ALLOW_SYSTEM_LIBS})
177 set(_pkg_config_result "")
179 COMMAND ${PKG_CONFIG_EXECUTABLE} "--libs" ${_package}
180 OUTPUT_VARIABLE _pkg_config_result
181 RESULT_VARIABLE _pkg_config_failed
182 OUTPUT_STRIP_TRAILING_WHITESPACE)
184 if (_pkg_config_failed)
186 # pkg-config failed; assume that means that there is no such
187 # package for it to find. XXX - what do we do here?
189 set(${_prefix}_FOUND_WITH_PKG_CONFIG FALSE)
192 # pkg-config succeeded; replace CR and LF with spaces.
194 string(REGEX REPLACE "[\r\n]" " " ${_prefix}_LIBS "${_pkg_config_result}")
197 # Now get the --libs --static information.
199 set(_pkg_config_result "")
201 COMMAND ${PKG_CONFIG_EXECUTABLE} "--libs" "--static" ${_package}
202 OUTPUT_VARIABLE _pkg_config_result
203 RESULT_VARIABLE _pkg_config_failed
204 OUTPUT_STRIP_TRAILING_WHITESPACE)
206 if (_pkg_config_failed)
208 # pkg-config failed; assume that means that there is no such
209 # package for it to find. XXX - what do we do here?
211 set(${_prefix}_FOUND_WITH_PKG_CONFIG FALSE)
214 # pkg-config succeeded; replace CR and LF with spaces.
216 string(REGEX REPLACE "[\r\n]" " " ${_prefix}_LIBS_STATIC "${_pkg_config_result}")
219 # List this package in its PACKAGE_NAME variable.
221 set(${_prefix}_PACKAGE_NAME "${_package}")
226 set(${_prefix}_FOUND_WITH_PKG_CONFIG TRUE)
232 macro(get_link_info_from_library_path _library_prefix _library_name)
233 if(NOT ${_library_prefix}_LIBRARY STREQUAL "${_library_prefix}_LIBRARY-NOTFOUND")
234 get_filename_component(_lib_directory "${${_library_prefix}_LIBRARY}}" DIRECTORY)
237 # The closest thing to a list of "system library directories" in
238 # which the linker will, by default, search for libraries appears to
239 # be CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES, so that's what we use
240 # when we're trying to construct a -L argument, for insertion into
241 # pcap-config and libpcap.pc, for a library upon which we depend.
243 # In some versions of CMake it appears to have duplicate entries,
244 # but that shouldn't affect a search for a directory in that list.
246 list(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${_lib_directory}" _lib_index)
247 if(_lib_index EQUAL -1)
249 # No, so add a -L flag to get the linker to search in that
252 set(${_library_prefix}_LIBS "-L${_lib_directory}")
253 set(${_library_prefix}_LIBS_STATIC "-L${_lib_directory}")
254 set(${_libraryprefix}_LIBS_PRIVATE "-L${_lib_directory}")
256 set(${_library_prefix}_LIBS "${${_library_prefix}_LIBS} -l${_library_name}")
257 set(${_library_prefix}_LIBS_STATIC "${${_library_prefix}_LIBS} -l${_library_name}")
258 set(${_library_prefix}_LIBS_PRIVATE "${${_library_prefix}_LIBS} -l${_library_name}")
263 # Show the bit width for which we're compiling.
264 # This can help debug problems if you're dealing with a compiler that
265 # defaults to generating 32-bit code even when running on a 64-bit
266 # platform, and where that platform may provide only 64-bit versions of
267 # libraries that we might use (looking at *you*, Oracle Studio!).
269 if(CMAKE_SIZEOF_VOID_P EQUAL 4)
270 message(STATUS "Building 32-bit")
271 elseif(CMAKE_SIZEOF_VOID_P EQUAL 8)
272 message(STATUS "Building 64-bit")
276 # Solaris pkg-config is annoying. For at least one package (D-Bus, I'm
277 # looking at *you*!), there are separate include files for 32-bit and
278 # 64-bit builds (I guess using "unsigned long long" as a 64-bit integer
279 # type on a 64-bit build is like crossing the beams or something), and
280 # there are two separate .pc files, so if we're doing a 32-bit build we
281 # should make sure we look in /usr/lib/pkgconfig for .pc files and if
282 # we're doing a 64-bit build we should make sure we look in
283 # /usr/lib/amd64/pkgconfig for .pc files.
285 if(CMAKE_SYSTEM_NAME STREQUAL "SunOS" AND CMAKE_SYSTEM_VERSION MATCHES "5[.][0-9.]*")
287 # Note: string(REPLACE) does not appear to support using ENV{...}
288 # as an argument, so we set a variable and then use set() to set
289 # the environment variable.
291 if(CMAKE_SIZEOF_VOID_P EQUAL 8)
293 # 64-bit build. If /usr/lib/pkgconfig appears in the path,
294 # prepend /usr/lib/amd64/pkgconfig to it; otherwise,
295 # put /usr/lib/amd64 at the end.
297 if((NOT DEFINED ENV{PKG_CONFIG_PATH}) OR "$ENV{PKG_CONFIG_PATH}" EQUAL "")
299 # Not set, or empty. Set it to /usr/lib/amd64/pkgconfig.
301 set(fixed_path "/usr/lib/amd64/pkgconfig")
302 elseif("$ENV{PKG_CONFIG_PATH}" MATCHES "/usr/lib/pkgconfig")
304 # It contains /usr/lib/pkgconfig. Prepend
305 # /usr/lib/amd64/pkgconfig to /usr/lib/pkgconfig.
307 string(REPLACE "/usr/lib/pkgconfig"
308 "/usr/lib/amd64/pkgconfig:/usr/lib/pkgconfig"
309 fixed_path "$ENV{PKG_CONFIG_PATH}")
312 # Not empty, but doesn't contain /usr/lib/pkgconfig.
313 # Append /usr/lib/amd64/pkgconfig to it.
315 set(fixed_path "$ENV{PKG_CONFIG_PATH}:/usr/lib/amd64/pkgconfig")
317 set(ENV{PKG_CONFIG_PATH} "${fixed_path}")
318 elseif(CMAKE_SIZEOF_VOID_P EQUAL 4)
320 # 32-bit build. If /usr/amd64/lib/pkgconfig appears in the path,
321 # prepend /usr/lib/pkgconfig to it.
323 if("$ENV{PKG_CONFIG_PATH}" MATCHES "/usr/lib/amd64/pkgconfig")
325 # It contains /usr/lib/amd64/pkgconfig. Prepend
326 # /usr/lib/pkgconfig to /usr/lib/amd64/pkgconfig.
328 string(REPLACE "/usr/lib/amd64/pkgconfig"
329 "/usr/lib/pkgconfig:/usr/lib/amd64/pkgconfig"
330 fixed_path "$ENV{PKG_CONFIG_PATH}")
331 set(ENV{PKG_CONFIG_PATH} "${fixed_path}")
336 include(CheckCCompilerFlag)
339 # For checking if a compiler flag works and adding it if it does.
341 macro(check_and_add_compiler_option _option)
342 message(STATUS "Checking C compiler flag ${_option}")
343 string(REPLACE "=" "-" _temp_option_variable ${_option})
344 string(REGEX REPLACE "^-" "" _option_variable ${_temp_option_variable})
345 check_c_compiler_flag("${_option}" ${_option_variable})
346 if(${${_option_variable}})
347 set(C_ADDITIONAL_FLAGS "${C_ADDITIONAL_FLAGS} ${_option}")
352 # If we're building with Visual Studio, we require Visual Studio 2015,
353 # in order to get sufficient C99 compatibility. Check for that.
355 # If not, try the appropriate flag for the compiler to enable C99
358 set(C_ADDITIONAL_FLAGS "")
360 if(MSVC_VERSION LESS 1900)
361 message(FATAL_ERROR "Visual Studio 2015 or later is required")
365 # Treat source files as being in UTF-8 with MSVC if it's not using
366 # the Clang front end.
367 # We assume that UTF-8 source is OK with other compilers and with
368 # MSVC if it's using the Clang front end.
370 if(NOT ${CMAKE_C_COMPILER} MATCHES "clang*")
371 set(C_ADDITIONAL_FLAGS "${C_ADDITIONAL_FLAGS} /utf-8")
372 endif(NOT ${CMAKE_C_COMPILER} MATCHES "clang*")
375 # For checking if a compiler flag works, failing if it doesn't,
376 # and adding it otherwise.
378 macro(require_and_add_compiler_option _option)
379 message(STATUS "Checking C compiler flag ${_option}")
380 string(REPLACE "=" "-" _temp_option_variable ${_option})
381 string(REGEX REPLACE "^-" "" _option_variable ${_temp_option_variable})
382 check_c_compiler_flag("${_option}" ${_option_variable})
383 if(${${_option_variable}})
384 set(C_ADDITIONAL_FLAGS "${C_ADDITIONAL_FLAGS} ${_option}")
386 message(FATAL_ERROR "C99 support is required, but the compiler doesn't support a compiler flag to enable it")
391 # Try to enable as many C99 features as we can.
392 # At minimum, we want C++/C99-style // comments.
394 # Newer versions of compilers might default to supporting C99, but
395 # older versions may require a special flag.
397 # Prior to CMake 3.1, setting CMAKE_C_STANDARD will not have any effect,
398 # so, unless and until we require CMake 3.1 or later, we have to do it
399 # ourselves on pre-3.1 CMake, so we just do it ourselves on all versions
402 # Note: with CMake 3.1 through 3.5, the only compilers for which CMake
403 # handles CMAKE_C_STANDARD are GCC and Clang. 3.6 adds support only
404 # for Intel C; 3.9 adds support for PGI C, Sun C, and IBM XL C, and
405 # 3.10 adds support for Cray C and IAR C, but no version of CMake has
406 # support for HP C. Therefore, even if we use CMAKE_C_STANDARD with
407 # compilers for which CMake supports it, we may still have to do it
408 # ourselves on other compilers.
410 # See the CMake documentation for the CMAKE_<LANG>_COMPILER_ID variables
411 # for a list of compiler IDs.
413 # XXX - this just tests whether the option works, fails if it doesn't,
414 # and adds it if it does. We don't test whether it's necessary in order
415 # to get the C99 features that we use, or whether, if it's used, it
416 # enables all the features that we require.
418 if(CMAKE_C_COMPILER_ID MATCHES "GNU" OR
419 CMAKE_C_COMPILER_ID MATCHES "Clang")
420 require_and_add_compiler_option("-std=gnu99")
421 elseif(CMAKE_C_COMPILER_ID MATCHES "XL")
423 # We want support for extensions picked up for GNU C compatibility,
424 # so we use -qlanglvl=extc99.
426 require_and_add_compiler_option("-qlanglvl=extc99")
427 elseif(CMAKE_C_COMPILER_ID MATCHES "HP")
428 require_and_add_compiler_option("-AC99")
429 elseif(CMAKE_C_COMPILER_ID MATCHES "Sun")
430 require_and_add_compiler_option("-xc99")
431 elseif(CMAKE_C_COMPILER_ID MATCHES "Intel")
432 require_and_add_compiler_option("-c99")
437 # If we're building with MinGW, we need to specify _WIN32_WINNT as
438 # 0x0600 ("NT 6.0", a/k/a Vista/Windows Server 2008) or higher
439 # in order to get the full IPv6 API, including inet_ntop(), and we
440 # need to specify it as 0x0601 ("NT 6.1", a/k/a Windows 7) or higher
441 # in order to get NdisMediumIP.
443 # NOTE: pcap does *NOT* work with msvcrt.dll; it must link with
444 # a newer version of the C library, i.e. Visual Studio 2015 or
445 # later, as it depends on C99 features introduced in VS 2015.
448 add_definitions(-D_WIN32_WINNT=0x0601)
452 # Build all runtimes in the top-level binary directory; that way,
453 # on Windows, the executables will be in the same directory as
454 # the DLLs, so the system will find pcap.dll when any of the
455 # executables are run.
457 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/run)
459 ###################################################################
461 ###################################################################
465 # On Windows, allow the library name to be overridden, for the
466 # benefit of projects that combine libpcap with their own
467 # kernel-mode code to support capturing.
469 set(LIBRARY_NAME pcap CACHE STRING "Library name")
472 # On UN*X, it's always been libpcap.
474 set(LIBRARY_NAME pcap)
478 option(USE_STATIC_RT "Use static Runtime" ON)
480 option(BUILD_SHARED_LIBS "Build shared libraries" ON)
481 set(dpdk_ROOT "" CACHE PATH "Path to directory with include and lib subdirectories for DPDK")
483 set(Packet_ROOT "" CACHE PATH "Path to directory with include and lib subdirectories for packet.dll")
486 option(ENABLE_PROFILING "Enable code profiling" OFF)
488 # To pacify those who hate the protochain instruction
489 option(ENABLE_PROTOCHAIN "Enable protochain instruction" ON)
490 if(ENABLE_PROTOCHAIN)
491 set(NO_PROTOCHAIN OFF)
493 set(NO_PROTOCHAIN ON)
497 # Start out with the capture mechanism type unspecified; the user
498 # can explicitly specify it and, if they don't, we'll pick an
501 set(PCAP_TYPE "" CACHE STRING "Packet capture type")
504 # Default to having remote capture support on Windows and, for now, to
505 # not having it on UN*X.
508 option(ENABLE_REMOTE "Enable remote capture" ON)
510 option(ENABLE_REMOTE "Enable remote capture" OFF)
513 if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
514 option(BUILD_WITH_LIBNL "Build with libnl" ON)
518 # Additional capture modules.
520 if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
521 option(DISABLE_LINUX_USBMON "Disable Linux usbmon USB sniffing support" OFF)
523 option(DISABLE_BLUETOOTH "Disable Bluetooth sniffing support" OFF)
525 if(CMAKE_SYSTEM_NAME STREQUAL "Linux" OR
526 CMAKE_SYSTEM_NAME STREQUAL "FreeBSD" OR
528 option(DISABLE_NETMAP "Disable netmap support" OFF)
530 set(DISABLE_NETMAP ON)
534 # Require that DPDK support be explicitly enabled, as the code is
535 # immensely hard to keep compiling for every random API change
536 # the DPDK folks make.
541 # We don't support D-Bus sniffing on macOS; see
543 # https://bugs.freedesktop.org/show_bug.cgi?id=74029
546 option(DISABLE_DBUS "Disable D-Bus sniffing support" ON)
548 option(DISABLE_DBUS "Disable D-Bus sniffing support" OFF)
551 if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
552 option(DISABLE_RDMA "Disable RDMA sniffing support" OFF)
554 option(DISABLE_DAG "Disable Endace DAG card support" OFF)
555 option(ENABLE_DAG_TX "Enable Endace DAG transmit support" OFF)
557 option(DISABLE_SNF "Disable Myricom SNF support" OFF)
562 set(ENABLE_DAG_TX OFF)
570 option(BDEBUG "Build optimizer debugging code" OFF)
571 option(YYDEBUG "Build parser debugging code" OFF)
573 ###################################################################
575 # (and other variables to keep config.h consistent with Autoconf)
576 ###################################################################
578 # Get, parse, format and set pcap's version string from [pcap_root]/VERSION
581 # Get MAJOR, MINOR, PATCH & SUFFIX
582 file(STRINGS ${pcap_SOURCE_DIR}/VERSION
584 LIMIT_COUNT 1 # Read only the first line
588 string(REGEX MATCH "^([0-9]+)" PACKAGE_VERSION_MAJOR "${PACKAGE_VERSION}")
590 # Get MAJOR, MINOR & PATCH
591 string(REGEX MATCH "^([0-9]+.)?([0-9]+.)?([0-9]+)" PACKAGE_VERSION_NOSUFFIX "${PACKAGE_VERSION}")
594 # Convert PCAP_VERSION_NOSUFFIX to Windows preferred version format
595 string(REPLACE "." "," PACKAGE_VERSION_PREDLL ${PACKAGE_VERSION_NOSUFFIX})
597 # Append NANO (used for Windows internal versioning) to PCAP_VERSION_PREDLL
599 set(PACKAGE_VERSION_DLL ${PACKAGE_VERSION_PREDLL},0)
602 set(PACKAGE_BUGREPORT "https://github.com/the-tcpdump-group/libpcap/issues")
603 set(PACKAGE_NAME "${LIBRARY_NAME}")
604 set(PACKAGE_STRING "${LIBRARY_NAME} ${PACKAGE_VERSION}")
605 set(PACKAGE_TARNAME "lib${PACKAGE_NAME}")
606 set(PACKAGE_URL "https://www.tcpdump.org/")
608 ######################################
610 ######################################
613 ${CMAKE_CURRENT_BINARY_DIR}
617 include(CheckFunctionExists)
618 include(CMakePushCheckState)
619 include(CheckSymbolExists)
620 include(CheckIncludeFile)
624 if(IS_DIRECTORY ${CMAKE_HOME_DIRECTORY}/../../Common)
625 include_directories(${CMAKE_HOME_DIRECTORY}/../../Common)
626 endif(IS_DIRECTORY ${CMAKE_HOME_DIRECTORY}/../../Common)
630 set(HAVE_PACKET32 TRUE)
631 include_directories(${Packet_INCLUDE_DIRS})
633 # Check whether we have the NPcap PacketIsLoopbackAdapter()
636 cmake_push_check_state()
637 set(CMAKE_REQUIRED_LIBRARIES ${Packet_LIBRARIES})
638 set(CMAKE_REQUIRED_INCLUDES ${Packet_INCLUDE_DIRS})
639 check_function_exists(PacketIsLoopbackAdapter HAVE_PACKET_IS_LOOPBACK_ADAPTER)
640 check_function_exists(PacketGetTimestampModes HAVE_PACKET_GET_TIMESTAMP_MODES)
641 check_function_exists(PacketGetInfo HAVE_PACKET_GET_INFO)
642 check_include_file(npcap-bpf.h HAVE_NPCAP_BPF_H)
643 cmake_pop_check_state()
646 message(STATUS "checking for Npcap's version.h")
647 check_symbol_exists(WINPCAP_PRODUCT_NAME "${CMAKE_SOURCE_DIR}/../../version.h" HAVE_VERSION_H)
649 message(STATUS "HAVE version.h")
651 message(STATUS "MISSING version.h")
652 endif(HAVE_VERSION_H)
657 add_definitions(-D__STDC__)
658 add_definitions(-D_CRT_SECURE_NO_WARNINGS)
662 message(STATUS "Use STATIC runtime")
665 CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
666 CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO)
667 string(REGEX REPLACE "/MD" "/MT" ${RT_FLAG} "${${RT_FLAG}}")
670 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -static-libgcc")
673 message(STATUS "Use DYNAMIC runtime")
677 # CMake's definition of "cross-compiling" appears to be "compiling
678 # for an *operating system* other than the one on which the build
681 # This is an inadequate definition, as people build for the same
682 # operating system but a different instruction set, e.g. building
683 # on an IA-32 or x86-64 Linux box for an Arm embedded Linux box,
684 # or building Arm code on an IA-32 or x86-64 Windows box.
686 # So just test whether check_c_source_runs() on a trivial program
687 # works; if not, it's probably because the generated code won't
688 # run on the platform on which we're running.
690 include(CheckCSourceRuns)
691 if (NOT CMAKE_CROSSCOMPILING)
692 check_c_source_runs("
698 CHECK_C_SOURCE_RUNS_WORKS
700 if (NOT CHECK_C_SOURCE_RUNS_WORKS)
701 set(CMAKE_CROSSCOMPILING TRUE)
705 ###################################################################
706 # Detect available platform features
707 ###################################################################
709 include(CheckIncludeFiles)
710 include(CheckStructHasMember)
711 include(CheckTypeSize)
714 # Tests are a bit expensive with Visual Studio on Windows, so, on
715 # Windows, we skip tests for UN*X-only headers and functions.
721 check_include_file(unistd.h HAVE_UNISTD_H)
722 if(NOT HAVE_UNISTD_H)
723 add_definitions(-DYY_NO_UNISTD_H)
724 endif(NOT HAVE_UNISTD_H)
726 check_include_file(netinet/if_ether.h HAVE_NETINET_IF_ETHER_H)
732 # First, check for the __atomic_load_n() and __atomic_store_n()
735 # We can't use check_function_exists(), as it tries to declare
736 # the function, and attempting to declare a compiler builtin
737 # can produce an error.
739 # We don't use check_symbol_exists(), as it expects a header
740 # file to be specified to declare the function, but there isn't
741 # such a header file.
743 # So we use check_c_source_compiles().
745 check_c_source_compiles(
750 return __atomic_load_n(&i, __ATOMIC_RELAXED);
753 HAVE___ATOMIC_LOAD_N)
754 check_c_source_compiles(
759 __atomic_store_n(&i, 17, __ATOMIC_RELAXED);
763 HAVE___ATOMIC_STORE_N)
766 # Now check for various system functions.
768 check_function_exists(strerror_r HAVE_STRERROR_R)
771 # We have strerror_r; if we define _GNU_SOURCE, is it a
772 # POSIX-compliant strerror_r() or a GNU strerror_r()?
774 check_c_source_compiles(
778 /* Define it GNU-style; that will cause an error if it's not GNU-style */
779 extern char *strerror_r(int, char *, size_t);
788 if(NOT HAVE_GNU_STRERROR_R)
789 set(HAVE_POSIX_STRERROR_R YES)
790 endif(NOT HAVE_GNU_STRERROR_R)
791 else(HAVE_STRERROR_R)
793 # We don't have strerror_r; do we have _wcserror_s?
795 check_function_exists(_wcserror_s HAVE__WCSERROR_S)
796 endif(HAVE_STRERROR_R)
798 if (NOT CMAKE_CROSSCOMPILING)
800 # Require a proof of suitable snprintf(3), same as in Autoconf.
802 check_c_source_runs("
805 #include <inttypes.h>
806 #include <sys/types.h>
808 #if defined(_WIN32) && !defined(_SSIZE_T_DEFINED)
810 * On UN*Xes, this is a signed integer type of the same size as size_t.
812 * It's not defined by Visual Studio; we assume that ptrdiff_t will
813 * be a type that is a signed integer type of the same size as size_t.
815 typedef ptrdiff_t ssize_t;
819 * Avoid trying to cast negative values to unsigned types, or doing
820 * shifts of signed types, in order not to have the test program fail
821 * if we're building with undefined-behavior sanitizers enabled.
826 unsigned int ui = sizeof(buf);
828 int64_t i64 = INT64_C(0x100000000);
829 uint64_t ui64 = UINT64_C(0x100000000);
831 snprintf(buf, sizeof(buf), \"%zu\", (size_t)ui);
832 if (strncmp(buf, \"100\", sizeof(buf)))
835 snprintf(buf, sizeof(buf), \"%zd\", (ssize_t)(-i));
836 if (strncmp(buf, \"-100\", sizeof(buf)))
839 snprintf(buf, sizeof(buf), \"%\" PRId64, -i64);
840 if (strncmp(buf, \"-4294967296\", sizeof(buf)))
843 snprintf(buf, sizeof(buf), \"0o%\" PRIo64, ui64);
844 if (strncmp(buf, \"0o40000000000\", sizeof(buf)))
847 snprintf(buf, sizeof(buf), \"0x%\" PRIx64, ui64);
848 if (strncmp(buf, \"0x100000000\", sizeof(buf)))
851 snprintf(buf, sizeof(buf), \"%\" PRIu64, ui64);
852 if (strncmp(buf, \"4294967296\", sizeof(buf)))
861 if(NOT SUITABLE_SNPRINTF)
863 "The snprintf(3) implementation in this libc is not suitable,
864 libpcap would not work correctly even if it managed to compile."
868 message(STATUS "Skipped SUITABLE_SNPRINTF because cross-compiling.")
871 check_function_exists(strlcpy HAVE_STRLCPY)
872 check_function_exists(strlcat HAVE_STRLCAT)
873 check_function_exists(asprintf HAVE_ASPRINTF)
874 check_function_exists(vasprintf HAVE_VASPRINTF)
875 check_function_exists(strtok_r HAVE_STRTOK_R)
878 # Look for various networking-related libraries that we may need.
880 # We need getaddrinfo() to translate host names in filters to IP
881 # addresses. We use getaddrinfo() because we want a portable
882 # thread-safe way of getting information for a host name or port;
883 # there exist _r versions of gethostbyname() and getservbyname() on
884 # some platforms, but not on all platforms.
886 # We may also need socket() and other socket functions to support:
888 # Local packet capture with capture mechanisms that use sockets.
890 # Local capture device enumeration if a socket call is needed to
891 # enumerate devices or get device attributes.
893 # Packet capture from services that put captured packets on the
894 # network, such as rpcap servers.
896 # We may also need getnameinfo() for packet capture from services
897 # that put packets on the network.
899 set(PCAP_LINK_LIBRARIES "")
902 set(REQUIRES_PRIVATE "")
904 include(CheckLibraryExists)
909 # We need winsock2.h and ws2tcpip.h.
911 # On Windows, getaddrinfo() is in the ws2_32 library.
913 cmake_push_check_state()
914 set(CMAKE_REQUIRED_LIBRARIES ws2_32)
915 check_symbol_exists(getaddrinfo "winsock2.h;ws2tcpip.h" LIBWS2_32_HAS_GETADDRINFO)
916 cmake_pop_check_state()
917 if(LIBWS2_32_HAS_GETADDRINFO)
918 set(PCAP_LINK_LIBRARIES ws2_32 ${PCAP_LINK_LIBRARIES})
919 else(LIBWS2_32_HAS_GETADDRINFO)
920 message(FATAL_ERROR "getaddrinfo is required, but wasn't found")
921 endif(LIBWS2_32_HAS_GETADDRINFO)
926 # Most UN*Xes have getaddrinfo(), and the other routines we may
927 # need, in the default searched libraries (e.g., libc).
930 # NOTE: if you hand check_library_exists as its last argument a
931 # variable that's been set, it skips the test, so we need different
932 # variables for different libraries.
934 check_function_exists(getaddrinfo STDLIBS_HAVE_GETADDRINFO)
935 if(NOT STDLIBS_HAVE_GETADDRINFO)
937 # Not found in the standard system libraries.
939 # In some versions of Solaris, we need to link with libsocket
940 # and libnsl, so check in libsocket and also link with liblnsl
941 # when doing this test.
943 # Linking with libsocket and libnsl will find all the routines
946 cmake_push_check_state()
947 set(CMAKE_REQUIRED_LIBRARIES nsl)
948 check_library_exists(socket getaddrinfo "" LIBSOCKET_HAS_GETADDRINFO)
949 cmake_pop_check_state()
950 if(LIBSOCKET_HAS_GETADDRINFO)
952 # OK, we found it in libsocket.
954 set(PCAP_LINK_LIBRARIES socket nsl ${PCAP_LINK_LIBRARIES})
955 set(LIBS "-lsocket -lnsl ${LIBS}")
956 set(LIBS_STATIC "-lsocket -lnsl ${LIBS_STATIC}")
957 set(LIBS_PRIVATE "-lsocket -lnsl ${LIBS_PRIVATE}")
958 else(LIBSOCKET_HAS_GETADDRINFO)
960 # Not found in libsocket; test for it in libnetwork, which
961 # is where it is in Haiku.
963 # Linking with libnetwork will find all the routines we
966 check_library_exists(network getaddrinfo "" LIBNETWORK_HAS_GETADDRINFO)
967 if(LIBNETWORK_HAS_GETADDRINFO)
969 # OK, we found it in libnetwork.
971 set(PCAP_LINK_LIBRARIES network ${PCAP_LINK_LIBRARIES})
972 set(LIBS "-lnetwork ${LIBS}")
973 set(LIBS_STATIC "-lnetwork ${LIBS_STATIC}")
974 set(LIBS_PRIVATE "-lnetwork ${LIBS_PRIVATE}")
975 else(LIBNETWORK_HAS_GETADDRINFO)
979 message(FATAL_ERROR "getaddrinfo is required, but wasn't found")
980 endif(LIBNETWORK_HAS_GETADDRINFO)
981 endif(LIBSOCKET_HAS_GETADDRINFO)
984 # We require a version of recvmsg() that conforms to the Single
985 # UNIX Specification, so that we can check whether a datagram
986 # received with recvmsg() was truncated when received due to the
987 # buffer being too small.
989 # On most systems, the version of recvmsg() in the libraries
990 # found above conforms to the SUS.
992 # On at least some versions of Solaris, it does not conform to
993 # the SUS, and we need the version in libxnet, which does
996 # Check whether libxnet exists and has a version of recvmsg();
997 # if it does, link with libxnet before we link with libsocket,
998 # to get that version.
1000 # This test also links with libsocket and libnsl.
1002 cmake_push_check_state()
1003 set(CMAKE_REQUIRED_LIBRARIES socket nsl)
1004 check_library_exists(xnet recvmsg "" LIBXNET_HAS_RECVMSG)
1005 cmake_pop_check_state()
1006 if(LIBXNET_HAS_RECVMSG)
1008 # libxnet has recvmsg(); link with it as well.
1010 set(PCAP_LINK_LIBRARIES xnet ${PCAP_LINK_LIBRARIES})
1011 set(LIBSC "-lxnet ${LIBS_LIBS}")
1012 set(LIBS_STATIC "-lxnet ${LIBS_STATIC}")
1013 set(LIBS_PRIVATE "-lxnet ${LIBS_PRIVATE}")
1014 endif(LIBXNET_HAS_RECVMSG)
1015 endif(NOT STDLIBS_HAVE_GETADDRINFO)
1017 if(CMAKE_SYSTEM_NAME STREQUAL "HP-UX")
1019 # Same as the respective AC_CHECK_MEMBERS() in Autoconf.
1021 check_struct_has_member("struct dl_hp_ppa_info_t" dl_module_id_1 "sys/types.h;sys/dlpi.h;sys/dlpi_ext.h" HAVE_DL_HP_PPA_INFO_T_DL_MODULE_ID_1)
1024 # Same as the respective AC_SEARCH_LIBS() in Autoconf.
1026 check_function_exists(putmsg STDLIBS_HAVE_PUTMSG)
1027 if(NOT STDLIBS_HAVE_PUTMSG)
1028 check_library_exists(str putmsg "" LIBSTR_HAS_PUTMSG)
1029 if(LIBSTR_HAS_PUTMSG)
1030 set(PCAP_LINK_LIBRARIES str ${PCAP_LINK_LIBRARIES})
1031 set(LIBS "-lstr ${LIBS}")
1032 set(LIBS_STATIC "-lstr ${LIBS_STATIC}")
1033 set(LIBS_PRIVATE "-lstr ${LIBS_PRIVATE}")
1034 endif(LIBSTR_HAS_PUTMSG)
1035 endif(NOT STDLIBS_HAVE_PUTMSG)
1039 # Haiku has getpass() in libbsd.
1041 if(CMAKE_SYSTEM_NAME STREQUAL "Haiku")
1042 set(PCAP_LINK_LIBRARIES bsd ${PCAP_LINK_LIBRARIES})
1047 # Check for reentrant versions of getnetbyname_r(), as provided by
1048 # Linux (glibc), Solaris, and AIX (with three different APIs!).
1049 # If we don't find one, we just use getnetbyname(), which uses
1050 # thread-specific data on many platforms, but doesn't use it on
1051 # NetBSD or OpenBSD, and may not use it on older versions of other
1054 # Only do the check if we have a declaration of getnetbyname_r();
1055 # without it, we can't check which API it has. (We assume that
1056 # if there's a declaration, it has a prototype, so that the API
1059 cmake_push_check_state()
1060 set(CMAKE_REQUIRED_LIBRARIES ${PCAP_LINK_LIBRARIES})
1061 check_symbol_exists(getnetbyname_r netdb.h NETDB_H_DECLARES_GETNETBYNAME_R)
1062 if(NETDB_H_DECLARES_GETNETBYNAME_R)
1063 check_c_source_compiles(
1069 struct netent netent_buf;
1071 struct netent *resultp;
1074 return getnetbyname_r((const char *)0, &netent_buf, buf, sizeof buf, &resultp, &h_errnoval);
1077 HAVE_LINUX_GETNETBYNAME_R)
1078 if(NOT HAVE_LINUX_GETNETBYNAME_R)
1079 check_c_source_compiles(
1085 struct netent netent_buf;
1088 return getnetbyname_r((const char *)0, &netent_buf, buf, (int)sizeof buf) != NULL;
1091 HAVE_SOLARIS_GETNETBYNAME_R)
1092 if(NOT HAVE_SOLARIS_GETNETBYNAME_R)
1093 check_c_source_compiles(
1099 struct netent netent_buf;
1100 struct netent_data net_data;
1102 return getnetbyname_r((const char *)0, &netent_buf, &net_data);
1105 HAVE_AIX_GETNETBYNAME_R)
1106 endif(NOT HAVE_SOLARIS_GETNETBYNAME_R)
1107 endif(NOT HAVE_LINUX_GETNETBYNAME_R)
1108 endif(NETDB_H_DECLARES_GETNETBYNAME_R)
1109 cmake_pop_check_state()
1112 # Check for reentrant versions of getprotobyname_r(), as provided by
1113 # Linux (glibc), Solaris, and AIX (with three different APIs!).
1114 # If we don't find one, we just use getprotobyname(), which uses
1115 # thread-specific data on many platforms, but doesn't use it on
1116 # NetBSD or OpenBSD, and may not use it on older versions of other
1119 # Only do the check if we have a declaration of getprotobyname_r();
1120 # without it, we can't check which API it has. (We assume that
1121 # if there's a declaration, it has a prototype, so that the API
1124 cmake_push_check_state()
1125 set(CMAKE_REQUIRED_LIBRARIES ${PCAP_LINK_LIBRARIES})
1126 check_symbol_exists(getprotobyname_r netdb.h NETDB_H_DECLARES_GETPROTOBYNAME_R)
1127 if(NETDB_H_DECLARES_GETPROTOBYNAME_R)
1128 check_c_source_compiles(
1134 struct protoent protoent_buf;
1136 struct protoent *resultp;
1138 return getprotobyname_r((const char *)0, &protoent_buf, buf, sizeof buf, &resultp);
1141 HAVE_LINUX_GETPROTOBYNAME_R)
1142 if(NOT HAVE_LINUX_GETPROTOBYNAME_R)
1143 check_c_source_compiles(
1149 struct protoent protoent_buf;
1152 return getprotobyname_r((const char *)0, &protoent_buf, buf, (int)sizeof buf) != NULL;
1155 HAVE_SOLARIS_GETPROTOBYNAME_R)
1156 if(NOT HAVE_SOLARIS_GETPROTOBYNAME_R)
1157 check_c_source_compiles(
1163 struct protoent protoent_buf;
1164 struct protoent_data proto_data;
1166 return getprotobyname_r((const char *)0, &protoent_buf, &proto_data);
1169 HAVE_AIX_GETPROTOBYNAME_R)
1170 endif(NOT HAVE_SOLARIS_GETPROTOBYNAME_R)
1171 endif(NOT HAVE_LINUX_GETPROTOBYNAME_R)
1172 endif(NETDB_H_DECLARES_GETPROTOBYNAME_R)
1173 cmake_pop_check_state()
1178 # XXX - there's no check_type() macro that's like check_type_size()
1179 # except that it only checks for the existence of the structure type,
1180 # so we use check_type_size() and ignore the size.
1182 cmake_push_check_state()
1184 set(CMAKE_EXTRA_INCLUDE_FILES winsock2.h)
1186 set(CMAKE_EXTRA_INCLUDE_FILES unistd.h sys/socket.h)
1188 check_type_size("socklen_t" SOCKLEN_T)
1189 cmake_pop_check_state()
1195 check_struct_has_member("struct sockaddr" sa_len winsock2.h HAVE_STRUCT_SOCKADDR_SA_LEN)
1197 check_struct_has_member("struct sockaddr" sa_len sys/socket.h HAVE_STRUCT_SOCKADDR_SA_LEN)
1201 # This requires the libraries that we require, as ether_hostton might be
1202 # in one of those libraries. That means we have to do this after
1203 # we check for those libraries.
1205 # You are in a twisty little maze of UN*Xes, all different.
1206 # Some might not have ether_hostton().
1207 # Some might have it and declare it in <net/ethernet.h>.
1208 # Some might have it and declare it in <netinet/ether.h>
1209 # Some might have it and declare it in <sys/ethernet.h>.
1210 # Some might have it and declare it in <arpa/inet.h>.
1211 # Some might have it and declare it in <netinet/if_ether.h>.
1212 # Some might have it and not declare it in any header file.
1214 # Before you is a C compiler.
1216 cmake_push_check_state()
1217 set(CMAKE_REQUIRED_LIBRARIES ${PCAP_LINK_LIBRARIES})
1218 check_function_exists(ether_hostton HAVE_ETHER_HOSTTON)
1219 if(HAVE_ETHER_HOSTTON)
1221 # OK, we have ether_hostton(). Is it declared in <net/ethernet.h>?
1223 # This test fails if we don't have <net/ethernet.h> or if we do
1224 # but it doesn't declare ether_hostton().
1226 check_symbol_exists(ether_hostton net/ethernet.h NET_ETHERNET_H_DECLARES_ETHER_HOSTTON)
1227 if(NET_ETHERNET_H_DECLARES_ETHER_HOSTTON)
1229 # Yes - we have it declared.
1231 set(HAVE_DECL_ETHER_HOSTTON TRUE)
1236 if(NOT HAVE_DECL_ETHER_HOSTTON)
1238 # No - how about <netinet/ether.h>, as on Linux?
1240 # This test fails if we don't have <netinet/ether.h>
1241 # or if we do but it doesn't declare ether_hostton().
1243 check_symbol_exists(ether_hostton netinet/ether.h NETINET_ETHER_H_DECLARES_ETHER_HOSTTON)
1244 if(NETINET_ETHER_H_DECLARES_ETHER_HOSTTON)
1246 # Yes - we have it declared.
1248 set(HAVE_DECL_ETHER_HOSTTON TRUE)
1254 if(NOT HAVE_DECL_ETHER_HOSTTON)
1256 # No - how about <sys/ethernet.h>, as on Solaris 10 and later?
1258 # This test fails if we don't have <sys/ethernet.h>
1259 # or if we do but it doesn't declare ether_hostton().
1261 check_symbol_exists(ether_hostton sys/ethernet.h SYS_ETHERNET_H_DECLARES_ETHER_HOSTTON)
1262 if(SYS_ETHERNET_H_DECLARES_ETHER_HOSTTON)
1264 # Yes - we have it declared.
1266 set(HAVE_DECL_ETHER_HOSTTON TRUE)
1272 if(NOT HAVE_DECL_ETHER_HOSTTON)
1274 # No, how about <arpa/inet.h>, as on AIX?
1276 # This test fails if we don't have <arpa/inet.h>
1277 # or if we do but it doesn't declare ether_hostton().
1279 check_symbol_exists(ether_hostton arpa/inet.h ARPA_INET_H_DECLARES_ETHER_HOSTTON)
1280 if(ARPA_INET_H_DECLARES_ETHER_HOSTTON)
1282 # Yes - we have it declared.
1284 set(HAVE_DECL_ETHER_HOSTTON TRUE)
1290 if(NOT HAVE_DECL_ETHER_HOSTTON)
1292 # No, how about <netinet/if_ether.h>?
1293 # On some platforms, it requires <net/if.h> and
1294 # <netinet/in.h>, and we always include it with
1295 # both of them, so test it with both of them.
1297 # This test fails if we don't have <netinet/if_ether.h>
1298 # and the headers we include before it, or if we do but
1299 # <netinet/if_ether.h> doesn't declare ether_hostton().
1301 check_symbol_exists(ether_hostton "sys/types.h;sys/socket.h;net/if.h;netinet/in.h;netinet/if_ether.h" NETINET_IF_ETHER_H_DECLARES_ETHER_HOSTTON)
1302 if(NETINET_IF_ETHER_H_DECLARES_ETHER_HOSTTON)
1304 # Yes - we have it declared.
1306 set(HAVE_DECL_ETHER_HOSTTON TRUE)
1310 # After all that, is ether_hostton() declared?
1312 if(NOT HAVE_DECL_ETHER_HOSTTON)
1314 # No, we'll have to declare it ourselves.
1315 # Do we have "struct ether_addr" if we include <netinet/if_ether.h>?
1317 # XXX - there's no check_type() macro that's like check_type_size()
1318 # except that it only checks for the existence of the structure type,
1319 # so we use check_type_size() and ignore the size.
1321 cmake_push_check_state()
1322 set(CMAKE_EXTRA_INCLUDE_FILES sys/types.h sys/socket.h net/if.h netinet/in.h netinet/if_ether.h)
1323 check_type_size("struct ether_addr" STRUCT_ETHER_ADDR)
1324 cmake_pop_check_state()
1327 cmake_pop_check_state()
1330 # Same as in Autoconf.
1332 if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
1333 check_symbol_exists(__GLIBC__ features.h HAVE_GLIBC)
1335 check_symbol_exists(__UCLIBC__ features.h HAVE_UCLIBC)
1340 # Large file support on UN*X, a/k/a LFS.
1346 # Add the required #defines.
1348 add_definitions(${LFS_DEFINITIONS})
1352 # Check for fseeko as well.
1359 # Add the required #defines.
1361 add_definitions(${FSEEKO_DEFINITIONS})
1366 # Find and print the size of time_t.
1368 cmake_push_check_state()
1369 set(CMAKE_EXTRA_INCLUDE_FILES time.h)
1370 check_type_size("time_t" SIZEOF_TIME_T)
1371 if(SIZEOF_TIME_T EQUAL 4)
1372 message(STATUS "32-bit time_t")
1373 elseif(SIZEOF_TIME_T EQUAL 8)
1374 message(STATUS "64-bit time_t")
1376 cmake_pop_check_state()
1380 # We might need them, because some libraries we use might use them,
1381 # but we don't necessarily need them.
1382 # That's only on UN*X; on Windows, if they use threads, we assume
1383 # they're native Windows threads.
1386 set(CMAKE_THREAD_PREFER_PTHREAD ON)
1387 find_package(Threads)
1388 if(NOT CMAKE_USE_PTHREADS_INIT)
1390 # If it's not pthreads, we won't use it; we use it for libraries
1393 set(CMAKE_THREAD_LIBS_INIT "")
1394 endif(NOT CMAKE_USE_PTHREADS_INIT)
1397 if(ENABLE_PROFILING)
1399 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pg")
1406 # https://github.com/commonmark/cmark/blob/master/FindAsan.cmake
1408 # The MIT License (MIT)
1410 # Copyright (c) 2013 Matthew Arsenault
1412 # Permission is hereby granted, free of charge, to any person obtaining a copy
1413 # of this software and associated documentation files (the "Software"), to deal
1414 # in the Software without restriction, including without limitation the rights
1415 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1416 # copies of the Software, and to permit persons to whom the Software is
1417 # furnished to do so, subject to the following conditions:
1419 # The above copyright notice and this permission notice shall be included in
1420 # all copies or substantial portions of the Software.
1422 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1423 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1424 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1425 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1426 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1427 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
1430 # Test if the each of the sanitizers in the ENABLE_SANITIZERS list are
1431 # supported by the compiler, and, if so, adds the appropriate flags to
1432 # CMAKE_C_FLAGS, and SANITIZER_FLAGS. If not, it fails.
1434 # Do this last, in the hope that it will prevent configuration on Linux
1435 # from somehow deciding it doesn't need -lpthread when building rpcapd
1436 # (it does require it, but somehow, in some mysterious fashion that no
1437 # obvious CMake debugging flag reveals, it doesn't realize that if we
1438 # turn sanitizer stuff on).
1440 # Note: apparently, some projects have decided that ENABLE_SANITIZERS
1441 # is a Boolean, with OFF meaning "no sanitizers" and ON meaning "all
1442 # sanitizers". Whoever decided that didn't put it up as a common
1443 # CMake idiom, as far as I can tell; we only discovered this because
1444 # JetBrains' CLion "helpfully" appears to pass -DENABLE_SANITIZERS=OFF
1445 # to CMake by default, which causes CMake to fail on libpcap. Thanks!
1447 # We thus also allow a setting of OFF to mean "no sanitizers" and ON to
1448 # mean "all supported sanitizers that we know about and that can all
1449 # be used together".
1451 macro(test_sanitizer _sanitizer _sanitizer_flag)
1452 message(STATUS "Checking sanitizer ${_sanitizer}")
1453 set(sanitizer_variable "sanitize_${_sanitizer}")
1454 # Set -Werror to catch "argument unused during compilation" warnings
1455 set(CMAKE_REQUIRED_FLAGS "-Werror -fsanitize=${_sanitizer}")
1456 check_c_compiler_flag("-fsanitize=${_sanitizer}" ${sanitizer_variable})
1457 if(${${sanitizer_variable}})
1458 set(${_sanitizer_flag} "-fsanitize=${_sanitizer}")
1461 # Try the versions supported prior to Clang 3.2.
1462 # If the sanitizer is "address", try -fsanitize-address.
1463 # If it's "undefined", try -fcatch-undefined-behavior.
1464 # Otherwise, give up.
1466 set(sanitizer_variable "OLD_${sanitizer_variable}")
1467 if ("${_sanitizer}" STREQUAL "address")
1468 set(CMAKE_REQUIRED_FLAGS "-Werror -fsanitize-address")
1469 check_c_compiler_flag("-fsanitize-address" ${sanitizer_variable})
1470 if(${${sanitizer_variable}})
1471 set(${_sanitizer_flag} "-fsanitize-address")
1473 elseif("${_sanitizer}" STREQUAL "undefined")
1474 set(CMAKE_REQUIRED_FLAGS "-Werror -fcatch-undefined-behavior")
1475 check_c_compiler_flag("-fcatch-undefined-behavior" ${sanitizer_variable})
1476 if(${${sanitizer_variable}})
1477 set(${_sanitizer_flag} "-fcatch-undefined-behavior")
1481 unset(CMAKE_REQUIRED_FLAGS)
1482 endmacro(test_sanitizer)
1484 set(SANITIZER_FLAGS "")
1485 if("${ENABLE_SANITIZERS}")
1487 # This appears to indicate that ENABLE_SANITIZERS was set to a
1488 # string value that is "one of the true constants", meaning
1489 # "1, ON, YES, TRUE, Y, or a non-zero number".
1491 # It does not appear to happen for other settings, including
1492 # setting it to a list of one or more sanitizers.
1494 # This setting means "enable all sanitizers that the compiler
1497 foreach(sanitizer "address" "undefined")
1498 unset(SANITIZER_FLAG)
1499 test_sanitizer(${sanitizer} SANITIZER_FLAG)
1501 message(STATUS "${sanitizer} sanitizer supported using ${SANITIZER_FLAG}")
1502 set(SANITIZER_FLAGS "${SANITIZER_FLAGS} ${SANITIZER_FLAG}")
1504 message(STATUS "${sanitizer} isn't a supported sanitizer")
1507 if("${SANITIZER_FLAGS}" STREQUAL "")
1508 message(FATAL_ERROR "No supported sanitizers found")
1512 # This appears to indicate that ENABLE_SANITIZERS was either:
1515 # set to a set to a string value that is not "one of the true
1516 # constants", meaning "1, ON, YES, TRUE, Y, or a non-zero number".
1518 # The latter includes setting it to "one of the false constants",
1519 # meaning the string "is 0, OFF, NO, FALSE, N, IGNORE, NOTFOUND,
1520 # the empty string, or ends in the suffix -NOTFOUND."
1522 # It also includes setting it to a list of one or more sanitizers.
1524 # We want to treat "not set" and "set to one of the false constants"
1525 # as meaning "do not enable any sanitizers".
1527 # We want to treat "set to a list of one or more sanitizers" as
1528 # meaning "enable all the sanitizers in the list".
1530 # This requires that we distinguish between those two cases.
1532 if(ENABLE_SANITIZERS)
1534 # This appears to indicate that ENABLE_SANITIZERS was set to
1535 # a string value that is "not one of the false constants".
1537 # We already know it's "not one of the true constants", so
1538 # we treat it as a list of sanitizers.
1540 foreach(sanitizer IN LISTS ENABLE_SANITIZERS)
1541 unset(SANITIZER_FLAG)
1542 test_sanitizer(${sanitizer} SANITIZER_FLAG)
1544 message(STATUS "${sanitizer} sanitizer supported using ${SANITIZER_FLAG}")
1545 set(SANITIZER_FLAGS "${SANITIZER_FLAGS} ${SANITIZER_FLAG}")
1547 message(FATAL_ERROR "${sanitizer} isn't a supported sanitizer")
1552 # This appears to indicate that ENABLE_SANITIZERS was either:
1555 # set to a value that's "one of the false constants";
1557 # so we don't enable any sanitizers.
1559 message(STATUS "Not enabling sanitizers")
1563 if(NOT "${SANITIZER_FLAGS}" STREQUAL "")
1564 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O1 -g ${SANITIZER_FLAGS} -fno-omit-frame-pointer -fno-optimize-sibling-calls")
1571 find_package(OpenSSL)
1576 include_directories(SYSTEM ${OPENSSL_INCLUDE_DIR})
1577 set(PCAP_LINK_LIBRARIES ${PCAP_LINK_LIBRARIES} ${OPENSSL_LIBRARIES})
1580 # The find_package() module CMake provides for OpenSSL uses does not
1581 # give us a defined indication of whether it found OpenSSL with
1582 # pkg-config or not. We need to know that as, if it was found with
1583 # pkg-config, we should set the Requires.private value in libpcap.pc
1584 # to include its package name, openssl, otherwise we should add the
1585 # names for the static libraries to Libs.private.
1587 # On UN*X, FindOpenSSL happens to use pkg-config to find OpenSSL, but
1588 # it doesn't appear to be documented as doing so; therefore, we don't
1589 # assume that, if we got here, we have pkg-config.
1591 # So we use pkg_get_link_info() to run pkg-config ourselves, both
1592 # because FindOpenSSL doesn't set the OPENSSL_LDFLAGS or
1593 # OPENSSL_STATIC_LDFLAGS variables and because, for reasons explained
1594 # in the comment before the pkg_get_link_info() macro, even if it did,
1595 # it wouldn't be what we want anyway.
1597 if (PKG_CONFIG_EXECUTABLE)
1598 pkg_get_link_info(OPENSSL openssl)
1599 if (OPENSSL_FOUND_WITH_PKG_CONFIG)
1601 # pkg-config failed; assume that means that there is no openssl
1602 # package for it to find. Just add OPENSSL_LIBRARIES to
1603 # LIBS_PRIVATE AND LIBS_STATIC, as that's the
1604 # best we can do. XXX - need list of -l and -L flags to add....
1606 set(LIBS "${LIBS} ${OPENSSL_LIBS}")
1607 set(LIBS_STATIC "${LIBS_STATIC} ${OPENSSL_LIBS_STATIC}")
1608 set(REQUIRES_PRIVATE "${REQUIRES_PRIVATE} ${OPENSSL_PACKAGE_NAME}")
1611 # Get it from OPENSSL_LIBRARIES
1612 foreach(_lib IN LISTS OPENSSL_LIBRARIES)
1614 # Get the directory in which the library resides.
1616 get_filename_component(_lib_directory "${_lib}" DIRECTORY)
1619 # Is the library directory in CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES?
1620 # (See comment above on why we use that.)
1622 list(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${_lib_directory}" _lib_index)
1623 if(_lib_index EQUAL -1)
1625 # No, so add a -L flag to get the linker to search in that
1628 set(LIBS "${LIBS} -L${_lib_directory}")
1629 set(LIBS_STATIC "${LIBS_STATIC} -L${_lib_directory}")
1630 set(LIBS_PRIVATE "${LIBS_PRIVATE} -L${_lib_directory}")
1634 # Get the file name of the library, without the extension.
1636 get_filename_component(_lib_filename "${_lib}" NAME_WE)
1639 # Strip off the "lib" prefix to get the library name, and
1640 # add a -l flag based on that.
1642 string(REGEX REPLACE "^lib" "" _library_name "${_lib_filename}")
1643 set(LIBS "${LIBS} -l${_library_name}")
1644 set(LIBS_STATIC "${LIBS_STATIC} -l${_library_name}")
1645 set(LIBS_PRIVATE "${LIBS_PRIVATE} -l${_library_name}")
1648 set(HAVE_OPENSSL YES)
1649 endif(OPENSSL_FOUND)
1650 endif(ENABLE_REMOTE)
1653 # On macOS, build libpcap for the appropriate architectures, if
1654 # CMAKE_OSX_ARCHITECTURES isn't set (if it is, let that control
1655 # the architectures for which to build it).
1657 if(APPLE AND "${CMAKE_OSX_ARCHITECTURES}" STREQUAL "")
1659 # Get the major version of Darwin.
1661 string(REGEX MATCH "^([0-9]+)" SYSTEM_VERSION_MAJOR "${CMAKE_SYSTEM_VERSION}")
1663 if(SYSTEM_VERSION_MAJOR LESS 8)
1667 # Build libraries and executables only for 32-bit PowerPC, as
1668 # that's all that is supported.
1670 set(OSX_LIBRARY_ARCHITECTURES "ppc")
1671 set(OSX_EXECUTABLE_ARCHITECTURES "ppc")
1672 elseif(SYSTEM_VERSION_MAJOR EQUAL 8)
1674 # Tiger. Is this prior to, or with, Intel support?
1676 # Get the minor version of Darwin.
1678 string(REPLACE "${SYSTEM_VERSION_MAJOR}." "" SYSTEM_MINOR_AND_PATCH_VERSION ${CMAKE_SYSTEM_VERSION})
1679 string(REGEX MATCH "^([0-9]+)" SYSTEM_VERSION_MINOR "${SYSTEM_MINOR_AND_PATCH_VERSION}")
1680 if(SYSTEM_VERSION_MINOR LESS 4)
1682 # Prior to Intel support.
1684 # Build libraries and executables for 32-bit PowerPC and
1685 # 64-bit PowerPC, with 32-bit PowerPC first, as those
1686 # are both supported. (I'm guessing that's what Apple
1689 set(OSX_LIBRARY_ARCHITECTURES "ppc;ppc64")
1690 set(OSX_EXECUTABLE_ARCHITECTURES "ppc;ppc64")
1691 elseif(SYSTEM_VERSION_MINOR LESS 7)
1693 # With Intel support but prior to x86-64 support.
1695 # Build for 32-bit PowerPC, 64-bit PowerPC, and 32-bit x86,
1696 # with 32-bit PowerPC first, as those are all supported.
1697 # (I'm guessing that's what Apple does.)
1699 set(OSX_LIBRARY_ARCHITECTURES "ppc;ppc64;i386")
1700 set(OSX_EXECUTABLE_ARCHITECTURES "ppc;ppc64;i386")
1703 # With Intel support including x86-64 support.
1705 # Build for 32-bit PowerPC, 64-bit PowerPC, 32-bit x86,
1706 # and x86-64, with 32-bit PowerPC first, as those are
1707 # all supported. (I'm guessing that's what Apple does.)
1709 set(OSX_LIBRARY_ARCHITECTURES "ppc;ppc64;i386;x86_64")
1710 set(OSX_EXECUTABLE_ARCHITECTURES "ppc;ppc64;i386;x86_64")
1712 elseif(SYSTEM_VERSION_MAJOR EQUAL 9)
1716 # Build libraries and executables for 32-bit PowerPC, 64-bit
1717 # PowerPC, 32-bit x86, and x86-64, with 32-bit PowerPC
1718 # first, as those are all supported. (That's what Apple
1721 set(OSX_LIBRARY_ARCHITECTURES "ppc;ppc64;i386;x86_64")
1722 set(OSX_EXECUTABLE_ARCHITECTURES "ppc;ppc64;i386;x86_64")
1723 elseif(SYSTEM_VERSION_MAJOR EQUAL 10)
1727 # Build libraries for x86-64, 32-bit x86, and 32-bit PowerPC,
1728 # with x86-64 first, because 32-bit PowerPC executables are
1729 # supported with Rosetta. (That's what Apple does, even though
1730 # Snow Leopard doesn't run on PPC, so PPC libpcap runs under
1731 # Rosetta, and Rosetta doesn't support BPF ioctls, so PPC
1732 # executables can't do live captures.)
1734 set(OSX_LIBRARY_ARCHITECTURES "x86_64;i386;ppc")
1737 # Build executables only for 32-bit x86 and 64-bit x86, as PPC
1738 # machines are no longer supported.
1740 set(OSX_EXECUTABLE_ARCHITECTURES "x86_64;i386")
1741 elseif(SYSTEM_VERSION_MAJOR GREATER 10 AND SYSTEM_VERSION_MAJOR LESS 19)
1743 # Post-Snow Leopard, pre-Catalina.
1745 # Build libraries for 64-bit x86 and 32-bit x86, with 64-bit x86
1746 # first, as PPC machines are no longer supported, but 32-bit
1747 # x86 executables are. (That's what Apple does.)
1749 # First, check whether we're building with OpenSSL.
1750 # If so, don't bother trying to build fat.
1753 set(X86_32_BIT_SUPPORTED NO)
1754 set(OSX_LIBRARY_ARCHITECTURES "x86_64")
1755 set(OSX_EXECUTABLE_ARCHITECTURES "x86_64")
1756 message(WARNING "We're assuming the OpenSSL libraries are 64-bit only, so we're not compiling for 32-bit x86")
1759 # Now, check whether we *can* build for i386.
1761 cmake_push_check_state()
1762 set(CMAKE_REQUIRED_FLAGS "-arch i386")
1763 check_c_source_compiles(
1770 X86_32_BIT_SUPPORTED)
1771 cmake_pop_check_state()
1772 if(X86_32_BIT_SUPPORTED)
1773 set(OSX_LIBRARY_ARCHITECTURES "x86_64;i386")
1775 set(OSX_LIBRARY_ARCHITECTURES "x86_64")
1777 # We can't build fat; suggest that the user install the
1778 # /usr/include headers if they want to build fat.
1780 if(SYSTEM_VERSION_MAJOR LESS 18)
1782 # Pre-Mojave; the command-line tools should be sufficient to
1783 # enable 32-bit x86 builds.
1785 message(WARNING "Compiling for 32-bit x86 gives an error; try installing the command-line tools")
1787 message(WARNING "Compiling for 32-bit x86 gives an error; try installing the command-line tools and, after that, installing the /usr/include headers from the /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg package")
1793 # Build executables only for 64-bit x86, as 32-bit x86 machines
1794 # are no longer supported.
1796 set(OSX_EXECUTABLE_ARCHITECTURES "x86_64")
1797 elseif(SYSTEM_VERSION_MAJOR EQUAL 19)
1801 # Build libraries and executables only for x86-64, as 32-bit
1802 # executables are no longer supported. (That's what Apple
1805 set(OSX_LIBRARY_ARCHITECTURES "x86_64")
1806 set(OSX_EXECUTABLE_ARCHITECTURES "x86_64")
1809 # Post-Catalina. Build libraries and
1810 # executables for x86-64 and ARM64.
1811 # (That's what Apple does, except they
1812 # build for arm64e, which may include
1813 # some of the pointer-checking extensions.)
1815 # If we're building with libssl, make sure
1816 # we can build fat with it (i.e., that it
1817 # was built fat); if we can't, don't set
1818 # the target architectures, and just
1819 # build for the host we're on.
1821 # Otherwise, just add both of them.
1824 cmake_push_check_state()
1825 set(CMAKE_REQUIRED_FLAGS "-arch x86_64 -arch arm64")
1826 set(CMAKE_REQUIRED_INCLUDES ${OPENSSL_INCLUDE_DIR})
1827 set(CMAKE_REQUIRED_LIBRARIES ${OPENSSL_LIBRARIES})
1829 # We must test whether this compiles and links, so
1830 # check_symbol_exists() isn't sufficient.
1832 # SSL_library_init() may be a macro that's #defined
1833 # to be the real function to call, so we have to
1834 # include <openssl/ssl.h>, and check_function_exists()
1837 check_c_source_compiles(
1838 "#include <openssl/ssl.h>
1846 FAT_SSL_BUILDS_SUPPORTED)
1847 cmake_pop_check_state()
1848 if(FAT_SSL_BUILDS_SUPPORTED)
1849 set(OSX_LIBRARY_ARCHITECTURES "x86_64;arm64")
1850 set(OSX_EXECUTABLE_ARCHITECTURES "x86_64;arm64")
1853 set(OSX_LIBRARY_ARCHITECTURES "x86_64;arm64")
1854 set(OSX_EXECUTABLE_ARCHITECTURES "x86_64;arm64")
1860 # Additional linker flags.
1862 set(LINKER_FLAGS "${SANITIZER_FLAGS}")
1863 if(ENABLE_PROFILING)
1865 set(LINKER_FLAGS " /PROFILE")
1867 set(LINKER_FLAGS " -pg")
1871 ######################################
1873 ######################################
1875 set(PROJECT_SOURCE_LIST_C
1894 # We add the character set conversion routines; they're Windows-only
1897 # We assume we don't have asprintf(), and provide an implementation
1898 # that uses _vscprintf() to determine how big the string needs to be.
1900 set(PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C}
1901 charconv.c missing/win_asprintf.c)
1903 if(NOT HAVE_ASPRINTF)
1904 set(PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C} missing/asprintf.c)
1906 if(NOT HAVE_STRLCAT)
1907 set(PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C} missing/strlcat.c)
1908 endif(NOT HAVE_STRLCAT)
1909 if(NOT HAVE_STRLCPY)
1910 set(PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C} missing/strlcpy.c)
1911 endif(NOT HAVE_STRLCPY)
1912 if(NOT HAVE_STRTOK_R)
1913 set(PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C} missing/strtok_r.c)
1914 endif(NOT HAVE_STRTOK_R)
1918 # Determine the main pcap-XXX.c file to use, and the libraries with
1919 # which we need to link libpcap, if any.
1925 # Has the user explicitly specified a capture type?
1927 if(PCAP_TYPE STREQUAL "")
1929 # The user didn't explicitly specify a capture mechanism.
1930 # Check whether we have packet.dll.
1934 # We have packet.dll.
1935 # Set the capture type to NPF.
1940 # We don't have any capture type we know about.
1941 # Report an error, and tell the user to configure with
1942 # -DPCAP_TYPE=null if they want a libpcap that can't
1943 # capture but that can read capture files. That way,
1944 # nobody gets surprised by getting a no-capture
1945 # libpcap without asking for that.
1947 message(FATAL_ERROR "No supported packet capture interface was found.
1948 In order to build a version of libpcap that supports packet capture
1949 on Windows, you will need to install Npcap and the Npcap SDK, or
1950 WinPcap and the WinPcap SDK, and run cmake with -DPacket_ROOT={path of SDK},
1951 where {path of SDK} is the path name of the top-level directory of the SDK.
1952 That argument may have to be quoted if the path contains blanks.
1953 If you want a libpcap that cannot capture packets but that can read
1954 pcap and pcapng files, run cmake with -DPCAP_TYPE=null.")
1961 # Figure out what type of packet capture mechanism we have, and
1962 # what libraries we'd need to link libpcap with, if any.
1966 # Has the user explicitly specified a capture type?
1968 if(PCAP_TYPE STREQUAL "")
1970 # No. Try auto-detecting the obvious types first.
1972 if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
1973 set(PCAP_TYPE linux)
1974 elseif(CMAKE_SYSTEM_NAME STREQUAL "Haiku")
1975 set(PCAP_TYPE haiku)
1976 elseif(CMAKE_SYSTEM_NAME STREQUAL "GNU")
1981 if(PCAP_TYPE STREQUAL "")
1983 # It is not one of the above obvious types, let's see if it is BPF.
1984 # Check this before DLPI to pick BPF on Solaris 11 and later.
1986 check_include_files("sys/types.h;net/bpf.h" HAVE_NET_BPF_H)
1989 # HAVE_SYS_IOCCOM_H will be required for a few workarounds until all
1990 # supported OSes that use BPF have <net/bpf.h> that includes <sys/ioccom.h>
1991 # (this might have already happened).
1993 check_include_file(sys/ioccom.h HAVE_SYS_IOCCOM_H)
1996 # Does it define BIOCSETIF?
1997 # I.e., is it a header for an LBL/BSD-style capture
1998 # mechanism, or is it just a header for a BPF filter
1999 # engine? Some versions of Arch Linux, for example,
2000 # have a net/bpf.h that doesn't define BIOCSETIF;
2001 # as it's a Linux, it should use packet sockets,
2006 # sys/types.h, because FreeBSD 10's net/bpf.h
2007 # requires that various BSD-style integer types
2010 # sys/time.h, because AIX 5.2 and 5.3's net/bpf.h
2011 # doesn't include it but does use struct timeval
2012 # in ioctl definitions;
2014 # sys/ioctl.h and, if we have it, sys/ioccom.h,
2015 # because net/bpf.h defines ioctls;
2017 # net/if.h, because it defines some structures
2018 # used in ioctls defined by net/bpf.h;
2020 # sys/socket.h, because OpenBSD 5.9's net/bpf.h
2021 # defines some structure fields as being
2024 # and net/bpf.h doesn't necessarily include all
2025 # of those headers itself.
2027 if(HAVE_SYS_IOCCOM_H)
2028 check_symbol_exists(BIOCSETIF "sys/types.h;sys/time.h;sys/ioctl.h;sys/socket.h;sys/ioccom.h;net/bpf.h;net/if.h" BPF_H_DEFINES_BIOCSETIF)
2029 else(HAVE_SYS_IOCCOM_H)
2030 check_symbol_exists(BIOCSETIF "sys/types.h;sys/time.h;sys/ioctl.h;sys/socket.h;net/bpf.h;net/if.h" BPF_H_DEFINES_BIOCSETIF)
2031 endif(HAVE_SYS_IOCCOM_H)
2032 endif(HAVE_NET_BPF_H)
2034 if(BPF_H_DEFINES_BIOCSETIF)
2039 if(PCAP_TYPE STREQUAL "")
2041 # It is not BPF either, let's see if it is DLPI on pre-Solaris 11
2042 # SunOS 5, HP-UX, possibly others. Otherwise fail properly.
2044 check_include_file(sys/dlpi.h HAVE_SYS_DLPI_H)
2047 # DLPI on pre-Solaris 11 SunOS 5, HP-UX, possibly others.
2052 # We don't have any capture type we know about.
2053 # Report an error, and tell the user to configure with
2054 # -DPCAP_TYPE=null if they want a libpcap that can't
2055 # capture but that can read capture files. That way,
2056 # nobody gets surprised by getting a no-capture
2057 # libpcap without asking for that.
2059 message(FATAL_ERROR "No supported packet capture interface was found.
2060 See the INSTALL.md file for information on packet capture support in
2061 various operating systems.
2062 If you want a libpcap that cannot capture packets but that can read
2063 pcap and pcapng files, run cmake with -DPCAP_TYPE=null.")
2067 message(STATUS "Packet capture mechanism type: ${PCAP_TYPE}")
2069 find_package(PkgConfig)
2072 # Do capture-mechanism-dependent tests.
2075 if(PCAP_TYPE STREQUAL "npf")
2077 # Link with packet.dll before Winsock2.
2079 set(PCAP_LINK_LIBRARIES ${Packet_LIBRARIES} ${PCAP_LINK_LIBRARIES})
2080 elseif(PCAP_TYPE STREQUAL "null")
2082 message(FATAL_ERROR "${PCAP_TYPE} is not a valid pcap type")
2085 if(PCAP_TYPE STREQUAL "dlpi")
2087 # Needed for common functions used by pcap-[dlpi,libdlpi].c
2089 set(PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C} dlpisubs.c)
2092 # Checks for some header files.
2094 check_include_file(sys/bufmod.h HAVE_SYS_BUFMOD_H)
2095 check_include_file(sys/dlpi_ext.h HAVE_SYS_DLPI_EXT_H)
2098 # Checks to see if Solaris has the public libdlpi(3LIB) library.
2099 # Note: The existence of /usr/include/libdlpi.h does not mean it is the
2100 # public libdlpi(3LIB) version. Before libdlpi was made public, a
2101 # private version also existed, which did not have the same APIs.
2102 # Due to a gcc bug, the default search path for 32-bit libraries does
2103 # not include /lib, we add it explicitly here.
2104 # [http://bugs.opensolaris.org/view_bug.do?bug_id=6619485].
2105 # Also, due to the bug above applications that link to libpcap with
2106 # libdlpi will have to add "-L/lib" option to "configure".
2108 cmake_push_check_state()
2109 set(CMAKE_REQUIRED_FLAGS "-L/lib")
2110 set(CMAKE_REQUIRED_LIBRARIES dlpi)
2111 check_function_exists(dlpi_walk HAVE_LIBDLPI)
2112 cmake_pop_check_state()
2117 set(PCAP_LINK_LIBRARIES ${PCAP_LINK_LIBRARIES} dlpi)
2118 set(LIBS "${LIBS} -ldlpi")
2119 set(LIBS_STATIC "${LIBS_STATIC} -ldlpi")
2120 set(LIBS_PRIVATE "${LIBS_PRIVATE} -ldlpi")
2121 set(PCAP_TYPE libdlpi)
2125 # This check is for Solaris with DLPI support for passive modes.
2126 # See dlpi(7P) for more details.
2128 # XXX - there's no check_type() macro that's like check_type_size()
2129 # except that it only checks for the existence of the structure type,
2130 # so we use check_type_size() and ignore the size.
2132 cmake_push_check_state()
2133 set(CMAKE_EXTRA_INCLUDE_FILES sys/types.h sys/dlpi.h)
2134 check_type_size(dl_passive_req_t DL_PASSIVE_REQ_T)
2135 cmake_pop_check_state()
2136 elseif(PCAP_TYPE STREQUAL "linux")
2139 # We only want version 3. Version 2 was, apparently,
2140 # short-lived, and version 1 is source and binary
2141 # incompatible with version 3, and it appears that,
2142 # these days, everybody's using version 3. We're
2143 # not supporting older versions of the Linux kernel;
2144 # let's drop support for older versions of libnl, too.
2146 # Due to lack of testing facilities this block does not
2147 # include a branch for libnl-tiny with pkg-config, which
2148 # is not consistent with Autoconf.
2150 if(BUILD_WITH_LIBNL)
2151 pkg_check_modules(LIBNL libnl-genl-3.0)
2153 set(HAVE_LIBNL TRUE)
2154 include_directories(${LIBNL_INCLUDE_DIRS})
2155 set(PCAP_LINK_LIBRARIES ${LIBNL_LIBRARIES} ${PCAP_LINK_LIBRARIES})
2158 # Get raw link flags from pkg-config.
2160 pkg_get_link_info(LIBNL libnl-genl-3.0)
2161 set(LIBS "${LIBNL_LIBS} ${LIBS}")
2162 set(LIBS_STATIC "${LIBNL_LIBS_STATIC} ${LIBS_STATIC}")
2163 set(REQUIRES_PRIVATE "${LIBNL_PACKAGE_NAME} ${REQUIRES_PRIVATE}")
2165 cmake_push_check_state()
2166 set(CMAKE_REQUIRED_LIBRARIES nl-3)
2167 check_function_exists(nl_socket_alloc HAVE_LIBNL)
2168 cmake_pop_check_state()
2171 # Yes, we have libnl 3.x.
2173 set(PCAP_LINK_LIBRARIES nl-genl-3 nl-3 ${PCAP_LINK_LIBRARIES})
2174 include_directories("/usr/include/libnl3")
2175 set(LIBS "-lnl-genl-3 -lnl-3 ${LIBS}")
2176 set(LIBS_STATIC "-lnl-genl-3 -lnl-3 ${LIBS_STATIC}")
2177 set(LIBS_PRIVATE "-lnl-genl-3 -lnl-3 ${LIBS_PRIVATE}")
2181 unset(HAVE_LIBNL CACHE) # check_function_exists stores results in cache
2184 check_struct_has_member("struct tpacket_auxdata" tp_vlan_tci linux/if_packet.h HAVE_STRUCT_TPACKET_AUXDATA_TP_VLAN_TCI)
2186 # This check is for TESTrun purposes, not for the C code.
2187 # This and the #cmakedefine01 in cmakeconfig.h.in reproduce exactly the
2188 # behaviour of "AC_CHECK_DECLS([SKF_AD_VLAN_TAG_PRESENT], ...".
2189 check_symbol_exists(SKF_AD_VLAN_TAG_PRESENT linux/filter.h HAVE_DECL_SKF_AD_VLAN_TAG_PRESENT)
2190 elseif(PCAP_TYPE STREQUAL "bpf")
2192 # Check whether we have the *BSD-style ioctls.
2194 check_include_files("sys/types.h;net/if_media.h" HAVE_NET_IF_MEDIA_H)
2197 # Check whether we have struct BPF_TIMEVAL.
2199 # XXX - there's no check_type() macro that's like check_type_size()
2200 # except that it only checks for the existence of the structure type,
2201 # so we use check_type_size() and ignore the size.
2203 cmake_push_check_state()
2204 if(HAVE_SYS_IOCCOM_H)
2205 set(CMAKE_EXTRA_INCLUDE_FILES sys/types.h sys/ioccom.h net/bpf.h)
2206 check_type_size("struct BPF_TIMEVAL" STRUCT_BPF_TIMEVAL)
2208 set(CMAKE_EXTRA_INCLUDE_FILES sys/types.h net/bpf.h)
2209 check_type_size("struct BPF_TIMEVAL" STRUCT_BPF_TIMEVAL)
2211 cmake_pop_check_state()
2213 if(CMAKE_SYSTEM_NAME STREQUAL "SunOS")
2215 # Check whether there's a inet/ipnet.h header and,
2216 # if so, whether it defines IPNET_ANY_LINK - if so,
2217 # we assume we have the "any" device (that's a
2218 # Solaris header, and later versions of Solaris
2219 # have an "any" device).
2221 # Attempting to include it at compile time could
2222 # be a pain, as it's a kernel header.
2224 message(STATUS "Checking whether the Solaris \"any\" device is supported")
2225 if(EXISTS /usr/include/inet/ipnet.h)
2226 file(STRINGS /usr/include/inet/ipnet.h IPNET_ANY_LINK_LINES REGEX IPNET_ANY_LINK)
2227 if(NOT IPNET_ANY_LINK_LINES STREQUAL "")
2228 set(HAVE_SOLARIS_ANY_DEVICE TRUE)
2231 if(HAVE_SOLARIS_ANY_DEVICE)
2232 message(STATUS "Checking whether the Solaris \"any\" device is supported - supported")
2234 message(STATUS "Checking whether the Solaris \"any\" device is supported - not supported")
2237 elseif(PCAP_TYPE STREQUAL "haiku")
2238 set(PCAP_SRC pcap-${PCAP_TYPE}.c)
2239 elseif(PCAP_TYPE STREQUAL "hurd")
2241 # Check for some headers just in case.
2243 set(PCAP_LINK_LIBRARIES ${PCAP_LINK_LIBRARIES} rt)
2244 set(LIBS "${LIBS} -lrt")
2245 set(LIBS_STATIC "${LIBS_STATIC} -lrt")
2246 set(LIBS_PRIVATE "${LIBS_PRIVATE} -lrt")
2247 set(PCAP_SRC pcap-${PCAP_TYPE}.c)
2248 elseif(PCAP_TYPE STREQUAL "null")
2250 message(FATAL_ERROR "${PCAP_TYPE} is not a valid pcap type")
2254 if(NOT DEFINED PCAP_SRC)
2255 set(PCAP_SRC pcap-${PCAP_TYPE}.c)
2258 set(PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C} ${PCAP_SRC})
2261 # Now figure out how we get a list of interfaces and addresses,
2262 # if we support capturing. Don't bother if we don't support
2267 # UN*X - figure out what type of interface list mechanism we
2270 # If the capture type is null, that means we can't capture,
2271 # so we can't open any capture devices, so we won't return
2274 if(NOT PCAP_TYPE STREQUAL "null")
2275 cmake_push_check_state()
2276 set(CMAKE_REQUIRED_LIBRARIES ${PCAP_LINK_LIBRARIES})
2277 check_function_exists(getifaddrs HAVE_GETIFADDRS)
2278 cmake_pop_check_state()
2279 if(NOT HAVE_GETIFADDRS)
2281 # It's not in the libraries that, at this point, we've
2282 # found we need to link libpcap with.
2284 # It's in libsocket on Solaris and possibly other OSes;
2285 # as long as we're not linking with libxnet, check there.
2287 # NOTE: if you hand check_library_exists as its last
2288 # argument a variable that's been set, it skips the test,
2289 # so we need different variables.
2291 if(NOT LIBXNET_HAS_GETHOSTBYNAME)
2292 check_library_exists(socket getifaddrs "" SOCKET_HAS_GETIFADDRS)
2293 if(SOCKET_HAS_GETIFADDRS)
2294 set(PCAP_LINK_LIBRARIES socket ${PCAP_LINK_LIBRARIES})
2295 set(LIBS "-lsocket ${LIBS}")
2296 set(LIBS_STATIC "-lsocket ${LIBS_STATIC}")
2297 set(LIBS_PRIVATE "-lsocket ${LIBS_PRIVATE}")
2298 set(HAVE_GETIFADDRS TRUE)
2304 # We have "getifaddrs()"; make sure we have <ifaddrs.h>
2305 # as well, just in case some platform is really weird.
2306 # It may require that sys/types.h be included first,
2307 # so include it first.
2309 check_include_files("sys/types.h;ifaddrs.h" HAVE_IFADDRS_H)
2312 # We have the header, so we use "getifaddrs()" to
2313 # get the list of interfaces.
2315 set(FINDALLDEVS_TYPE getad)
2318 # We don't have the header - give up.
2319 # XXX - we could also fall back on some other
2320 # mechanism, but, for now, this'll catch this
2321 # problem so that we can at least try to figure
2322 # out something to do on systems with "getifaddrs()"
2323 # but without "ifaddrs.h", if there is something
2324 # we can do on those systems.
2326 message(FATAL_ERROR "Your system has getifaddrs() but doesn't have a usable <ifaddrs.h>.")
2330 # Well, we don't have "getifaddrs()", at least not with the
2331 # libraries with which we've decided we need to link
2332 # libpcap with, so we have to use some other mechanism.
2334 # Note that this may happen on Solaris, which has
2335 # getifaddrs(), but in -lsocket, not in -lxnet, so we
2336 # won't find it if we link with -lxnet, which we want
2337 # to do for other reasons.
2339 # For now, we use either the SIOCGIFCONF ioctl or the
2340 # SIOCGLIFCONF ioctl, preferring the latter if we have
2341 # it; the latter is a Solarisism that first appeared
2342 # in Solaris 8. (Solaris's getifaddrs() appears to
2343 # be built atop SIOCGLIFCONF; using it directly
2344 # avoids a not-all-that-useful middleman.)
2346 try_compile(HAVE_SIOCGLIFCONF ${CMAKE_CURRENT_BINARY_DIR} "${pcap_SOURCE_DIR}/cmake/have_siocglifconf.c" )
2347 if(HAVE_SIOCGLIFCONF)
2348 set(FINDALLDEVS_TYPE glifc)
2350 set(FINDALLDEVS_TYPE gifc)
2353 message(STATUS "Find-interfaces mechanism type: ${FINDALLDEVS_TYPE}")
2354 set(PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C} fad-${FINDALLDEVS_TYPE}.c)
2358 # Check for hardware timestamp support.
2359 if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
2360 check_include_file(linux/net_tstamp.h HAVE_LINUX_NET_TSTAMP_H)
2364 # Check for additional native sniffing capabilities.
2368 # Various Linux-specific mechanisms.
2370 if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
2371 # Check for usbmon USB sniffing support.
2372 if(NOT DISABLE_LINUX_USBMON)
2373 set(PCAP_SUPPORT_LINUX_USBMON TRUE)
2374 set(PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C} pcap-usb-linux.c)
2376 # Do we have a version of <linux/compiler.h> available?
2377 # If so, we might need it for <linux/usbdevice_fs.h>.
2379 check_include_files("linux/compiler.h" HAVE_LINUX_COMPILER_H)
2380 if(HAVE_LINUX_COMPILER_H)
2382 # Yes - include it when testing for <linux/usbdevice_fs.h>.
2384 check_include_files("linux/compiler.h;linux/usbdevice_fs.h" HAVE_LINUX_USBDEVICE_FS_H)
2385 else(HAVE_LINUX_COMPILER_H)
2386 check_include_files("linux/usbdevice_fs.h" HAVE_LINUX_USBDEVICE_FS_H)
2387 endif(HAVE_LINUX_COMPILER_H)
2388 if(HAVE_LINUX_USBDEVICE_FS_H)
2390 # OK, does it define bRequestType? Older versions of the kernel
2391 # define fields with names like "requesttype, "request", and
2392 # "value", rather than "bRequestType", "bRequest", and
2395 if(HAVE_LINUX_COMPILER_H)
2396 check_struct_has_member("struct usbdevfs_ctrltransfer" bRequestType "linux/compiler.h;linux/usbdevice_fs.h" HAVE_STRUCT_USBDEVFS_CTRLTRANSFER_BREQUESTTYPE)
2397 else(HAVE_LINUX_COMPILER_H)
2398 check_struct_has_member("struct usbdevfs_ctrltransfer" bRequestType "linux/usbdevice_fs.h" HAVE_STRUCT_USBDEVFS_CTRLTRANSFER_BREQUESTTYPE)
2399 endif(HAVE_LINUX_COMPILER_H)
2404 # Check for netfilter sniffing support.
2406 # Life's too short to deal with trying to get this to compile
2407 # if you don't get the right types defined with
2408 # __KERNEL_STRICT_NAMES getting defined by some other include.
2410 # Check whether the includes Just Work. If not, don't turn on
2411 # netfilter support.
2413 check_c_source_compiles(
2414 "#include <sys/socket.h>
2415 #include <netinet/in.h>
2416 #include <linux/types.h>
2418 #include <linux/netlink.h>
2419 #include <linux/netfilter.h>
2420 #include <linux/netfilter/nfnetlink.h>
2421 #include <linux/netfilter/nfnetlink_log.h>
2422 #include <linux/netfilter/nfnetlink_queue.h>
2430 PCAP_SUPPORT_NETFILTER)
2431 if(PCAP_SUPPORT_NETFILTER)
2432 set(PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C} pcap-netfilter-linux.c)
2433 endif(PCAP_SUPPORT_NETFILTER)
2436 # Check for netmap sniffing support.
2437 if(NOT DISABLE_NETMAP)
2439 # Check whether net/netmap_user.h is usable if NETMAP_WITH_LIBS is
2440 # defined; it's not usable on DragonFly BSD 4.6 if NETMAP_WITH_LIBS
2441 # is defined, for example, as it includes a nonexistent malloc.h
2444 check_c_source_compiles(
2445 "#define NETMAP_WITH_LIBS
2446 #include <net/netmap_user.h>
2454 PCAP_SUPPORT_NETMAP)
2455 if(PCAP_SUPPORT_NETMAP)
2456 set(PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C} pcap-netmap.c)
2457 endif(PCAP_SUPPORT_NETMAP)
2460 # Check for DPDK sniffing support
2461 if(NOT DISABLE_DPDK)
2465 # We call rte_eth_dev_count_avail(), and older versions of DPDK
2466 # didn't have it, so check for it.
2468 cmake_push_check_state()
2469 set(CMAKE_REQUIRED_INCLUDES ${dpdk_INCLUDE_DIRS})
2470 set(CMAKE_REQUIRED_LIBRARIES ${dpdk_LIBRARIES})
2471 check_function_exists(rte_eth_dev_count_avail HAVE_RTE_ETH_DEV_COUNT_AVAIL)
2472 cmake_pop_check_state()
2473 if(HAVE_RTE_ETH_DEV_COUNT_AVAIL)
2474 set(DPDK_C_FLAGS "-march=native")
2475 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${DPDK_C_FLAGS}")
2476 include_directories(AFTER ${dpdk_INCLUDE_DIRS})
2477 link_directories(AFTER ${dpdk_LIBRARIES})
2478 set(PCAP_LINK_LIBRARIES ${PCAP_LINK_LIBRARIES} ${dpdk_LIBRARIES})
2479 set(LIBS "${LIBS} ${dpdk_LIBS}")
2480 set(LIBS_STATIC "${LIBS_STATIC} ${dpdk_LIBS_STATIC}")
2481 set(REQUIRES_PRIVATE "${REQUIRES_PRIVATE} ${dpdk_PACKAGE_NAME}")
2482 set(PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C} pcap-dpdk.c)
2483 set(PCAP_SUPPORT_DPDK TRUE)
2486 # Check whether the rte_ether.h file defines
2487 # struct ether_addr or struct rte_ether_addr.
2489 # ("API compatibility? That's for losers!")
2491 cmake_push_check_state()
2492 set(CMAKE_REQUIRED_INCLUDES ${dpdk_INCLUDE_DIRS})
2493 set(CMAKE_EXTRA_INCLUDE_FILES rte_ether.h)
2494 check_type_size("struct rte_ether_addr" STRUCT_RTE_ETHER_ADDR)
2495 cmake_pop_check_state()
2498 # Same as in Autoconf.
2500 message(STATUS "building with DPDK")
2503 # Same as in Autoconf.
2505 message(STATUS "building without DPDK (present, but cannot be used)")
2507 # Else: Finddpdk.cmake has already printed the diagnostics.
2511 # Check for Bluetooth sniffing support
2512 if(NOT DISABLE_BLUETOOTH)
2513 if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
2514 check_include_file(bluetooth/bluetooth.h HAVE_BLUETOOTH_BLUETOOTH_H)
2515 if(HAVE_BLUETOOTH_BLUETOOTH_H)
2516 set(PCAP_SUPPORT_BT TRUE)
2517 set(PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C} pcap-bt-linux.c)
2519 # OK, does struct sockaddr_hci have an hci_channel
2522 check_struct_has_member("struct sockaddr_hci" hci_channel "bluetooth/bluetooth.h;bluetooth/hci.h" HAVE_STRUCT_SOCKADDR_HCI_HCI_CHANNEL)
2523 if(HAVE_STRUCT_SOCKADDR_HCI_HCI_CHANNEL)
2525 # OK, is HCI_CHANNEL_MONITOR defined?
2527 check_c_source_compiles(
2528 "#include <bluetooth/bluetooth.h>
2529 #include <bluetooth/hci.h>
2534 int i = HCI_CHANNEL_MONITOR;
2538 PCAP_SUPPORT_BT_MONITOR)
2539 if(PCAP_SUPPORT_BT_MONITOR)
2541 # Yes, so we can also support Bluetooth monitor
2544 set(PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C} pcap-bt-monitor-linux.c)
2545 endif(PCAP_SUPPORT_BT_MONITOR)
2546 endif(HAVE_STRUCT_SOCKADDR_HCI_HCI_CHANNEL)
2547 endif(HAVE_BLUETOOTH_BLUETOOTH_H)
2550 unset(PCAP_SUPPORT_BT_MONITOR CACHE)
2553 # Check for D-Bus sniffing support
2554 if(NOT DISABLE_DBUS)
2556 # We don't support D-Bus sniffing on macOS; see
2558 # https://bugs.freedesktop.org/show_bug.cgi?id=74029
2561 message(FATAL_ERROR "Due to freedesktop.org bug 74029, D-Bus capture support is not available on macOS")
2563 pkg_check_modules(DBUS dbus-1)
2565 set(PCAP_SUPPORT_DBUS TRUE)
2566 set(PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C} pcap-dbus.c)
2567 include_directories(${DBUS_INCLUDE_DIRS})
2570 # This "helpfully" supplies DBUS_LIBRARIES as a bunch of
2571 # library names - not paths - and DBUS_LIBRARY_DIRS as
2572 # a bunch of directories.
2574 # CMake *really* doesn't like the notion of specifying "here are
2575 # the directories in which to look for libraries" except in
2576 # find_library() calls; it *really* prefers using full paths to
2577 # library files, rather than library names.
2579 # Find the libraries and add their full paths.
2581 set(DBUS_LIBRARY_FULLPATHS)
2582 foreach(_lib IN LISTS DBUS_LIBRARIES)
2584 # Try to find this library, so we get its full path.
2586 find_library(_libfullpath ${_lib} HINTS ${DBUS_LIBRARY_DIRS})
2587 list(APPEND DBUS_LIBRARY_FULLPATHS ${_libfullpath})
2589 set(PCAP_LINK_LIBRARIES ${PCAP_LINK_LIBRARIES} ${DBUS_LIBRARY_FULLPATHS})
2592 # Get library information for DPDK.
2594 pkg_get_link_info(DBUS dbus-1)
2595 set(LIBS "${LIBS} ${DBUS_LIBS}")
2596 set(LIBS_STATIC "${LIBS_STATIC} ${DBUS_LIBS_STATIC}")
2597 set(REQUIRES_PRIVATE "${REQUIRES_PRIVATE} ${DBUS_PACKAGE_NAME}")
2599 endif(NOT DISABLE_DBUS)
2601 # Check for RDMA sniffing support
2602 if(NOT DISABLE_RDMA)
2603 pkg_check_modules(LIBIBVERBS libibverbs)
2604 if(LIBIBVERBS_FOUND)
2606 # pkg-config found it; remember its pkg-config name.
2608 set(LIBIBVERBS_REQUIRES_PRIVATE ${LIBIBVERBS_PACKAGE_NAME})
2611 # Get static linking information for it.
2613 pkg_get_link_info(LIBIBVERBS libibverbs)
2616 # pkg-config didn't find it; try to look for it ourselves
2618 check_library_exists(ibverbs ibv_get_device_list "" LIBIBVERBS_HAS_IBV_GET_DEVICE_LIST)
2619 if(LIBIBVERBS_HAS_IBV_GET_DEVICE_LIST)
2620 set(LIBIBVERBS_FOUND TRUE)
2621 set(LIBIBVERBS_LIBRARIES ibverbs)
2622 # XXX - at least on Ubuntu 20.04, there are many more
2623 # libraries needed; is there any platform where
2624 # libibverbs is available but where pkg-config
2625 # isn't available or libibverbs doesn't use it?
2626 # If not, we should only use pkg-config for it.
2627 set(LIBIBVERBS_STATIC_LIBRARIES ibverbs)
2628 set(LIBIBVERBS_LIBS -libverbs)
2629 set(LIBIBVERBS_LIBS_STATIC -libverbs)
2630 set(LIBIBVERBS_LIBS_PRIVATE -libverbs)
2633 if(LIBIBVERBS_FOUND)
2635 # For unknown reasons, check_include_file() doesn't just attempt
2636 # to compile a test program that includes the header in
2637 # question, it also attempts to link it.
2639 # For unknown reasons, at least some of the static inline
2640 # functions defined in infiniband/verbs.h are not inlined by the
2641 # Sun^WOracle Studio C compiler, so the compiler generates code
2642 # for them as part of the object code resulting from compiling
2643 # the test program. At lest some of those functions call
2644 # routines in -libverbs, so, in order to keep the compile and
2645 # link from failing, even though the header file exists and is
2646 # usable, we need to link with -libverbs.
2648 cmake_push_check_state()
2649 set(CMAKE_REQUIRED_LIBRARIES ${LIBIBVERBS_LIBRARIES})
2650 check_include_file(infiniband/verbs.h HAVE_INFINIBAND_VERBS_H)
2651 if(HAVE_INFINIBAND_VERBS_H)
2652 check_symbol_exists(ibv_create_flow infiniband/verbs.h PCAP_SUPPORT_RDMASNIFF)
2653 if(PCAP_SUPPORT_RDMASNIFF)
2654 set(PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C} pcap-rdmasniff.c)
2655 set(PCAP_LINK_LIBRARIES ${LIBIBVERBS_LIBRARIES} ${PCAP_LINK_LIBRARIES})
2656 set(LIBS "${LIBIBVERBS_LIBS} ${LIBS}")
2657 set(LIBS_STATIC "${LIBIBVERBS_LIBS_STATIC} ${LIBS_STATIC}")
2658 set(LIBS_PRIVATE "${LIBIBVERBS_LIBS_PRIVATE} ${LIBS_PRIVATE}")
2659 set(REQUIRES_PRIVATE "${REQUIRES_PRIVATE} ${LIBIBVERBS_PACKAGE_NAME}")
2660 endif(PCAP_SUPPORT_RDMASNIFF)
2661 endif(HAVE_INFINIBAND_VERBS_H)
2662 cmake_pop_check_state()
2663 endif(LIBIBVERBS_FOUND)
2664 endif(NOT DISABLE_RDMA)
2667 # Check for sniffing capabilities using third-party APIs.
2670 # Check for Endace DAG card support.
2673 # Try to find the DAG header file and library.
2683 # Check for various DAG API functions.
2685 cmake_push_check_state()
2686 set(CMAKE_REQUIRED_INCLUDES ${DAG_INCLUDE_DIRS})
2687 set(CMAKE_REQUIRED_LIBRARIES ${DAG_LIBRARIES})
2688 check_function_exists(dag_attach_stream64 HAVE_DAG_STREAMS_API)
2689 if(NOT HAVE_DAG_STREAMS_API)
2690 message(FATAL_ERROR "DAG library lacks 64-bit streams support")
2692 cmake_pop_check_state()
2694 include_directories(AFTER ${DAG_INCLUDE_DIRS})
2695 set(PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C} pcap-dag.c)
2696 set(HAVE_DAG_API TRUE)
2697 set(PCAP_LINK_LIBRARIES ${PCAP_LINK_LIBRARIES} ${DAG_LIBRARIES})
2698 set(LIBS "${LIBS} ${DAG_LIBS}")
2699 set(LIBS_STATIC "${LIBS_STATIC} ${DAG_LIBS_STATIC}")
2700 set(LIBS_PRIVATE "${LIBS_PRIVATE} ${DAG_LIBS_PRIVATE}")
2702 get_filename_component(DAG_LIBRARY_DIR ${DAG_LIBRARY} PATH)
2703 check_library_exists(vdag vdag_set_device_info ${DAG_LIBRARY_DIR} HAVE_DAG_VDAG)
2705 set(PCAP_LINK_LIBRARIES ${PCAP_LINK_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
2706 set(LIBS "${LIBS} ${CMAKE_THREAD_LIBS_INIT}")
2707 set(LIBS_STATIC "${LIBS_STATIC} ${CMAKE_THREAD_LIBS_INIT}")
2708 set(LIBS_PRIVATE "${LIBS_PRIVATE} ${CMAKE_THREAD_LIBS_INIT}")
2712 set(ENABLE_DAG_TX TRUE)
2714 message(STATUS "Endace DAG transmit support (EXPERIMENTAL): ${ENABLE_DAG_TX}")
2718 # Check for Myricom SNF support.
2721 # Try to find the SNF header file and library.
2732 include_directories(AFTER ${SNF_INCLUDE_DIRS})
2733 set(PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C} pcap-snf.c)
2734 set(HAVE_SNF_API TRUE)
2735 set(PCAP_LINK_LIBRARIES ${PCAP_LINK_LIBRARIES} ${SNF_LIBRARIES})
2736 set(LIBS "${LIBS_STATIC} ${SNF_LIBS}")
2737 set(LIBS_STATIC "${LIBS_STATIC} ${SNF_LIBS_STATIC}")
2738 set(LIBS_PRIVATE "${LIBS_PRIVATE} ${SNF_LIBS_PRIVATE}")
2743 # Remote capture support.
2748 # Check for various members of struct msghdr.
2749 # We need to include ftmacros.h on some platforms, to make sure we
2750 # get the POSIX/Single USER Specification version of struct msghdr,
2751 # which has those members, rather than the backwards-compatible
2752 # version, which doesn't. That's not a system header file, and
2753 # at least some versions of CMake include it as <ftmacros.h>, which
2754 # won't check the current directory, so we add the top-level
2755 # source directory to the list of include directories when we do
2758 cmake_push_check_state()
2759 set(CMAKE_REQUIRED_INCLUDES ${CMAKE_CURRENT_SOURCE_DIR})
2760 check_struct_has_member("struct msghdr" msg_control "ftmacros.h;sys/socket.h" HAVE_STRUCT_MSGHDR_MSG_CONTROL)
2761 check_struct_has_member("struct msghdr" msg_flags "ftmacros.h;sys/socket.h" HAVE_STRUCT_MSGHDR_MSG_FLAGS)
2762 cmake_pop_check_state()
2763 set(PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C}
2764 pcap-rpcap.c rpcap-protocol.c sockutils.c)
2766 set(PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C} sslutils.c)
2768 endif(ENABLE_REMOTE)
2770 ###################################################################
2772 ###################################################################
2775 # Check and add warning options if we have a .devel file.
2777 if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/.devel OR EXISTS ${CMAKE_BINARY_DIR}/.devel)
2781 if(MSVC AND NOT ${CMAKE_C_COMPILER} MATCHES "clang*")
2783 # MSVC, with Microsoft's front end and code generator.
2784 # "MSVC" is also set for Microsoft's compiler with a Clang
2785 # front end and their code generator ("Clang/C2"), so we
2786 # check for clang.exe and treat that differently.
2788 check_and_add_compiler_option(-Wall)
2790 # Disable some pointless warnings that /Wall turns on.
2792 # Unfortunately, MSVC does not appear to have an equivalent
2793 # to "__attribute__((unused))" to mark a particular function
2794 # parameter as being known to be unused, so that the compiler
2795 # won't warn about it (for example, the function might have
2796 # that parameter because a pointer to it is being used, and
2797 # the signature of that function includes that parameter).
2798 # C++ lets you give a parameter a type but no name, but C
2799 # doesn't have that.
2801 check_and_add_compiler_option(-wd4100)
2803 # In theory, we care whether somebody uses f() rather than
2804 # f(void) to declare a function with no arguments, but, in
2805 # practice, there are places in the Windows header files
2806 # that appear to do that, so we squelch that warning.
2808 check_and_add_compiler_option(-wd4255)
2810 # Windows FD_SET() generates this, so we suppress it.
2812 check_and_add_compiler_option(-wd4548)
2814 # Perhaps testing something #defined to be 0 with #ifdef is an
2815 # error, and it should be tested with #if, but perhaps it's
2816 # not, and Microsoft does that in its headers, so we squelch
2819 check_and_add_compiler_option(-wd4574)
2821 # The Windows headers also test not-defined values in #if, so
2822 # we don't want warnings about that, either.
2824 check_and_add_compiler_option(-wd4668)
2826 # We do *not* care whether some function is, or isn't, going to be
2829 check_and_add_compiler_option(-wd4710)
2830 check_and_add_compiler_option(-wd4711)
2832 # We do *not* care whether we're adding padding bytes after
2833 # structure members.
2835 check_and_add_compiler_option(-wd4820)
2837 # We do *not* care about every single place the compiler would
2838 # have inserted Spectre mitigation if only we had told it to
2839 # do so with /Qspectre. Maybe it's worth it, as that's in
2840 # Bison-generated code that we don't control.
2842 # XXX - add /Qspectre if that is really worth doing.
2844 check_and_add_compiler_option(-wd5045)
2847 # Treat all (remaining) warnings as errors.
2849 check_and_add_compiler_option(-WX)
2852 # Other compilers, including MSVC with a Clang front end and
2853 # Microsoft's code generator. We currently treat them as if
2854 # they might support GCC-style -W options.
2856 check_and_add_compiler_option(-W)
2857 check_and_add_compiler_option(-Wall)
2858 check_and_add_compiler_option(-Wcomma)
2859 # Warns about safeguards added in case the enums are extended
2860 # check_and_add_compiler_option(-Wcovered-switch-default)
2861 check_and_add_compiler_option(-Wdocumentation)
2862 check_and_add_compiler_option(-Wformat-nonliteral)
2863 check_and_add_compiler_option(-Wmissing-noreturn)
2864 check_and_add_compiler_option(-Wmissing-prototypes)
2865 check_and_add_compiler_option(-Wmissing-variable-declarations)
2866 check_and_add_compiler_option(-Wnull-pointer-subtraction)
2867 check_and_add_compiler_option(-Wpointer-arith)
2868 check_and_add_compiler_option(-Wpointer-sign)
2869 check_and_add_compiler_option(-Wshadow)
2870 check_and_add_compiler_option(-Wshorten-64-to-32)
2871 check_and_add_compiler_option(-Wsign-compare)
2872 check_and_add_compiler_option(-Wstrict-prototypes)
2873 check_and_add_compiler_option(-Wundef)
2874 check_and_add_compiler_option(-Wunreachable-code)
2875 check_and_add_compiler_option(-Wunused-but-set-parameter)
2876 check_and_add_compiler_option(-Wunused-but-set-variable)
2877 check_and_add_compiler_option(-Wunused-parameter)
2878 check_and_add_compiler_option(-Wused-but-marked-unused)
2883 # Suppress some warnings we get with MSVC even without /Wall.
2885 if(MSVC AND NOT ${CMAKE_C_COMPILER} MATCHES "clang*")
2887 # Yes, we have some functions that never return but that
2888 # have a non-void return type. That's because, on some
2889 # platforms, they *do* return values but, on other
2890 # platforms, including Windows, they just fail and
2891 # longjmp out by calling bpf_error().
2893 check_and_add_compiler_option(-wd4646)
2896 file(GLOB PROJECT_SOURCE_LIST_H
2902 # Try to have the compiler default to hiding symbols, so that only
2903 # symbols explicitly exported with PCAP_API will be visible outside
2904 # (shared) libraries.
2906 # Not necessary with MSVC, as that's the default.
2908 # XXX - we don't use ADD_COMPILER_EXPORT_FLAGS, because, as of CMake
2909 # 2.8.12.2, it doesn't know about Sun C/Oracle Studio, and, as of
2910 # CMake 2.8.6, it only sets the C++ compiler flags, rather than
2911 # allowing an arbitrary variable to be set with the "hide symbols
2912 # not explicitly exported" flag.
2915 if(CMAKE_C_COMPILER_ID MATCHES "SunPro")
2917 # Sun C/Oracle Studio.
2919 check_and_add_compiler_option(-xldscope=hidden)
2922 # Try this for all other compilers; it's what GCC uses,
2923 # and a number of other compilers, such as Clang and Intel C,
2926 check_and_add_compiler_option(-fvisibility=hidden)
2931 # Extra compiler options for the build matrix scripts to request -Werror or
2932 # its equivalent if required. The CMake variable name cannot be CFLAGS
2933 # because that is already used for a different purpose in CMake. Example
2934 # usage: cmake -DEXTRA_CFLAGS='-Wall -Wextra -Werror' ...
2936 if(NOT "${EXTRA_CFLAGS}" STREQUAL "")
2937 # The meaning of EXTRA_CFLAGS is "use the exact specified options, or the
2938 # build risks failing to fail", not "try every specified option, omit those
2939 # that do not work and use the rest". Thus use add_compile_options(), not
2940 # foreach()/check_and_add_compiler_option(). Another reason to do that is
2941 # that the effect lasts in testprogs/ and testprogs/fuzz/.
2942 string(REPLACE " " ";" _extra_cflags_list ${EXTRA_CFLAGS})
2943 add_compile_options(${_extra_cflags_list})
2944 message(STATUS "Added extra compile options (${EXTRA_CFLAGS})")
2948 # Flex/Lex and YACC/Berkeley YACC/Bison.
2949 # From a mail message to the CMake mailing list by Andy Cedilnik of
2954 # Try to find Flex, a Windows version of Flex, or Lex.
2956 find_program(LEX_EXECUTABLE NAMES flex win_flex lex)
2957 if(LEX_EXECUTABLE STREQUAL "LEX_EXECUTABLE-NOTFOUND")
2958 message(FATAL_ERROR "Neither flex nor win_flex nor lex was found.")
2960 message(STATUS "Lexical analyzer generator: ${LEX_EXECUTABLE}")
2963 # Make sure {f}lex supports the -P, --header-file, and --nounput flags
2964 # and supports processing our scanner.l.
2967 set(NULL_DEVICE "NUL:")
2969 set(NULL_DEVICE "/dev/null")
2971 execute_process(COMMAND ${LEX_EXECUTABLE} -P pcap_ --header-file=${NULL_DEVICE} --nounput -t ${pcap_SOURCE_DIR}/scanner.l
2972 OUTPUT_QUIET RESULT_VARIABLE EXIT_STATUS)
2973 if(NOT EXIT_STATUS EQUAL 0)
2974 message(FATAL_ERROR "${LEX_EXECUTABLE} is insufficient to compile libpcap.
2975 libpcap requires Flex 2.5.31 or later, or a compatible version of lex.
2976 If a suitable version of Lex/Flex is available as a non-standard command
2977 and/or not in the PATH, you can specify it using the LEX environment
2978 variable. That said, on some systems the error can mean that Flex/Lex is
2979 actually acceptable, but m4 is not. Likewise, if a suitable version of
2980 m4 (such as GNU M4) is available but has not been detected, you can
2981 specify it using the M4 environment variable.")
2985 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/scanner.c ${CMAKE_CURRENT_BINARY_DIR}/scanner.h
2986 COMMAND ${LEX_EXECUTABLE} -P pcap_ --header-file=scanner.h --nounput -o${CMAKE_CURRENT_BINARY_DIR}/scanner.c ${pcap_SOURCE_DIR}/scanner.l
2987 DEPENDS ${pcap_SOURCE_DIR}/scanner.l
2991 # Since scanner.c does not exist yet when cmake is run, mark
2994 # Since scanner.c includes grammar.h, mark that as a dependency.
2996 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/scanner.c PROPERTIES
2998 OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/grammar.h
3002 # Add scanner.c to the list of sources.
3004 #set(PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C} ${CMAKE_CURRENT_BINARY_DIR}/scanner.c)
3007 # Try to find YACC or Bison.
3009 find_program(YACC_EXECUTABLE NAMES bison win_bison byacc yacc)
3010 if(YACC_EXECUTABLE STREQUAL "YACC_EXECUTABLE-NOTFOUND")
3011 message(FATAL_ERROR "Neither bison nor win_bison nor byacc nor yacc was found.")
3014 if(YACC_EXECUTABLE MATCHES "byacc" OR YACC_EXECUTABLE MATCHES "yacc")
3016 # Make sure this is Berkeley YACC, not AT&T YACC;
3017 # the latter doesn't support reentrant parsers.
3018 # Run it with "-V"; that succeeds and reports the
3019 # version number with Berkeley YACC, but will
3020 # (probably) fail with various vendor flavors
3023 # Hopefully this also eliminates any versions
3024 # of Berkeley YACC that don't support reentrant
3025 # parsers, if there are any.
3027 execute_process(COMMAND ${YACC_EXECUTABLE} -V OUTPUT_QUIET
3028 RESULT_VARIABLE EXIT_STATUS)
3029 if(NOT EXIT_STATUS EQUAL 0)
3030 message(FATAL_ERROR "${YACC_EXECUTABLE} is insufficient to compile libpcap.
3031 libpcap requires Bison, a newer version of Berkeley YACC with support
3032 for reentrant parsers, or another YACC compatible with them.")
3035 # Berkeley YACC doesn't support "%define api.pure", so use
3038 set(REENTRANT_PARSER "%pure-parser")
3041 # Bison prior to 2.4(.1) doesn't support "%define api.pure", so use
3044 execute_process(COMMAND ${YACC_EXECUTABLE} -V OUTPUT_VARIABLE bison_full_version)
3045 string(REGEX MATCH "[1-9][0-9]*[.][0-9]+" bison_major_minor ${bison_full_version})
3046 if (bison_major_minor VERSION_LESS "2.4")
3047 set(REENTRANT_PARSER "%pure-parser")
3049 set(REENTRANT_PARSER "%define api.pure")
3053 message(STATUS "Parser generator: ${YACC_EXECUTABLE}")
3056 # Create custom command for the scanner.
3059 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/grammar.c ${CMAKE_CURRENT_BINARY_DIR}/grammar.h
3060 COMMAND ${YACC_EXECUTABLE} -p pcap_ -o ${CMAKE_CURRENT_BINARY_DIR}/grammar.c -d ${pcap_BINARY_DIR}/grammar.y
3061 DEPENDS ${pcap_BINARY_DIR}/grammar.y
3065 # Since grammar.c does not exists yet when cmake is run, mark
3068 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/grammar.c PROPERTIES
3070 OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/scanner.h
3074 # Add grammar.c to the list of sources.
3076 #set(PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C} ${CMAKE_CURRENT_BINARY_DIR}/grammar.c)
3079 # Assume, by default, no support for shared libraries and V7/BSD
3080 # convention for man pages (devices in section 4, file formats in
3081 # section 5, miscellaneous info in section 7, administrative commands
3082 # and daemons in section 8). Individual cases can override this.
3083 # Individual cases can override this.
3086 set(MAN_FILE_FORMATS 5)
3087 set(MAN_MISC_INFO 7)
3088 set(MAN_ADMIN_COMMANDS 8)
3089 if(CMAKE_SYSTEM_NAME STREQUAL "AIX")
3090 if(PCAP_TYPE STREQUAL "bpf")
3092 # If we're using BPF, we need libodm and libcfg, as
3093 # we use them to load the BPF module.
3095 set(PCAP_LINK_LIBRARIES ${PCAP_LINK_LIBRARIES} odm cfg)
3096 set(LIBS "${LIBS} -lodm -lcfg")
3097 set(LIBS_STATIC "${LIBS_STATIC} -lodm -lcfg")
3098 set(LIBS_PRIVATE "${LIBS_PRIVATE} -lodm -lcfg")
3100 elseif(CMAKE_SYSTEM_NAME STREQUAL "HP-UX")
3102 CMAKE_SYSTEM_VERSION MATCHES "[A-Z.]*9\.[0-9]*"
3103 OR CMAKE_SYSTEM_VERSION MATCHES "[A-Z.]*10\.[01]"
3110 message(FATAL_ERROR \"HP-UX version must be 10.20 or later, not \${CMAKE_SYSTEM_VERSION}\")
3113 # HP-UX 10.20 and later.
3115 set(HAVE_HPUX10_20_OR_LATER TRUE)
3119 # Use System V conventions for man pages.
3121 set(MAN_ADMIN_COMMANDS 1m)
3122 set(MAN_FILE_FORMATS 4)
3123 set(MAN_MISC_INFO 5)
3124 elseif(CMAKE_SYSTEM_NAME STREQUAL "SunOS" AND CMAKE_SYSTEM_VERSION MATCHES "5[.][0-9.]*")
3128 set(HAVE_SOLARIS TRUE)
3130 # Make sure errno is thread-safe, in case we're called in
3131 # a multithreaded program. We don't guarantee that two
3132 # threads can use the *same* pcap_t safely, but the
3133 # current version does guarantee that you can use different
3134 # pcap_t's in different threads, and even that pcap_compile()
3135 # is thread-safe (it wasn't thread-safe in some older versions).
3137 add_definitions(-D_TS_ERRNO)
3139 if(CMAKE_SYSTEM_VERSION STREQUAL "5.12")
3142 # Use System V conventions for man pages.
3144 set(MAN_ADMIN_COMMANDS 1m)
3145 set(MAN_FILE_FORMATS 4)
3146 set(MAN_MISC_INFO 5)
3149 elseif(CMAKE_SYSTEM_NAME STREQUAL "Haiku")
3151 # Haiku needs _BSD_SOURCE for the _IO* macros because it doesn't use them.
3153 add_definitions(-D_BSD_SOURCE)
3156 source_group("Source Files" FILES ${PROJECT_SOURCE_LIST_C})
3157 source_group("Header Files" FILES ${PROJECT_SOURCE_LIST_H})
3161 # Add pcap-dll.rc to the list of sources.
3163 set(PROJECT_SOURCE_LIST_C ${PROJECT_SOURCE_LIST_C} ${pcap_SOURCE_DIR}/pcap-dll.rc)
3167 # Add subdirectories after we've set various variables, so they pick up
3168 # pick up those variables.
3171 add_subdirectory(rpcapd)
3172 endif(ENABLE_REMOTE)
3173 add_subdirectory(testprogs)
3175 ######################################
3177 ######################################
3180 # Special target to serialize the building of the generated source.
3184 # https://public.kitware.com/pipermail/cmake/2013-August/055510.html
3186 add_custom_target(SerializeTarget
3188 ${CMAKE_CURRENT_BINARY_DIR}/grammar.c
3189 ${CMAKE_CURRENT_BINARY_DIR}/scanner.c
3192 set_source_files_properties(${PROJECT_EXTERNAL_OBJECT_LIST} PROPERTIES
3193 EXTERNAL_OBJECT TRUE)
3195 if(BUILD_SHARED_LIBS)
3196 add_library(${LIBRARY_NAME} SHARED
3197 ${PROJECT_SOURCE_LIST_C}
3198 ${CMAKE_CURRENT_BINARY_DIR}/grammar.c
3199 ${CMAKE_CURRENT_BINARY_DIR}/scanner.c
3200 ${PROJECT_EXTERNAL_OBJECT_LIST}
3202 target_include_directories(${LIBRARY_NAME} PUBLIC
3203 $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
3204 $<INSTALL_INTERFACE:include>
3206 add_dependencies(${LIBRARY_NAME} SerializeTarget)
3207 set_target_properties(${LIBRARY_NAME} PROPERTIES
3208 COMPILE_DEFINITIONS BUILDING_PCAP)
3210 # No matter what the library is called - it might be called "wpcap"
3211 # in a Windows build - the symbol to define to indicate that we're
3212 # building the library, rather than a program using the library,
3213 # and thus that we're exporting functions defined in our public
3214 # header files, rather than importing those functions, is
3217 set_target_properties(${LIBRARY_NAME} PROPERTIES
3218 DEFINE_SYMBOL pcap_EXPORTS)
3219 if(NOT "${LINKER_FLAGS}" STREQUAL "")
3220 set_target_properties(${LIBRARY_NAME} PROPERTIES
3221 LINK_FLAGS "${LINKER_FLAGS}")
3223 endif(BUILD_SHARED_LIBS)
3225 add_library(${LIBRARY_NAME}_static STATIC
3226 ${PROJECT_SOURCE_LIST_C}
3227 ${CMAKE_CURRENT_BINARY_DIR}/grammar.c
3228 ${CMAKE_CURRENT_BINARY_DIR}/scanner.c
3229 ${PROJECT_EXTERNAL_OBJECT_LIST}
3231 target_include_directories(${LIBRARY_NAME}_static PUBLIC
3232 $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
3233 $<INSTALL_INTERFACE:include>
3235 add_dependencies(${LIBRARY_NAME}_static SerializeTarget)
3236 set_target_properties(${LIBRARY_NAME}_static PROPERTIES
3237 COMPILE_DEFINITIONS BUILDING_PCAP)
3240 if(BUILD_SHARED_LIBS)
3241 set_target_properties(${LIBRARY_NAME} PROPERTIES
3242 VERSION ${PACKAGE_VERSION_NOSUFFIX} # only MAJOR and MINOR are needed
3244 endif(BUILD_SHARED_LIBS)
3246 # XXX For DLLs, the TARGET_PDB_FILE generator expression can be used to locate
3247 # its PDB file's output directory for installation.
3248 # cmake doesn't offer a generator expression for PDB files generated by the
3249 # compiler (static libraries).
3250 # So instead of considering any possible output there is (there are many),
3251 # this will search for the PDB file in the compiler's initial output directory,
3252 # which is always ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles\wpcap_static.dir
3253 # regardless of architecture, build generator etc.
3254 # Quite hackish indeed.
3255 set(CMAKE_COMPILE_PDB_OUTPUT_DIRECTORY $<TARGET_FILE_DIR:${LIBRARY_NAME}_static>)
3256 set_target_properties(${LIBRARY_NAME}_static PROPERTIES
3257 COMPILE_PDB_NAME ${LIBRARY_NAME}_static
3258 OUTPUT_NAME "${LIBRARY_NAME}_static"
3262 # For compatibility, build the shared library without the "lib" prefix on
3265 set_target_properties(${LIBRARY_NAME} PROPERTIES
3267 OUTPUT_NAME "${LIBRARY_NAME}"
3269 set_target_properties(${LIBRARY_NAME}_static PROPERTIES
3270 OUTPUT_NAME "${LIBRARY_NAME}"
3274 if(BUILD_SHARED_LIBS)
3276 set_target_properties(${LIBRARY_NAME} PROPERTIES
3277 VERSION ${PACKAGE_VERSION}
3281 set_target_properties(${LIBRARY_NAME} PROPERTIES
3282 VERSION ${PACKAGE_VERSION}
3283 SOVERSION ${PACKAGE_VERSION_MAJOR}
3286 endif(BUILD_SHARED_LIBS)
3287 set_target_properties(${LIBRARY_NAME}_static PROPERTIES
3288 OUTPUT_NAME "${LIBRARY_NAME}"
3292 if(BUILD_SHARED_LIBS)
3293 if(NOT C_ADDITIONAL_FLAGS STREQUAL "")
3294 set_target_properties(${LIBRARY_NAME} PROPERTIES COMPILE_FLAGS ${C_ADDITIONAL_FLAGS})
3298 # If this is macOS and we've picked the default architectures on
3299 # which to build, build the library on them.
3301 if(APPLE AND "${CMAKE_OSX_ARCHITECTURES}" STREQUAL "")
3302 set_target_properties(${LIBRARY_NAME} PROPERTIES
3303 OSX_ARCHITECTURES "${OSX_LIBRARY_ARCHITECTURES}")
3305 target_link_libraries(${LIBRARY_NAME} ${PCAP_LINK_LIBRARIES})
3306 endif(BUILD_SHARED_LIBS)
3308 if(NOT C_ADDITIONAL_FLAGS STREQUAL "")
3309 set_target_properties(${LIBRARY_NAME}_static PROPERTIES COMPILE_FLAGS ${C_ADDITIONAL_FLAGS})
3313 # If this is macOS and we've picked the default architectures on
3314 # which to build, build the library on them.
3316 if(APPLE AND "${CMAKE_OSX_ARCHITECTURES}" STREQUAL "")
3317 set_target_properties(${LIBRARY_NAME}_static PROPERTIES
3318 OSX_ARCHITECTURES "${OSX_LIBRARY_ARCHITECTURES}")
3321 ######################################
3322 # Write out the config.h file
3323 ######################################
3325 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmakeconfig.h.in ${CMAKE_CURRENT_BINARY_DIR}/config.h)
3327 ######################################
3328 # Write out the grammar.y file
3329 ######################################
3331 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/grammar.y.in ${CMAKE_CURRENT_BINARY_DIR}/grammar.y @ONLY)
3333 ######################################
3334 # Install pcap library, include files, and man pages
3335 ######################################
3338 # "Define GNU standard installation directories", which actually
3339 # are also defined, to some degree, by autotools, and at least
3340 # some of which are general UN*X conventions.
3342 include(GNUInstallDirs)
3344 set(LIBRARY_NAME_STATIC ${LIBRARY_NAME}_static)
3346 function(install_manpage_symlink SOURCE TARGET MANDIR)
3349 # If we haven't found an ln executable with MinGW, we don't try
3350 # generating and installing the man pages, so if we get here,
3351 # we've found that executable.
3352 set(LINK_COMMAND "\"${LINK_EXECUTABLE}\" \"-s\" \"${SOURCE}\" \"${TARGET}\"")
3354 set(LINK_COMMAND "\"${CMAKE_COMMAND}\" \"-E\" \"create_symlink\" \"${SOURCE}\" \"${TARGET}\"")
3358 if(NOT ${CMAKE_INSTALL_MESSAGE} STREQUAL \"NEVER\")
3359 message(STATUS \"Symlinking: \$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/${MANDIR}/${SOURCE} to ${TARGET}\")
3362 COMMAND \"${CMAKE_COMMAND}\" \"-E\" \"remove\" \"${TARGET}\"
3363 WORKING_DIRECTORY \$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/${MANDIR}
3366 COMMAND ${LINK_COMMAND}
3367 WORKING_DIRECTORY \$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/${MANDIR}
3368 RESULT_VARIABLE EXIT_STATUS
3370 if(NOT EXIT_STATUS EQUAL 0)
3371 message(FATAL_ERROR \"Could not create symbolic link from \${CMAKE_INSTALL_PREFIX}/${MANDIR}/${SOURCE} to ${TARGET}\")
3373 set(CMAKE_INSTALL_MANIFEST_FILES \${CMAKE_INSTALL_MANIFEST_FILES} \${CMAKE_INSTALL_PREFIX}/${MANDIR}/${TARGET})")
3374 endfunction(install_manpage_symlink)
3376 set(MAN1_NOEXPAND pcap-config.1)
3379 pcap_compile.3pcap.in
3380 pcap_datalink.3pcap.in
3381 pcap_dump_open.3pcap.in
3382 pcap_get_tstamp_precision.3pcap.in
3383 pcap_list_datalinks.3pcap.in
3384 pcap_list_tstamp_types.3pcap.in
3385 pcap_open_dead.3pcap.in
3386 pcap_open_offline.3pcap.in
3387 pcap_set_immediate_mode.3pcap.in
3388 pcap_set_tstamp_precision.3pcap.in
3389 pcap_set_tstamp_type.3pcap.in
3391 set(MAN3PCAP_NOEXPAND
3393 pcap_breakloop.3pcap
3394 pcap_can_set_rfmon.3pcap
3397 pcap_datalink_name_to_val.3pcap
3398 pcap_datalink_val_to_name.3pcap
3400 pcap_dump_close.3pcap
3401 pcap_dump_file.3pcap
3402 pcap_dump_flush.3pcap
3403 pcap_dump_ftell.3pcap
3406 pcap_findalldevs.3pcap
3408 pcap_get_required_select_timeout.3pcap
3409 pcap_get_selectable_fd.3pcap
3413 pcap_is_swapped.3pcap
3414 pcap_lib_version.3pcap
3415 pcap_lookupdev.3pcap
3416 pcap_lookupnet.3pcap
3418 pcap_major_version.3pcap
3420 pcap_offline_filter.3pcap
3421 pcap_open_live.3pcap
3422 pcap_set_buffer_size.3pcap
3423 pcap_set_datalink.3pcap
3424 pcap_set_promisc.3pcap
3425 pcap_set_protocol_linux.3pcap
3426 pcap_set_rfmon.3pcap
3427 pcap_set_snaplen.3pcap
3428 pcap_set_timeout.3pcap
3429 pcap_setdirection.3pcap
3430 pcap_setfilter.3pcap
3431 pcap_setnonblock.3pcap
3434 pcap_statustostr.3pcap
3436 pcap_tstamp_type_name_to_val.3pcap
3437 pcap_tstamp_type_val_to_name.3pcap
3440 cbpf-savefile.manfile.in
3441 pcap-savefile.manfile.in
3444 pcap-filter.manmisc.in
3445 pcap-linktype.manmisc.in
3446 pcap-tstamp.manmisc.in
3449 if(BUILD_SHARED_LIBS)
3450 set(LIBRARIES_TO_INSTALL "${LIBRARY_NAME}" "${LIBRARY_NAME_STATIC}")
3451 else(BUILD_SHARED_LIBS)
3452 set(LIBRARIES_TO_INSTALL "${LIBRARY_NAME_STATIC}")
3453 endif(BUILD_SHARED_LIBS)
3455 if(WIN32 OR CYGWIN OR MSYS)
3457 # XXX - according to the CMake documentation, WIN32 is set if
3458 # the target is Windows; would there ever be a case where
3459 # CYGWIN or MSYS are set but WIN32 *isn't* set?
3461 if(MSVC AND CMAKE_SIZEOF_VOID_P EQUAL 8)
3463 # Install 64-bit code built with MSVC in the x64 subdirectories,
3464 # as that's where it expects it to be.
3466 install(TARGETS ${LIBRARIES_TO_INSTALL}
3467 RUNTIME DESTINATION bin/x64
3468 LIBRARY DESTINATION lib/x64
3469 ARCHIVE DESTINATION lib/x64)
3471 install(FILES $<TARGET_FILE_DIR:${LIBRARY_NAME_STATIC}>/${LIBRARY_NAME_STATIC}.pdb
3472 DESTINATION bin/x64 OPTIONAL)
3473 if(BUILD_SHARED_LIBS)
3474 install(FILES $<TARGET_PDB_FILE:${LIBRARY_NAME}>
3475 DESTINATION bin/x64 OPTIONAL)
3476 endif(BUILD_SHARED_LIBS)
3478 else(MSVC AND CMAKE_SIZEOF_VOID_P EQUAL 8)
3480 # Install 32-bit code, and 64-bit code not built with MSVC
3481 # in the top-level directories, as those are where they
3484 install(TARGETS ${LIBRARIES_TO_INSTALL}
3485 RUNTIME DESTINATION bin
3486 LIBRARY DESTINATION lib
3487 ARCHIVE DESTINATION lib)
3489 install(FILES $<TARGET_FILE_DIR:${LIBRARY_NAME_STATIC}>/${LIBRARY_NAME_STATIC}.pdb
3490 DESTINATION bin OPTIONAL)
3491 if(BUILD_SHARED_LIBS)
3492 install(FILES $<TARGET_PDB_FILE:${LIBRARY_NAME}>
3493 DESTINATION bin OPTIONAL)
3494 endif(BUILD_SHARED_LIBS)
3496 endif(MSVC AND CMAKE_SIZEOF_VOID_P EQUAL 8)
3497 else(WIN32 OR CYGWIN OR MSYS)
3498 install(TARGETS ${LIBRARIES_TO_INSTALL} DESTINATION ${CMAKE_INSTALL_LIBDIR})
3499 endif(WIN32 OR CYGWIN OR MSYS)
3501 install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/pcap/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/pcap)
3502 install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/pcap.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
3503 install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/pcap-bpf.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
3504 install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/pcap-namedb.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
3506 # On UN*X, and on Windows when not using MSVC, generate libpcap.pc and
3507 # pcap-config and process man pages and arrange that they be installed.
3509 set(prefix ${CMAKE_INSTALL_PREFIX})
3510 set(exec_prefix "\${prefix}")
3511 set(includedir "\${prefix}/include")
3512 set(libdir "\${exec_prefix}/${CMAKE_INSTALL_LIBDIR}")
3515 # If this is a platform where we need to have the .pc file and
3516 # pcap-config script supply an rpath option to specify the directory
3517 # in which the libpcap shared library is installed, and the install
3518 # prefix /usr (meaning we're not installing a system library),
3519 # provide the rpath option.
3521 # (We must check CMAKE_INSTALL_PREFIX, as the library directory
3522 # isn't necessarily /usr/lib in this case - for example, Linux
3523 # distributions for 64-bit platforms that also provide support for
3524 # binaries for a 32-bit version of the platform may put the 64-bit
3525 # libraries, the 32-bit libraries, or both in directories other than
3528 # In AIX, do we have to do this?
3530 # In Darwin-based OSes, the full paths of the shared libraries with
3531 # which the program was linked are stored in the executable, so we
3532 # don't need to provide an rpath option.
3534 # With the HP-UX linker, directories specified with -L are, by
3535 # default, added to the run-time search path, so we don't need to
3538 # This must *not* depend on the compiler, as, on platforms where
3539 # there's a GCC-compatible compiler and a vendor compiler, we need
3540 # to work with both.
3542 if(NOT CMAKE_INSTALL_PREFIX STREQUAL "/usr")
3543 if(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD" OR
3544 CMAKE_SYSTEM_NAME STREQUAL "NetBSD" OR
3545 CMAKE_SYSTEM_NAME STREQUAL "OpenBSD" OR
3546 CMAKE_SYSTEM_NAME STREQUAL "DragonFly BSD" OR
3547 CMAKE_SYSTEM_NAME STREQUAL "Linux")
3549 # Platforms where the "native" C compiler is GCC or accepts
3550 # compatible command-line arguments, and the "native" linker
3551 # is the GNU linker or accepts compatible command-line
3554 set(RPATH "-Wl,-rpath,\${libdir}")
3555 elseif(CMAKE_SYSTEM_NAME STREQUAL "SunOS" AND CMAKE_SYSTEM_VERSION MATCHES "5[.][0-9.]*")
3559 # Sun/Oracle's linker, the GNU linker, and GNU-compatible
3560 # linkers all support -R.
3562 set(RPATH "-Wl,-R,\${libdir}")
3565 # No option needed to set the RPATH.
3570 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/pcap-config.in ${CMAKE_CURRENT_BINARY_DIR}/pcap-config @ONLY)
3571 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/libpcap.pc.in ${CMAKE_CURRENT_BINARY_DIR}/libpcap.pc @ONLY)
3572 install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/pcap-config DESTINATION bin)
3573 install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libpcap.pc DESTINATION lib/pkgconfig)
3578 # For each section of the manual for which we have man pages
3579 # that require macro expansion, do the expansion.
3581 # If this is MinGW, maybe we have a UN*X-style ln command and
3582 # maybe we don't. (No, we do *NOT* require MSYS!) If we don't
3583 # have it, don't do the man pages.
3586 find_program(LINK_EXECUTABLE ln)
3588 if(UNIX OR (MINGW AND LINK_EXECUTABLE))
3590 foreach(MANPAGE ${MAN1_NOEXPAND})
3591 set(MAN1 ${MAN1} ${CMAKE_CURRENT_SOURCE_DIR}/${MANPAGE})
3593 install(FILES ${MAN1} DESTINATION ${CMAKE_INSTALL_MANDIR}/man1)
3596 foreach(MANPAGE ${MAN3PCAP_NOEXPAND})
3597 set(MAN3PCAP ${MAN3PCAP} ${CMAKE_CURRENT_SOURCE_DIR}/${MANPAGE})
3599 foreach(TEMPLATE_MANPAGE ${MAN3PCAP_EXPAND})
3600 string(REPLACE ".in" "" MANPAGE ${TEMPLATE_MANPAGE})
3601 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/${TEMPLATE_MANPAGE} ${CMAKE_CURRENT_BINARY_DIR}/${MANPAGE} @ONLY)
3602 set(MAN3PCAP ${MAN3PCAP} ${CMAKE_CURRENT_BINARY_DIR}/${MANPAGE})
3603 endforeach(TEMPLATE_MANPAGE)
3604 install(FILES ${MAN3PCAP} DESTINATION ${CMAKE_INSTALL_MANDIR}/man3)
3605 install_manpage_symlink(pcap_datalink_val_to_name.3pcap pcap_datalink_val_to_description.3pcap ${CMAKE_INSTALL_MANDIR}/man3)
3606 install_manpage_symlink(pcap_datalink_val_to_name.3pcap pcap_datalink_val_to_description_or_dlt.3pcap ${CMAKE_INSTALL_MANDIR}/man3)
3607 install_manpage_symlink(pcap_dump_open.3pcap pcap_dump_fopen.3pcap ${CMAKE_INSTALL_MANDIR}/man3)
3608 install_manpage_symlink(pcap_findalldevs.3pcap pcap_freealldevs.3pcap ${CMAKE_INSTALL_MANDIR}/man3)
3609 install_manpage_symlink(pcap_geterr.3pcap pcap_perror.3pcap ${CMAKE_INSTALL_MANDIR}/man3)
3610 install_manpage_symlink(pcap_inject.3pcap pcap_sendpacket.3pcap ${CMAKE_INSTALL_MANDIR}/man3)
3611 install_manpage_symlink(pcap_list_datalinks.3pcap pcap_free_datalinks.3pcap ${CMAKE_INSTALL_MANDIR}/man3)
3612 install_manpage_symlink(pcap_list_tstamp_types.3pcap pcap_free_tstamp_types.3pcap ${CMAKE_INSTALL_MANDIR}/man3)
3613 install_manpage_symlink(pcap_loop.3pcap pcap_dispatch.3pcap ${CMAKE_INSTALL_MANDIR}/man3)
3614 install_manpage_symlink(pcap_major_version.3pcap pcap_minor_version.3pcap ${CMAKE_INSTALL_MANDIR}/man3)
3615 install_manpage_symlink(pcap_next_ex.3pcap pcap_next.3pcap ${CMAKE_INSTALL_MANDIR}/man3)
3616 install_manpage_symlink(pcap_open_dead.3pcap pcap_open_dead_with_tstamp_precision.3pcap ${CMAKE_INSTALL_MANDIR}/man3)
3617 install_manpage_symlink(pcap_open_offline.3pcap pcap_open_offline_with_tstamp_precision.3pcap ${CMAKE_INSTALL_MANDIR}/man3)
3618 install_manpage_symlink(pcap_open_offline.3pcap pcap_fopen_offline.3pcap ${CMAKE_INSTALL_MANDIR}/man3)
3619 install_manpage_symlink(pcap_open_offline.3pcap pcap_fopen_offline_with_tstamp_precision.3pcap ${CMAKE_INSTALL_MANDIR}/man3)
3620 install_manpage_symlink(pcap_tstamp_type_val_to_name.3pcap pcap_tstamp_type_val_to_description.3pcap ${CMAKE_INSTALL_MANDIR}/man3)
3621 install_manpage_symlink(pcap_setnonblock.3pcap pcap_getnonblock.3pcap ${CMAKE_INSTALL_MANDIR}/man3)
3624 foreach(TEMPLATE_MANPAGE ${MANFILE_EXPAND})
3625 string(REPLACE ".manfile.in" ".${MAN_FILE_FORMATS}" MANPAGE ${TEMPLATE_MANPAGE})
3626 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/${TEMPLATE_MANPAGE} ${CMAKE_CURRENT_BINARY_DIR}/${MANPAGE} @ONLY)
3627 set(MANFILE ${MANFILE} ${CMAKE_CURRENT_BINARY_DIR}/${MANPAGE})
3628 endforeach(TEMPLATE_MANPAGE)
3629 install(FILES ${MANFILE} DESTINATION ${CMAKE_INSTALL_MANDIR}/man${MAN_FILE_FORMATS})
3632 foreach(TEMPLATE_MANPAGE ${MANMISC_EXPAND})
3633 string(REPLACE ".manmisc.in" ".${MAN_MISC_INFO}" MANPAGE ${TEMPLATE_MANPAGE})
3634 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/${TEMPLATE_MANPAGE} ${CMAKE_CURRENT_BINARY_DIR}/${MANPAGE} @ONLY)
3635 set(MANMISC ${MANMISC} ${CMAKE_CURRENT_BINARY_DIR}/${MANPAGE})
3636 endforeach(TEMPLATE_MANPAGE)
3637 install(FILES ${MANMISC} DESTINATION ${CMAKE_INSTALL_MANDIR}/man${MAN_MISC_INFO})
3638 endif(UNIX OR (MINGW AND LINK_EXECUTABLE))
3643 "${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in"
3644 "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"
3647 add_custom_target(uninstall
3648 COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)
3652 # We try to find the Perl interpreter and, if we do, we have the check
3653 # rule run testprogs/TESTrun with it, because just trying to run the TESTrun
3654 # script as a command won't work on Windows.
3656 # However, if the PERL environment variable is set, use its value instead of
3657 # trying to find the interpreter, this implements the same behaviour as in the
3660 if(NOT "$ENV{PERL}" STREQUAL "")
3661 set(PERL $ENV{PERL})
3662 message(STATUS "Using PERL='${PERL}'")
3664 find_program(PERL perl)
3666 message(STATUS "Found perl at ${PERL}")
3668 message(STATUS "Didn't find perl")
3673 add_custom_target(check
3674 COMMAND ${PERL} ${CMAKE_SOURCE_DIR}/testprogs/TESTrun