]> The Tcpdump Group git mirrors - tcpdump/blob - cmake/Modules/FindPCAP.cmake
Set PCAP_INCLUDE_DIRS as well as PCAP_INCLUDE_DIR.
[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 # We have pcap-config; use it.
8 # First, get the include directory.
9 execute_process(COMMAND "${PCAP_CONFIG}" "--cflags"
10 RESULT_VARIABLE PCAP_CONFIG_RESULT
11 OUTPUT_VARIABLE PCAP_CONFIG_OUTPUT
12 OUTPUT_STRIP_TRAILING_WHITESPACE
13 )
14 if(NOT PCAP_CONFIG_RESULT EQUAL 0)
15 message(FATAL_ERROR "pcap-config --cflags failed")
16 endif()
17 #
18 # XXX - this assumes that there's only one -I flag in the output
19 # of pcap-config --cflags. That *should* be the case.
20 #
21 string(REGEX REPLACE "-I" "" PCAP_INCLUDE_DIRS ${PCAP_CONFIG_OUTPUT})
22 set(PCAP_INCLUDE_DIR ${PCAP_INCLUDE_DIRS})
23
24 # Now, get the libraries.
25 execute_process(COMMAND "${PCAP_CONFIG}" "--libs"
26 RESULT_VARIABLE PCAP_CONFIG_RESULT
27 OUTPUT_VARIABLE PCAP_CONFIG_OUTPUT
28 OUTPUT_STRIP_TRAILING_WHITESPACE
29 )
30 if(NOT PCAP_CONFIG_RESULT EQUAL 0)
31 message(FATAL_ERROR "pcap-config --libs failed")
32 endif()
33 separate_arguments(LIBS_LIST UNIX_COMMAND ${PCAP_CONFIG_OUTPUT})
34 set(_pcap_library_dirs)
35 set(PCAP_LIBRARIES)
36 foreach(_arg IN LISTS LIBS_LIST)
37 if(_arg MATCHES "^-L")
38 # Add this directory to _pcap_library_dirs
39 string(REGEX REPLACE "-L" "" _dir ${_arg})
40 list(APPEND _pcap_library_dirs ${_dir})
41 elseif(_arg MATCHES "^-l")
42 string(REGEX REPLACE "-l" "" _lib ${_arg})
43 #
44 # Try to find that library, so we get its full path.
45 # CMake *really* doesn't like the notion of specifying "here are
46 # the directories in which to look for libraries" except in
47 # find_library() calls; it *really* prefers using full paths to
48 # library files, rather than library names.
49 #
50 find_library(_libfullpath ${_lib} HINTS ${__pcap_library_dirs})
51 list(APPEND PCAP_LIBRARIES ${_libfullpath})
52 endif()
53 endforeach()
54
55 # Now, get the library directories and libraries for static linking.
56 execute_process(COMMAND "${PCAP_CONFIG}" "--libs" "--static"
57 RESULT_VARIABLE PCAP_CONFIG_RESULT
58 OUTPUT_VARIABLE PCAP_CONFIG_OUTPUT
59 )
60 if(NOT PCAP_CONFIG_RESULT EQUAL 0)
61 message(FATAL_ERROR "pcap-config --libs --static failed")
62 endif()
63 separate_arguments(LIBS_LIST UNIX_COMMAND ${PCAP_CONFIG_OUTPUT})
64 set(_pcap_static_library_dirs)
65 set(PCAP_STATIC_LIBRARIES)
66 foreach(_arg IN LISTS LIBS_LIST)
67 if(_arg MATCHES "^-L")
68 # Add this directory to _pcap_static_library_dirs
69 string(REGEX REPLACE "-L" "" _dir ${_arg})
70 list(APPEND _pcap_static_library_dirs ${_dir})
71 elseif(_arg MATCHES "^-l")
72 string(REGEX REPLACE "-l" "" _lib ${_arg})
73 #
74 # Try to find that library, so we get its full path, as
75 # we do with dynamic libraries.
76 #
77 find_library(_libfullpath ${_lib} HINTS ${__pcap_static_library_dirs})
78 list(APPEND PCAP_STATIC_LIBRARIES ${_libfullpath})
79 endif()
80 endforeach()
81
82 # Try to find the header
83 find_path(PCAP_INCLUDE_DIR pcap.h HINTS ${PCAP_INCLUDE_DIRS})
84
85 # Try to find the library
86 find_library(PCAP_LIBRARY pcap HINTS ${_pcap_library_dirs})
87
88 # Try to find the static library (XXX - what about AIX?)
89 include(CMakePushCheckState)
90 cmake_push_check_state()
91 set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
92 find_library(PCAP_STATIC_LIBRARY pcap HINTS ${_pcap_static_library_dirs})
93 cmake_pop_check_state()
94 else(PCAP_CONFIG)
95 # Try to find the header
96 find_path(PCAP_INCLUDE_DIR pcap.h)
97
98 # Try to find the library
99 find_library(PCAP_LIBRARY pcap)
100
101 # Try to find the static library (XXX - what about AIX?)
102 include(CMakePushCheckState)
103 cmake_push_check_state()
104 set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
105 find_library(PCAP_STATIC_LIBRARY pcap)
106 cmake_pop_check_state()
107
108 set(PCAP_INCLUDE_DIRS ${PCAP_INCLUDE_DIR})
109 set(PCAP_LIBRARIES ${PCAP_LIBRARY})
110 set(PCAP_STATIC_LIBRARIES ${PCAP_STATIC_LIBRARY})
111 endif(PCAP_CONFIG)
112
113 include(FindPackageHandleStandardArgs)
114 find_package_handle_standard_args(PCAP
115 DEFAULT_MSG
116 PCAP_INCLUDE_DIR
117 PCAP_LIBRARY
118 )
119
120 mark_as_advanced(
121 PCAP_INCLUDE_DIR
122 PCAP_LIBRARY
123 PCAP_STATIC_LIBRARY
124 )