Skip to content

Commit

Permalink
CMake: Import installed CMake target for KLU from SuiteSparse (#407)
Browse files Browse the repository at this point in the history
Newer versions of SuiteSparse install import targets for its libraries.
Use the import target of KLU as installed by SuiteSparse if it is
available and the user didn't set flags to select a different one.

This fixes an issue if the SuiteSparse headers are installed at their
new default location `$prefix/include/suitesparse`.

---------

Signed-off-by: Markus Mützel <[email protected]>
Co-authored-by: Daniel R. Reynolds <[email protected]>
Co-authored-by: Cody Balos <[email protected]>
Co-authored-by: David Gardner <[email protected]>
  • Loading branch information
4 people authored Feb 28, 2024
1 parent 0f2d49d commit 6f7f591
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
10 changes: 7 additions & 3 deletions cmake/SUNDIALSConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,13 @@ if("@ENABLE_HYPRE@" AND NOT TARGET SUNDIALS::HYPRE)
endif()

if("@ENABLE_KLU@" AND NOT TARGET SUNDIALS::KLU)
add_library(SUNDIALS::KLU INTERFACE IMPORTED)
target_link_libraries(SUNDIALS::KLU INTERFACE "@KLU_LIBRARIES@")
set_target_properties(SUNDIALS::KLU PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "@KLU_INCLUDE_DIR@")
if("@KLU_SUITESPARSE_TARGET@")
find_dependency(KLU)
else()
add_library(SUNDIALS::KLU INTERFACE IMPORTED)
target_link_libraries(SUNDIALS::KLU INTERFACE "@KLU_LIBRARIES@")
set_target_properties(SUNDIALS::KLU PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "@KLU_INCLUDE_DIR@")
endif()
endif()

if("@ENABLE_KOKKOS@" AND NOT TARGET Kokkos::kokkos)
Expand Down
15 changes: 15 additions & 0 deletions cmake/tpl/FindKLU.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@
# KLU_LIBRARIES - all of the libraries needed for KLU
# ---------------------------------------------------------------

if (NOT (KLU_INCLUDE_DIR OR KLU_LIBRARY_DIR OR KLU_LIBRARY))
# Prefer the import target from upstream SuiteSparse if it is available
# and the user didn't point to a specific (different) version.
find_package(KLU CONFIG)

if(TARGET SuiteSparse::KLU)
if(NOT TARGET SUNDIALS::KLU)
add_library(SUNDIALS::KLU ALIAS SuiteSparse::KLU)
set(KLU_SUITESPARSE_TARGET ON)
mark_as_advanced(KLU_SUITESPARSE_TARGET)
endif()
return()
endif()
endif()

# Set library prefixes for Windows
if(WIN32)
set(CMAKE_FIND_LIBRARY_PREFIXES lib ${CMAKE_FIND_LIBRARY_PREFIXES})
Expand Down
15 changes: 13 additions & 2 deletions doc/shared/sundials/Install.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1336,11 +1336,22 @@ Texas A&M University and is available from the `SuiteSparse GitHub repository

To enable KLU, set ``ENABLE_KLU`` to ``ON``, set ``KLU_INCLUDE_DIR`` to the
``include`` path of the KLU installation and set ``KLU_LIBRARY_DIR``
to the ``lib`` path of the KLU installation. The CMake configure will
result in populating the following variables: ``AMD_LIBRARY``,
to the ``lib`` path of the KLU installation. In that case, the CMake configure
will result in populating the following variables: ``AMD_LIBRARY``,
``AMD_LIBRARY_DIR``, ``BTF_LIBRARY``, ``BTF_LIBRARY_DIR``,
``COLAMD_LIBRARY``, ``COLAMD_LIBRARY_DIR``, and ``KLU_LIBRARY``.

For SuiteSparse 7.4.0 and newer, the necessary information can also be gathered
from a CMake import target. If SuiteSparse is installed in a non-default
prefix, the path to the CMake Config file can be set using
``CMAKE_PREFIX_PATH``. In that case, the CMake configure step won't populate
the previously mentioned variables. It is still possible to set
``KLU_INCLUDE_DIR`` and ``KLU_LIBRARY_DIR`` which take precedence over a
potentially installed CMake import target file.

In either case, a CMake target ``SUNDIALS::KLU`` will be created if the KLU
library could be found. Dependent targets should link to that target.

SUNDIALS has been tested with SuiteSparse version 5.10.1.


Expand Down

0 comments on commit 6f7f591

Please sign in to comment.