#  Copyright (C) 2017 The authors of Py-ChemShell
#
#  This file is part of Py-ChemShell.
#
#  Py-ChemShell is free software: you can redistribute it and/or modify
#  it under the terms of the GNU Lesser General Public License as
#  published by the Free Software Foundation, either version 3 of the
#  License, or (at your option) any later version.
#
#  Py-ChemShell is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU Lesser General Public License for more details.
#
#  You should have received a copy of the GNU Lesser General Public
#  License along with Py-ChemShell.  If not, see
#  <http://www.gnu.org/licenses/>.

if(${WITH_MPI})
    set(PARALLEL_SOURCES parallel.f90
                         taskfarm.f90
                         parallel.c)
else()
    set(PARALLEL_SOURCES serial.f90
                         serial.c)
endif()

# YL 17/03/2026: added a system mpiexec command for system calls
configure_file(mpiexec.py.in mpiexec.py @ONLY)

if(${CHEMSH_ARCH} STREQUAL "cray")
#    set_target_properties(parallel PROPERTIES LINK_FLAGS "-Wl,-rpath -Wl,${CHEMSH_LD_LIBRARY_PATH} -Wl,--version-script=${CMAKE_CURRENT_LIST_DIR}/export.map")
elseif(${CHEMSH_ARCH} STREQUAL "cpe-gnu")
    # the CPE (Cray MPICH with GNU) compiler doesn't compile parallel.f90 which works well with the GNU/OpenMPI
    set(PARALLEL_SOURCES parallel_cpe_gnu.f90 taskfarm.f90 parallel.c)
    if(DEBUG)
        set_source_files_properties(parallel_cpe_gnu.f90 PROPERTIES COMPILE_FLAGS "-O0 -g")
    endif()
elseif(${CHEMSH_ARCH} STREQUAL "gnu")
    set_source_files_properties(parallel.c PROPERTIES COMPILE_FLAGS "-Wno-format-contains-nul")
elseif(${CHEMSH_ARCH} STREQUAL "intel")
endif()

# compile
set(CMAKE_Fortran_MODULE_DIRECTORY ${CHEMSH_BUILD_DIR}/modules/parallel)
add_library(parallel SHARED ${PARALLEL_SOURCES})

# symbols control
set_target_properties(parallel PROPERTIES LINK_FLAGS "-Wl,--version-script=${CMAKE_CURRENT_LIST_DIR}/export.map ${CHEMSH_LINK_FLAGS}")

# additional parallel programming schemes: Global Arrays (GA), Charm++
list(APPEND PARALLELISM_APIS ga
                             charm++
    )

foreach(PARALLELISM_API ${PARALLELISM_APIS})
    add_subdirectory(${PARALLELISM_API})
    # clean policy
    set(CURRENT_DIR ${PARALLELISM_API})
    include(${CHEMSH_DIR}/chemsh/utils/cmake/clean.cmake)
endforeach(PARALLELISM_API)

add_dependencies(parallel ga)
add_dependencies(parallel dl_py2f)
target_link_libraries(parallel ga dl_py2f)

