blob: 0ef702126487e65e361a6e4613725492555d6c6c [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001# Ceres Solver - A fast non-linear least squares minimizer
2# Copyright 2015 Google Inc. All rights reserved.
3# http://ceres-solver.org/
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions are met:
7#
8# * Redistributions of source code must retain the above copyright notice,
9# this list of conditions and the following disclaimer.
10# * Redistributions in binary form must reproduce the above copyright notice,
11# this list of conditions and the following disclaimer in the documentation
12# and/or other materials provided with the distribution.
13# * Neither the name of Google Inc. nor the names of its contributors may be
14# used to endorse or promote products derived from this software without
15# specific prior written permission.
16#
17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27# POSSIBILITY OF SUCH DAMAGE.
28#
29# Authors: pablo.speciale@gmail.com (Pablo Speciale)
30# alexs.mac@gmail.com (Alex Stewart)
31#
32
33# Config file for Ceres Solver - Find Ceres & dependencies.
34#
35# This file is used by CMake when find_package(Ceres) is invoked and either
36# the directory containing this file either is present in CMAKE_MODULE_PATH
37# (if Ceres was installed), or exists in the local CMake package registry if
38# the Ceres build directory was exported.
39#
40# This module defines the following variables:
41#
42# Ceres_FOUND / CERES_FOUND: True if Ceres has been successfully
43# found. Both variables are set as although
44# FindPackage() only references Ceres_FOUND
45# in Config mode, given the conventions for
46# <package>_FOUND when FindPackage() is
47# called in Module mode, users could
48# reasonably expect to use CERES_FOUND
49# instead.
50#
51# CERES_VERSION: Version of Ceres found.
52#
53# CERES_LIBRARIES: Libraries for Ceres and all
54# dependencies against which Ceres was
55# compiled. This will not include any optional
56# dependencies that were disabled when Ceres was
57# compiled.
58#
59# NOTE: There is no equivalent of CERES_INCLUDE_DIRS as the exported
60# CMake target already includes the definition of its public
61# include directories.
62
63# Called if we failed to find Ceres or any of its required dependencies,
64# unsets all public (designed to be used externally) variables and reports
65# error message at priority depending upon [REQUIRED/QUIET/<NONE>] argument.
66macro(CERES_REPORT_NOT_FOUND REASON_MSG)
67 # FindPackage() only references Ceres_FOUND, and requires it to be
68 # explicitly set FALSE to denote not found (not merely undefined).
69 set(Ceres_FOUND FALSE)
70 set(CERES_FOUND FALSE)
71 unset(CERES_INCLUDE_DIR)
72 unset(CERES_INCLUDE_DIRS)
73 unset(CERES_LIBRARIES)
74
75 # Reset the CMake module path to its state when this script was called.
76 set(CMAKE_MODULE_PATH ${CALLERS_CMAKE_MODULE_PATH})
77
78 # Note <package>_FIND_[REQUIRED/QUIETLY] variables defined by
79 # FindPackage() use the camelcase library name, not uppercase.
80 if (Ceres_FIND_QUIETLY)
81 message(STATUS "Failed to find Ceres - " ${REASON_MSG} ${ARGN})
82 elseif (Ceres_FIND_REQUIRED)
83 message(FATAL_ERROR "Failed to find Ceres - " ${REASON_MSG} ${ARGN})
84 else()
85 # Neither QUIETLY nor REQUIRED, use SEND_ERROR which emits an error
86 # that prevents generation, but continues configuration.
87 message(SEND_ERROR "Failed to find Ceres - " ${REASON_MSG} ${ARGN})
88 endif ()
89 return()
90endmacro(CERES_REPORT_NOT_FOUND)
91
92# ceres_pretty_print_cmake_list( OUTPUT_VAR [item1 [item2 ... ]] )
93#
94# Sets ${OUTPUT_VAR} in the caller's scope to a human-readable string
95# representation of the list passed as the remaining arguments formed
96# as: "[item1, item2, ..., itemN]".
97function(ceres_pretty_print_cmake_list OUTPUT_VAR)
98 string(REPLACE ";" ", " PRETTY_LIST_STRING "[${ARGN}]")
99 set(${OUTPUT_VAR} "${PRETTY_LIST_STRING}" PARENT_SCOPE)
100endfunction()
101
102# The list of (optional) components this version of Ceres was compiled with.
103set(CERES_COMPILED_COMPONENTS "@CERES_COMPILED_COMPONENTS@")
104
105# If Ceres was not installed, then by definition it was exported
106# from a build directory.
107set(CERES_WAS_INSTALLED @SETUP_CERES_CONFIG_FOR_INSTALLATION@)
108
109# Record the state of the CMake module path when this script was
110# called so that we can ensure that we leave it in the same state on
111# exit as it was on entry, but modify it locally.
112set(CALLERS_CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH})
113
114# Get the (current, i.e. installed) directory containing this file.
115get_filename_component(CERES_CURRENT_CONFIG_DIR
116 "${CMAKE_CURRENT_LIST_FILE}" PATH)
117
118if (CERES_WAS_INSTALLED)
119 # Reset CMake module path to the installation directory of this
120 # script, thus we will use the FindPackage() scripts shipped with
121 # Ceres to find Ceres' dependencies, even if the user has equivalently
122 # named FindPackage() scripts in their project.
123 set(CMAKE_MODULE_PATH ${CERES_CURRENT_CONFIG_DIR})
124
125 # Build the absolute root install directory as a relative path
126 # (determined when Ceres was configured & built) from the current
127 # install directory for this this file. This allows for the install
128 # tree to be relocated, after Ceres was built, outside of CMake.
129 get_filename_component(CURRENT_ROOT_INSTALL_DIR
130 ${CERES_CURRENT_CONFIG_DIR}/@INSTALL_ROOT_REL_CONFIG_INSTALL_DIR@
131 ABSOLUTE)
132 if (NOT EXISTS ${CURRENT_ROOT_INSTALL_DIR})
133 ceres_report_not_found(
134 "Ceres install root: ${CURRENT_ROOT_INSTALL_DIR}, "
135 "determined from relative path from CeresConfig.cmake install location: "
136 "${CERES_CURRENT_CONFIG_DIR}, does not exist. Either the install "
137 "directory was deleted, or the install tree was only partially relocated "
138 "outside of CMake after Ceres was built.")
139 endif (NOT EXISTS ${CURRENT_ROOT_INSTALL_DIR})
140
141else(CERES_WAS_INSTALLED)
142 # Ceres was exported from the build tree.
143 set(CERES_EXPORTED_BUILD_DIR ${CERES_CURRENT_CONFIG_DIR})
144 get_filename_component(CERES_EXPORTED_SOURCE_DIR
145 ${CERES_EXPORTED_BUILD_DIR}/@INSTALL_ROOT_REL_CONFIG_INSTALL_DIR@
146 ABSOLUTE)
147 if (NOT EXISTS ${CERES_EXPORTED_SOURCE_DIR})
148 ceres_report_not_found(
149 "Ceres exported source directory: ${CERES_EXPORTED_SOURCE_DIR}, "
150 "determined from relative path from CeresConfig.cmake exported build "
151 "directory: ${CERES_EXPORTED_BUILD_DIR} does not exist.")
152 endif()
153
154 # Reset CMake module path to the cmake directory in the Ceres source
155 # tree which was exported, thus we will use the FindPackage() scripts shipped
156 # with Ceres to find Ceres' dependencies, even if the user has equivalently
157 # named FindPackage() scripts in their project.
158 set(CMAKE_MODULE_PATH ${CERES_EXPORTED_SOURCE_DIR}/cmake)
159endif(CERES_WAS_INSTALLED)
160
161# Set the version.
162set(CERES_VERSION @CERES_VERSION@ )
163
164include(CMakeFindDependencyMacro)
165find_dependency(Threads)
166
167# Eigen.
168# Flag set during configuration and build of Ceres.
169set(CERES_EIGEN_VERSION @EIGEN_VERSION@)
170set(EIGEN_WAS_BUILT_WITH_CMAKE @FOUND_INSTALLED_EIGEN_CMAKE_CONFIGURATION@)
171# Append the locations of Eigen when Ceres was built to the search path hints.
172if (EIGEN_WAS_BUILT_WITH_CMAKE)
173 set(Eigen3_DIR @Eigen3_DIR@)
174 set(EIGEN_PREFER_EXPORTED_EIGEN_CMAKE_CONFIGURATION TRUE)
175else()
176 list(APPEND EIGEN_INCLUDE_DIR_HINTS @EIGEN_INCLUDE_DIR@)
177endif()
178# Search quietly to control the timing of the error message if not found. The
179# search should be for an exact match, but for usability reasons do a soft
180# match and reject with an explanation below.
181find_package(Eigen ${CERES_EIGEN_VERSION} QUIET)
182if (EIGEN_FOUND)
183 if (NOT EIGEN_VERSION VERSION_EQUAL CERES_EIGEN_VERSION)
184 # CMake's VERSION check in FIND_PACKAGE() will accept any version >= the
185 # specified version. However, only version = is supported. Improve
186 # usability by explaining why we don't accept non-exact version matching.
187 ceres_report_not_found("Found Eigen dependency, but the version of Eigen "
188 "found (${EIGEN_VERSION}) does not exactly match the version of Eigen "
189 "Ceres was compiled with (${CERES_EIGEN_VERSION}). This can cause subtle "
190 "bugs by triggering violations of the One Definition Rule. See the "
191 "Wikipedia article http://en.wikipedia.org/wiki/One_Definition_Rule "
192 "for more details")
193 endif ()
194 message(STATUS "Found required Ceres dependency: "
195 "Eigen version ${CERES_EIGEN_VERSION} in ${EIGEN_INCLUDE_DIRS}")
196else (EIGEN_FOUND)
197 ceres_report_not_found("Missing required Ceres "
198 "dependency: Eigen version ${CERES_EIGEN_VERSION}, please set "
199 "EIGEN_INCLUDE_DIR.")
200endif (EIGEN_FOUND)
201
202# Glog.
203# Flag set during configuration and build of Ceres.
204set(CERES_USES_MINIGLOG @MINIGLOG@)
205set(CERES_USES_GFLAGS @GFLAGS@)
206if (CERES_USES_MINIGLOG)
207 # Output message at standard log level (not the lower STATUS) so that
208 # the message is output in GUI during configuration to warn user.
209 message("-- Found Ceres compiled with miniglog substitute "
210 "for glog, beware this will likely cause problems if glog is later linked.")
211else(CERES_USES_MINIGLOG)
212 # As imported CMake targets are not re-exported when a dependent target is
213 # exported, we must invoke find_package(XXX) here to reload the definition
214 # of their targets. Without this, the dependency target names (e.g.
215 # 'gflags-shared') which will be present in the ceres target would not be
216 # defined, and so CMake will assume that they refer to a library name and
217 # fail to link correctly.
218
219 # Append the locations of glog when Ceres was built to the search path hints.
220 set(GLOG_WAS_BUILT_WITH_CMAKE @FOUND_INSTALLED_GLOG_CMAKE_CONFIGURATION@)
221 if (GLOG_WAS_BUILT_WITH_CMAKE)
222 set(glog_DIR @glog_DIR@)
223 set(GLOG_PREFER_EXPORTED_GLOG_CMAKE_CONFIGURATION TRUE)
224 else()
225 list(APPEND GLOG_INCLUDE_DIR_HINTS @GLOG_INCLUDE_DIR@)
226 get_filename_component(CERES_BUILD_GLOG_LIBRARY_DIR @GLOG_LIBRARY@ PATH)
227 list(APPEND GLOG_LIBRARY_DIR_HINTS ${CERES_BUILD_GLOG_LIBRARY_DIR})
228 endif()
229 # Search quietly s/t we control the timing of the error message if not found.
230 find_package(Glog QUIET)
231 if (GLOG_FOUND)
232 message(STATUS "Found required Ceres dependency: glog")
233 else()
234 ceres_report_not_found("Missing required Ceres "
235 "dependency: glog. Searched using GLOG_INCLUDE_DIR_HINTS: "
236 "${GLOG_INCLUDE_DIR_HINTS} and glog_DIR: ${glog_DIR}.")
237 endif()
238
239 # gflags is only a public dependency of Ceres via glog, thus is not required
240 # if Ceres was built with MINIGLOG.
241 if (CERES_USES_GFLAGS)
242 set(GFLAGS_WAS_BUILT_WITH_CMAKE @FOUND_INSTALLED_GFLAGS_CMAKE_CONFIGURATION@)
243 if (GFLAGS_WAS_BUILT_WITH_CMAKE)
244 set(gflags_DIR @gflags_DIR@)
245 set(GFLAGS_PREFER_EXPORTED_GFLAGS_CMAKE_CONFIGURATION TRUE)
246 else()
247 list(APPEND GFLAGS_INCLUDE_DIR_HINTS @GFLAGS_INCLUDE_DIR@)
248 get_filename_component(CERES_BUILD_GFLAGS_LIBRARY_DIR @GFLAGS_LIBRARY@ PATH)
249 list(APPEND GFLAGS_LIBRARY_DIR_HINTS ${CERES_BUILD_GFLAGS_LIBRARY_DIR})
250 endif()
251 # Search quietly s/t we control the timing of the error message if not found.
252 find_package(Gflags QUIET)
253 if (GFLAGS_FOUND)
254 message(STATUS "Found required Ceres dependency: gflags")
255 else()
256 ceres_report_not_found("Missing required Ceres "
257 "dependency: gflags. Searched using GFLAGS_INCLUDE_DIR_HINTS: "
258 "${GFLAGS_INCLUDE_DIR_HINTS} and gflags_DIR: ${gflags_DIR}.")
259 endif()
260 endif()
261endif(CERES_USES_MINIGLOG)
262
263# Import exported Ceres targets, if they have not already been imported.
264if (NOT TARGET ceres AND NOT Ceres_BINARY_DIR)
265 include(${CERES_CURRENT_CONFIG_DIR}/CeresTargets.cmake)
266endif (NOT TARGET ceres AND NOT Ceres_BINARY_DIR)
267# Set the expected XX_LIBRARIES variable for FindPackage().
268set(CERES_LIBRARIES ceres)
269
270# Reset CMake module path to its state when this script was called.
271set(CMAKE_MODULE_PATH ${CALLERS_CMAKE_MODULE_PATH})
272
273# Build the detected Ceres version string to correctly capture whether it
274# was installed, or exported.
275ceres_pretty_print_cmake_list(CERES_COMPILED_COMPONENTS_STRING
276 ${CERES_COMPILED_COMPONENTS})
277if (CERES_WAS_INSTALLED)
278 set(CERES_DETECTED_VERSION_STRING "Ceres version: ${CERES_VERSION} "
279 "installed in: ${CURRENT_ROOT_INSTALL_DIR} with components: "
280 "${CERES_COMPILED_COMPONENTS_STRING}")
281else (CERES_WAS_INSTALLED)
282 set(CERES_DETECTED_VERSION_STRING "Ceres version: ${CERES_VERSION} "
283 "exported from build directory: ${CERES_EXPORTED_BUILD_DIR} with "
284 "components: ${CERES_COMPILED_COMPONENTS_STRING}")
285endif()
286
287# If the user called this script through find_package() whilst specifying
288# particular Ceres components that should be found via:
289# find_package(Ceres COMPONENTS XXX YYY), check the requested components against
290# those with which Ceres was compiled. In this case, we should only report
291# Ceres as found if all the requested components have been found.
292if (Ceres_FIND_COMPONENTS)
293 foreach (REQUESTED_COMPONENT ${Ceres_FIND_COMPONENTS})
294 list(FIND CERES_COMPILED_COMPONENTS ${REQUESTED_COMPONENT} HAVE_REQUESTED_COMPONENT)
295 # list(FIND ..) returns -1 if the element was not in the list, but CMake
296 # interprets if (VAR) to be true if VAR is any non-zero number, even
297 # negative ones, hence we have to explicitly check for >= 0.
298 if (HAVE_REQUESTED_COMPONENT EQUAL -1)
299 # Check for the presence of all requested components before reporting
300 # not found, such that we report all of the missing components rather
301 # than just the first.
302 list(APPEND MISSING_CERES_COMPONENTS ${REQUESTED_COMPONENT})
303 endif()
304 endforeach()
305 if (MISSING_CERES_COMPONENTS)
306 ceres_pretty_print_cmake_list(REQUESTED_CERES_COMPONENTS_STRING
307 ${Ceres_FIND_COMPONENTS})
308 ceres_pretty_print_cmake_list(MISSING_CERES_COMPONENTS_STRING
309 ${MISSING_CERES_COMPONENTS})
310 ceres_report_not_found("Missing requested Ceres components: "
311 "${MISSING_CERES_COMPONENTS_STRING} (components requested: "
312 "${REQUESTED_CERES_COMPONENTS_STRING}). Detected "
313 "${CERES_DETECTED_VERSION_STRING}.")
314 endif()
315endif()
316
317# As we use CERES_REPORT_NOT_FOUND() to abort, if we reach this point we have
318# found Ceres and all required dependencies.
319message(STATUS "Found " ${CERES_DETECTED_VERSION_STRING})
320
321# Set CERES_FOUND to be equivalent to Ceres_FOUND, which is set to
322# TRUE by FindPackage() if this file is found and run, and after which
323# Ceres_FOUND is not (explicitly, i.e. undefined does not count) set
324# to FALSE.
325set(CERES_FOUND TRUE)