blob: ceb7e269c0fcfcc1fce342de108e0f7a832f49a6 [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001# Ceres Solver - A fast non-linear least squares minimizer
Austin Schuh3de38b02024-06-25 18:25:10 -07002# Copyright 2022 Google Inc. All rights reserved.
Austin Schuh70cc9552019-01-21 19:46:48 -08003# 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.
Austin Schuh3de38b02024-06-25 18:25:10 -0700176set(CERES_VERSION @CERES_VERSION@)
Austin Schuh70cc9552019-01-21 19:46:48 -0800177
178include(CMakeFindDependencyMacro)
Austin Schuh3de38b02024-06-25 18:25:10 -0700179# Optional dependencies
180@METIS_DEPENDENCY@
181@SuiteSparse_DEPENDENCY@
182@CUDAToolkit_DEPENDENCY@
183@Threads_DEPENDENCY@
Austin Schuh70cc9552019-01-21 19:46:48 -0800184
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800185# As imported CMake targets are not re-exported when a dependent target is
186# exported, we must invoke find_package(XXX) here to reload the definition
187# of their targets. Without this, the dependency target names (e.g.
188# 'gflags-shared') which will be present in the ceres target would not be
189# defined, and so CMake will assume that they refer to a library name and
190# fail to link correctly.
191
Austin Schuh70cc9552019-01-21 19:46:48 -0800192# Eigen.
193# Flag set during configuration and build of Ceres.
Austin Schuh3de38b02024-06-25 18:25:10 -0700194set(CERES_EIGEN_VERSION @Eigen3_VERSION@)
Austin Schuh70cc9552019-01-21 19:46:48 -0800195# Search quietly to control the timing of the error message if not found. The
196# search should be for an exact match, but for usability reasons do a soft
197# match and reject with an explanation below.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800198find_package(Eigen3 ${CERES_EIGEN_VERSION} QUIET)
Austin Schuh3de38b02024-06-25 18:25:10 -0700199if (Eigen3_FOUND)
200 if (NOT Eigen3_VERSION VERSION_EQUAL CERES_EIGEN_VERSION)
Austin Schuh70cc9552019-01-21 19:46:48 -0800201 # CMake's VERSION check in FIND_PACKAGE() will accept any version >= the
202 # specified version. However, only version = is supported. Improve
203 # usability by explaining why we don't accept non-exact version matching.
204 ceres_report_not_found("Found Eigen dependency, but the version of Eigen "
Austin Schuh3de38b02024-06-25 18:25:10 -0700205 "found (${Eigen3_VERSION}) does not exactly match the version of Eigen "
Austin Schuh70cc9552019-01-21 19:46:48 -0800206 "Ceres was compiled with (${CERES_EIGEN_VERSION}). This can cause subtle "
207 "bugs by triggering violations of the One Definition Rule. See the "
208 "Wikipedia article http://en.wikipedia.org/wiki/One_Definition_Rule "
209 "for more details")
210 endif ()
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800211 ceres_message(STATUS "Found required Ceres dependency: "
Austin Schuh3de38b02024-06-25 18:25:10 -0700212 "Eigen version ${CERES_EIGEN_VERSION} in ${Eigen3_DIR}")
213else (Eigen3_FOUND)
Austin Schuh70cc9552019-01-21 19:46:48 -0800214 ceres_report_not_found("Missing required Ceres "
215 "dependency: Eigen version ${CERES_EIGEN_VERSION}, please set "
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800216 "Eigen3_DIR.")
Austin Schuh3de38b02024-06-25 18:25:10 -0700217endif (Eigen3_FOUND)
Austin Schuh70cc9552019-01-21 19:46:48 -0800218
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800219# glog (and maybe gflags).
220#
221# Flags set during configuration and build of Ceres.
Austin Schuh70cc9552019-01-21 19:46:48 -0800222set(CERES_USES_MINIGLOG @MINIGLOG@)
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800223set(CERES_GLOG_VERSION @glog_VERSION@)
224set(CERES_GLOG_WAS_BUILT_WITH_CMAKE @FOUND_INSTALLED_GLOG_CMAKE_CONFIGURATION@)
225
Austin Schuh70cc9552019-01-21 19:46:48 -0800226set(CERES_USES_GFLAGS @GFLAGS@)
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800227set(CERES_GFLAGS_VERSION @gflags_VERSION@)
228
Austin Schuh70cc9552019-01-21 19:46:48 -0800229if (CERES_USES_MINIGLOG)
230 # Output message at standard log level (not the lower STATUS) so that
231 # the message is output in GUI during configuration to warn user.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800232 ceres_message("-- Found Ceres compiled with miniglog substitute "
Austin Schuh70cc9552019-01-21 19:46:48 -0800233 "for glog, beware this will likely cause problems if glog is later linked.")
234else(CERES_USES_MINIGLOG)
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800235 if (CERES_GLOG_WAS_BUILT_WITH_CMAKE)
236 find_package(glog ${CERES_GLOG_VERSION} CONFIG QUIET)
237 set(GLOG_FOUND ${glog_FOUND})
Austin Schuh70cc9552019-01-21 19:46:48 -0800238 else()
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800239 # Version of glog against which Ceres was built was not built with CMake,
240 # use the exported glog find_package() module from Ceres to find it again.
241 # Append the locations of glog when Ceres was built to the search path hints.
242 list(APPEND GLOG_INCLUDE_DIR_HINTS "@GLOG_INCLUDE_DIR@")
243 get_filename_component(CERES_BUILD_GLOG_LIBRARY_DIR "@GLOG_LIBRARY@" PATH)
Austin Schuh70cc9552019-01-21 19:46:48 -0800244 list(APPEND GLOG_LIBRARY_DIR_HINTS ${CERES_BUILD_GLOG_LIBRARY_DIR})
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800245
246 # Search quietly s/t we control the timing of the error message if not found.
247 find_package(Glog QUIET)
Austin Schuh70cc9552019-01-21 19:46:48 -0800248 endif()
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800249
Austin Schuh70cc9552019-01-21 19:46:48 -0800250 if (GLOG_FOUND)
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800251 ceres_message(STATUS "Found required Ceres dependency: glog")
Austin Schuh70cc9552019-01-21 19:46:48 -0800252 else()
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800253 ceres_report_not_found("Missing required Ceres dependency: glog.")
Austin Schuh70cc9552019-01-21 19:46:48 -0800254 endif()
255
256 # gflags is only a public dependency of Ceres via glog, thus is not required
257 # if Ceres was built with MINIGLOG.
258 if (CERES_USES_GFLAGS)
Austin Schuh70cc9552019-01-21 19:46:48 -0800259 # Search quietly s/t we control the timing of the error message if not found.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800260 find_package(gflags ${CERES_GFLAGS_VERSION} QUIET)
261 if (gflags_FOUND AND TARGET gflags)
262 ceres_message(STATUS "Found required Ceres dependency: gflags")
Austin Schuh70cc9552019-01-21 19:46:48 -0800263 else()
264 ceres_report_not_found("Missing required Ceres "
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800265 "dependency: gflags (not found, or not found as exported CMake target).")
Austin Schuh70cc9552019-01-21 19:46:48 -0800266 endif()
267 endif()
268endif(CERES_USES_MINIGLOG)
269
270# Import exported Ceres targets, if they have not already been imported.
271if (NOT TARGET ceres AND NOT Ceres_BINARY_DIR)
272 include(${CERES_CURRENT_CONFIG_DIR}/CeresTargets.cmake)
273endif (NOT TARGET ceres AND NOT Ceres_BINARY_DIR)
274# Set the expected XX_LIBRARIES variable for FindPackage().
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800275set(CERES_LIBRARIES Ceres::ceres)
Austin Schuh70cc9552019-01-21 19:46:48 -0800276
277# Reset CMake module path to its state when this script was called.
278set(CMAKE_MODULE_PATH ${CALLERS_CMAKE_MODULE_PATH})
279
280# Build the detected Ceres version string to correctly capture whether it
281# was installed, or exported.
282ceres_pretty_print_cmake_list(CERES_COMPILED_COMPONENTS_STRING
283 ${CERES_COMPILED_COMPONENTS})
284if (CERES_WAS_INSTALLED)
285 set(CERES_DETECTED_VERSION_STRING "Ceres version: ${CERES_VERSION} "
286 "installed in: ${CURRENT_ROOT_INSTALL_DIR} with components: "
287 "${CERES_COMPILED_COMPONENTS_STRING}")
288else (CERES_WAS_INSTALLED)
289 set(CERES_DETECTED_VERSION_STRING "Ceres version: ${CERES_VERSION} "
290 "exported from build directory: ${CERES_EXPORTED_BUILD_DIR} with "
291 "components: ${CERES_COMPILED_COMPONENTS_STRING}")
292endif()
293
294# If the user called this script through find_package() whilst specifying
295# particular Ceres components that should be found via:
296# find_package(Ceres COMPONENTS XXX YYY), check the requested components against
297# those with which Ceres was compiled. In this case, we should only report
298# Ceres as found if all the requested components have been found.
299if (Ceres_FIND_COMPONENTS)
300 foreach (REQUESTED_COMPONENT ${Ceres_FIND_COMPONENTS})
301 list(FIND CERES_COMPILED_COMPONENTS ${REQUESTED_COMPONENT} HAVE_REQUESTED_COMPONENT)
302 # list(FIND ..) returns -1 if the element was not in the list, but CMake
303 # interprets if (VAR) to be true if VAR is any non-zero number, even
304 # negative ones, hence we have to explicitly check for >= 0.
305 if (HAVE_REQUESTED_COMPONENT EQUAL -1)
306 # Check for the presence of all requested components before reporting
307 # not found, such that we report all of the missing components rather
308 # than just the first.
309 list(APPEND MISSING_CERES_COMPONENTS ${REQUESTED_COMPONENT})
310 endif()
311 endforeach()
312 if (MISSING_CERES_COMPONENTS)
313 ceres_pretty_print_cmake_list(REQUESTED_CERES_COMPONENTS_STRING
314 ${Ceres_FIND_COMPONENTS})
315 ceres_pretty_print_cmake_list(MISSING_CERES_COMPONENTS_STRING
316 ${MISSING_CERES_COMPONENTS})
317 ceres_report_not_found("Missing requested Ceres components: "
318 "${MISSING_CERES_COMPONENTS_STRING} (components requested: "
319 "${REQUESTED_CERES_COMPONENTS_STRING}). Detected "
320 "${CERES_DETECTED_VERSION_STRING}.")
321 endif()
322endif()
323
324# As we use CERES_REPORT_NOT_FOUND() to abort, if we reach this point we have
325# found Ceres and all required dependencies.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800326ceres_message(STATUS "Found " ${CERES_DETECTED_VERSION_STRING})
Austin Schuh70cc9552019-01-21 19:46:48 -0800327
328# Set CERES_FOUND to be equivalent to Ceres_FOUND, which is set to
329# TRUE by FindPackage() if this file is found and run, and after which
330# Ceres_FOUND is not (explicitly, i.e. undefined does not count) set
331# to FALSE.
332set(CERES_FOUND TRUE)
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800333
334if (NOT TARGET ceres)
335 # For backwards compatibility, create a local 'alias' target with the
336 # non-namespace-qualified Ceres target name. Note that this is not a
337 # true ALIAS library in CMake terms as they cannot point to imported targets.
338 add_library(ceres INTERFACE IMPORTED)
339 set_target_properties(ceres PROPERTIES INTERFACE_LINK_LIBRARIES Ceres::ceres)
340endif()