BASIS  version 1.2.3 (revision 2104)
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 #  
00014 # - OpenCV_FOUND
00015 # - OpenCV_LIBS
00016 # - OpenCV_INCLUDE_DIR
00017 # - OpenCV_VERSION (OpenCV_VERSION_MAJOR, OpenCV_VERSION_MINOR, OpenCV_VERSION_PATCH)
00018 #
00019 #
00020 # The following variables are used to maintain compatibility with other
00021 # Find<Pkg>.cmake modules, including the FindOpenCV.cmake module of
00022 # of Jan Woetzel (2006/09, www.mip.informatik.uni-kiel.de/~jw):
00023 # - OpenCV_INCLUDE_DIRS
00024 # - OpenCV_LIBRARIES
00025 # - OpenCV_LINK_DIRECTORIES
00026 # 
00027 # @par 3. Version
00028 #
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 
00051 # ----------------------------------------------------------------------------
00052 # initialize search
00053 set (OpenCV_FOUND FALSE)
00054 
00055 if (NOT OpenCV_DIR)
00056   if (DEFINED ENV{OpenCV_DIR})
00057     set (OpenCV_DIR "$ENV{OpenCV_DIR}" CACHE PATH "Installation prefix of OpenCV Library." FORCE)
00058   elseif (DEFINED ENV{OPENCV_DIR})
00059     set (OpenCV_DIR "$ENV{OPENCV_DIR}" CACHE PATH "Installation prefix of OpenCV Library." FORCE)
00060   else ()
00061     find_path (
00062       OpenCV_DIR "OpenCVConfig.cmake"
00063       DOC "Directory containing OpenCVConfig.cmake file or installation prefix of OpenCV."
00064     )
00065   endif ()
00066 endif ()
00067 
00068 set (OpenCV_LIBS)                # found libraries
00069 set (OpenCV_COMPONENTS_REQUIRED) # requested components
00070 set (OpenCV_LIB_COMPONENTS)      # found components
00071 set (OpenCV_VERSION)             # found version
00072 
00073 # ----------------------------------------------------------------------------
00074 # find headers and libraries
00075 if (EXISTS "${OpenCV_DIR}")
00076 
00077   # --------------------------------------------------------------------------
00078   # OpenCV 2
00079   if (EXISTS "${OpenCV_DIR}/OpenCVConfig.cmake")
00080 
00081     include ("${OpenCV_DIR}/OpenCVConfig.cmake")
00082 
00083     foreach (__CVLIB IN LISTS OpenCV_COMPONENTS)
00084       if (NOT __CVLIB MATCHES "^opencv_")
00085         set (__CVLIB "opencv_${__CVLIB}")
00086       endif ()
00087       list (APPEND OpenCV_COMPONENTS_REQUIRED "${__CVLIB}")
00088     endforeach ()
00089 
00090     # Note that OpenCV 2.0.0 does only call the command include_directories()
00091     # but does not set OpenCV_INCLUDE_DIRS. This variable was added to
00092     # OpenCVConfig.cmake since version 2.1.0 of OpenCV.
00093     get_directory_property (__INCLUDE_DIRS INCLUDE_DIRECTORIES)
00094     find_path (
00095       OpenCV_INCLUDE_DIR "cv.h"
00096       HINTS ${__INCLUDE_DIRS}
00097       DOC "Directory of cv.h header file."
00098       NO_DEFAULT_PATH
00099     )
00100     mark_as_advanced (OpenCV_INCLUDE_DIR)
00101     unset (__INCLUDE_DIRS)
00102 
00103   # --------------------------------------------------------------------------
00104   # OpenCV 1
00105   else ()
00106 
00107     # will be adjusted on Unix to find the correct library version
00108     set (OpenCV_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES "${CMAKE_FIND_LIBRARY_SUFFIXES}")
00109 
00110     # library components
00111     set (OpenCV_LIB_COMPONENTS cxcore cv ml highgui cvaux)
00112 
00113     if (OpenCV_COMPONENTS)
00114       foreach (__CVLIB IN LISTS OpenCV_COMPONENTS)
00115         string (REGEX REPLACE "^opencv_" "" __CVLIB__ "${__CVLIB}")
00116         list (FIND OpenCV_LIB_COMPONENTS ${__CVLIB__} IDX)
00117         if (IDX EQUAL -1)
00118           message (FATAL_ERROR "Unknown OpenCV library component: ${__CVLIB}"
00119                                " Are you looking for OpenCV 2.0.0 or greater?"
00120                                " In this case, please set OpenCV_DIR to the"
00121                                " directory containing the OpenCVConfig.cmake file.")
00122         endif ()
00123         list (APPEND OpenCV_COMPONENTS_REQUIRED "${__CVLIB__}")
00124       endforeach ()
00125     else ()
00126       set (OpenCV_COMPONENTS_REQUIRED ${OpenCV_LIB_COMPONENTS})
00127     endif ()
00128 
00129     # find include directory
00130     find_path (
00131       OpenCV_INCLUDE_DIR "cv.h"
00132       PATHS "${OpenCV_DIR}"
00133       PATH_SUFFIXES "include" "include/opencv"
00134       DOC "Directory of cv.h header file."
00135       NO_DEFAULT_PATH
00136     )
00137 
00138     mark_as_advanced (OpenCV_INCLUDE_DIR)
00139 
00140     if (EXISTS ${OpenCV_INCLUDE_DIR})
00141       # should not be done by Find module, but OpenCVConfig.cmake does it
00142       # as well, unfortunately...
00143       include_directories (${OpenCV_INCLUDE_DIR})
00144       # extract version information from cvver.h
00145       file (STRINGS "${OpenCV_INCLUDE_DIR}/cvver.h" OpenCV_VERSIONS_TMP REGEX "^#define CV_[A-Z]+_VERSION[ \t]+[0-9]+$")
00146       string (REGEX REPLACE ".*#define CV_MAJOR_VERSION[ \t]+([0-9]+).*" "\\1" OpenCV_VERSION_MAJOR ${OpenCV_VERSIONS_TMP})
00147       string (REGEX REPLACE ".*#define CV_MINOR_VERSION[ \t]+([0-9]+).*" "\\1" OpenCV_VERSION_MINOR ${OpenCV_VERSIONS_TMP})
00148       string (REGEX REPLACE ".*#define CV_SUBMINOR_VERSION[ \t]+([0-9]+).*" "\\1" OpenCV_VERSION_PATCH ${OpenCV_VERSIONS_TMP})
00149       set (OpenCV_VERSION "${OpenCV_VERSION_MAJOR}.${OpenCV_VERSION_MINOR}.${OpenCV_VERSION_PATCH}")
00150       # file name suffixes
00151       if (UNIX)
00152         set (OpenCV_CVLIB_NAME_SUFFIX)
00153         set (CMAKE_FIND_LIBRARY_SUFFIXES)
00154         foreach (SUFFIX IN LISTS OpenCV_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES)
00155           if (NOT SUFFIX MATCHES "${OpenCV_VERSION_MAJOR}\\.${OpenCV_VERSION_MINOR}\\.${OpenCV_VERSION_PATCH}$")
00156             set (SUFFIX "${SUFFIX}.${OpenCV_VERSION}")
00157           endif ()
00158           list (APPEND CMAKE_FIND_LIBRARY_SUFFIXES "${SUFFIX}")
00159         endforeach ()
00160       else ()
00161         set (OpenCV_CVLIB_NAME_SUFFIX "${OpenCV_VERSION_MAJOR}${OpenCV_VERSION_MINOR}${OpenCV_VERSION_PATCH}")
00162       endif ()
00163     endif ()
00164 
00165     # find libraries of components
00166     set (OpenCV_LIB_COMPONENTS)
00167     foreach (__CVLIB IN LISTS OpenCV_COMPONENTS_REQUIRED)
00168 
00169       # debug build
00170       find_library (
00171         OpenCV_${__CVLIB}_LIBRARY_DEBUG
00172         NAMES "${__CVLIB}${OpenCV_CVLIB_NAME_SUFFIX}d"
00173         PATHS "${OpenCV_DIR}/lib"
00174         NO_DEFAULT_PATH
00175       )
00176 
00177       # release build
00178       find_library (
00179         OpenCV_${__CVLIB}_LIBRARY_RELEASE
00180         NAMES "${__CVLIB}${OpenCV_CVLIB_NAME_SUFFIX}"
00181         PATHS "${OpenCV_DIR}/lib"
00182         NO_DEFAULT_PATH
00183       )
00184 
00185       mark_as_advanced (OpenCV_${__CVLIB}_LIBRARY_DEBUG)
00186       mark_as_advanced (OpenCV_${__CVLIB}_LIBRARY_RELEASE)
00187 
00188       # both debug/release
00189       if (OpenCV_${__CVLIB}_LIBRARY_DEBUG AND OpenCV_${__CVLIB}_LIBRARY_RELEASE)
00190         set (OpenCV_${__CVLIB}_LIBRARY debug ${OpenCV_${__CVLIB}_LIBRARY_DEBUG} optimized ${OpenCV_${__CVLIB}_LIBRARY_RELEASE})
00191       # only debug
00192       elseif (OpenCV_${__CVLIB}_LIBRARY_DEBUG)
00193         set (OpenCV_${__CVLIB}_LIBRARY ${OpenCV_${__CVLIB}_LIBRARY_DEBUG})
00194       # only release
00195       elseif (OpenCV_${__CVLIB}_LIBRARY_RELEASE)
00196         set (OpenCV_${__CVLIB}_LIBRARY ${OpenCV_${__CVLIB}_LIBRARY_RELEASE})
00197       # not found
00198       else ()
00199         set (OpenCV_${__CVLIB}_LIBRARY)
00200       endif()
00201 
00202       # add to list of found libraries
00203       if (OpenCV_${__CVLIB}_LIBRARY)
00204         list (APPEND OpenCV_LIB_COMPONENTS ${__CVLIB})
00205         list (APPEND OpenCV_LIBS "${OpenCV_${__CVLIB}_LIBRARY}")
00206       endif ()
00207 
00208     endforeach ()
00209 
00210     # restore library suffixes
00211     set (OpenCV_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES "${CMAKE_FIND_LIBRARY_SUFFIXES}")
00212 
00213     # compatibility with OpenCV 2
00214     set (OpenCV_INCLUDE_DIRS "${OpenCV_INCLUDE_DIR}")
00215 
00216   endif ()
00217 
00218   # --------------------------------------------------------------------------
00219   # set OpenCV_INCLUDE_DIRS - required for OpenCV before version 2.1.0
00220   if (OpenCV_INCLUDE_DIR MATCHES "/opencv$" AND NOT OpenCV_INCLUDE_DIRS)
00221     get_filename_component (OpenCV_INCLUDE_DIRS "${OpenCV_INCLUDE_DIR}" PATH)
00222     list (APPEND OpenCV_INCLUDE_DIRS "${OpenCV_INCLUDE_DIR}")
00223   endif ()
00224 
00225   # --------------------------------------------------------------------------
00226   # handle the QUIETLY and REQUIRED arguments and set *_FOUND to TRUE
00227   # if all listed variables are found or TRUE
00228   include (FindPackageHandleStandardArgs)
00229 
00230   set (OpenCV_REQUIRED_COMPONENTS_FOUND TRUE)
00231   set (OpenCV_COMPONENTS_NOT_FOUND)
00232   foreach (__CVLIB IN LISTS OpenCV_COMPONENTS_REQUIRED)
00233     list (FIND OpenCV_LIB_COMPONENTS ${__CVLIB} IDX)
00234     if (IDX EQUAL -1)
00235       set (OpenCV_REQUIRED_COMPONENTS_FOUND FALSE)
00236       list (APPEND OpenCV_COMPONENTS_NOT_FOUND ${__CVLIB})
00237     endif ()
00238   endforeach ()
00239 
00240   if (NOT OpenCV_REQUIRED_COMPONENTS_FOUND)
00241     if (NOT OpenCV_FIND_QUIET AND OpenCV_FIND_REQUIRED)
00242       message (FATAL_ERROR "The following required OpenCV components"
00243                            " were not found: ${OpenCV_COMPONENTS_NOT_FOUND}")
00244     endif ()
00245   endif ()
00246 
00247   find_package_handle_standard_args (
00248     OpenCV
00249     REQUIRED_VARS
00250       OpenCV_INCLUDE_DIR
00251       OpenCV_LIBS
00252       OpenCV_REQUIRED_COMPONENTS_FOUND
00253     VERSION_VAR
00254       OpenCV_VERSION
00255   )
00256 
00257   set (OpenCV_FOUND "${OPENCV_FOUND}")
00258 
00259   # --------------------------------------------------------------------------
00260   # (backward) compatibility
00261   if (OpenCV_FOUND)
00262     set (OpenCV_LIBRARIES "${OpenCV_LIBS}")
00263   endif ()
00264 
00265 elseif (NOT OpenCV_FIND_QUIET AND OpenCV_FIND_REQUIRED)
00266   message (FATAL_ERROR "Please specify the OpenCV directory using OpenCV_DIR (environment) variable.")
00267 endif ()