summaryrefslogtreecommitdiff
path: root/cmake/Modules/FindPCAP.cmake
blob: 63597a1e6839cfa4173634502f11e92000622d59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#
# Try to find libpcap.
#

find_program(PCAP_CONFIG pcap-config)
if(PCAP_CONFIG)
  # We have pcap-config; use it.
  # First, get the include directory.
  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()
  #
  # XXX - this assumes that there's only one -I flag in the output
  # of pcap-config --cflags.  That *should* be the case.
  #
  string(REGEX REPLACE "-I" "" PCAP_INCLUDE_DIRS ${PCAP_CONFIG_OUTPUT})
  set(PCAP_INCLUDE_DIR ${PCAP_INCLUDE_DIRS})

  # Now, get the libraries.
  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(_pcap_library_dirs)
  set(PCAP_LIBRARIES)
  foreach(_arg IN LISTS LIBS_LIST)
    if(_arg MATCHES "^-L")
      # Add this directory to _pcap_library_dirs
      string(REGEX REPLACE "-L" "" _dir ${_arg})
      list(APPEND _pcap_library_dirs ${_dir})
    elseif(_arg MATCHES "^-l")
      string(REGEX REPLACE "-l" "" _lib ${_arg})
      #
      # Try to find that library, so we get its full path.
      # CMake *really* doesn't like the notion of specifying "here are
      # the directories in which to look for libraries" except in
      # find_library() calls; it *really* prefers using full paths to
      # library files, rather than library names.
      #
      find_library(_libfullpath ${_lib} HINTS ${__pcap_library_dirs})
      list(APPEND PCAP_LIBRARIES ${_libfullpath})
    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(_pcap_static_library_dirs)
  set(PCAP_STATIC_LIBRARIES)
  foreach(_arg IN LISTS LIBS_LIST)
    if(_arg MATCHES "^-L")
      # Add this directory to _pcap_static_library_dirs
      string(REGEX REPLACE "-L" "" _dir ${_arg})
      list(APPEND _pcap_static_library_dirs ${_dir})
    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.
      #
      find_library(_libfullpath ${_lib} HINTS ${__pcap_static_library_dirs})
      list(APPEND PCAP_STATIC_LIBRARIES ${_libfullpath})
    endif()
  endforeach()

  # Try to find the header
  find_path(PCAP_INCLUDE_DIR pcap.h HINTS ${PCAP_INCLUDE_DIRS})

  # Try to find the library
  find_library(PCAP_LIBRARY pcap HINTS ${_pcap_library_dirs})

  # Try to find the static library (XXX - what about AIX?)
  include(CMakePushCheckState)
  cmake_push_check_state()
  set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
  find_library(PCAP_STATIC_LIBRARY pcap HINTS ${_pcap_static_library_dirs})
  cmake_pop_check_state()
else(PCAP_CONFIG)
  # Try to find the header
  find_path(PCAP_INCLUDE_DIR pcap.h)

  # Try to find the library
  find_library(PCAP_LIBRARY pcap)

  # Try to find the static library (XXX - what about AIX?)
  include(CMakePushCheckState)
  cmake_push_check_state()
  set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
  find_library(PCAP_STATIC_LIBRARY pcap)
  cmake_pop_check_state()

  set(PCAP_INCLUDE_DIRS ${PCAP_INCLUDE_DIR})
  set(PCAP_LIBRARIES ${PCAP_LIBRARY})
  set(PCAP_STATIC_LIBRARIES ${PCAP_STATIC_LIBRARY})
endif(PCAP_CONFIG)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(PCAP
  DEFAULT_MSG
  PCAP_INCLUDE_DIR
  PCAP_LIBRARY
)

mark_as_advanced(
  PCAP_INCLUDE_DIR
  PCAP_LIBRARY
  PCAP_STATIC_LIBRARY
)