FindFLANN.cmake
Go to the documentation of this file.
1 #.rst:
2 # FindFLANN
3 # ---------
4 #
5 # Find Fast Library for Approximate Nearest Neighbors (FLANN).
6 # See http://www.cs.ubc.ca/research/flann for details.
7 #
8 # This module considers the following CMake variables set by find_package:
9 #
10 # ::
11 #
12 # FLANN_FIND_COMPONENTS - Names of requested components:
13 # cpp - shared or static C++ library
14 # cpp_shared - shared C++ library
15 # cpp_static - static C++ library
16 # c - shared or static C bindings library
17 # c_shared - shared C bindings library
18 # c_static - static C bindings library
19 # matlab - MATLAB bindings
20 # python - Python bindings
21 # FLANN_FIND_REQUIRED_<C> - Whether FLANN component <C> is required.
22 # FLANN is considered to be not found when at least
23 # one required library or its include path is missing.
24 # When no FLANN_FIND_COMPONENTS are specified,
25 # the static and shared C++ libraries are looked for.
26 # FLANN_FIND_REQUIRED - Raise FATAL_ERROR when required components not found.
27 # FLANN_FIND_QUIETLY - Suppress all other (status) messages.
28 #
29 # .. note::
30 #
31 # The "matlab" and "python" components are currently not supported yet.
32 #
33 # This module caches the following variables:
34 #
35 # ::
36 #
37 # FLANN_INCLUDE_DIR - Include path of C/C++ header files.
38 # FLANN_C_LIBRARY_SHARED - Path of shared C bindings link library.
39 # FLANN_C_LIBRARY_STATIC - Path of static C bindings link library.
40 # FLANN_CPP_LIBRARY_SHARED - Path of shared C++ link library.
41 # FLANN_CPP_LIBRARY_STATIC - Path of static c++ link library.
42 #
43 # It further defines the following uncached variables:
44 #
45 # ::
46 #
47 # FLANN_FOUND - Whether all required FLANN components were found.
48 # FLANN_<C>_FOUND - Whether library component <C> was found.
49 # FLANN_C_LIBRARY - Path of C bindings link library (shared preferred).
50 # FLANN_CPP_LIBRARY - Path of C++ link library (shared preferred).
51 # FLANN_LIBRARIES - Paths of all found libraries (shared preferred).
52 # FLANN_VERSION - Version for use in VERSION_LESS et al. comparisons.
53 # FLANN_VERSION_MAJOR - Major library version number.
54 # FLANN_VERSION_MINOR - Minor library version number.
55 # FLANN_VERSION_PATCH - Patch library version number.
56 # FLANN_VERSION_STRING - Version string for output messages.
57 
58 #=============================================================================
59 # Copyright 2016 Andreas Schuh <andreas.schuh.84@gmail.com>
60 #
61 # Distributed under the OSI-approved BSD License (the "License");
62 # see accompanying file Copyright.txt for details.
63 #
64 # This software is distributed WITHOUT ANY WARRANTY; without even the
65 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
66 # See the License for more information.
67 #=============================================================================
68 # (To distribute this file outside of CMake, substitute the full
69 # License text for the above reference.)
70 
71 if (NOT FLANN_FIND_QUIETLY)
72  set(_FLANN_FIND_STATUS "Looking for FLANN")
74  set(_FLANN_FIND_STATUS "${_FLANN_FIND_STATUS} [${FLANN_FIND_COMPONENTS}]")
75  endif ()
76  if (NOT FLANN_FIND_REQUIRED)
77  set(_FLANN_FIND_STATUS "${_FLANN_FIND_STATUS} (optional)")
78  endif ()
79  message(STATUS "${_FLANN_FIND_STATUS}...")
80 endif ()
81 
82 # ------------------------------------------------------------------------------
83 # Components
84 set(_FLANN_COMPONENTS c c_shared c_static cpp cpp_shared cpp_static matlab python)
85 foreach (_FLANN_COMPONENT IN LISTS _FLANN_COMPONENTS)
86  set(_FLANN_FIND_${_FLANN_COMPONENT} FALSE)
87 endforeach ()
88 
89 if (NOT FLANN_FIND_COMPONENTS)
90  set(FLANN_FIND_COMPONENTS cpp)
91  set(FLANN_FIND_REQUIRED_cpp ${FLANN_FIND_REQUIRED})
92 endif ()
93 
94 foreach (_FLANN_COMPONENT IN LISTS FLANN_FIND_COMPONENTS)
95  if (_FLANN_COMPONENT MATCHES "^c$")
96  set(_FLANN_FIND_c TRUE)
97  elseif (_FLANN_COMPONENT MATCHES "^(c_shared|flann)$")
99  elseif (_FLANN_COMPONENT MATCHES "^(c_static|flann_s)$")
101  elseif (_FLANN_COMPONENT MATCHES "^cpp$")
102  set(_FLANN_FIND_cpp TRUE)
103  elseif (_FLANN_COMPONENT MATCHES "^(cpp_shared|flann_cpp)$")
105  elseif (_FLANN_COMPONENT MATCHES "^(cpp_static|cpp_s|flann_cpp_s)$")
107  elseif (_FLANN_COMPONENT MATCHES "^(matlab|mex)$")
108  message(FATAL_ERROR "FLANN library component \"${_FLANN_COMPONENT}\" not supported yet")
109  elseif (_FLANN_COMPONENT MATCHES "^python$")
110  message(FATAL_ERROR "FLANN library component \"python\" not supported yet")
111  else ()
112  message(FATAL_ERROR "Unknown FLANN library component: ${_FLANN_COMPONENT}\n"
113  "Valid component names are: c, c_shared|flann, c_static|flann_s, cpp_shared|flann_cpp, cpp_static|cpp_s|flann_cpp_s, matlab|mex, python")
114  endif ()
115 endforeach ()
116 
117 if (FLANN_DEBUG)
118  message("** FindFLANN: Components = [${FLANN_FIND_COMPONENTS}]")
119  foreach (_FLANN_COMPONENT IN LISTS _FLANN_COMPONENTS)
120  message("** FindFLANN: - Find component ${_FLANN_COMPONENT} = ${_FLANN_FIND_${_FLANN_COMPONENT}}")
121  endforeach ()
122 endif ()
123 
124 # ------------------------------------------------------------------------------
125 # Construct a set of search paths
126 set(_FLANN_INC_DIR_HINTS)
127 set(_FLANN_LIB_DIR_HINTS)
128 
129 if (NOT FLANN_ROOT)
130  file(TO_CMAKE_PATH "$ENV{FLANN_ROOT}" FLANN_ROOT)
131 endif ()
132 
133 find_package(PkgConfig QUIET)
134 if (PkgConfig_FOUND)
135  pkg_check_modules(_FLANN flann QUIET)
136  if (_FLANN_INCLUDEDIR)
137  list(APPEND _FLANN_INC_DIR_HINTS ${_FLANN_INCLUDEDIR})
138  endif ()
139  if (_FLANN_INCLUDE_DIRS)
140  list(APPEND _FLANN_INC_DIR_HINTS ${_FLANN_INCLUDE_DIRS})
141  endif ()
142  if (_FLANN_LIBDIR)
143  list(APPEND _FLANN_LIB_DIR_HINTS ${_FLANN_LIBDIR})
144  endif ()
145  if (_FLANN_LIBRARY_DIRS)
146  list(APPEND _FLANN_LIB_DIR_HINTS ${_FLANN_LIBRARY_DIRS})
147  endif ()
148  unset(_FLANN_INCLUDEDIR)
149  unset(_FLANN_INCLUDE_DIRS)
150  unset(_FLANN_LIBDIR)
151  unset(_FLANN_LIBRARY_DIRS)
152  unset(_FLANN_CFLAGS_OTHER)
153 endif ()
154 
155 if (FLANN_DEBUG)
156  message("** FindFLANN: Initial search paths:")
157  message("** FindFLANN: - Root directory hints = [${FLANN_ROOT}]")
158  message("** FindFLANN: - PkgConfig include path = [${_FLANN_INC_DIR_HINTS}]")
159  message("** FindFLANN: - PkgConfig library path = [${_FLANN_LIB_DIR_HINTS}]")
160 endif ()
161 
162 # ------------------------------------------------------------------------------
163 # Find common include directory
164 #
165 # Looking for flann/config.h because we use this path later to read this file
166 # in order to extract the version information.
167 find_path(FLANN_INCLUDE_DIR
168  NAMES flann/config.h
169  HINTS ${FLANN_ROOT} ${_FLANN_INC_DIR_HINTS}
170 )
171 
172 mark_as_advanced(FLANN_INCLUDE_DIR)
173 
174 # ------------------------------------------------------------------------------
175 # Derive FLANN_ROOT from FLANN_INCLUDE_DIR if unset
176 if (FLANN_INCLUDE_DIR AND NOT FLANN_ROOT)
177  get_filename_component(FLANN_ROOT "${FLANN_INCLUDE_DIR}" DIRECTORY)
178 endif ()
179 
180 if (FLANN_DEBUG)
181  message("** FindFLANN: After initial search of FLANN include path")
182  message("** FindFLANN: - FLANN_INCLUDE_DIR = ${FLANN_INCLUDE_DIR}")
183  message("** FindFLANN: - FLANN_ROOT = [${FLANN_ROOT}]")
184 endif ()
185 
186 # ------------------------------------------------------------------------------
187 # Find libraries
188 unset(FLANN_C_LIBRARY)
189 unset(FLANN_CPP_LIBRARY)
190 
191 set(FLANN_INCLUDE_DIRS ${FLANN_INCLUDE_DIR})
192 set(FLANN_LIBRARIES)
193 
194 foreach (_FLANN_COMPONENT IN LISTS FLANN_FIND_COMPONENTS)
195  set(FLANN_${_FLANN_COMPONENT}_FOUND FALSE)
196 endforeach ()
197 
198 if (FLANN_INCLUDE_DIR)
199 
200  # Shared C library
201  if (_FLANN_FIND_c OR _FLANN_FIND_c_shared)
202  find_library(FLANN_C_LIBRARY_SHARED
203  NAMES flann
204  HINTS ${FLANN_ROOT} ${_FLANN_LIB_DIR_HINTS}
205  )
206  if (FLANN_C_LIBRARY_SHARED)
207  set(FLANN_c_FOUND TRUE)
208  set(FLANN_c_shared_FOUND TRUE)
209  endif ()
210  mark_as_advanced(FLANN_C_LIBRARY_SHARED)
211  endif ()
212 
213  # Static C library
214  if (_FLANN_FIND_c OR _FLANN_FIND_c_static)
215  find_library(FLANN_C_LIBRARY_STATIC
216  NAMES flann_s
217  HINTS ${FLANN_ROOT} ${_FLANN_LIB_DIR_HINTS}
218  )
219  if (FLANN_C_LIBRARY_STATIC)
220  set(FLANN_c_FOUND TRUE)
221  set(FLANN_c_static_FOUND TRUE)
222  endif ()
223  mark_as_advanced(FLANN_C_LIBRARY_STATIC)
224  endif ()
225 
226  # Set FLANN_C_LIBRARY and add it to FLANN_LIBRARIES
227  if (FLANN_C_LIBRARY_SHARED)
228  set(FLANN_C_LIBRARY ${FLANN_C_LIBRARY_SHARED})
229  elseif (FLANN_C_LIBRARY_STATIC)
230  set(FLANN_C_LIBRARY ${FLANN_C_LIBRARY_STATIC})
231  endif ()
232  if (FLANN_C_LIBRARY)
233  list(APPEND FLANN_LIBRARIES ${FLANN_C_LIBRARY})
234  endif ()
235 
236  # Shared C++ library
237  if (_FLANN_FIND_cpp OR _FLANN_FIND_cpp_shared)
238  find_library(FLANN_CPP_LIBRARY_SHARED
239  NAMES flann_cpp
240  HINTS ${FLANN_ROOT} ${_FLANN_LIB_DIR_HINTS}
241  )
242  if (FLANN_CPP_LIBRARY_SHARED)
243  set(FLANN_cpp_FOUND TRUE)
244  set(FLANN_cpp_shared_FOUND TRUE)
245  endif ()
246  mark_as_advanced(FLANN_CPP_LIBRARY_SHARED)
247  endif ()
248 
249  # Static C++ library
250  if (_FLANN_FIND_cpp OR _FLANN_FIND_cpp_static)
251  find_library(FLANN_CPP_LIBRARY_STATIC
252  NAMES flann_cpp_s
253  HINTS ${FLANN_ROOT} ${_FLANN_LIB_DIR_HINTS}
254  )
255  if (FLANN_CPP_LIBRARY_STATIC)
256  set(FLANN_cpp_FOUND TRUE)
257  set(FLANN_cpp_static_FOUND TRUE)
258  endif ()
259  mark_as_advanced(FLANN_CPP_LIBRARY_STATIC)
260  endif ()
261 
262  # Set FLANN_CPP_LIBRARY and add it to FLANN_LIBRARIES
263  if (FLANN_CPP_LIBRARY_SHARED)
264  set(FLANN_CPP_LIBRARY ${FLANN_CPP_LIBRARY_SHARED})
265  elseif (FLANN_CPP_LIBRARY_STATIC)
266  set(FLANN_CPP_LIBRARY ${FLANN_CPP_LIBRARY_STATIC})
267  endif ()
268  if (FLANN_CPP_LIBRARY)
269  list(APPEND FLANN_LIBRARIES ${FLANN_CPP_LIBRARY})
270  endif ()
271 
272  if (FLANN_DEBUG)
273  message("** FindFLANN: C/C++ library paths:")
274  message("** FindFLANN: - FLANN_C_LIBRARY = ${FLANN_C_LIBRARY}")
275  message("** FindFLANN: - FLANN_C_LIBRARY_SHARED = ${FLANN_C_LIBRARY_SHARED}")
276  message("** FindFLANN: - FLANN_C_LIBRARY_STATIC = ${FLANN_C_LIBRARY_STATIC}")
277  message("** FindFLANN: - FLANN_CPP_LIBRARY = ${FLANN_CPP_LIBRARY}")
278  message("** FindFLANN: - FLANN_CPP_LIBRARY_SHARED = ${FLANN_CPP_LIBRARY_SHARED}")
279  message("** FindFLANN: - FLANN_CPP_LIBRARY_STATIC = ${FLANN_CPP_LIBRARY_STATIC}")
280  message("** FindFLANN: - FLANN_LIBRARIES = [${FLANN_LIBRARIES}]")
281  endif ()
282 endif ()
283 
284 # ------------------------------------------------------------------------------
285 # Extract library version from flann/config.h
286 if (FLANN_INCLUDE_DIR)
287  if (NOT DEFINED FLANN_VERSION_MAJOR OR
288  NOT DEFINED FLANN_VERSION_MINOR OR
289  NOT DEFINED FLANN_VERSION_PATCH)
290  file(READ "${FLANN_INCLUDE_DIR}/flann/config.h" _FLANN_CONFIG_CONTENTS LIMIT 2048)
291  if (_FLANN_CONFIG_CONTENTS MATCHES "#define FLANN_VERSION_? \"([0-9]+)\\.([0-9]+)\\.([0-9]+)\"")
292  set(FLANN_VERSION_MAJOR ${CMAKE_MATCH_1})
293  set(FLANN_VERSION_MINOR ${CMAKE_MATCH_2})
294  set(FLANN_VERSION_PATCH ${CMAKE_MATCH_3})
295  else ()
296  if (NOT FLANN_FIND_QUIETLY)
297  message(WARNING "Could not extract FLANN version numbers from: ${FLANN_INCLUDE_DIR}/flann/config.h")
298  endif ()
299  set(FLANN_VERSION_MAJOR 0)
300  set(FLANN_VERSION_MINOR 0)
301  set(FLANN_VERSION_PATCH 0)
302  endif ()
303  unset(_FLANN_CONFIG_CONTENTS)
304  endif ()
306  set(FLANN_VERSION_STRING "${FLANN_VERSION}")
307 else ()
308  unset(FLANN_VERSION)
309  unset(FLANN_VERSION_MAJOR)
310  unset(FLANN_VERSION_MINOR)
311  unset(FLANN_VERSION_PATCH)
312  unset(FLANN_VERSION_STRING)
313 endif ()
314 
315 if (FLANN_DEBUG)
316  message("** FindFLANN: Version information from ${FLANN_INCLUDE_DIR}/flann/config.h")
317  message("** FindFLANN: - FLANN_VERSION_STRING = ${FLANN_VERSION_STRING}")
318  message("** FindFLANN: - FLANN_VERSION_MAJOR = ${FLANN_VERSION_MAJOR}")
319  message("** FindFLANN: - FLANN_VERSION_MINOR = ${FLANN_VERSION_MINOR}")
320  message("** FindFLANN: - FLANN_VERSION_PATCH = ${FLANN_VERSION_PATCH}")
321 endif ()
322 
323 # ------------------------------------------------------------------------------
324 # Handle QUIET, REQUIRED, and [EXACT] VERSION arguments and set FLANN_FOUND
325 include(FindPackageHandleStandardArgs)
326 find_package_handle_standard_args(FLANN
327  REQUIRED_VARS FLANN_INCLUDE_DIR
328  VERSION_VAR FLANN_VERSION
329  HANDLE_COMPONENTS
330 )
331 
332 if (NOT FLANN_FIND_QUIETLY)
333  if (FLANN_FOUND)
334  message(STATUS "${_FLANN_FIND_STATUS}... - found v${FLANN_VERSION_STRING}")
335  else ()
336  message(STATUS "${_FLANN_FIND_STATUS}... - not found")
337  endif ()
338 endif ()
339 
340 # ------------------------------------------------------------------------------
341 # Unset local auxiliary variables
342 foreach (_FLANN_COMPONENT IN LISTS _FLANN_COMPONENTS)
343  unset(_FLANN_FIND_${_FLANN_COMPONENT})
344 endforeach ()
345 unset(_FLANN_COMPONENT)
346 unset(_FLANN_COMPONENTS)
347 unset(_FLANN_FIND_STATUS)
cmake _FLANN_FIND_c
Definition: FindFLANN.cmake:96
cmake _FLANN_FIND_cpp_shared
cmake _FLANN_COMPONENTS
Definition: FindFLANN.cmake:84
cmake FLANN_CPP_LIBRARY
cmake FLANN_VERSION_PATCH
cmake _FLANN_FIND_STATUS
Definition: FindFLANN.cmake:72
cmake FLANN_FIND_COMPONENTS
Definition: FindFLANN.cmake:90
cmake FLANN_VERSION_MAJOR
cmake _FLANN_FIND_cpp
cmake FLANN_FIND_REQUIRED_cpp
Definition: FindFLANN.cmake:91
cmake FLANN_C_LIBRARY
cmake FLANN_VERSION_MINOR
cmake _FLANN_FIND_c_static
cmake _FLANN_FIND_c_shared
Definition: FindFLANN.cmake:98
cmake _FLANN_FIND_cpp_static
cmake FLANN_VERSION
cmake FLANN_VERSION_STRING