1 ##############################################################################
2 # @file FindOpenCV.cmake
3 # @brief Find OpenCV Library (http://sourceforge.net/projects/opencvlibrary/)
7 # The following variables are optionally searched for defaults
8 # OpenCV_DIR: Base directory of OpenCv tree to use.
12 # The following are set after configuration is done:
15 # - OpenCV_INCLUDE_DIR
16 # - OpenCV_VERSION (OpenCV_VERSION_MAJOR, OpenCV_VERSION_MINOR, OpenCV_VERSION_PATCH)
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
24 # - OpenCV_LINK_DIRECTORIES
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.
36 # - OpenCV 2.1: MinGW, MSVC2008
37 # - OpenCV 2.0: MinGW, MSVC2008, GCC4
41 # LGPL 2.1 : GNU Lesser General Public License Usage
42 # Alternatively, this file may be used under the terms of the GNU Lesser
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.
50 # @ingroup CMakeFindModules
51 ##############################################################################
53 # ----------------------------------------------------------------------------
57 # 1. set OpenCV_DIR from environment variables
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)
65 # 2. otherwise, try to derive it from include path
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
70 OpenCV_INCLUDE_DIR
"cv.h"
71 PATH_SUFFIXES
"include" "include/opencv"
72 DOC
"Directory of cv.h header file."
74 mark_as_advanced (OpenCV_INCLUDE_DIR)
75 # b) derive OpenCV_DIR from include path
76 if (OpenCV_INCLUDE_DIR)
78 string (REGEX REPLACE
"/Headers(/.*)?$" "" OpenCV_DIR
"${OpenCV_INCLUDE_DIR}")
80 string (REGEX REPLACE
"/include(/.*)$" "" OpenCV_DIR
"${OpenCV_DIR}")
82 if (EXISTS
"${OpenCV_DIR}/share/opencv/OpenCVConfig.cmake")
83 set (OpenCV_DIR
"${OpenCV_DIR}/share/opencv")
85 # cache it such that users can view/correct it
86 set (OpenCV_DIR
"${OpenCV_DIR}" CACHE PATH
"Installation prefix of OpenCV Library." FORCE)
90 set (OpenCV_LIBS)
# found libraries
95 # ----------------------------------------------------------------------------
96 # find headers and libraries
97 if (EXISTS
"${OpenCV_DIR}")
99 # --------------------------------------------------------------------------
101 if (EXISTS
"${OpenCV_DIR}/OpenCVConfig.cmake")
103 include (
"${OpenCV_DIR}/OpenCVConfig.cmake")
105 foreach (
__CVLIB IN LISTS OpenCV_COMPONENTS)
109 list (APPEND OpenCV_COMPONENTS_REQUIRED "${
__CVLIB}
")
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)
117 OpenCV_INCLUDE_DIR "cv.h
"
118 HINTS ${__INCLUDE_DIRS}
119 DOC "Directory of cv.h header file.
"
122 mark_as_advanced (OpenCV_INCLUDE_DIR)
123 unset (__INCLUDE_DIRS)
125 # --------------------------------------------------------------------------
126 # OpenCV 1 (or OpenCV 2 with missing OpenCVConfig.cmake file)
129 # will be adjusted on Unix to find the correct library version
132 # find include directory
134 OpenCV_INCLUDE_DIR "cv.h
"
135 PATHS "${OpenCV_DIR}
"
136 PATH_SUFFIXES "include
" "include/opencv
"
137 DOC "Directory of cv.h header file.
"
141 mark_as_advanced (OpenCV_INCLUDE_DIR)
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]+$
")
153 message (FATAL_ERROR "Missing ${OpenCV_INCLUDE_DIR}/cvver.h or ${OpenCV_INCLUDE_DIR}/../opencv2/core/version.hpp file!
")
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}
")
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}$
")
167 list (APPEND CMAKE_FIND_LIBRARY_SUFFIXES "${
SUFFIX}
")
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
")
179 set (OpenCV_CVLIB_NAME_SUFFIX "${OpenCV_VERSION_MAJOR}${OpenCV_VERSION_MINOR}${OpenCV_VERSION_PATCH}
")
184 if (OpenCV_VERSION_MAJOR GREATER 1)
185 set (OpenCV_LIB_COMPONENTS core ml video calib3d contrib features2d flann gpu highgui imgproc objdetect legacy)
187 set (OpenCV_LIB_COMPONENTS cxcore cv ml highgui cvaux)
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)
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.
")
200 list (APPEND OpenCV_COMPONENTS_REQUIRED "${__CVLIB__}
")
203 set (OpenCV_COMPONENTS_REQUIRED ${OpenCV_LIB_COMPONENTS})
206 # find libraries of components
207 set (OpenCV_LIB_COMPONENTS)
208 foreach (__CVLIB IN LISTS OpenCV_COMPONENTS_REQUIRED)
212 OpenCV_${__CVLIB}_LIBRARY_DEBUG
214 PATHS "${OpenCV_DIR}/lib
"
219 if (APPLE AND OpenCV_DIR MATCHES "/OpenCV\\.framework
cmake OpenCV_CVLIB_NAME_SUFFIX
cmake CMAKE_FIND_LIBRARY_SUFFIXES
cmake OpenCV_LIB_COMPONENTS
cmake OpenCV_COMPONENTS_REQUIRED