]> The Tcpdump Group git mirrors - tcpdump/blob - cmake/Modules/FindPCAP.cmake
Add support for PCAP_DLL_DIR as a hint on Windows.
[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 if(WIN32)
102 #
103 # On Windows, we support PCAP_DLL_DIR being set to the path of
104 # a directory containing an SDK for {whatever}Pcap.
105 # XXX - is there a CMake convention for "look for package XXX
106 # here"?
107 #
108 # Try to find the header
109 find_path(PCAP_INCLUDE_DIR pcap.h HINTS ${PCAP_DLL_DIR})
110
111 # Try to find the library
112 find_library(PCAP_LIBRARY pcap HINTS ${PCAP_DLL_DIR})
113 else(WIN32)
114 # Try to find the header
115 find_path(PCAP_INCLUDE_DIR pcap.h)
116
117 # Try to find the library
118 find_library(PCAP_LIBRARY pcap)
119
120 # Try to find the static library (XXX - what about AIX?)
121 include(CMakePushCheckState)
122 cmake_push_check_state()
123 set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
124 find_library(PCAP_STATIC_LIBRARY pcap)
125 cmake_pop_check_state()
126 endif(WIN32)
127
128 set(PCAP_INCLUDE_DIRS ${PCAP_INCLUDE_DIR})
129 set(PCAP_LIBRARIES ${PCAP_LIBRARY})
130 set(PCAP_STATIC_LIBRARIES ${PCAP_STATIC_LIBRARY})
131 endif(PCAP_CONFIG)
132
133 include(FindPackageHandleStandardArgs)
134 find_package_handle_standard_args(PCAP
135 DEFAULT_MSG
136 PCAP_INCLUDE_DIR
137 PCAP_LIBRARY
138 )
139
140 mark_as_advanced(
141 PCAP_INCLUDE_DIR
142 PCAP_LIBRARY
143 PCAP_STATIC_LIBRARY
144 )