# -----------------------------------------------------------------
# Programmer(s): David J. Gardner @ LLNL
# -----------------------------------------------------------------
# SUNDIALS Copyright Start
# Copyright (c) 2002-2024, 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.31.6)

# Set cache variables for compilers and flags
set(CMAKE_Fortran_COMPILER
  "/usr/bin/mpif90"
  CACHE FILEPATH "MPI Fortran compiler")

set(CMAKE_Fortran_FLAGS
  "-g -O2 -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer -ffile-prefix-map=/build/sundials-zNYaEP/sundials-7.1.1+dfsg1=. -fstack-protector-strong -fstack-clash-protection -mbranch-protection=standard -fdebug-prefix-map=/build/sundials-zNYaEP/sundials-7.1.1+dfsg1=/usr/src/sundials-7.1.1+dfsg1-10build3"
  CACHE STRING "Fortran compiler flags")

# Fortran preprocessor must be enabled
set(CMAKE_Fortran_PREPROCESS ON)

# Set cache variables for C 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(CMAKE_C_STANDARD
  "99"
  CACHE STRING "C standard")

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

set(CMAKE_CXX_FLAGS
  "-g -O2 -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer -ffile-prefix-map=/build/sundials-zNYaEP/sundials-7.1.1+dfsg1=. -fstack-protector-strong -fstack-clash-protection -Wformat -Werror=format-security -mbranch-protection=standard -fdebug-prefix-map=/build/sundials-zNYaEP/sundials-7.1.1+dfsg1=/usr/src/sundials-7.1.1+dfsg1-10build3 -Wdate-time -D_FORTIFY_SOURCE=3"
  CACHE STRING "C++ compiler flags")

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

# Specify project name and languages
project(IDA_F2003_parallel_examples C Fortran)

# Enable testing
include(CTest)

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

# Path to SUNDIALSConfig.cmake
set(SUNDIALS_DIR
  /usr/lib/aarch64-linux-gnu/cmake/sundials
  CACHE PATH "Location of SUNDIALSConfig.cmake")

# Path to SUNDIALS Fortran mod files
set(SUNDIALS_FMOD_DIR
  /usr/fortran
  CACHE PATH "Location of SUNDIALS Fortran mod files")

# Find SUNDIALS
find_package(SUNDIALS REQUIRED NO_DEFAULT_PATH)

# SUNDIALS targets needed
set(TARGETS_NEEDED  SUNDIALS::ida SUNDIALS::fida_mod SUNDIALS::nvecparallel SUNDIALS::fnvecparallel_mod)

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

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

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

# Examples to be built and their dependencies
set(examples )
set(examples_dependencies )
if(examples)
  list(REMOVE_DUPLICATES examples)
endif()

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

  # Keep fortran modules to a unique directory to avoid naming collisions
  set(CMAKE_Fortran_MODULE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${example}.dir)

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

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

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

  target_compile_definitions(${example} PRIVATE SUNDIALS_INT32_T)

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

endforeach()
