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