#!/bin/csh
#
#  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
#
if (-e install.info) then
   source install.info
else
   echo "Please run 'config' first, to set up GAMESS compiling information"
   exit 4
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.
#
#if (-e $EXE.$VERNO.x) then
   #echo An old executable $EXE.$VERNO.x already exists!
   #echo Bombing out...
   #exit 4
#endif
#
#   By hand editing, the user can select some "plug-in" optional codes
#   ==================================================================
#
#   1. optional SIMOMM method, using Tinker MM code
#
set TINKER=false
#
#   2. optional Valence Bond method, using VB2000 code --or-- XMVB code.
#      you can link only one (1) VB program, please do not select both!
#      As of 2012, the source code for VB2000 is distributed with GAMESS,
#      so the assumption is that VB2000 should be included.
#
if ($GMS_PHI != none) then
  set VB2000=false
else if ($GMS_OPENMP == true) then
  set VB2000=false
else
  set VB2000=true
endif
set XMVB=false
#
#   4. optional Nuclear-Electronic Orbital method, using NEO code
#
set NEO=false
#
#   5. optional C++ code, running on CPUs, and possibly Nvidia/CUDA GPUs
#         see ~/gamess/libcchem/aaa.readme.1st for more information
#
if (! $?GMS_LIBCCHEM ) then
   set CCHEM=false
else
   set CCHEM=$GMS_LIBCCHEM
endif
#
#   6. 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"
#
#   7. optional MPQC interface
#
set MPQC=false
#
#   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
#       VECTOR    - set to vector.o on a vector processor.
#                   set to blank on a scalar processor.
#       QUICHE    - set to zunix.o if using true dynamic memory,
#                   possibly add other system specific objects.
#                   set to blank otherwise.
#   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)
   set LAPACK='dgeev.o dgesvd.o zheev.o'

#   Digital/Compaq/HP 64 bit "alpha workstation"
#   Use the Digital/Compaq eXtended Math Library for BLAS if installed.
#
if ($TARGET == axp64) then
   set LDR='f90'
   if (`uname` == Linux) set LDR='fort'
   set LDOPTS=''  #  -Wl,-M' shows load map, -non_shared to see libraries
   set LIBRARIES=' '
   set BLAS='blas.o'
   if (-e /usr/lib/libdxml.a) then
      set LIBRARIES='-ldxml'
      set BLAS=' '
   endif
   if (-e /usr/lib/libcxml.a) then
      set LIBRARIES='-lcxml'
      set BLAS=' '
   endif
   set VECTOR=' '
   set QUICHE='zunix.o'
endif
#
if ($TARGET == cray-pvp) then
   set LDR='f90'
   set LDOPTS='-m 0 -v -V'
   set LIBRARIES=' '
   set BLAS=' '
   set VECTOR='vector.o'
   set QUICHE='zunix.o'
endif
#
if ($TARGET == cray-t3e) then
   set LDR='env TARGET=cray-t3e cld -Dstreams=on'
   set LDOPTS='-V -Xm'
   set LIBRARIES='-lc -lm -lu -lf -lsci -lfi -lsma -lmpi -lpmpi'
   set BLAS=' '
   set VECTOR=' '
   set QUICHE='zunix.o'
endif
#
if ($TARGET == cray-x1) then
   set LDR='ftn'
   set LDOPTS='-V -dp -sdefault64 -Ossp'
   set LIBRARIES=' '
   set BLAS=' '
   set VECTOR='vector.o'
   set QUICHE='zunix.o'
endif
#
if ($TARGET == cray-xd1) then
   set LDR='mpif90'
#    select 'amd64e' in next line if your Opteron supports SSE3 instructions
   set LDOPTS='-tp amd64 -i8 -fast -Mnosecond_underscore'
   set LIBRARIES=' '
   set BLAS=' '
