#!/bin/csh
#
# 27 Mar 2014 - script to compile the GAMESS quantum chemistry code.
#
#     Invoke this script by './compall >& compall.log &'.
#     UNIX compilers are sometimes slow, so start this up, and go for tea.
#
#  This script no longer requires customization, as it starts off by
#  reading information about the compilation from a configuration file.
#
if (-e install.info) then
   source install.info
else if (-e ../install.info) then
   source ../install.info
else
   echo "Please run 'config' first, to set up GAMESS compiling information"
   exit 4
endif
#
#        this flag allows compall to exit after compilation of zunix
set makezunix=$1
#
#  the configuration file read just above provides the next two variables.
#
set TARGET=$GMS_TARGET
#AAD this script is run from GMS_BUILD_DIR only which may be the same
#    as GMS_PATH
#chdir $GMS_PATH
#
#  ----- and now the script begins...
#
date
#
if (!(-e object)) mkdir object
#
#  Check to see if the configuration step has built actvte.x
#
if (!(-x tools/actvte.x)) then
   echo "Something went wrong with 'config', so that the source code"
   echo "activation program was not correctly compiled.  Please repeat"
   echo "the 'config' step, so that tools/actvte.x is created."
   exit 8
endif
#
#  Compile a little bit of C to allow for runtime memory allocation.
#  1. we must select the name of the C compiler, often it is simply cc.
#  2. we must select the #define in zunix.c, and maybe other options.
#
set CCOMP=cc
if (($TARGET == axp64) && (`uname` == Linux))     set CCOMP='ccc'
if  ($TARGET == cray-xd1)                  set CCOMP='pgcc -fastsse -tp amd64'
if  ($TARGET == fuji-pp32)                        set CCOMP='fcc'
if  ($TARGET == fuji-pp64)                        set CCOMP='fcc'
if (($TARGET == ibm64) && (`uname` == AIX))       set CCOMP='xlc'
if (($TARGET == ibm64) && (`uname` == Linux))     set CCOMP='gcc'
if  ($TARGET == ibm-bg)                           set CCOMP='bgxlc_r'
if  ($TARGET == linux32)                          set CCOMP='gcc'
if  ($TARGET == linux64)                          set CCOMP='gcc'
#       it is not true that icc will exist if ifort is in use
#---if (($TARGET == linux64) && ($GMS_FORTRAN == ifort)) set CCOMP='icc'
if  ($TARGET == necsx)                            set CCOMP='c++'
#       it might not be true that icc will exist if it's a KNL/KNC node
# if  ($GMS_PHI != none)                            set CCOMP='icc'
if  ($TARGET == cray-xc)                          set CCOMP='cc'
#
unset extraflags
#
if ($TARGET == axp64)        set extraflags='-DAXP64'
if ($TARGET == cray-pvp)     set extraflags='-DCRAY'
if ($TARGET == cray-t3e)     set extraflags='-DCRAY'
if ($TARGET == cray-x1)      set extraflags='-DCRAYX1'
if ($TARGET == cray-xd1)     set extraflags='-DCRAYXD1'
if ($TARGET == cray-xt)      set extraflags='-DLINUX64'
if ($TARGET == cray-xc)      set extraflags='-DLINUX64 -m64'
if ($TARGET == fuji-pp32)    set extraflags='-DSUN32'
if ($TARGET == fuji-pp64)    set extraflags='-DSUN64 -KV9'
if ($TARGET == hpux32)       goto skip-zunix
if ($TARGET == hpux64)       goto skip-zunix
if ($TARGET == ibm32)        set extraflags='-DIBM32'
if ($TARGET == ibm64) then
   if(`uname` == AIX)        set extraflags='-DIBM64 -q64'
   if(`uname` == Linux)      set extraflags='-DIBMPPC64 -m64'
endif
if ($TARGET == ibm64-sp)     set extraflags='-DIBM64 -q64'
if ($TARGET == ibm-bg) then
   set extraflags="-DIBMBG -qarch=qp -qtune=qp -q64"
   set extraflags="$extraflags -DCLK_TCK=CLOCKS_PER_SEC"
