blob: 8b380c90e485a4b0ab9bb5aa0cdd9fbe1520ba5e [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# Author: alexs.mac@gmail.com (Alex Stewart)
30#
31
32# FindCXSparse.cmake - Find CXSparse libraries & dependencies.
33#
34# This module defines the following variables which should be referenced
35# by the caller to use the library.
36#
37# CXSPARSE_FOUND: TRUE iff CXSparse and all dependencies have been found.
38# CXSPARSE_INCLUDE_DIRS: Include directories for CXSparse.
39# CXSPARSE_LIBRARIES: Libraries for CXSparse and all dependencies.
40#
41# CXSPARSE_VERSION: Extracted from cs.h.
42# CXSPARSE_MAIN_VERSION: Equal to 3 if CXSPARSE_VERSION = 3.1.2
43# CXSPARSE_SUB_VERSION: Equal to 1 if CXSPARSE_VERSION = 3.1.2
44# CXSPARSE_SUBSUB_VERSION: Equal to 2 if CXSPARSE_VERSION = 3.1.2
45#
46# The following variables control the behaviour of this module:
47#
48# CXSPARSE_INCLUDE_DIR_HINTS: List of additional directories in which to
49# search for CXSparse includes,
50# e.g: /timbuktu/include.
51# CXSPARSE_LIBRARY_DIR_HINTS: List of additional directories in which to
52# search for CXSparse libraries, e.g: /timbuktu/lib.
53#
54# The following variables are also defined by this module, but in line with
55# CMake recommended FindPackage() module style should NOT be referenced directly
56# by callers (use the plural variables detailed above instead). These variables
57# do however affect the behaviour of the module via FIND_[PATH/LIBRARY]() which
58# are NOT re-called (i.e. search for library is not repeated) if these variables
59# are set with valid values _in the CMake cache_. This means that if these
60# variables are set directly in the cache, either by the user in the CMake GUI,
61# or by the user passing -DVAR=VALUE directives to CMake when called (which
62# explicitly defines a cache variable), then they will be used verbatim,
63# bypassing the HINTS variables and other hard-coded search locations.
64#
65# CXSPARSE_INCLUDE_DIR: Include directory for CXSparse, not including the
66# include directory of any dependencies.
67# CXSPARSE_LIBRARY: CXSparse library, not including the libraries of any
68# dependencies.
69
70# Reset CALLERS_CMAKE_FIND_LIBRARY_PREFIXES to its value when
71# FindCXSparse was invoked.
72macro(CXSPARSE_RESET_FIND_LIBRARY_PREFIX)
73 if (MSVC)
74 set(CMAKE_FIND_LIBRARY_PREFIXES "${CALLERS_CMAKE_FIND_LIBRARY_PREFIXES}")
75 endif (MSVC)
76endmacro(CXSPARSE_RESET_FIND_LIBRARY_PREFIX)
77
78# Called if we failed to find CXSparse or any of it's required dependencies,
79# unsets all public (designed to be used externally) variables and reports
80# error message at priority depending upon [REQUIRED/QUIET/<NONE>] argument.
81macro(CXSPARSE_REPORT_NOT_FOUND REASON_MSG)
82 unset(CXSPARSE_FOUND)
83 unset(CXSPARSE_INCLUDE_DIRS)
84 unset(CXSPARSE_LIBRARIES)
85 # Make results of search visible in the CMake GUI if CXSparse has not
86 # been found so that user does not have to toggle to advanced view.
87 mark_as_advanced(CLEAR CXSPARSE_INCLUDE_DIR
88 CXSPARSE_LIBRARY)
89
90 cxsparse_reset_find_library_prefix()
91
92 # Note <package>_FIND_[REQUIRED/QUIETLY] variables defined by FindPackage()
93 # use the camelcase library name, not uppercase.
94 if (CXSparse_FIND_QUIETLY)
95 message(STATUS "Failed to find CXSparse - " ${REASON_MSG} ${ARGN})
96 elseif (CXSparse_FIND_REQUIRED)
97 message(FATAL_ERROR "Failed to find CXSparse - " ${REASON_MSG} ${ARGN})
98 else()
99 # Neither QUIETLY nor REQUIRED, use no priority which emits a message
100 # but continues configuration and allows generation.
101 message("-- Failed to find CXSparse - " ${REASON_MSG} ${ARGN})
102 endif ()
103 return()
104endmacro(CXSPARSE_REPORT_NOT_FOUND)
105
106# Protect against any alternative find_package scripts for this library having
107# been called previously (in a client project) which set CXSPARSE_FOUND, but not
108# the other variables we require / set here which could cause the search logic
109# here to fail.
110unset(CXSPARSE_FOUND)
111
112# Handle possible presence of lib prefix for libraries on MSVC, see
113# also CXSPARSE_RESET_FIND_LIBRARY_PREFIX().
114if (MSVC)
115 # Preserve the caller's original values for CMAKE_FIND_LIBRARY_PREFIXES
116 # s/t we can set it back before returning.
117 set(CALLERS_CMAKE_FIND_LIBRARY_PREFIXES "${CMAKE_FIND_LIBRARY_PREFIXES}")
118 # The empty string in this list is important, it represents the case when
119 # the libraries have no prefix (shared libraries / DLLs).
120 set(CMAKE_FIND_LIBRARY_PREFIXES "lib" "" "${CMAKE_FIND_LIBRARY_PREFIXES}")
121endif (MSVC)
122
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800123# On macOS, add the Homebrew prefix (with appropriate suffixes) to the
124# respective HINTS directories (after any user-specified locations). This
125# handles Homebrew installations into non-standard locations (not /usr/local).
126# We do not use CMAKE_PREFIX_PATH for this as given the search ordering of
127# find_xxx(), doing so would override any user-specified HINTS locations with
128# the Homebrew version if it exists.
129if (CMAKE_SYSTEM_NAME MATCHES "Darwin")
130 find_program(HOMEBREW_EXECUTABLE brew)
131 mark_as_advanced(FORCE HOMEBREW_EXECUTABLE)
132 if (HOMEBREW_EXECUTABLE)
133 # Detected a Homebrew install, query for its install prefix.
134 execute_process(COMMAND ${HOMEBREW_EXECUTABLE} --prefix
135 OUTPUT_VARIABLE HOMEBREW_INSTALL_PREFIX
136 OUTPUT_STRIP_TRAILING_WHITESPACE)
137 message(STATUS "Detected Homebrew with install prefix: "
138 "${HOMEBREW_INSTALL_PREFIX}, adding to CMake search paths.")
139 list(APPEND CXSPARSE_INCLUDE_DIR_HINTS "${HOMEBREW_INSTALL_PREFIX}/include")
140 list(APPEND CXSPARSE_LIBRARY_DIR_HINTS "${HOMEBREW_INSTALL_PREFIX}/lib")
141 endif()
142endif()
143
Austin Schuh70cc9552019-01-21 19:46:48 -0800144# Search user-installed locations first, so that we prefer user installs
145# to system installs where both exist.
146#
147# TODO: Add standard Windows search locations for CXSparse.
148list(APPEND CXSPARSE_CHECK_INCLUDE_DIRS
149 /usr/local/include
150 /usr/local/homebrew/include # Mac OS X
151 /opt/local/var/macports/software # Mac OS X.
152 /opt/local/include
153 /usr/include)
154list(APPEND CXSPARSE_CHECK_LIBRARY_DIRS
155 /usr/local/lib
156 /usr/local/homebrew/lib # Mac OS X.
157 /opt/local/lib
158 /usr/lib)
159# Additional suffixes to try appending to each search path.
160list(APPEND CXSPARSE_CHECK_PATH_SUFFIXES
161 suitesparse) # Linux/Windows
162
163# Search supplied hint directories first if supplied.
164find_path(CXSPARSE_INCLUDE_DIR
165 NAMES cs.h
166 HINTS ${CXSPARSE_INCLUDE_DIR_HINTS}
167 PATHS ${CXSPARSE_CHECK_INCLUDE_DIRS}
168 PATH_SUFFIXES ${CXSPARSE_CHECK_PATH_SUFFIXES})
169if (NOT CXSPARSE_INCLUDE_DIR OR
170 NOT EXISTS ${CXSPARSE_INCLUDE_DIR})
171 cxsparse_report_not_found(
172 "Could not find CXSparse include directory, set CXSPARSE_INCLUDE_DIR "
173 "to directory containing cs.h")
174endif (NOT CXSPARSE_INCLUDE_DIR OR
175 NOT EXISTS ${CXSPARSE_INCLUDE_DIR})
176
177find_library(CXSPARSE_LIBRARY NAMES cxsparse
178 HINTS ${CXSPARSE_LIBRARY_DIR_HINTS}
179 PATHS ${CXSPARSE_CHECK_LIBRARY_DIRS}
180 PATH_SUFFIXES ${CXSPARSE_CHECK_PATH_SUFFIXES})
181if (NOT CXSPARSE_LIBRARY OR
182 NOT EXISTS ${CXSPARSE_LIBRARY})
183 cxsparse_report_not_found(
184 "Could not find CXSparse library, set CXSPARSE_LIBRARY "
185 "to full path to libcxsparse.")
186endif (NOT CXSPARSE_LIBRARY OR
187 NOT EXISTS ${CXSPARSE_LIBRARY})
188
189# Mark internally as found, then verify. CXSPARSE_REPORT_NOT_FOUND() unsets
190# if called.
191set(CXSPARSE_FOUND TRUE)
192
193# Extract CXSparse version from cs.h
194if (CXSPARSE_INCLUDE_DIR)
195 set(CXSPARSE_VERSION_FILE ${CXSPARSE_INCLUDE_DIR}/cs.h)
196 if (NOT EXISTS ${CXSPARSE_VERSION_FILE})
197 cxsparse_report_not_found(
198 "Could not find file: ${CXSPARSE_VERSION_FILE} "
199 "containing version information in CXSparse install located at: "
200 "${CXSPARSE_INCLUDE_DIR}.")
201 else (NOT EXISTS ${CXSPARSE_VERSION_FILE})
202 file(READ ${CXSPARSE_INCLUDE_DIR}/cs.h CXSPARSE_VERSION_FILE_CONTENTS)
203
204 string(REGEX MATCH "#define CS_VER [0-9]+"
205 CXSPARSE_MAIN_VERSION "${CXSPARSE_VERSION_FILE_CONTENTS}")
206 string(REGEX REPLACE "#define CS_VER ([0-9]+)" "\\1"
207 CXSPARSE_MAIN_VERSION "${CXSPARSE_MAIN_VERSION}")
208
209 string(REGEX MATCH "#define CS_SUBVER [0-9]+"
210 CXSPARSE_SUB_VERSION "${CXSPARSE_VERSION_FILE_CONTENTS}")
211 string(REGEX REPLACE "#define CS_SUBVER ([0-9]+)" "\\1"
212 CXSPARSE_SUB_VERSION "${CXSPARSE_SUB_VERSION}")
213
214 string(REGEX MATCH "#define CS_SUBSUB [0-9]+"
215 CXSPARSE_SUBSUB_VERSION "${CXSPARSE_VERSION_FILE_CONTENTS}")
216 string(REGEX REPLACE "#define CS_SUBSUB ([0-9]+)" "\\1"
217 CXSPARSE_SUBSUB_VERSION "${CXSPARSE_SUBSUB_VERSION}")
218
219 # This is on a single line s/t CMake does not interpret it as a list of
220 # elements and insert ';' separators which would result in 3.;1.;2 nonsense.
221 set(CXSPARSE_VERSION "${CXSPARSE_MAIN_VERSION}.${CXSPARSE_SUB_VERSION}.${CXSPARSE_SUBSUB_VERSION}")
222 endif (NOT EXISTS ${CXSPARSE_VERSION_FILE})
223endif (CXSPARSE_INCLUDE_DIR)
224
225# Catch the case when the caller has set CXSPARSE_LIBRARY in the cache / GUI and
226# thus FIND_LIBRARY was not called, but specified library is invalid, otherwise
227# we would report CXSparse as found.
228# TODO: This regex for CXSparse library is pretty primitive, we use lowercase
229# for comparison to handle Windows using CamelCase library names, could
230# this check be better?
231string(TOLOWER "${CXSPARSE_LIBRARY}" LOWERCASE_CXSPARSE_LIBRARY)
232if (CXSPARSE_LIBRARY AND
233 EXISTS ${CXSPARSE_LIBRARY} AND
234 NOT "${LOWERCASE_CXSPARSE_LIBRARY}" MATCHES ".*cxsparse[^/]*")
235 cxsparse_report_not_found(
236 "Caller defined CXSPARSE_LIBRARY: "
237 "${CXSPARSE_LIBRARY} does not match CXSparse.")
238endif (CXSPARSE_LIBRARY AND
239 EXISTS ${CXSPARSE_LIBRARY} AND
240 NOT "${LOWERCASE_CXSPARSE_LIBRARY}" MATCHES ".*cxsparse[^/]*")
241
242# Set standard CMake FindPackage variables if found.
243if (CXSPARSE_FOUND)
244 set(CXSPARSE_INCLUDE_DIRS ${CXSPARSE_INCLUDE_DIR})
245 set(CXSPARSE_LIBRARIES ${CXSPARSE_LIBRARY})
246endif (CXSPARSE_FOUND)
247
248cxsparse_reset_find_library_prefix()
249
250# Handle REQUIRED / QUIET optional arguments and version.
251include(FindPackageHandleStandardArgs)
252find_package_handle_standard_args(CXSparse
253 REQUIRED_VARS CXSPARSE_INCLUDE_DIRS CXSPARSE_LIBRARIES
254 VERSION_VAR CXSPARSE_VERSION)
255
256# Only mark internal variables as advanced if we found CXSparse, otherwise
257# leave them visible in the standard GUI for the user to set manually.
258if (CXSPARSE_FOUND)
259 mark_as_advanced(FORCE CXSPARSE_INCLUDE_DIR
260 CXSPARSE_LIBRARY)
261endif (CXSPARSE_FOUND)