
add_library(mpi_siesta
  # Sources
  mpi_siesta.F90
  mpi_interfaces.F90
  mpi__include.f90
  Interfaces.f90
  )

# We need to find "Interfaces.f90", which is generated in the build
#("binary") directory, so we need to add this directory to the list
# The following line will work also, but it is better practice to have
# target-specific settings
###set(CMAKE_INCLUDE_CURRENT_DIR ON)
target_include_directories(mpi_siesta PRIVATE "${CMAKE_CURRENT_BINARY_DIR}")

target_compile_definitions(
  mpi_siesta
  PUBLIC
  "$<$<BOOL:${SIESTA_WITH_GRID_SP}>:GRID_SP>"
  "$<$<BOOL:${SIESTA_WITH_NO_MPI_INTERFACES}>:NO_MPI_INTERFACES>"
)

  target_link_libraries(mpi_siesta PUBLIC MPI::MPI_Fortran)
  # So that clients of mpi_siesta can find its modules:
  target_include_directories(mpi_siesta INTERFACE ${CMAKE_CURRENT_BINARY_DIR})


# Handling of interface-generation script
# These generation scripts do not work with the suffix for the executables
siesta_suffix(
  NEW ""
  VARIABLES EXECUTABLE)

# Create the int and kind explorer targets
add_executable(mpi_int_explorer
  # Do not default to build this one, it is only
  # a temporary build dependency
  EXCLUDE_FROM_ALL
  int_explorer.f90
  )

add_executable(mpi_kind_explorer
  # Do not default to build this one, it is only
  # a temporary build dependency
  EXCLUDE_FROM_ALL
  kind_explorer.f90
  )

# Restore the old suffix before using this one.
siesta_suffix(
  POP
  VARIABLES EXECUTABLE)

# By setting the OUTPUT line, we ensure that this generation command
# is run before attempting to compile mpi_siesta
  
add_custom_command(
  # These are output files generated by the generate.sh script
  OUTPUT Interfaces.f90 VS.uses V_S.uses
  # Dependency targets
  DEPENDS mpi_int_explorer mpi_kind_explorer
  COMMAND sh "${CMAKE_CURRENT_SOURCE_DIR}/generate.sh"
  VERBATIM
  COMMENT "Creating MPI interfaces and modules"
  )