endif
if ($TARGET == linux32)      set extraflags='-DLINUX32'
#
#    gcc's incantation for a 64 bit address space depends on the chip type!
#      Fedora uses the -p flag to name the processor.
#      SUSE   uses the -p flag to name the processor.
#      Ubuntu uses the -m flag, since its -p replies "unknown"
#      Debian uses the -m flag, since its -p replies "unknown"
#      Gentoo uses the -m flag, since its -p gives an unusable multiword reply
if ($TARGET == linux64) then
   set chip=(`uname -p`)
   if (${#chip} > 1) set chip=unknown
   if ($chip == unknown) set chip=`uname -m`
   if ($chip == x86_64) set extraflags='-DLINUX64 -m64'
   if ($chip == ia64)   set extraflags='-DLINUX64'
   if ($chip == ppc64)  set extraflags='-DLINUX64 -mpowerpc64'
   if ($chip == i386)   then
     set chip=`uname -m`
     if ($chip == x86_64) set extraflags='-DLINUX64 -m64 -I/usr/include/malloc'
   endif
   if ($GMS_PHI == knl && $CCOMP == icc )  set extraflags='-DLINUX64 -xMIC-AVX512'
   if ($GMS_PHI == knc && $CCOMP == icc )  set extraflags='-DLINUX64 -mmic'
endif
if ($TARGET == mac32)        set extraflags='-DLINUX32'
if ($TARGET == mac64)    set extraflags='-DLINUX64 -m64 -I/usr/include/malloc'
if ($TARGET == necsx)        set extraflags='-DNECSX -size_t64'
if ($TARGET == sgi32)        set extraflags='-DSGI32 -n32'
if ($TARGET == sgi64)        set extraflags='-DSGI64 -64'
if (($TARGET == sun32) || ($TARGET == sun64)) then
   if ($GMS_SUN_OS_VERNO >= 7) then
      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
   else
      set newf90=false
   endif
   if ($TARGET == sun32) then
      if ($newf90 == true) then
                                   set extraflags='-DSUN32 -m32'
      else
         if(`uname -p` == sparc)   set extraflags='-DSUN32 -xarch=v8plus'
         if(`uname -p` == i386)    set extraflags='-DSUN32 -xarch=pentium_pro'
      endif
   endif
   if ($TARGET == sun64) then
      if ($newf90 == true) then
                                   set extraflags='-DSUN64 -m64'
      else
         if(`uname -p` == sparc)   set extraflags='-DSUN64 -xarch=v9'
         if(`uname -p` == i386)    set extraflags='-DSUN64 -xarch=amd64'
      endif
   endif
endif
#
if (($TARGET == win32) || ($TARGET == win64) || ($TARGET == winazure)) then
   set EXTRA_FLAGS=''
   set MPIFLAG=''
   if ($GMS_FORTRAN == pgfortran) then
     set CCOMP='pgcc'
     set CFLAGS="-v -Bstatic -Minfo=all -Minform=severe"
     if ($GMS_WIN_OPT == baseline) set CFLAGS="$CFLAGS -O0"
     if ($GMS_WIN_OPT == linux)    set CFLAGS="$CFLAGS -O2"
     if ($GMS_WIN_OPT == fast)     set CFLAGS="$CFLAGS -fastsse"
     if ($GMS_WIN_OPT == testing)  set CFLAGS="$CFLAGS $GMS_PGI_OPT"
     if ($GMS_MPI_LIB == msmpi)    set MPIFLAG='-Mmpi=msmpi'
     if ($GMS_MPI_LIB == mpich2)   set MPIFLAG="-I C:\Progra~1\MPICH2\include"
     if ($TARGET == win32)         set extraflags="$GMS_WIN_FLAGS $CFLAGS $GMS_WIN_TP -DWINDOWS32 $MPIFLAG"
     if ($TARGET == win64)         set extraflags="$GMS_WIN_FLAGS $CFLAGS $GMS_WIN_TP -DWINDOWS64 $MPIFLAG"
     if ($TARGET == winazure)      set extraflags="$GMS_WIN_FLAGS $CFLAGS $GMS_WIN_TP -DWINDOWS64 -DWINDOWSAZURE"
   endif
   if ($GMS_FORTRAN == ifort) then
     set CCOMP='icl'
     set CFLAGS="-Dmemget_=MEMGET -Dmemrel_=MEMREL -Dnaptime_=NAPTIME -Dwalltimer_=WALLTIMER -DWINTEL"
     if ($GMS_WIN_OPT == baseline) set CFLAGS="$CFLAGS -Od"
     if ($GMS_WIN_OPT == linux)    set CFLAGS="$CFLAGS -O2"
     if ($GMS_WIN_OPT == samara)   set CFLAGS="$CFLAGS -O3"
     if ($GMS_WIN_OPT == fast)     set CFLAGS="$CFLAGS -fast"
     if ($GMS_WIN_OPT == testing)  set CFLAGS="$CFLAGS $GMS_INT_OPT"
     if ($GMS_MPI_LIB == msmpi) then
       if ($?MSMPI_INC) then
         echo "MPI header path: $MSMPI_INC"
         set MPIFLAG=""
         set EXTRA_FLAGS="-I$MSMPI_INC"
       else
         echo "Set MSMPI_INC to the MPI header directory and try again."
         exit 1
       endif
     endif
     if ($GMS_MPI_LIB == mpich2)     set MPIFLAG="-I C:\Progra~1\MPICH2\include"
     if ($TARGET == win32)           set extraflags="$GMS_WIN_TP $CFLAGS $GMS_WIN_FLAGS -DWINDOWS32 $MPIFLAG"
     if ($TARGET == win64)           set extraflags="$GMS_WIN_TP $CFLAGS $GMS_WIN_FLAGS -DWINDOWS64 $MPIFLAG"
     if ($TARGET == winazure)        set extraflags="$GMS_WIN_TP $CFLAGS $GMS_WIN_FLAGS -DWINDOWS64 -DWINDOWSAZURE"
   endif
endif
#
#     defend against possibly mistyped compilation target
#
if ($?extraflags == 0) then
   echo "The compall script does not have a correct TARGET machine type."
   echo "The value you supplied at 'config' time was TARGET=$TARGET"
   exit 4
endif
#
#     Compile the memory allocation C code
#
if ($?GMS_COMP_DRYRUN) set CCOMP="echo ZUNIX:$CCOMP"
#
if (($TARGET == win32) || ($TARGET == win64) || ($TARGET == winazure)) then
  set echo
  $CCOMP -c $extraflags $EXTRA_FLAGS:q -o $GMS_BUILD_DIR/object/zunix.o c:/cygwin/$GMS_PATH/source/zunix.c
  if (-e zunix.obj) mv zunix.obj $GMS_BUILD_DIR/object/zunix.o
  unset echo
else
  set echo
  $CCOMP -c $extraflags -o $GMS_BUILD_DIR/object/zunix.o $GMS_PATH/source/zunix.c
  unset echo
endif
set FAILED=$status
#
if (null$makezunix != null) then
   exit $FAILED
endif
skip-zunix:

#
#  If we do not have the BLAS in a system library, then compile a plain
#  vanilla FORTRAN version of the BLAS routines, supplied with GAMESS.
#  In some cases, the linking script will use a library if one is found,
#  in which case some of the blas.o files produced here may never be used.
#
if ($TARGET == axp64)      ./comp blas
if ($TARGET == ibm64)      ./comp blas
if ($TARGET == linux32)    ./comp blas
if ($TARGET == linux64)    ./comp blas
if ($TARGET == mac64)      ./comp blas
if ($TARGET == sun32)      ./comp blas
if ($TARGET == sun64)      ./comp blas
if ($TARGET == win32)      ./comp blas
if ($TARGET == win64)      ./comp blas
if ($TARGET == winazure)   ./comp blas
#
#  Sometimes we need a wrapper to pass 64 bit integers in GAMESS to
#  system BLAS libraries that expect 32 bit args (see 'comp' hacking)
#
if ($TARGET == fuji-pp64)  ./comp blaswrap
if ($TARGET == ibm64)      ./comp blaswrap
if ($TARGET == ibm64-sp)   ./comp blaswrap
if ($TARGET == ibm-bg  )   ./comp blaswrap
if ($TARGET == mac64)      ./comp blaswrap
if ($TARGET == sgi64)      ./comp blaswrap
if ($TARGET == sun64)      ./comp blaswrap
#
#  If this is a vector machine, we must compile the vectorized routines
#
if ($TARGET == cray-pvp) ./comp vector
if ($TARGET == cray-x1)  ./comp vector
if ($TARGET == necsx)    ./comp vector
#
#  If we are compiling a serial binary
#
if ($GMS_DDI_COMM == serial) ./comp serial
#
# openmp-start-section
#
#   modules and special code for the threaded Rys quadrature, if used
#   for compilation of files not needed by non-threaded GAMESS
#
if ($GMS_OPENMP == true) then
./comp prec
./comp mx_limits
./comp grd2_consts
./comp blkint
./comp ompmod_tools
./comp ompmod
./comp ompgrd2
endif
#
# openmp-end-section
#
#  ---------- now, compile all of the individual source files -----------
#
# compile the subset of files, where modules are defined,
# which are used in other files.
#
./comp params
./comp mpcdatpm6
./comp mpchbond
./comp rmddat
./comp secor
#
./comp aldeci
./comp algnci
./comp basccn
./comp basecp
./comp basext
./comp basg3l
./comp basg3x
./comp bashuz
./comp bashz2
./comp baskar
./comp basminix
./comp basn21
./comp basn31
./comp baspcn
./comp bassto
./comp casino
./comp ccaux
./comp cchdmy
./comp ccddi
./comp ccqaux
./comp ccquad
./comp ccsdt
./comp ceeis
./comp cepa
./comp chgpen
./comp cimf
./comp ciminf
./comp cimi
./comp cimlib
./comp cimsub
./comp cisgrd
./comp cnglob
./comp comp
./comp cosmo
./comp cosprt
./comp cphf
./comp cpmchf
./comp cprohf
./comp cpuhf
./comp dccc
./comp dcgrd
./comp dcgues
./comp dcint2
./comp dclib
./comp dcmp2
./comp dcscf
./comp dctran
./comp ddilib
./comp delocl
./comp demrpt
./comp dft
./comp dftaux
./comp dftbfo
./comp dftbgr
./comp dftbhs
./comp dftblb
./comp dftbsk
./comp dftbtd
./comp dftbx
./comp dftdis
./comp dftfun
./comp dftgrd
./comp dftint
./comp dftxca
./comp dftxcb
./comp dftxcc
./comp dftxcd
./comp dftxce
./comp dftxcf
./comp dftxcg
./comp diab
./comp dgeev
./comp dgesvd
./comp dmulti
./comp drc
./comp eaipcc
./comp ecp
./comp ecpder
./comp ecplib
./comp ecppot
./comp efpmodule
./comp efchtr
./comp efdrvr
./comp efelec
./comp efgrd2
./comp efgrda
./comp efgrdb
./comp efgrdc
./comp efinp
./comp efinta
./comp efintb
./comp efmo
./comp efmograd
./comp efmograd_es
./comp efmograd_exrep
./comp efmograd_pol
./comp efmograd_disp
./comp efpaul
./comp efpcm
./comp efpcov
./comp efpfmo
./comp eftei
./comp eigen
./comp elglib
./comp elgloc
./comp elgscf
./comp excorr
./comp eomcc
./comp ewald
./comp ffield
./comp fmo
./comp fmocp
./comp fmoesd
./comp fmogrd
./comp fmoh1a
./comp fmoh2a
./comp fmoh2b
./comp fmoh2c
./comp fmohss
./comp fmoint
./comp fmoio
./comp fmolib
./comp fmomm
./comp fmopbc
./comp fmoprp
./comp frfmt
./comp fsodci
./comp g3
./comp gamess
./comp globop
./comp gmcpt
./comp gradex
./comp grd1
./comp grd2a
./comp grd2b
./comp grd2c
./comp guess
./comp gugdga
./comp gugdgb
./comp gugdm
./comp gugdm2
./comp gugdrt
./comp gugem
./comp gugsrt
./comp gvb
./comp hess
./comp hss1a
./comp hss1b
./comp hss1c
./comp hss2a
./comp hss2b
./comp hss2c
./comp inputa
./comp inputb
./comp inputc
./comp int1
./comp int2a
./comp int2b
./comp int2c
./comp int2d
./comp int2f
./comp int2g
./comp int2r
./comp int2s
./comp iolib
./comp ivocas
./comp lagran
./comp local
./comp locatd
./comp loccd
./comp locpol
./comp locsvd
./comp lrd
./comp lut
./comp mccas
./comp mcjac
./comp mcpgrd
./comp mcpinp
./comp mcpint
./comp mcpl10
./comp mcpl20
./comp mcpl30
./comp mcpl40
./comp mcpl50
./comp mcpl60
./comp mcpl70
./comp mcpl80
./comp mcqdpt
./comp mcqdwt
./comp mcqud
./comp mcscf
./comp mctwo
./comp mdefp
./comp mexing
./comp mltfmo
./comp mm23
./comp mnsol
./comp morokm
./comp mp2
./comp mp2ddi
./comp mp2grd
./comp mp2gr2
./comp mp2ims
./comp mpcdat
./comp mpcgrd
./comp mpcint
./comp mpcmol
./comp mpcmsc
./comp mpcpcm
./comp mpqcst
./comp mthlib
./comp nameio
./comp nbostb
./comp neostb
./comp nmr
./comp optcix
./comp ordint
./comp ormas1
./comp ormpt2
./comp parley
./comp pcm
./comp pcmcav
./comp pcmcv2
./comp pcmder
./comp pcmdis
./comp pcmhss
./comp pcmief
./comp pcmpol
./comp pcmvch
./comp prpamm
./comp prpel
./comp prplib
./comp prppop
./comp qeigen
./comp qfmm
./comp qmfm
./comp qmmm
./comp qrel
./comp quanpo
./comp raman
./comp reorg
./comp rhfuhf
./comp ricab
./comp riint
./comp rimp2
./comp rimp2omp
./comp rmd
./comp rmdgen
./comp rmdrun
./comp rmdwrk
./comp roeom
./comp rohfcc
./comp rxncrd
./comp ryspol
./comp scflib
./comp scfmi
./comp scrf
./comp sfdft
./comp sfgrad
./comp sobrt
./comp soffac
./comp solib
./comp sozeff
./comp statpt
./comp surf
./comp svpchg
./comp svpinp
./comp svpleb
./comp symhi
./comp symorb
./comp symslc
./comp tddft
./comp tddefp
./comp tddfun
./comp tddfxc
./comp tddgrd
./comp tddint
./comp tddnlr
./comp tddxca
./comp tddxcc
./comp tddxcd_m05
./comp tddxcd_m06
./comp tddxcd_m08
./comp tddxcd_pkzb
./comp tddxcd_revtpss
./comp tddxcd_tpss
./comp tddxcd_vs98
./comp tdhf
./comp tdx
./comp tdxio
./comp tdxitr
./comp tdxni
./comp tdxprp
./comp trans
./comp trfdm2
./comp trnstn
./comp trudge
./comp umpddi
./comp unport
./comp utddft
./comp vibanl
./comp vscf
./comp vvos
./comp zapddi
./comp zheev
./comp zmatrx
#
#                 four plug-in codes are available:
#      TINKER  is available from the GAMESS web site    (see INTRO.DOC),
#      VB2000  is distributed with GAMESS source code tar file.
#      XMVB    is possibly available by electronic mail (see INTRO.DOC),
#      NEO     is distributed with GAMESS source code tar file.
#
#           Select one or more by setting its flag to "true".
#           You can select only one of the VB programs at a time.
#
#      optional Surface Integrated MO/MM method (SIMOMM),
#      using a customized version of the Tinker MM code.
#      To select this plug-in, set "TINKER" true in compall/comp/lked,
#      and if you are adding the Tinker code in after a previous 'compall',
#      recompile -- inputb -- to select the correct number of MM atoms,
#      as well as the file names shown just below.
#
set TINKER=false
#
# IMPORTANT if you use TINKER=true : change MAXATM in source to 12000 !!!
#
if ($TINKER == true) then
   ./comp Libtad
   ./comp Libteac
   ./comp Libtedl
   ./comp Libtemo
   ./comp Libterx
   ./comp Libtfi
   ./comp Libtjo
   ./comp Libtpr
   ./comp Libtsx
   ./comp Tdrive
   ./comp Tinkin
   ./comp Toys
endif
#
#      optional Valence Bond method, using VB2000 code
#      As of 2012, this plug-in's source is distributed with GAMESS,
#      so it is safe to select VB2000.
#      to select this plug-in, set "VB2000" true in compall/lked.
#      VB2000 is incompatible with XMVB, at most, choose only one of these.
#
set VB2000=true
if ($GMS_PHI != none)    set VB2000=false
if ($GMS_OPENMP == true) set VB2000=false
#
if ($VB2000 == true) then
   ./comp vb2000
   ./comp vb2gms
else
   ./comp vbdum
endif
#
#      optional Valence Bond method, using XMVB code
#      to select this plug-in, set "XMVB" true in compall/lked.
#      XMVB is incompatible with VB2000, at most, choose only one of these.
#
set XMVB=false
if ($XMVB == true) then
   ./comp xmvb
else
   ./comp vbdum
endif
#
#      optional Nuclear-Electronic Orbital method, using NEO code
#      to select this plug-in, set "NEO" true in compall/lked.
#
set NEO=false
if ($NEO == true) then
   ./comp neo
   ./comp neobas
   ./comp neocas
   ./comp neoden
   ./comp neofci
   ./comp neog2a
   ./comp neog2b
   ./comp neog2c
   ./comp neogrd
   ./comp neohf
   ./comp neohss
   ./comp neoint
   ./comp neomp2
   ./comp neonci
   ./comp neoopt
   ./comp neopos
   ./comp neoprp
   ./comp neosym
   ./comp neotrn
   ./comp neovib
endif
#
unset echo
echo ------------------- done with all compilations --------------------
date
time
