#!/bin/csh -f
#
#  5 Dec 2014 - script to "link-edit" a GAMESS executable
#
#  Invoke this job by 'lked EXE VERNO >& lked.log &'
#    EXE is normally 'gamess',
#    VERNO is the next version number, e.g. 01, 02, 03, ...
#    This produces executable "EXE.VERNO.x", e.g. gamess.01.x
#
#  Please send your issues to the GAMESS public
#  issue tracker on GitHub:
#  https://github.com/gms-bbg/gamess-issues/issues
#
unset echo
if (-e install.info) then
   source install.info
else
   echo "Please run 'config' first, to set up GAMESS compiling information" >> /dev/stderr
   exit 4
endif
#
#  Allow debug flags during linking
#
if ($?GMS_DEBUG_LINK_FLAGS == 0) then
   set GMS_DEBUG_LINK_FLAGS=''
endif
#
set EXTRA_LINK_FLAGS=''
set EXTRA_MPI_LIB_FLAGS=''
#
#  the configuration file read just above provides the next two variables,
#  and some others that are used below.
#
set TARGET=$GMS_TARGET
chdir $GMS_BUILD_DIR
#
if (!($?GMS_BG_MODEL)) set GMS_BG_MODEL=' '
#
#    ---- and now the script begins...
#
set FAILED=0
set EXE=$1
set VERNO=$2
if (null$EXE == null) set EXE=gamess
if (null$VERNO == null) set VERNO=00
date
#
echo GAMESS will be linked into the binary file $EXE.$VERNO.x.
#
#  Optional Valence Bond method, using VB2000 code --or-- XMVB code
#
if (($GMS_TARGET == ibm64) && (! -f $GMS_BUILD_DIR/object/vb2c.o)) set VB2000=false
if (($GMS_TARGET =~ hpe-cray-*) && (! -f $GMS_BUILD_DIR/object/vb2c.o)) set VB2000=false
if ((`uname` == Darwin) && (! -f $GMS_BUILD_DIR/object/vb2c.o)) set VB2000=false
if ($GMS_PHI != none) set VB2000=false
if ($GMS_OPENMP == true) set VB2000=false
#
#   Uncommenting the line below will bypass VB2000 linking if issues are encountered compiling VB2000
# if (! -f $GMS_BUILD_DIR/object/vb2c.o) set VB2000=false
#
#   Optional C++ code, running on CPUs, and possibly Nvidia/CUDA GPUs
#         see ~/gamess/libcchem/aaa.readme.1st for more information
#
if ( $GMS_LIBCCHEM == false ) then
   set CCHEM=false
else
   set CCHEM=$GMS_LIBCCHEM
   set EXE=gamess.cchem
endif
#
#   Optional Natural Bond Orbital (NBO) interface
#         see http://www.chem.wisc.edu/~nbo6
#      you must specify the full file name of your NBO library here,
#      being careful about your choice of i4 or i8 integers.
#
set NBO=false
set NBOLIB="/u1/mike/nbo6/bin/gmsnbo.i8.a"
#
#   Optional MPQC interface
#
set MPQC=false
#   Optional interface to VeraChem's VM2 package
#
#   choose compiler and math library options for each system
#   ========================================================
#   these consist of
#       LDR       - the name of the loader (usually a compiler).
#       LDOPTS    - options, such as asking for a load map.
#       LIBRARIES - extra system libraries which must be searched.
#       BLAS      - set to blas.o if not using system BLAS.
#                   set to blank if using system BLAS.
#       LAPACK    - default: set to 'dgeev.o dgesvd.o zheev.o'
#                 - set to blank to take LAPACK from a math lib
#   In principle, the 'config' step has captured enough information
#   about library locations that no user need change anything below.

# for all systems use GAMESS LAPACK (by default)
if ($GMS_MATHLIB == libflame) then
   set LAPACK=' '
else if (($GMS_MATHLIB == pgiblas) && ($CCHEM == true)) then
   set LAPACK=' '
else
   set LAPACK='dgeev.o dgesvd.o zheev.o nebpathlib.o'
endif

#
#   HPE Cray Series:
#
if ($TARGET =~ hpe-cray-*) then
   # If your load a PrgEnv-(aocc,cray,gnu,intel-nvidia) the LDR (Loader) will
   # be 'ftn'. However, we also account for situations where the user
   # what want to unload a PrgEnv and load a compiler directly. In those
   # situations we specify the LDR explicitly.
   set LDR = $GMS_FORTRAN

   if ( $?CRAY_FTN_VERSION ) then
      set GMS_HPE_CRAY_COMP = cray
   endif

   if ( $?INTEL_VERSION ) then
      set GMS_HPE_CRAY_COMP = intel
      if ( $?PE_ENV ) then
         if ( $PE_ENV != "INTEL") set LDR = ifort
      endif
   endif

   if ( $?GNU_VERSION ) then
      set GMS_HPE_CRAY_COMP = gnu
      if ( $?PE_ENV ) then
         if ( $PE_ENV != "GNU") set LDR = gfortran
      endif
   endif

   if ( $?NVIDIA_VERSION ) then
      set GMS_HPE_CRAY_COMP = nvidia
      if ( $?PE_ENV ) then
         if ( $PE_ENV != "NVIDIA") set LDR = nvfortran
      endif
   endif

   if ( $?AOCC_VERSION ) then
      set GMS_HPE_CRAY_COMP = aocc
      if ( $?PE_ENV ) then
         if ( $PE_ENV != "AOCC") set LD = flang
      endif
   endif

   # Previous Cray platforms there has been a need to also take into
   # account the CPU target. We have found that $CRAY_CPU_TARGET is
   # typically available when a PrgEnv is loaded. However, $CPU seems
   # to always be avilable.
   if  ( $?CRAY_CPU_TARGET ) then
      set GMS_HPE_CRAY_ARCH = $CRAY_CPU_TARGET
   else
      set GMS_HPE_CRAY_ARCH = $CPU
   endif

   # Set your LDR flags here.
   set LDOPTS='-default64'

   if ($GMS_OPENMP == true) set LDOPTS="$LDOPTS -lcraymp"

   # Placeholder in case we need to handle things differently based on the compiler
   switch ($GMS_HPE_CRAY_COMP)
      case gnu:
      case intel:
      case cray:
      default:
         breaksw
   endsw

   # Placeholder in case we need to handle things differently based on the CPU architecture
   switch ($GMS_HPE_CRAY_ARCH)
      case x86-rome:
      case x86_64:
      default:
         set LDOPTS = "$LDOPTS"
         breaksw
   endsw

   # Linking Cray's math library
   set MATHLIBS="-L$GMS_MATHLIB_PATH/lib -lsci_$GMS_HPE_CRAY_COMP"

   # Linking message passing libraries
   if ( $?CRAY_MPICH_VERSION ) then
      echo "Linking Cray MPICH libraries"
      set MATHLIBS="$MATHLIBS -lsci_${GMS_HPE_CRAY_COMP}_mpi"
      if ( "$CRAY_MPICH_PREFIX" =~ "$CRAY_MPICH_BASEDIR/aocc"*   ) set MPILIBS="-L$CRAY_MPICH_PREFIX/lib -lmpi"
      if ( "$CRAY_MPICH_PREFIX" =~ "$CRAY_MPICH_BASEDIR/nvidia"* ) set MPILIBS="-L$CRAY_MPICH_PREFIX/lib -lmpi"
      if ( "$CRAY_MPICH_PREFIX" =~ "$CRAY_MPICH_BASEDIR/gnu"*    ) set MPILIBS="-L$CRAY_MPICH_PREFIX/lib -lmpi"
      if ( "$CRAY_MPICH_PREFIX" =~ "$CRAY_MPICH_BASEDIR/intel"*  ) set MPILIBS="-L$CRAY_MPICH_PREFIX/lib -lmpi"
      if ( "$CRAY_MPICH_PREFIX" =~ "$CRAY_MPICH_BASEDIR/cray"*   ) set MPILIBS="-L$CRAY_MPICH_PREFIX/lib -lmpi"
   else if ( $?MPT_VERSION ) then
      echo "Linking SGI MPT libraries"
      set MPILIBS="-L$MPI_ROOT/lib -lmpi"
   else if ( $?I_MPI_ROOT ) then
      echo "Linking Intel MPI libraries"
      set MPILIBS="-L$I_MPI_ROOT/lib"
      set MPILIBS="$MPILIBS -lmpifort -lrt"
      set MPILIBS="$MPILIBS -L$I_MPI_ROOT/lib/release"
      set MPILIBS="$MPILIBS -lmpi"
   endif

   # TODO: Linking other math libraries instead of using Cray's math library

   set LIBRARIES="$MATHLIBS $MPILIBS"

   set BLAS=' '
   set VECTOR=' '
   set QUICHE='zunix.o'
endif

#
# Cray XT
#
if ($TARGET == cray-xt) then
   #         this will be set to 'ftn' by the configuration step
   set LDR=$GMS_FORTRAN

   #         two choices not included in the configuration step
   #         These must match 'comp' and 'compddi', and loaded software modules.
   #            valid choices: intel, cray, pgi
   set GMS_XT_COMP = 'pgi'
   #            valid choices: i7, barcelona, shanghai, or blank
   set GMS_XT_ARCH = ' '

   set LDOPTS='-default64'

   switch ($GMS_XT_COMP)
      case pgi:
         set LDOPTS="$LDOPTS -i8 -mcmodel=medium"
         breaksw
      case intel:
      case cray:
      default:
         breaksw
   endsw

   switch ($GMS_XT_ARCH)
      case barcelona:
         set LDOPTS = "$LDOPTS -tp barcelona-64 -Mfpmisalign"
         breaksw
      case shanghai:
         set LDOPTS = "$LDOPTS -tp shanghai-64 -Mfpmisalign"
         breaksw
      case i7:
         set LDOPTS = "$LDOPTS -tp x64"
         breaksw
      default:
         set LDOPTS = "$LDOPTS"
         breaksw
   endsw
   set LIBRARIES="-L/cray/iss/park_bench/lib -lfpmpi64-cnl"
   set LIBRARIES=" "
   set LIBRARIES="$LIBRARIES -lhugetlbfs"
   #
   # ------------------------ -lfast_mv ------------------------------
   # use -lfast_mv for faster evaluation of the exponential function.
   # -lfast_mv must come after the objects, yet before -lm or -lacml
   # performance improvement will vary.
   # not used by default, since it is so far a non-standard module.
   # ------------------------ -lfast_mv ------------------------------
   #
   set BLAS=' '
