blob: f2f434062f7bef5237da31d33bc689c48544ed31 [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001# Ceres Solver - A fast non-linear least squares minimizer
2# Copyright 2018 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# FindAccelerateSparse.cmake - Find the sparse solvers in Apple's Accelerate
33# framework, introduced in Xcode 9.0 (2017).
34# Note that this is distinct from the Accelerate
35# framework on its own, which existed in previous
36# versions but without the sparse solvers.
37#
38# This module defines the following variables which should be referenced
39# by the caller to use the library.
40#
41# AccelerateSparse_FOUND: TRUE iff an Accelerate framework including the sparse
42# solvers, and all dependencies, has been found.
43# AccelerateSparse_INCLUDE_DIRS: Include directories for Accelerate framework.
44# AccelerateSparse_LIBRARIES: Libraries for Accelerate framework and all
45# dependencies.
46#
47# The following variables are also defined by this module, but in line with
48# CMake recommended FindPackage() module style should NOT be referenced directly
49# by callers (use the plural variables detailed above instead). These variables
50# do however affect the behaviour of the module via FIND_[PATH/LIBRARY]() which
51# are NOT re-called (i.e. search for library is not repeated) if these variables
52# are set with valid values _in the CMake cache_. This means that if these
53# variables are set directly in the cache, either by the user in the CMake GUI,
54# or by the user passing -DVAR=VALUE directives to CMake when called (which
55# explicitly defines a cache variable), then they will be used verbatim,
56# bypassing the HINTS variables and other hard-coded search locations.
57#
58# AccelerateSparse_INCLUDE_DIR: Include directory for Accelerate framework, not
59# including the include directory of any
60# dependencies.
61# AccelerateSparse_LIBRARY: Accelerate framework, not including the libraries of
62# any dependencies.
63
64# Called if we failed to find the Accelerate framework with the sparse solvers.
65# Unsets all public (designed to be used externally) variables and reports
66# error message at priority depending upon [REQUIRED/QUIET/<NONE>] argument.
67macro(accelerate_sparse_report_not_found REASON_MSG)
68 unset(AccelerateSparse_FOUND)
69 unset(AccelerateSparse_INCLUDE_DIRS)
70 unset(AccelerateSparse_LIBRARIES)
71 # Make results of search visible in the CMake GUI if Accelerate has not
72 # been found so that user does not have to toggle to advanced view.
73 mark_as_advanced(CLEAR AccelerateSparse_INCLUDE_DIR
74 AccelerateSparse_LIBRARY)
75
76 # Note <package>_FIND_[REQUIRED/QUIETLY] variables defined by FindPackage()
77 # use the camelcase library name, not uppercase.
78 if (AccelerateSparse_FIND_QUIETLY)
79 message(STATUS "Failed to find Accelerate framework with sparse solvers - "
80 ${REASON_MSG} ${ARGN})
81 elseif (AccelerateSparse_FIND_REQUIRED)
82 message(FATAL_ERROR "Failed to find Accelerate framework with sparse solvers - "
83 ${REASON_MSG} ${ARGN})
84 else()
85 # Neither QUIETLY nor REQUIRED, use no priority which emits a message
86 # but continues configuration and allows generation.
87 message("-- Failed to find Accelerate framework with sparse solvers - "
88 ${REASON_MSG} ${ARGN})
89 endif()
90 return()
91endmacro()
92
93unset(AccelerateSparse_FOUND)
94
95find_path(AccelerateSparse_INCLUDE_DIR NAMES Accelerate.h)
96if (NOT AccelerateSparse_INCLUDE_DIR OR
97 NOT EXISTS ${AccelerateSparse_INCLUDE_DIR})
98 accelerate_sparse_report_not_found(
99 "Could not find Accelerate framework headers. Set "
100 "AccelerateSparse_INCLUDE_DIR to the directory containing Accelerate.h")
101endif()
102
103find_library(AccelerateSparse_LIBRARY NAMES Accelerate)
104if (NOT AccelerateSparse_LIBRARY OR
105 NOT EXISTS ${AccelerateSparse_LIBRARY})
106 accelerate_sparse_report_not_found(
107 "Could not find Accelerate framework. Set AccelerateSparse_LIBRARY "
108 "to the Accelerate.framework directory")
109endif()
110
111set(AccelerateSparse_FOUND TRUE)
112
113# Determine if the Accelerate framework detected includes the sparse solvers.
114include(CheckCXXSourceCompiles)
115set(CMAKE_REQUIRED_INCLUDES ${AccelerateSparse_INCLUDE_DIR})
116set(CMAKE_REQUIRED_LIBRARIES ${AccelerateSparse_LIBRARY})
117check_cxx_source_compiles(
118 "#include <Accelerate.h>
119 int main() {
120 SparseMatrix_Double A;
121 SparseFactor(SparseFactorizationCholesky, A);
122 return 0;
123 }"
124 ACCELERATE_FRAMEWORK_HAS_SPARSE_SOLVER)
125unset(CMAKE_REQUIRED_INCLUDES)
126unset(CMAKE_REQUIRED_LIBRARIES)
127if (NOT ACCELERATE_FRAMEWORK_HAS_SPARSE_SOLVER)
128 accelerate_sparse_report_not_found(
129 "Detected Accelerate framework: ${AccelerateSparse_LIBRARY} does not "
130 "include the sparse solvers.")
131endif()
132
133if (AccelerateSparse_FOUND)
134 set(AccelerateSparse_INCLUDE_DIRS ${AccelerateSparse_INCLUDE_DIR})
135 set(AccelerateSparse_LIBRARIES ${AccelerateSparse_LIBRARY})
136endif()
137
138# Handle REQUIRED / QUIET optional arguments and version.
139include(FindPackageHandleStandardArgs)
140find_package_handle_standard_args(AccelerateSparse
141 REQUIRED_VARS AccelerateSparse_INCLUDE_DIRS AccelerateSparse_LIBRARIES)
142if (AccelerateSparse_FOUND)
143 mark_as_advanced(FORCE AccelerateSparse_INCLUDE_DIR
144 AccelerateSparse_LIBRARY)
145endif()