# -----------------------------------------------------------------
# Programmer(s): Cody J. Balos @ 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
# -----------------------------------------------------------------

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

# Set cache variables for compilers and flags
set(CMAKE_C_COMPILER
  /usr/bin/cc
  CACHE FILEPATH "C compiler")

set(CMAKE_C_FLAGS
  "-pipe -O2 -isystem /usr/local/include -fno-strict-aliasing"
  CACHE STRING "C compiler flags")

# Set cache variables for compilers and flags
set(CMAKE_CXX_COMPILER
  /usr/bin/c++
  CACHE FILEPATH "CXX compiler")

set(CMAKE_CXX_FLAGS
  " -pipe -O2 -isystem /usr/local/include -fno-strict-aliasing -isystem /usr/local/include"
  CACHE STRING "CXX compiler flags")

# Specify project name and languages
project(ARKODE_examples C CXX)

# Enable testing
include(CTest)

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

# Specify the path to SUNDIALSConfig.cmake
set(SUNDIALS_DIR
  /usr/local/lib/cmake/sundials
  CACHE PATH "Location of SUNDIALSConfig.cmake")

find_package(SUNDIALS REQUIRED NO_DEFAULT_PATH)

# Set additional libraries
set(SUNDIALS_EXTRA_LIBS  -lm CACHE STRING "Additional libraries")

# Set SUNDIALS targets needed
set(SUNDIALS_TARGETS_NEEDED "")
foreach(tgt "arkode")
  list(APPEND SUNDIALS_TARGETS_NEEDED SUNDIALS::${tgt})
endforeach()

# List of SUNDIALS libraries
set(SUNDIALS_LIBRARIES
    ${SUNDIALS_TARGETS_NEEDED}
    ${SUNDIALS_EXTRA_LIBS})

# Additional includes
include_directories(. )

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

# Set the names of the examples to be built and their dependencies
set(examples  ark_analytic_sys.cpp ark_heat2D.cpp ark_kpr_Mt.cpp)
set(examples_dependencies )
if(examples)
  list(REMOVE_DUPLICATES examples)
endif()

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

  # get filename without extension
  get_filename_component(example_target ${example} NAME_WE)

  # example source files
  add_executable(${example_target} ${example} ${examples_dependencies})

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

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

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

endforeach()