endif
#
#   Various IBM sections follow.
#   To see length of common blocks and subroutine sizes,
#     add "-Wl,-bmap:../lked.sizes" to LDOPTS.
#   The following will pick out the huge common blocks:
#          a) grep -i " cm " lked.sizes >& filtered
#          b) vi filtered
#             :%s/^/ /
#             :wq     (pushes subroutine names above column72)
#          c) strip72   (Mike's program on Si.FI)
#               input=filtered
#               output=stripped
#          d) vi stripped
#             :%s/^     //
#             :wq    (no blanks in column 1)
#          e) sort +1 -d stripped >& sorted
#          f) keep only the end of 'sorted'
#
#   To avoid use of dynamic link libraries, if xlfrte is absent,
#     add "-Wl,-bnso -Wl,-bI:/lib/syscalls.exp" to LDOPTS.
#
#   The fast math library from IBM is called ESSL:
#   1. BLAS routines in ESSL are faster than the free 32 bit lib in AIX,
#      note that -lblas works *IF AND ONLY IF* you are running 32 bit.
#   2. Power2 has a different ESSL name, -lessl2, which we don't
#      any longer try to detect since few Power2 systems remain.
#   3. If you don't have ESSL, a fallback is to remove the 32 bit
#      wrapper hack in "comp" (search on DASUM), repeat compall,
#      and set BLAS to "blas.o" and LIBRARIES to blank below.
#
if ($TARGET == ibm64) then
   if ($GMS_FORTRAN == xlf) then
      set LDR='xlf_r'
      if(`uname` == AIX) then
         set LDOPTS="-bbigtoc -q64 -Wl,-m -Wl,-bloadmap:$GMS_BUILD_DIR/lked.map"
      else
         set LDOPTS="-L$GMS_XL_PATH/xlsmp/5.1.1/lib -lxlsmp -lgfortran -g -q64 -lxlf90_r -qintsize=8"
         if ($GMS_LIBCCHEM == true) set LDOPTS="$LDOPTS -L$GMS_XL_PATH/xlC/16.1.1/lib  -libmc++"
      endif
      if ($GMS_OPENMP == true) then
         set LDOPTS="$LDOPTS -qsmp=omp"
      endif
   else if (($GMS_FORTRAN == nvfortran) || ($GMS_FORTRAN == pgfortran) || ($GMS_FORTRAN == pgf77)) then
      set LDR="$GMS_FORTRAN"
      set LDOPTS='-g77libs'
      set FORTLIBS=' '
   else
      set LDR='gfortran'
      set LDOPTS=''
   endif

   set MATHLIBS="-L$GMS_MATHLIB_PATH/lib64 -lessl6464"
   set MATHLIBS="$MATHLIBS -L$GMS_XL_PATH/xlf/16.1.1/lib/ -lxlf90_r -lxlfmath"
   set BLAS=' '
   # also adding in norm2 since XLF2008 doesn't support norm2 right now.
   if ($GMS_FORTRAN == xlf) then
      if ($GMS_OPENMP == true) then
         set BLAS="$BLAS norm2.o"
      endif
   endif

   set LIBRARIES="$MATHLIBS"
endif
#
#        remarks common to linux32 and linux64
#   Linux has become the standard Unix flavor, and thus blossomed into
#   the usual Unix confusion: multiple FORTRAN compilers, multiple
#   math libraries, and since there is no vendor, or even one main
#   distribution, the software is installed at random locations.
#   Hence, the 'config' script will have asked many questions to get the
#   compiler, math library, version numbers, and appropriate path names.
#
#   ------ Linux on 32 bit chips -----
#
if ($TARGET == linux32) then
   #
   #  settings which depend on the compiler choice:
   #
   switch ($GMS_FORTRAN)
      case gfortran:
         set LDR='gfortran'
         set LDOPTS='-m32 '
         set FORTLIBS=' '
         breaksw
      case ifort:
         set LDR='ifort'
         set LDOPTS=' '  # add '-Wl,-M' to see load map
         set FORTLIBS='-Vaxlib'
         breaksw
      case pgfortran:
      case pgf77:
         set LDR="$GMS_FORTRAN"
         set LDOPTS='-g77libs -Mlfs'
         set FORTLIBS=' '
         breaksw
      default:
         echo "Please spell your linux32 compiler correctly." >> /dev/stderr
         exit 4
   endsw
   #
   #  settings which depend on the math library: mkl, atlas, acml, none
   #
   switch ($GMS_MATHLIB)

      #       for MKL, we lack experience.  The following is the 64-bit
      #       stuff, with all 64's removed.  It might be right!
      #       For the case of "11", this has actually been verified.
      case mkl:
         set mpath=$GMS_MATHLIB_PATH
         switch ($GMS_MKL_VERNO)
            case 8:
               set MATHLIBS="-L$mpath -lmkl_lapack   -lmkl -lguide -lpthread"
               breaksw
            case 9:
               set MATHLIBS="-L$mpath -lmkl_lapack   -lmkl -lguide -lpthread"
               breaksw
            case 10:
               #  next one forces single-threaded usage only...exactly what we want.
               set MATHLIBS="-L$mpath -lmkl_intel -lmkl_sequential -lmkl_core"
               breaksw
            case 11:
            case 12:
               # next attempts a static link, whereas 10 above is a dynamic link.
               # if this fails to work, try the version 10 lines shown just above.
               set MATHLIBS="-Wl,--start-group"
               set MATHLIBS="$MATHLIBS $mpath/libmkl_intel.a "
               set MATHLIBS="$MATHLIBS $mpath/libmkl_sequential.a "
               set MATHLIBS="$MATHLIBS $mpath/libmkl_core.a "
               set MATHLIBS="$MATHLIBS -Wl,--end-group"
               breaksw
            default:
               echo "unknown version number for MKL/linux32" >> /dev/stderr
               echo "please edit 'lked' to select the right 32 bit MKL libraries." >> /dev/stderr
               exit
               breaksw
         endsw
         set BLAS=' '
         breaksw

      case atlas:
         #     If the ATLAS comes with static libraries, link to them,
         #     so that only the compile node has to have the libraries.
         #     Otherwise, attempt a link against shared object libs.
         #     See the 64 bit Atlas below for a way to fix the linker's
         #     not being able to locate -lf77blas, if that happens.
         if (-e $GMS_MATHLIB_PATH/libf77blas.a) then
            echo " case 1 $GMS_MATHLIB_PATH"
            set MATHLIBS="$GMS_MATHLIB_PATH/libf77blas.a"
            set MATHLIBS="$MATHLIBS $GMS_MATHLIB_PATH/libatlas.a"
            set MATHLIBS="$MATHLIBS $GMS_MATHLIB_PATH/libcblas.a"
            set MATHLIBS="-L$GMS_MATHLIB_PATHi/lib -lf77blas -latlas"
         else
            echo " case 2 $GMS_MATHLIB_PATH"
            set MATHLIBS="-L$GMS_MATHLIB_PATHi/lib -lf77blas -latlas"
         endif
         set BLAS=' '
         breaksw
      case atlas-so:
         set MATHLIBS="-L$GMS_MATHLIB_PATH -lf77blas -latlas"
         set BLAS=' '
         breaksw
      case acml:
         #     do a static link so that only compile node needs to install ACML
         set MATHLIBS="$GMS_MATHLIB_PATH/libacml.a"
         set BLAS=' '
         breaksw

      case openblas:
         if (-e $GMS_MATHLIB_PATH/libopenblas.a) then
            set MATHLIBS="$GMS_MATHLIB_PATH/libopenblas.a"
         else
            set MATHLIBS="-L$GMS_MATHLIB_PATH -lopenblas"
         endif
         set BLAS=' '
         breaksw
      case openblas-so:
         set MATHLIBS="-L$GMS_MATHLIB_PATH -lopenblas"
         set BLAS=' '
         breaksw

      case none:
      default:
         echo "Warning.  No math library was found, you should install one."
         echo "    MP2 calculations speed up about 2x with a math library."
         echo "CCSD(T) calculations speed up about 5x with a math library."
         set BLAS='blas.o'
         set MATHLIBS=' '
         breaksw
   endsw
   #
   set LIBRARIES="$FORTLIBS $MATHLIBS"
endif
#
# Cray XC
#
if ($TARGET == cray-xc) then
   #         this will be set to 'ftn' by the configuration step
   set LDR=$GMS_FORTRAN
   set MATHLIBS=""
   #
   # Need to read these variables from config
   set GMS_XC_COMP = 'intel'
   set GMS_IFORT_VERNO = '16'
   #
   switch ($GMS_XC_COMP)
      case intel:
         if ( $?GMS_HPC_SYSTEM_TARGET ) then
            if ( $GMS_HPC_SYSTEM_TARGET == onyx ) then
               if ($GMS_PHI == none) set LDOPTS='-i8 -xCORE-AVX2'
               if ($GMS_PHI == knl)  set LDOPTS='-i8 -xMIC-AVX512'
            endif
            #
            if ( $GMS_HPC_SYSTEM_TARGET == theta ) then
               if ($GMS_PHI == knl) set LDOPTS='-i8 -xMIC-AVX512'
            endif
         endif
         set LIBRARIES=' '
         set BLAS=' '
         if ($GMS_OPENMP == true) set LDOPTS="$LDOPTS -qopenmp"
         breaksw
      default:
         echo "Please spell your FORTRAN compiler name correctly." >> /dev/stderr
         exit 4
         breaksw
   endsw
