# Link in pre-compiled LSDalton static libraries

#  Copyright (C) 2019 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/>.

# AJL April 2019 for FHI-aims

# paths
file(MAKE_DIRECTORY ${CHEMSH_BUILD_DIR}/lib/fhiaims)
set(FHIAIMS_LIB_DIRECTORY ${CHEMSH_BUILD_DIR}/lib/fhiaims)
set(CMAKE_Fortran_MODULE_DIRECTORY ${CHEMSH_BUILD_DIR}/modules/fhiaims)
include_directories(${CHEMSH_BUILD_DIR}/modules
                    ${CHEMSH_BUILD_DIR}/modules/parallel
                    ${CHEMSH_BUILD_DIR}/modules/dl_py2f)

if(NOT ${FHIAIMS} STREQUAL "")
    get_filename_component(FHIAMS ${FHIAIMS} ABSOLUTE)
endif()

if(EXISTS ${FHIAIMS})
    # Link in FHI-aims

    message("Found FHI-aims as ${FHIAIMS}")

    # compile libfhiaims.so (containing only Py-ChemShell interface files)
    add_library(fhiaims SHARED fhiaims.f90 fhiaims_routines.f90 ${LINKED})

    file(COPY ${FHIAIMS} DESTINATION ${FHIAIMS_LIB_DIRECTORY})
    get_filename_component(FHIAIMS_LIBNAME ${FHIAIMS} NAME)
    target_link_libraries(fhiaims ${FHIAIMS_LIB_DIRECTORY}/${FHIAIMS_LIBNAME})

else()
    # Don't link in FHI-aims

    # compile libfhiaims.so (containing only Py-ChemShell interface files and dummy FHI-aims main routine)
    add_library(fhiaims SHARED fhiaims.f90 fhiaims_routines.f90 fhiaims_dummies.f90 ${NOTLINKED})

endif()

# link the static library against libfhiaims.so
target_link_libraries(fhiaims dl_py2f parallel)
