#!/bin/csh
#
#  13 Mar 06 - 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
#
#  ------ The next two lines require your attention.
#  1. Choose your machine, the legal values for TARGET are
#       amd64,axp64,compaq-sc,cray-pvp,cray-t3e,cray-x1,cray-xd1,cray-xt3,
#       fuji-pp32,fuji-pp64,hpux32,hpux64,ibm32,ibm64,ibm64-sp,ibm-bg,
#       linux-ia64,linux-pc,mac32,macG5,necsx,sgi32,sgi64,sun32,sun64
#  2. Choose your GAMESS directory tree.
#
set TARGET=ibm64
chdir /u1/mike/gamess
#
#    ---- and now the script begins...
#
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 executable image $EXE.$VERNO.x.
#
if (-e $EXE.$VERNO.x) then
   echo An old executable $EXE.$VERNO.x already exists!
   echo Bombing out...
   exit 4
endif
#
#   Choose the message passing code, and possible system libraries.
#
set MSG_LIBRARIES=' '
#
switch ($TARGET)
#             TCP/IP sockets interface (most systems)
#      this covers both the new TCP/IP + SystemV memory DDI,
#          or the original old TCP/IP DDI implementation
   case amd64:
   case axp64:
   case cray-pvp:
   case cray-xd1:
   case fuji-pp32:
   case fuji-pp64:
   case hpux32:
   case hpux64:
   case ibm32:
   case ibm64:
   case linux-ia64:
   case linux-pc:
   case mac32:
   case macG5:
   case sgi32:
   case sun32:
   case sun64:
      set MSG_LIBRARIES='../ddi/libddi.a -lpthread'
      if (($TARGET == sun32)     || ($TARGET == sun64) || \
          ($TARGET == fuji-pp32) || ($TARGET == fuji-pp64)) then
        set MSG_LIBRARIES="$MSG_LIBRARIES -lsocket -lnsl"
      endif
      breaksw
#               SHMEM interface
   case compaq-sc:
   case cray-t3e:
   case cray-x1:
      set MSG_LIBRARIES='../ddi/libddi.a'
      breaksw
   case sgi64:
      set MSG_LIBRARIES='../ddi/libddi.a -lsma -lmpi'  
      breaksw
#               LAPI interface
   case ibm64-sp:
      set MSG_LIBRARIES='../ddi/libddi.a -llapi_r -lpthread'
      breaksw
#               MPI interface, using DDI version 1
   case ibm-bg:
      set MSG_LIBRARIES="../ddi/libddi.a"
      set MSG_LIBRARIES="$MSG_LIBRARIES -L/bgl/BlueLight/ppcfloor/bglsys/lib"
      set MSG_LIBRARIES="$MSG_LIBRARIES -lmpich.rts -lmsglayer.rts"
      set MSG_LIBRARIES="$MSG_LIBRARIES -lrts.rts -ldevices.rts"
      breaksw
   case cray-xt3:
      set MSG_LIBRARIES='../ddi/libddi.a'
      breaksw
#               unknown...
   case necsx:
      set MSG_LIBRARIES='undef'
      echo Please fix lked for this machine $TARGET
      exit 5
      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
#
#   optional SIMOMM method using Tinker MM code
#   qmmm.o is dummy substitute from the standard GAMESS distribution.
set TINKER=false
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"
else
   set QMMMOBJ='qmmm.o'
endif
#
#   optional Valence Bond method using VB2000 code
#   vbdum.o is dummy substitute from the standard GAMESS distribution.
set VB2000=false
if ($VB2000 == true) then
   set VBOBJ='vb2000.o vb2gms.o'
else
   set VBOBJ='vbdum.o'
