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

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

set(CMAKE_Fortran_FLAGS
  "-O2 -g -pipe -Wformat -Werror=format-security -Wp,-U_FORTIFY_SOURCE,-D_FORTIFY_SOURCE=3 -fcommon -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -fstack-protector-strong -mbranch-protection=standard -fasynchronous-unwind-tables -fstack-clash-protection"
  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/gcc"
  CACHE FILEPATH "C compiler")

set(CMAKE_C_FLAGS
  "-O2 -g -pipe -Wformat -Werror=format-security -Wp,-U_FORTIFY_SOURCE,-D_FORTIFY_SOURCE=3 -fcommon -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -fstack-protector-strong -mbranch-protection=standard -fasynchronous-unwind-tables -fstack-clash-protection"
  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(IDA_F2003_parallel_examples C Fortran)

# Enable testing
include(CTest)

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

# Path to SUNDIALSConfig.cmake
set(SUNDIALS_DIR
  /usr//usr/lib64/cmake/sundials
  CACHE PATH "Location of SUNDIALSConfig.cmake")

# Path to SUNDIALS Fortran mod files
set(SUNDIALS_FMOD_DIR
  /usr//usr/lib/gcc/aarch64-mageia-linux/15/finclude
  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  ida_heat2D_kry_bbd_f2003)
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_INT64_T)

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

endforeach()
