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