# Build Construct as a shared library using its native Makefile

#  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/>.

# m.farrow@ucl.ac.uk
# you.lu@stfc.ac.uk
# May 2016

# paths
set(CMAKE_Fortran_MODULE_DIRECTORY ${CHEMSH_BUILD_DIR}/modules/cluster)

# YL 01/08/2018: cluster_create.f90 removed and merged into cluster_routines.f90
list(APPEND CLUSTER_FSOURCES calerf.f90
                             cluster.f90
                             cluster_adjust_charge.f90
                             cluster_cut.f90
                             cluster_fit_bqs.f90
                             cluster_routines.f90
                             coord_transform.f90
                             periodic_electrostatics.f90
                             vector_utils.f90
)

# setting property GENERATED forces CMake to check dependency
#set_source_files_properties(${CLUSTER_FSOURCES} PROPERTIES GENERATED TRUE)

# needr math lib
include_directories(${CHEMSH_BUILD_DIR}/modules
                    ${CHEMSH_BUILD_DIR}/modules/cluster
                    ${CHEMSH_BUILD_DIR}/modules/dl_py2f)

# compile:
add_library(cluster SHARED ${CLUSTER_FSOURCES})
target_link_libraries(cluster dl_py2f)

# preprocessor using lowercase .f90 extension
if(${CHEMSH_ARCH} STREQUAL "gnu")
    target_link_libraries(cluster ${BLAS_LIBRARIES} ${LAPACK_LIBRARIES})
    target_link_libraries(cluster "${CHEMSH_ScaLAPACK_FLAGS}")

elseif(${CHEMSH_ARCH} STREQUAL "cpe-gnu")
    target_link_libraries(cluster ${BLAS_LIBRARIES} ${LAPACK_LIBRARIES})
    set(CHEMSH_CLUSTER_FFLAGS "-std=gnu -pedantic")
    target_link_libraries(cluster "${CHEMSH_ScaLAPACK_FLAGS}")
elseif(${CHEMSH_ARCH} STREQUAL "intel")
    set(CHEMSH_CLUSTER_FFLAGS "-fpp")
    # TWK 4/3/2021 Using target_link_libraries puts the MKL flags in the 
    #              correct position on the link line (i.e. at the end, after files)
    target_link_libraries(cluster "${CHEMSH_MKL_FLAGS}")
    set_target_properties(cluster PROPERTIES COMPILE_FLAGS "${CHEMSH_CLUSTER_FFLAGS} ${MKL_COMPILE_FLAGS}")
endif()

# use export.map to avoid name clashing
# YL 06/03/2021: some newer versions of CMake can't take the -Wl,--version-script= clause to target_link_libraries()
#                so let's still use the set_target_properties()
set_target_properties(cluster PROPERTIES LINK_FLAGS "-Wl,--version-script=${CMAKE_CURRENT_LIST_DIR}/export.map")