endif
#
#   ------ Fujitsu PrimeHPC on A64FX cpus -----
#
if ($TARGET == fj-a64fx) then
   set VECTOR=' '
   set QUICHE='zunix.o'
   if (($GMS_DDI_COMM == mpi) || ($GMS_DDI_COMM == mixed)) then
      switch ($GMS_FORTRAN)
         case frt:
            set LDR='mpifrt'
            breaksw
         case frtpx:
            set LDR='mpifrtpx'
            breaksw
         default:
            echo "don't know how to use a $TARGET compiler named $GMS_FORTRAN with $GMS_DDI_COMM"
            exit 4
            breaksw
      endsw
   else
      set LDR=$GMS_FORTRAN
   endif
   if ($GMS_OPENMP == true) then
      set LDOPTS="-Kopenmp"
   else
      set LDOPTS=' '
   endif
#   if ($GMS_MSUCC == true) set LDOPTS="$LDOPTS -Kcmodel=large"
   set FORTLIBS=' '
   #
   #  settings which depend on the math library: ssl2, none
   #
   switch ($GMS_MATHLIB)
      case ssl2:
         set MATHLIBS="-SSL2"
         set BLAS=' '
         breaksw
      case none:
      default:
         echo "Warning.  No math library was found, you should install one."
         echo "    MP2 calculations speed up about 2x with a math library."
         echo "CCSD(T) calculations speed up about 5x with a math library."
         set BLAS='blas.o'
         set MATHLIBS=' '
         breaksw
   endsw
   #
   set LIBRARIES="$FORTLIBS"
#
endif
#
#   ------ Linux on 64 bit chips -----
#
if ($TARGET == linux64) then
   #
   #  settings which depend on the FORTRAN:
   #
   switch ($GMS_FORTRAN)
      case armflang:
         set LDR='armflang'
         set LDOPTS=' '
         set FORTLIBS=' '
         if ($GMS_OPENMP == true) set LDOPTS="$LDOPTS -fopenmp"
         breaksw
      case aocc:
         set LDR='clang'
         set LDOPTS=' '
         set FORTLIBS='-lgfortran -lquadmath -lm'
         if (-f $GMS_AMD_LIBM_PATH/static/libamdlibm.a) then
            set FORTLIBS="$GMS_AMD_LIBM_PATH/static/libamdlibm.a -Bsymbolic $FORTLIBS "
         endif
         if ($GMS_OPENMP == true)    set LDOPTS="$LDOPTS -fopenmp"
         breaksw
      case gfortran:
         set LDR='gfortran'
         set LDOPTS=' '
         set FORTLIBS=' '
         if ($GMS_OPENMP == true)    set LDOPTS="$LDOPTS -fopenmp"
         breaksw
      case oneapi-ifort:
      case oneapi-ifx:
      case ifort:
         if ($GMS_DDI_COMM != sockets) then
            if ($GMS_MPI_LIB == impi) then
               set LDR='mpiifort'
            else
               set LDR='ifort'
               if ($GMS_FORTRAN == oneapi-ifx) set LDR='ifx'
            endif
         else
             set LDR='ifort'
         endif
         set LDOPTS='-i8'  # -Wl,-M generates a load map
         #            this auxiliary library seems to have disappeared in ifort 10
         set FORTLIBS=' '
         if ($GMS_IFORT_VERNO < 10) set FORTLIBS='-Vaxlib'
         if ($GMS_PHI == knl) then
            set LDOPTS="$LDOPTS -xMIC-AVX512"
         else if ($GMS_PHI == knc) then
            set LDOPTS="$LDOPTS -mmic"
         endif
         if ($GMS_OPENMP == true) then
            if ($GMS_IFORT_VERNO > 13) then
               set LDOPTS="$LDOPTS -qopenmp"
            else
               set LDOPTS="$LDOPTS -openmp"
            endif
         endif
         breaksw
      case nvfortran:
         set LDR="$GMS_FORTRAN"
         set LDOPTS="-i8 "
         set FORTLIBS=' '
         breaksw
      case pgfortran:
      case pgf77:
         set LDR="$GMS_FORTRAN"
         if ( `uname` == Darwin ) then
            set LDOPTS="$GMS_LIN_FLAGS -i8               "
         else
            set LDOPTS="$GMS_LIN_FLAGS -i8 -mcmodel=medium"
         endif
         if ($GMS_L64_OPT == testing) then
            set LDOPTS="$LDOPTS $GMS_PGI_OPT $GMS_IPA_OPT"
         else
            set LDOPTS="$LDOPTS -fastsse"
         endif
         set LDOPTS="$LDOPTS "
         set FORTLIBS=' '
         breaksw
      default:
         echo "don't know how to use a linux64 compiler named $GMS_FORTRAN" >> /dev/stderr
         exit 4
         breaksw
   endsw
   #
   #  settings which depend on the math library: mkl, atlas, acml, none
   #
   switch ($GMS_MATHLIB)

      case mkl:
         set mpath=$GMS_MATHLIB_PATH
         set mklver=$GMS_MKL_VERNO
         if ($CCHEM == true) set mklver=${mklver}-so
         if ($GMS_PHI != none) set mklver=${mklver}-so
         switch ($mklver)
            case 8:
               set MATHLIBS="-L$mpath -lmkl_lapack64 -lmkl -lguide -lpthread"
               breaksw
            case 9:
               set MATHLIBS="-L$mpath -lmkl_lapack   -lmkl -lguide -lpthread"
               breaksw
               breaksw
            case 10:
            case 11:
            case 12:
            case oneapi:
               # next are a static link, in single-threaded mode (serial BLAS).
               # The choices preclude any need to
               #     a) install .so shared object libraries on every node,
               #     b) define LD_LIBRARY_PATH to find the .so libraries, or
               #     c) define MKL_NUM_THREADS=1
               # If desired, there is a single-threaded shared object link below.
               # Note, there's now a gfortran-specific compiler interface library.
               set iflib=intel
               if (($GMS_FORTRAN == gfortran) || ($GMS_FORTRAN == aocc)) set iflib=gf
               set MATHLIBS="-Wl,--start-group"
               set MATHLIBS="$MATHLIBS $mpath/libmkl_${iflib}_ilp64.a "
               set MATHLIBS="$MATHLIBS $mpath/libmkl_sequential.a "
               set MATHLIBS="$MATHLIBS $mpath/libmkl_core.a "
               set MATHLIBS="$MATHLIBS -Wl,--end-group"
               if ($GMS_OPENMP == true) set MATHLIBS="$MATHLIBS -ldl"
               if (($GMS_FORTRAN == gfortran) || ($GMS_FORTRAN == aocc)) then
                  if (`echo "$GMS_GFORTRAN_VERNO > 4.7" | /usr/bin/bc`) then
                     set MATHLIBS="$MATHLIBS -ldl"
                  endif
               endif
               breaksw
            case 10-so:
            case 11-so:
            case 12-so:
            case oneapi-so:
               #  next one chooses dynamic linking (so= shared object libs)
               #  Just hand edit GMS_MKL_VERNO in your 'install.info' to use this.
               #  run-time libs (and LD_LIBRARY_PATH) needed on every compute node!
               #  note that it does force single-threaded usage.
               set iflib=intel
               if (($GMS_FORTRAN == gfortran) || ($GMS_FORTRAN == aocc)) set iflib=gf
               if ($CCHEM == false) then
                  set MATHLIBS="-L$mpath -lmkl_${iflib}_ilp64 -lmkl_sequential -lmkl_core"
                  if ($GMS_VM2 == true ) set MATHLIBS=" $MATHLIBS -lmpi_ilp64 -lpthread -lm -ilp64"
                  if (($GMS_FORTRAN == ifort)||$GMS_FORTRAN =~ oneapi-*)) then
                     if ($GMS_PHI != none) then
                        # not necessarily right thing to do
                        #                set MATHLIBS="-mkl"
                     else if ($GMS_OPENMP == true) then
                        set MATHLIBS="-mkl"
                     endif
                  endif
                  if (($GMS_FORTRAN == gfortran) || ($GMS_FORTRAN == aocc)) then
                     if ($GMS_PHI == knc) then
                        echo "INVALID OPTION: GMS_FORTRAN=gfortran AND GMS_PHI=knc" >> /dev/stderr
                        exit 4
                     else if ($GMS_OPENMP == true) then
                        set MATHLIBS="-Wl,--no-as-needed -L$mpath -lmkl_${iflib}_ilp64 -lmkl_gnu_thread -lmkl_core -lpthread -lm -ldl"
                     endif
                  endif
               else
                  #  LIBCCHEM fails to link properly against static libraries,
                  #  so it is forced to come here.
                  #  LIBCCHEM prefers to use threading
                  #  LIBCCHEM needs to see ifort's iomp5 library for some reason.
                  switch ($GMS_FORTRAN)
                     case oneapi-ifort:
                     case ifort:
                        set MATHLIBS="-L$mpath -lmkl_${iflib}_ilp64 -lmkl_${iflib}_thread -lmkl_core -liomp5"
                        breaksw
                     case aocc:
                     case gfortran:
                        set MATHLIBS="-L$mpath -lmkl_${iflib}_ilp64 -lmkl_gnu_thread -lmkl_core"
                        breaksw
                     case nvfortran:
                     case pgfortran:
                     case pg77:
                        set MATHLIBS="-L$mpath -lmkl_${iflib}_ilp64 -lmkl_core -lmkl_gnu_thread -pgf90libs -lpgf90rtl -ldl -lpthread -lm"
                        breaksw
                     default:
                        echo "only ifort and gfortran are supported with LIBCCHEM" >> /dev/stderr
                        exit 4
                        breaksw
                  endsw
               endif
               breaksw
            default:
               echo "unknown version number for MKL/linux64" >> /dev/stderr
               echo "please edit 'lked' to select the right 64 bit MKL libraries." >> /dev/stderr
               exit
               breaksw
         endsw
         set BLAS=' '
         breaksw

      case atlas:
         #     If the ATLAS comes with static libraries, link to them,
         #     so that only the compile node has to have the libraries.
         #     Otherwise, attempt a link against shared object libs.
         #     Alas, recent RPMs for Atlas have only .so libraries.
         #
         #     Most RPM versions of Atlas come w/o proper setup to use them.
         #     For example, your 'root' account should probably do the
         #     following soft links:
         #          chdir /usr/lib64/atlas   (just 'lib' for linux32)
         #          ln -s libatlas.so.3.0        libatlas.so
         #          ln -s libblas.so.3.0         libblas.so
         #          ln -s libcblas.so.3.0        libcblas.so
         #          ln -s libf77blas.so.3.0      libf77blas.so
         #          ln -s liblapack_atlas.so.3.0 liblapack.so
         #          ln -s liblapack.so.3.0       liblapack.so
         #     It is strange the RPM's don't do these soft links for us.
         if (-e $GMS_MATHLIB_PATH/lib/libf77blas.a) then
            echo "case 1 $GMS_MATHLIB_PATH"
            set MATHLIBS="$GMS_MATHLIB_PATH/lib/libf77blas.a"
            set MATHLIBS="$MATHLIBS $GMS_MATHLIB_PATH/lib/libatlas.a"
            set MATHLIBS="$MATHLIBS $GMS_MATHLIB_PATH/lib/libcblas.a"
         else
            echo "case 2 $GMS_MATHLIB_PATH"
            set MATHLIBS="-L$GMS_MATHLIB_PATH -lf77blas -latlas"
         endif
         if ($CCHEM == true) then
            set MATHLIBS="-L$GMS_MATHLIB_PATH/lib -Wl,--start-group -lf77blas -latlas -lcblas -llapack -Wl,--end-group"
            if (($GMS_FORTRAN == nvfortran) || ($GMS_FORTRAN == pgfortran) || ($GMS_FORTRAN == pgf77)) then
               set MATHLIBS="$MATHLIBS"
            endif
         endif
         set BLAS=' '
         breaksw
      case atlas-so:
         set MATHLIBS="-L$GMS_MATHLIB_PATH/lib -lf77blas -latlas"
         if ($CCHEM == true) then
            set MATHLIBS="-L$GMS_MATHLIB_PATH/lib -Wl,--start-group -lf77blas -latlas -lcblas -llapack -Wl,--end-group"
         endif
         set BLAS=' '
         breaksw
      case acml:
         #     do a static link so that only compile node needs to install ACML
         set MATHLIBS="$GMS_MATHLIB_PATH/libacml.a"
         #     if you want to try a dynamic link, use this:
         #---set MATHLIBS="-L$GMS_MATHLIB_PATH -lacml -lacml_mv"
         set BLAS=' '
         breaksw

      case pgiblas:
         # As of PGI 19.10, pgiblas must be linked with -fopenmp.
         # This would suggest that the threaded version of OpenBLAS is being
         # used.
         set MATHLIBS=""
         if ($CCHEM == true) set MATHLIBS="${MATHLIBS} -L$GMS_MATHLIB_PATH -llapack"
         if ((-e $GMS_MATHLIB_PATH/libblas.a) || ($CCHEM == false)) then
            set MATHLIBS="${MATHLIBS} $GMS_MATHLIB_PATH/libblas.a"
         else
            set MATHLIBS="${MATHLIBS} -L$GMS_MATHLIB_PATH -lblas"
         endif

         if (($GMS_OPENMP == false) && ($GMS_FORTRAN == gfortran)) set MATHLIBS="${MATHLIBS} -fopenmp"

         set BLAS=' '
         breaksw

      case openblas:
         set MATHLIBS=""
         if ($CCHEM == false) then
            if (-e $GMS_MATHLIB_PATH/libopenblas64.a) then
               set openblaslib=$GMS_MATHLIB_PATH/libopenblas64.a
            else if (-e $GMS_MATHLIB_PATH/libopenblas.a) then
               set openblaslib=$GMS_MATHLIB_PATH/libopenblas.a
            else
               echo "The directory $GMS_MATHLIB_PATH does not contain either libopenblas64.a or libopenblas.a" >> /dev/stderr
               exit 4
            endif
            switch ($GMS_FORTRAN)
               case gfortran:
                  echo " "
                  echo "Checking whether $openblaslib uses 64-bit integers."
                  echo " "

                  set echo
                  $GMS_FORTRAN -fdefault-integer-8 -finit-integer=-9223372036854775808 -fopenmp $GMS_PATH/tools/test_dgesv.f90 $openblaslib -o $GMS_BUILD_DIR/test_dgesv
                  $GMS_BUILD_DIR/test_dgesv
                  set rc=$status
                  unset echo

                  if ($rc != 0) then
                     echo "Could not verify that $openblaslib uses 64-bit integers." >> /dev/stderr
                     exit 4
                  endif

                  rm $GMS_BUILD_DIR/test_dgesv
                  unset rc
                  breaksw
               default:
                  breaksw
            endsw
            set MATHLIBS="${MATHLIBS} $openblaslib"
            unset openblaslib
         else
            set MATHLIBS="${MATHLIBS} -L$GMS_MATHLIB_PATH -lopenblas"
         endif
         set BLAS=' '
         breaksw
      case openblas-so:
         set MATHLIBS="-L$GMS_MATHLIB_PATH -lopenblas"
         set BLAS=' '
         breaksw

      case libflame:
         #do a static link so that only compile node needs to install LibFLAME
         if (-f $GMS_AMD_BLAS_PATH/lib/libblis.a) then
            set MATHLIBS="$GMS_MATHLIB_PATH/lib/libflame.a $GMS_AMD_BLAS_PATH/lib/libblis.a"
         else
            set MATHLIBS="$GMS_MATHLIB_PATH/lib/libflame.a"
         endif
         set BLAS=''
         breaksw

      case armpl:
         # note that we link the 64-bit version!
         set LAPACK=""
         if (-e $GMS_MATHLIB_PATH/libarmpl_lp64.a) then
            set MATHLIBS="$GMS_MATHLIB_PATH/libarmpl_ilp64.a -lrt"
         else
            set MATHLIBS="-L$GMS_MATHLIB_PATH -larmpl_ilp64"
         endif
         set BLAS=' '
         breaksw

      case none:
      default:
         echo "Warning.  No math library was found, you should install one."
         echo "    MP2 calculations speed up about 2x with a math library."
         echo "CCSD(T) calculations speed up about 5x with a math library."
         set BLAS='blas.o'
         set MATHLIBS=' '
         breaksw
   endsw
   #
   set LIBRARIES="$FORTLIBS $MATHLIBS"
