# Build GA as a static 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/>.

# you.lu@stfc.ac.uk
# June 2015
# revised Aug 2017

include(ExternalProject)

# if GA is not defined
if("${GA}" STREQUAL "")
    # if NWCHEM is defined
    if(NOT "${NWCHEM}" STREQUAL "")
        get_filename_component(NWCHEM_ABS_PATH ${NWCHEM} ABSOLUTE)
        # there is no extended regex in file(GLOB) so that we have to string(REGEX MATCH)
        file(GLOB GA_VERSION "${NWCHEM_ABS_PATH}/src/tools/ga-[0-9]\.[0-9]*")
        if("${GA_VERSION}" STREQUAL "")
            set(GA_VERSION "unknown")
        endif()
        string(REGEX MATCH "ga-[0-9]\.[0-9]([\.][0-9])?" GA_VERSION "${GA_VERSION}")
        set(GA_VERSION "${GA_VERSION}" CACHE STRING "GA version")
        file(TO_CMAKE_PATH ${NWCHEM_ABS_PATH}/src/tools/${GA_VERSION} GA)
    endif()
else()
    get_filename_component(GA ${GA} ABSOLUTE)
endif()

# source paths
get_filename_component(GA_INCLUDE_DIR ${GA}/../install/include ABSOLUTE)
#set(GA_INCLUDE_DIR     ${GA}/global/src)
set(MA_INCLUDE_DIR     ${GA}/ma)
set(GLOBAL_INCLUDE_DIR ${GA}/global/src)
set(NWCHEM_LIB_DIR     ${NWCHEM}/lib/${NWCHEM_TARGET}) # do NOT use ChemShell NWChem lib folder as could not be ready yet
set(PEIGS_INCLUDE_DIR  ${NWCHEM}/src/peigs) # do NOT use ChemShell NWChem lib folder as could not be ready yet

# desitination paths
set(CHEMSH_GA_LIB_DIR        ${CHEMSH_BUILD_DIR}/lib/ga)
set(CHEMSH_GA_INCLUDE_DIR    ${CHEMSH_BUILD_DIR}/include/ga)
set(CHEMSH_MA_INCLUDE_DIR    ${CHEMSH_BUILD_DIR}/include/ga/ma)
set(CHEMSH_PEIGS_INCLUDE_DIR ${CHEMSH_BUILD_DIR}/include/ga/peigs)
file(MAKE_DIRECTORY ${CHEMSH_GA_LIB_DIR})
file(MAKE_DIRECTORY ${CHEMSH_GA_INCLUDE_DIR})
file(MAKE_DIRECTORY ${CHEMSH_MA_INCLUDE_DIR})
file(MAKE_DIRECTORY ${CHEMSH_PEIGS_INCLUDE_DIR})

# if GA specified by user
set(GA_NAME ${GA_VERSION})

