|
| 1 | +# ~~~ |
| 2 | +# - Try to find libpcap include dirs and libraries |
| 3 | +# |
| 4 | +# Usage of this module as follows: |
| 5 | +# |
| 6 | +# find_package(PCAP) |
| 7 | +# |
| 8 | +# Variables used by this module, they can change the default behaviour and need |
| 9 | +# to be set before calling find_package: |
| 10 | +# |
| 11 | +# Imported Targets: |
| 12 | +# PCAP::PCAP The libpcap library, if found |
| 13 | +# |
| 14 | +# Variables defined by this module: |
| 15 | +# |
| 16 | +# PCAP_FOUND System has libpcap, include and library dirs found |
| 17 | +# PCAP_INCLUDE_DIR The libpcap include directories. |
| 18 | +# PCAP_LIBRARY The libpcap library (possibly includes a thread |
| 19 | +# library e.g. required by pf_ring's libpcap) |
| 20 | +# HAVE_PCAP_IMMEDIATE_MODE If the version of libpcap found supports immediate mode |
| 21 | +# HAVE_PCAP_DIRECTION If the version of libpcap found support for setting direction |
| 22 | +# |
| 23 | +# Hints and Backward Compatibility |
| 24 | +# ================================ |
| 25 | +# |
| 26 | +# To tell this module where to look, a user may set the environment variable |
| 27 | +# PCAP_ROOT to point cmake to the *root* of a directory with include and lib |
| 28 | +# subdirectories for packet.dll (e.g WpdPack or npcap-sdk). Alternatively, |
| 29 | +# PCAP_ROOT may also be set from cmake command line or GUI (e.g cmake |
| 30 | +# -DPCAP_ROOT=C:\path\to\packet [...]) |
| 31 | +# ~~~ |
| 32 | + |
| 33 | +find_path( |
| 34 | + PCAP_INCLUDE_DIR |
| 35 | + NAMES pcap/pcap.h pcap.h |
| 36 | + PATH_SUFFIXES include Include) |
| 37 | + |
| 38 | +# The 64-bit Wpcap.lib is located under /x64 |
| 39 | +if(WIN32 AND CMAKE_SIZEOF_VOID_P EQUAL 8) |
| 40 | + # |
| 41 | + # For the WinPcap and Npcap SDKs, the Lib subdirectory of the top-level directory contains 32-bit libraries. The |
| 42 | + # 64-bit libraries are in the Lib/x64 directory. |
| 43 | + # |
| 44 | + # The only way to *FORCE* CMake to look in the Lib/x64 directory without searching in the Lib directory first appears |
| 45 | + # to be to set CMAKE_LIBRARY_ARCHITECTURE to "x64". |
| 46 | + # |
| 47 | + set(CMAKE_LIBRARY_ARCHITECTURE "x64") |
| 48 | +endif() |
| 49 | + |
| 50 | +find_library(PCAP_LIBRARY NAMES pcap wpcap) |
| 51 | + |
| 52 | +# If Pcap is not found as this level no need to continue |
| 53 | +if(NOT PCAP_LIBRARY OR NOT PCAP_INCLUDE_DIR) |
| 54 | + return() |
| 55 | +endif() |
| 56 | + |
| 57 | +include(CheckCXXSourceCompiles) |
| 58 | +set(CMAKE_REQUIRED_LIBRARIES ${PCAP_LIBRARY}) |
| 59 | +check_cxx_source_compiles("int main() { return 0; }" PCAP_LINKS_SOLO) |
| 60 | +set(CMAKE_REQUIRED_LIBRARIES) |
| 61 | + |
| 62 | +# check if linking against libpcap also needs to link against a thread library |
| 63 | +if(NOT PCAP_LINKS_SOLO) |
| 64 | + find_package(Threads) |
| 65 | + if(THREADS_FOUND) |
| 66 | + set(CMAKE_REQUIRED_LIBRARIES ${PCAP_LIBRARY} ${CMAKE_THREAD_LIBS_INIT}) |
| 67 | + check_cxx_source_compiles("int main() { return 0; }" PCAP_NEEDS_THREADS) |
| 68 | + set(CMAKE_REQUIRED_LIBRARIES) |
| 69 | + endif(THREADS_FOUND) |
| 70 | + if(THREADS_FOUND AND PCAP_NEEDS_THREADS) |
| 71 | + set(_tmp ${PCAP_LIBRARY} ${CMAKE_THREAD_LIBS_INIT}) |
| 72 | + list(REMOVE_DUPLICATES _tmp) |
| 73 | + set(PCAP_LIBRARY |
| 74 | + ${_tmp} |
| 75 | + CACHE STRING "Libraries needed to link against libpcap" FORCE) |
| 76 | + else(THREADS_FOUND AND PCAP_NEEDS_THREADS) |
| 77 | + message(FATAL_ERROR "Couldn't determine how to link against libpcap") |
| 78 | + endif(THREADS_FOUND AND PCAP_NEEDS_THREADS) |
| 79 | +endif(NOT PCAP_LINKS_SOLO) |
| 80 | + |
| 81 | +include(CheckFunctionExists) |
| 82 | +set(CMAKE_REQUIRED_LIBRARIES ${PCAP_LIBRARY}) |
| 83 | +check_function_exists(pcap_set_immediate_mode HAVE_PCAP_IMMEDIATE_MODE) |
| 84 | +check_function_exists(pcap_setdirection HAVE_PCAP_DIRECTION) |
| 85 | +check_function_exists(pcap_lib_version HAVE_PCAP_LIB_VERSION) |
| 86 | +set(CMAKE_REQUIRED_LIBRARIES) |
| 87 | + |
| 88 | +# Check libPCAP version |
| 89 | +if(HAVE_PCAP_LIB_VERSION AND NOT CMAKE_CROSSCOMPILING) |
| 90 | + # Simple C code to extract the libpcap version |
| 91 | + set(PCAP_VERSION_CODE |
| 92 | + " |
| 93 | + #include <stdio.h> |
| 94 | + #include <string.h> |
| 95 | + #include <pcap/pcap.h> |
| 96 | +
|
| 97 | + int main() { |
| 98 | + const char* version = pcap_lib_version(); |
| 99 | + const char* prefix = \"libpcap version \"; |
| 100 | + if (strncmp(version, prefix, strlen(prefix)) == 0) { |
| 101 | + version += strlen(prefix); |
| 102 | + } |
| 103 | + printf(\"%s\", version); |
| 104 | + return 0; |
| 105 | + } |
| 106 | + ") |
| 107 | + |
| 108 | + # Write the code to a temporary file |
| 109 | + set(detect_pcap_version_file "${PROJECT_BINARY_DIR}/detect_pcap_version.c") |
| 110 | + file(WRITE "${detect_pcap_version_file}" "${PCAP_VERSION_CODE}") |
| 111 | + |
| 112 | + # Try to compile and run the program |
| 113 | + try_run( |
| 114 | + RUN_RESULT_VAR |
| 115 | + COMPILE_RESULT_VAR |
| 116 | + "${CMAKE_BINARY_DIR}" |
| 117 | + "${detect_pcap_version_file}" |
| 118 | + CMAKE_FLAGS "-DINCLUDE_DIRECTORIES=${PCAP_INCLUDE_DIR}" LINK_LIBRARIES ${PCAP_LIBRARY} |
| 119 | + RUN_OUTPUT_VARIABLE PCAP_VERSION_OUTPUT) |
| 120 | + |
| 121 | + # If successful, parse the output to get the version string |
| 122 | + if(COMPILE_RESULT_VAR AND RUN_RESULT_VAR EQUAL 0) |
| 123 | + set(PCAP_VERSION ${PCAP_VERSION_OUTPUT}) |
| 124 | + endif() |
| 125 | +endif() |
| 126 | + |
| 127 | +include(FindPackageHandleStandardArgs) |
| 128 | +find_package_handle_standard_args( |
| 129 | + PCAP |
| 130 | + REQUIRED_VARS PCAP_LIBRARY PCAP_INCLUDE_DIR |
| 131 | + VERSION_VAR PCAP_VERSION) |
| 132 | + |
| 133 | +# create IMPORTED target for libpcap dependency |
| 134 | +if(NOT TARGET PCAP::PCAP) |
| 135 | + add_library(PCAP::PCAP IMPORTED SHARED) |
| 136 | + set_target_properties( |
| 137 | + PCAP::PCAP |
| 138 | + PROPERTIES IMPORTED_LOCATION ${PCAP_LIBRARY} |
| 139 | + IMPORTED_IMPLIB ${PCAP_LIBRARY} |
| 140 | + INTERFACE_INCLUDE_DIRECTORIES ${PCAP_INCLUDE_DIR}) |
| 141 | +endif() |
| 142 | + |
| 143 | +mark_as_advanced(PCAP_INCLUDE_DIR PCAP_LIBRARY) |
0 commit comments