#
endif
#
#      MacIntosh
#
#      The incantation to link to Apple's BLAS library is rather strange.
#      The math library is Apple's veclib framework,
#      which is part of a larger framework called Accelerate.
#
#      Older sytems may need to link against the "SystemStubs library"
#      Even older systems (e.g. 10.3) may need this changed to -lcc_dynamic
#
if ($TARGET == mac64) then
   set LDR=$GMS_FORTRAN
   set MATHLIBS=""
   if ($GMS_MAC_OSX_VERNO == 11) then
      set LDOPTS=''
   else
      set LDOPTS='-m64'
   endif
   switch ($GMS_MAC_OSX_VERNO)
      #     the Accelerate framework is the new standard
      case 10.8:
      case 10.9:
      case 10.10:
      case 10.11:
      case 10.12:
      case 10.13:
      case 10.14:
      case 10.15:
      case 11.0:
      case 11.1:
      case 11.2:
      case 11:
         set BLAS=' '
         set LIBRARIES='-Wl,-framework -Wl,Accelerate'
      breaksw
   endsw
endif
#
#   ------ Windows on 64 bit chips -----
#
if ($TARGET == win64) then
   #
   #  settings which depend on the FORTRAN: pgfortran, ifort
   #
   #  WS2_32.Lib is needed for WinSock
   #
   set LIBRARIES="WS2_32.lib"
   #
   switch ($GMS_FORTRAN)
      case oneapi-ifort:
      case ifort:
         set LDR = "ifort"
         set EXTRA_LINK_FLAGS="-Qlocation,link,$VCToolsInstallDir/bin/Hostx64/x64"
         set LDOPTS="$GMS_WIN_TP -integer-size:64 -Qzero"
         if ($GMS_WIN_OPT == baseline)        set LDOPTS="$LDOPTS -Od $GMS_WIN_FLAGS"
         if ($GMS_WIN_OPT == linux)           set LDOPTS="$LDOPTS -O2 $GMS_WIN_FLAGS"
         if ($GMS_WIN_OPT == samara)          set LDOPTS="$LDOPTS -O3 $GMS_WIN_FLAGS"
         if ($GMS_WIN_OPT == fast)            set LDOPTS="$LDOPTS -fast $GMS_WIN_FLAGS"
         if ($GMS_WIN_OPT == testing)         set LDOPTS="$LDOPTS $GMS_INT_OPT $GMS_IPO_OPT $GMS_WIN_FLAGS"
         breaksw
      case pgfortran:
         set LDR = "$GMS_FORTRAN"
         set LDOPTS="$GMS_WIN_FLAGS -m64 -i8 -Bstatic"
         if ($GMS_WIN_OPT == baseline)        set LDOPTS="$LDOPTS -O0                             $GMS_WIN_TP"
         #     if ($GMS_WIN_OPT == linux)           set LDOPTS="$LDOPTS -fastsse -Mipa=fast,safe        $GMS_WIN_TP"
         if ($GMS_WIN_OPT == linux)           set LDOPTS="$LDOPTS -fastsse                        $GMS_WIN_TP"
         #     if ($GMS_WIN_OPT == samara)          set LDOPTS="$LDOPTS -O3      -Mipa=fast,safe        $GMS_WIN_TP"
         if ($GMS_WIN_OPT == samara)          set LDOPTS="$LDOPTS -O3                             $GMS_WIN_TP"
         #     if ($GMS_WIN_OPT == fast)            set LDOPTS="$LDOPTS -fastsse -Mipa=fast,inline,safe $GMS_WIN_TP"
         if ($GMS_WIN_OPT == fast)            set LDOPTS="$LDOPTS -fastsse                        $GMS_WIN_TP"
         if ($GMS_WIN_OPT == testing)         set LDOPTS="$LDOPTS $GMS_PGI_OPT $GMS_IPA_OPT       $GMS_WIN_TP"
         #
         #     For more detailed information as to which flags are being used
         #     by the compiler during the build - uncomment the line below.
         #
         #     set LDOPTS="$LDOPTS -v"
         #
         if ($GMS_OPENMP == true) then
            set LDOPTS="$LDOPTS -mp"
         endif
         breaksw
      default:
      echo "Please spell your win64 compiler correctly." >> /dev/stderr
         exit 4
         breaksw
   endsw
   #
   #  settings which depend on the math library: mkl, acml, blas, none
   #
   switch ($GMS_MATHLIB)
      case mkl:
         if ($GMS_FORTRAN == pgfortran) then
            set GMS_MATHLIB_DOSPATH=`cygpath -d $GMS_MATHLIB_PATH:q`
            set MATHLIBS="-Wl,-libpath:$GMS_MATHLIB_DOSPATH:q mkl_intel_ilp64.lib mkl_sequential.lib mkl_core.lib"
            #set MATHLIBS="-Wl,-libpath:$GMS_MATHLIB_DOSPATH:q mkl_blas95_ilp64.lib mkl_intel_ilp64.lib mkl_sequential.lib mkl_core.lib"
            set BLAS=' '
            echo "Using Intel's MKL libraries: $LIBRARIES"
         endif
         if ($GMS_FORTRAN == ifort) then
            set MATHLIBS="mkl_blas95_ilp64.lib mkl_intel_ilp64.lib mkl_sequential.lib mkl_core.lib"
            set BLAS=' '
            echo "Using Intel's MKL libraries: $LIBRARIES"
         endif
         if ($GMS_FORTRAN == oneapi-ifort) then
            set MATHLIBS="mkl_blas95_ilp64.lib mkl_intel_ilp64.lib mkl_sequential.lib mkl_core.lib"
            set BLAS=' '
            echo "Using Intel's MKL libraries: $LIBRARIES"
         endif
         breaksw
      case acml:
         if ($GMS_FORTRAN == pgfortran) then
            set MATHLIBS="-Bdynamic -Macml"
            set BLAS=' '
            echo "Using ACML blas libraries"
         endif
         if ($GMS_FORTRAN == ifort) then
            set MATHLIBS="$GMS_MATHLIB_PATH:q libacml.lib"
            set BLAS=' '
            echo "Using ACML blas libraries"
         endif
         breaksw
      case blas:
         if ($GMS_FORTRAN == pgfortran) then
            set MATHLIBS="-Bstatic -lblas"
            set BLAS=' '
