+ pkg_search_module(CONFIG_PCAP ${_quiet} libpcap)
+ set(ENV{PKG_CONFIG_PATH} "${_saved_pkg_config_path}")
+
+ if(NOT CONFIG_PCAP_FOUND)
+ #
+ # That didn't work. Try pcap-config.
+ #
+ find_program(PCAP_CONFIG pcap-config)
+ if(PCAP_CONFIG)
+ #
+ # We have pcap-config; use it.
+ #
+ if(NOT "${_quiet}" STREQUAL "QUIET")
+ message(STATUS "Found pcap-config")
+ endif()
+
+ #
+ # If this is a vendor-supplied pcap-config, which we define as
+ # being "a pcap-config in /usr/bin or /usr/ccs/bin" (the latter
+ # is for Solaris and Sun/Oracle Studio), there are some issues.
+ # Work around them.
+ #
+ if("${PCAP_CONFIG}" STREQUAL /usr/bin/pcap-config OR
+ "${PCAP_CONFIG}" STREQUAL /usr/ccs/bin/pcap-config)
+ #
+ # It's vendor-supplied.
+ #
+ if(APPLE)
+ #
+ # This is macOS or another Darwin-based OS.
+ #
+ # That means that /usr/bin/pcap-config it may provide
+ # -I/usr/local/include with --cflags and -L/usr/local/lib
+ # with --libs; if there's no pcap installed under /usr/local,
+ # that will cause the build to fail, and if there is a pcap
+ # installed there, you'll get that pcap even if you don't
+ # want it. Remember that, so we ignore those values.
+ #
+ set(_broken_apple_pcap_config TRUE)
+ elseif(CMAKE_SYSTEM_NAME STREQUAL "SunOS" AND CMAKE_SYSTEM_VERSION MATCHES "5[.][0-9.]*")
+ #
+ # This is Solaris 2 or later, i.e. SunOS 5.x.
+ #
+ # At least on Solaris 11; there's /usr/bin/pcap-config, which
+ # reports -L/usr/lib with --libs, causing the 32-bit libraries
+ # to be found, and there's /usr/bin/{64bitarch}/pcap-config,
+ # where {64bitarch} is a name for the 64-bit version of the
+ # instruction set, which reports -L /usr/lib/{64bitarch},
+ # causing the 64-bit libraries to be found.
+ #
+ # So if we're building 64-bit targets, we replace PCAP_CONFIG
+ # with /usr/bin/{64bitarch}; we get {64bitarch} as the
+ # output of "isainfo -n".
+ #
+ if(CMAKE_SIZEOF_VOID_P EQUAL 8)
+ execute_process(COMMAND "isainfo" "-n"
+ RESULT_VARIABLE ISAINFO_RESULT
+ OUTPUT_VARIABLE ISAINFO_OUTPUT
+ OUTPUT_STRIP_TRAILING_WHITESPACE
+ )
+ if(ISAINFO_RESULT EQUAL 0)
+ #
+ # Success - change PCAP_CONFIG.
+ #
+ string(REPLACE "/bin/" "/bin/${ISAINFO_OUTPUT}/" PCAP_CONFIG "${PCAP_CONFIG}")
+ endif()
+ endif()
+ endif()
+ endif()
+
+ #
+ # Now get the include directories.
+ #
+ execute_process(COMMAND "${PCAP_CONFIG}" "--cflags"
+ RESULT_VARIABLE PCAP_CONFIG_RESULT
+ OUTPUT_VARIABLE PCAP_CONFIG_OUTPUT
+ OUTPUT_STRIP_TRAILING_WHITESPACE
+ )
+ if(NOT PCAP_CONFIG_RESULT EQUAL 0)
+ message(FATAL_ERROR "pcap-config --cflags failed")
+ endif()
+ separate_arguments(CFLAGS_LIST UNIX_COMMAND ${PCAP_CONFIG_OUTPUT})
+ set(CONFIG_PCAP_INCLUDE_DIRS "")
+ foreach(_arg IN LISTS CFLAGS_LIST)
+ if(_arg MATCHES "^-I")
+ #
+ # Extract the directory by removing the -I.
+ #
+ string(REGEX REPLACE "-I" "" _dir ${_arg})
+ #
+ # Work around macOS (and probably other Darwin) brokenness,
+ # by not adding /usr/local/include if it's from the broken
+ # Apple pcap-config.
+ #
+ if(NOT _broken_apple_pcap_config OR
+ NOT "${_dir}" STREQUAL /usr/local/include)
+ # Add it to CONFIG_PCAP_INCLUDE_DIRS
+ list(APPEND CONFIG_PCAP_INCLUDE_DIRS ${_dir})
+ endif()
+ endif()
+ endforeach()
+
+ #
+ # Now, get the library directories and libraries for dynamic linking.
+ #
+ execute_process(COMMAND "${PCAP_CONFIG}" "--libs"
+ RESULT_VARIABLE PCAP_CONFIG_RESULT
+ OUTPUT_VARIABLE PCAP_CONFIG_OUTPUT
+ OUTPUT_STRIP_TRAILING_WHITESPACE
+ )
+ if(NOT PCAP_CONFIG_RESULT EQUAL 0)
+ message(FATAL_ERROR "pcap-config --libs failed")
+ endif()
+ separate_arguments(LIBS_LIST UNIX_COMMAND ${PCAP_CONFIG_OUTPUT})
+ set(CONFIG_PCAP_LIBRARY_DIRS "")
+ set(CONFIG_PCAP_LIBRARIES "")
+ foreach(_arg IN LISTS LIBS_LIST)
+ if(_arg MATCHES "^-L")
+ #
+ # Extract the directory by removing the -L.
+ #
+ string(REGEX REPLACE "-L" "" _dir ${_arg})
+ #
+ # Work around macOS (and probably other Darwin) brokenness,
+ # by not adding /usr/local/lib if it's from the broken
+ # Apple pcap-config.
+ #
+ if(NOT _broken_apple_pcap_config OR
+ NOT "${_dir}" STREQUAL /usr/local/lib)
+ # Add this directory to CONFIG_PCAP_LIBRARY_DIRS
+ list(APPEND CONFIG_PCAP_LIBRARY_DIRS ${_dir})
+ endif()
+ elseif(_arg MATCHES "^-l")
+ string(REGEX REPLACE "-l" "" _lib ${_arg})
+ list(APPEND CONFIG_PCAP_LIBRARIES ${_lib})
+ endif()
+ endforeach()
+
+ #
+ # Now, get the library directories and libraries for static linking.
+ #
+ execute_process(COMMAND "${PCAP_CONFIG}" "--libs" "--static"
+ RESULT_VARIABLE PCAP_CONFIG_RESULT
+ OUTPUT_VARIABLE PCAP_CONFIG_OUTPUT
+ )
+ if(NOT PCAP_CONFIG_RESULT EQUAL 0)
+ message(FATAL_ERROR "pcap-config --libs --static failed")
+ endif()
+ separate_arguments(LIBS_LIST UNIX_COMMAND ${PCAP_CONFIG_OUTPUT})
+ set(CONFIG_PCAP_STATIC_LIBRARY_DIRS "")
+ set(CONFIG_PCAP_STATIC_LIBRARIES "")
+ foreach(_arg IN LISTS LIBS_LIST)
+ if(_arg MATCHES "^-L")
+ #
+ # Extract the directory by removing the -L.
+ #
+ string(REGEX REPLACE "-L" "" _dir ${_arg})
+ #
+ # Work around macOS (and probably other Darwin) brokenness,
+ # by not adding /usr/local/lib if it's from the broken
+ # Apple pcap-config.
+ #
+ if(NOT _broken_apple_pcap_config OR
+ NOT "${_dir}" STREQUAL /usr/local/lib)
+ # Add this directory to CONFIG_PCAP_STATIC_LIBRARY_DIRS
+ list(APPEND CONFIG_PCAP_STATIC_LIBRARY_DIRS ${_dir})
+ endif()
+ elseif(_arg MATCHES "^-l")
+ string(REGEX REPLACE "-l" "" _lib ${_arg})
+ #
+ # Try to find that library, so we get its full path, as
+ # we do with dynamic libraries.
+ #
+ list(APPEND CONFIG_PCAP_STATIC_LIBRARIES ${_lib})
+ endif()
+ endforeach()
+
+ #
+ # We've set CONFIG_PCAP_INCLUDE_DIRS, CONFIG_PCAP_LIBRARIES, and
+ # CONFIG_PCAP_STATIC_LIBRARIES above; set CONFIG_PCAP_FOUND.
+ #
+ set(CONFIG_PCAP_FOUND YES)