# Custom targets always executed to force the generation of the source file 
# that contains compile flags, git info and module infos

string (REPLACE ";" " " CMAKE_Fortran_FLAGS_RELEASE_STR "${CMAKE_Fortran_FLAGS_RELEASE}")
cmake_host_system_information(RESULT MW_HOSTNAME QUERY HOSTNAME)
add_custom_target(gen_compilation_flags ALL
  COMMAND python ${PROJECT_SOURCE_DIR}/scripts/gen_compilation_flags.py ${PROJECT_BINARY_DIR}/version_compilation_flags.inc ${MW_HOSTNAME} ${CMAKE_Fortran_COMPILER} "${CMAKE_Fortran_FLAGS_RELEASE_STR}" " " " ") 

add_custom_target(gen_version_module_list ALL
  COMMAND ${PROJECT_SOURCE_DIR}/scripts/gen_module_list.sh ${PROJECT_BINARY_DIR}/version_module_list.inc 
  )

add_custom_target(gen_git_info ALL
  COMMAND ${PROJECT_SOURCE_DIR}/scripts/gen_git_info.sh ${PROJECT_BINARY_DIR}/version_git_info.inc ${MW_RELEASE} ${GEN_RELEASE}
  )

# Generation of _2DPBC.inc _3DPBC.inc files from _PBC.inc files in src
set (INC_FILES 
  coulomb_fix_molecule_energy       coulomb_sr  harmonic_angle  lennard_jones    rattle
  coulomb_melt_fix_molecule_forces  fumi_tosi   harmonic_bond   linear_molecule  minimum_image_distance
  )

foreach(F ${INC_FILES})
  set (PBC 2DPBC)
  configure_file (
    "${PROJECT_SOURCE_DIR}/src/${F}_PBC.inc"
    "${PROJECT_BINARY_DIR}/${F}_2DPBC.inc"
    )

  set (PBC 3DPBC)
  configure_file (
    "${PROJECT_SOURCE_DIR}/src/${F}_PBC.inc"
    "${PROJECT_BINARY_DIR}/${F}_3DPBC.inc"
    )
endforeach()

set ( SRC   coulomb.F90            coulomb_keq0.f90       errors.F90               fumi_tosi.F90
            kinds.F90              lennard_jones.F90      output.F90               parallel.F90
            version.F90            algorithms.f90         configuration.f90        coulomb_sr.f90
            ewald.f90              linear_molecule.f90    rattle.f90               thermostat.f90
            banner.f90             configuration_line.f90 diagnostic.f90           fileunit.f90
            localwork.f90          species.f90            timers.f90               box.f90
            constants.F90          electrode_charge.f90   harmonic_angle.F90       system.f90 
            species_parameters.f90 velocity.f90           cg.f90                   coulomb_lr.f90
            electrode.f90          harmonic_bond.F90      molecule.f90             stdio.f90
            command.f90            coulomb_self.f90       electrode_parameters.f90 ion.f90
            random.f90             
)

# Metalwalls is built as a static library such that it can be reussed for unit tests without recompiling everything
add_library(mw_lib STATIC ${SRC})
add_executable(mw main.f90)


if(WITH_MPI)
  find_package(MPI REQUIRED)

  # For supporting CMake < 3.9:

  if(NOT TARGET MPI::MPI_Fortran)
    add_library(MPI::MPI_Fortran IMPORTED INTERFACE)

    #Supress some trailing white spaces that breaks CMAKE with some MPI libs
    string(STRIP ${MPI_Fortran_LINK_FLAGS} MPI_Fortran_LINK_FLAGS)
    set_property(TARGET MPI::MPI_Fortran
      PROPERTY INTERFACE_COMPILE_OPTIONS ${MPI_Fortran_COMPILE_FLAGS})
    set_property(TARGET MPI::MPI_Fortran
      PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${MPI_Fortran_INCLUDE_PATH})
    set_property(TARGET MPI::MPI_Fortran
      PROPERTY INTERFACE_LINK_LIBRARIES ${MPI_Fortran_LINK_FLAGS} ${MPI_Fortran_LIBRARIES})
  endif()

  message(STATUS "Run: ${MPIEXEC} ${MPIEXEC_NUMPROC_FLAG} ${MPIEXEC_MAX_NUMPROCS} ${MPIEXEC_PREFLAGS} EXECUTABLE ${MPIEXEC_POSTFLAGS} ARGS")

  target_include_directories(mw_lib PUBLIC ${MPI_Fortran_INCLUDE_PATH})
  target_compile_options(mw_lib PUBLIC ${MPI_Fortran_COMPILE_FLAGS})
  target_link_libraries(mw_lib PUBLIC MPI::MPI_Fortran)
else()
  set( SERIAL_COMPILE_FLAG -DMW_SERIAL )
endif()

target_compile_options(mw_lib PRIVATE ${CI_OPTION} ${SERIAL_COMPILE_FLAG})
target_include_directories(mw_lib PUBLIC ${PROJECT_BINARY_DIR})
target_link_libraries(mw PUBLIC mw_lib)
add_dependencies(mw_lib gen_compilation_flags gen_version_module_list gen_git_info)