#         select PGI library over ACML library.
   if (-e $PGI/linux86-64/lib/libblas.a) then
      set LIBRARIES="$PGI/linux86-64/lib/libblas.a"
      echo "Found BLAS in $LIBRARIES"
   else if (-e $ACML_DIR/lib/libacml.a) then
      set LIBRARIES="$ACML_DIR/lib/libacml.a"
      echo "Found BLAS in $LIBRARIES"
   else
      echo ' '
      echo '* * * * * * * * * * * * * * * * * * * * * * * * * * * *'
      echo '          Warning.  No math library was found.'
      echo '    You should install one for maximum performance.'
      echo '* * * * * * * * * * * * * * * * * * * * * * * * * * * *'
      echo ' '
      set BLAS='blas.o'
   endif
   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: pathscale, 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 pathscale:
         set LDOPTS="$LDOPTS -L$MPT_DIR/mpich2-pgi/lib"
         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=' '
   set VECTOR=' '
   set QUICHE='zunix.o'
endif
#
#  Fujitsu PrimePower, using SSL2 BLAS library
#
if (($TARGET == fuji-pp32) || ($TARGET == fuji-pp64)) then
   set LDR='f90'
                             set LDOPTS='-KV8PLUS'
   if ($TARGET == fuji-pp64) set LDOPTS='-KV9'
   set LIBRARIES='-SSL2'
                             set BLAS=' '
   if ($TARGET == fuji-pp64) set BLAS='blaswrap.o'
   set VECTOR=' '
   set QUICHE='zunix.o'
endif
#
if (($TARGET == hpux32) || ($TARGET == hpux64)) then
   set LDR='f90'
   if ($TARGET == hpux32) then
      set LDOPTS='+O2 +Ofastaccess'       # add -Wl,-m to see map
      set LIBRARIES='-ldld /opt/fortran90/lib/pa2.0/libblas.a +U77'
   endif
   if ($TARGET == hpux64) then
      set LDOPTS='+DD64 +O2'              # add -Wl,-m to see map
      set LIBRARIES='-ldld -lveclib8 +U77'
   endif
   set BLAS=' '
   set VECTOR=' '
   set QUICHE=' '
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. We can use -lessl on 64 bit machines only by using the 32 bit
#      wrapper hack in blaswrap.src.
#   4. 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 == ibm32) then
   set LDR='xlf'
   set LDOPTS="-Wl,-m -Wl,-bmaxdata:0x70000000 -Wl,-bloadmap:$GMS_BUILD_DIR/lked.map"
                          set LIBRARIES='-lblas'  # free 32 bit lib in AIX
   if (-e /lib/libessl.a) set LIBRARIES='-lessl'  # a bit better if exists
   set BLAS=' '
   set VECTOR=' '
   set QUICHE='zunix.o'
endif
#
if ($TARGET == ibm64) then
   set LDR='xlf'
   if(`uname` == AIX) then
      set LDOPTS="-bbigtoc -q64 -Wl,-m -Wl,-bloadmap:$GMS_BUILD_DIR/lked.map"
      set LIBRARIES='-lessl'
   else
      set LDOPTS='-q64'
      set LIBRARIES='-lessl -lxlf90_r'
   endif
   set BLAS='blaswrap.o'
   set VECTOR=' '
   set QUICHE='zunix.o'
endif
#
if ($TARGET == ibm64-sp) then
   set LDR='mpxlf_r'
   set LDOPTS='-bbigtoc -q64'
   set LIBRARIES='-lessl'
   set BLAS='blaswrap.o'
   set VECTOR=' '
   set QUICHE='zunix.o'
endif
#
#        the loader is a special wrapper around xlf,
#        that will know where Blue Gene's MPI libraries are.
if ($TARGET == ibm-bg) then
   set LDR=mpixlf77_r
   set LDOPTS=' '
   set LIBRARIES='-L/soft/libraries/essl/current/essl/5.1/lib64 -lesslbg'
   set BLAS='blaswrap.o'
   set VECTOR=' '
   set QUICHE='zunix.o'
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
   set VECTOR=' '
   set QUICHE='zunix.o'
#
#  settings which depend on the compiler choice:
#
   switch ($GMS_FORTRAN)
     case gfortran:
        set LDR='gfortran'
        set LDOPTS=' '
        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.
        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"
          echo "please edit 'lked' to select the right 32 bit MKL libraries."
          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
         set MATHLIBS="$GMS_MATHLIB_PATH/libf77blas.a"
         set MATHLIBS="$MATHLIBS $GMS_MATHLIB_PATH/libatlas.a"
      else
         set MATHLIBS="-L$GMS_MATHLIB_PATH -lf77blas -latlas"
      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"
      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
