diff --git a/cmake/SUNDIALSConfig.cmake.in b/cmake/SUNDIALSConfig.cmake.in index 58bbe448e5..9cbe9e6c23 100644 --- a/cmake/SUNDIALSConfig.cmake.in +++ b/cmake/SUNDIALSConfig.cmake.in @@ -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) diff --git a/cmake/tpl/FindKLU.cmake b/cmake/tpl/FindKLU.cmake index cad926d4b8..96b9d7343b 100644 --- a/cmake/tpl/FindKLU.cmake +++ b/cmake/tpl/FindKLU.cmake @@ -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}) diff --git a/doc/shared/sundials/Install.rst b/doc/shared/sundials/Install.rst index 6843e2c376..aad89e575d 100644 --- a/doc/shared/sundials/Install.rst +++ b/doc/shared/sundials/Install.rst @@ -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.