# -----------------------------------------------------------------
# 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.22.1)

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

set(CMAKE_C_FLAGS
  "-fcommon"
  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
  "-g -O2 -ffile-prefix-map=/build/sundials-wHsbk6/sundials-5.8.0+dfsg=. -flto=auto -ffat-lto-objects -flto=auto -ffat-lto-objects -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2"
  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/lib/x86_64-linux-gnu/cmake/sundials
  CACHE PATH "Location of SUNDIALSConfig.cmake")

find_package(SUNDIALS REQUIRED NO_DEFAULT_PATH)

# Set additional libraries
set(EXTRA_LIBS  -lm /usr/lib/x86_64-linux-gnu/librt.a CACHE STRING "Additional libraries")

# Set targets needed
set(TARGETS_NEEDED  SUNDIALS::arkode)

# List of SUNDIALS libraries
set(LIBRARIES
    ${TARGETS_NEEDED}
    ${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} ${LIBRARIES})

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

endforeach()