#
# Need to read these variables from config
   set GMS_XC_COMP = 'intel'
   set GMS_IFORT_VERNO = '16'
#
   switch ($GMS_XC_COMP)
   case intel:
      set LDOPTS='-i8 -xMIC-AVX512'
      set LIBRARIES=' '
      set BLAS=' '
      set VECTOR=' '
      set QUICHE='zunix.o'
      if ($GMS_OPENMP == true) set LDOPTS="$LDOPTS -qopenmp"
      breaksw
   default:
      echo "Please spell your FORTRAN compiler name correctly."
      exit 4
      breaksw
   endsw
endif
#
#   ------ Linux on 64 bit chips -----
#
if ($TARGET == linux64) then
   set VECTOR=' '
   set QUICHE='zunix.o'
#
#  settings which depend on the FORTRAN:
#
   switch ($GMS_FORTRAN)
      case gfortran:
         set LDR='gfortran'
         set LDOPTS=' '
         set FORTLIBS=' '
         if ($GMS_OPENMP == true)    set LDOPTS="$LDOPTS -fopenmp"
         breaksw
      case ifort:
         set LDR='ifort'
         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 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 -Mipa=fast,safe"
         endif
         set FORTLIBS=' '
         breaksw
      case pathf90:
         set LDR='pathf90'
         set LDOPTS=' '
         set FORTLIBS=' '
         breaksw
      default:
         echo "don't know how to use a linux64 compiler named $GMS_FORTRAN"
         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:
          # 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) 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"
          breaksw
        case 10-so:
        case 11-so:
        case 12-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) set iflib=gf
          if ($CCHEM == false) then
            set MATHLIBS="-L$mpath -lmkl_${iflib}_ilp64 -lmkl_sequential -lmkl_core"
            if ($GMS_FORTRAN == ifort) 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) then
              if ($GMS_PHI == knc) then
                echo "INVALID OPTION: GMS_FORTRAN=gfortran AND GMS_PHI=knc"
                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 ifort:
                 set MATHLIBS="-L$mpath -lmkl_${iflib}_ilp64 -lmkl_${iflib}_thread -lmkl_core -liomp5"
                 breaksw
               case gfortran:
                 set MATHLIBS="-L$mpath -lmkl_${iflib}_ilp64 -lmkl_gnu_thread -lmkl_core"
                 breaksw
               case pgfortran:
               case pg77:
                 set MATHLIBS="-L$mpath -lmkl_${iflib}_ilp64 -lmkl_core -lmkl_pgi_thread -pgf90libs -ldl -lpthread -lm"
                 breaksw
               default:
                 echo "only ifort and gfortran are supported with LIBCCHEM"
                 exit 4
                 breaksw
             endsw
          endif
          breaksw
        default:
          echo "unknown version number for MKL/linux64"
          echo "please edit 'lked' to select the right 64 bit MKL libraries."
          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/libf77blas.a) then
         set MATHLIBS="$GMS_MATHLIB_PATH/libf77blas.a"
         set MATHLIBS="$MATHLIBS $GMS_MATHLIB_PATH/libatlas.a"
      else
         set MATHLIBS="-L$GMS_MATHLIB_PATH -lf77blas -latlas"
         if ($CCHEM == true) set MATHLIBS="$MATHLIBS -llapack"
      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:
      set MATHLIBS="$GMS_MATHLIB_PATH/libblas.a"
      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
#
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 == mac32) || ($TARGET == mac64)) then
   set LDR=$GMS_FORTRAN
   switch ($TARGET)
      case mac32:
         set LDOPTS='-Wl,-multiply_defined -Wl,suppress'
         set BLAS=' '
         set LIBRARIES='-Wl,-framework -Wl,vecLib -lSystemStubs'
         breaksw
      case mac64:
         set LDOPTS='-m64'
         switch ($GMS_MAC_OSX_VERNO)
            case 10.4:
            case 10.5:
              set BLAS='blaswrap.o'
              set LIBRARIES='-Wl,-framework -Wl,vecLib -lSystemStubs'
              breaksw
            case 10.6:
              set BLAS=' '
              set LIBRARIES='-Wl,-framework -Wl,vecLib'
              breaksw
