FindOpenCV.cmake 5.19 KB
Newer Older
1
#
Julien Michel's avatar
Julien Michel committed
2
# Copyright (C) 2005-2019 Centre National d'Etudes Spatiales (CNES)
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#
# This file is part of Orfeo Toolbox
#
#     https://www.orfeo-toolbox.org/
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

Rashad Kanavath's avatar
Rashad Kanavath committed
21 22 23 24 25 26 27 28 29 30 31 32
set(OPENCV_SEARCH_PATH)

if(OpenCV_DIR)
  get_filename_component(OPENCV_SEARCH_PATH "${OpenCV_DIR}" PATH)
  if(OPENCV_SEARCH_PATH)
    get_filename_component(OPENCV_SEARCH_PATH "${OPENCV_SEARCH_PATH}" PATH)
  endif()
  if(OPENCV_SEARCH_PATH)
    find_path(
      opencv_INCLUDE_DIR
      opencv/cv.h
      PATHS "${OPENCV_SEARCH_PATH}"
33 34
      #no additional paths are added to the search if OpenCV_DIR
      NO_DEFAULT_PATH
Rashad Kanavath's avatar
Rashad Kanavath committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
      PATH_SUFFIXES "include"
      DOC "The directory where opencv/cv.h is installed")
  endif()
endif()

if(NOT opencv_INCLUDE_DIR)
  find_path(
    opencv_INCLUDE_DIR
    opencv/cv.h
    PATHS "${OpenCV_DIR}"
    PATH_SUFFIXES "include"
    DOC "The directory where opencv/cv.h is installed")
endif()

if(NOT EXISTS ${opencv_INCLUDE_DIR}/opencv2/opencv.hpp)
  message(FATAL_ERROR "${opencv_INCLUDE_DIR}/opencv2/opencv.hpp does not exists. "
    "Make sure you have opencv 2.3 or higher. We had searched in ${OPENCV_SEARCH_PATHS}")
endif()

set(opencv_core_NAMES opencv_core)
set(opencv_ml_NAMES opencv_ml)

57
if (opencv_INCLUDE_DIR)
Rashad Kanavath's avatar
Rashad Kanavath committed
58 59 60 61 62 63

  set(OPENCV_INCLUDE_DIRS "${opencv_INCLUDE_DIR}")

  if(NOT OpenCV_VERSION)
    file(READ "${opencv_INCLUDE_DIR}/opencv2/core/version.hpp" _header_content)

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    # detect the type of version file (2.3.x , 2.4.x or 3.x)
    string(REGEX MATCH  ".*# *define +CV_VERSION_EPOCH +([0-9]+).*" has_epoch ${_header_content})
    string(REGEX MATCH  ".*# *define +CV_MAJOR_VERSION +([0-9]+).*" has_old_major ${_header_content})
    string(REGEX MATCH  ".*# *define +CV_MINOR_VERSION +([0-9]+).*" has_old_minor ${_header_content})
    string(REGEX MATCH  ".*# *define +CV_SUBMINOR_VERSION +([0-9]+).*" has_old_subminor ${_header_content})

    if(has_old_major AND has_old_minor AND has_old_subminor)
      #for opencv 2.3.x
      string(REGEX REPLACE ".*# *define +CV_MAJOR_VERSION +([0-9]+).*" "\\1"
        OpenCV_VERSION_MAJOR ${_header_content})
      string(REGEX REPLACE ".*# *define +CV_MINOR_VERSION +([0-9]+).*" "\\1"
        OpenCV_VERSION_MINOR ${_header_content})
      string(REGEX REPLACE ".*# *define +CV_SUBMINOR_VERSION +([0-9]+).*" "\\1"
        OpenCV_VERSION_PATCH ${_header_content})
      set(OpenCV_VERSION_TWEAK)
    elseif(has_epoch)
      # for opencv 2.4.x
