# -*- mode: cmake -*-
# -----------------------------------------------------------------
# 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 for CVODE Fortran 2003 interface examples
#
# This file is generated from a template using  various variables
# set at configuration time. It can be used as a template for
# other user CMakeLists configuration files.
# -----------------------------------------------------------------

cmake_minimum_required(VERSION 3.1.3)

# Specify project name
PROJECT(F2003_CVODE Fortran)

MARK_AS_ADVANCED(EXECUTABLE_OUTPUT_PATH LIBRARY_OUTPUT_PATH)

# Set the names of the examples to be built
SET(examples  cv_analytic_fp cv_analytic_sys_dns cv_analytic_sys_dns_jac cv_brusselator_dns)
SET(examples_klu  cv_analytic_sys_klu)

# Set cmake variables from template inputsd
SET(NVEC_LIB  ""  CACHE STRING "NVECTOR library")
SET(SUNLS_LIB "sundials_sunlinsolklu" CACHE STRING "SUNLinearSolver library")
SET(KLU_LIBRARIES "/usr/lib64/libklu.so;/usr/lib64/libamd.so;/usr/lib64/libcolamd.so;/usr/lib64/libbtf.so;/usr/lib64/libsuitesparseconfig.so")

# Specify path to the Fortran module files
SET(SUNDIALS_INC_DIR
  /usr//usr/lib/gcc/x86_64-mandriva-linux-gnu/8.3.1/finclude
  CACHE STRING
  "Location of SUNDIALS Fortran module files")

# Include path to SUNDIALS Fortran module files
INCLUDE_DIRECTORIES(${SUNDIALS_INC_DIR})

# Set search path for SUNDIALS libraries 
SET(SUNDIALS_LIB_DIR /usr//usr/lib64)

# Find the SUNDIALS solver's library
FIND_LIBRARY(SUNDIALS_SOLVER_LIB
  sundials_cvode ${SUNDIALS_LIB_DIR}
  DOC "CVODE library")

FIND_LIBRARY(SUNDIALS_SOLVER_FLIB
  sundials_fcvode_mod ${SUNDIALS_LIB_DIR}
  DOC "CVODE library")

# Find the NVECTOR library
IF("${NVEC_LIB}" STREQUAL "")
  # No additional NVECTOR library necessary
ELSE()
  # Find the additonal NVECTOR library
  FIND_LIBRARY(SUNDIALS_NVEC_LIB
     ${SUNDIALS_LIB_DIR}
    DOC "NVECTOR library")
  
  # Find the additonal NVECTOR library
  FIND_LIBRARY(SUNDIALS_NVEC_FLIB
     ${SUNDIALS_LIB_DIR}
    DOC "NVECTOR library")
ENDIF()

IF("${SUNLS_LIB}" STREQUAL "")
  # No additional SUNLinearSolver library necessary
ELSE()
  # Find the additional SUNLinearSolver library
  FIND_LIBRARY(SUNDIALS_SUNLS_LIB
    sundials_sunlinsolklu ${SUNDIALS_LIB_DIR}
    DOC "SUNLinearSolver library")
  
  FIND_LIBRARY(SUNDIALS_SUNLS_FLIB
    sundials_fsunlinsolklu_mod ${SUNDIALS_LIB_DIR}
    DOC "SUNLinearSolver library")
ENDIF()

# Set additional libraries
SET(SUNDIALS_EXTRA_LIB  -lm /usr/lib64/librt.so CACHE STRING "Additional libraries")

# List of all libraries
SET(SUNDIALS_LIBS 
  ${SUNDIALS_SOLVER_LIB} 
  ${SUNDIALS_SOLVER_FLIB} 
  ${SUNDIALS_NVEC_LIB} 
  ${SUNDIALS_NVEC_FLIB} 
  ${SUNDIALS_SUNLS_LIB}
  ${SUNDIALS_SUNLS_FLIB}
  ${SUNDIALS_EXTRA_LIB})

# Build each example one by one
FOREACH(example ${examples})
  
  # Keep fortran modules to a unique directory to avoid naming collisions
  set(CMAKE_Fortran_MODULE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${example}.dir)

  # example source files
  ADD_EXECUTABLE(${example} ${example}.f90)

  # libraries to link agaisnt
  TARGET_LINK_LIBRARIES(${example} ${SUNDIALS_LIBS})

ENDFOREACH(example ${examples})

# Build each example one by one
FOREACH(example ${examples_klu})
  
  # Keep fortran modules to a unique directory to avoid naming collisions
  set(CMAKE_Fortran_MODULE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${example}.dir)

  # example source files
  ADD_EXECUTABLE(${example} ${example}.f90)

  # libraries to link agaisnt
  TARGET_LINK_LIBRARIES(${example} ${SUNDIALS_LIBS} ${KLU_LIBRARIES})

ENDFOREACH(example ${examples_klu})