endif
#
#   optional Nuclear-Electron Orbital method
set NEOOBJ='neostb.o'
#
#   Define the options for the system loader 'ld'.  These are
#       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.
#       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.
#
#     --- start of machine specific options ---
#
#   AMD 64 bit chips (e.g. the Opteron, and others)
#   Since this version uses a commercially licensed compiler, which is
#   likely to be installed on only one machine, we do a static link to
#   get a binary that will run on all our other machines.  The ACML
#   library for BLAS is presumed to exist, in a path name such as
#          /usr/pgi/linux86-64/5.2/lib/libacml.a
#
if ($TARGET == amd64) then
   set LDR = 'pgf77'
   set LDOPTS='-Bstatic -i8 -i8storage'
   set LDOPTS="$LDOPTS -fastsse -Mipa=fast,safe -Mprof=func"
   set LIBRARIES='-lacml'
   set BLAS=' '
   set VECTOR=' '
   set QUICHE='zunix.o'
endif
#
#   Use the Digital/Compaq eXtended Math Library for BLAS if installed.
#
if ($TARGET == axp64) then
   set LDR='f77'
   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 == compaq-sc) then
   set LDR='f77'
   set LDOPTS=' '   # add -non_shared to see libraries
   set LIBRARIES='-lcxml -lmpi -lshmem -lelan'
   set BLAS=' '
   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
#
#         The ACML's BLAS library is preferred
if (($TARGET == cray-xd1) || ($TARGET == cray-xt3)) then 
   if ($TARGET == cray-xd1) set LDR='mpif90'
   if ($TARGET == cray-xt3) set LDR='ftn'
   set LDOPTS='-tp amd64 -i8 -fast -Mnosecond_underscore'
   set LIBRARIES=' '
   set BLAS=' '
   if (-e $ACML_DIR/lib/libacml.a) then
      set LIBRARIES="$ACML_DIR/lib/libacml.a"
   else if (-e $PGI/linux86-64/6.0/lib/libblas.a) then
      set LIBRARIES="$PGI/linux86-64/6.0/lib/libblas.a"
   else if (-e $PGI/linux86-64/5.2/lib/libblas.a) then
      set LIBRARIES="$PGI/linux86-64/5.2/lib/libblas.a"
   else
      set BLAS='blas.o'
      echo " "
      echo Warning, taking BLAS from a really stupid place...
      echo " "
   endif
   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:../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='-q64 -Wl,-m -Wl,-bloadmap:../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='-q64'
   set LIBRARIES='-lessl'
   set BLAS='blaswrap.o'
   set VECTOR=' '
   set QUICHE='zunix.o'
endif
#
if ($TARGET == ibm-bg) then
   set LDR='/opt/ibmcmp/xlf/9.1/bin/blrts_xlf'
   set LDOPTS=' '
   set LIBRARIES=' '
   set BLAS='blas.o'
   set VECTOR=' '
   set QUICHE='zunix.o'