#     work around busted vecLib in Lion
            case 10.7:
              echo "Lion's vecLib seems to be broken."
              echo "Therefore, using a slow FORTRAN equivalent for BLAS."
              set BLAS='blas.o'
              set LIBRARIES=' '
              breaksw
#     the Accelerate framework is the new standard
            case 10.8:
            case 10.9:
            case 10.10:
            case 10.11:
              set BLAS=' '
              set LIBRARIES='-Wl,-framework -Wl,Accelerate'
              breaksw
         endsw
         breaksw
   endsw
   set VECTOR=' '
   set QUICHE='zunix.o'
endif
#
if ($TARGET == necsx) then
   set LDR='f90'
   set LDOPTS='-ebw -size_t64 -Wf"-v -pvctl noassume vwork=stack"'
#  If MathKeisan is installed do not use the old memory layout.
   if ({(/bin/nm /usr/lib/libblas.a | grep -c mkversion)}) then
      echo 'MathKeisan is in use'
      set LDOPTS="$LDOPTS -Pmulti"
   else
      echo 'MathKeisan is NOT in use'
      set LDOPTS="$LDOPTS -Pmulti -Wl,-h32G_memlayout"
   endif
   set LIBRARIES='/usr/lib/lib64/libblas_64.a'
   set BLAS=' '
   set VECTOR='vector.o'
   set QUICHE='zunix.o'
endif
#
#   IRIX/MIPS chips only.   In other words, only systems sold long, long ago
#   add '-Wl,-M' to LDOPTS in order to see load map
#   error 136 about padding common blocks is to be suppressed.
if (($TARGET == sgi32) || ($TARGET == sgi64)) then
   set LDR=$GMS_FORTRAN
   if ($TARGET == sgi32) set LDOPTS='-n32 -woff 136'
   if ($TARGET == sgi64) set LDOPTS=' -64 -woff 136'
   set LIBRARIES='-lblas'
                         set BLAS=' '
   if ($TARGET == sgi64) set BLAS='blaswrap.o'
   set VECTOR=' '
   set QUICHE='zunix.o'
endif
#
if (($TARGET == sun32) || ($TARGET == sun64)) then
   set VECTOR=' '
   set QUICHE='zunix.o'
#
#         linker name and link options depend on Solaris and FORTRAN release
   if ($GMS_SUN_OS_VERNO >= 7) then
      set LDR='f90'
      set xxx=$GMS_SUN_FORT_VERNO
      set f90_major=$xxx:r
      set f90_minor=$xxx:e
                                                  set newf90=false
      if (($f90_major == 8) && ($f90_minor >= 3)) set newf90=true
      if  ($f90_major >= 9)                       set newf90=true
      if ($TARGET == sun32) then
         if ($newf90 == true) then
            set LDOPTS=-m32
         else
            if(`uname -p` == sparc)  set LDOPTS='-fast -xarch=v8plus'
            if(`uname -p` == i386)   set LDOPTS='-fast -xarch=pentium_pro'
         endif
      endif
      if ($TARGET == sun64) then
         if ($newf90 == true) then
            set LDOPTS=-m64
         else
            if(`uname -p` == sparc)  set LDOPTS='-fast -xarch=v9'
            if(`uname -p` == i386)   set LDOPTS='-fast -xarch=amd64'
         endif
      endif
   else
      set LDR=f77
      set LDOPTS='-fast -xarch=v8plus'
   endif
#                                   add -m to LDOPTS to see a load map
   if (-e /opt/SUNWspro/lib/libsunperf.so) then
      set LIBRARIES='-xlic_lib=sunperf'
      set BLAS=' '
      if ($TARGET == sun64) set BLAS='blaswrap.o'
   else
      set LIBRARIES=' '
      set BLAS='blas.o'
      if ($TARGET == sun64) set BLAS='blaswrap.o blas.o'
      echo " "
      echo "You did not install the Sun Performance Library,"
      echo "which should should in your compiler suite."
      echo "We strongly recommend tthat you do install this..."
      echo " "
      echo "Proceeding without a good math library, which is slow at run time."
      echo " "
   endif
