set(DBCSR_PROGRAM_SRCS_FTN dbcsr_example_1.F dbcsr_example_2.F
                           dbcsr_example_3.F dbcsr_tensor_example_1.F)

set(DBCSR_PROGRAM_SRCS_CPP dbcsr_example_3.cpp dbcsr_tensor_example_2.cpp)

# Compile Fortran examples
foreach (dbcsr_program_src ${DBCSR_PROGRAM_SRCS_FTN})
  get_filename_component(dbcsr_program_name ${dbcsr_program_src} NAME_WE)
  if (USE_HIP)
    hip_add_executable(${dbcsr_program_name} ${dbcsr_program_src})
  else ()
    add_executable(${dbcsr_program_name} ${dbcsr_program_src})
  endif ()
  target_link_libraries(${dbcsr_program_name} dbcsr)

  # with the Intel compiler CMake 3.12 seems to forget that the source is
  # actually Fortran and needs to be told explicitly:
  set_target_properties(${dbcsr_program_name} PROPERTIES LINKER_LANGUAGE
                                                         Fortran)
endforeach ()

# Compile C++ examples
if (WITH_C_API)
  foreach (dbcsr_program_src ${DBCSR_PROGRAM_SRCS_CPP})
    get_filename_component(dbcsr_program_name ${dbcsr_program_src} NAME_WE)
    set(dbcsr_program_name ${dbcsr_program_name}_cpp)
    if (USE_HIP)
      hip_add_executable(${dbcsr_program_name} ${dbcsr_program_src})
    else ()
      add_executable(${dbcsr_program_name} ${dbcsr_program_src})
    endif ()
    target_link_libraries(${dbcsr_program_name} dbcsr_c MPI::MPI_CXX)

    if (CMAKE_CXX_COMPILER_ID STREQUAL "Cray")
      # for recent Cray compiler versions CMake doesn't know
      target_compile_options(${dbcsr_program_name} PRIVATE "-hstd=c++14")
    else ()
      target_compile_features(${dbcsr_program_name} PRIVATE cxx_std_14)
    endif ()
  endforeach ()
endif ()

# =================================== DOCUMENTATION GENERATION Copy example
# source files into the build directory so that their documentation can be
# generated by FORD

set(DBCSR_PROGRAM_SRCS ${DBCSR_PROGRAM_SRCS_FTN} ${DBCSR_PROGRAM_SRCS_CPP})

# Make a list of the copy commands
set(example_copy_commands)
foreach (example ${DBCSR_PROGRAM_SRCS})
  list(
    APPEND
    example_copy_commands
    COMMAND
    ${CMAKE_COMMAND}
    -E
    copy
    ${CMAKE_SOURCE_DIR}/examples/${example}
    ${CMAKE_BINARY_DIR}/examples)
endforeach ()

add_custom_target(
  doc_copy_examples
  COMMENT "Copy examples for documentation generation"
  COMMAND mkdir -p ${CMAKE_BINARY_DIR}/examples ${example_copy_commands}
  VERBATIM)
