# -----------------------------------------------------------------
# Programmer(s): Radu Serban @ LLNL
#                David J. Gardner @ LLNL
# -----------------------------------------------------------------
# SUNDIALS Copyright Start
# Copyright (c) 2002-2021, 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 for @SOLVER@ Fortran 77 serial examples.
#
# This file is generated from a template using variables
# set at configuration time. It can be used as a template for
# other user CMakeLists configuration files.
# -----------------------------------------------------------------

# Set the minimum required cmake version
cmake_minimum_required(VERSION 3.5)

# Set cache variables for compilers and flags
set(CMAKE_Fortran_COMPILER
  @CMAKE_Fortran_COMPILER@
  CACHE FILEPATH "Fortran compiler")

set(CMAKE_Fortran_FLAGS
  "@CMAKE_Fortran_FLAGS@"
  CACHE STRING "Fortran compiler flags")

set(CMAKE_C_COMPILER
  @CMAKE_C_COMPILER@
  CACHE FILEPATH "C compiler")

set(CMAKE_C_FLAGS
  "@CMAKE_C_FLAGS@"
  CACHE STRING "C compiler flags")

# Specify project name and languages
project(@SOLVER@_F77_serial_examples Fortran C)

# Enable testing
include(CTest)

# ------------------------------------------------------------------------------

# Specify the path to SUNDIALS header files
set(SUNDIALS_INCLUDE_DIR
  @CMAKE_INSTALL_PREFIX@/include
  CACHE PATH "Location of SUNDIALS header files")

# Specify the path to SUNDIALS libraries
set(SUNDIALS_LIBRARY_DIR
  @CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_LIBDIR@
  CACHE PATH "Location of SUNDIALS libraries")

# Find the SUNDIALS libraries
find_library(SUNDIALS_SOLVER_LIB
  @SOLVER_LIB@ ${SUNDIALS_LIBRARY_DIR}
  DOC "@SOLVER@ library")

find_library(SUNDIALS_SOLVER_FLIB
  @SOLVER_FLIB@ ${SUNDIALS_LIBRARY_DIR}
  DOC "@SOLVER@ Fortran-C library")

find_library(SUNDIALS_NVEC_LIB
  sundials_nvecserial ${SUNDIALS_LIBRARY_DIR}
  DOC "NVECTOR_SERIAL library")

find_library(SUNDIALS_NVEC_FLIB
  sundials_fnvecserial ${SUNDIALS_LIBRARY_DIR}
  DOC "NVECTOR_SERIAL Fortran-C library")

# Set additional libraries
set(SUNDIALS_EXTRA_LIBS @LIBS@ CACHE STRING "Additional libraries")

# For SUNDIALS module examples the solver library is not needed
if(NOT SUNDIALS_SOLVER_LIB)
  set(SUNDIALS_SOLVER_LIB "")
endif()

# List of SUNDIALS libraries
set(SUNDIALS_LIBRARIES
  -L${SUNDIALS_LIBRARY_DIR}
  ${SUNDIALS_SOLVER_FLIB}
  ${SUNDIALS_SOLVER_LIB}
  ${SUNDIALS_NVEC_FLIB}
  ${SUNDIALS_NVEC_LIB}
  ${SUNDIALS_EXTRA_LIBS})

# ------------------------------------------------------------------------------

# Set the names of the examples to be built and their dependencies
set(examples @EXAMPLES@)
set(examples_dependencies @EXAMPLES_DEPENDENCIES@)
if(examples)
  list(REMOVE_DUPLICATES examples)
endif()

# Create targets for each example
foreach(example ${examples})

  # example source files
  add_executable(${example} ${example}.f ${examples_dependencies})

  # directories to include
  target_include_directories(${example} PRIVATE ${SUNDIALS_INCLUDE_DIR})

  # libraries to link against
  target_link_libraries(${example} ${SUNDIALS_LIBRARIES})

  # add the example to ctest
  add_test(NAME ${example} COMMAND ${example})

endforeach()

# ------------------------------------------------------------------------------

# Set LAPACK libraries
set(LAPACK_LIBRARIES
  @LAPACK_LIBS@
  CACHE STRING "Lapack libraries")

# Set the names of the examples to be built and their dependencies
set(examples_bl @EXAMPLES_BL@)
set(examples_dependencies_bl @EXAMPLES_DEPENDENCIES_BL@)
if(examples_bl)
  list(REMOVE_DUPLICATES examples_bl)
endif()

if(LAPACK_LIBRARIES AND examples_bl)

  # Find the SUNDIALS linear solver libraries
  find_library(SUNDIALS_SUNLINSOLLAPACKBAND_LIB
    sundials_sunlinsollapackband ${SUNDIALS_LIBRARY_DIR}
    DOC "SUNDIALS banded LAPACK linear solver library")

  find_library(SUNDIALS_SUNLINSOLLAPACKBAND_FLIB
    sundials_fsunlinsollapackband ${SUNDIALS_LIBRARY_DIR}
    DOC "SUNDIALS banded LAPACK linear solver Fortran-C library")

  find_library(SUNDIALS_SUNLINSOLLAPACKDENSE_LIB
    sundials_sunlinsollapackdense ${SUNDIALS_LIBRARY_DIR}
    DOC "SUNDIALS dense LAPACK linear solver library")

  find_library(SUNDIALS_SUNLINSOLLAPACKDENSE_FLIB
    sundials_fsunlinsollapackdense ${SUNDIALS_LIBRARY_DIR}
    DOC "SUNDIALS dense LAPACK linear solver Fortran-C library")

  # Create targets for each example
  foreach(example ${examples_bl})

    # example source files
    add_executable(${example} ${example}.f ${examples_dependencies_bl})

    # directories to include
    target_include_directories(${example} PRIVATE ${SUNDIALS_INCLUDE_DIR})

    # libraries to link against
    target_link_libraries(${example} ${SUNDIALS_LIBRARIES})
    target_link_libraries(${example} ${SUNDIALS_SUNLINSOLLAPACKBAND_FLIB})
    target_link_libraries(${example} ${SUNDIALS_SUNLINSOLLAPACKDENSE_FLIB})
    target_link_libraries(${example} ${SUNDIALS_SUNLINSOLLAPACKBAND_LIB})
    target_link_libraries(${example} ${SUNDIALS_SUNLINSOLLAPACKDENSE_LIB})
    target_link_libraries(${example} ${LAPACK_LIBRARIES})

    # add the example to ctest
    add_test(NAME ${example} COMMAND ${example})

  endforeach()