Rashad Kanavath's avatar
Rashad Kanavath committed
81 82 83 84 85 86 87 88 89
      string(REGEX REPLACE ".*# *define +CV_VERSION_EPOCH +([0-9]+).*" "\\1"
        OpenCV_VERSION_MAJOR ${_header_content})
      string(REGEX REPLACE ".*# *define +CV_VERSION_MAJOR +([0-9]+).*" "\\1"
        OpenCV_VERSION_MINOR ${_header_content})
      string(REGEX REPLACE ".*# *define +CV_VERSION_MINOR +([0-9]+).*" "\\1"
        OpenCV_VERSION_PATCH ${_header_content})
      string(REGEX REPLACE ".*# *define +CV_VERSION_REVISION +([0-9]+).*" "\\1"
        OpenCV_VERSION_TWEAK ${_header_content})
    else()
90
      string(REGEX REPLACE ".*# *define +CV_VERSION_MAJOR +([0-9]+).*" "\\1"
Rashad Kanavath's avatar
Rashad Kanavath committed
91
        OpenCV_VERSION_MAJOR ${_header_content})
92
      string(REGEX REPLACE ".*# *define +CV_VERSION_MINOR +([0-9]+).*" "\\1"
Rashad Kanavath's avatar
Rashad Kanavath committed
93
        OpenCV_VERSION_MINOR ${_header_content})
94
      string(REGEX REPLACE ".*# *define +CV_VERSION_REVISION +([0-9]+).*" "\\1"
Rashad Kanavath's avatar
Rashad Kanavath committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
        OpenCV_VERSION_PATCH ${_header_content})
      set(OpenCV_VERSION_TWEAK)
    endif()
    set(OpenCV_VERSION
      "${OpenCV_VERSION_MAJOR}.${OpenCV_VERSION_MINOR}.${OpenCV_VERSION_PATCH}")
  endif()

  if(WIN32)
    list(APPEND opencv_core_NAMES
      "opencv_core${OpenCV_VERSION_MAJOR}${OpenCV_VERSION_MINOR}${OpenCV_VERSION_PATCH}")
    list(APPEND opencv_ml_NAMES
      "opencv_ml${OpenCV_VERSION_MAJOR}${OpenCV_VERSION_MINOR}${OpenCV_VERSION_PATCH}")
  endif()
endif()

110 111 112
if(NOT OPENCV_SEARCH_PATH)
  get_filename_component(OPENCV_SEARCH_PATH "${opencv_INCLUDE_DIR}" PATH)
endif()
Rashad Kanavath's avatar
Rashad Kanavath committed
113 114 115 116 117 118

# Prefer the static library.
find_library(
  OPENCV_core_LIBRARY
  NAMES ${opencv_core_NAMES}
  PATHS ${OPENCV_SEARCH_PATH}
119
  PATH_SUFFIXES "lib" "lib64" "lib/${CMAKE_LIBRARY_ARCHITECTURE}"
Rashad Kanavath's avatar
Rashad Kanavath committed
120 121 122 123 124 125 126
  NO_DEFAULT_PATH
  DOC "Path to opencv_core library")

find_library(
  OPENCV_ml_LIBRARY
  NAMES ${opencv_ml_NAMES}
  PATHS ${OPENCV_SEARCH_PATH}
127
  PATH_SUFFIXES "lib" "lib64" "lib/${CMAKE_LIBRARY_ARCHITECTURE}"
Rashad Kanavath's avatar
Rashad Kanavath committed
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
  NO_DEFAULT_PATH
  DOC "Path to opencv_ml library")

set(OpenCV_FOUND FALSE)
if ( OPENCV_core_LIBRARY AND OPENCV_ml_LIBRARY )
  set(OPENCV_LIBRARIES "${OPENCV_core_LIBRARY};${OPENCV_ml_LIBRARY}")
endif ()

if( OPENCV_INCLUDE_DIRS AND OPENCV_LIBRARIES )
  set(OpenCV_FOUND TRUE)
  set(OPENCV_VERSION ${OpenCV_VERSION}) #for compatility
endif()

include(${CMAKE_ROOT}/Modules/FindPackageHandleStandardArgs.cmake)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(OpenCV
  REQUIRED_VARS OPENCV_core_LIBRARY OPENCV_INCLUDE_DIRS
  VERSION_VAR OpenCV_VERSION)