1# Ceres Solver - A fast non-linear least squares minimizer
2# Copyright 2013 Google Inc. All rights reserved.
3# http://code.google.com/p/ceres-solver/
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# Called if we failed to find CXSparse or any of it's required dependencies,
71# unsets all public (designed to be used externally) variables and reports
72# error message at priority depending upon [REQUIRED/QUIET/<NONE>] argument.
73MACRO(CXSPARSE_REPORT_NOT_FOUND REASON_MSG)
74  UNSET(CXSPARSE_FOUND)
75  UNSET(CXSPARSE_INCLUDE_DIRS)
76  UNSET(CXSPARSE_LIBRARIES)
77  # Make results of search visible in the CMake GUI if CXSparse has not
78  # been found so that user does not have to toggle to advanced view.
79  MARK_AS_ADVANCED(CLEAR CXSPARSE_INCLUDE_DIR
80                         CXSPARSE_LIBRARY)
81  # Note <package>_FIND_[REQUIRED/QUIETLY] variables defined by FindPackage()
82  # use the camelcase library name, not uppercase.
83  IF (CXSparse_FIND_QUIETLY)
84    MESSAGE(STATUS "Failed to find CXSparse - " ${REASON_MSG} ${ARGN})
85  ELSEIF (CXSparse_FIND_REQUIRED)
86    MESSAGE(FATAL_ERROR "Failed to find CXSparse - " ${REASON_MSG} ${ARGN})
87  ELSE()
88    # Neither QUIETLY nor REQUIRED, use no priority which emits a message
89    # but continues configuration and allows generation.
90    MESSAGE("-- Failed to find CXSparse - " ${REASON_MSG} ${ARGN})
91  ENDIF ()
92ENDMACRO(CXSPARSE_REPORT_NOT_FOUND)
93
94# Search user-installed locations first, so that we prefer user installs
95# to system installs where both exist.
96#
97# TODO: Add standard Windows search locations for CXSparse.
98LIST(APPEND CXSPARSE_CHECK_INCLUDE_DIRS
99  /usr/local/include
100  /usr/local/homebrew/include # Mac OS X
101  /opt/local/var/macports/software # Mac OS X.
102  /opt/local/include
103  /usr/include)
104LIST(APPEND CXSPARSE_CHECK_LIBRARY_DIRS
105  /usr/local/lib
106  /usr/local/homebrew/lib # Mac OS X.
107  /opt/local/lib
108  /usr/lib)
109
110# Search supplied hint directories first if supplied.
111FIND_PATH(CXSPARSE_INCLUDE_DIR
112  NAMES cs.h
113  PATHS ${CXSPARSE_INCLUDE_DIR_HINTS}
114  ${CXSPARSE_CHECK_INCLUDE_DIRS})
115IF (NOT CXSPARSE_INCLUDE_DIR OR
116    NOT EXISTS ${CXSPARSE_INCLUDE_DIR})
117  CXSPARSE_REPORT_NOT_FOUND(
118    "Could not find CXSparse include directory, set CXSPARSE_INCLUDE_DIR "
119    "to directory containing cs.h")
120ENDIF (NOT CXSPARSE_INCLUDE_DIR OR
121       NOT EXISTS ${CXSPARSE_INCLUDE_DIR})
122
123FIND_LIBRARY(CXSPARSE_LIBRARY NAMES cxsparse
124  PATHS ${CXSPARSE_LIBRARY_DIR_HINTS}
125  ${CXSPARSE_CHECK_LIBRARY_DIRS})
126IF (NOT CXSPARSE_LIBRARY OR
127    NOT EXISTS ${CXSPARSE_LIBRARY})
128  CXSPARSE_REPORT_NOT_FOUND(
129    "Could not find CXSparse library, set CXSPARSE_LIBRARY "
130    "to full path to libcxsparse.")
131ENDIF (NOT CXSPARSE_LIBRARY OR
132       NOT EXISTS ${CXSPARSE_LIBRARY})
133
134# Mark internally as found, then verify. CXSPARSE_REPORT_NOT_FOUND() unsets
135# if called.
136SET(CXSPARSE_FOUND TRUE)
137
138# Extract CXSparse version from cs.h
139IF (CXSPARSE_INCLUDE_DIR)
140  SET(CXSPARSE_VERSION_FILE ${CXSPARSE_INCLUDE_DIR}/cs.h)
141  IF (NOT EXISTS ${CXSPARSE_VERSION_FILE})
142    CXSPARSE_REPORT_NOT_FOUND(
143      "Could not find file: ${CXSPARSE_VERSION_FILE} "
144      "containing version information in CXSparse install located at: "
145      "${CXSPARSE_INCLUDE_DIR}.")
146  ELSE (NOT EXISTS ${CXSPARSE_VERSION_FILE})
147    FILE(READ ${CXSPARSE_INCLUDE_DIR}/cs.h CXSPARSE_VERSION_FILE_CONTENTS)
148
149    STRING(REGEX MATCH "#define CS_VER [0-9]+"
150      CXSPARSE_MAIN_VERSION "${CXSPARSE_VERSION_FILE_CONTENTS}")
151    STRING(REGEX REPLACE "#define CS_VER ([0-9]+)" "\\1"
152      CXSPARSE_MAIN_VERSION "${CXSPARSE_MAIN_VERSION}")
153
154    STRING(REGEX MATCH "#define CS_SUBVER [0-9]+"
155      CXSPARSE_SUB_VERSION "${CXSPARSE_VERSION_FILE_CONTENTS}")
156    STRING(REGEX REPLACE "#define CS_SUBVER ([0-9]+)" "\\1"
157      CXSPARSE_SUB_VERSION "${CXSPARSE_SUB_VERSION}")
158
159    STRING(REGEX MATCH "#define CS_SUBSUB [0-9]+"
160      CXSPARSE_SUBSUB_VERSION "${CXSPARSE_VERSION_FILE_CONTENTS}")
161    STRING(REGEX REPLACE "#define CS_SUBSUB ([0-9]+)" "\\1"
162      CXSPARSE_SUBSUB_VERSION "${CXSPARSE_SUBSUB_VERSION}")
163
164    # This is on a single line s/t CMake does not interpret it as a list of
165    # elements and insert ';' separators which would result in 3.;1.;2 nonsense.
166    SET(CXSPARSE_VERSION "${CXSPARSE_MAIN_VERSION}.${CXSPARSE_SUB_VERSION}.${CXSPARSE_SUBSUB_VERSION}")
167  ENDIF (NOT EXISTS ${CXSPARSE_VERSION_FILE})
168ENDIF (CXSPARSE_INCLUDE_DIR)
169
170# Catch the case when the caller has set CXSPARSE_LIBRARY in the cache / GUI and
171# thus FIND_LIBRARY was not called, but specified library is invalid, otherwise
172# we would report CXSparse as found.
173# TODO: This regex for CXSparse library is pretty primitive, we use lowercase
174#       for comparison to handle Windows using CamelCase library names, could
175#       this check be better?
176STRING(TOLOWER "${CXSPARSE_LIBRARY}" LOWERCASE_CXSPARSE_LIBRARY)
177IF (CXSPARSE_LIBRARY AND
178    EXISTS ${CXSPARSE_LIBRARY} AND
179    NOT "${LOWERCASE_CXSPARSE_LIBRARY}" MATCHES ".*cxsparse[^/]*")
180  CXSPARSE_REPORT_NOT_FOUND(
181    "Caller defined CXSPARSE_LIBRARY: "
182    "${CXSPARSE_LIBRARY} does not match CXSparse.")
183ENDIF (CXSPARSE_LIBRARY AND
184       EXISTS ${CXSPARSE_LIBRARY} AND
185       NOT "${LOWERCASE_CXSPARSE_LIBRARY}" MATCHES ".*cxsparse[^/]*")
186
187# Set standard CMake FindPackage variables if found.
188IF (CXSPARSE_FOUND)
189  SET(CXSPARSE_INCLUDE_DIRS ${CXSPARSE_INCLUDE_DIR})
190  SET(CXSPARSE_LIBRARIES ${CXSPARSE_LIBRARY})
191ENDIF (CXSPARSE_FOUND)
192
193# Handle REQUIRED / QUIET optional arguments and version.
194INCLUDE(FindPackageHandleStandardArgs)
195FIND_PACKAGE_HANDLE_STANDARD_ARGS(CXSparse
196  REQUIRED_VARS CXSPARSE_INCLUDE_DIRS CXSPARSE_LIBRARIES
197  VERSION_VAR CXSPARSE_VERSION)
198
199# Only mark internal variables as advanced if we found CXSparse, otherwise
200# leave them visible in the standard GUI for the user to set manually.
201IF (CXSPARSE_FOUND)
202  MARK_AS_ADVANCED(FORCE CXSPARSE_INCLUDE_DIR
203                         CXSPARSE_LIBRARY)
204ENDIF (CXSPARSE_FOUND)
205