1# - Try to find OpenCV library installation
2# See http://sourceforge.net/projects/opencvlibrary/
3#
4# The following variable is optionally searched for defaults
5#  OPENCV_ROOT_DIR:            Base directory of OpenCv tree to use.
6#
7#  OPENCV_NEW_LIBRARY_NAMES   Set to YES before searching if you want to
8# The following are set after configuration is done:
9#  OPENCV_FOUND
10#  OPENCV_INCLUDE_DIRS
11#  OPENCV_LIBRARIES
12#
13# 2004/05 Jan Woetzel, Friso, Daniel Grest
14# 2006/01 complete rewrite by Jan Woetzel
15# 2006/09 2nd rewrite introducing ROOT_DIR and PATH_SUFFIXES
16#   to handle multiple installed versions gracefully by Jan Woetzel
17# 2010/02 Ryan Pavlik (Iowa State University) - partial rewrite to standardize
18#
19# tested with:
20# -OpenCV 0.97 (beta5a):  MSVS 7.1, gcc 3.3, gcc 4.1
21# -OpenCV 0.99 (1.0rc1):  MSVS 7.1
22#
23# www.mip.informatik.uni-kiel.de/~jw
24# academic.cleardefinition.com
25# --------------------------------
26
27set(OPENCV_ROOT_DIR
28	"${OPENCV_ROOT_DIR}"
29	CACHE
30	PATH
31	"Path to search for OpenCV")
32
33find_package(OpenCV QUIET NO_MODULE)
34if(OpenCV_LIBS AND NOT OpenCV_LIBRARIES)
35	set(OPENCV_LIBRARIES ${OpenCV_LIBS})
36	set(OPENCV_FOUND true)
37else()
38	include(ProgramFilesGlob)
39
40	# typical root dirs of installations, exactly one of them is used
41	program_files_glob(_dirs "/OpenCV*/")
42
43	#
44	# select exactly ONE OPENCV base directory/tree
45	# to avoid mixing different version headers and libs
46	#
47	find_path(OPENCV_BASE_DIR
48		NAMES
49		cv/include/cv.h
50		include/opencv/cv.h
51		include/cv/cv.h
52		include/cv.h
53		HINTS
54		"${OPENCV_ROOT_DIR}"
55		"$ENV{OPENCV_ROOT_DIR}"
56		"[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Intel(R) Open Source Computer Vision Library_is1;Inno Setup: App Path]"
57		${_dirs})
58
59
60
61
62	# header include dir suffixes appended to OPENCV_BASE_DIR
63	set(OPENCV_INCDIR_SUFFIXES
64		include
65		include/cv
66		include/opencv
67		cv/include
68		cxcore/include
69		cvaux/include
70		otherlibs/cvcam/include
71		otherlibs/highgui
72		otherlibs/highgui/include
73		otherlibs/_graphics/include)
74
75	# library linkdir suffixes appended to OPENCV_BASE_DIR
76	set(OPENCV_LIBDIR_SUFFIXES
77		lib
78		lib64
79		OpenCV/lib
80		otherlibs/_graphics/lib)
81
82
83	#
84	# find incdir for each lib
85	#
86	find_path(OPENCV_cv_INCLUDE_DIR
87		NAMES
88		cv.h
89		HINTS
90		"${OPENCV_BASE_DIR}"
91		"${OPENCV_ROOT_DIR}"
92		PATH_SUFFIXES
93		${OPENCV_INCDIR_SUFFIXES})
94	find_path(OPENCV_cxcore_INCLUDE_DIR
95		NAMES
96		cxcore.h
97		HINTS
98		"${OPENCV_BASE_DIR}"
99		"${OPENCV_ROOT_DIR}"
100		PATH_SUFFIXES
101		${OPENCV_INCDIR_SUFFIXES})
102	find_path(OPENCV_cxcore_INCLUDE_DIR
103		NAMES
104		cvaux.h
105		HINTS
106		"${OPENCV_BASE_DIR}"
107		"${OPENCV_ROOT_DIR}"
108		PATH_SUFFIXES
109		${OPENCV_INCDIR_SUFFIXES})
110	find_path(OPENCV_highgui_INCLUDE_DIR
111		NAMES
112		highgui.h
113		HINTS
114		"${OPENCV_BASE_DIR}"
115		"${OPENCV_ROOT_DIR}"
116		PATH_SUFFIXES
117		${OPENCV_INCDIR_SUFFIXES})
118	find_path(OPENCV_cvcam_INCLUDE_DIR
119		NAMES
120		cvcam.h
121		HINTS
122		"${OPENCV_BASE_DIR}"
123		"${OPENCV_ROOT_DIR}"
124		PATH_SUFFIXES
125		${OPENCV_INCDIR_SUFFIXES})
126
127	#
128	# find sbsolute path to all libraries
129	# some are optionally, some may not exist on Linux
130	#
131	find_library(OPENCV_legacy_LIBRARY
132		NAMES
133		opencv_legacy
134		HINTS
135		"${OPENCV_BASE_DIR}"
136		"${OPENCV_ROOT_DIR}"
137		PATH_SUFFIXES
138		${OPENCV_LIBDIR_SUFFIXES})
139
140	set(OPENCV_NEW_COMPONENTS calib3d contrib core features2d highgui imgproc legacy ml objdetect video)
141	set(OPENCV_OLD_COMPONENTS cv cvaux cvcam cvhaartraining cxcore cxts highgui ml trs)
142	set(opencv_components)
143	if(OPENCV_NEW_LIBRARY_NAMES OR OPENCV_legacy_LIBRARY)
144
145		# New-style library names
146		foreach(component ${OPENCV_NEW_COMPONENTS})
147			find_library(OPENCV_${component}_LIBRARY
148			NAMES
149			opencv_${component}
150			HINTS
151			${OPENCV_BASE_DIR}
152			PATH_SUFFIXES
153			${OPENCV_LIBDIR_SUFFIXES})
154		endforeach()
155
156		# cv components with header and library if COMPONENTS unspecified
157		if(NOT OpenCV_FIND_COMPONENTS)
158			# default
159			set(opencv_components core legacy imgproc highgui)
160			if(WIN32)
161				list(APPEND opencv_components video)	# WIN32 only actually
162			endif()
163		else()
164			# TODO: clean up/convert to new components
165			string(TOLOWER "${OpenCV_FIND_COMPONENTS}" opencv_components)
166		endif()
167
168	else()
169		# Old-style lib names
170		if(NOT OpenCV_FIND_COMPONENTS)
171			# default
172			set(opencv_components cv cxcore cvaux highgui)
173			if(WIN32)
174				list(APPEND opencv_components cvcam)	# WIN32 only actually
175			endif()
176		else()
177			string(TOLOWER "${OpenCV_FIND_COMPONENTS}" opencv_components)
178		endif()
179
180		foreach(component ${OPENCV_OLD_COMPONENTS})
181			find_library(OPENCV_${component}_LIBRARY
182				NAMES
183				${component}
184				HINTS
185				${OPENCV_BASE_DIR}
186				PATH_SUFFIXES
187				${OPENCV_LIBDIR_SUFFIXES})
188		endforeach()
189	endif()
190
191	#
192	# Logic selecting required libs and headers
193	#
194
195	set(_req_check)
196	set(_req_libs)
197	set(_req_includes)
198	foreach(component ${opencv_components})
199		#message(STATUS "Component requested: ${component}")
200
201		# only good if header and library both found
202		list(APPEND
203			_req_check
204			OPENCV_${component}_LIBRARY)
205		list(APPEND _req_libs "${OPENCV_${component}_LIBRARY}")
206		if(DEFINED OPENCV_${component}_INCLUDE_DIR)
207			list(APPEND
208				_req_check
209				OPENCV_${component}_INCLUDE_DIR)
210			list(APPEND _req_includes "${OPENCV_${component}_INCLUDE_DIR}")
211		endif()
212
213
214	endforeach()
215
216	# handle the QUIETLY and REQUIRED arguments and set xxx_FOUND to TRUE if
217	# all listed variables are TRUE
218	include(FindPackageHandleStandardArgs)
219	find_package_handle_standard_args(OpenCV
220		DEFAULT_MSG
221		OPENCV_cv_INCLUDE_DIR
222		${_req_check})
223
224	if(OPENCV_FOUND)
225		set(OPENCV_LIBRARY_DIRS)
226		foreach(lib ${_req_libs})
227			get_filename_component(dir "${lib}" PATH)
228			list(APPEND OPENCV_LIBRARY_DIRS "${dir}")
229		endforeach()
230		list(REVERSE OPENCV_LIBRARY_DIRS)
231		list(REMOVE_DUPLICATES OPENCV_LIBRARY_DIRS)
232		list(REVERSE OPENCV_LIBRARY_DIRS)
233
234		set(OPENCV_INCLUDE_DIRS ${_req_includes})
235		set(OPENCV_LIBRARIES ${_req_libs})
236		mark_as_advanced(OPENCV_ROOT_DIR OpenCV_DIR)
237	endif()
238
239	mark_as_advanced(OPENCV_BASE_DIR)
240	foreach(component ${OPENCV_NEW_COMPONENTS} ${OPENCV_OLD_COMPONENTS})
241		mark_as_advanced(OPENCV_${component}_LIBRARY OPENCV_${component}_INCLUDE_DIR)
242	endforeach()
243endif()
244
245
246