#           set LAPACK='dgeev.o dgesvd.o zheev.o nebpathlib.o'
            echo "Using PGI's 64-bit blas"
         else if ($GMS_FORTRAN == ifort) then
            set MATHLIBS="$GMS_MATHLIB_PATH:q libblas.lib"
            set BLAS=' '
            echo "Using PGI's 64-bit blas"
         endif
         breaksw
      case none:
      default:
         echo "Warning.  No math library was found, you should install one."
         echo "    MP2 calculations speed up about 2x with a math library."
         echo "CCSD(T) calculations speed up about 5x with a math library."
         set MATHLIBS=' '
         set BLAS='blas.o'
         breaksw
   endsw
   #
endif
#
#   Choose the message passing libraries, MSG_LIBRARIES.
#   ====================================================
#
#      In all cases, the Distributed Data Interface has been
#      compiled to a library archive, with the name ../ddi/libddi.a,
#      where we must look first.  Depending on the machine DDI has
#      several implementations and may be looking for
#         TCP/IP sockets, SHMEM, MPI, or LAPI/MPI
#      so here we select the specific low level system libraries.
#
switch ($TARGET)
   #             TCP/IP sockets interface (most systems)
   case fj-a64fx:
   case ibm64:
   case linux32:
   case linux64:
   case mac64:
      #
      #                   set up Spectrum MPI usage on 64-bit IBM clusters.
      set MPILIBS=''
      if ($TARGET == ibm64) then
         set MPILIBS="-L$GMS_MPI_PATH/lib -L/usr/lib -L/usr/lib64"
         #set MPILIBS="$MPILIBS -lmpi_ibm_usempi -lmpi_ibm_mpifh -lmpi_ibm -lgfortran"
         #set MPILIBS="$MPILIBS -pthread -lmpiprofilesupport -lmpi_ibm_mpifh  -lmpi_ibm -libverbs"
         if ($GMS_HPC_SYSTEM_TARGET == generic) then
            set MPILIBS="$MPILIBS -lmpiprofilesupport -lmpi_mpifh  -lmpi_ibm -libverbs"
         else if (($GMS_HPC_SYSTEM_TARGET == summit) || ($GMS_HPC_SYSTEM_TARGET == hokulea)) then
            set MPILIBS="$MPILIBS -lmpiprofilesupport -lmpi_ibm_mpifh  -lmpi_ibm -libverbs"
         endif
      endif
      #                   set up MPI usage on 64-bit linux clusters.
      if (($TARGET == fj-a64fx) && (($GMS_DDI_COMM == mpi) || ($GMS_DDI_COMM == mixed))) then
         switch ($GMS_MPI_LIB)
            case fj-mpi:
#              Fujitsu MPI is based on OpenMPI so all flags are similar to linux64/openmpi case
               if ($CCHEM == true) then
                  set MPILIBS="-L$GMS_MPI_PATH/lib"
                  set MPILIBS="$MPILIBS -lmpi -lmpi_mpifh -lmpi_cxx"
               else
                  set MPILIBS="-L$GMS_MPI_PATH/lib"
                  set MPILIBS="$MPILIBS -lmpi"
               endif
               breaksw
            default:
               echo "lked script has unknown MPI software requested"
               exit 4
               breaksw
         endsw
      endif
      if (($TARGET == linux64) && (($GMS_DDI_COMM == mpi) || ($GMS_DDI_COMM == mixed))) then
         switch ($GMS_MPI_LIB)
            case impi:
               set MPILIBS=$GMS_MPI_PATH/lib64
               if ($GMS_PHI == knc) set MPILIBS="-L$GMS_MPI_PATH/mic/lib"
               if (! -d $MPILIBS) then
                  #
                  # 2019 Intel directory structure for MPI includes
                  #
                  set MPILIBS="-L$GMS_MPI_PATH/intel64/lib"
                  set MPILIBS="$MPILIBS -lmpifort -lrt"
                  set MPILIBS="$MPILIBS -L$GMS_MPI_PATH/intel64/lib/release"
                  set MPILIBS="$MPILIBS -lmpi "
               else
                  #
                  # Pre-2019 Intel directory structure for MPI includes
                  #
                  set MPILIBS="-L$GMS_MPI_PATH/lib64"
                  set MPILIBS="$MPILIBS -lmpi -lmpigf -lmpigi -lrt"
               endif
               breaksw
            case spectrum:
               set MPILIBS="-L$GMS_MPI_PATH/lib -L/usr/lib -L/usr/lib64"
               #set MPILIBS="$MPILIBS -lmpi_ibm_usempi -lmpi_ibm_mpifh -lmpi_ibm -lgfortran"
               set MPILIBS="$MPILIBS -pthread -lmpiprofilesupport -lmpi_ibm_mpifh  -lmpi_ibm -libverbs"
               breaksw
            case mpich:
            case mpich2:
               set MPILIBS="-L$GMS_MPI_PATH/lib"
               if ( `uname` == Darwin ) then
                  set MPILIBS="$MPILIBS -lmpi -lmpl -lmpich     "
               else
                  set MPILIBS="$MPILIBS -lmpi -lmpl -lmpich -lmpifort -lrt"
               endif
               breaksw
            case mvapich2:
               switch ($GMS_MVAPICH2_VERNO)
                  #
                  #                    early versions needed the user to specify three
                  #                    basic libraries from MVAPICH2, and to follow that
                  #                    up with explicit OFED libraries.
                  #                    These are a bit tricky, being both a function of the
                  #                    Infiniband type, and to at least some extent, maybe
                  #                    also the MVAPICH2 version.
                  #                    E-mail us if you have trouble with MVAPICH2 linking!
                  case 1.4:
                  case 1.5:
                  case 1.6:
                  case 1.7:
                  case 1.8:
                  case 1.9:
                     #                          several of our older clusters allow a static link
                     set MPILIBS="$GMS_MPI_PATH/lib/libmpich.a"
                     set MPILIBS="$MPILIBS $GMS_MPI_PATH/lib/libopa.a"
                     set MPILIBS="$MPILIBS $GMS_MPI_PATH/lib/libmpl.a"
                     #                          but you can try a .so link if you like:
                     #--set MPILIBS="-L$GMS_MPI_PATH/lib -lmpich -lopa -lmpl"
                     #
                     #                   QLogic Infiniband likes to look at its
                     #                   its "path scale messaging" PSM library.
                     #                   Mellanux Infiniband does not need this library,
                     #                   and it can be an error to name if non-existent.
                     #
                     if (-e /usr/lib64/libpsm_infinipath.so) then
                        set MPILIBS="$MPILIBS -L/usr/lib64 -lpsm_infinipath"
                     endif
                     #
                     #                   Append OFED's libs to the end of the MVAPICH2 list,
                     #                   one particular cluster needed to look into -libnetdisc.
                     #                   Finally, allow it to peek into the runtime/thread libs
                     #
                     set OFEDLIST="-libverbs -lrdmacm -libumad"
                     if (-e $GMS_OFED_PATH/libibnetdisc.so ) then
                        set OFEDLIST="$OFEDLIST -libnetdisc"
                     endif
                     set MPILIBS="$MPILIBS -L$GMS_OFED_PATH $OFEDLIST"
                     set MPILIBS="$MPILIBS -lrt -lpthread"
                     breaksw
                     #
                     #                       as of version 2.0, MVAPICH2 seems to have a simple
                     #                       link list, just its own two libs.  It then finds
                     #                       the requisite OFED libraries all on its own.
                     #                       On our 2.1 cluster, DDI linked to various libs:
                     #                           xml2, ibmad, rdmacm, ibumad, ibverbs, rt
                     #                       without any need to specify them here.
                  case 2.0:
                  case 2.1:
                  default:
                     set MPILIBS="-L$GMS_MPI_PATH/lib -lmpifort -lmpi"
                     set MPILIBS="$MPILIBS -lpthread"
                     breaksw
               endsw
               breaksw
            case openmpi:
               if ($CCHEM == true) then
                  set MPILIBS="-L$GMS_MPI_PATH/lib"
                  set MPILIBS="$MPILIBS -lmpi -lmpi_mpifh -lmpi_cxx"
               else
                  set MPILIBS="-L$GMS_MPI_PATH/lib"
                  set MPILIBS="$MPILIBS -lmpi"
               endif
               breaksw
            case mpt:
               set MPILIBS="-L$GMS_MPI_PATH/lib"
               set MPILIBS="$MPILIBS -lmpi"
               breaksw
            case myrinet:
               # very old information, perhaps not correct anymore.
               set MPILIBS="/usr/local/mpich-gm/lib/libmpich.a"
               set MPILIBS="$MPILIBS /usr/local/gm/lib/libgm.a"
               breaksw
            default:
               echo "comp script has unknown MPI software requested" >> /dev/stderr
               exit 4
               breaksw
         endsw
      endif
      #
      #         plain old TCP/IP will have initialized MPILIBS as empty string
      if ($GMS_DDI_COMM =~ serial*) then
         set MSG_LIBRARIES="serial.o -lpthread"
      else
         set MSG_LIBRARIES="$GMS_BUILD_DIR/ddi/libddi.a $MPILIBS -lpthread"
      endif
      #
      #         On linux (and possibly other systems) librt is needed for POSIX mem
      if (("$GMS_SHMTYPE" == posix) && \
         (($TARGET == linux32) || ($TARGET == linux64))) then
         set MSG_LIBRARIES="$MSG_LIBRARIES -lrt"
      endif
      breaksw
      #
      #               MPI interface, for entire HPE Cray EX product line
   case hpe-cray-cs:
   case hpe-cray-ex:
      set MSG_LIBRARIES="$GMS_BUILD_DIR/ddi/libddi.a"
      breaksw
      #
      #               MPI interface, for entire XT product line
   case cray-xt:
      set MSG_LIBRARIES="$GMS_BUILD_DIR/ddi/libddi.a"
      breaksw
      #
      #               MPI interface, for XC product line
   case cray-xc:
      set MSG_LIBRARIES="$GMS_BUILD_DIR/ddi/libddi.a"
      breaksw
      #
      #               Microsoft Windows systems uses MPI (MS-MPI or IMPI)
   case win64:
      #
      #     Use Microsoft MPI implementation (MS-MPI)
      #
      #     This flag only works with PGI compilers. Any other compiler will
      #     have to specify the actual location of the MS-MPI libraries.
      #
      if (($GMS_MPI_LIB == msmpi) && \
         ($GMS_FORTRAN == pgfortran)) set MPILIBS="-Mmpi=msmpi"
      if (($GMS_MPI_LIB == msmpi) && ($GMS_FORTRAN == ifort)) then
         set MPILIBS=""
         if (! $?MSMPI_LIB64) then
            echo "Set MSMPI_LIB64 to the directory containing the MS MPI library and try again" >&2
            exit 1
         endif
         set EXTRA_MPI_LIB_FLAGS="$MSMPI_LIB64/msmpi.lib"
      endif
      #
      #     Use Intel MPI implementation (IMPI)
      #
      if ($GMS_MPI_LIB == impi) then
         if ($TARGET == win64) set MPILIBS="$GMS_MPI_PATH""lib\release\impi.lib"
      endif
      #
      set MSG_LIBRARIES="$GMS_BUILD_DIR/ddi/libddi.a $MPILIBS"
      set C_LIBDDI_PATH=`cygpath -d "$GMS_BUILD_DIR/ddi/libddi.a"`
      set MSG_LIBRARIES="$C_LIBDDI_PATH $MPILIBS"
      breaksw
   #               to get here must be a target typo.
   default:
      echo The lked script does not select a correct TARGET machine type.
      echo What you typed when editing this script was $TARGET
      exit 4
      breaksw