if(EXISTS ${GA})

    # if is directory
    if(IS_DIRECTORY ${GA})
        set(GA_SRC_DIR    ${GA})
        set(GA_LIB_DIR    ${GA}/.libs)
        # See: http://hpc.pnl.gov/globalarrays
        set(ComEx_SRC_DIR ${GA}/armci)
        set(ComEx_LIB_DIR ${GA}/comex/.libs)
        set(PeIGS_LIB_DIR ${NWCHEM_LIB_DIR})
    endif()

    # PEIGS header files
    list(APPEND PEIGS_HEADER_FILES ${PEIGS_INCLUDE_DIR}/h/blas_lapackf.h
                                   ${PEIGS_INCLUDE_DIR}/h/blas_lapack.h
                                   ${PEIGS_INCLUDE_DIR}/h/clustr_inv.h
                                   ${PEIGS_INCLUDE_DIR}/h/defines.h
                                   ${PEIGS_INCLUDE_DIR}/h/globalp.c.h
                                   ${PEIGS_INCLUDE_DIR}/h/peigs_types.h
                                   ${PEIGS_INCLUDE_DIR}/h/timing.h)
    foreach(PEIGS_HEADER ${PEIGS_HEADER_FILES})
        if(EXISTS ${PEIGS_HEADER})
            file(COPY ${PEIGS_HEADER} DESTINATION ${CHEMSH_PEIGS_INCLUDE_DIR})
        endif()
    endforeach(PEIGS_HEADER)

    # GA header files
    list(APPEND GA_HEADER_FILES ${GA_INCLUDE_DIR}/global.fh
                                ${GA_INCLUDE_DIR}/gacommon.h)
    foreach(GA_HEADER ${GA_HEADER_FILES})
        if(EXISTS ${GA_HEADER})
            file(COPY ${GA_HEADER} DESTINATION ${CHEMSH_GA_INCLUDE_DIR})
        endif()
    endforeach(GA_HEADER)

    # MA header files
    # because we always do a "make clean" first, so mafdecls.fh will be cleared from ${MA_INCLUDE_DIR} but found in ${GA_INCLUDE_DIR}
    list(APPEND MA_HEADER_FILES ${GA_INCLUDE_DIR}/macommon.h
                                ${GA_INCLUDE_DIR}/maf2c.fh
                                ${GA_INCLUDE_DIR}/mafdecls.fh
                                ${GA_INCLUDE_DIR}/error.h
                                ${GA_INCLUDE_DIR}/macdecls.h
                                ${MA_INCLUDE_DIR}/ma.h
                                ${GA_INCLUDE_DIR}/matypes.h
                                ${MA_INCLUDE_DIR}/memcpy.h
                                ${GA_INCLUDE_DIR}/scope.h
                                ${GA_INCLUDE_DIR}/string-util.h
                                ${GA_INCLUDE_DIR}/table.h)

    foreach(MA_HEADER ${MA_HEADER_FILES})
        if(EXISTS ${MA_HEADER})
            file(COPY ${MA_HEADER} DESTINATION ${CHEMSH_MA_INCLUDE_DIR})
        endif()
    endforeach(MA_HEADER)

    # YL 16/11/2021: enabled C++ interface
    if(NOT ${CMAKE_CXX_COMPILER} STREQUAL "")
        set(GA_ENABLE_CXX "--enable-cxx")
    endif()

    ### Intel ###
    if(${CHEMSH_ARCH} STREQUAL "intel")

        # determine 32-/64-bit MKL
        if(CHEMSH_64_BIT)
            set(GA_MATH_SUFFIX "8")
        else()
            set(GA_MATH_SUFFIX "4")
        endif()

        # force 64-bit integers to match NWChem (see interfaces/nwchem/CMakeLists.txt)
        set(GA_64_BIT_FLAG "--enable-i8")
