]> The Tcpdump Group git mirrors - tcpdump/blob - cmake/Modules/FindPCAP.cmake
Updated 802.15.4 code
[tcpdump] / cmake / Modules / FindPCAP.cmake
1 #
2 # Try to find libpcap.
3 #
4
5 find_program(PCAP_CONFIG pcap-config)
6 if(PCAP_CONFIG)
7 #
8 # We have pcap-config; use it.
9 # XXX - what if this is on Windows? If you're using, for example,
10 # MinGW, that might be the right thing to do, *if* pcap-config
11 # were made to work properly on Windows, but what about MSVC?
12 #
13 # First, get the include directory.
14 #
15 execute_process(COMMAND "${PCAP_CONFIG}" "--cflags"
16 RESULT_VARIABLE PCAP_CONFIG_RESULT
17 OUTPUT_VARIABLE PCAP_CONFIG_OUTPUT
18 OUTPUT_STRIP_TRAILING_WHITESPACE
19 )
20 if(NOT PCAP_CONFIG_RESULT EQUAL 0)
21 message(FATAL_ERROR "pcap-config --cflags failed")
22 endif()
23 #
24 # XXX - this assumes that there's only one -I flag in the output
25 # of pcap-config --cflags. That *should* be the case.
26 #
27 string(REGEX REPLACE "-I" "" PCAP_INCLUDE_DIRS "${PCAP_CONFIG_OUTPUT}")
28 set(PCAP_INCLUDE_DIR ${PCAP_INCLUDE_DIRS})
29
30 # Now, get the libraries.
31 execute_process(COMMAND "${PCAP_CONFIG}" "--libs"
32 RESULT_VARIABLE PCAP_CONFIG_RESULT
33 OUTPUT_VARIABLE PCAP_CONFIG_OUTPUT
34 OUTPUT_STRIP_TRAILING_WHITESPACE
35 )
36 if(NOT PCAP_CONFIG_RESULT EQUAL 0)
37 message(FATAL_ERROR "pcap-config --libs failed")
38 endif()
39 separate_arguments(LIBS_LIST UNIX_COMMAND ${PCAP_CONFIG_OUTPUT})
40 set(_pcap_library_dirs)
41 set(PCAP_LIBRARIES)
42 foreach(_arg IN LISTS LIBS_LIST)
43 if(_arg MATCHES "^-L")
44 # Add this directory to _pcap_library_dirs
45 string(REGEX REPLACE "-L" "" _dir ${_arg})
46 list(APPEND _pcap_library_dirs ${_dir})
47 elseif(_arg MATCHES "^-l")
48 string(REGEX REPLACE "-l" "" _lib ${_arg})
49 #
50 # Try to find that library, so we get its full path.
51 # CMake *really* doesn't like the notion of specifying "here are
52 # the directories in which to look for libraries" except in
53 # find_library() calls; it *really* prefers using full paths to
54 # library files, rather than library names.
55 #
56 find_library(_libfullpath ${_lib} HINTS ${__pcap_library_dirs})
57 list(APPEND PCAP_LIBRARIES ${_libfullpath})
58 endif()
59 endforeach()
60
61 # Now, get the library directories and libraries for static linking.
62 execute_process(COMMAND "${PCAP_CONFIG}" "--libs" "--static"
63 RESULT_VARIABLE PCAP_CONFIG_RESULT
64 OUTPUT_VARIABLE PCAP_CONFIG_OUTPUT
65 )
66 if(NOT PCAP_CONFIG_RESULT EQUAL 0)
67 message(FATAL_ERROR "pcap-config --libs --static failed")
68 endif()
69 separate_arguments(LIBS_LIST UNIX_COMMAND ${PCAP_CONFIG_OUTPUT})
70 set(_pcap_static_library_dirs)
71 set(PCAP_STATIC_LIBRARIES)
72 foreach(_arg IN LISTS LIBS_LIST)
73 if(_arg MATCHES "^-L")
74 # Add this directory to _pcap_static_library_dirs
75 string(REGEX REPLACE "-L" "" _dir ${_arg})
76 list(APPEND _pcap_static_library_dirs ${_dir})
77 elseif(_arg MATCHES "^-l")
78 string(REGEX REPLACE "-l" "" _lib ${_arg})
79 #
80 # Try to find that library, so we get its full path, as
81 # we do with dynamic libraries.
82 #
83 find_library(_libfullpath ${_lib} HINTS ${__pcap_static_library_dirs})
84 list(APPEND PCAP_STATIC_LIBRARIES ${_libfullpath})
85 endif()
86 endforeach()
87
88 # Try to find the header
89 find_path(PCAP_INCLUDE_DIR pcap.h HINTS ${PCAP_INCLUDE_DIRS})
90
91 # Try to find the library
92 find_library(PCAP_LIBRARY pcap HINTS ${_pcap_library_dirs})
93
94 # Try to find the static library (XXX - what about AIX?)
95 include(CMakePushCheckState)
96 cmake_push_check_state()
97 set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
98 find_library(PCAP_STATIC_LIBRARY pcap HINTS ${_pcap_static_library_dirs})
99 cmake_pop_check_state()
100 else(PCAP_CONFIG)
101 # Try to find the header
102 find_path(PCAP_INCLUDE_DIR pcap.h)
103
104 # Try to find the library
105 if(WIN32)
106 # The 64-bit Packet.lib is located under /x64
107 if(CMAKE_SIZEOF_VOID_P EQUAL 8)
108 #
109 # For the WinPcap and Npcap SDKs, the Lib subdirectory of the top-level
110 # directory contains 32-bit libraries; the 64-bit libraries are in the
111 # Lib/x64 directory.
112 #
113 # The only way to *FORCE* CMake to look in the Lib/x64 directory
114 # without searching in the Lib directory first appears to be to set
115 # CMAKE_LIBRARY_ARCHITECTURE to "x64".
116 #
117 set(CMAKE_LIBRARY_ARCHITECTURE "x64")
118 endif()
119 endif()
120
121 find_library(PCAP_LIBRARY pcap)
122 if(WIN32)
123 if(NOT PCAP_LIBRARY)
124 #
125 # OK, look for it under the name wpcap.
126 #
127 find_library(PCAP_LIBRARY wpcap)
128 endif(NOT PCAP_LIBRARY)
129 endif(WIN32)
130 if(NOT WIN32)
131 # Try to find the static library (XXX - what about AIX?)
132 include(CMakePushCheckState)
133 cmake_push_check_state()
134 set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
135 find_library(PCAP_STATIC_LIBRARY pcap)
136 cmake_pop_check_state()
137 endif(NOT WIN32)
138
139 set(PCAP_INCLUDE_DIRS ${PCAP_INCLUDE_DIR})
140 set(PCAP_LIBRARIES ${PCAP_LIBRARY})
141 set(PCAP_STATIC_LIBRARIES ${PCAP_STATIC_LIBRARY})
142 endif(PCAP_CONFIG)
143
144 include(FindPackageHandleStandardArgs)
145 find_package_handle_standard_args(PCAP
146 DEFAULT_MSG
147 PCAP_INCLUDE_DIR
148 PCAP_LIBRARY
149 )
150
151 mark_as_advanced(
152 PCAP_INCLUDE_DIR
153 PCAP_LIBRARY
154 PCAP_STATIC_LIBRARY
155 )