endsw
#
#         It might at some times be convenient to run in serial, such
#         as porting to a new machine, when you might want to divide
#         the process of understanding new compiler/math libraries,
#         from understanding a new MPI.  "./comp serial" to compile
#         a file which is purely serial, then uncomment the next line.
#---set MSG_LIBRARIES=serial.o
#
#     Final setup for linking
#     =======================
#
unset echo
set STANDARD_GAMESS_OBJ1=(aldeci.o algnci.o \
                          basccn.o basecp.o basext.o basg3l.o bashuz.o bashz2.o baskar.o  \
                          basminix.o basn21.o basn31.o baspcn.o basg3x.o bassto.o  \
                          casino.o ccaux.o  ccddi.o  ccqaux.o ccquad.o ccsdt.o   \
                          ceeis.o  cepa.o   cnglob.o \
                          chgpen.o cimf.o   ciminf.o cimi.o   cimlib.o cimsub.o  \
                          cisgrd.o comp.o   cosmo.o  cosprt.o cphf.o   cpmchf.o cprohf.o  \
                          cpuhf.o  dccc.o   dcgrd.o  dcgues.o dcint2.o dclib.o  dcmp2.o   \
                          dcscf.o  dctran.o ddilib.o delocl.o demrpt.o dft.o    dftaux.o  \
                          dftbfo.o dftbgr.o dftbhs.o dftblb.o dftbpb.o dftbsk.o dftbtd.o dftbx.o \
                          dftdis.o dftfun.o dftgrd.o dftint.o  \
                          dftxca.o dftxcb.o dftxcc.o dftxcd.o dftxce.o dftxcf.o dftxcg.o  \
                          diab.o   dmulti.o drc.o     \
                          eaipcc.o ecp.o    ecpder.o ecplib.o ecppot.o  \
                          efpmodule.o \
                          efchtr.o efdrvr.o efelec.o efgrd2.o efgrda.o efgrdb.o efgrdc.o  \
                          efinp.o  efinta.o efintb.o efmo.o efmograd.o efmograd_es.o \
                          efmograd_exrep.o efmograd_disp.o efmograd_pol.o  efpaul.o efpcm.o  \
                          efpcov.o efpfmo.o eftei.o eftei_genr70.o eftei_genr03.o eftei_eric.o  \
                          eigen.o  elglib.o elgloc.o elgscf.o eomcc.o  ewald.o  excorr.o  \
                          ffield.o fmo.o    fmoafo.o fmocp.o  fmoesd.o fmogrd.o fmoh1a.o fmoh2a.o \
                          fmoh2b.o fmoh2c.o fmohss.o fmoint.o  \
                          fmoio.o  fmolib.o fmomm.o  fmopbc.o fmoprp.o )
set STANDARD_GAMESS_OBJ2=(frfmt.o  fsodci.o  \
                          g3.o     globop.o gmcpt.o  gradex.o guess.o   \
                          grd1.o   grd2a.o  grd2b.o  grd2c.o   \
                          gugdga.o gugdgb.o gugdm.o  gugdm2.o gugdrt.o gugem.o  gugsrt.o  \
                          gvb.o    hess.o   hss1a.o  hss1b.o  hss1c.o  hss2a.o  hss2b.o   \
                          hss2c.o  inputa.o inputb.o inputc.o  \
                          int1.o   int2a.o  int2b.o  int2c.o  int2d.o  int2f.o  int2g.o   \
                          int2r.o  int2s.o   \
                          iolib.o  ivocas.o  \
                          lagran.o local.o  locatd.o loccd.o  locpol.o locsvd.o  \
                          lrd.o    lut.o     \
                          modmcpdft.o mcpdft.o mcpgrd.o mcpinp.o mcpint.o \
                          mcpl10.o mcpl20.o mcpl30.o mcpl40.o  \
                          mcpl50.o mcpl60.o mcpl70.o mcpl80.o  \
                          mccas.o  mcjac.o  mcqdpt.o mcqdwt.o mcqud.o  mcscf.o  mctwo.o   \
                          mdefp.o  mexing.o mltfmo.o mm23.o   modmnfun.o   \
                          modcc.o morokm.o mnsol.o   \
                          mp2.o    mp2ddi.o mp2grd.o mp2grd-defensive.o mp2gr2.o mp2ims.o mod_sformas.o  \
                          mpcdat.o mpcdatpm6.o mpcgrd.o mpchbond.o mpcint.o mpcmol.o mpcmsc.o  \
                          mpcpcm.o mthlib.o nameio.o nebpath.o nmr.o )
set STANDARD_GAMESS_OBJ3=(optcix.o ordint.o ormas1.o ormpt2.o \
                          parley.o pcm.o    pcmcav.o pcmcv2.o pcmder.o  \
                          pcmdis.o pcmhss.o pcmief.o pcmpol.o pcmvch.o  \
                          prpamm.o prpel.o  prplib.o prppop.o  \
                          qeigen.o qfmm.o   qmfm.o   qrel.o   \
                          quanpoa.o quanpob.o quanpoc.o quanpod.o quanpoe.o quanpof.o \
                          raman.o  reorg.o  rhfuhf.o ricab.o  \
                          riint.o  rimp2.o rimp2omp.o rimp2grd.o \
                          rmd.o    rmddat.o rmdgen.o rmdwrk.o rmdrun.o \
                          roeom.o  rohfcc.o rxncrd.o ryspol.o  \
                          scflib.o scfmi.o  scrf.o   secor.o  sfdft.o  sfgrad.o  \
                          sobrt.o  soffac.o solib.o  sozeff.o  \
                          statpt.o hrmrst.o surf.o   svpchg.o svpinp.o svpleb.o  \
                          symhi.o  symorb.o symslc.o  \
                          tddft.o  tddefp.o tddfun.o tddfxc.o tddgrd.o tddint.o tddnlr.o  \
                          tddxca.o tddxcc.o \
                          tddxcd_m05.o  tddxcd_m06.o     tddxcd_m08.o \
                          tddxcd_pkzb.o tddxcd_revtpss.o tddxcd_tpss.o tddxcd_vs98.o \
                          tddxce.o tdhf.o   tdx.o    tdxio.o  tdxitr.o tdxni.o  tdxprp.o  \
                          trans.o  trfdm2.o trnstn.o trudge.o umpddi.o utddft.o utdgrd.o \
                          vibanl.o vm2_module.o vscf.o   vvos.o   zapddi.o zmatrx.o \
                          mod_nosp_basis.o \
                          mod_grid_storage.o mod_dft_partfunc.o \
                          mod_dft_molgrid.o mod_dft_fuzzycell.o \
                          mod_dft_gridint.o constants.o gausshermite.o \
                          modules_dft.o utils_strings.o modules_common.o \
                          fcidump.o )
