# -----------------------------------------------------------------
# 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_Fortran_COMPILER
  /usr/bin/mpif90
  CACHE FILEPATH "Fortran compiler")
set(CMAKE_Fortran_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"
  CACHE STRING "Fortran compiler flags")

set(CMAKE_C_COMPILER
  /usr/bin/mpicc
  CACHE FILEPATH "C compiler")
set(CMAKE_C_FLAGS
  "-fcommon"
  CACHE STRING "C compiler flags")

set(CMAKE_CXX_COMPILER
  /usr/bin/mpicxx
  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")
set(CMAKE_CXX_STANDARD 11)

set(CMAKE_CUDA_COMPILER
  
  CACHE FILEPATH "CUDA compiler")
set(CMAKE_CUDA_FLAGS
  ""
  CACHE STRING "CUDA compiler flags")
set(CMAKE_CUDA_STANDARD 11)

set(CMAKE_CUDA_HOST_COMPILER
  ${MPI_CXX_COMPILER}
  CACHE FILEPATH "CUDA host compiler")

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

# Enable testing
include(CTest)

# Require MPI
find_package(MPI REQUIRED)

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

# 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(SUNDIALS_EXTRA_LIBS  -lm /usr/lib/x86_64-linux-gnu/librt.a CACHE STRING "Additional libraries")

# Set SUNDIALS targets needed
set(TARGETS_NEEDED SUNDIALS::arkode;SUNDIALS::nvecparallel;SUNDIALS::nvecmpiplusx)

# List of libraries
set(LIBRARIES_TO_LINK
    ${TARGETS_NEEDED}
    ${SUNDIALS_EXTRA_LIBS})

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

# Set the names of the examples to be built and their dependencies
set(examples  ark_heat2D_p)
set(examples_dependencies )
if(examples)
  list(REMOVE_DUPLICATES examples)
endif()

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

  # example source files
  add_executable(${example} ${example}.cpp ${examples_dependencies})

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

  # libraries to link against
  target_link_libraries(${example} ${LIBRARIES_TO_LINK})

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

endforeach()