endif
#
#  Some hand edits will be necessary for Linux,
#  depending on which compiler you used in the 'comp' script:
#              -g77-       -f2c-         -ifort-    -pgf77-
#  LDR        'g77'       'gcc'         'ifort'    'pgf77'
#  LDOPTS     ' '         ' '           ' '        '-g77libs -Mlfs'
#  LIBRARIES  ' '         '-lf2c -lm'   '-Vaxlib'  ' '
#  QUICHE     'zunix.o'   'zunix.o'     'zunix.o'  'zunix.o'
#
#  If you are using g77 2.xx there is a serious I/O problem, and you
#  should download this subroutine from our web site, to fix the error:
#     www.msg.ameslab.gov/GAMESS/dist.menu.html
#  Compile endfile.c, and then set QUICHE to 'zunix.o endfile.o'.
#  The command 'rpm -qa | grep g77' will tell you your compiler version
#  number.  RedHat 8 and 9 and Fedora Core are based on 3.x compilers.
#
#  This will attempt to use the precompiled BLAS in Fedora Core, or
#  any other library you might have (e.g. ORNL's free ASCI download).
#  We found ASCI to be faster than the Fedora BLAS, both way better
#  than the FORTRAN included with GAMESS.  For a MP2 calculation:
#        FORTRAN=2178,  Fedora=1693,  ASCI=1440 CPU seconds
#  You may wish to select a different BLAS library name, both here
#  and in the "comp" script.  Be sure to recompile mthlib with the
#  correct library name in the "comp" script for maximum benefit.
#  See IRON.DOC for the suitable URL to download the ASCI library.
#
#  If you get unresolved external references for subroutines with
#  names such as DDI_XXX, this means you overlooked the note in 'comp'
#  about matching the number of trailing underscores.
#
if ($TARGET == linux-pc) then
   set LDR='g77'
   set LDOPTS=' '  # add '-Wl,-M' to see load map
   if (-e /usr/lib/libblas.a) then
      set LIBRARIES='-lblas'
      set BLAS=' '
   else
      set LIBRARIES=' '
      set BLAS='blas.o'
   endif
   if (-e /usr/local/bin/libblas-asci.a) then
      set LIBRARIES='/usr/local/bin/libblas-asci.a'
      set BLAS=' '
   endif
   if (-e /usr/local/lib/libatlas.a) then
      set LIBRARIES="/usr/local/lib/libf77blas.a"
      set LIBRARIES="$LIBRARIES /usr/local/lib/libatlas.a"
      set BLAS=' '
   endif
   set VECTOR=' '
   set QUICHE='zunix.o'
endif
#
if ($TARGET == linux-ia64) then
   set LDR='ifort'
   set LDOPTS='-i8'  # -Wl,-M generates a load map
   set VECTOR=' '
   set QUICHE='zunix.o'
#      start by assuming there is no BLAS library on the system
   set LIBRARIES='-Vaxlib'
   set BLAS='blas.o'
#              Use a BLAS library, if one seems to exist.
#      next part is for Intel's Math Kernel Library, which unfortunately
#      contain version numbers as part of the path names.  Hard to detect!
   if (-e /opt/intel) then
      if(qqq`cd /opt/intel ; ls | grep mkl` =~ qqqmkl* ) then
         setenv MKLPATH `ls -d /opt/intel/mkl*`/lib/64
         set LIBRARIES="-L$MKLPATH -lmkl_lapack64 -lmkl -lguide -Vaxlib"
         set BLAS=' '
      endif
   endif
#      next part is for the NEC platform:
   if (-e /opt/MathKeisan/lib/libblas.a) then
      set LIBRARIES='/opt/MathKeisan/lib/libblas.a -lguide -Vaxlib'
      set BLAS=' '
   endif
#      next part is for the SGI SCSL library, path name contains ver.nos.
   if (-e /opt/sgi) then
      if(qqq`cd /opt/sgi ; ls | grep scsl` =~ qqqscsl* ) then
         set LIBRARIES='-llibscs_i8 -lguide -Vaxlib'
         set BLAS=' '
      endif
   endif
endif
#
#      MacIntosh, clauses for using g77 or xlf.
#      If Atlas was downloaded, it may be a little faster than Apple's.
#
if ($TARGET == mac32) then
   set LDR='g77'
   set LDOPTS='-Wl,-multiply_defined -Wl,suppress' # add '-Wl,-M' for load map
   if (-e /sw/lib/libatlas.a) then
      set LIBRARIES='-L/sw/lib -lf77blas -latlas'  # Atlas
   else
      set LIBRARIES='-Wl,-framework -Wl,vecLib'    # Apple's BLAS library
   endif
   set BLAS=' '
   set VECTOR=' '
   set QUICHE='zunix.o'