endif()

# ------------------------------------------------------------------------------

# KLU include directory and libraries
set(KLU_INCLUDE_DIR
  @KLU_INCLUDE_DIR@
  CACHE PATH "Location of KLU header files")

set(KLU_LIBRARIES
  @KLU_LIBRARIES@
  CACHE STRING "KLU libraries")

# Set the names of the examples to be built and their dependencies
set(examples_klu @EXAMPLES_KLU@)
set(examples_dependencies_klu @EXAMPLES_DEPENDENCIES_KLU@)
if(examples_klu)
  list(REMOVE_DUPLICATES examples_klu)
endif()

if(KLU_LIBRARIES AND examples_klu)

  # Find the SUNDIALS linear solver libraries
  find_library(SUNDIALS_SUNLINSOLKLU_LIB
    sundials_sunlinsolklu ${SUNDIALS_LIBRARY_DIR}
    DOC "SUNDIALS KLU linear solver library")

  find_library(SUNDIALS_SUNLINSOLKLU_FLIB
    sundials_fsunlinsolklu ${SUNDIALS_LIBRARY_DIR}
    DOC "SUNDIALS KLU linear solver Fortran-C library")

  # Create targets for each example
  foreach(example ${examples_klu})

    # example source files
    add_executable(${example} ${example}.f ${examples_dependencies_klu})

    # directories to include
    target_include_directories(${example} PRIVATE ${SUNDIALS_INCLUDE_DIR})
    target_include_directories(${example} PRIVATE ${KLU_INCLUDE_DIR})

    # libraries to link against
    target_link_libraries(${example} ${SUNDIALS_LIBRARIES})
    target_link_libraries(${example} ${SUNDIALS_SUNLINSOLKLU_FLIB})
    target_link_libraries(${example} ${SUNDIALS_SUNLINSOLKLU_LIB})
    target_link_libraries(${example} ${KLU_LIBRARIES})

    # add the example to ctest
    add_test(NAME ${example} COMMAND ${example})

  endforeach()

endif()

# ------------------------------------------------------------------------------

# SuperLU_MT include directory, libraries, and threading type
set(SUPERLUMT_INCLUDE_DIR
  @SUPERLUMT_INCLUDE_DIR@
  CACHE PATH "Location of SuperLU_MT KLU header files")

set(SUPERLUMT_LIBRARIES
  @SUPERLUMT_LIBRARY@ @SUPERLUMT_LIBRARIES@
  CACHE STRING "SuperLU_MT libraries")

set(SUPERLUMT_THREAD_TYPE
  @SUPERLUMT_THREAD_TYPE@
  CACHE STRING "SuperLU_MT Threading")

# Set the names of the examples to be built and their dependencies
set(examples_slumt @EXAMPLES_SLUMT@)
set(examples_dependencies_slumt @EXAMPLES_DEPENDENCIES_SLUMT@)
if(examples_slumt)
  list(REMOVE_DUPLICATES examples_slumt)
endif()

if(SUPERLUMT_LIBRARIES AND examples_slumt)

  # Find the SUNDIALS linear solver libraries
  find_library(SUNDIALS_SUNLINSOLSLUMT_LIB
    sundials_sunlinsolsuperlumt ${SUNDIALS_LIBRARY_DIR}
    DOC "SUNDIALS SuperLU_MT linear solver library")

  find_library(SUNDIALS_SUNLINSOLSLUMT_FLIB
    sundials_fsunlinsolsuperlumt ${SUNDIALS_LIBRARY_DIR}
    DOC "SUNDIALS SuperLU_MT linear solver Fortran-C library")

  if(SUPERLUMT_THREAD_TYPE STREQUAL "OPENMP")
    find_package(OpenMP REQUIRED)
    # Update C compiler and linker flags
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_C_FLAGS}")
    set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} ${OpenMP_Fortran_FLAGS}")
  else(SUPERLUMT_THREAD_TYPE STREQUAL "PTHREAD")
    find_package(Threads REQUIRED)
  endif()

  # Create targets for each example
  foreach(example ${examples_slumt})

    # example source files
    add_executable(${example} ${example}.f ${examples_dependencies_slumt})

    # directories to include
    target_include_directories(${example} PRIVATE ${SUNDIALS_INCLUDE_DIR})
    target_include_directories(${example} PRIVATE ${SUPERLUMT_INCLUDE_DIR})

    # libraries to link against
    target_link_libraries(${example} ${SUNDIALS_LIBRARIES})
    target_link_libraries(${example} ${SUNDIALS_SUNLINSOLSLUMT_FLIB})
    target_link_libraries(${example} ${SUNDIALS_SUNLINSOLSLUMT_LIB})
    target_link_libraries(${example} ${SUPERLUMT_LIBRARIES})

    if(SUPERLUMT_THREAD_TYPE STREQUAL "PTHREAD")
      target_link_libraries(${example} Threads::Threads)
    endif()

    # add the example to ctest
    add_test(NAME ${example} COMMAND ${example})

  endforeach()

endif()
