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