endif
#
#      Since xlf's license is expensive, it and its associated run-time
#      libraries are probably only installed on one (1) machine.  So here
#      we do a static link to produce a binary for any other G5 system.
#      The static link consists of naming .a libraries, instead of .dylib
#
if ($TARGET == macG5) then
   set LDR='gcc'
   set LDOPTS='-multiply_defined suppress'  # add '-Wl,-M' for a load map
   if (-e /sw/lib/libatlas.a) then
      set LIBRARIES='-L/sw/lib -lf77blas -latlas'  # Atlas
   else
      set LIBRARIES='-framework vecLib'    # Apple's BLAS library
   endif
   set LIBRARIES="$LIBRARIES -L/opt/ibmcmp/xlf/8.1/lib -lxlf90 -lxlfmath -lxl"
   set BLAS=' '
   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
#
# 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='f77'
   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 LDR='f90'
#                                   add -m to LDOPTS to see a load map
   if ($TARGET == sun32) then
      if(`uname -p` == sparc)  set LDOPTS='-fast -xarch=v8plus'
      if(`uname -p` == i386)   set LDOPTS='-fast -xarch=pentium_pro'
   endif
   if ($TARGET == sun64) then
      if(`uname -p` == sparc)  set LDOPTS='-fast -xarch=v9'
      if(`uname -p` == i386)   set LDOPTS='-fast -xarch=amd64'
   endif
   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'
   endif
   set VECTOR=' '
   set QUICHE='zunix.o'
endif
#
#     --- end of machine specific options ---
#
set echo
chdir object
$LDR  -o ../$EXE.$VERNO.x   $LDOPTS    \
      gamess.o unport.o $BLAS    $VECTOR  $QUICHE \
      aldeci.o algnci.o basccn.o basecp.o basext.o bashuz.o bashz2.o  \
      basn21.o basn31.o baspcn.o bassto.o  \
      ccaux.o  ccqaux.o ccquad.o ccsdt.o   \
      chgpen.o cisgrd.o cosmo.o            \
      cphf.o   cpmchf.o cprohf.o ddilib.o delocl.o demrpt.o  \
      dft.o    dftaux.o dftexc.o dftfun.o dftgrd.o dftint.o  \
      dgeev.o  dgesvd.o dmulti.o drc.o     \
      ecp.o    ecpder.o ecplib.o ecppot.o  \
      efchtr.o efdrvr.o efgrda.o efgrdb.o efgrdc.o efgrd2.o efinp.o   \
      efinta.o efintb.o efelec.o efpaul.o efpcm.o  efpcov.o efpfmo.o  \
      eigen.o  eomcc.o  ffield.o  \
      fmo.o    fmogrd.o fmoint.o fmoio.o  fmolib.o fmoprp.o  \
      frfmt.o  fsodci.o  \
      globop.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  hss2a.o  hss2b.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  int2t.o  int2u.o  int2v.o  int2w.o  int2x.o   \
      iolib.o  lagran.o local.o  loccd.o  locpol.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 mm23.o   mp2.o    mp2ddi.o mp2grd.o  \
      mpcdat.o mpcgrd.o mpcint.o mpcmol.o mpcmsc.o  \
      morokm.o mthlib.o nameio.o nmr.o     \
      ordint.o ormas1.o parley.o pcm.o    pcmcav.o pcmcv2.o pcmder.o  \
      pcmdis.o pcmief.o pcmpol.o pcmvch.o prpel.o  prplib.o prppop.o  \
      qeigen.o qfmm.o   qmfm.o   qrel.o    \
      raman.o  rhfuhf.o rxncrd.o ryspol.o  \
      scflib.o scfmi.o  scrf.o   sobrt.o  soffac.o solib.o  sozeff.o  \
      statpt.o surf.o   svpchg.o svpinp.o svpleb.o symorb.o symslc.o  \
      tdhf.o   tdx.o    tdxio.o  tdxitr.o tdxni.o  tdxprp.o  \
      trans.o  trfdm2.o trnstn.o trudge.o  \
      umpddi.o vibanl.o vscf.o   zapddi.o zheev.o  zmatrx.o  \
      $QMMMOBJ $VBOBJ $NEOOBJ \
      $MSG_LIBRARIES $LIBRARIES
#
unset echo
time
exit
