FindOpenCV.cmake
Go to the documentation of this file.
1 ##############################################################################
2 # @file FindOpenCV.cmake
3 # @brief Find OpenCV Library (http://sourceforge.net/projects/opencvlibrary/)
4 #
5 # @par 1. Setup
6 #
7 # The following variables are optionally searched for defaults
8 # OpenCV_DIR: Base directory of OpenCv tree to use.
9 #
10 # @par 2. Variable
11 #
12 # The following are set after configuration is done:
13 # - OpenCV_FOUND
14 # - OpenCV_LIBS
15 # - OpenCV_INCLUDE_DIR
16 # - OpenCV_VERSION (OpenCV_VERSION_MAJOR, OpenCV_VERSION_MINOR, OpenCV_VERSION_PATCH)
17 #
18 #
19 # The following variables are used to maintain compatibility with other
20 # Find<Pkg>.cmake modules, including the FindOpenCV.cmake module of
21 # Jan Woetzel (2006/09, www.mip.informatik.uni-kiel.de/~jw):
22 # - OpenCV_INCLUDE_DIRS
23 # - OpenCV_LIBRARIES
24 # - OpenCV_LINK_DIRECTORIES
25 #
26 # @par 3. Version
27 #
28 # 2012/10/22 Andreas Schuh, Find OpenCV 2 also if OpenCVConfig.cmake missing.
29 # 2012/02/28 Andreas Schuh, Reimplemented module to work also for OpenCV 1.x.
30 # 2010/04/07 Benoit Rat, Correct a bug when OpenCVConfig.cmake is not found.
31 # 2010/03/24 Benoit Rat, Add compatibility for when OpenCVConfig.cmake is not found.
32 # 2010/03/22 Benoit Rat, Creation of the script.
33 #
34 #
35 # tested with:
36 # - OpenCV 2.1: MinGW, MSVC2008
37 # - OpenCV 2.0: MinGW, MSVC2008, GCC4
38 #
39 # @par 4. Licence
40 #
41 # LGPL 2.1 : GNU Lesser General Public License Usage
42 # Alternatively, this file may be used under the terms of the GNU Lesser
43 #
44 # General Public License version 2.1 as published by the Free Software
45 # Foundation and appearing in the file LICENSE.LGPL included in the
46 # packaging of this file. Please review the following information to
47 # ensure the GNU Lesser General Public License version 2.1 requirements
48 # will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
49 #
50 # @ingroup CMakeFindModules
51 ##############################################################################
52 
53 # ----------------------------------------------------------------------------
54 # initialize search
55 set (OpenCV_FOUND FALSE)
56 
57 # 1. set OpenCV_DIR from environment variables
58 if (NOT OpenCV_DIR)
59  if (DEFINED ENV{OpenCV_DIR})
60  set (OpenCV_DIR "$ENV{OpenCV_DIR}" CACHE PATH "Installation prefix of OpenCV Library." FORCE)
61  elseif (DEFINED ENV{OPENCV_DIR})
62  set (OpenCV_DIR "$ENV{OPENCV_DIR}" CACHE PATH "Installation prefix of OpenCV Library." FORCE)
63  endif ()
64 endif ()
65 # 2. otherwise, try to derive it from include path
66 if (NOT OpenCV_DIR)
67  # a) look for include path which might be easily found using system default
68  # paths such as C_INCLUDE_PATH or CXX_INCLUDE_PATH
69  find_path (
70  OpenCV_INCLUDE_DIR "cv.h"
71  PATH_SUFFIXES "include" "include/opencv"
72  DOC "Directory of cv.h header file."
73  )
74  mark_as_advanced (OpenCV_INCLUDE_DIR)
75  # b) derive OpenCV_DIR from include path
76  if (OpenCV_INCLUDE_DIR)
77  # Mac OS Framework
78  string (REGEX REPLACE "/Headers(/.*)?$" "" OpenCV_DIR "${OpenCV_INCLUDE_DIR}")
79  # OpenCV 1
80  string (REGEX REPLACE "/include(/.*)$" "" OpenCV_DIR "${OpenCV_DIR}")
81  # OpenCV >= 2
82  if (EXISTS "${OpenCV_DIR}/share/opencv/OpenCVConfig.cmake")
83  set (OpenCV_DIR "${OpenCV_DIR}/share/opencv")
84  endif ()
85  # cache it such that users can view/correct it
86  set (OpenCV_DIR "${OpenCV_DIR}" CACHE PATH "Installation prefix of OpenCV Library." FORCE)
87  endif ()
88 endif ()
89 
90 set (OpenCV_LIBS) # found libraries
91 set (OpenCV_COMPONENTS_REQUIRED) # requested components
92 set (OpenCV_LIB_COMPONENTS) # found components
93 set (OpenCV_VERSION) # found version
94 
95 # ----------------------------------------------------------------------------
96 # find headers and libraries
97 if (EXISTS "${OpenCV_DIR}")
98 
99  # --------------------------------------------------------------------------
100  # OpenCV 2
101  if (EXISTS "${OpenCV_DIR}/OpenCVConfig.cmake")
102 
103  include ("${OpenCV_DIR}/OpenCVConfig.cmake")
104 
105  foreach (__CVLIB IN LISTS OpenCV_COMPONENTS)
106  if (NOT __CVLIB MATCHES "^opencv_")
107  set (__CVLIB "opencv_${__CVLIB}")
108  endif ()
109  list (APPEND OpenCV_COMPONENTS_REQUIRED "${__CVLIB}")
110  endforeach ()
111 
112  # Note that OpenCV 2.0.0 does only call the command include_directories()
113  # but does not set OpenCV_INCLUDE_DIRS. This variable was added to
114  # OpenCVConfig.cmake since version 2.1.0 of OpenCV.
115  get_directory_property (__INCLUDE_DIRS INCLUDE_DIRECTORIES)
116  find_path (
117  OpenCV_INCLUDE_DIR "cv.h"
118  HINTS ${__INCLUDE_DIRS}
119  DOC "Directory of cv.h header file."
120  NO_DEFAULT_PATH
121  )
122  mark_as_advanced (OpenCV_INCLUDE_DIR)
123  unset (__INCLUDE_DIRS)
124 
125  # --------------------------------------------------------------------------
126  # OpenCV 1 (or OpenCV 2 with missing OpenCVConfig.cmake file)
127  else ()
128 
129  # will be adjusted on Unix to find the correct library version
130  set (OpenCV_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES "${CMAKE_FIND_LIBRARY_SUFFIXES}")
131 
132  # find include directory
133  find_path (
134  OpenCV_INCLUDE_DIR "cv.h"
135  PATHS "${OpenCV_DIR}"
136  PATH_SUFFIXES "include" "include/opencv"
137  DOC "Directory of cv.h header file."
138  NO_DEFAULT_PATH
139  )
140 
141  mark_as_advanced (OpenCV_INCLUDE_DIR)
142 
143  if (EXISTS ${OpenCV_INCLUDE_DIR})
144  # should not be done by Find module, but OpenCVConfig.cmake does it
145  # as well, unfortunately...
146  include_directories (${OpenCV_INCLUDE_DIR})
147  # extract version information from header file
148  if (EXISTS "${OpenCV_INCLUDE_DIR}/cvver.h")
149  file (STRINGS "${OpenCV_INCLUDE_DIR}/cvver.h" OpenCV_VERSIONS_TMP REGEX "^#define CV_[A-Z]+_VERSION[ \t]+[0-9]+$")
150  elseif (EXISTS "${OpenCV_INCLUDE_DIR}/../opencv2/core/version.hpp")
151  file (STRINGS "${OpenCV_INCLUDE_DIR}/../opencv2/core/version.hpp" OpenCV_VERSIONS_TMP REGEX "^#define CV_[A-Z]+_VERSION[ \t]+[0-9]+$")
152  else ()
153  message (FATAL_ERROR "Missing ${OpenCV_INCLUDE_DIR}/cvver.h or ${OpenCV_INCLUDE_DIR}/../opencv2/core/version.hpp file!")
154  endif ()
155  string (REGEX REPLACE ".*#define CV_MAJOR_VERSION[ \t]+([0-9]+).*" "\\1" OpenCV_VERSION_MAJOR ${OpenCV_VERSIONS_TMP})
156  string (REGEX REPLACE ".*#define CV_MINOR_VERSION[ \t]+([0-9]+).*" "\\1" OpenCV_VERSION_MINOR ${OpenCV_VERSIONS_TMP})
157  string (REGEX REPLACE ".*#define CV_SUBMINOR_VERSION[ \t]+([0-9]+).*" "\\1" OpenCV_VERSION_PATCH ${OpenCV_VERSIONS_TMP})
158  set (OpenCV_VERSION "${OpenCV_VERSION_MAJOR}.${OpenCV_VERSION_MINOR}.${OpenCV_VERSION_PATCH}")
159  # file name suffixes
160  if (UNIX)
161  set (OpenCV_CVLIB_NAME_SUFFIX)
162  set (CMAKE_FIND_LIBRARY_SUFFIXES)
163  foreach (SUFFIX IN LISTS OpenCV_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES)
164  if (NOT SUFFIX MATCHES "\\.${OpenCV_VERSION_MAJOR}\\.${OpenCV_VERSION_MINOR}\\.${OpenCV_VERSION_PATCH}$")
165  set (SUFFIX "${SUFFIX}.${OpenCV_VERSION}")
166  endif ()
167  list (APPEND CMAKE_FIND_LIBRARY_SUFFIXES "${SUFFIX}")
168  endforeach ()
169  # for the 1.1pre1 version, the suffix of the libraries is by default .2.0.0 instead of .1.1.0
170  # thus consider these library files as well, assuming that the suffix has not been corrected
171  if (OpenCV_VERSION VERSION_EQUAL 1.1.0)
172  foreach (SUFFIX IN LISTS OpenCV_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES)
173  if (NOT SUFFIX MATCHES "\\.2\\.0\\.0$")
174  list (APPEND CMAKE_FIND_LIBRARY_SUFFIXES "${SUFFIX}.2.0.0")
175  endif ()
176  endforeach ()
177  endif ()
178  else ()
179  set (OpenCV_CVLIB_NAME_SUFFIX "${OpenCV_VERSION_MAJOR}${OpenCV_VERSION_MINOR}${OpenCV_VERSION_PATCH}")
180  endif ()
181  endif ()
182 
183  # library components
184  if (OpenCV_VERSION_MAJOR GREATER 1)
185  set (OpenCV_LIB_COMPONENTS core ml video calib3d contrib features2d flann gpu highgui imgproc objdetect legacy)
186  else ()
187  set (OpenCV_LIB_COMPONENTS cxcore cv ml highgui cvaux)
188  endif ()
189 
190  if (OpenCV_COMPONENTS)
191  foreach (__CVLIB IN LISTS OpenCV_COMPONENTS)
192  string (REGEX REPLACE "^opencv_" "" __CVLIB__ "${__CVLIB}")
193  list (FIND OpenCV_LIB_COMPONENTS ${__CVLIB__} IDX)
194  if (IDX EQUAL -1)
195  message (FATAL_ERROR "Unknown OpenCV library component: ${__CVLIB}"
196  " Are you looking for OpenCV 2.0.0 or greater?"
197  " In this case, please set OpenCV_DIR to the"
198  " directory containing the OpenCVConfig.cmake file.")
199  endif ()
200  list (APPEND OpenCV_COMPONENTS_REQUIRED "${__CVLIB__}")
201  endforeach ()
202  else ()
203  set (OpenCV_COMPONENTS_REQUIRED ${OpenCV_LIB_COMPONENTS})
204  endif ()
205 
206  # find libraries of components
207  set (OpenCV_LIB_COMPONENTS)
208  foreach (__CVLIB IN LISTS OpenCV_COMPONENTS_REQUIRED)
209 
210  # debug build
211  find_library (
212  OpenCV_${__CVLIB}_LIBRARY_DEBUG
214  PATHS "${OpenCV_DIR}/lib"
215  NO_DEFAULT_PATH
216  )
217 
218  # release build
219  if (APPLE AND OpenCV_DIR MATCHES "/OpenCV\\.framework/*$" AND EXISTS "${OpenCV_DIR}/OpenCV" AND NOT IS_DIRECTORY "${OpenCV_DIR}/OpenCV")
220  find_file (
221  OpenCV_${__CVLIB}_LIBRARY_RELEASE
222  NAMES OpenCV
223  PATHS "${OpenCV_DIR}"
224  NO_DEFAULT_PATH
225  )
226  else ()
227  find_library (
228  OpenCV_${__CVLIB}_LIBRARY_RELEASE
229  NAMES "opencv_${__CVLIB}${OpenCV_CVLIB_NAME_SUFFIX}" "${__CVLIB}${OpenCV_CVLIB_NAME_SUFFIX}"
230  PATHS "${OpenCV_DIR}/lib"
231  NO_DEFAULT_PATH
232  )
233  endif ()
234 
235  mark_as_advanced (OpenCV_${__CVLIB}_LIBRARY_DEBUG)
236  mark_as_advanced (OpenCV_${__CVLIB}_LIBRARY_RELEASE)
237 
238  # both debug/release
239  if (OpenCV_${__CVLIB}_LIBRARY_DEBUG AND OpenCV_${__CVLIB}_LIBRARY_RELEASE)
240  set (OpenCV_${__CVLIB}_LIBRARY debug ${OpenCV_${__CVLIB}_LIBRARY_DEBUG} optimized ${OpenCV_${__CVLIB}_LIBRARY_RELEASE})
241  # only debug
242  elseif (OpenCV_${__CVLIB}_LIBRARY_DEBUG)
243  set (OpenCV_${__CVLIB}_LIBRARY ${OpenCV_${__CVLIB}_LIBRARY_DEBUG})
244  # only release
245  elseif (OpenCV_${__CVLIB}_LIBRARY_RELEASE)
246  set (OpenCV_${__CVLIB}_LIBRARY ${OpenCV_${__CVLIB}_LIBRARY_RELEASE})
247  # not found
248  else ()
249  set (OpenCV_${__CVLIB}_LIBRARY)
250  endif()
251 
252  # add to list of found libraries
253  if (OpenCV_${__CVLIB}_LIBRARY)
254  list (APPEND OpenCV_LIB_COMPONENTS ${__CVLIB})
255  list (APPEND OpenCV_LIBS "${OpenCV_${__CVLIB}_LIBRARY}")
256  endif ()
257 
258  endforeach ()
259 
260  # restore library suffixes
261  set (CMAKE_FIND_LIBRARY_SUFFIXES "${OpenCV_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES}")
262 
263  # compatibility with OpenCV 2
264  set (OpenCV_INCLUDE_DIRS "${OpenCV_INCLUDE_DIR}")
265 
266  endif ()
267 
268  # --------------------------------------------------------------------------
269  # set OpenCV_INCLUDE_DIRS - required for OpenCV before version 2.1.0
270  if (OpenCV_INCLUDE_DIR MATCHES "/opencv$" AND NOT OpenCV_INCLUDE_DIRS)
271  get_filename_component (OpenCV_INCLUDE_DIRS "${OpenCV_INCLUDE_DIR}" PATH)
272  list (APPEND OpenCV_INCLUDE_DIRS "${OpenCV_INCLUDE_DIR}")
273  endif ()
274 
275  # --------------------------------------------------------------------------
276  # handle the QUIETLY and REQUIRED arguments and set *_FOUND to TRUE
277  # if all listed variables are found or TRUE
278  include (FindPackageHandleStandardArgs)
279 
280  set (OpenCV_REQUIRED_COMPONENTS_FOUND TRUE)
281  set (OpenCV_COMPONENTS_NOT_FOUND)
282  foreach (__CVLIB IN LISTS OpenCV_COMPONENTS_REQUIRED)
283  list (FIND OpenCV_LIB_COMPONENTS ${__CVLIB} IDX)
284  if (IDX EQUAL -1)
285  set (OpenCV_REQUIRED_COMPONENTS_FOUND FALSE)
286  list (APPEND OpenCV_COMPONENTS_NOT_FOUND ${__CVLIB})
287  endif ()
288  endforeach ()
289 
290  if (NOT OpenCV_REQUIRED_COMPONENTS_FOUND)
291  if (NOT OpenCV_FIND_QUIET AND OpenCV_FIND_REQUIRED)
292  message (FATAL_ERROR "The following required OpenCV components"
293  " were not found: ${OpenCV_COMPONENTS_NOT_FOUND}")
294  endif ()
295  endif ()
296 
297  find_package_handle_standard_args (
298  OpenCV
299  REQUIRED_VARS
300  OpenCV_INCLUDE_DIR
301  OpenCV_LIBS
302  OpenCV_REQUIRED_COMPONENTS_FOUND
303  VERSION_VAR
304  OpenCV_VERSION
305  )
306 
307  set (OpenCV_FOUND "${OPENCV_FOUND}")
308 
309  # --------------------------------------------------------------------------
310  # (backward) compatibility
311  if (OpenCV_FOUND)
312  set (OpenCV_LIBRARIES "${OpenCV_LIBS}")
313  endif ()
314 
315 elseif (NOT OpenCV_FIND_QUIET AND OpenCV_FIND_REQUIRED)
316  message (FATAL_ERROR "Please specify the OpenCV directory using OpenCV_DIR (environment) variable.")
317 endif ()
cmake __CVLIB
cmake OpenCV_CVLIB_NAME_SUFFIX
cmake OpenCV_DIR
cmake OpenCV_FOUND
cmake CMAKE_FIND_LIBRARY_SUFFIXES
cmake OpenCV_LIB_COMPONENTS
cmake OpenCV_COMPONENTS_REQUIRED
cmake OpenCV_VERSION
cmake SUFFIX
if(oldcoutbuf)