endif
#
#   ------ Windows on 32 bit chips -----
#
if ($TARGET == win32) then
#
#  settings which depend on the FORTRAN: pgfortran
#
#  WS2_32.Lib is needed for WinSock
#
   set LIBRARIES="WS2_32.lib"
#
   switch ($GMS_FORTRAN)
     case pgfortran:
        set LDR="$GMS_FORTRAN"
        set LDOPTS="$GMS_WIN_FLAGS -Bstatic"
        if ($GMS_WIN_OPT == baseline)     set LDOPTS="$LDOPTS -O0  $GMS_WIN_TP"
        if ($GMS_WIN_OPT == linux)        set LDOPTS="$LDOPTS -O2  $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 == 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"
#
        set LIBRARIES="$LIBRARIES"
        breaksw
     default:
        echo "Please spell your win32 compiler correctly."
        exit 4
   endsw
#
#  settings which depend on the math library: acml, blas, none
#
   switch ($GMS_MATHLIB)
   case mkl:
         set LIBRARIES="$LIBRARIES mkl_intel_c.lib mkl_sequential.lib mkl_core.lib"
         set BLAS=' '
         echo "Using Intel's MKL libraries: $LIBRARIES"
         breaksw
   case acml:
#     do a static link so that only compile node needs to install ACML
      set LIBRARIES="$LIBRARIES -Bdynamic -Macml"
      set BLAS=' '
      breaksw
   case blas:
      echo "Using PGI's 32-bit blas"
      set LIBRARIES="$LIBRARIES -lblas"
      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'
      breaksw
   endsw
#
   set VECTOR=' '
   set QUICHE='zunix.o'
endif
#
#   ------ Windows on 64 bit chips -----
#
if (($TARGET == win64) || ($TARGET == winazure)) 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 ifort:
      set LDR = "$GMS_FORTRAN"
      if (! $?VCINSTALLDIR) then
        echo "Set VCINSTALLDIR to the directory containing VC++ and try again" >&2
        exit 1
      endif
      set EXTRA_LINK_FLAGS="-Qlocation,link,$VCINSTALLDIR/bin"
      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 -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 == samara)          set LDOPTS="$LDOPTS -O3      -Mipa=fast,safe        $GMS_WIN_TP"
      if ($GMS_WIN_OPT == fast)            set LDOPTS="$LDOPTS -fastsse -Mipa=fast,inline,safe $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"
#
      breaksw
   default:
      echo "Please spell your win64/winazure compiler correctly."
      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 LIBRARIES="$LIBRARIES -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 LIBRARIES="$LIBRARIES 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 LIBRARIES="$LIBRARIES -Bdynamic -Macml"
        set BLAS=' '
        echo "Using ACML blas libraries"
      endif
      if ($GMS_FORTRAN == ifort) then
        set LIBRARIES="$LIBRARIES $GMS_MATHLIB_PATH:q libacml.lib"
        set BLAS=' '
        echo "Using ACML blas libraries"
      endif
      breaksw
   case blas:
      if ($GMS_FORTRAN == pgfortran) then
        set LIBRARIES="$LIBRARIES -Bstatic -lblas"
        set BLAS=' '
        echo "Using PGI's 64-bit blas"
      else if ($GMS_FORTRAN == ifort) then
        set LIBRARIES="$LIBRARIES $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 BLAS='blas.o'
      breaksw
   endsw
#
   set VECTOR=' '
   set QUICHE='zunix.o'
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 axp64:
   case cray-pvp:
   case cray-xd1:
   case fuji-pp32:
   case fuji-pp64:
   case hpux32:
   case hpux64:
   case ibm32:
   case ibm64:
   case linux32:
   case linux64:
   case mac32:
   case mac64:
   case sgi32:
   case sun32:
   case sun64:
