1*ee67461eSJoseph Mingrone# 2*ee67461eSJoseph Mingrone# Try to find libpcap. 3*ee67461eSJoseph Mingrone# 4*ee67461eSJoseph Mingrone# To tell this module where to look, a user may set the environment variable 5*ee67461eSJoseph Mingrone# PCAP_ROOT to point cmake to the *root* of a directory with include and 6*ee67461eSJoseph Mingrone# lib subdirectories for pcap.dll (e.g WpdPack or npcap-sdk). 7*ee67461eSJoseph Mingrone# Alternatively, PCAP_ROOT may also be set from cmake command line or GUI 8*ee67461eSJoseph Mingrone# (e.g cmake -DPCAP_ROOT=C:\path\to\pcap [...]) 9*ee67461eSJoseph Mingrone# 10*ee67461eSJoseph Mingrone 11*ee67461eSJoseph Mingroneif(WIN32) 12*ee67461eSJoseph Mingrone # 13*ee67461eSJoseph Mingrone # Building for Windows. 14*ee67461eSJoseph Mingrone # 15*ee67461eSJoseph Mingrone # libpcap isn't set up to install .pc files or pcap-config on Windows, 16*ee67461eSJoseph Mingrone # and it's not clear that either of them would work without a lot 17*ee67461eSJoseph Mingrone # of additional effort. WinPcap doesn't supply them, and neither 18*ee67461eSJoseph Mingrone # does Npcap. 19*ee67461eSJoseph Mingrone # 20*ee67461eSJoseph Mingrone # So just search for them directly. Look for both pcap and wpcap. 21*ee67461eSJoseph Mingrone # Don't bother looking for static libraries; unlike most UN*Xes 22*ee67461eSJoseph Mingrone # (with the exception of AIX), where different extensions are used 23*ee67461eSJoseph Mingrone # for shared and static, Windows uses .lib both for import libraries 24*ee67461eSJoseph Mingrone # for DLLs and for static libraries. 25*ee67461eSJoseph Mingrone # 26*ee67461eSJoseph Mingrone # We don't directly set PCAP_INCLUDE_DIRS or PCAP_LIBRARIES, as 27*ee67461eSJoseph Mingrone # they're not supposed to be cache entries, and find_path() and 28*ee67461eSJoseph Mingrone # find_library() set cache entries. 29*ee67461eSJoseph Mingrone # 30*ee67461eSJoseph Mingrone find_path(PCAP_INCLUDE_DIR pcap.h) 31*ee67461eSJoseph Mingrone 32*ee67461eSJoseph Mingrone # The 64-bit Packet.lib is located under /x64 33*ee67461eSJoseph Mingrone if(CMAKE_SIZEOF_VOID_P EQUAL 8) 34*ee67461eSJoseph Mingrone # 35*ee67461eSJoseph Mingrone # For the WinPcap and Npcap SDKs, the Lib subdirectory of the top-level 36*ee67461eSJoseph Mingrone # directory contains 32-bit libraries; the 64-bit libraries are in the 37*ee67461eSJoseph Mingrone # Lib/x64 directory. 38*ee67461eSJoseph Mingrone # 39*ee67461eSJoseph Mingrone # The only way to *FORCE* CMake to look in the Lib/x64 directory 40*ee67461eSJoseph Mingrone # without searching in the Lib directory first appears to be to set 41*ee67461eSJoseph Mingrone # CMAKE_LIBRARY_ARCHITECTURE to "x64". 42*ee67461eSJoseph Mingrone # 43*ee67461eSJoseph Mingrone set(CMAKE_LIBRARY_ARCHITECTURE "x64") 44*ee67461eSJoseph Mingrone endif() 45*ee67461eSJoseph Mingrone find_library(PCAP_LIBRARY NAMES pcap wpcap) 46*ee67461eSJoseph Mingrone 47*ee67461eSJoseph Mingrone # 48*ee67461eSJoseph Mingrone # Do the standard arg processing, including failing if it's a 49*ee67461eSJoseph Mingrone # required package. 50*ee67461eSJoseph Mingrone # 51*ee67461eSJoseph Mingrone include(FindPackageHandleStandardArgs) 52*ee67461eSJoseph Mingrone find_package_handle_standard_args(PCAP 53*ee67461eSJoseph Mingrone DEFAULT_MSG 54*ee67461eSJoseph Mingrone PCAP_INCLUDE_DIR 55*ee67461eSJoseph Mingrone PCAP_LIBRARY 56*ee67461eSJoseph Mingrone ) 57*ee67461eSJoseph Mingrone mark_as_advanced( 58*ee67461eSJoseph Mingrone PCAP_INCLUDE_DIR 59*ee67461eSJoseph Mingrone PCAP_LIBRARY 60*ee67461eSJoseph Mingrone ) 61*ee67461eSJoseph Mingrone if(PCAP_FOUND) 62*ee67461eSJoseph Mingrone set(PCAP_LIBRARIES ${PCAP_LIBRARY}) 63*ee67461eSJoseph Mingrone set(PCAP_INCLUDE_DIRS ${PCAP_INCLUDE_DIR}) 64*ee67461eSJoseph Mingrone endif() 65*ee67461eSJoseph Mingroneelse(WIN32) 66*ee67461eSJoseph Mingrone # 67*ee67461eSJoseph Mingrone # Building for UN*X. 68*ee67461eSJoseph Mingrone # 69*ee67461eSJoseph Mingrone # See whether we were handed a QUIET argument, so we can pass it on 70*ee67461eSJoseph Mingrone # to pkg_search_module. Do *NOT* pass on the REQUIRED argument, 71*ee67461eSJoseph Mingrone # because, if pkg-config isn't found, or it is but it has no .pc 72*ee67461eSJoseph Mingrone # files for libpcap, that is *not* necessarily an indication that 73*ee67461eSJoseph Mingrone # libpcap isn't available - not all systems ship pkg-config, and 74*ee67461eSJoseph Mingrone # libpcap didn't have .pc files until libpcap 1.9.0. 75*ee67461eSJoseph Mingrone # 76*ee67461eSJoseph Mingrone if(PCAP_FIND_QUIETLY) 77*ee67461eSJoseph Mingrone set(_quiet "QUIET") 78*ee67461eSJoseph Mingrone endif() 79*ee67461eSJoseph Mingrone 80*ee67461eSJoseph Mingrone # 81*ee67461eSJoseph Mingrone # First, try pkg-config. 82*ee67461eSJoseph Mingrone # Before doing so, set the PKG_CONFIG_PATH environment variable 83*ee67461eSJoseph Mingrone # to include all the directories in CMAKE_PREFIX_PATH. 84*ee67461eSJoseph Mingrone # 85*ee67461eSJoseph Mingrone # *If* we were to require CMake 3.1 or later on UN*X, 86*ee67461eSJoseph Mingrone # pkg_search_module() would do this for us, but, for now, 87*ee67461eSJoseph Mingrone # we're not doing that, in case somebody's building with 88*ee67461eSJoseph Mingrone # CMake on some "long-term support" version, predating 89*ee67461eSJoseph Mingrone # CMake 3.1, of an OS that supplies an earlier 90*ee67461eSJoseph Mingrone # version as a package. 91*ee67461eSJoseph Mingrone # 92*ee67461eSJoseph Mingrone # If we ever set a minimum of 3.1 or later on UN*X, we should 93*ee67461eSJoseph Mingrone # remove the environment variable changes. 94*ee67461eSJoseph Mingrone # 95*ee67461eSJoseph Mingrone # This is based on code in the CMake 3.12.4 FindPkgConfig.cmake, 96*ee67461eSJoseph Mingrone # which is "Distributed under the OSI-approved BSD 3-Clause License." 97*ee67461eSJoseph Mingrone # 98*ee67461eSJoseph Mingrone find_package(PkgConfig) 99*ee67461eSJoseph Mingrone 100*ee67461eSJoseph Mingrone # 101*ee67461eSJoseph Mingrone # Get the current PKG_CONFIG_PATH setting. 102*ee67461eSJoseph Mingrone # 103*ee67461eSJoseph Mingrone set(_pkg_config_path "$ENV{PKG_CONFIG_PATH}") 104*ee67461eSJoseph Mingrone 105*ee67461eSJoseph Mingrone # 106*ee67461eSJoseph Mingrone # Save it, so we can restore it after we run pkg-config. 107*ee67461eSJoseph Mingrone # 108*ee67461eSJoseph Mingrone set(_saved_pkg_config_path "${_pkg_config_path}") 109*ee67461eSJoseph Mingrone 110*ee67461eSJoseph Mingrone if(NOT "${CMAKE_PREFIX_PATH}" STREQUAL "") 111*ee67461eSJoseph Mingrone # 112*ee67461eSJoseph Mingrone # Convert it to a CMake-style path, before we add additional 113*ee67461eSJoseph Mingrone # values to it. 114*ee67461eSJoseph Mingrone # 115*ee67461eSJoseph Mingrone if(NOT "${_pkg_config_path}" STREQUAL "") 116*ee67461eSJoseph Mingrone file(TO_CMAKE_PATH "${_pkg_config_path}" _pkg_config_path) 117*ee67461eSJoseph Mingrone endif() 118*ee67461eSJoseph Mingrone 119*ee67461eSJoseph Mingrone # 120*ee67461eSJoseph Mingrone # Turn CMAKE_PREFIX_PATH into a list of extra paths to add 121*ee67461eSJoseph Mingrone # to _pkg_config_path. 122*ee67461eSJoseph Mingrone # 123*ee67461eSJoseph Mingrone set(_extra_paths "") 124*ee67461eSJoseph Mingrone list(APPEND _extra_paths ${CMAKE_PREFIX_PATH}) 125*ee67461eSJoseph Mingrone 126*ee67461eSJoseph Mingrone # Create a list of the possible pkgconfig subfolder (depending on 127*ee67461eSJoseph Mingrone # the system 128*ee67461eSJoseph Mingrone set(_lib_dirs) 129*ee67461eSJoseph Mingrone if(NOT DEFINED CMAKE_SYSTEM_NAME 130*ee67461eSJoseph Mingrone OR (CMAKE_SYSTEM_NAME MATCHES "^(Linux|kFreeBSD|GNU)$" 131*ee67461eSJoseph Mingrone AND NOT CMAKE_CROSSCOMPILING)) 132*ee67461eSJoseph Mingrone if(EXISTS "/etc/debian_version") # is this a debian system ? 133*ee67461eSJoseph Mingrone if(CMAKE_LIBRARY_ARCHITECTURE) 134*ee67461eSJoseph Mingrone list(APPEND _lib_dirs "lib/${CMAKE_LIBRARY_ARCHITECTURE}/pkgconfig") 135*ee67461eSJoseph Mingrone endif() 136*ee67461eSJoseph Mingrone else() 137*ee67461eSJoseph Mingrone # not debian, check the FIND_LIBRARY_USE_LIB32_PATHS and FIND_LIBRARY_USE_LIB64_PATHS properties 138*ee67461eSJoseph Mingrone get_property(uselib32 GLOBAL PROPERTY FIND_LIBRARY_USE_LIB32_PATHS) 139*ee67461eSJoseph Mingrone if(uselib32 AND CMAKE_SIZEOF_VOID_P EQUAL 4) 140*ee67461eSJoseph Mingrone list(APPEND _lib_dirs "lib32/pkgconfig") 141*ee67461eSJoseph Mingrone endif() 142*ee67461eSJoseph Mingrone get_property(uselib64 GLOBAL PROPERTY FIND_LIBRARY_USE_LIB64_PATHS) 143*ee67461eSJoseph Mingrone if(uselib64 AND CMAKE_SIZEOF_VOID_P EQUAL 8) 144*ee67461eSJoseph Mingrone list(APPEND _lib_dirs "lib64/pkgconfig") 145*ee67461eSJoseph Mingrone endif() 146*ee67461eSJoseph Mingrone get_property(uselibx32 GLOBAL PROPERTY FIND_LIBRARY_USE_LIBX32_PATHS) 147*ee67461eSJoseph Mingrone if(uselibx32 AND CMAKE_INTERNAL_PLATFORM_ABI STREQUAL "ELF X32") 148*ee67461eSJoseph Mingrone list(APPEND _lib_dirs "libx32/pkgconfig") 149*ee67461eSJoseph Mingrone endif() 150*ee67461eSJoseph Mingrone endif() 151*ee67461eSJoseph Mingrone endif() 152*ee67461eSJoseph Mingrone if(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD" AND NOT CMAKE_CROSSCOMPILING) 153*ee67461eSJoseph Mingrone list(APPEND _lib_dirs "libdata/pkgconfig") 154*ee67461eSJoseph Mingrone endif() 155*ee67461eSJoseph Mingrone list(APPEND _lib_dirs "lib/pkgconfig") 156*ee67461eSJoseph Mingrone list(APPEND _lib_dirs "share/pkgconfig") 157*ee67461eSJoseph Mingrone 158*ee67461eSJoseph Mingrone # Check if directories exist and eventually append them to the 159*ee67461eSJoseph Mingrone # pkgconfig path list 160*ee67461eSJoseph Mingrone foreach(_prefix_dir ${_extra_paths}) 161*ee67461eSJoseph Mingrone foreach(_lib_dir ${_lib_dirs}) 162*ee67461eSJoseph Mingrone if(EXISTS "${_prefix_dir}/${_lib_dir}") 163*ee67461eSJoseph Mingrone list(APPEND _pkg_config_path "${_prefix_dir}/${_lib_dir}") 164*ee67461eSJoseph Mingrone list(REMOVE_DUPLICATES _pkg_config_path) 165*ee67461eSJoseph Mingrone endif() 166*ee67461eSJoseph Mingrone endforeach() 167*ee67461eSJoseph Mingrone endforeach() 168*ee67461eSJoseph Mingrone 169*ee67461eSJoseph Mingrone if(NOT "${_pkg_config_path}" STREQUAL "") 170*ee67461eSJoseph Mingrone # remove empty values from the list 171*ee67461eSJoseph Mingrone list(REMOVE_ITEM _pkg_config_path "") 172*ee67461eSJoseph Mingrone file(TO_NATIVE_PATH "${_pkg_config_path}" _pkg_config_path) 173*ee67461eSJoseph Mingrone if(UNIX) 174*ee67461eSJoseph Mingrone string(REPLACE ";" ":" _pkg_config_path "${_pkg_config_path}") 175*ee67461eSJoseph Mingrone string(REPLACE "\\ " " " _pkg_config_path "${_pkg_config_path}") 176*ee67461eSJoseph Mingrone endif() 177*ee67461eSJoseph Mingrone set(ENV{PKG_CONFIG_PATH} "${_pkg_config_path}") 178*ee67461eSJoseph Mingrone endif() 179*ee67461eSJoseph Mingrone endif() 180*ee67461eSJoseph Mingrone pkg_search_module(CONFIG_PCAP ${_quiet} libpcap) 181*ee67461eSJoseph Mingrone set(ENV{PKG_CONFIG_PATH} "${_saved_pkg_config_path}") 182*ee67461eSJoseph Mingrone 183*ee67461eSJoseph Mingrone if(NOT CONFIG_PCAP_FOUND) 184*ee67461eSJoseph Mingrone # 185*ee67461eSJoseph Mingrone # That didn't work. Try pcap-config. 186*ee67461eSJoseph Mingrone # 187*ee67461eSJoseph Mingrone find_program(PCAP_CONFIG pcap-config) 188*ee67461eSJoseph Mingrone if(PCAP_CONFIG) 189*ee67461eSJoseph Mingrone # 190*ee67461eSJoseph Mingrone # We have pcap-config; use it. 191*ee67461eSJoseph Mingrone # 192*ee67461eSJoseph Mingrone if(NOT "${_quiet}" STREQUAL "QUIET") 193*ee67461eSJoseph Mingrone message(STATUS "Found pcap-config") 194*ee67461eSJoseph Mingrone endif() 195*ee67461eSJoseph Mingrone 196*ee67461eSJoseph Mingrone # 197*ee67461eSJoseph Mingrone # If this is a vendor-supplied pcap-config, which we define as 198*ee67461eSJoseph Mingrone # being "a pcap-config in /usr/bin or /usr/ccs/bin" (the latter 199*ee67461eSJoseph Mingrone # is for Solaris and Sun/Oracle Studio), there are some issues. 200*ee67461eSJoseph Mingrone # Work around them. 201*ee67461eSJoseph Mingrone # 202*ee67461eSJoseph Mingrone if("${PCAP_CONFIG}" STREQUAL /usr/bin/pcap-config OR 203*ee67461eSJoseph Mingrone "${PCAP_CONFIG}" STREQUAL /usr/ccs/bin/pcap-config) 204*ee67461eSJoseph Mingrone # 205*ee67461eSJoseph Mingrone # It's vendor-supplied. 206*ee67461eSJoseph Mingrone # 207*ee67461eSJoseph Mingrone if(APPLE) 208*ee67461eSJoseph Mingrone # 209*ee67461eSJoseph Mingrone # This is macOS or another Darwin-based OS. 210*ee67461eSJoseph Mingrone # 211*ee67461eSJoseph Mingrone # That means that /usr/bin/pcap-config it may provide 212*ee67461eSJoseph Mingrone # -I/usr/local/include with --cflags and -L/usr/local/lib 213*ee67461eSJoseph Mingrone # with --libs; if there's no pcap installed under /usr/local, 214*ee67461eSJoseph Mingrone # that will cause the build to fail, and if there is a pcap 215*ee67461eSJoseph Mingrone # installed there, you'll get that pcap even if you don't 216*ee67461eSJoseph Mingrone # want it. Remember that, so we ignore those values. 217*ee67461eSJoseph Mingrone # 218*ee67461eSJoseph Mingrone set(_broken_apple_pcap_config TRUE) 219*ee67461eSJoseph Mingrone elseif(CMAKE_SYSTEM_NAME STREQUAL "SunOS" AND CMAKE_SYSTEM_VERSION MATCHES "5[.][0-9.]*") 220*ee67461eSJoseph Mingrone # 221*ee67461eSJoseph Mingrone # This is Solaris 2 or later, i.e. SunOS 5.x. 222*ee67461eSJoseph Mingrone # 223*ee67461eSJoseph Mingrone # At least on Solaris 11; there's /usr/bin/pcap-config, which 224*ee67461eSJoseph Mingrone # reports -L/usr/lib with --libs, causing the 32-bit libraries 225*ee67461eSJoseph Mingrone # to be found, and there's /usr/bin/{64bitarch}/pcap-config, 226*ee67461eSJoseph Mingrone # where {64bitarch} is a name for the 64-bit version of the 227*ee67461eSJoseph Mingrone # instruction set, which reports -L /usr/lib/{64bitarch}, 228*ee67461eSJoseph Mingrone # causing the 64-bit libraries to be found. 229*ee67461eSJoseph Mingrone # 230*ee67461eSJoseph Mingrone # So if we're building 64-bit targets, we replace PCAP_CONFIG 231*ee67461eSJoseph Mingrone # with /usr/bin/{64bitarch}; we get {64bitarch} as the 232*ee67461eSJoseph Mingrone # output of "isainfo -n". 233*ee67461eSJoseph Mingrone # 234*ee67461eSJoseph Mingrone if(CMAKE_SIZEOF_VOID_P EQUAL 8) 235*ee67461eSJoseph Mingrone execute_process(COMMAND "isainfo" "-n" 236*ee67461eSJoseph Mingrone RESULT_VARIABLE ISAINFO_RESULT 237*ee67461eSJoseph Mingrone OUTPUT_VARIABLE ISAINFO_OUTPUT 238*ee67461eSJoseph Mingrone OUTPUT_STRIP_TRAILING_WHITESPACE 239*ee67461eSJoseph Mingrone ) 240*ee67461eSJoseph Mingrone if(ISAINFO_RESULT EQUAL 0) 241*ee67461eSJoseph Mingrone # 242*ee67461eSJoseph Mingrone # Success - change PCAP_CONFIG. 243*ee67461eSJoseph Mingrone # 244*ee67461eSJoseph Mingrone string(REPLACE "/bin/" "/bin/${ISAINFO_OUTPUT}/" PCAP_CONFIG "${PCAP_CONFIG}") 245*ee67461eSJoseph Mingrone endif() 246*ee67461eSJoseph Mingrone endif() 247*ee67461eSJoseph Mingrone endif() 248*ee67461eSJoseph Mingrone endif() 249*ee67461eSJoseph Mingrone 250*ee67461eSJoseph Mingrone # 251*ee67461eSJoseph Mingrone # Now get the include directories. 252*ee67461eSJoseph Mingrone # 253*ee67461eSJoseph Mingrone execute_process(COMMAND "${PCAP_CONFIG}" "--cflags" 254*ee67461eSJoseph Mingrone RESULT_VARIABLE PCAP_CONFIG_RESULT 255*ee67461eSJoseph Mingrone OUTPUT_VARIABLE PCAP_CONFIG_OUTPUT 256*ee67461eSJoseph Mingrone OUTPUT_STRIP_TRAILING_WHITESPACE 257*ee67461eSJoseph Mingrone ) 258*ee67461eSJoseph Mingrone if(NOT PCAP_CONFIG_RESULT EQUAL 0) 259*ee67461eSJoseph Mingrone message(FATAL_ERROR "pcap-config --cflags failed") 260*ee67461eSJoseph Mingrone endif() 261*ee67461eSJoseph Mingrone separate_arguments(CFLAGS_LIST UNIX_COMMAND ${PCAP_CONFIG_OUTPUT}) 262*ee67461eSJoseph Mingrone set(CONFIG_PCAP_INCLUDE_DIRS "") 263*ee67461eSJoseph Mingrone foreach(_arg IN LISTS CFLAGS_LIST) 264*ee67461eSJoseph Mingrone if(_arg MATCHES "^-I") 265*ee67461eSJoseph Mingrone # 266*ee67461eSJoseph Mingrone # Extract the directory by removing the -I. 267*ee67461eSJoseph Mingrone # 268*ee67461eSJoseph Mingrone string(REGEX REPLACE "-I" "" _dir ${_arg}) 269*ee67461eSJoseph Mingrone # 270*ee67461eSJoseph Mingrone # Work around macOS (and probably other Darwin) brokenness, 271*ee67461eSJoseph Mingrone # by not adding /usr/local/include if it's from the broken 272*ee67461eSJoseph Mingrone # Apple pcap-config. 273*ee67461eSJoseph Mingrone # 274*ee67461eSJoseph Mingrone if(NOT _broken_apple_pcap_config OR 275*ee67461eSJoseph Mingrone NOT "${_dir}" STREQUAL /usr/local/include) 276*ee67461eSJoseph Mingrone # Add it to CONFIG_PCAP_INCLUDE_DIRS 277*ee67461eSJoseph Mingrone list(APPEND CONFIG_PCAP_INCLUDE_DIRS ${_dir}) 278*ee67461eSJoseph Mingrone endif() 279*ee67461eSJoseph Mingrone endif() 280*ee67461eSJoseph Mingrone endforeach() 281*ee67461eSJoseph Mingrone 282*ee67461eSJoseph Mingrone # 283*ee67461eSJoseph Mingrone # Now, get the library directories and libraries for dynamic linking. 284*ee67461eSJoseph Mingrone # 285*ee67461eSJoseph Mingrone execute_process(COMMAND "${PCAP_CONFIG}" "--libs" 286*ee67461eSJoseph Mingrone RESULT_VARIABLE PCAP_CONFIG_RESULT 287*ee67461eSJoseph Mingrone OUTPUT_VARIABLE PCAP_CONFIG_OUTPUT 288*ee67461eSJoseph Mingrone OUTPUT_STRIP_TRAILING_WHITESPACE 289*ee67461eSJoseph Mingrone ) 290*ee67461eSJoseph Mingrone if(NOT PCAP_CONFIG_RESULT EQUAL 0) 291*ee67461eSJoseph Mingrone message(FATAL_ERROR "pcap-config --libs failed") 292*ee67461eSJoseph Mingrone endif() 293*ee67461eSJoseph Mingrone separate_arguments(LIBS_LIST UNIX_COMMAND ${PCAP_CONFIG_OUTPUT}) 294*ee67461eSJoseph Mingrone set(CONFIG_PCAP_LIBRARY_DIRS "") 295*ee67461eSJoseph Mingrone set(CONFIG_PCAP_LIBRARIES "") 296*ee67461eSJoseph Mingrone foreach(_arg IN LISTS LIBS_LIST) 297*ee67461eSJoseph Mingrone if(_arg MATCHES "^-L") 298*ee67461eSJoseph Mingrone # 299*ee67461eSJoseph Mingrone # Extract the directory by removing the -L. 300*ee67461eSJoseph Mingrone # 301*ee67461eSJoseph Mingrone string(REGEX REPLACE "-L" "" _dir ${_arg}) 302*ee67461eSJoseph Mingrone # 303*ee67461eSJoseph Mingrone # Work around macOS (and probably other Darwin) brokenness, 304*ee67461eSJoseph Mingrone # by not adding /usr/local/lib if it's from the broken 305*ee67461eSJoseph Mingrone # Apple pcap-config. 306*ee67461eSJoseph Mingrone # 307*ee67461eSJoseph Mingrone if(NOT _broken_apple_pcap_config OR 308*ee67461eSJoseph Mingrone NOT "${_dir}" STREQUAL /usr/local/lib) 309*ee67461eSJoseph Mingrone # Add this directory to CONFIG_PCAP_LIBRARY_DIRS 310*ee67461eSJoseph Mingrone list(APPEND CONFIG_PCAP_LIBRARY_DIRS ${_dir}) 311*ee67461eSJoseph Mingrone endif() 312*ee67461eSJoseph Mingrone elseif(_arg MATCHES "^-l") 313*ee67461eSJoseph Mingrone string(REGEX REPLACE "-l" "" _lib ${_arg}) 314*ee67461eSJoseph Mingrone list(APPEND CONFIG_PCAP_LIBRARIES ${_lib}) 315*ee67461eSJoseph Mingrone endif() 316*ee67461eSJoseph Mingrone endforeach() 317*ee67461eSJoseph Mingrone 318*ee67461eSJoseph Mingrone # 319*ee67461eSJoseph Mingrone # Now, get the library directories and libraries for static linking. 320*ee67461eSJoseph Mingrone # 321*ee67461eSJoseph Mingrone execute_process(COMMAND "${PCAP_CONFIG}" "--libs" "--static" 322*ee67461eSJoseph Mingrone RESULT_VARIABLE PCAP_CONFIG_RESULT 323*ee67461eSJoseph Mingrone OUTPUT_VARIABLE PCAP_CONFIG_OUTPUT 324*ee67461eSJoseph Mingrone ) 325*ee67461eSJoseph Mingrone if(NOT PCAP_CONFIG_RESULT EQUAL 0) 326*ee67461eSJoseph Mingrone message(FATAL_ERROR "pcap-config --libs --static failed") 327*ee67461eSJoseph Mingrone endif() 328*ee67461eSJoseph Mingrone separate_arguments(LIBS_LIST UNIX_COMMAND ${PCAP_CONFIG_OUTPUT}) 329*ee67461eSJoseph Mingrone set(CONFIG_PCAP_STATIC_LIBRARY_DIRS "") 330*ee67461eSJoseph Mingrone set(CONFIG_PCAP_STATIC_LIBRARIES "") 331*ee67461eSJoseph Mingrone foreach(_arg IN LISTS LIBS_LIST) 332*ee67461eSJoseph Mingrone if(_arg MATCHES "^-L") 333*ee67461eSJoseph Mingrone # 334*ee67461eSJoseph Mingrone # Extract the directory by removing the -L. 335*ee67461eSJoseph Mingrone # 336*ee67461eSJoseph Mingrone string(REGEX REPLACE "-L" "" _dir ${_arg}) 337*ee67461eSJoseph Mingrone # 338*ee67461eSJoseph Mingrone # Work around macOS (and probably other Darwin) brokenness, 339*ee67461eSJoseph Mingrone # by not adding /usr/local/lib if it's from the broken 340*ee67461eSJoseph Mingrone # Apple pcap-config. 341*ee67461eSJoseph Mingrone # 342*ee67461eSJoseph Mingrone if(NOT _broken_apple_pcap_config OR 343*ee67461eSJoseph Mingrone NOT "${_dir}" STREQUAL /usr/local/lib) 344*ee67461eSJoseph Mingrone # Add this directory to CONFIG_PCAP_STATIC_LIBRARY_DIRS 345*ee67461eSJoseph Mingrone list(APPEND CONFIG_PCAP_STATIC_LIBRARY_DIRS ${_dir}) 346*ee67461eSJoseph Mingrone endif() 347*ee67461eSJoseph Mingrone elseif(_arg MATCHES "^-l") 348*ee67461eSJoseph Mingrone string(REGEX REPLACE "-l" "" _lib ${_arg}) 349*ee67461eSJoseph Mingrone # 350*ee67461eSJoseph Mingrone # Try to find that library, so we get its full path, as 351*ee67461eSJoseph Mingrone # we do with dynamic libraries. 352*ee67461eSJoseph Mingrone # 353*ee67461eSJoseph Mingrone list(APPEND CONFIG_PCAP_STATIC_LIBRARIES ${_lib}) 354*ee67461eSJoseph Mingrone endif() 355*ee67461eSJoseph Mingrone endforeach() 356*ee67461eSJoseph Mingrone 357*ee67461eSJoseph Mingrone # 358*ee67461eSJoseph Mingrone # We've set CONFIG_PCAP_INCLUDE_DIRS, CONFIG_PCAP_LIBRARIES, and 359*ee67461eSJoseph Mingrone # CONFIG_PCAP_STATIC_LIBRARIES above; set CONFIG_PCAP_FOUND. 360*ee67461eSJoseph Mingrone # 361*ee67461eSJoseph Mingrone set(CONFIG_PCAP_FOUND YES) 362*ee67461eSJoseph Mingrone endif() 363*ee67461eSJoseph Mingrone endif() 364*ee67461eSJoseph Mingrone 365*ee67461eSJoseph Mingrone # 366*ee67461eSJoseph Mingrone # If CONFIG_PCAP_FOUND is set, we have information from pkg-config and 367*ee67461eSJoseph Mingrone # pcap-config; we need to convert library names to library full paths. 368*ee67461eSJoseph Mingrone # 369*ee67461eSJoseph Mingrone # If it's not set, we have to look for the libpcap headers and library 370*ee67461eSJoseph Mingrone # ourselves. 371*ee67461eSJoseph Mingrone # 372*ee67461eSJoseph Mingrone if(CONFIG_PCAP_FOUND) 373*ee67461eSJoseph Mingrone # 374*ee67461eSJoseph Mingrone # Use CONFIG_PCAP_INCLUDE_DIRS as the value for PCAP_INCLUDE_DIRS. 375*ee67461eSJoseph Mingrone # 376*ee67461eSJoseph Mingrone set(PCAP_INCLUDE_DIRS "${CONFIG_PCAP_INCLUDE_DIRS}") 377*ee67461eSJoseph Mingrone 378*ee67461eSJoseph Mingrone # 379*ee67461eSJoseph Mingrone # CMake *really* doesn't like the notion of specifying 380*ee67461eSJoseph Mingrone # "here are the directories in which to look for libraries" 381*ee67461eSJoseph Mingrone # except in find_library() calls; it *really* prefers using 382*ee67461eSJoseph Mingrone # full paths to library files, rather than library names. 383*ee67461eSJoseph Mingrone # 384*ee67461eSJoseph Mingrone foreach(_lib IN LISTS CONFIG_PCAP_LIBRARIES) 385*ee67461eSJoseph Mingrone find_library(_libfullpath ${_lib} HINTS ${CONFIG_PCAP_LIBRARY_DIRS}) 386*ee67461eSJoseph Mingrone list(APPEND PCAP_LIBRARIES ${_libfullpath}) 387*ee67461eSJoseph Mingrone # 388*ee67461eSJoseph Mingrone # Remove that from the cache; we're using it as a local variable, 389*ee67461eSJoseph Mingrone # but find_library insists on making it a cache variable. 390*ee67461eSJoseph Mingrone # 391*ee67461eSJoseph Mingrone unset(_libfullpath CACHE) 392*ee67461eSJoseph Mingrone endforeach() 393*ee67461eSJoseph Mingrone 394*ee67461eSJoseph Mingrone # 395*ee67461eSJoseph Mingrone # Now do the same for the static libraries. 396*ee67461eSJoseph Mingrone # 397*ee67461eSJoseph Mingrone set(SAVED_CMAKE_FIND_LIBRARY_SUFFIXES "${CMAKE_FIND_LIBRARY_SUFFIXES}") 398*ee67461eSJoseph Mingrone set(CMAKE_FIND_LIBRARY_SUFFIXES ".a") 399*ee67461eSJoseph Mingrone foreach(_lib IN LISTS CONFIG_PCAP_STATIC_LIBRARIES) 400*ee67461eSJoseph Mingrone find_library(_libfullpath ${_lib} HINTS ${CONFIG_PCAP_LIBRARY_DIRS}) 401*ee67461eSJoseph Mingrone list(APPEND PCAP_STATIC_LIBRARIES ${_libfullpath}) 402*ee67461eSJoseph Mingrone # 403*ee67461eSJoseph Mingrone # Remove that from the cache; we're using it as a local variable, 404*ee67461eSJoseph Mingrone # but find_library insists on making it a cache variable. 405*ee67461eSJoseph Mingrone # 406*ee67461eSJoseph Mingrone unset(_libfullpath CACHE) 407*ee67461eSJoseph Mingrone endforeach() 408*ee67461eSJoseph Mingrone set(CMAKE_FIND_LIBRARY_SUFFIXES "${SAVED_CMAKE_FIND_LIBRARY_SUFFIXES}") 409*ee67461eSJoseph Mingrone 410*ee67461eSJoseph Mingrone # 411*ee67461eSJoseph Mingrone # We found libpcap using pkg-config or pcap-config. 412*ee67461eSJoseph Mingrone # 413*ee67461eSJoseph Mingrone set(PCAP_FOUND YES) 414*ee67461eSJoseph Mingrone else(CONFIG_PCAP_FOUND) 415*ee67461eSJoseph Mingrone # 416*ee67461eSJoseph Mingrone # We didn't have pkg-config, or we did but it didn't have .pc files 417*ee67461eSJoseph Mingrone # for libpcap, and we don't have pkg-config, so we have to look for 418*ee67461eSJoseph Mingrone # the headers and libraries ourself. 419*ee67461eSJoseph Mingrone # 420*ee67461eSJoseph Mingrone # We don't directly set PCAP_INCLUDE_DIRS or PCAP_LIBRARIES, as 421*ee67461eSJoseph Mingrone # they're not supposed to be cache entries, and find_path() and 422*ee67461eSJoseph Mingrone # find_library() set cache entries. 423*ee67461eSJoseph Mingrone # 424*ee67461eSJoseph Mingrone # Try to find the header file. 425*ee67461eSJoseph Mingrone # 426*ee67461eSJoseph Mingrone find_path(PCAP_INCLUDE_DIR pcap.h) 427*ee67461eSJoseph Mingrone 428*ee67461eSJoseph Mingrone # 429*ee67461eSJoseph Mingrone # Try to find the library 430*ee67461eSJoseph Mingrone # 431*ee67461eSJoseph Mingrone find_library(PCAP_LIBRARY pcap) 432*ee67461eSJoseph Mingrone 433*ee67461eSJoseph Mingrone # Try to find the static library (XXX - what about AIX?) 434*ee67461eSJoseph Mingrone set(SAVED_CMAKE_FIND_LIBRARY_SUFFIXES "${CMAKE_FIND_LIBRARY_SUFFIXES}") 435*ee67461eSJoseph Mingrone set(CMAKE_FIND_LIBRARY_SUFFIXES ".a") 436*ee67461eSJoseph Mingrone find_library(PCAP_STATIC_LIBRARY pcap) 437*ee67461eSJoseph Mingrone set(CMAKE_FIND_LIBRARY_SUFFIXES "${SAVED_CMAKE_FIND_LIBRARY_SUFFIXES}") 438*ee67461eSJoseph Mingrone 439*ee67461eSJoseph Mingrone # 440*ee67461eSJoseph Mingrone # This will fail if REQUIRED is set and PCAP_INCLUDE_DIR or 441*ee67461eSJoseph Mingrone # PCAP_LIBRARY aren't set. 442*ee67461eSJoseph Mingrone # 443*ee67461eSJoseph Mingrone include(FindPackageHandleStandardArgs) 444*ee67461eSJoseph Mingrone find_package_handle_standard_args(PCAP 445*ee67461eSJoseph Mingrone DEFAULT_MSG 446*ee67461eSJoseph Mingrone PCAP_INCLUDE_DIR 447*ee67461eSJoseph Mingrone PCAP_LIBRARY 448*ee67461eSJoseph Mingrone ) 449*ee67461eSJoseph Mingrone 450*ee67461eSJoseph Mingrone mark_as_advanced( 451*ee67461eSJoseph Mingrone PCAP_INCLUDE_DIR 452*ee67461eSJoseph Mingrone PCAP_LIBRARY 453*ee67461eSJoseph Mingrone PCAP_STATIC_LIBRARY 454*ee67461eSJoseph Mingrone ) 455*ee67461eSJoseph Mingrone 456*ee67461eSJoseph Mingrone if(PCAP_FOUND) 457*ee67461eSJoseph Mingrone set(PCAP_INCLUDE_DIRS ${PCAP_INCLUDE_DIR}) 458*ee67461eSJoseph Mingrone set(PCAP_LIBRARIES ${PCAP_LIBRARY}) 459*ee67461eSJoseph Mingrone set(PCAP_STATIC_LIBRARIES ${PCAP_STATIC_LIBRARY}) 460*ee67461eSJoseph Mingrone endif(PCAP_FOUND) 461*ee67461eSJoseph Mingrone endif(CONFIG_PCAP_FOUND) 462*ee67461eSJoseph Mingroneendif(WIN32) 463