1# 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# FindSuiteSparse.cmake - Find SuiteSparse libraries & dependencies.
33#
34# This module defines the following variables:
35#
36# SUITESPARSE_FOUND: TRUE iff SuiteSparse and all dependencies have been found.
37# SUITESPARSE_INCLUDE_DIRS: Include directories for all SuiteSparse components.
38# SUITESPARSE_LIBRARIES: Libraries for all SuiteSparse component libraries and
39#                        dependencies.
40# SUITESPARSE_VERSION: Extracted from UFconfig.h (<= v3) or
41#                      SuiteSparse_config.h (>= v4).
42# SUITESPARSE_MAIN_VERSION: Equal to 4 if SUITESPARSE_VERSION = 4.2.1
43# SUITESPARSE_SUB_VERSION: Equal to 2 if SUITESPARSE_VERSION = 4.2.1
44# SUITESPARSE_SUBSUB_VERSION: Equal to 1 if SUITESPARSE_VERSION = 4.2.1
45#
46# SUITESPARSE_IS_BROKEN_SHARED_LINKING_UBUNTU_SYSTEM_VERSION: TRUE iff running
47#     on Ubuntu, SUITESPARSE_VERSION is 3.4.0 and found SuiteSparse is a system
48#     install, in which case found version of SuiteSparse cannot be used to link
49#     a shared library due to a bug (static linking is unaffected).
50#
51# The following variables control the behaviour of this module:
52#
53# SUITESPARSE_INCLUDE_DIR_HINTS: List of additional directories in which to
54#                                search for SuiteSparse includes,
55#                                e.g: /timbuktu/include.
56# SUITESPARSE_LIBRARY_DIR_HINTS: List of additional directories in which to
57#                                search for SuiteSparse libraries,
58#                                e.g: /timbuktu/lib.
59#
60# The following variables define the presence / includes & libraries for the
61# SuiteSparse components searched for, the SUITESPARSE_XX variables are the
62# union of the variables for all components.
63#
64# == Symmetric Approximate Minimum Degree (AMD)
65# AMD_FOUND
66# AMD_INCLUDE_DIR
67# AMD_LIBRARY
68#
69# == Constrained Approximate Minimum Degree (CAMD)
70# CAMD_FOUND
71# CAMD_INCLUDE_DIR
72# CAMD_LIBRARY
73#
74# == Column Approximate Minimum Degree (COLAMD)
75# COLAMD_FOUND
76# COLAMD_INCLUDE_DIR
77# COLAMD_LIBRARY
78#
79# Constrained Column Approximate Minimum Degree (CCOLAMD)
80# CCOLAMD_FOUND
81# CCOLAMD_INCLUDE_DIR
82# CCOLAMD_LIBRARY
83#
84# == Sparse Supernodal Cholesky Factorization and Update/Downdate (CHOLMOD)
85# CHOLMOD_FOUND
86# CHOLMOD_INCLUDE_DIR
87# CHOLMOD_LIBRARY
88#
89# == Multifrontal Sparse QR (SuiteSparseQR)
90# SUITESPARSEQR_FOUND
91# SUITESPARSEQR_INCLUDE_DIR
92# SUITESPARSEQR_LIBRARY
93#
94# == Common configuration for all but CSparse (SuiteSparse version >= 4).
95# SUITESPARSE_CONFIG_FOUND
96# SUITESPARSE_CONFIG_INCLUDE_DIR
97# SUITESPARSE_CONFIG_LIBRARY
98#
99# == Common configuration for all but CSparse (SuiteSparse version < 4).
100# UFCONFIG_FOUND
101# UFCONFIG_INCLUDE_DIR
102#
103# Optional SuiteSparse Dependencies:
104#
105# == Serial Graph Partitioning and Fill-reducing Matrix Ordering (METIS)
106# METIS_FOUND
107# METIS_LIBRARY
108
109# Reset CALLERS_CMAKE_FIND_LIBRARY_PREFIXES to its value when
110# FindSuiteSparse was invoked.
111macro(SUITESPARSE_RESET_FIND_LIBRARY_PREFIX)
112  if (MSVC)
113    set(CMAKE_FIND_LIBRARY_PREFIXES "${CALLERS_CMAKE_FIND_LIBRARY_PREFIXES}")
114  endif (MSVC)
115endmacro(SUITESPARSE_RESET_FIND_LIBRARY_PREFIX)
116
117# Called if we failed to find SuiteSparse or any of it's required dependencies,
118# unsets all public (designed to be used externally) variables and reports
119# error message at priority depending upon [REQUIRED/QUIET/<NONE>] argument.
120macro(SUITESPARSE_REPORT_NOT_FOUND REASON_MSG)
121  unset(SUITESPARSE_FOUND)
122  unset(SUITESPARSE_INCLUDE_DIRS)
123  unset(SUITESPARSE_LIBRARIES)
124  unset(SUITESPARSE_VERSION)
125  unset(SUITESPARSE_MAIN_VERSION)
126  unset(SUITESPARSE_SUB_VERSION)
127  unset(SUITESPARSE_SUBSUB_VERSION)
128  # Do NOT unset SUITESPARSE_FOUND_REQUIRED_VARS here, as it is used by
129  # FindPackageHandleStandardArgs() to generate the automatic error message on
130  # failure which highlights which components are missing.
131
132  suitesparse_reset_find_library_prefix()
133
134  # Note <package>_FIND_[REQUIRED/QUIETLY] variables defined by FindPackage()
135  # use the camelcase library name, not uppercase.
136  if (SuiteSparse_FIND_QUIETLY)
137    message(STATUS "Failed to find SuiteSparse - " ${REASON_MSG} ${ARGN})
138  elseif (SuiteSparse_FIND_REQUIRED)
139    message(FATAL_ERROR "Failed to find SuiteSparse - " ${REASON_MSG} ${ARGN})
140  else()
141    # Neither QUIETLY nor REQUIRED, use no priority which emits a message
142    # but continues configuration and allows generation.
143    message("-- Failed to find SuiteSparse - " ${REASON_MSG} ${ARGN})
144  endif (SuiteSparse_FIND_QUIETLY)
145
146  # Do not call return(), s/t we keep processing if not called with REQUIRED
147  # and report all missing components, rather than bailing after failing to find
148  # the first.
149endmacro(SUITESPARSE_REPORT_NOT_FOUND)
150
151# Protect against any alternative find_package scripts for this library having
152# been called previously (in a client project) which set SUITESPARSE_FOUND, but
153# not the other variables we require / set here which could cause the search
154# logic here to fail.
155unset(SUITESPARSE_FOUND)
156
157# Handle possible presence of lib prefix for libraries on MSVC, see
158# also SUITESPARSE_RESET_FIND_LIBRARY_PREFIX().
159if (MSVC)
160  # Preserve the caller's original values for CMAKE_FIND_LIBRARY_PREFIXES
161  # s/t we can set it back before returning.
162  set(CALLERS_CMAKE_FIND_LIBRARY_PREFIXES "${CMAKE_FIND_LIBRARY_PREFIXES}")
163  # The empty string in this list is important, it represents the case when
164  # the libraries have no prefix (shared libraries / DLLs).
165  set(CMAKE_FIND_LIBRARY_PREFIXES "lib" "" "${CMAKE_FIND_LIBRARY_PREFIXES}")
166endif (MSVC)
167
168# On macOS, add the Homebrew prefix (with appropriate suffixes) to the
169# respective HINTS directories (after any user-specified locations).  This
170# handles Homebrew installations into non-standard locations (not /usr/local).
171# We do not use CMAKE_PREFIX_PATH for this as given the search ordering of
172# find_xxx(), doing so would override any user-specified HINTS locations with
173# the Homebrew version if it exists.
174if (CMAKE_SYSTEM_NAME MATCHES "Darwin")
175  find_program(HOMEBREW_EXECUTABLE brew)
176  mark_as_advanced(FORCE HOMEBREW_EXECUTABLE)
177  if (HOMEBREW_EXECUTABLE)
178    # Detected a Homebrew install, query for its install prefix.
179    execute_process(COMMAND ${HOMEBREW_EXECUTABLE} --prefix
180      OUTPUT_VARIABLE HOMEBREW_INSTALL_PREFIX
181      OUTPUT_STRIP_TRAILING_WHITESPACE)
182    message(STATUS "Detected Homebrew with install prefix: "
183      "${HOMEBREW_INSTALL_PREFIX}, adding to CMake search paths.")
184    list(APPEND SUITESPARSE_INCLUDE_DIR_HINTS "${HOMEBREW_INSTALL_PREFIX}/include")
185    list(APPEND SUITESPARSE_LIBRARY_DIR_HINTS "${HOMEBREW_INSTALL_PREFIX}/lib")
186  endif()
187endif()
188
189# Specify search directories for include files and libraries (this is the union
190# of the search directories for all OSs).  Search user-specified hint
191# directories first if supplied, and search user-installed locations first
192# so that we prefer user installs to system installs where both exist.
193list(APPEND SUITESPARSE_CHECK_INCLUDE_DIRS
194  /opt/local/include
195  /opt/local/include/ufsparse # Mac OS X
196  /usr/local/homebrew/include # Mac OS X
197  /usr/local/include
198  /usr/include)
199list(APPEND SUITESPARSE_CHECK_LIBRARY_DIRS
200  /opt/local/lib
201  /opt/local/lib/ufsparse # Mac OS X
202  /usr/local/homebrew/lib # Mac OS X
203  /usr/local/lib
204  /usr/lib)
205# Additional suffixes to try appending to each search path.
206list(APPEND SUITESPARSE_CHECK_PATH_SUFFIXES
207  suitesparse) # Windows/Ubuntu
208
209# Wrappers to find_path/library that pass the SuiteSparse search hints/paths.
210#
211# suitesparse_find_component(<component> [FILES name1 [name2 ...]]
212#                                        [LIBRARIES name1 [name2 ...]]
213#                                        [REQUIRED])
214macro(suitesparse_find_component COMPONENT)
215  include(CMakeParseArguments)
216  set(OPTIONS REQUIRED)
217  set(MULTI_VALUE_ARGS FILES LIBRARIES)
218  cmake_parse_arguments(SUITESPARSE_FIND_${COMPONENT}
219    "${OPTIONS}" "" "${MULTI_VALUE_ARGS}" ${ARGN})
220
221  if (SUITESPARSE_FIND_${COMPONENT}_REQUIRED)
222    list(APPEND SUITESPARSE_FOUND_REQUIRED_VARS ${COMPONENT}_FOUND)
223  endif()
224
225  set(${COMPONENT}_FOUND TRUE)
226  if (SUITESPARSE_FIND_${COMPONENT}_FILES)
227    find_path(${COMPONENT}_INCLUDE_DIR
228      NAMES ${SUITESPARSE_FIND_${COMPONENT}_FILES}
229      HINTS ${SUITESPARSE_INCLUDE_DIR_HINTS}
230      PATHS ${SUITESPARSE_CHECK_INCLUDE_DIRS}
231      PATH_SUFFIXES ${SUITESPARSE_CHECK_PATH_SUFFIXES})
232    if (${COMPONENT}_INCLUDE_DIR)
233      message(STATUS "Found ${COMPONENT} headers in: "
234        "${${COMPONENT}_INCLUDE_DIR}")
235      mark_as_advanced(${COMPONENT}_INCLUDE_DIR)
236    else()
237      # Specified headers not found.
238      set(${COMPONENT}_FOUND FALSE)
239      if (SUITESPARSE_FIND_${COMPONENT}_REQUIRED)
240        suitesparse_report_not_found(
241          "Did not find ${COMPONENT} header (required SuiteSparse component).")
242      else()
243        message(STATUS "Did not find ${COMPONENT} header (optional "
244          "SuiteSparse component).")
245        # Hide optional vars from CMake GUI even if not found.
246        mark_as_advanced(${COMPONENT}_INCLUDE_DIR)
247      endif()
248    endif()
249  endif()
250
251  if (SUITESPARSE_FIND_${COMPONENT}_LIBRARIES)
252    find_library(${COMPONENT}_LIBRARY
253      NAMES ${SUITESPARSE_FIND_${COMPONENT}_LIBRARIES}
254      HINTS ${SUITESPARSE_LIBRARY_DIR_HINTS}
255      PATHS ${SUITESPARSE_CHECK_LIBRARY_DIRS}
256      PATH_SUFFIXES ${SUITESPARSE_CHECK_PATH_SUFFIXES})
257    if (${COMPONENT}_LIBRARY)
258      message(STATUS "Found ${COMPONENT} library: ${${COMPONENT}_LIBRARY}")
259      mark_as_advanced(${COMPONENT}_LIBRARY)
260    else ()
261      # Specified libraries not found.
262      set(${COMPONENT}_FOUND FALSE)
263      if (SUITESPARSE_FIND_${COMPONENT}_REQUIRED)
264        suitesparse_report_not_found(
265          "Did not find ${COMPONENT} library (required SuiteSparse component).")
266      else()
267        message(STATUS "Did not find ${COMPONENT} library (optional SuiteSparse "
268          "dependency)")
269        # Hide optional vars from CMake GUI even if not found.
270        mark_as_advanced(${COMPONENT}_LIBRARY)
271      endif()
272    endif()
273  endif()
274endmacro()
275
276# Given the number of components of SuiteSparse, and to ensure that the
277# automatic failure message generated by FindPackageHandleStandardArgs()
278# when not all required components are found is helpful, we maintain a list
279# of all variables that must be defined for SuiteSparse to be considered found.
280unset(SUITESPARSE_FOUND_REQUIRED_VARS)
281
282# BLAS.
283find_package(BLAS QUIET)
284if (NOT BLAS_FOUND)
285  suitesparse_report_not_found(
286    "Did not find BLAS library (required for SuiteSparse).")
287endif (NOT BLAS_FOUND)
288list(APPEND SUITESPARSE_FOUND_REQUIRED_VARS BLAS_FOUND)
289
290# LAPACK.
291find_package(LAPACK QUIET)
292if (NOT LAPACK_FOUND)
293  suitesparse_report_not_found(
294    "Did not find LAPACK library (required for SuiteSparse).")
295endif (NOT LAPACK_FOUND)
296list(APPEND SUITESPARSE_FOUND_REQUIRED_VARS LAPACK_FOUND)
297
298suitesparse_find_component(AMD REQUIRED FILES amd.h LIBRARIES amd)
299suitesparse_find_component(CAMD REQUIRED FILES camd.h LIBRARIES camd)
300suitesparse_find_component(COLAMD REQUIRED FILES colamd.h LIBRARIES colamd)
301suitesparse_find_component(CCOLAMD REQUIRED FILES ccolamd.h LIBRARIES ccolamd)
302suitesparse_find_component(CHOLMOD REQUIRED FILES cholmod.h LIBRARIES cholmod)
303suitesparse_find_component(
304  SUITESPARSEQR REQUIRED FILES SuiteSparseQR.hpp LIBRARIES spqr)
305if (SUITESPARSEQR_FOUND)
306  # SuiteSparseQR may be compiled with Intel Threading Building Blocks,
307  # we assume that if TBB is installed, SuiteSparseQR was compiled with
308  # support for it, this will do no harm if it wasn't.
309  find_package(TBB QUIET)
310  if (TBB_FOUND)
311    message(STATUS "Found Intel Thread Building Blocks (TBB) library "
312      "(${TBB_VERSION_MAJOR}.${TBB_VERSION_MINOR} / ${TBB_INTERFACE_VERSION}) "
313      "include location: ${TBB_INCLUDE_DIRS}. Assuming SuiteSparseQR was "
314      "compiled with TBB.")
315    # Add the TBB libraries to the SuiteSparseQR libraries (the only
316    # libraries to optionally depend on TBB).
317    list(APPEND SUITESPARSEQR_LIBRARY ${TBB_LIBRARIES})
318  else()
319    message(STATUS "Did not find Intel TBB library, assuming SuiteSparseQR was "
320      "not compiled with TBB.")
321  endif()
322endif(SUITESPARSEQR_FOUND)
323
324# UFconfig / SuiteSparse_config.
325#
326# If SuiteSparse version is >= 4 then SuiteSparse_config is required.
327# For SuiteSparse 3, UFconfig.h is required.
328suitesparse_find_component(
329  SUITESPARSE_CONFIG FILES SuiteSparse_config.h LIBRARIES suitesparseconfig)
330
331if (SUITESPARSE_CONFIG_FOUND)
332  # SuiteSparse_config (SuiteSparse version >= 4) requires librt library for
333  # timing by default when compiled on Linux or Unix, but not on OSX (which
334  # does not have librt).
335  if (CMAKE_SYSTEM_NAME MATCHES "Linux" OR UNIX AND NOT APPLE)
336    suitesparse_find_component(LIBRT LIBRARIES rt)
337    if (LIBRT_FOUND)
338      message(STATUS "Adding librt: ${LIBRT_LIBRARY} to "
339        "SuiteSparse_config libraries (required on Linux & Unix [not OSX] if "
340        "SuiteSparse is compiled with timing).")
341      list(APPEND SUITESPARSE_CONFIG_LIBRARY ${LIBRT_LIBRARY})
342    else()
343      message(STATUS "Could not find librt, but found SuiteSparse_config, "
344        "assuming that SuiteSparse was compiled without timing.")
345    endif ()
346  endif (CMAKE_SYSTEM_NAME MATCHES "Linux" OR UNIX AND NOT APPLE)
347else()
348  # Failed to find SuiteSparse_config (>= v4 installs), instead look for
349  # UFconfig header which should be present in < v4 installs.
350  suitesparse_find_component(UFCONFIG FILES UFconfig.h)
351endif ()
352
353if (NOT SUITESPARSE_CONFIG_FOUND AND
354    NOT UFCONFIG_FOUND)
355  suitesparse_report_not_found(
356    "Failed to find either: SuiteSparse_config header & library (should be "
357    "present in all SuiteSparse >= v4 installs), or UFconfig header (should "
358    "be present in all SuiteSparse < v4 installs).")
359endif()
360
361# Extract the SuiteSparse version from the appropriate header (UFconfig.h for
362# <= v3, SuiteSparse_config.h for >= v4).
363list(APPEND SUITESPARSE_FOUND_REQUIRED_VARS SUITESPARSE_VERSION)
364
365if (UFCONFIG_FOUND)
366  # SuiteSparse version <= 3.
367  set(SUITESPARSE_VERSION_FILE ${UFCONFIG_INCLUDE_DIR}/UFconfig.h)
368  if (NOT EXISTS ${SUITESPARSE_VERSION_FILE})
369    suitesparse_report_not_found(
370      "Could not find file: ${SUITESPARSE_VERSION_FILE} containing version "
371      "information for <= v3 SuiteSparse installs, but UFconfig was found "
372      "(only present in <= v3 installs).")
373  else (NOT EXISTS ${SUITESPARSE_VERSION_FILE})
374    file(READ ${SUITESPARSE_VERSION_FILE} UFCONFIG_CONTENTS)
375
376    string(REGEX MATCH "#define SUITESPARSE_MAIN_VERSION [0-9]+"
377      SUITESPARSE_MAIN_VERSION "${UFCONFIG_CONTENTS}")
378    string(REGEX REPLACE "#define SUITESPARSE_MAIN_VERSION ([0-9]+)" "\\1"
379      SUITESPARSE_MAIN_VERSION "${SUITESPARSE_MAIN_VERSION}")
380
381    string(REGEX MATCH "#define SUITESPARSE_SUB_VERSION [0-9]+"
382      SUITESPARSE_SUB_VERSION "${UFCONFIG_CONTENTS}")
383    string(REGEX REPLACE "#define SUITESPARSE_SUB_VERSION ([0-9]+)" "\\1"
384      SUITESPARSE_SUB_VERSION "${SUITESPARSE_SUB_VERSION}")
385
386    string(REGEX MATCH "#define SUITESPARSE_SUBSUB_VERSION [0-9]+"
387      SUITESPARSE_SUBSUB_VERSION "${UFCONFIG_CONTENTS}")
388    string(REGEX REPLACE "#define SUITESPARSE_SUBSUB_VERSION ([0-9]+)" "\\1"
389      SUITESPARSE_SUBSUB_VERSION "${SUITESPARSE_SUBSUB_VERSION}")
390
391    # This is on a single line s/t CMake does not interpret it as a list of
392    # elements and insert ';' separators which would result in 4.;2.;1 nonsense.
393    set(SUITESPARSE_VERSION
394      "${SUITESPARSE_MAIN_VERSION}.${SUITESPARSE_SUB_VERSION}.${SUITESPARSE_SUBSUB_VERSION}")
395  endif (NOT EXISTS ${SUITESPARSE_VERSION_FILE})
396endif (UFCONFIG_FOUND)
397
398if (SUITESPARSE_CONFIG_FOUND)
399  # SuiteSparse version >= 4.
400  set(SUITESPARSE_VERSION_FILE
401    ${SUITESPARSE_CONFIG_INCLUDE_DIR}/SuiteSparse_config.h)
402  if (NOT EXISTS ${SUITESPARSE_VERSION_FILE})
403    suitesparse_report_not_found(
404      "Could not find file: ${SUITESPARSE_VERSION_FILE} containing version "
405      "information for >= v4 SuiteSparse installs, but SuiteSparse_config was "
406      "found (only present in >= v4 installs).")
407  else (NOT EXISTS ${SUITESPARSE_VERSION_FILE})
408    file(READ ${SUITESPARSE_VERSION_FILE} SUITESPARSE_CONFIG_CONTENTS)
409
410    string(REGEX MATCH "#define SUITESPARSE_MAIN_VERSION [0-9]+"
411      SUITESPARSE_MAIN_VERSION "${SUITESPARSE_CONFIG_CONTENTS}")
412    string(REGEX REPLACE "#define SUITESPARSE_MAIN_VERSION ([0-9]+)" "\\1"
413      SUITESPARSE_MAIN_VERSION "${SUITESPARSE_MAIN_VERSION}")
414
415    string(REGEX MATCH "#define SUITESPARSE_SUB_VERSION [0-9]+"
416      SUITESPARSE_SUB_VERSION "${SUITESPARSE_CONFIG_CONTENTS}")
417    string(REGEX REPLACE "#define SUITESPARSE_SUB_VERSION ([0-9]+)" "\\1"
418      SUITESPARSE_SUB_VERSION "${SUITESPARSE_SUB_VERSION}")
419
420    string(REGEX MATCH "#define SUITESPARSE_SUBSUB_VERSION [0-9]+"
421      SUITESPARSE_SUBSUB_VERSION "${SUITESPARSE_CONFIG_CONTENTS}")
422    string(REGEX REPLACE "#define SUITESPARSE_SUBSUB_VERSION ([0-9]+)" "\\1"
423      SUITESPARSE_SUBSUB_VERSION "${SUITESPARSE_SUBSUB_VERSION}")
424
425    # This is on a single line s/t CMake does not interpret it as a list of
426    # elements and insert ';' separators which would result in 4.;2.;1 nonsense.
427    set(SUITESPARSE_VERSION
428      "${SUITESPARSE_MAIN_VERSION}.${SUITESPARSE_SUB_VERSION}.${SUITESPARSE_SUBSUB_VERSION}")
429  endif (NOT EXISTS ${SUITESPARSE_VERSION_FILE})
430endif (SUITESPARSE_CONFIG_FOUND)
431
432# METIS (Optional dependency).
433suitesparse_find_component(METIS LIBRARIES metis)
434
435# Only mark SuiteSparse as found if all required components and dependencies
436# have been found.
437set(SUITESPARSE_FOUND TRUE)
438foreach(REQUIRED_VAR ${SUITESPARSE_FOUND_REQUIRED_VARS})
439  if (NOT ${REQUIRED_VAR})
440    set(SUITESPARSE_FOUND FALSE)
441  endif (NOT ${REQUIRED_VAR})
442endforeach(REQUIRED_VAR ${SUITESPARSE_FOUND_REQUIRED_VARS})
443
444if (SUITESPARSE_FOUND)
445  list(APPEND SUITESPARSE_INCLUDE_DIRS
446    ${AMD_INCLUDE_DIR}
447    ${CAMD_INCLUDE_DIR}
448    ${COLAMD_INCLUDE_DIR}
449    ${CCOLAMD_INCLUDE_DIR}
450    ${CHOLMOD_INCLUDE_DIR}
451    ${SUITESPARSEQR_INCLUDE_DIR})
452  # Handle config separately, as otherwise at least one of them will be set
453  # to NOTFOUND which would cause any check on SUITESPARSE_INCLUDE_DIRS to fail.
454  if (SUITESPARSE_CONFIG_FOUND)
455    list(APPEND SUITESPARSE_INCLUDE_DIRS
456      ${SUITESPARSE_CONFIG_INCLUDE_DIR})
457  endif (SUITESPARSE_CONFIG_FOUND)
458  if (UFCONFIG_FOUND)
459    list(APPEND SUITESPARSE_INCLUDE_DIRS
460      ${UFCONFIG_INCLUDE_DIR})
461  endif (UFCONFIG_FOUND)
462  # As SuiteSparse includes are often all in the same directory, remove any
463  # repetitions.
464  list(REMOVE_DUPLICATES SUITESPARSE_INCLUDE_DIRS)
465
466  # Important: The ordering of these libraries is *NOT* arbitrary, as these
467  # could potentially be static libraries their link ordering is important.
468  list(APPEND SUITESPARSE_LIBRARIES
469    ${SUITESPARSEQR_LIBRARY}
470    ${CHOLMOD_LIBRARY}
471    ${CCOLAMD_LIBRARY}
472    ${CAMD_LIBRARY}
473    ${COLAMD_LIBRARY}
474    ${AMD_LIBRARY}
475    ${LAPACK_LIBRARIES}
476    ${BLAS_LIBRARIES})
477  if (SUITESPARSE_CONFIG_FOUND)
478    list(APPEND SUITESPARSE_LIBRARIES
479      ${SUITESPARSE_CONFIG_LIBRARY})
480  endif (SUITESPARSE_CONFIG_FOUND)
481  if (METIS_FOUND)
482    list(APPEND SUITESPARSE_LIBRARIES
483      ${METIS_LIBRARY})
484  endif (METIS_FOUND)
485endif()
486
487# Determine if we are running on Ubuntu with the package install of SuiteSparse
488# which is broken and does not support linking a shared library.
489set(SUITESPARSE_IS_BROKEN_SHARED_LINKING_UBUNTU_SYSTEM_VERSION FALSE)
490if (CMAKE_SYSTEM_NAME MATCHES "Linux" AND
491    SUITESPARSE_VERSION VERSION_EQUAL 3.4.0)
492  find_program(LSB_RELEASE_EXECUTABLE lsb_release)
493  if (LSB_RELEASE_EXECUTABLE)
494    # Any even moderately recent Ubuntu release (likely to be affected by
495    # this bug) should have lsb_release, if it isn't present we are likely
496    # on a different Linux distribution (should be fine).
497    execute_process(COMMAND ${LSB_RELEASE_EXECUTABLE} -si
498      OUTPUT_VARIABLE LSB_DISTRIBUTOR_ID
499      OUTPUT_STRIP_TRAILING_WHITESPACE)
500
501    if (LSB_DISTRIBUTOR_ID MATCHES "Ubuntu" AND
502        SUITESPARSE_LIBRARIES MATCHES "/usr/lib/libamd")
503      # We are on Ubuntu, and the SuiteSparse version matches the broken
504      # system install version and is a system install.
505      set(SUITESPARSE_IS_BROKEN_SHARED_LINKING_UBUNTU_SYSTEM_VERSION TRUE)
506      message(STATUS "Found system install of SuiteSparse "
507        "${SUITESPARSE_VERSION} running on Ubuntu, which has a known bug "
508        "preventing linking of shared libraries (static linking unaffected).")
509    endif (LSB_DISTRIBUTOR_ID MATCHES "Ubuntu" AND
510      SUITESPARSE_LIBRARIES MATCHES "/usr/lib/libamd")
511  endif (LSB_RELEASE_EXECUTABLE)
512endif (CMAKE_SYSTEM_NAME MATCHES "Linux" AND
513  SUITESPARSE_VERSION VERSION_EQUAL 3.4.0)
514
515suitesparse_reset_find_library_prefix()
516
517# Handle REQUIRED and QUIET arguments to FIND_PACKAGE
518include(FindPackageHandleStandardArgs)
519if (SUITESPARSE_FOUND)
520  find_package_handle_standard_args(SuiteSparse
521    REQUIRED_VARS ${SUITESPARSE_FOUND_REQUIRED_VARS}
522    VERSION_VAR SUITESPARSE_VERSION
523    FAIL_MESSAGE "Failed to find some/all required components of SuiteSparse.")
524else (SUITESPARSE_FOUND)
525  # Do not pass VERSION_VAR to FindPackageHandleStandardArgs() if we failed to
526  # find SuiteSparse to avoid a confusing autogenerated failure message
527  # that states 'not found (missing: FOO) (found version: x.y.z)'.
528  find_package_handle_standard_args(SuiteSparse
529    REQUIRED_VARS ${SUITESPARSE_FOUND_REQUIRED_VARS}
530    FAIL_MESSAGE "Failed to find some/all required components of SuiteSparse.")
531endif (SUITESPARSE_FOUND)
532