# ---------------------------------------------------------------
# Programmer:  Cody J. Balos @ LLNL
# ---------------------------------------------------------------
# SUNDIALS Copyright Start
# Copyright (c) 2002-2019, 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 F2003 CVODE object library

set(cvode_SOURCES fcvode.f90)

# Add variable nvec_SOURCES with the common NVECTOR sources which will
# also be included in the CVODE library
set(nvec_SOURCES
  ${sundials_SOURCE_DIR}/src/nvector/serial/F90/fnvector_serial.f90
)

# Add variable sunmatrix_SOURCES with the common SUNMatrix sources which will
# also be included in the CVODE library
set(sunmatrix_SOURCES
  ${sundials_SOURCE_DIR}/src/sunmatrix/band/F90/fsunmatrix_band.f90
  ${sundials_SOURCE_DIR}/src/sunmatrix/dense/F90/fsunmatrix_dense.f90
  ${sundials_SOURCE_DIR}/src/sunmatrix/sparse/F90/fsunmatrix_sparse.f90
)

# Add variable sunlinsol_SOURCES with the common SUNLinearSolver sources which will
# also be included in the CVODE library
set(sunlinsol_SOURCES
  ${sundials_SOURCE_DIR}/src/sunlinsol/band/F90/fsunlinsol_band.f90
  ${sundials_SOURCE_DIR}/src/sunlinsol/dense/F90/fsunlinsol_dense.f90
  ${sundials_SOURCE_DIR}/src/sunlinsol/spbcgs/F90/fsunlinsol_spbcgs.f90
  ${sundials_SOURCE_DIR}/src/sunlinsol/spfgmr/F90/fsunlinsol_spfgmr.f90
  ${sundials_SOURCE_DIR}/src/sunlinsol/spgmr/F90/fsunlinsol_spgmr.f90
  ${sundials_SOURCE_DIR}/src/sunlinsol/sptfqmr/F90/fsunlinsol_sptfqmr.f90
  ${sundials_SOURCE_DIR}/src/sunlinsol/pcg/F90/fsunlinsol_pcg.f90
)

# Add variable sunnonlinsol_SOURCES with the common SUNNonlinearSolver sources
# which will also be included in the CVODE library
set(sunnonlinsol_SOURCES
  ${sundials_SOURCE_DIR}/src/sunnonlinsol/newton/F90/fsunnonlinsol_newton.f90
  ${sundials_SOURCE_DIR}/src/sunnonlinsol/fixedpoint/F90/fsunnonlinsol_fixedpoint.f90
)

# Set the Fortran module directory to a temporary location.
# We do this to avoid naming collisions in parallel builds (make -j).
# The .mod files generated when building the fcvode_mod library are
# duplicates of the .mod files generated by the SUNDIALS modules themselves.
# As such, we do not need to keep them around.
set(CMAKE_Fortran_MODULE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/tmp)

if(BUILD_STATIC_LIBS)
  sundials_add_f2003_interface_library(sundials_fcvode_mod_static STATIC
    ${cvode_SOURCES}
    ${nvec_SOURCES}
    ${sunmatrix_SOURCES}
    ${sunlinsol_SOURCES}
    ${sunnonlinsol_SOURCES}
  )

  # include the directoy where the .mod files reside
  target_include_directories(sundials_fcvode_mod_static
    PUBLIC
      $<BUILD_INTERFACE:${CMAKE_Fortran_MODULE_DIRECTORY}_STATIC>
      $<INSTALL_INTERFACE:${Fortran_INSTALL_MODDIR}>
  )

  # Set the library name and make sure it is not deleted
  set_target_properties(sundials_fcvode_mod_static
    PROPERTIES OUTPUT_NAME sundials_fcvode_mod CLEAN_DIRECT_OUTPUT 1)

  # install the library
  install(TARGETS sundials_fcvode_mod_static DESTINATION ${CMAKE_INSTALL_LIBDIR})

  # install the cvode mod file from here since it won't go into the
  # top level fortran mod file directory
  install(FILES ${CMAKE_Fortran_MODULE_DIRECTORY}_STATIC/fcvode_mod.mod
          DESTINATION ${Fortran_INSTALL_MODDIR})
endif()

if(BUILD_SHARED_LIBS)
  sundials_add_f2003_interface_library(sundials_fcvode_mod_shared SHARED
    ${cvode_SOURCES}
    ${nvec_SOURCES}
    ${sunmatrix_SOURCES}
    ${sunlinsol_SOURCES}
    ${sunnonlinsol_SOURCES}
  )

  # include the directoy where the .mod files reside
  target_include_directories(sundials_fcvode_mod_shared
    PUBLIC
      $<BUILD_INTERFACE:${CMAKE_Fortran_MODULE_DIRECTORY}_SHARED>
      $<INSTALL_INTERFACE:${Fortran_INSTALL_MODDIR}>
  )

  # Set the library name and make sure it is not deleted
  set_target_properties(sundials_fcvode_mod_shared
    PROPERTIES OUTPUT_NAME sundials_fcvode_mod CLEAN_DIRECT_OUTPUT 1)

  # install the library
  install(TARGETS sundials_fcvode_mod_shared DESTINATION ${CMAKE_INSTALL_LIBDIR})

  # install the cvode mod file from here since it won't go into the
  # top level fortran mod file directory
  install(FILES ${CMAKE_Fortran_MODULE_DIRECTORY}_SHARED/fcvode_mod.mod
          DESTINATION ${Fortran_INSTALL_MODDIR})
endif()

message(STATUS "Added CVODE F2003 interface")