#
#                   set up MPI usage on 64-bit linux clusters.
      set MPILIBS=''
      if ($TARGET == linux64) then
        if (($GMS_DDI_COMM == mpi) || ($GMS_DDI_COMM == mixed)) then
          switch ($GMS_MPI_LIB)
            case impi:
               set MPILIBS="-L$GMS_MPI_PATH/lib64"
               if ($GMS_PHI == knc) set MPILIBS="-L$GMS_MPI_PATH/mic/lib"
               set MPILIBS="$MPILIBS -lmpi -lmpigf -lmpigi -lrt"
               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:
               set MPILIBS="-L$GMS_MPI_PATH/lib"
               set MPILIBS="$MPILIBS -lmpi"
               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"
               exit 4
               breaksw
          endsw
        endif
      endif
#
#         plain old TCP/IP will have initialized MPILIBS as empty string
      set MSG_LIBRARIES="$GMS_BUILD_DIR/ddi/libddi.a $MPILIBS -lpthread"
#
#         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
#
#           Sun-like needs some extra TCP/IP library searches
      if (($TARGET == sun64) || \
          ($TARGET == fuji-pp32) || ($TARGET == fuji-pp64)) then
           set MSG_LIBRARIES="$MSG_LIBRARIES -lsocket -lnsl -lrt"
      endif
      if ($TARGET == sun32) then
        if ($GMS_SUN_OS_VERNO < 7) then
           set MSG_LIBRARIES="$MSG_LIBRARIES -lsocket -lnsl -lposix4"
        else
           set MSG_LIBRARIES="$MSG_LIBRARIES -lsocket -lnsl -lrt"
        endif
      endif
      breaksw
#
#               SHMEM interface, which is only partly functional at best.
   case cray-t3e:
   case cray-x1:
      set MSG_LIBRARIES="$GMS_BUILD_DIR/ddi/libddi.a"
      breaksw
   case sgi64:
      set MSG_LIBRARIES="$GMS_BUILD_DIR/ddi/libddi.a -lsma -lmpi"
      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
#
#               LAPI/MPI interface, for the SP product line
   case ibm64-sp:
      set MSG_LIBRARIES="$GMS_BUILD_DIR/ddi/libddi.a -llapi_r -lpthread"
      breaksw
#
#               IBM Blue Gene (MPI paths are known by the LDR script)
   case ibm-bg:
      set MSG_LIBRARIES="$GMS_BUILD_DIR/ddi/libddi.a"
      breaksw
#
#               correct stuff for NEC-SX is unknown...
   case necsx:
      set MSG_LIBRARIES='undefined'
      echo Please fix lked for this machine $TARGET
      exit 5
      breaksw
#
#               Microsoft Windows systems uses MPI (MS-MPI or MPICH2)
   case win32:
   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 Argonne National Laboratory's MPI implementation (MPICH2)
#
#     Assumes the mpich2 library is stored at the same directory level as the
#     GAMESS.
#
      if ($GMS_MPI_LIB == mpich2) then
        if ($TARGET == win32) set MPILIBS="C:\Progra~1\MPICH2\lib\libmpi.a"
        if ($TARGET == win64) set MPILIBS="C:\Progra~1\MPICH2\lib\mpi.lib"
      endif
#
      if (($TARGET == win64) || ($TARGET == win32)) then
        set MSG_LIBRARIES="c:/cygwin/$GMS_BUILD_DIR/ddi/libddi.a $MPILIBS"
      else
        set MSG_LIBRARIES="$GMS_BUILD_DIR/ddi/libddi.a $MPILIBS"
      endif
      breaksw
   case winazure:
      set MSG_LIBRARIES=serial.o
      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 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   \
      eigen.o  elglib.o elgloc.o elgscf.o eomcc.o  ewald.o  excorr.o  \
      ffield.o fmo.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     \
      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   morokm.o mnsol.o   \
      mp2.o    mp2ddi.o mp2grd.o mp2gr2.o mp2ims.o  \
      mpcdat.o mpcdatpm6.o mpcgrd.o mpchbond.o mpcint.o mpcmol.o mpcmsc.o  \
      mpcpcm.o mthlib.o nameio.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   quanpo.o  \
      raman.o  reorg.o  rhfuhf.o ricab.o  riint.o  rimp2.o rimp2omp.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 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 \
      tdhf.o   tdx.o    tdxio.o  tdxitr.o tdxni.o  tdxprp.o  \
      trans.o  trfdm2.o trnstn.o trudge.o umpddi.o utddft.o  \
      vibanl.o vscf.o   vvos.o   zapddi.o zmatrx.o )
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 )
else
  set OPENMP_GAMESS_OBJ=(params.o )
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 VECTOR object code '\"$VECTOR\",
echo 'followed by memory object code '\"$QUICHE\",
echo 'followed by the standard GAMESS object code list,'
echo " "
echo $STANDARD_GAMESS_OBJ1
echo $STANDARD_GAMESS_OBJ2
echo $STANDARD_GAMESS_OBJ3
echo $OPENMP_GAMESS_OBJ
#
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
      set VBOBJ='vb2000.o vb2gms.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"
   echo "   Nuclear Electron Orbital object files are $NEOOBJ"
