blob: 3f4b0155da4793e19b5c4aa60d2438ddaa796d5b [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
123# Search user-installed locations first, so that we prefer user installs
124# to system installs where both exist.
125#
126# TODO: Add standard Windows search locations for CXSparse.
127list(APPEND CXSPARSE_CHECK_INCLUDE_DIRS
128 /usr/local/include
129 /usr/local/homebrew/include # Mac OS X
130 /opt/local/var/macports/software # Mac OS X.
131 /opt/local/include
132 /usr/include)
133list(APPEND CXSPARSE_CHECK_LIBRARY_DIRS
134 /usr/local/lib
135 /usr/local/homebrew/lib # Mac OS X.
136 /opt/local/lib
137 /usr/lib)
138# Additional suffixes to try appending to each search path.
139list(APPEND CXSPARSE_CHECK_PATH_SUFFIXES
140 suitesparse) # Linux/Windows
141
142# Search supplied hint directories first if supplied.
143find_path(CXSPARSE_INCLUDE_DIR
144 NAMES cs.h
145 HINTS ${CXSPARSE_INCLUDE_DIR_HINTS}
146 PATHS ${CXSPARSE_CHECK_INCLUDE_DIRS}
147 PATH_SUFFIXES ${CXSPARSE_CHECK_PATH_SUFFIXES})
148if (NOT CXSPARSE_INCLUDE_DIR OR
149 NOT EXISTS ${CXSPARSE_INCLUDE_DIR})
150 cxsparse_report_not_found(
151 "Could not find CXSparse include directory, set CXSPARSE_INCLUDE_DIR "
152 "to directory containing cs.h")
153endif (NOT CXSPARSE_INCLUDE_DIR OR
154 NOT EXISTS ${CXSPARSE_INCLUDE_DIR})
155
156find_library(CXSPARSE_LIBRARY NAMES cxsparse
157 HINTS ${CXSPARSE_LIBRARY_DIR_HINTS}
158 PATHS ${CXSPARSE_CHECK_LIBRARY_DIRS}
159 PATH_SUFFIXES ${CXSPARSE_CHECK_PATH_SUFFIXES})
160if (NOT CXSPARSE_LIBRARY OR
161 NOT EXISTS ${CXSPARSE_LIBRARY})
162 cxsparse_report_not_found(
163 "Could not find CXSparse library, set CXSPARSE_LIBRARY "
164 "to full path to libcxsparse.")
165endif (NOT CXSPARSE_LIBRARY OR
166 NOT EXISTS ${CXSPARSE_LIBRARY})
167
168# Mark internally as found, then verify. CXSPARSE_REPORT_NOT_FOUND() unsets
169# if called.
170set(CXSPARSE_FOUND TRUE)
171
172# Extract CXSparse version from cs.h
173if (CXSPARSE_INCLUDE_DIR)
174 set(CXSPARSE_VERSION_FILE ${CXSPARSE_INCLUDE_DIR}/cs.h)
175 if (NOT EXISTS ${CXSPARSE_VERSION_FILE})
176 cxsparse_report_not_found(
177 "Could not find file: ${CXSPARSE_VERSION_FILE} "
178 "containing version information in CXSparse install located at: "
179 "${CXSPARSE_INCLUDE_DIR}.")
180 else (NOT EXISTS ${CXSPARSE_VERSION_FILE})
181 file(READ ${CXSPARSE_INCLUDE_DIR}/cs.h CXSPARSE_VERSION_FILE_CONTENTS)
182
183 string(REGEX MATCH "#define CS_VER [0-9]+"
184 CXSPARSE_MAIN_VERSION "${CXSPARSE_VERSION_FILE_CONTENTS}")
185 string(REGEX REPLACE "#define CS_VER ([0-9]+)" "\\1"
186 CXSPARSE_MAIN_VERSION "${CXSPARSE_MAIN_VERSION}")
187
188 string(REGEX MATCH "#define CS_SUBVER [0-9]+"
189 CXSPARSE_SUB_VERSION "${CXSPARSE_VERSION_FILE_CONTENTS}")
190 string(REGEX REPLACE "#define CS_SUBVER ([0-9]+)" "\\1"
191 CXSPARSE_SUB_VERSION "${CXSPARSE_SUB_VERSION}")
192
193 string(REGEX MATCH "#define CS_SUBSUB [0-9]+"
194 CXSPARSE_SUBSUB_VERSION "${CXSPARSE_VERSION_FILE_CONTENTS}")
195 string(REGEX REPLACE "#define CS_SUBSUB ([0-9]+)" "\\1"
196 CXSPARSE_SUBSUB_VERSION "${CXSPARSE_SUBSUB_VERSION}")
197
198 # This is on a single line s/t CMake does not interpret it as a list of
199 # elements and insert ';' separators which would result in 3.;1.;2 nonsense.
200 set(CXSPARSE_VERSION "${CXSPARSE_MAIN_VERSION}.${CXSPARSE_SUB_VERSION}.${CXSPARSE_SUBSUB_VERSION}")
201 endif (NOT EXISTS ${CXSPARSE_VERSION_FILE})
202endif (CXSPARSE_INCLUDE_DIR)
203
204# Catch the case when the caller has set CXSPARSE_LIBRARY in the cache / GUI and
205# thus FIND_LIBRARY was not called, but specified library is invalid, otherwise
206# we would report CXSparse as found.
207# TODO: This regex for CXSparse library is pretty primitive, we use lowercase
208# for comparison to handle Windows using CamelCase library names, could
209# this check be better?
210string(TOLOWER "${CXSPARSE_LIBRARY}" LOWERCASE_CXSPARSE_LIBRARY)
211if (CXSPARSE_LIBRARY AND
212 EXISTS ${CXSPARSE_LIBRARY} AND
213 NOT "${LOWERCASE_CXSPARSE_LIBRARY}" MATCHES ".*cxsparse[^/]*")
214 cxsparse_report_not_found(
215 "Caller defined CXSPARSE_LIBRARY: "
216 "${CXSPARSE_LIBRARY} does not match CXSparse.")
217endif (CXSPARSE_LIBRARY AND
218 EXISTS ${CXSPARSE_LIBRARY} AND
219 NOT "${LOWERCASE_CXSPARSE_LIBRARY}" MATCHES ".*cxsparse[^/]*")
220
221# Set standard CMake FindPackage variables if found.
222if (CXSPARSE_FOUND)
223 set(CXSPARSE_INCLUDE_DIRS ${CXSPARSE_INCLUDE_DIR})
224 set(CXSPARSE_LIBRARIES ${CXSPARSE_LIBRARY})
225endif (CXSPARSE_FOUND)
226
227cxsparse_reset_find_library_prefix()
228
229# Handle REQUIRED / QUIET optional arguments and version.
230include(FindPackageHandleStandardArgs)
231find_package_handle_standard_args(CXSparse
232 REQUIRED_VARS CXSPARSE_INCLUDE_DIRS CXSPARSE_LIBRARIES
233 VERSION_VAR CXSPARSE_VERSION)
234
235# Only mark internal variables as advanced if we found CXSparse, otherwise
236# leave them visible in the standard GUI for the user to set manually.
237if (CXSPARSE_FOUND)
238 mark_as_advanced(FORCE CXSPARSE_INCLUDE_DIR
239 CXSPARSE_LIBRARY)
240endif (CXSPARSE_FOUND)