#        set(GA_INTEGER_SIZE "-i8")

        # YL 10/01/2020: to avoid such warning (never seen before)
        # configure: WARNING: Unable to find a flag to promote Fortran integers
        # configure: WARNING: INTEGER*8 promotion is not supported
        set(GA_INTEGER_SIZE "64")

        if(DEBUG)
        else()
            set(GA_DIAG_FLAGS "-diag-disable=warn")
        endif()

        set(GA_BLAS_FLAGS   "${CHEMSH_MKL_FLAGS}")
        set(GA_LAPACK_FLAGS "${CHEMSH_MKL_FLAGS}")

        # CPP is the preprocessor (not C++!)
        set(GA_PREPROCESSOR "${CMAKE_C_COMPILER} -E")
        set(GA_CFLAGS       "-fPIC -g -O2")
        set(GA_FFLAGS       "-O2 -fPIC ${GA_DIAG_FLAGS} ${CHEMSH_EXTRA_FFLAGS} -integer-size ${GA_INTEGER_SIZE}")
        set(GA_CFLAGS       "-O2 -fPIC ${GA_DIAG_FLAGS} ${CHEMSH_EXTRA_CFLAGS} -diag-disable=10441")
        set(GA_MPI_LIBS     "-lmpifort -lmpi -lmpigi -ldl -lrt -lpthread")

        # YL: if you see "configure: WARNING: ScaLAPACK library not found, interfaces won't be defined"
        #     when compiling GA there must be something WRONG!

        if(${GA_RECONFIGURE})
            set(GA_CONFIGURE_COMMAND ./configure --enable-peigs
                                                 --enable-underscoring
                                                 --enable-mpi-tests
                                                 --with-tcgmsg
                                                 --with-mpi
                                                 --with-mpi-ts
                                                 --with-blas${GA_MATH_SUFFIX}=${GA_BLAS_FLAGS}
                                                 --with-lapack=${GA_LAPACK_FLAGS}
                                                 --with-scalapack=${CHEMSH_ScaLAPACK_FLAGS}
                                                 ${GA_ENABLE_CXX}
                                                 F77=${CMAKE_Fortran_COMPILER}
                                                 CC=${CMAKE_C_COMPILER}
                                                 CXX=${CMAKE_CXX_COMPILER}
                                                 CPP=${GA_PREPROCESSOR}
                                                 MPIF77=${CMAKE_Fortran_COMPILER}
                                                 MPICC=${CMAKE_C_COMPILER}
                                                 MPICXX=${CMAKE_CXX_COMPILER}
                                                 FFLAGS=${GA_FFLAGS}
                                                 CFLAGS=${GA_CFLAGS}
                                                 SCOREP_WRAPPER=OFF
                                                 ARMCI_DEFAULT_SHMMAX_UBOUND=131072
                                                 > ${CMAKE_CURRENT_LIST_DIR}/_ga_config.log)
        else()
            set(GA_CONFIGURE_COMMAND "")
        endif()

        set(GA_MAKE_COMMAND make -j${CHEMSH_MAKE_NJOBS} > ${CMAKE_CURRENT_LIST_DIR}/_ga_make.log)

        set(ARMCI_WITH_MPI "-lmpifort -lmpi -lmpigi -ldl -lrt -lpthread")

        if(${GA_RECONFIGURE})
            set(ComEx_CONFIGURE_COMMAND ./configure --with-mpi-ts
                                                    --with-blas${GA_MATH_SUFFIX}=${GA_BLAS_FLAGS}
                                                    FC=${CMAKE_Fortran_COMPILER}
                                                    F77=${CMAKE_Fortran_COMPILER}
                                                    CC=${CMAKE_C_COMPILER}
                                                    MPIF77=${CMAKE_Fortran_COMPILER}
                                                    MPICC=${CMAKE_C_COMPILER}
                                                    MPICXX=${CMAKE_CXX_COMPILER}
                                                    CFLAGS=${GA_CFLAGS}
                                                    ARMCI_DEFAULT_SHMMAX_UBOUND=131072
                                                    > ${CMAKE_CURRENT_LIST_DIR}/_comex_config.log)
        else()
            set(ComEx_CONFIGURE_COMMAND "")
        endif()

        set(ComEx_MAKE_COMMAND make -j${CHEMSH_MAKE_NJOBS} CFLAG_NO_LOOP_OPT= > ${CMAKE_CURRENT_LIST_DIR}/_comex_make.log)

    ### Cray GNU (ARCHER2 only) ###
    elseif(${CHEMSH_ARCH} STREQUAL "cpe-gnu")

        # determine 32-/64-bit MKL
        if(CHEMSH_64_BIT)
            set(GA_MATH_SUFFIX "8")
        else()
            set(GA_MATH_SUFFIX "4")
        endif()

        # force 64-bit integers to match NWChem (see interfaces/nwchem/CMakeLists.txt)

        set(GA_INTEGER_SIZE "64")

        set(GA_BLAS_FLAGS      "-L${BLAS_DIR} -lsci_gnu_mpi")
        set(GA_LAPACK_FLAGS    "-L${LAPACK_DIR} -lsci_gnu_mpi")
#        set(GA_ScaLAPACK_FLAGS "-L${ScaLAPACK_DIR} -lsci_gnu_mpi")

        if(DEBUG)
            set(GA_CFLAGS "-O0 -g -fPIC -Wall")
            set(GA_FFLAGS "-std=legacy -O0 -g -fPIC -fdefault-integer-8 -finit-local-zero -finit-integer=0 -finit-real=zero ${CHEMSH_EXTRA_FFLAGS} ${GA_WARN_FLAGS}")
        else()
            set(GA_CFLAGS "-O2 -fPIC -fopenmp -funroll-loops -ffast-math -fno-finite-math-only -m64 ${CHEMSH_EXTRA_CFLAGS} -Wno-all")
            set(GA_CFLAGS "-O2 -fPIC -fopenmp")
            set(GA_FFLAGS "-std=legacy -O2 -fPIC -fopenmp -fdefault-integer-8 -fbacktrace -finit-local-zero -finit-integer=0 -finit-real=zero ${CHEMSH_EXTRA_FFLAGS} -Wno-all")
            set(GA_FFLAGS "-std=legacy -O2 -fPIC -fopenmp -fdefault-integer-8")
