FindOpenCV.cmake
Go to the documentation of this file.
00001 ############################################################################## 00002 # @file FindOpenCV.cmake 00003 # @brief Find OpenCV Library (http://sourceforge.net/projects/opencvlibrary/) 00004 # 00005 # @par 1. Setup 00006 # 00007 # The following variables are optionally searched for defaults 00008 # OpenCV_DIR: Base directory of OpenCv tree to use. 00009 # 00010 # @par 2. Variable 00011 # 00012 # The following are set after configuration is done: 00013 # - OpenCV_FOUND 00014 # - OpenCV_LIBS 00015 # - OpenCV_INCLUDE_DIR 00016 # - OpenCV_VERSION (OpenCV_VERSION_MAJOR, OpenCV_VERSION_MINOR, OpenCV_VERSION_PATCH) 00017 # 00018 # 00019 # The following variables are used to maintain compatibility with other 00020 # Find<Pkg>.cmake modules, including the FindOpenCV.cmake module of 00021 # Jan Woetzel (2006/09, www.mip.informatik.uni-kiel.de/~jw): 00022 # - OpenCV_INCLUDE_DIRS 00023 # - OpenCV_LIBRARIES 00024 # - OpenCV_LINK_DIRECTORIES 00025 # 00026 # @par 3. Version 00027 # 00028 # 2012/10/22 Andreas Schuh, Find OpenCV 2 also if OpenCVConfig.cmake missing. 00029 # 2012/02/28 Andreas Schuh, Reimplemented module to work also for OpenCV 1.x. 00030 # 2010/04/07 Benoit Rat, Correct a bug when OpenCVConfig.cmake is not found. 00031 # 2010/03/24 Benoit Rat, Add compatibility for when OpenCVConfig.cmake is not found. 00032 # 2010/03/22 Benoit Rat, Creation of the script. 00033 # 00034 # 00035 # tested with: 00036 # - OpenCV 2.1: MinGW, MSVC2008 00037 # - OpenCV 2.0: MinGW, MSVC2008, GCC4 00038 # 00039 # @par 4. Licence 00040 # 00041 # LGPL 2.1 : GNU Lesser General Public License Usage 00042 # Alternatively, this file may be used under the terms of the GNU Lesser 00043 # 00044 # General Public License version 2.1 as published by the Free Software 00045 # Foundation and appearing in the file LICENSE.LGPL included in the 00046 # packaging of this file. Please review the following information to 00047 # ensure the GNU Lesser General Public License version 2.1 requirements 00048 # will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. 00049 # 00050 # @ingroup CMakeFindModules 00051 ############################################################################## 00052 00053 # ---------------------------------------------------------------------------- 00054 # initialize search 00055 set (OpenCV_FOUND FALSE) 00056 00057 # 1. set OpenCV_DIR from environment variables 00058 if (NOT OpenCV_DIR) 00059 if (DEFINED ENV{OpenCV_DIR}) 00060 set (OpenCV_DIR "$ENV{OpenCV_DIR}" CACHE PATH "Installation prefix of OpenCV Library." FORCE) 00061 elseif (DEFINED ENV{OPENCV_DIR}) 00062 set (OpenCV_DIR "$ENV{OPENCV_DIR}" CACHE PATH "Installation prefix of OpenCV Library." FORCE) 00063 endif () 00064 endif () 00065 # 2. otherwise, try to derive it from include path 00066 if (NOT OpenCV_DIR) 00067 # a) look for include path which might be easily found using system default 00068 # paths such as C_INCLUDE_PATH or CXX_INCLUDE_PATH 00069 find_path ( 00070 OpenCV_INCLUDE_DIR "cv.h" 00071 PATH_SUFFIXES "include" "include/opencv" 00072 DOC "Directory of cv.h header file." 00073 ) 00074 mark_as_advanced (OpenCV_INCLUDE_DIR) 00075 # b) derive OpenCV_DIR from include path 00076 if (OpenCV_INCLUDE_DIR) 00077 # Mac OS Framework 00078 string (REGEX REPLACE "/Headers(/.*)?$" "" OpenCV_DIR "${OpenCV_INCLUDE_DIR}") 00079 # OpenCV 1 00080 string (REGEX REPLACE "/include(/.*)$" "" OpenCV_DIR "${OpenCV_DIR}") 00081 # OpenCV >= 2 00082 if (EXISTS "${OpenCV_DIR}/share/opencv/OpenCVConfig.cmake") 00083 set (OpenCV_DIR "${OpenCV_DIR}/share/opencv") 00084 endif () 00085 # cache it such that users can view/correct it 00086 set (OpenCV_DIR "${OpenCV_DIR}" CACHE PATH "Installation prefix of OpenCV Library." FORCE) 00087 endif () 00088 endif () 00089 00090 set (OpenCV_LIBS) # found libraries 00091 set (OpenCV_COMPONENTS_REQUIRED) # requested components 00092 set (OpenCV_LIB_COMPONENTS) # found components 00093 set (OpenCV_VERSION) # found version 00094 00095 # ---------------------------------------------------------------------------- 00096 # find headers and libraries 00097 if (EXISTS "${OpenCV_DIR}") 00098 00099 # -------------------------------------------------------------------------- 00100 # OpenCV 2 00101 if (EXISTS "${OpenCV_DIR}/OpenCVConfig.cmake") 00102 00103 include ("${OpenCV_DIR}/OpenCVConfig.cmake") 00104 00105 foreach (__CVLIB IN LISTS OpenCV_COMPONENTS) 00106 if (NOT __CVLIB MATCHES "^opencv_") 00107 set (__CVLIB "opencv_${__CVLIB}") 00108 endif () 00109 list (APPEND OpenCV_COMPONENTS_REQUIRED "${__CVLIB}") 00110 endforeach () 00111 00112 # Note that OpenCV 2.0.0 does only call the command include_directories() 00113 # but does not set OpenCV_INCLUDE_DIRS. This variable was added to 00114 # OpenCVConfig.cmake since version 2.1.0 of OpenCV. 00115 get_directory_property (__INCLUDE_DIRS INCLUDE_DIRECTORIES) 00116 find_path ( 00117 OpenCV_INCLUDE_DIR "cv.h" 00118 HINTS ${__INCLUDE_DIRS} 00119 DOC "Directory of cv.h header file." 00120 NO_DEFAULT_PATH 00121 ) 00122 mark_as_advanced (OpenCV_INCLUDE_DIR) 00123 unset (__INCLUDE_DIRS) 00124 00125 # -------------------------------------------------------------------------- 00126 # OpenCV 1 (or OpenCV 2 with missing OpenCVConfig.cmake file) 00127 else () 00128 00129 # will be adjusted on Unix to find the correct library version 00130 set (OpenCV_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES "${CMAKE_FIND_LIBRARY_SUFFIXES}") 00131 00132 # find include directory 00133 find_path ( 00134 OpenCV_INCLUDE_DIR "cv.h" 00135 PATHS "${OpenCV_DIR}" 00136 PATH_SUFFIXES "include" "include/opencv" 00137 DOC "Directory of cv.h header file." 00138 NO_DEFAULT_PATH 00139 ) 00140 00141 mark_as_advanced (OpenCV_INCLUDE_DIR) 00142 00143 if (EXISTS ${OpenCV_INCLUDE_DIR}) 00144 # should not be done by Find module, but OpenCVConfig.cmake does it 00145 # as well, unfortunately... 00146 include_directories (${OpenCV_INCLUDE_DIR}) 00147 # extract version information from header file 00148 if (EXISTS "${OpenCV_INCLUDE_DIR}/cvver.h") 00149 file (STRINGS "${OpenCV_INCLUDE_DIR}/cvver.h" OpenCV_VERSIONS_TMP REGEX "^#define CV_[A-Z]+_VERSION[ \t]+[0-9]+$") 00150 elseif (EXISTS "${OpenCV_INCLUDE_DIR}/../opencv2/core/version.hpp") 00151 file (STRINGS "${OpenCV_INCLUDE_DIR}/../opencv2/core/version.hpp" OpenCV_VERSIONS_TMP REGEX "^#define CV_[A-Z]+_VERSION[ \t]+[0-9]+$") 00152 else () 00153 message (FATAL_ERROR "Missing ${OpenCV_INCLUDE_DIR}/cvver.h or ${OpenCV_INCLUDE_DIR}/../opencv2/core/version.hpp file!") 00154 endif () 00155 string (REGEX REPLACE ".*#define CV_MAJOR_VERSION[ \t]+([0-9]+).*" "\\1" OpenCV_VERSION_MAJOR ${OpenCV_VERSIONS_TMP}) 00156 string (REGEX REPLACE ".*#define CV_MINOR_VERSION[ \t]+([0-9]+).*" "\\1" OpenCV_VERSION_MINOR ${OpenCV_VERSIONS_TMP}) 00157 string (REGEX REPLACE ".*#define CV_SUBMINOR_VERSION[ \t]+([0-9]+).*" "\\1" OpenCV_VERSION_PATCH ${OpenCV_VERSIONS_TMP}) 00158 set (OpenCV_VERSION "${OpenCV_VERSION_MAJOR}.${OpenCV_VERSION_MINOR}.${OpenCV_VERSION_PATCH}") 00159 # file name suffixes 00160 if (UNIX) 00161 set (OpenCV_CVLIB_NAME_SUFFIX) 00162 set (CMAKE_FIND_LIBRARY_SUFFIXES) 00163 foreach (SUFFIX IN LISTS OpenCV_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES) 00164 if (NOT SUFFIX MATCHES "\\.${OpenCV_VERSION_MAJOR}\\.${OpenCV_VERSION_MINOR}\\.${OpenCV_VERSION_PATCH}$") 00165 set (SUFFIX "${SUFFIX}.${OpenCV_VERSION}") 00166 endif () 00167 list (APPEND CMAKE_FIND_LIBRARY_SUFFIXES "${SUFFIX}") 00168 endforeach () 00169 # for the 1.1pre1 version, the suffix of the libraries is by default .2.0.0 instead of .1.1.0 00170 # thus consider these library files as well, assuming that the suffix has not been corrected 00171 if (OpenCV_VERSION VERSION_EQUAL 1.1.0) 00172 foreach (SUFFIX IN LISTS OpenCV_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES) 00173 if (NOT SUFFIX MATCHES "\\.2\\.0\\.0$") 00174 list (APPEND CMAKE_FIND_LIBRARY_SUFFIXES "${SUFFIX}.2.0.0") 00175 endif () 00176 endforeach () 00177 endif () 00178 else () 00179 set (OpenCV_CVLIB_NAME_SUFFIX "${OpenCV_VERSION_MAJOR}${OpenCV_VERSION_MINOR}${OpenCV_VERSION_PATCH}") 00180 endif () 00181 endif () 00182 00183 # library components 00184 if (OpenCV_VERSION_MAJOR GREATER 1) 00185 set (OpenCV_LIB_COMPONENTS core ml video calib3d contrib features2d flann gpu highgui imgproc objdetect legacy) 00186 else () 00187 set (OpenCV_LIB_COMPONENTS cxcore cv ml highgui cvaux) 00188 endif () 00189 00190 if (OpenCV_COMPONENTS) 00191 foreach (__CVLIB IN LISTS OpenCV_COMPONENTS) 00192 string (REGEX REPLACE "^opencv_" "" __CVLIB__ "${__CVLIB}") 00193 list (FIND OpenCV_LIB_COMPONENTS ${__CVLIB__} IDX) 00194 if (IDX EQUAL -1) 00195 message (FATAL_ERROR "Unknown OpenCV library component: ${__CVLIB}" 00196 " Are you looking for OpenCV 2.0.0 or greater?" 00197 " In this case, please set OpenCV_DIR to the" 00198 " directory containing the OpenCVConfig.cmake file.") 00199 endif () 00200 list (APPEND OpenCV_COMPONENTS_REQUIRED "${__CVLIB__}") 00201 endforeach () 00202 else () 00203 set (OpenCV_COMPONENTS_REQUIRED ${OpenCV_LIB_COMPONENTS}) 00204 endif () 00205 00206 # find libraries of components 00207 set (OpenCV_LIB_COMPONENTS) 00208 foreach (__CVLIB IN LISTS OpenCV_COMPONENTS_REQUIRED) 00209 00210 # debug build 00211 find_library ( 00212 OpenCV_${__CVLIB}_LIBRARY_DEBUG 00213 NAMES "opencv_${__CVLIB}${OpenCV_CVLIB_NAME_SUFFIX}d" "${__CVLIB}${OpenCV_CVLIB_NAME_SUFFIX}d" 00214 PATHS "${OpenCV_DIR}/lib" 00215 NO_DEFAULT_PATH 00216 ) 00217 00218 # release build 00219 if (APPLE AND OpenCV_DIR MATCHES "/OpenCV\\.framework/*$" AND EXISTS "${OpenCV_DIR}/OpenCV" AND NOT IS_DIRECTORY "${OpenCV_DIR}/OpenCV") 00220 find_file ( 00221 OpenCV_${__CVLIB}_LIBRARY_RELEASE 00222 NAMES OpenCV 00223 PATHS "${OpenCV_DIR}" 00224 NO_DEFAULT_PATH 00225 ) 00226 else () 00227 find_library ( 00228 OpenCV_${__CVLIB}_LIBRARY_RELEASE 00229 NAMES "opencv_${__CVLIB}${OpenCV_CVLIB_NAME_SUFFIX}" "${__CVLIB}${OpenCV_CVLIB_NAME_SUFFIX}" 00230 PATHS "${OpenCV_DIR}/lib" 00231 NO_DEFAULT_PATH 00232 ) 00233 endif () 00234 00235 mark_as_advanced (OpenCV_${__CVLIB}_LIBRARY_DEBUG) 00236 mark_as_advanced (OpenCV_${__CVLIB}_LIBRARY_RELEASE) 00237 00238 # both debug/release 00239 if (OpenCV_${__CVLIB}_LIBRARY_DEBUG AND OpenCV_${__CVLIB}_LIBRARY_RELEASE) 00240 set (OpenCV_${__CVLIB}_LIBRARY debug ${OpenCV_${__CVLIB}_LIBRARY_DEBUG} optimized ${OpenCV_${__CVLIB}_LIBRARY_RELEASE}) 00241 # only debug 00242 elseif (OpenCV_${__CVLIB}_LIBRARY_DEBUG) 00243 set (OpenCV_${__CVLIB}_LIBRARY ${OpenCV_${__CVLIB}_LIBRARY_DEBUG}) 00244 # only release 00245 elseif (OpenCV_${__CVLIB}_LIBRARY_RELEASE) 00246 set (OpenCV_${__CVLIB}_LIBRARY ${OpenCV_${__CVLIB}_LIBRARY_RELEASE}) 00247 # not found 00248 else () 00249 set (OpenCV_${__CVLIB}_LIBRARY) 00250 endif() 00251 00252 # add to list of found libraries 00253 if (OpenCV_${__CVLIB}_LIBRARY) 00254 list (APPEND OpenCV_LIB_COMPONENTS ${__CVLIB}) 00255 list (APPEND OpenCV_LIBS "${OpenCV_${__CVLIB}_LIBRARY}") 00256 endif () 00257 00258 endforeach () 00259 00260 # restore library suffixes 00261 set (CMAKE_FIND_LIBRARY_SUFFIXES "${OpenCV_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES}") 00262 00263 # compatibility with OpenCV 2 00264 set (OpenCV_INCLUDE_DIRS "${OpenCV_INCLUDE_DIR}") 00265 00266 endif () 00267 00268 # -------------------------------------------------------------------------- 00269 # set OpenCV_INCLUDE_DIRS - required for OpenCV before version 2.1.0 00270 if (OpenCV_INCLUDE_DIR MATCHES "/opencv$" AND NOT OpenCV_INCLUDE_DIRS) 00271 get_filename_component (OpenCV_INCLUDE_DIRS "${OpenCV_INCLUDE_DIR}" PATH) 00272 list (APPEND OpenCV_INCLUDE_DIRS "${OpenCV_INCLUDE_DIR}") 00273 endif () 00274 00275 # -------------------------------------------------------------------------- 00276 # handle the QUIETLY and REQUIRED arguments and set *_FOUND to TRUE 00277 # if all listed variables are found or TRUE 00278 include (FindPackageHandleStandardArgs) 00279 00280 set (OpenCV_REQUIRED_COMPONENTS_FOUND TRUE) 00281 set (OpenCV_COMPONENTS_NOT_FOUND) 00282 foreach (__CVLIB IN LISTS OpenCV_COMPONENTS_REQUIRED) 00283 list (FIND OpenCV_LIB_COMPONENTS ${__CVLIB} IDX) 00284 if (IDX EQUAL -1) 00285 set (OpenCV_REQUIRED_COMPONENTS_FOUND FALSE) 00286 list (APPEND OpenCV_COMPONENTS_NOT_FOUND ${__CVLIB}) 00287 endif () 00288 endforeach () 00289 00290 if (NOT OpenCV_REQUIRED_COMPONENTS_FOUND) 00291 if (NOT OpenCV_FIND_QUIET AND OpenCV_FIND_REQUIRED) 00292 message (FATAL_ERROR "The following required OpenCV components" 00293 " were not found: ${OpenCV_COMPONENTS_NOT_FOUND}") 00294 endif () 00295 endif () 00296 00297 find_package_handle_standard_args ( 00298 OpenCV 00299 REQUIRED_VARS 00300 OpenCV_INCLUDE_DIR 00301 OpenCV_LIBS 00302 OpenCV_REQUIRED_COMPONENTS_FOUND 00303 VERSION_VAR 00304 OpenCV_VERSION 00305 ) 00306 00307 set (OpenCV_FOUND "${OPENCV_FOUND}") 00308 00309 # -------------------------------------------------------------------------- 00310 # (backward) compatibility 00311 if (OpenCV_FOUND) 00312 set (OpenCV_LIBRARIES "${OpenCV_LIBS}") 00313 endif () 00314 00315 elseif (NOT OpenCV_FIND_QUIET AND OpenCV_FIND_REQUIRED) 00316 message (FATAL_ERROR "Please specify the OpenCV directory using OpenCV_DIR (environment) variable.") 00317 endif ()