# -----------------------------------------------------------------
# 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 @CMAKE_VERSION@)

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

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

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

set(CMAKE_CXX_FLAGS
  "@CMAKE_CXX_FLAGS@"
  CACHE STRING "CXX compiler flags")

# Specify project name and languages
project(@SOLVER@_hip_examples CXX)

# Enable testing
include(CTest)

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

# Specify the path to SUNDIALSConfig.cmake
set(SUNDIALS_DIR
  @CMAKE_INSTALL_PREFIX@/@SUNDIALS_INSTALL_CMAKEDIR@
  CACHE PATH "Location of SUNDIALSConfig.cmake")

find_package(SUNDIALS REQUIRED NO_DEFAULT_PATH)

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

# Set SUNDIALS targets needed
set(TARGETS_NEEDED @EXAMPLES_CMAKE_TARGETS@)

# List of SUNDIALS libraries
set(LIBRARIES
  ${TARGETS_NEEDED}
  ${EXTRA_LIBS})

# Need HIP
set(CMAKE_PREFIX_PATH "@CMAKE_PREFIX_PATH@")
find_package(HIP REQUIRED)

# Additional includes
include_directories(. @EXTRA_INCLUDES@)

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

# Set the names of the examples to be built and their dependencies
set(examples @EXAMPLES@)
if(examples)
  list(REMOVE_DUPLICATES examples)
endif()
set(examples_dependencies @EXAMPLES_DEPENDENCIES@)
foreach(depend ${examples_dependencies})
    set_source_files_properties(${depend} PROPERTIES LANGUAGE CXX)
endforeach()

# 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()
