FindAccelerateSparse.cmake 6.7 KB

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