#! /bin/csh
# 
# Compile vb2c.c to allow for runtime memory allocation.
# Mostly copied from compall script in GAMESS 2014 
#
# This script must run from GMS_BUILD_DIR/vb2000 only,
# which may be the same as GMS_PATH/vb2000
#
# 1. Load variables from GAMESS install.info
if (-e ../install.info) then
   source ../install.info
else
   echo "File install.info not found. Please run 'config' first," 
   echo "to set up GAMESS compiling information"
   exit 4
endif
#
#  The file read just above provides the next two variables.
#
set VBPATH=$GMS_PATH/vb2000/SRC
set TARGET=$GMS_TARGET
#
#  2. Select the name of the C compiler. Often it is simply cc.
#
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'
if  ($TARGET == necsx)                            set CCOMP='c++'
#
#  3. we must select the #define in vb2c.c, and maybe other options.
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 == fuji-pp32)    set extraflags='-DSUN32'
if ($TARGET == fuji-pp64)    set extraflags='-DSUN64 -KV9'
if ($TARGET == hpux32)       goto error_not_supported
if ($TARGET == hpux64)       goto error_not_supported
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'
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 vb2c 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 (($TARGET == win32) || ($TARGET == win64) || ($TARGET == winazure)) then
  set echo
  $CCOMP -c $extraflags $EXTRA_FLAGS:q -o $GMS_BUILD_DIR/object/vb2c.o $VBPATH/vb2c.c
  if (-e vb2c.obj) mv vb2c.obj $GMS_BUILD_DIR/object/vb2c.o
  unset echo
else
  set echo
  $CCOMP -c $extraflags -o $GMS_BUILD_DIR/object/vb2c.o $VBPATH/vb2c.c
  unset echo
endif
set FAILED=$status
exit $FAILED

error_not_supported:
echo "hpux32 and hpux64 not supported."
exit 1
