blob: e5e297604109571aa3b32fbbd147e59d0db09aa2 [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
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080063include(CMakeFindDependencyMacro)
64
Austin Schuh70cc9552019-01-21 19:46:48 -080065# Called if we failed to find Ceres or any of its required dependencies,
66# unsets all public (designed to be used externally) variables and reports
67# error message at priority depending upon [REQUIRED/QUIET/<NONE>] argument.
68macro(CERES_REPORT_NOT_FOUND REASON_MSG)
69 # FindPackage() only references Ceres_FOUND, and requires it to be
70 # explicitly set FALSE to denote not found (not merely undefined).
71 set(Ceres_FOUND FALSE)
72 set(CERES_FOUND FALSE)
73 unset(CERES_INCLUDE_DIR)
74 unset(CERES_INCLUDE_DIRS)
75 unset(CERES_LIBRARIES)
76
77 # Reset the CMake module path to its state when this script was called.
78 set(CMAKE_MODULE_PATH ${CALLERS_CMAKE_MODULE_PATH})
79
80 # Note <package>_FIND_[REQUIRED/QUIETLY] variables defined by
81 # FindPackage() use the camelcase library name, not uppercase.
82 if (Ceres_FIND_QUIETLY)
83 message(STATUS "Failed to find Ceres - " ${REASON_MSG} ${ARGN})
84 elseif (Ceres_FIND_REQUIRED)
85 message(FATAL_ERROR "Failed to find Ceres - " ${REASON_MSG} ${ARGN})
86 else()
87 # Neither QUIETLY nor REQUIRED, use SEND_ERROR which emits an error
88 # that prevents generation, but continues configuration.
89 message(SEND_ERROR "Failed to find Ceres - " ${REASON_MSG} ${ARGN})
90 endif ()
91 return()
92endmacro(CERES_REPORT_NOT_FOUND)
93
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080094
95# ceres_message([mode] "message text")
96#
97# Wraps the standard cmake 'message' command, but suppresses output
98# if the QUIET flag was passed to the find_package(Ceres ...) call.
99function(ceres_message)
100 if (NOT Ceres_FIND_QUIETLY)
101 message(${ARGN})
102 endif()
103endfunction()
104
105
Austin Schuh70cc9552019-01-21 19:46:48 -0800106# ceres_pretty_print_cmake_list( OUTPUT_VAR [item1 [item2 ... ]] )
107#
108# Sets ${OUTPUT_VAR} in the caller's scope to a human-readable string
109# representation of the list passed as the remaining arguments formed
110# as: "[item1, item2, ..., itemN]".
111function(ceres_pretty_print_cmake_list OUTPUT_VAR)
112 string(REPLACE ";" ", " PRETTY_LIST_STRING "[${ARGN}]")
113 set(${OUTPUT_VAR} "${PRETTY_LIST_STRING}" PARENT_SCOPE)
114endfunction()
115
116# The list of (optional) components this version of Ceres was compiled with.
117set(CERES_COMPILED_COMPONENTS "@CERES_COMPILED_COMPONENTS@")
118
119# If Ceres was not installed, then by definition it was exported
120# from a build directory.
121set(CERES_WAS_INSTALLED @SETUP_CERES_CONFIG_FOR_INSTALLATION@)
122
123# Record the state of the CMake module path when this script was
124# called so that we can ensure that we leave it in the same state on
125# exit as it was on entry, but modify it locally.
126set(CALLERS_CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH})
127
128# Get the (current, i.e. installed) directory containing this file.
129get_filename_component(CERES_CURRENT_CONFIG_DIR
130 "${CMAKE_CURRENT_LIST_FILE}" PATH)
131
132if (CERES_WAS_INSTALLED)
133 # Reset CMake module path to the installation directory of this
134 # script, thus we will use the FindPackage() scripts shipped with
135 # Ceres to find Ceres' dependencies, even if the user has equivalently
136 # named FindPackage() scripts in their project.
137 set(CMAKE_MODULE_PATH ${CERES_CURRENT_CONFIG_DIR})
138
139 # Build the absolute root install directory as a relative path
140 # (determined when Ceres was configured & built) from the current
141 # install directory for this this file. This allows for the install
142 # tree to be relocated, after Ceres was built, outside of CMake.
143 get_filename_component(CURRENT_ROOT_INSTALL_DIR
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800144 "${CERES_CURRENT_CONFIG_DIR}/@INSTALL_ROOT_REL_CONFIG_INSTALL_DIR@"
Austin Schuh70cc9552019-01-21 19:46:48 -0800145 ABSOLUTE)
146 if (NOT EXISTS ${CURRENT_ROOT_INSTALL_DIR})
147 ceres_report_not_found(
148 "Ceres install root: ${CURRENT_ROOT_INSTALL_DIR}, "
149 "determined from relative path from CeresConfig.cmake install location: "
150 "${CERES_CURRENT_CONFIG_DIR}, does not exist. Either the install "
151 "directory was deleted, or the install tree was only partially relocated "
152 "outside of CMake after Ceres was built.")
153 endif (NOT EXISTS ${CURRENT_ROOT_INSTALL_DIR})
154
155else(CERES_WAS_INSTALLED)
156 # Ceres was exported from the build tree.
157 set(CERES_EXPORTED_BUILD_DIR ${CERES_CURRENT_CONFIG_DIR})
158 get_filename_component(CERES_EXPORTED_SOURCE_DIR
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800159 "${CERES_EXPORTED_BUILD_DIR}/@INSTALL_ROOT_REL_CONFIG_INSTALL_DIR@"
Austin Schuh70cc9552019-01-21 19:46:48 -0800160 ABSOLUTE)
161 if (NOT EXISTS ${CERES_EXPORTED_SOURCE_DIR})
162 ceres_report_not_found(
163 "Ceres exported source directory: ${CERES_EXPORTED_SOURCE_DIR}, "
164 "determined from relative path from CeresConfig.cmake exported build "
165 "directory: ${CERES_EXPORTED_BUILD_DIR} does not exist.")
166 endif()
167
168 # Reset CMake module path to the cmake directory in the Ceres source
169 # tree which was exported, thus we will use the FindPackage() scripts shipped
170 # with Ceres to find Ceres' dependencies, even if the user has equivalently
171 # named FindPackage() scripts in their project.
172 set(CMAKE_MODULE_PATH ${CERES_EXPORTED_SOURCE_DIR}/cmake)
173endif(CERES_WAS_INSTALLED)
174
175# Set the version.
176set(CERES_VERSION @CERES_VERSION@ )
177
178include(CMakeFindDependencyMacro)
179find_dependency(Threads)
180
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800181# As imported CMake targets are not re-exported when a dependent target is
182# exported, we must invoke find_package(XXX) here to reload the definition
183# of their targets. Without this, the dependency target names (e.g.
184# 'gflags-shared') which will be present in the ceres target would not be
185# defined, and so CMake will assume that they refer to a library name and
186# fail to link correctly.
187
Austin Schuh70cc9552019-01-21 19:46:48 -0800188# Eigen.
189# Flag set during configuration and build of Ceres.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800190set(CERES_EIGEN_VERSION @EIGEN3_VERSION_STRING@)
Austin Schuh70cc9552019-01-21 19:46:48 -0800191# Search quietly to control the timing of the error message if not found. The
192# search should be for an exact match, but for usability reasons do a soft
193# match and reject with an explanation below.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800194find_package(Eigen3 ${CERES_EIGEN_VERSION} QUIET)
195if (EIGEN3_FOUND)
196 if (NOT EIGEN3_VERSION_STRING VERSION_EQUAL CERES_EIGEN_VERSION)
Austin Schuh70cc9552019-01-21 19:46:48 -0800197 # CMake's VERSION check in FIND_PACKAGE() will accept any version >= the
198 # specified version. However, only version = is supported. Improve
199 # usability by explaining why we don't accept non-exact version matching.
200 ceres_report_not_found("Found Eigen dependency, but the version of Eigen "
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800201 "found (${EIGEN3_VERSION_STRING}) does not exactly match the version of Eigen "
Austin Schuh70cc9552019-01-21 19:46:48 -0800202 "Ceres was compiled with (${CERES_EIGEN_VERSION}). This can cause subtle "
203 "bugs by triggering violations of the One Definition Rule. See the "
204 "Wikipedia article http://en.wikipedia.org/wiki/One_Definition_Rule "
205 "for more details")
206 endif ()
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800207 ceres_message(STATUS "Found required Ceres dependency: "
208 "Eigen version ${CERES_EIGEN_VERSION} in ${EIGEN3_INCLUDE_DIRS}")
209else (EIGEN3_FOUND)
Austin Schuh70cc9552019-01-21 19:46:48 -0800210 ceres_report_not_found("Missing required Ceres "
211 "dependency: Eigen version ${CERES_EIGEN_VERSION}, please set "
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800212 "Eigen3_DIR.")
213endif (EIGEN3_FOUND)
Austin Schuh70cc9552019-01-21 19:46:48 -0800214
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800215# glog (and maybe gflags).
216#
217# Flags set during configuration and build of Ceres.
Austin Schuh70cc9552019-01-21 19:46:48 -0800218set(CERES_USES_MINIGLOG @MINIGLOG@)
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800219set(CERES_GLOG_VERSION @glog_VERSION@)
220set(CERES_GLOG_WAS_BUILT_WITH_CMAKE @FOUND_INSTALLED_GLOG_CMAKE_CONFIGURATION@)
221
Austin Schuh70cc9552019-01-21 19:46:48 -0800222set(CERES_USES_GFLAGS @GFLAGS@)
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800223set(CERES_GFLAGS_VERSION @gflags_VERSION@)
224
Austin Schuh70cc9552019-01-21 19:46:48 -0800225if (CERES_USES_MINIGLOG)
226 # Output message at standard log level (not the lower STATUS) so that
227 # the message is output in GUI during configuration to warn user.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800228 ceres_message("-- Found Ceres compiled with miniglog substitute "
Austin Schuh70cc9552019-01-21 19:46:48 -0800229 "for glog, beware this will likely cause problems if glog is later linked.")
230else(CERES_USES_MINIGLOG)
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800231 if (CERES_GLOG_WAS_BUILT_WITH_CMAKE)
232 find_package(glog ${CERES_GLOG_VERSION} CONFIG QUIET)
233 set(GLOG_FOUND ${glog_FOUND})
Austin Schuh70cc9552019-01-21 19:46:48 -0800234 else()
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800235 # Version of glog against which Ceres was built was not built with CMake,
236 # use the exported glog find_package() module from Ceres to find it again.
237 # Append the locations of glog when Ceres was built to the search path hints.
238 list(APPEND GLOG_INCLUDE_DIR_HINTS "@GLOG_INCLUDE_DIR@")
239 get_filename_component(CERES_BUILD_GLOG_LIBRARY_DIR "@GLOG_LIBRARY@" PATH)
Austin Schuh70cc9552019-01-21 19:46:48 -0800240 list(APPEND GLOG_LIBRARY_DIR_HINTS ${CERES_BUILD_GLOG_LIBRARY_DIR})
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800241
242 # Search quietly s/t we control the timing of the error message if not found.
243 find_package(Glog QUIET)
Austin Schuh70cc9552019-01-21 19:46:48 -0800244 endif()
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800245
Austin Schuh70cc9552019-01-21 19:46:48 -0800246 if (GLOG_FOUND)
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800247 ceres_message(STATUS "Found required Ceres dependency: glog")
Austin Schuh70cc9552019-01-21 19:46:48 -0800248 else()
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800249 ceres_report_not_found("Missing required Ceres dependency: glog.")
Austin Schuh70cc9552019-01-21 19:46:48 -0800250 endif()
251
252 # gflags is only a public dependency of Ceres via glog, thus is not required
253 # if Ceres was built with MINIGLOG.
254 if (CERES_USES_GFLAGS)
Austin Schuh70cc9552019-01-21 19:46:48 -0800255 # Search quietly s/t we control the timing of the error message if not found.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800256 find_package(gflags ${CERES_GFLAGS_VERSION} QUIET)
257 if (gflags_FOUND AND TARGET gflags)
258 ceres_message(STATUS "Found required Ceres dependency: gflags")
Austin Schuh70cc9552019-01-21 19:46:48 -0800259 else()
260 ceres_report_not_found("Missing required Ceres "
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800261 "dependency: gflags (not found, or not found as exported CMake target).")
Austin Schuh70cc9552019-01-21 19:46:48 -0800262 endif()
263 endif()
264endif(CERES_USES_MINIGLOG)
265
266# Import exported Ceres targets, if they have not already been imported.
267if (NOT TARGET ceres AND NOT Ceres_BINARY_DIR)
268 include(${CERES_CURRENT_CONFIG_DIR}/CeresTargets.cmake)
269endif (NOT TARGET ceres AND NOT Ceres_BINARY_DIR)
270# Set the expected XX_LIBRARIES variable for FindPackage().
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800271set(CERES_LIBRARIES Ceres::ceres)
Austin Schuh70cc9552019-01-21 19:46:48 -0800272
273# Reset CMake module path to its state when this script was called.
274set(CMAKE_MODULE_PATH ${CALLERS_CMAKE_MODULE_PATH})
275
276# Build the detected Ceres version string to correctly capture whether it
277# was installed, or exported.
278ceres_pretty_print_cmake_list(CERES_COMPILED_COMPONENTS_STRING
279 ${CERES_COMPILED_COMPONENTS})
280if (CERES_WAS_INSTALLED)
281 set(CERES_DETECTED_VERSION_STRING "Ceres version: ${CERES_VERSION} "
282 "installed in: ${CURRENT_ROOT_INSTALL_DIR} with components: "
283 "${CERES_COMPILED_COMPONENTS_STRING}")
284else (CERES_WAS_INSTALLED)
285 set(CERES_DETECTED_VERSION_STRING "Ceres version: ${CERES_VERSION} "
286 "exported from build directory: ${CERES_EXPORTED_BUILD_DIR} with "
287 "components: ${CERES_COMPILED_COMPONENTS_STRING}")
288endif()
289
290# If the user called this script through find_package() whilst specifying
291# particular Ceres components that should be found via:
292# find_package(Ceres COMPONENTS XXX YYY), check the requested components against
293# those with which Ceres was compiled. In this case, we should only report
294# Ceres as found if all the requested components have been found.
295if (Ceres_FIND_COMPONENTS)
296 foreach (REQUESTED_COMPONENT ${Ceres_FIND_COMPONENTS})
297 list(FIND CERES_COMPILED_COMPONENTS ${REQUESTED_COMPONENT} HAVE_REQUESTED_COMPONENT)
298 # list(FIND ..) returns -1 if the element was not in the list, but CMake
299 # interprets if (VAR) to be true if VAR is any non-zero number, even
300 # negative ones, hence we have to explicitly check for >= 0.
301 if (HAVE_REQUESTED_COMPONENT EQUAL -1)
302 # Check for the presence of all requested components before reporting
303 # not found, such that we report all of the missing components rather
304 # than just the first.
305 list(APPEND MISSING_CERES_COMPONENTS ${REQUESTED_COMPONENT})
306 endif()
307 endforeach()
308 if (MISSING_CERES_COMPONENTS)
309 ceres_pretty_print_cmake_list(REQUESTED_CERES_COMPONENTS_STRING
310 ${Ceres_FIND_COMPONENTS})
311 ceres_pretty_print_cmake_list(MISSING_CERES_COMPONENTS_STRING
312 ${MISSING_CERES_COMPONENTS})
313 ceres_report_not_found("Missing requested Ceres components: "
314 "${MISSING_CERES_COMPONENTS_STRING} (components requested: "
315 "${REQUESTED_CERES_COMPONENTS_STRING}). Detected "
316 "${CERES_DETECTED_VERSION_STRING}.")
317 endif()
318endif()
319
320# As we use CERES_REPORT_NOT_FOUND() to abort, if we reach this point we have
321# found Ceres and all required dependencies.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800322ceres_message(STATUS "Found " ${CERES_DETECTED_VERSION_STRING})
Austin Schuh70cc9552019-01-21 19:46:48 -0800323
324# Set CERES_FOUND to be equivalent to Ceres_FOUND, which is set to
325# TRUE by FindPackage() if this file is found and run, and after which
326# Ceres_FOUND is not (explicitly, i.e. undefined does not count) set
327# to FALSE.
328set(CERES_FOUND TRUE)
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800329
330if (NOT TARGET ceres)
331 # For backwards compatibility, create a local 'alias' target with the
332 # non-namespace-qualified Ceres target name. Note that this is not a
333 # true ALIAS library in CMake terms as they cannot point to imported targets.
334 add_library(ceres INTERFACE IMPORTED)
335 set_target_properties(ceres PROPERTIES INTERFACE_LINK_LIBRARIES Ceres::ceres)
336endif()