cmake_minimum_required(VERSION 3.7)

# ---------------------------------------------------------------
# $Revision: 4456 $
# $Date: 2015-03-28 20:22:03 -0700 (Sat, 28 Mar 2015) $
# ---------------------------------------------------------------
# Programmer:  Steven Smith @ LLNL
# ---------------------------------------------------------------
# LLNS Copyright Start
# Copyright (c) 2014, Lawrence Livermore National Security
# This work was performed under the auspices of the U.S. Department 
# of Energy by Lawrence Livermore National Laboratory in part under 
# Contract W-7405-Eng-48 and in part under Contract DE-AC52-07NA27344.
# Produced at the Lawrence Livermore National Laboratory.
# All rights reserved.
# For details, see the LICENSE file.
# LLNS Copyright End
# ---------------------------------------------------------------
# CMakeLists.txt file for nvector examples

set( MPI_MPICC "mpicc" )
set( MPI_MPICXX "mpicxx" )
set( MPI_MPIF77 "mpif77")
set( MPI_MPIF90 "mpif90")

find_package(PkgConfig)
pkg_check_modules(PC_PETSC REQUIRED PETSc)
find_library(PETSC_LIBRARY NAMES petsc
             HINTS ${PC_PETSC_LIBDIR} ${PC_PETSC_LIBRARY_DIRS} )
set(PETSC_LIBRARIES ${PETSC_LIBRARY} )
INCLUDE_DIRECTORIES( ${PC_PETSC_INCLUDEDIR} ${PC_PETSC_INCLUDE_DIRS} )

# Add variable nvector_petsc_examples with the names of the nvector examples

SET(nvector_petsc_examples
  test_nvector_petsc
)

SET(nvector_examples_dependencies
  test_nvector
  sundials_nvector
)

# Add source directory to include directories
INCLUDE_DIRECTORIES(. ..)
INCLUDE_DIRECTORIES(${PETSC_INCLUDE_DIR})

SET(NVECS_LIB sundials_nvecpetsc)

# Set-up linker flags and link libraries

SET(SUNDIALS_LIBS ${NVECS_LIB} ${EXTRA_LINK_LIBS})

IF(MPI_MPICC)
  # use MPI_MPICC as the compiler
  SET(CMAKE_C_COMPILER ${MPI_MPICC})
ELSE(MPI_MPICC)
  # add MPI_INCLUDE_PATH to include directories
  INCLUDE_DIRECTORIES(${MPI_INCLUDE_PATH})
  TARGET_LINK_LIBRARIES(${example} -lm)
  TARGET_LINK_LIBRARIES(${example} MPI_LIBRARIES)
ENDIF(MPI_MPICC)

# Add the build and install targets for each nvector example

FOREACH(example ${nvector_petsc_examples})

  ADD_EXECUTABLE(${example} ${example}.c ../test_nvector.c ../sundials_nvector.c)
  SET_TARGET_PROPERTIES(${example} PROPERTIES FOLDER "Examples")

  # Run sequentially

  # Run sequentially with arguments

  # Run in petsc on 4 procs
  
  # Parallel with additional arguments and supplying an answer file
  
  TARGET_LINK_LIBRARIES(${example} ${SUNDIALS_LIBS})
  TARGET_LINK_LIBRARIES(${example} ${PETSC_LIBRARIES})

  IF(EXAMPLES_INSTALL)
    INSTALL(FILES ${example}.c ../test_nvector.c ../test_nvector.h ../sundials_nvector.c DESTINATION ${EXAMPLES_INSTALL_PATH}/nvector/petsc)
  ENDIF(EXAMPLES_INSTALL)
ENDFOREACH(example ${nvector_petsc_examples})

IF(EXAMPLES_INSTALL)

  # Install the README file
  INSTALL(FILES DESTINATION ${EXAMPLES_INSTALL_PATH}/nvector/petsc)

  # Prepare substitution variables for Makefile and/or CMakeLists templates
  SET(SOLVER_LIB "sundials_nvecpetsc")
  LIST2STRING(nvector_petsc_examples EXAMPLES)
  LIST2STRING(nvector_examples_dependencies EXAMPLES_DEPENDENCIES)

  # Regardless of the platform we're on, we will generate and install 
  # CMakeLists.txt file for building the examples. This file  can then 
  # be used as a template for the user's own programs.

  # generate CMakelists.txt in the binary directory
  CONFIGURE_FILE(
      ${PROJECT_SOURCE_DIR}/examples/templates/cmakelists_petsc_C_ex.in
      ${PROJECT_BINARY_DIR}/examples/nvector/petsc/CMakeLists.txt
      @ONLY
      )

  # install CMakelists.txt
  INSTALL(
    FILES ${PROJECT_BINARY_DIR}/examples/nvector/petsc/CMakeLists.txt
    DESTINATION ${EXAMPLES_INSTALL_PATH}/nvector/petsc
    )

  # On UNIX-type platforms, we also  generate and install a makefile for 
  # building the examples. This makefile can then be used as a template 
  # for the user's own programs.

  IF(UNIX)
    # generate Makefile and place it in the binary dir
    CONFIGURE_FILE(
      ${PROJECT_SOURCE_DIR}/examples/templates/makefile_petsc_C_ex.in
      ${PROJECT_BINARY_DIR}/examples/nvector/petsc/Makefile_ex
      @ONLY
      )
    # install the configured Makefile_ex as Makefile
    INSTALL(
      FILES ${PROJECT_BINARY_DIR}/examples/nvector/petsc/Makefile_ex 
      DESTINATION ${EXAMPLES_INSTALL_PATH}/nvector/petsc
      RENAME Makefile
      )
  ENDIF(UNIX)

ENDIF(EXAMPLES_INSTALL)