if ($GMS_MSUCC == true) then
   set STANDARD_GAMESS_OBJ4=(ccsd3aacgreorder.o ccsd3aacgsum.o ccsd3aacgt1A00.o \
                             ccsd3aacgt1A01.o ccsd3aacgt1A10.o ccsd3aacgt1A11.o ccsd3aacgt1A.o \
                             ccsd3aacgt1B00.o ccsd3aacgt1B01.o ccsd3aacgt1B10.o ccsd3aacgt1B11.o \
                             ccsd3aacgt1B.o ccsd3aacgt2A0000.o ccsd3aacgt2A0010.o ccsd3aacgt2A0011.o \
                             ccsd3aacgt2A1000.o ccsd3aacgt2A1010.o ccsd3aacgt2A1011.o \
                             ccsd3aacgt2A1100.o ccsd3aacgt2A1110.o ccsd3aacgt2A1111.o \
                             ccsd3aacgt2A1.o ccsd3aacgt2A.o ccsd3aacgt2B0000.o ccsd3aacgt2B0001.o \
                             ccsd3aacgt2B0010.o ccsd3aacgt2B0011.o ccsd3aacgt2B0100.o \
                             ccsd3aacgt2B0101.o ccsd3aacgt2B0110.o ccsd3aacgt2B0111.o \
                             ccsd3aacgt2B1000.o ccsd3aacgt2B1001.o ccsd3aacgt2B1010.o \
                             ccsd3aacgt2B1011.o ccsd3aacgt2B1100.o ccsd3aacgt2B1101.o \
                             ccsd3aacgt2B1110.o ccsd3aacgt2B1111.o ccsd3aacgt2B1.o ccsd3aacgt2B.o \
                             ccsd3aacgt2C0000.o ccsd3aacgt2C0010.o ccsd3aacgt2C0011.o \
                             ccsd3aacgt2C1000.o ccsd3aacgt2C1010.o ccsd3aacgt2C1011.o \
                             ccsd3aacgt2C1100.o ccsd3aacgt2C1110.o ccsd3aacgt2C1111.o \
                             ccsd3aacgt2C1.o ccsd3aacgt2C.o ccsd3aacgt3A100100.o \
                             ccsd3aacgt3A100110.o ccsd3aacgt3A100111.o ccsd3aacgt3A110100.o \
                             ccsd3aacgt3A110110.o ccsd3aacgt3A110111.o ccsd3aacgt3A111100.o \
                             ccsd3aacgt3A111110.o ccsd3aacgt3A111111.o ccsd3aacgt3AB.o \
                             ccsd3aacgt3B001001.o ccsd3aacgt3B001100.o ccsd3aacgt3B001101.o \
                             ccsd3aacgt3B001110.o ccsd3aacgt3B001111.o ccsd3aacgt3B100001.o \
                             ccsd3aacgt3B100100.o ccsd3aacgt3B100101.o ccsd3aacgt3B100110.o \
                             ccsd3aacgt3B100111.o ccsd3aacgt3B101001.o ccsd3aacgt3B101100.o \
                             ccsd3aacgt3B101101.o ccsd3aacgt3B101110.o ccsd3aacgt3B101111.o \
                             ccsd3aacgt3B110001.o ccsd3aacgt3B110100.o ccsd3aacgt3B110101.o \
                             ccsd3aacgt3B110110.o ccsd3aacgt3B110111.o ccsd3aacgt3B111001.o \
                             ccsd3aacgt3B111100.o ccsd3aacgt3B111101.o ccsd3aacgt3B111110.o \
                             ccsd3aacgt3B111111.o ccsd3aacgt3BC.o ccsd3aacgt3C010010.o \
                             ccsd3aacgt3C010011.o ccsd3aacgt3C010100.o ccsd3aacgt3C010110.o \
                             ccsd3aacgt3C010111.o ccsd3aacgt3C011010.o ccsd3aacgt3C011011.o \
                             ccsd3aacgt3C011100.o ccsd3aacgt3C011110.o ccsd3aacgt3C011111.o \
                             ccsd3aacgt3C100010.o ccsd3aacgt3C100011.o ccsd3aacgt3C100100.o \
                             ccsd3aacgt3C100110.o ccsd3aacgt3C100111.o ccsd3aacgt3C110010.o \
                             ccsd3aacgt3C110011.o ccsd3aacgt3C110100.o ccsd3aacgt3C110110.o \
                             ccsd3aacgt3C110111.o ccsd3aacgt3C111010.o ccsd3aacgt3C111011.o \
                             ccsd3aacgt3C111100.o ccsd3aacgt3C111110.o ccsd3aacgt3C111111.o \
                             ccsd3aacgt3CD.o ccsd3aacgt3D100100.o ccsd3aacgt3D100110.o \
                             ccsd3aacgt3D100111.o ccsd3aacgt3D110100.o ccsd3aacgt3D110110.o \
                             ccsd3aacgt3D110111.o ccsd3aacgt3D111100.o ccsd3aacgt3D111110.o \
                             ccsd3aacgt3D111111.o ccsd3amain.o)
else
   set STANDARD_GAMESS_OBJ4=( stubcc3.o )
endif

if ($GMS_OPENMP == true) then
   set OPENMP_GAMESS_OBJ=(params.o mx_limits.o prec.o grd2_consts.o \
                          blkint.o ompmod_tools.o ompmod.o ompgrd2.o \
                          mod_shell_tools.o mod_gauss_hermite.o \
                          omp_int1.o \
                          mod_1e_primitives.o omp_grd1.o omp_fmo.o omp_fmogrd1.o \
                          omp_fmogrd2.o omp_pcm.o efteiomp.o \
                          riccutils.o riccints.o riccdiag.o riccrhf.o )
else
   set OPENMP_GAMESS_OBJ=(prec.o params.o mx_limits.o )
endif

if ($GMS_LIBXC == true) then
  set LIBXC=( functionals.o libxc.o mod_nameio.o )
  set LIBXC_FLAGS="$GMS_PATH/libxc_lib/lib/libxcf03.a $GMS_PATH/libxc_lib/lib/libxc.a"
else
  set LIBXC=( libxc_empty.o )
  set LIBXC_FLAGS=""
endif
if ($GMS_MDI == true) then
  set MDI=( modmdi.o )
  set MDI_FLAGS="$GMS_PATH/mdi_lib/lib/mdi/libmdi.a"
else
  set MDI=( modmdi_empty.o )
  set MDI_FLAGS=""
endif
#
#     unfortunately, the link command has become too long to show it
#     explicitly (full command line below will exceed 2048 bytes).
#     Show what we will be linking piecemeal,
#     taking great care that the order matches the actual LDR command.
#
echo " "
echo The name of the linker on this machine is $LDR,
echo and the linker options are \"$LDOPTS\".
echo " "
echo 'Object code list begins "gamess.o unport.o",'
echo 'followed by   BLAS object code '\"$BLAS\",
echo 'followed by LAPACK object code '\"$LAPACK\",
echo 'followed by the standard GAMESS object code list,'
echo " "
echo $STANDARD_GAMESS_OBJ1
echo $STANDARD_GAMESS_OBJ2
echo $STANDARD_GAMESS_OBJ3
echo $STANDARD_GAMESS_OBJ4
echo $OPENMP_GAMESS_OBJ
echo $LIBXC
echo $LIBXC_FLAGS
echo $MDI
echo $MDI_FLAGS
#
echo " "
echo "Choices for some optional plug-in codes are"
#
#   Tinker is turned off with qmmm.o dummy substitute in GAMESS distribution.
if ($TINKER == true) then
   set QMMMOBJ="Libtad.o Libteac.o Libtedl.o Libtemo.o Libterx.o Libtfi.o"
   set QMMMOBJ="$QMMMOBJ Libtjo.o Libtpr.o Libtsx.o Tdrive.o Tinkin.o Toys.o"
   echo "   Tinker/SIMOMM object files are $QMMMOBJ"
else
   set QMMMOBJ='qmmm.o'
   echo "   Tinker/SIMOMM code skipped, using dummy file $QMMMOBJ"
endif
#
#   VB is turned off with vbdum.o dummy substitute in GAMESS distribution.
if (($VB2000 == true) || ($XMVB == true)) then
   if ($VB2000 == true) then
      # vb2c.o is new from VB2000 3.0
      set VBOBJ='vb2000.o vb2gms.o vb2c.o mod_vb2000.o'
      echo "   Valence Bond program VB2000 object files are $VBOBJ"
   endif
   if ($XMVB == true) then
      set VBOBJ='xmvb.o'
      echo "   Valence Bond program XMVB object files are $VBOBJ"
   endif
else
   set VBOBJ='vbdum.o'
   echo "   Both VB programs skipped, using dummy file $VBOBJ"
endif
#
#   NEO is turned off with neostb.o dummy substitute in GAMESS distribution.
if ($NEO == true) then
   set NEOOBJ="neo.o   neobas.o neocas.o neoden.o neofci.o"
   set NEOOBJ="$NEOOBJ neog2a.o neog2b.o neog2c.o neogrd.o"
   set NEOOBJ="$NEOOBJ neohf.o  neohss.o neoint.o neomp2.o"
   set NEOOBJ="$NEOOBJ neonci.o neoopt.o neopos.o neoprp.o"
   set NEOOBJ="$NEOOBJ neosym.o neotrn.o neovib.o neoepc.o"
   set NEOOBJ="$NEOOBJ neogradient.o neodft.o"
   echo "   Nuclear Electron Orbital object files are $NEOOBJ"
else
   set NEOOBJ='neostb.o'
   echo "   Nuclear Electron Orbital code skipped, using dummy file $NEOOBJ"
