# ---------------------------------------------------------------
# Programmer(s): Radu Serban and Cody J. Balos @ LLNL
# ---------------------------------------------------------------
# SUNDIALS Copyright Start
# Copyright (c) 2002-2022, Lawrence Livermore National Security
# and Southern Methodist University.
# All rights reserved.
#
# See the top-level LICENSE and NOTICE files for details.
#
# SPDX-License-Identifier: BSD-3-Clause
# SUNDIALS Copyright End
# ---------------------------------------------------------------
# CMakeLists.txt file for the generic SUNDIALS modules
# ---------------------------------------------------------------

# From here we only install the generic SUNDIALS headers.
# The implementations themselves are incorporated in the individual SUNDIALS solver libraries.

install(CODE "MESSAGE(\"\nInstall shared components\n\")")

# Add variable sundials_HEADERS with the exported SUNDIALS header files
set(sundials_HEADERS
  sundials_base.hpp
  sundials_band.h
  sundials_context.h
  sundials_context.hpp
  sundials_convertibleto.hpp
  sundials_dense.h
  sundials_direct.h
  sundials_iterative.h
  sundials_linearsolver.h
  sundials_linearsolver.hpp
  sundials_math.h
  sundials_matrix.h
  sundials_matrix.hpp
  sundials_memory.h
  sundials_mpi_types.h
  sundials_nonlinearsolver.h
  sundials_nonlinearsolver.hpp
  sundials_nvector.h
  sundials_nvector.hpp
  sundials_profiler.h
  sundials_logger.h
  sundials_types.h
  sundials_version.h
  )

if(BUILD_FORTRAN_MODULE_INTERFACE)
  list(APPEND sundials_HEADERS sundials_futils.h)
endif()

if(ENABLE_CUDA)
  list(APPEND sundials_HEADERS sundials_cuda_policies.hpp)
endif()

if(ENABLE_HIP)
  list(APPEND sundials_HEADERS sundials_hip_policies.hpp)
endif()

if(ENABLE_SYCL)
  list(APPEND sundials_HEADERS sundials_sycl_policies.hpp)
endif()

# If Blas/Lapack support was enabled, add the Lapack interface headers
if(BUILD_SUNLINSOL_LAPACKBAND OR BUILD_SUNLINSOL_LAPACKDENSE)
  list(APPEND sundials_HEADERS sundials_lapack.h)
endif()

# If enabled, add the XBraid interface header
if(ENABLE_XBRAID)
  list(APPEND sundials_HEADERS sundials_xbraid.h)
endif()

# Add prefix with complete path to the header files
add_prefix(${SUNDIALS_SOURCE_DIR}/include/sundials/ sundials_HEADERS)

set(sundials_SOURCES
  sundials_band.c
  sundials_context.c
  sundials_dense.c
  sundials_direct.c
  sundials_iterative.c
  sundials_linearsolver.c
  sundials_logger.c
  sundials_math.c
  sundials_matrix.c
  sundials_memory.c
  sundials_nonlinearsolver.c
  sundials_nvector.c
  sundials_nvector_senswrapper.c
  sundials_version.c
  )

if(SUNDIALS_BUILD_WITH_PROFILING)
  message(WARNING "SUNDIALS built with profiling turned on, performance may be affected.")
  list(APPEND sundials_SOURCES sundials_profiler.c)
endif()

if(BUILD_FORTRAN_MODULE_INTERFACE)
  list(APPEND sundials_SOURCES sundials_futils.c)
endif()

# Add prefix with complete path to the source files
add_prefix(${SUNDIALS_SOURCE_DIR}/src/sundials/ sundials_SOURCES)

if(SUNDIALS_BUILD_WITH_PROFILING OR SUNDIALS_LOGGING_ENABLE_MPI)
  if(ENABLE_MPI AND MPI_C_FOUND)
    # Workaround bug in CMake < 3.17.3 when using MPI::MPI_CXX and CUDA
    set(_include_mpi_if_needed PUBLIC ${MPI_C_INCLUDE_DIRS})
    set(_link_mpi_if_needed PUBLIC ${MPI_C_LIBRARIES})
  endif()
  if(ENABLE_CALIPER)
    set(_include_caliper_if_needed PUBLIC ${caliper_INCLUDE_DIR})
  endif()
endif()

# Create a library out of the generic sundials modules
sundials_add_library(sundials_generic
  SOURCES
    ${sundials_SOURCES}
  HEADERS
    ${sundials_HEADERS}
  INCLUDE_SUBDIR
    sundials
  INCLUDE_DIRECTORIES
    ${_include_mpi_if_needed}
    ${_include_caliper_if_needed}
  LINK_LIBRARIES
    ${_link_mpi_if_needed}
  OUTPUT_NAME
    sundials_generic
  VERSION
    ${sundialslib_VERSION}
  SOVERSION
    ${sundialslib_SOVERSION}
)

# SUNDIALS_EXPORT macro
include(GenerateExportHeader)
generate_export_header(
  sundials_generic BASE_NAME SUNDIALS
  EXPORT_FILE_NAME
  "${PROJECT_BINARY_DIR}/include/sundials/sundials_export.h"
)

# Add F2003 module if the interface is enabled
if(BUILD_FORTRAN_MODULE_INTERFACE)
  add_subdirectory(fmod)
endif()
