# -----------------------------------------------------------------
# Programmer(s): Radu Serban @ LLNL
#                David J. Gardner @ LLNL
# -----------------------------------------------------------------
# SUNDIALS Copyright Start
# Copyright (c) 2002-2019, 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
# -----------------------------------------------------------------
# CMakeLists.txt for CVODES OpenMP examples
#
# This file is generated from a template using variables
# set at configuration time. It can be used as a template for
# other user CMakeLists configuration files.
# -----------------------------------------------------------------

cmake_minimum_required(VERSION 3.5)

# Specify project name
project(CVODES_openmp_examples C)

# Enable testing
include(CTest)

mark_as_advanced(EXECUTABLE_OUTPUT_PATH LIBRARY_OUTPUT_PATH)

# Examples using SUNDIALS linear solvers
set(examples  cvsAdvDiff_bnd_omp)
set(examples_dependencies )

# Examples using LAPACK linear solvers
set(examples_bl )
set(examples_dependencies_bl )

# Examples using KLU linear solvers
set(examples_klu )
set(examples_dependencies_klu )

# Examples using SuperLU_MT linear solvers
set(examples_slumt )
set(examples_dependencies_slumt )

# Specify path to SUNDIALS header files
set(SUNDIALS_INC_DIR
  /usr/include
  CACHE STRING
  "Location of SUNDIALS header files")

# Add path to SUNDIALS header files
include_directories(${SUNDIALS_INC_DIR})

# Set search path for SUNDIALS libraries
set(SUNDIALS_LIB_DIR /usr/lib)

# Find the SUNDIALS solver's library
find_library(SUNDIALS_SOLVER_LIB
  sundials_cvodes ${SUNDIALS_LIB_DIR}
  DOC "CVODES library")

# Find the NVECTOR library
find_library(SUNDIALS_NVEC_LIB
  sundials_nvecopenmp ${SUNDIALS_LIB_DIR}
  DOC "NVECTOR library")

# Set additional libraries
set(SUNDIALS_EXTRA_LIB  -lm /usr/lib/librt.so CACHE STRING "Additional libraries")

# Set other libraries libraries
set(LAPACK_LIBRARIES  CACHE STRING "Lapack libraries")

include_directories()
set(SUPERLUMT_LIBRARIES   CACHE STRING "SuperLUMT libraries")
set(SUPERLUMT_THREAD_TYPE  CACHE STRING "SuperLUMT Threading")

include_directories(/usr/include)
set(KLU_LIBRARIES /usr/lib/libklu.so;/usr/lib/libamd.so;/usr/lib/libcolamd.so;/usr/lib/libbtf.so;/usr/lib/libsuitesparseconfig.so CACHE STRING "KLU libraries")

# List of Sundials libraries shared across all examples
set(SUNDIALS_LIBS ${SUNDIALS_SOLVER_LIB} ${SUNDIALS_NVEC_LIB} ${SUNDIALS_EXTRA_LIB})

# find OpenMP
find_package(OpenMP)
if(OPENMP_FOUND)
  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
  # Use C flags for linker as well.
  set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_C_FLAGS}")
else(OPENMP_FOUND)
  message(STATUS "Disabling OpenMP support, could not determine compiler flags")
endif(OPENMP_FOUND)

# Build examples with SUNDIALS linear solvers one by one
foreach(example ${examples})

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

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

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

endforeach(example ${examples})

# Build each example with LAPACK linear solvers one by one
if(LAPACK_LIBRARIES)
  foreach(example ${examples_bl})

    # Find the Sundials linear solver libraries
    find_library(SUNDIALS_SUNLINSOLLAPACKBAND_LIB
      sundials_sunlinsollapackband ${SUNDIALS_LIB_DIR}
      DOC "Sundials banded LAPACK linear solver library")

    find_library(SUNDIALS_SUNLINSOLLAPACKDENSE_LIB
      sundials_sunlinsollapackdense ${SUNDIALS_LIB_DIR}
      DOC "Sundials dense LAPACK linear solver library")

    # example source files
    add_executable(${example} ${example}.c ${examples_dependencies_bl})

    # libraries to link against
    target_link_libraries(${example} ${SUNDIALS_LIBS})
    target_link_libraries(${example} ${SUNDIALS_SUNLINSOLLAPACKBAND_LIB})
    target_link_libraries(${example} ${SUNDIALS_SUNLINSOLLAPACKDENSE_LIB})
    target_link_libraries(${example} ${LAPACK_LIBRARIES})

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

  endforeach(example ${examples_bl})
endif()


# Build each example with KLU linear solvers one by one
if(KLU_LIBRARIES)
  foreach(example ${examples_klu})

    # Find the Sundials linear solver libraries
    find_library(SUNDIALS_SUNLINSOLKLU_LIB
      sundials_sunlinsolklu ${SUNDIALS_LIB_DIR}
      DOC "Sundials KLU linear solver library")

    # example source files
    add_executable(${example} ${example}.c ${examples_dependencies_klu})

    # libraries to link against
    target_link_libraries(${example} ${SUNDIALS_LIBS})
    target_link_libraries(${example} ${SUNDIALS_SUNLINSOLKLU_LIB})
    target_link_libraries(${example} ${KLU_LIBRARIES})

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

  endforeach(example ${examples_klu})
endif()


# Build each example with SuperLU_MT linear solvers one by one
if(SUPERLUMT_LIBRARIES)

  # Find the Sundials linear solver libraries
  find_library(SUNDIALS_SUNLINSOLSLUMT_LIB
    sundials_sunlinsolsuperlumt ${SUNDIALS_LIB_DIR}
    DOC "Sundials KLU linear solver library")

  if(SUPERLUMT_THREAD_TYPE STREQUAL "PTHREAD")
    find_package(Threads REQUIRED)
  endif()

  foreach(example ${examples_slumt})

    # example source files
    add_executable(${example} ${example}.c ${examples_dependencies_slumt})

    # libraries to link against
    target_link_libraries(${example} ${SUNDIALS_LIBS})
    target_link_libraries(${example} ${SUNDIALS_SUNLINSOLSLUMT_LIB})
    target_link_libraries(${example} ${SUPERLUMT_LIBRARIES})

    if(SUPERLUMT_THREAD_TYPE STREQUAL "PTHREAD")
      target_link_libraries(${example} Threads::Threads)
    endif()

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

  endforeach(example ${examples_slumt})
endif()