endif
#   Writing out numpy arrays
set NPY_OBJ=''
if ($?NPY) then
   if ($NPY == true) set NPY_OBJ='npy.o endian_swap.o'
endif
#
#    CUDA (et cetera) libraries may require two edits:
#       path for CUDA to its two libraries.
#       path for HDF5 library.
#
if ($?GMS_LIBCCHEM_GPU_SUPPORT == 0) then
   set GMS_LIBCCHEM_GPU_SUPPORT=false
endif
if ($CCHEM == true) then
   echo "   Using LIBCCHEM add-in C++ code for CPUs and/or Nvidia/CUDA GPUs."
   set CCHEM_LIBS="cchem.o"

   #libcchem and rysq libraries
   #set CCHEM_LIBS="$CCHEM_LIBS -L$GMS_PATH/libcchem/lib -lcchem_gamess -lcchem -lrysq"
   set CCHEM_LIBS="$CCHEM_LIBS -L$GMS_PATH/libcchem/lib"
   if (-e $GMS_PATH/libcchem/lib/libcchem_gamess.a) then
      set CCHEM_LIBS="$CCHEM_LIBS -lcchem_gamess"
   endif
   set CCHEM_LIBS="$CCHEM_LIBS -lcchem -lrysq"
   #set CCHEM_LIBS="$CCHEM_LIBS $GMS_PATH/libcchem/lib/libcchem.a $GMS_PATH/libcchem/lib/librysq.a"

   #cuda libraries
   if ($GMS_LIBCCHEM_GPU_SUPPORT == true) then
      set CCHEM_LIBS="$CCHEM_LIBS -L$GMS_CUDA_PATH/lib64 -lcudart -lcublas -lcudadevrt"
   endif
   set CCHEM_LIBS="$CCHEM_LIBS -L$GMS_BOOST_PATH/lib"
   set CCHEM_LIBS="$CCHEM_LIBS -lboost_thread"
   #for newer boost libraries, libboost_system is needed. include it if present
   if (-e $GMS_BOOST_PATH/lib/libboost_system.a) then
      set CCHEM_LIBS="$CCHEM_LIBS -lboost_system"
   endif

   if (-d $GMS_HDF5_PATH/lib)   set HDF5_LIBPATH=$GMS_HDF5_PATH/lib
   if (-d $GMS_HDF5_PATH/lib64) set HDF5_LIBPATH=$GMS_HDF5_PATH/lib64
   if (-e $HDF5_LIBPATH/libhdf5.so) then
      set CCHEM_LIBS="$CCHEM_LIBS -L$HDF5_LIBPATH -lhdf5"
   else
      set CCHEM_LIBS="$CCHEM_LIBS $HDF5_LIBPATH/libhdf5.a"
   endif
   set CCHEM_LIBS="$CCHEM_LIBS -lz"
   set CCHEM_LIBS="$CCHEM_LIBS -lstdc++"
   if(($GMS_FORTRAN == gfortran) || ($GMS_FORTRAN == aocc)) set CCHEM_LIBS="$CCHEM_LIBS -lgomp"
   set CCHEM_LIBS="$CCHEM_LIBS -lpthread"
   if(($GMS_FORTRAN == ifort) || ($GMS_FORTRAN == pgfortran) || ($GMS_FORTRAN == pgf77)) set CCHEM_LIBS="$CCHEM_LIBS -lgfortran"
   set CCHEM_LIBS="$CCHEM_LIBS $GMS_LIBCCHEM_LIBS"
   echo "   LIBCCHEM's libraries are"
   echo "$CCHEM_LIBS"
   #           must switch to GA rather than DDI, this is echoed later on.
    set MSG_LIBRARIES="ga.o -L$GMS_GA_PATH/lib -lga -larmci $MPILIBS"
   #set MSG_LIBRARIES="ga.o -lga -larmci -lrt -L$GMS_GA_PATH/lib $MPILIBS -lblacs-openmpi -lscalapack-openmpi" #use for Big Chungus
else
   set CCHEM_LIBS="cchdmy.o"
endif
#
#   NBO is turned off with nbostb.o dummy substitute in GAMESS distribution.
if ($NBO == true) then
   set NBOOBJ=$NBOLIB
   echo "   Natural Bond Orbital (NBO) code is taken from library"
   echo "        $NBOOBJ"
else
   set NBOOBJ='nbostb.o'
   echo "   Natural Bond Orbital (NBO) code skipped, using dummy file $NBOOBJ"
endif
#
#   MPQC is turned off with mpqcst.o dummy substitute in GAMESS distribution.
if ($MPQC == true) then
   # Make sure MPQC and LIBINT are present in $GMS_BUILD_DIR
   if !(-e $GMS_BUILD_DIR/mpqc/lib/libmpqcinit.a || \
        -e $GMS_BUILD_DIR/libint-gamess/lib/libint1.a) then
      echo "   ERROR! MPQC AND/OR LIBINT COULD NOT BE FOUND IN THE GAMESS BUILD DIRECTORY: $GMS_BUILD_DIR/" >> /dev/stderr
      exit
   endif
   # prepend path to MPQC libraries
   set MPQCLIBS = "$GMS_BUILD_DIR/mpqc/src/bin/pt2r12/libpt2r12gamess.a"
   foreach LIB_NAME (libmpqcinit.a libSCmbptr12.a libSClmp2.a libSClibint2.a \
                     libSCmbpt.a libSCscf.a libSClcao.a libSCdistarray4.a \
                     libSCwfn.a libSCmmisc.a libSCintv3.a libSCbasis.a \
                     libSCoint3.a libSCmolecule.a libSCisosurf.a libSCsymmetry.a \
                     libSCrender.a libSCoptimize.a libSCscmat.a libSCgroup.a \
                     libSCmisc.a libSCstate.a libSCkeyval.a libSCclass.a \
                     libSCcontainer.a libSCref.a libSCoptions.a)
      set MPQCLIBS = "$MPQCLIBS $GMS_BUILD_DIR/mpqc/lib/$LIB_NAME"
   end
   #LIBINT libraries
   set LIBINT = "-L$GMS_BUILD_DIR/libint/lib -lint2 -lm -ldl"
   #LIBRARIES
   switch ($GMS_MATHLIB)
      case mkl:
         set LIBRARIES = "-Wl,--start-group $LIBRARIES -Wl,--end-group -lstdc++"
         breaksw
      case acml:
         set LIBRARIES="$GMS_MATHLIB_PATH/libacml.a -lm -lstdc++"
         breaksw
      case atlas-so:
      case atlas:
         set LIBRARIES = "$LIBRARIES  -llapack -lstdc++"
         breaksw
      case none:
         echo 'system math library is required' >> /dev/stderr
         exit 4
         breaksw
   endsw
   # The system math libraries must contain LAPACK: remove GAMESS LAPACK
   set LAPACK=''
   set MPQCOBJ=''
   echo "   MPQC libraries are $MPQCLIBS"
   echo "   LIBINT2 libraries are $LIBINT"
else
   set MPQCOBJ='mpqcst.o'
   set MPQCLIBS=''
   set LIBINT=''
   echo "   MPQC code skipped, using dummy file $MPQCOBJ"
endif
#
#   VM2 is turned off with vm2_stub.o dummy substitute in GAMESS distribution.
if ($GMS_VM2 == true) then
   set VM2OBJ=$VM2LIB
   echo "   VeraChem's VM2 package is taken from library"
   echo "        $VM2OBJ"
else
   set VM2OBJ='vm2_stub.o'
   echo "   VM2 code skipped, using dummy file $VM2OBJ"
endif
#
echo " "
echo "   The message passing libraries searched are"
echo $MSG_LIBRARIES
echo " "
echo "   Other libraries, including math libraries, to be searched are"
echo $LIBRARIES
echo " "
#
#    Optional user specified additional libraries and/or object files:
#
if ($?GMS_EXTRA_OBJS) then
   echo "   User specified libraries and/or object files:"
   echo "$GMS_EXTRA_OBJS"
else
   set GMS_EXTRA_OBJS=''
endif
#
#   link GAMESS, using objects/library information as collected above
#   =================================================================
#   Insert a "set echo" line to see the full link command in the log file,
#   which may result in too long a line to display for some vi/c-shells.
#   Hence it has been printed out piecemeal, but in order, just above.
#
echo "Linker messages (if any) follow..."
echo "$LAPACK"
if ($GMS_TARGET == win64) then
   if ($GMS_FORTRAN == ifort || $GMS_FORTRAN == oneapi-ifort || $GMS_FORTRAN == oneapi-ifx) then
      set GMS_BUILD_DIR=`cygpath -d "$GMS_BUILD_DIR"`
   endif
endif
chdir $GMS_BUILD_DIR/object
#
set echo
$LDR  $EXTRA_LINK_FLAGS:q -o $GMS_BUILD_DIR/$EXE.$VERNO.x $LDOPTS \
      $GMS_DEBUG_LINK_FLAGS \
      -I$GMS_BUILD_DIR/object \
      gamess.o unport.o util.o $BLAS $LAPACK \
      $STANDARD_GAMESS_OBJ1 \
      $STANDARD_GAMESS_OBJ2 \
      $STANDARD_GAMESS_OBJ3 \
      $STANDARD_GAMESS_OBJ4 \
      $LIBXC \
      $MDI \
      $QMMMOBJ $VBOBJ $NEOOBJ $NBOOBJ \
      $NPY_OBJ $VM2OBJ \
      $GMS_EXTRA_OBJS \
      $CCHEM_LIBS \
      $OPENMP_GAMESS_OBJ \
      $MPQCOBJ $MPQCLIBS $LIBINT \
      $LIBRARIES $MATHLIBS $MSG_LIBRARIES \
      $LIBXC_FLAGS \
      $MDI_FLAGS
#
set rc=$status
#
#   give the installer a simple thumbs up or down on success
#
unset echo
chdir ..
if ((-x $EXE.$VERNO.x) && ($rc == 0)) then
   echo " "
   echo The linking of GAMESS to binary $EXE.$VERNO.x was successful.
   set rc=0
else
   echo " "
   echo Unfortunately, there was an error while linking GAMESS.
   set rc=1
endif
time
exit $rc