#            set(GA_FFLAGS "-O2 -fPIC -std=legacy -fdefault-integer-8 ${CHEMSH_EXTRA_FFLAGS}")
        endif()
        set(GA_MPI_LIBS "-L/${MPI_LIB_PATH} -lmpich")

        # CPP is the preprocessor (not C++!)
        set(GA_PREPROCESSOR "${CMAKE_C_COMPILER} -E")

        if(${GA_RECONFIGURE})
            set(GA_CONFIGURE_COMMAND ./configure --enable-peigs
                                                 --enable-underscoring
                                                 --enable-mpi-tests
                                                 --with-tcgmsg
                                                 --with-mpi
                                                 --with-mpi-ts
                                                 --with-blas${GA_MATH_SUFFIX}=${GA_BLAS_FLAGS}
                                                 --with-lapack=${GA_LAPACK_FLAGS}
                                                 --with-scalapack=${CHEMSH_ScaLAPACK_FLAGS}
                                                 ${GA_ENABLE_CXX}
                                                 F77=${CMAKE_Fortran_COMPILER}
                                                 CC=${CMAKE_C_COMPILER}
                                                 CXX=${CMAKE_CXX_COMPILER}
                                                 CPP=${GA_PREPROCESSOR}
                                                 MPIF77=${CMAKE_Fortran_COMPILER}
                                                 MPICC=${CMAKE_C_COMPILER}
                                                 MPICXX=${CMAKE_CXX_COMPILER}
                                                 FFLAGS=${GA_FFLAGS}
                                                 CFLAGS=${GA_CFLAGS}
                                                 SCOREP_WRAPPER=OFF
                                                 ARMCI_DEFAULT_SHMMAX_UBOUND=131072
                                                 > ${CMAKE_CURRENT_LIST_DIR}/_ga_config.log)
        else()
            set(GA_CONFIGURE_COMMAND "")
        endif()

        set(GA_MAKE_COMMAND make -j${CHEMSH_MAKE_NJOBS} > ${CMAKE_CURRENT_LIST_DIR}/_ga_make.log)

        if(${GA_RECONFIGURE})
            set(ComEx_CONFIGURE_COMMAND ./configure --with-mpi-ts
                                                    CC=${CMAKE_C_COMPILER}
                                                    MPICC=${CMAKE_C_COMPILER}
                                                    MPICXX=${CMAKE_CXX_COMPILER}
                                                    CFLAGS=${GA_CFLAGS}
                                                    CFLAG_NO_LOOP_OPT=
                                                    ARMCI_DEFAULT_SHMMAX_UBOUND=131072
                                                    > ${CMAKE_CURRENT_LIST_DIR}/_comex_config.log)
        else()
            set(ComEx_CONFIGURE_COMMAND "")
        endif()

        set(ComEx_MAKE_COMMAND make -j${CHEMSH_MAKE_NJOBS} CFLAG_NO_LOOP_OPT= > ${CMAKE_CURRENT_LIST_DIR}/_comex_make.log)