else
   set NEOOBJ='neostb.o'
   echo "   Nuclear Electron Orbital code skipped, using dummy file $NEOOBJ"
endif
#
#    CUDA (et cetera) libraries may require two edits:
#       path for CUDA to its two libraries.
#       path for HDF5 library.
#
if ($CCHEM == true) then
   echo "   Using LIBCCHEM add-in C++ code for CPUs and/or Nvidia/CUDA GPUs."
   set CCHEM_LIBS="cchem.o"
   set CCHEM_LIBS="$CCHEM_LIBS -L$GMS_PATH/libcchem/lib -lcchem_gamess -lcchem -lrysq"
   if ($GMS_LIBCCHEM_GPU_SUPPORT == true) then
       set CCHEM_LIBS="$CCHEM_LIBS -L$GMS_CUDA_PATH/lib64 -lcudart -lcublas  -lcublas_device -lcudadevrt"
   endif
   set CCHEM_LIBS="$CCHEM_LIBS $GMS_PATH/libcchem/boost/lib/libboost_thread.a"
   if (-e $GMS_HDF5_PATH/lib/libhdf5.a)   set HDF5_LIBPATH=$GMS_HDF5_PATH/lib
   if (-e $GMS_HDF5_PATH/lib64/libhdf5.a) set HDF5_LIBPATH=$GMS_HDF5_PATH/lib64
   set CCHEM_LIBS="$CCHEM_LIBS $HDF5_LIBPATH/libhdf5.a"
   set CCHEM_LIBS="$CCHEM_LIBS -lz"
   set CCHEM_LIBS="$CCHEM_LIBS -lstdc++"
   set CCHEM_LIBS="$CCHEM_LIBS -lgomp"
   set CCHEM_LIBS="$CCHEM_LIBS -lpthread"
   if ($GMS_LIBCCHEM_LIBINT == true) then
        set CCHEM_LIBS="$CCHEM_LIBS -L$GMS_LIBINT_PATH/lib/ -lint2"
   endif
   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 zga.o $GMS_PATH/libcchem/ga/lib/libga.a $MPILIBS"
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/"
        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:
     set LIBRARIES = "$LIBRARIES  -llapack -lstdc++"
    breaksw
    case none:
     echo 'system math library is required'
     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
#
#
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..."
if (($TARGET == win32) || ($TARGET == win64) || ($TARGET == winazure)) then
  set GMS_BUILD_DIR_WIN=c:\/cygwin$GMS_BUILD_DIR
  set GMS_BUILD_DIR=$GMS_BUILD_DIR_WIN
endif
chdir $GMS_BUILD_DIR/object
#
$LDR  $EXTRA_LINK_FLAGS:q -o $GMS_BUILD_DIR/$EXE.$VERNO.x $LDOPTS \
      -I$GMS_BUILD_DIR/object \
      gamess.o unport.o $BLAS $LAPACK $VECTOR $QUICHE \
      $STANDARD_GAMESS_OBJ1 \
      $STANDARD_GAMESS_OBJ2 \
      $STANDARD_GAMESS_OBJ3 \
      $QMMMOBJ $VBOBJ $NEOOBJ $NBOOBJ \
      $GMS_EXTRA_OBJS \
      $CCHEM_LIBS \
      $OPENMP_GAMESS_OBJ \
      $MPQCOBJ $MPQCLIBS $LIBINT \
      $MSG_LIBRARIES $LIBRARIES $EXTRA_MPI_LIB_FLAGS:q
#
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
