# -----------------------------------------------------------------
# Programmer(s): Cody J. Balos and David J. Gardner @ LLNL
# -----------------------------------------------------------------
# SUNDIALS Copyright Start
# Copyright (c) 2025, Lawrence Livermore National Security,
# University of Maryland Baltimore County, and the SUNDIALS contributors.
# Copyright (c) 2013-2025, Lawrence Livermore National Security
# and Southern Methodist University.
# Copyright (c) 2002-2013, Lawrence Livermore National Security.
# 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 4.1.1-dirty)

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

set(CMAKE_C_FLAGS
  "-march=x86-64 -mtune=generic -O2 -pipe -fno-plt -fexceptions         -Wp,-D_FORTIFY_SOURCE=3 -Wformat -Werror=format-security         -fstack-clash-protection -fcf-protection -flto=auto"
  CACHE STRING "C compiler flags")

set(CMAKE_C_STANDARD
  "99"
  CACHE STRING "C standard")

# Set cache variables for C++ compilers and flags
set(CMAKE_CXX_COMPILER
  ""
  CACHE FILEPATH "C++ compiler")

set(CMAKE_CXX_FLAGS
  ""
  CACHE STRING "C++ compiler flags")

set(CMAKE_CXX_STANDARD
  ""
  CACHE STRING "C++ standard")

# Specify project name and languages
project(examples C)

# Enable testing
include(CTest)


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

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

# Find SUNDIALS
find_package(SUNDIALS
  COMPONENTS  nvecserial arkode sunlinsolklu
  REQUIRED NO_DEFAULT_PATH)

# Set the SUNDIALS targets
set(SUNDIALS_TARGETS
   SUNDIALS::nvecserial SUNDIALS::arkode SUNDIALS::sunlinsolklu)

# Set any additional libraries needed
set(EXTRA_LIBS
   -lm
  CACHE STRING "Additional libraries")

# Additional includes
include_directories(. )

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

# Set the names of the examples to be built and their dependencies
set(examples  ark_brusselator1D_klu.c)
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)

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

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

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

endforeach()
