1 # Ceres Solver - A fast non-linear least squares minimizer
2 # Copyright 2015 Google Inc. All rights reserved.
3 # http://ceres-solver.org/
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are met:
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.
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.
29 # Authors: pablo.speciale@gmail.com (Pablo Speciale)
30 # alexs.mac@gmail.com (Alex Stewart)
33 # Config file for Ceres Solver - Find Ceres & dependencies.
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.
40 # This module defines the following variables:
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
51 # CERES_VERSION: Version of Ceres found.
53 # CERES_INCLUDE_DIRS: Include directories for Ceres and the
54 # dependencies which appear in the Ceres public
55 # API and are thus required to use Ceres.
57 # CERES_LIBRARIES: Libraries for Ceres and all
58 # dependencies against which Ceres was
59 # compiled. This will not include any optional
60 # dependencies that were disabled when Ceres was
63 # The following variables are also defined for legacy compatibility
64 # only. Any new code should not use them as they do not conform to
65 # the standard CMake FindPackage naming conventions.
67 # CERES_INCLUDES = ${CERES_INCLUDE_DIRS}.
69 # Called if we failed to find Ceres or any of its required dependencies,
70 # unsets all public (designed to be used externally) variables and reports
71 # error message at priority depending upon [REQUIRED/QUIET/<NONE>] argument.
72 macro(CERES_REPORT_NOT_FOUND REASON_MSG)
73 # FindPackage() only references Ceres_FOUND, and requires it to be
74 # explicitly set FALSE to denote not found (not merely undefined).
75 set(Ceres_FOUND FALSE)
76 set(CERES_FOUND FALSE)
77 unset(CERES_INCLUDE_DIRS)
78 unset(CERES_LIBRARIES)
80 # Reset the CMake module path to its state when this script was called.
81 set(CMAKE_MODULE_PATH ${CALLERS_CMAKE_MODULE_PATH})
83 # Note <package>_FIND_[REQUIRED/QUIETLY] variables defined by
84 # FindPackage() use the camelcase library name, not uppercase.
85 if (Ceres_FIND_QUIETLY)
86 message(STATUS "Failed to find Ceres - " ${REASON_MSG} ${ARGN})
87 elseif (Ceres_FIND_REQUIRED)
88 message(FATAL_ERROR "Failed to find Ceres - " ${REASON_MSG} ${ARGN})
90 # Neither QUIETLY nor REQUIRED, use SEND_ERROR which emits an error
91 # that prevents generation, but continues configuration.
92 message(SEND_ERROR "Failed to find Ceres - " ${REASON_MSG} ${ARGN})
95 endmacro(CERES_REPORT_NOT_FOUND)
97 # ceres_pretty_print_cmake_list( OUTPUT_VAR [item1 [item2 ... ]] )
99 # Sets ${OUTPUT_VAR} in the caller's scope to a human-readable string
100 # representation of the list passed as the remaining arguments formed
101 # as: "[item1, item2, ..., itemN]".
102 function(ceres_pretty_print_cmake_list OUTPUT_VAR)
103 string(REPLACE ";" ", " PRETTY_LIST_STRING "[${ARGN}]")
104 set(${OUTPUT_VAR} "${PRETTY_LIST_STRING}" PARENT_SCOPE)
107 # The list of (optional) components this version of Ceres was compiled with.
108 set(CERES_COMPILED_COMPONENTS "@CERES_COMPILED_COMPONENTS@")
110 # If Ceres was not installed, then by definition it was exported
111 # from a build directory.
112 set(CERES_WAS_INSTALLED @SETUP_CERES_CONFIG_FOR_INSTALLATION@)
114 # Record the state of the CMake module path when this script was
115 # called so that we can ensure that we leave it in the same state on
116 # exit as it was on entry, but modify it locally.
117 set(CALLERS_CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH})
119 # Get the (current, i.e. installed) directory containing this file.
120 get_filename_component(CERES_CURRENT_CONFIG_DIR
121 "${CMAKE_CURRENT_LIST_FILE}" PATH)
123 if (CERES_WAS_INSTALLED)
124 # Reset CMake module path to the installation directory of this
125 # script, thus we will use the FindPackage() scripts shipped with
126 # Ceres to find Ceres' dependencies, even if the user has equivalently
127 # named FindPackage() scripts in their project.
128 set(CMAKE_MODULE_PATH ${CERES_CURRENT_CONFIG_DIR})
130 # Build the absolute root install directory as a relative path
131 # (determined when Ceres was configured & built) from the current
132 # install directory for this this file. This allows for the install
133 # tree to be relocated, after Ceres was built, outside of CMake.
134 get_filename_component(CURRENT_ROOT_INSTALL_DIR
135 ${CERES_CURRENT_CONFIG_DIR}/@INSTALL_ROOT_REL_CONFIG_INSTALL_DIR@
137 if (NOT EXISTS ${CURRENT_ROOT_INSTALL_DIR})
138 ceres_report_not_found(
139 "Ceres install root: ${CURRENT_ROOT_INSTALL_DIR}, "
140 "determined from relative path from CeresConfig.cmake install location: "
141 "${CERES_CURRENT_CONFIG_DIR}, does not exist. Either the install "
142 "directory was deleted, or the install tree was only partially relocated "
143 "outside of CMake after Ceres was built.")
144 endif (NOT EXISTS ${CURRENT_ROOT_INSTALL_DIR})
146 # Set the include directories for Ceres (itself).
147 set(CERES_INCLUDE_DIR "${CURRENT_ROOT_INSTALL_DIR}/include")
148 if (NOT EXISTS ${CERES_INCLUDE_DIR}/ceres/ceres.h)
149 ceres_report_not_found(
150 "Ceres install root: ${CURRENT_ROOT_INSTALL_DIR}, "
151 "determined from relative path from CeresConfig.cmake install location: "
152 "${CERES_CURRENT_CONFIG_DIR}, does not contain Ceres headers. "
153 "Either the install directory was deleted, or the install tree was only "
154 "partially relocated outside of CMake after Ceres was built.")
155 endif (NOT EXISTS ${CERES_INCLUDE_DIR}/ceres/ceres.h)
156 list(APPEND CERES_INCLUDE_DIRS ${CERES_INCLUDE_DIR})
158 else(CERES_WAS_INSTALLED)
159 # Ceres was exported from the build tree.
160 set(CERES_EXPORTED_BUILD_DIR ${CERES_CURRENT_CONFIG_DIR})
161 get_filename_component(CERES_EXPORTED_SOURCE_DIR
162 ${CERES_EXPORTED_BUILD_DIR}/@INSTALL_ROOT_REL_CONFIG_INSTALL_DIR@
164 if (NOT EXISTS ${CERES_EXPORTED_SOURCE_DIR})
165 ceres_report_not_found(
166 "Ceres exported source directory: ${CERES_EXPORTED_SOURCE_DIR}, "
167 "determined from relative path from CeresConfig.cmake exported build "
168 "directory: ${CERES_EXPORTED_BUILD_DIR} does not exist.")
171 # Reset CMake module path to the cmake directory in the Ceres source
172 # tree which was exported, thus we will use the FindPackage() scripts shipped
173 # with Ceres to find Ceres' dependencies, even if the user has equivalently
174 # named FindPackage() scripts in their project.
175 set(CMAKE_MODULE_PATH ${CERES_EXPORTED_SOURCE_DIR}/cmake)
177 # Set the include directories for Ceres (itself).
178 set(CERES_INCLUDE_DIR "${CERES_EXPORTED_SOURCE_DIR}/include")
179 if (NOT EXISTS ${CERES_INCLUDE_DIR}/ceres/ceres.h)
180 ceres_report_not_found(
181 "Ceres exported source directory: ${CERES_EXPORTED_SOURCE_DIR}, "
182 "determined from relative path from CeresConfig.cmake exported build "
183 "directory: ${CERES_EXPORTED_BUILD_DIR}, does not contain Ceres headers.")
184 endif (NOT EXISTS ${CERES_INCLUDE_DIR}/ceres/ceres.h)
185 list(APPEND CERES_INCLUDE_DIRS ${CERES_INCLUDE_DIR})
187 # Append the path to the configured config.h in the exported build directory
188 # to the Ceres include directories.
189 set(CERES_CONFIG_FILE
190 ${CERES_EXPORTED_BUILD_DIR}/config/ceres/internal/config.h)
191 if (NOT EXISTS ${CERES_CONFIG_FILE})
192 ceres_report_not_found(
193 "Ceres exported build directory: ${CERES_EXPORTED_BUILD_DIR}, "
194 "does not contain required configured Ceres config.h, it is not here: "
195 "${CERES_CONFIG_FILE}.")
196 endif (NOT EXISTS ${CERES_CONFIG_FILE})
197 list(APPEND CERES_INCLUDE_DIRS ${CERES_EXPORTED_BUILD_DIR}/config)
198 endif(CERES_WAS_INSTALLED)
201 set(CERES_VERSION @CERES_VERSION@ )
204 # Flag set during configuration and build of Ceres.
205 set(CERES_EIGEN_VERSION @EIGEN_VERSION@)
206 set(EIGEN_WAS_BUILT_WITH_CMAKE @FOUND_INSTALLED_EIGEN_CMAKE_CONFIGURATION@)
207 # Append the locations of Eigen when Ceres was built to the search path hints.
208 if (EIGEN_WAS_BUILT_WITH_CMAKE)
209 set(Eigen3_DIR @Eigen3_DIR@)
210 set(EIGEN_PREFER_EXPORTED_EIGEN_CMAKE_CONFIGURATION TRUE)
212 list(APPEND EIGEN_INCLUDE_DIR_HINTS @EIGEN_INCLUDE_DIR@)
214 # Search quietly to control the timing of the error message if not found. The
215 # search should be for an exact match, but for usability reasons do a soft
216 # match and reject with an explanation below.
217 find_package(Eigen ${CERES_EIGEN_VERSION} QUIET)
219 if (NOT EIGEN_VERSION VERSION_EQUAL CERES_EIGEN_VERSION)
220 # CMake's VERSION check in FIND_PACKAGE() will accept any version >= the
221 # specified version. However, only version = is supported. Improve
222 # usability by explaining why we don't accept non-exact version matching.
223 ceres_report_not_found("Found Eigen dependency, but the version of Eigen "
224 "found (${EIGEN_VERSION}) does not exactly match the version of Eigen "
225 "Ceres was compiled with (${CERES_EIGEN_VERSION}). This can cause subtle "
226 "bugs by triggering violations of the One Definition Rule. See the "
227 "Wikipedia article http://en.wikipedia.org/wiki/One_Definition_Rule "
230 message(STATUS "Found required Ceres dependency: "
231 "Eigen version ${CERES_EIGEN_VERSION} in ${EIGEN_INCLUDE_DIRS}")
233 ceres_report_not_found("Missing required Ceres "
234 "dependency: Eigen version ${CERES_EIGEN_VERSION}, please set "
235 "EIGEN_INCLUDE_DIR.")
237 list(APPEND CERES_INCLUDE_DIRS ${EIGEN_INCLUDE_DIRS})
240 # Flag set during configuration and build of Ceres.
241 set(CERES_USES_MINIGLOG @MINIGLOG@)
242 set(CERES_USES_GFLAGS @GFLAGS@)
243 if (CERES_USES_MINIGLOG)
244 set(MINIGLOG_INCLUDE_DIR ${CERES_INCLUDE_DIR}/ceres/internal/miniglog)
245 if (NOT CERES_WAS_INSTALLED)
246 # When Ceres was exported from the build tree, the miniglog headers
247 # will be in Ceres internal source directory, not in the public headers
248 # directory (they are copied with the public headers when installed).
249 set(MINIGLOG_INCLUDE_DIR
250 ${CERES_EXPORTED_SOURCE_DIR}/internal/ceres/miniglog)
252 if (NOT EXISTS ${MINIGLOG_INCLUDE_DIR})
253 ceres_report_not_found(
254 "Failed to find miniglog headers in expected include directory: "
255 "${MINIGLOG_INCLUDE_DIR}, but Ceres was compiled with MINIGLOG enabled "
256 "(in place of glog).")
257 endif (NOT EXISTS ${MINIGLOG_INCLUDE_DIR})
258 list(APPEND CERES_INCLUDE_DIRS ${MINIGLOG_INCLUDE_DIR})
259 # Output message at standard log level (not the lower STATUS) so that
260 # the message is output in GUI during configuration to warn user.
261 message("-- Found Ceres compiled with miniglog substitute "
262 "for glog, beware this will likely cause problems if glog is later linked.")
263 else (CERES_USES_MINIGLOG)
264 # Append the locations of glog when Ceres was built to the search path hints.
265 set(GLOG_WAS_BUILT_WITH_CMAKE @FOUND_INSTALLED_GLOG_CMAKE_CONFIGURATION@)
266 if (GLOG_WAS_BUILT_WITH_CMAKE)
267 set(glog_DIR @glog_DIR@)
268 set(GLOG_PREFER_EXPORTED_GLOG_CMAKE_CONFIGURATION TRUE)
270 list(APPEND GLOG_INCLUDE_DIR_HINTS @GLOG_INCLUDE_DIR@)
271 get_filename_component(CERES_BUILD_GLOG_LIBRARY_DIR @GLOG_LIBRARY@ PATH)
272 list(APPEND GLOG_LIBRARY_DIR_HINTS ${CERES_BUILD_GLOG_LIBRARY_DIR})
275 # Search quietly s/t we control the timing of the error message if not found.
276 find_package(Glog QUIET)
278 message(STATUS "Found required Ceres dependency: glog")
280 ceres_report_not_found("Missing required Ceres "
281 "dependency: glog. Searched using GLOG_INCLUDE_DIR_HINTS: "
282 "${GLOG_INCLUDE_DIR_HINTS} and glog_DIR: ${glog_DIR}.")
284 list(APPEND CERES_INCLUDE_DIRS ${GLOG_INCLUDE_DIRS})
286 # gflags is only a public dependency of Ceres via glog, thus is not required
287 # if Ceres was built with MINIGLOG.
288 if (CERES_USES_GFLAGS)
289 # If gflags was found as an imported CMake target, we need to call
290 # find_packge(Gflags) again here, as imported CMake targets are not
291 # re-exported. Without this, the 'gflags-shared' target name which is
292 # present in CERES_LIBRARIES in this case would not be defined, and so
293 # CMake will assume it is a library name (which it is not) and fail to link.
295 # Append the locations of gflags when Ceres was built to the search path
297 set(GFLAGS_WAS_BUILT_WITH_CMAKE @FOUND_INSTALLED_GFLAGS_CMAKE_CONFIGURATION@)
298 if (GFLAGS_WAS_BUILT_WITH_CMAKE)
299 set(gflags_DIR @gflags_DIR@)
300 set(GFLAGS_PREFER_EXPORTED_GFLAGS_CMAKE_CONFIGURATION TRUE)
302 list(APPEND GFLAGS_INCLUDE_DIR_HINTS @GFLAGS_INCLUDE_DIR@)
303 get_filename_component(CERES_BUILD_GFLAGS_LIBRARY_DIR @GFLAGS_LIBRARY@ PATH)
304 list(APPEND GFLAGS_LIBRARY_DIR_HINTS ${CERES_BUILD_GFLAGS_LIBRARY_DIR})
307 # Search quietly s/t we control the timing of the error message if not found.
308 find_package(Gflags QUIET)
310 message(STATUS "Found required Ceres dependency: gflags")
312 ceres_report_not_found("Missing required Ceres "
313 "dependency: gflags. Searched using GFLAGS_INCLUDE_DIR_HINTS: "
314 "${GFLAGS_INCLUDE_DIR_HINTS} and gflags_DIR: ${gflags_DIR}.")
316 list(APPEND CERES_INCLUDE_DIRS ${GFLAGS_INCLUDE_DIR_HINTS})
318 endif (CERES_USES_MINIGLOG)
320 # Import exported Ceres targets, if they have not already been imported.
321 if (NOT TARGET ceres AND NOT Ceres_BINARY_DIR)
322 include(${CERES_CURRENT_CONFIG_DIR}/CeresTargets.cmake)
323 endif (NOT TARGET ceres AND NOT Ceres_BINARY_DIR)
324 # Set the expected XX_LIBRARIES variable for FindPackage().
325 set(CERES_LIBRARIES ceres)
327 # Make user aware of any compile flags that will be added to their targets
328 # which use Ceres (i.e. flags exported in the Ceres target). Only CMake
329 # versions >= 2.8.12 support target_compile_options().
330 if (TARGET ${CERES_LIBRARIES} AND
331 NOT CMAKE_VERSION VERSION_LESS "2.8.12")
332 get_target_property(CERES_INTERFACE_COMPILE_OPTIONS
333 ${CERES_LIBRARIES} INTERFACE_COMPILE_OPTIONS)
335 if (CERES_WAS_INSTALLED)
336 set(CERES_LOCATION "${CURRENT_ROOT_INSTALL_DIR}")
338 set(CERES_LOCATION "${CERES_EXPORTED_BUILD_DIR}")
341 # Check for -std=c++11 flags.
342 if (CERES_INTERFACE_COMPILE_OPTIONS MATCHES ".*std=c\\+\\+11.*")
343 message(STATUS "Ceres version ${CERES_VERSION} detected here: "
344 "${CERES_LOCATION} was built with C++11. Ceres target will add "
345 "C++11 flags to compile options for targets using it.")
349 # Set legacy include directories variable for backwards compatibility.
350 set(CERES_INCLUDES ${CERES_INCLUDE_DIRS})
352 # Reset CMake module path to its state when this script was called.
353 set(CMAKE_MODULE_PATH ${CALLERS_CMAKE_MODULE_PATH})
355 # Build the detected Ceres version string to correctly capture whether it
356 # was installed, or exported.
357 ceres_pretty_print_cmake_list(CERES_COMPILED_COMPONENTS_STRING
358 ${CERES_COMPILED_COMPONENTS})
359 if (CERES_WAS_INSTALLED)
360 set(CERES_DETECTED_VERSION_STRING "Ceres version: ${CERES_VERSION} "
361 "installed in: ${CURRENT_ROOT_INSTALL_DIR} with components: "
362 "${CERES_COMPILED_COMPONENTS_STRING}")
363 else (CERES_WAS_INSTALLED)
364 set(CERES_DETECTED_VERSION_STRING "Ceres version: ${CERES_VERSION} "
365 "exported from build directory: ${CERES_EXPORTED_BUILD_DIR} with "
366 "components: ${CERES_COMPILED_COMPONENTS_STRING}")
369 # If the user called this script through find_package() whilst specifying
370 # particular Ceres components that should be found via:
371 # find_package(Ceres COMPONENTS XXX YYY), check the requested components against
372 # those with which Ceres was compiled. In this case, we should only report
373 # Ceres as found if all the requested components have been found.
374 if (Ceres_FIND_COMPONENTS)
375 foreach (REQUESTED_COMPONENT ${Ceres_FIND_COMPONENTS})
376 list(FIND CERES_COMPILED_COMPONENTS ${REQUESTED_COMPONENT} HAVE_REQUESTED_COMPONENT)
377 # list(FIND ..) returns -1 if the element was not in the list, but CMake
378 # interprets if (VAR) to be true if VAR is any non-zero number, even
379 # negative ones, hence we have to explicitly check for >= 0.
380 if (HAVE_REQUESTED_COMPONENT EQUAL -1)
381 # Check for the presence of all requested components before reporting
382 # not found, such that we report all of the missing components rather
383 # than just the first.
384 list(APPEND MISSING_CERES_COMPONENTS ${REQUESTED_COMPONENT})
387 if (MISSING_CERES_COMPONENTS)
388 ceres_pretty_print_cmake_list(REQUESTED_CERES_COMPONENTS_STRING
389 ${Ceres_FIND_COMPONENTS})
390 ceres_pretty_print_cmake_list(MISSING_CERES_COMPONENTS_STRING
391 ${MISSING_CERES_COMPONENTS})
392 ceres_report_not_found("Missing requested Ceres components: "
393 "${MISSING_CERES_COMPONENTS_STRING} (components requested: "
394 "${REQUESTED_CERES_COMPONENTS_STRING}). Detected "
395 "${CERES_DETECTED_VERSION_STRING}.")
399 # As we use CERES_REPORT_NOT_FOUND() to abort, if we reach this point we have
400 # found Ceres and all required dependencies.
401 message(STATUS "Found " ${CERES_DETECTED_VERSION_STRING})
403 # Set CERES_FOUND to be equivalent to Ceres_FOUND, which is set to
404 # TRUE by FindPackage() if this file is found and run, and after which
405 # Ceres_FOUND is not (explicitly, i.e. undefined does not count) set
407 set(CERES_FOUND TRUE)