#        set(GA_CONFIGURE_COMMAND    "")
#        set(ComEx_CONFIGURE_COMMAND "")
#        set(GA_MAKE_COMMAND         "")
#        set(ComEx_MAKE_COMMAND      "")

    ### GNU ###
    elseif(${CHEMSH_ARCH} STREQUAL "gnu")
        # GNU: -fno-aggressive-loop-optimizations -fdefault-integer-8
        set(GA_MAKE_COMMAND make ENABLE_PEIGS=TRUE F77=${CMAKE_Fortran_COMPILER} CC=${CMAKE_C_COMPILER} FFLAGS=-fPIC CFLAGS=-fPIC > _ga_make.log)
    endif()
    set(GA_INSTALL_COMMAND    ${CMAKE_COMMAND} -E copy ${GA_LIB_DIR}/libga.a       ${CHEMSH_GA_LIB_DIR})
    set(ComEx_INSTALL_COMMAND ${CMAKE_COMMAND} -E copy ${ComEx_LIB_DIR}/libarmci.a ${CHEMSH_GA_LIB_DIR})
    set(PeIGS_INSTALL_COMMAND ${CMAKE_COMMAND} -E copy ${PeIGS_LIB_DIR}/libpeigs.a ${CHEMSH_GA_LIB_DIR})

    if(${GA_RECOMPILE})
        set(GA_RECOMPILE 1)
        file(REMOVE_RECURSE ${CHEMSH_ARCH}/stamp)
    else()
        # check ARMCI
        if(EXISTS ${CHEMSH_GA_LIB_DIR}/libarmci.a)
            set(ComEx_CONFIGURE_COMMAND "")
            set(ComEx_MAKE_COMMAND      "")
            set(ComEx_INSTALL_COMMAND   "")
        else()
            if(NOT EXISTS ${ComEx_LIB_DIR}/libarmci.a)
                set(GA_RECOMPILE 1)
            endif()
        endif()
        # check PeIGS
        if(EXISTS ${CHEMSH_GA_LIB_DIR}/libpeigs.a)
            set(PeIGS_INSTALL_COMMAND "")
            if(NOT EXISTS ${PeIGS_LIB_DIR}/libpeigs.a)
                message("${BoldRed}GA_RECOMPILE${EndColour} set to 0: bug libpeigs.a not found")
            endif()
        endif()
        # check GA
        if(EXISTS ${CHEMSH_GA_LIB_DIR}/libga.a)
            set(GA_CONFIGURE_COMMAND "")
            set(GA_MAKE_COMMAND      "")
            set(GA_INSTALL_COMMAND   "")
            message("${BoldRed}GA_RECOMPILE${EndColour} set to 0: GA source code will not be recompiled")
        else()
            if(NOT EXISTS ${GA_LIB_DIR}/libga.a)
                set(GA_RECOMPILE 1)
                file(REMOVE_RECURSE ${CHEMSH_ARCH}/stamp)
                message("${BoldRed}GA_RECOMPILE${EndColour} set to 0 but libga.a not found: GA source code will still be recompiled")
            endif()
        endif()
    endif()
    
    # compile GA as an external project using its original Makefile
    #   PATCH_COMMAND   : no need to patch so far; if patching note cross-platform issues!
    #   DOWNLOAD_COMMAND: should not be set
    #   DOWNLOAD_DIR    : should not be set
    #   BINARY_DIR      : should not be set
    #   BUILD_IN_SOURCE : not the case
    #   BUILD_ALWAYS    : not needed since we use ALWAYS in ExternalProject_Add_Step instead to enforce a rebuild
    #   SOURCE_DIR      : destination of extraction
    #   INSTALL_COMMAND : make copy to keep different binaries of GA; this is in command-line mode so make sure use full absolute path!
    ExternalProject_Add(
        ${GA_NAME}
        PREFIX            ${CHEMSH_ARCH}
        STAMP_DIR         ${CHEMSH_ARCH}/stamp
        TMP_DIR           ${CHEMSH_ARCH}/tmp
        BUILD_ALWAYS      ${GA_RECOMPILE}
        SOURCE_DIR        ${GA_SRC_DIR}
        URL               file://${GA}
        CONFIGURE_COMMAND ""
        DOWNLOAD_COMMAND  ""
        BUILD_COMMAND     ${CMAKE_COMMAND} -E cmake_echo_color --switch=$(COLOR) --magenta --bold "Architecture for which '${GA_NAME}' is built: '${CHEMSH_ARCH}'"
        INSTALL_COMMAND   ""#${CMAKE_COMMAND} -E copy ${GA_SRC_DIR}/../libga.a ${CHEMSH_GA_LIB_DIR}
    )
    
    ExternalProject_Add_Step(
        ${GA_NAME} GA_CONFIGURE
        COMMENT           "Cleaning up previous binaries and configuring for '${GA_NAME}'..."
        ALWAYS            ${GA_RECOMPILE}
        DEPENDEES         configure
        COMMAND           ${GA_CONFIGURE_COMMAND}
        WORKING_DIRECTORY ${GA_SRC_DIR}
    )
        #COMMAND           ""
    
    ExternalProject_Add_Step(
        ${GA_NAME} GA_BUILD
        COMMENT           "Building static library for '${GA_NAME}'..."
        ALWAYS            ${GA_RECOMPILE}
        # always preceded by GA_CONFIGURE
        DEPENDEES         GA_CONFIGURE
        COMMAND           ${GA_MAKE_COMMAND}
        WORKING_DIRECTORY ${GA_SRC_DIR}
    )
    
    ExternalProject_Add_Step(
        ${GA_NAME} GA_INSTALL
        COMMENT           "Installing static library to ${CHEMSH_GA_LIB_DIR}"
        ALWAYS            ${GA_RECOMPILE}
        # always preceded by GA_BUILD
        DEPENDEES         GA_BUILD
        COMMAND           ${GA_INSTALL_COMMAND}
        WORKING_DIRECTORY ${GA_SRC_DIR}
    )

    ExternalProject_Add_Step(
        ${GA_NAME} ComEx_CONFIGURE
        COMMENT           "Cleaning up previous binaries and configuring for ComEx..."
        ALWAYS            ${GA_RECOMPILE}
        DEPENDEES         GA_INSTALL
        COMMAND           ${ComEx_CONFIGURE_COMMAND}
        WORKING_DIRECTORY ${ComEx_SRC_DIR}
    )
        #COMMAND           ""

    ExternalProject_Add_Step(
        ${GA_NAME} ComEx_BUILD
        COMMENT           "Building static library for ComEx..."
        ALWAYS            ${GA_RECOMPILE}
        DEPENDEES         ComEx_CONFIGURE
        COMMAND           ${ComEx_MAKE_COMMAND}
        WORKING_DIRECTORY ${ComEx_SRC_DIR}
    )

    ExternalProject_Add_Step(
        ${GA_NAME} ComEx_INSTALL
        COMMENT           "Installing ComEx static library to ${CHEMSH_GA_LIB_DIR}"
        ALWAYS            ${GA_RECOMPILE}
        DEPENDEES         ComEx_BUILD
        COMMAND           ${ComEx_INSTALL_COMMAND}
        WORKING_DIRECTORY ${ComEx_SRC_DIR}
    )

    ExternalProject_Add_Step(
        ${GA_NAME} PeIGS_INSTALL
        COMMENT           "Installing PeIGS static library to ${CHEMSH_GA_LIB_DIR}"
        ALWAYS            ${GA_RECOMPILE}
        DEPENDEES         ComEx_INSTALL
        COMMAND           ${PeIGS_INSTALL_COMMAND}
        WORKING_DIRECTORY ${CHEMSH_GA_LIB_DIR}
    )

    # make a dummy shared library
    add_library(ga SHARED ga.f90 ga_stubs.f90)
    if(${CHEMSH_ARCH} STREQUAL "intel")
        # determine 32-/64-bit MKL
        if(CHEMSH_64_BIT)
            # always compile 64-bit libga.so as MA requires 64-bit integers (see ma.c), while leave libparallel.so 64-/32-bit flexible
            set_target_properties(ga PROPERTIES COMPILE_FLAGS "-fpp -i8 -diag-disable=10441")
        else()
            set_target_properties(ga PROPERTIES COMPILE_FLAGS "-fpp -i8 -diag-disable=10441")
        endif()
    elseif(${CHEMSH_ARCH} STREQUAL "cpe-gnu")
        if(CHEMSH_64_BIT)
            set_target_properties(ga PROPERTIES COMPILE_FLAGS "-cpp -std=legacy -fdefault-integer-8")
        else()
            set_target_properties(ga PROPERTIES COMPILE_FLAGS "-cpp -std=legacy -fdefault-integer-8")
        endif()
    elseif(${CHEMSH_ARCH} STREQUAL "gnu")
        # GNU: -fno-aggressive-loop-optimizations -fdefault-integer-8
        if(CHEMSH_64_BIT)
            set_target_properties(ga PROPERTIES COMPILE_FLAGS "-cpp -fdefault-integer-8")
        else()
            set_target_properties(ga PROPERTIES COMPILE_FLAGS "-cpp -fdefault-integer-8")
        endif()
    endif()
    
    # add_dependencies to make sure target_link_libraries is performed only after ExternalProject_Add_Step is finished
    add_dependencies(ga ${GA_NAME})

    # include dir: use target_include_directories instead of include_directories
    target_include_directories(ga PUBLIC ${CHEMSH_GA_INCLUDE_DIR}
                                  PUBLIC ${CHEMSH_MA_INCLUDE_DIR}
                                  PUBLIC ${CHEMSH_PEIGS_INCLUDE_DIR})

    # YL 24/03/2021: -lm should appear after the filenames
    # YL 09/08/2021: added ${CHEMSH_ScaLAPACK_FLAGS} because it suddenly started to require linking to ScaLAPACK on SCARF
    set_target_properties(ga PROPERTIES LINK_FLAGS "-Wl,--whole-archive             \
                                                    ${CHEMSH_GA_LIB_DIR}/libga.a    \
                                                    ${CHEMSH_GA_LIB_DIR}/libarmci.a \
                                                    ${CHEMSH_GA_LIB_DIR}/libpeigs.a \
                                                    ${NWCHEM_LIB_DIR}/lib64to32.a   \
                                                    ${CHEMSH_ScaLAPACK_FLAGS}       \
                                                    -Wl,--no-whole-archive          \
                                                    -Wl,--allow-multiple-definition \
                                                    -Wl,--start-group               \
                                                    -Wl,--end-group")

else()

    if(${WITH_MPI})
        add_library(ga SHARED ga_dummies.f90)
    else()
        add_library(ga SHARED ga_serial.f90)
    endif()

endif()

