#!/bin/csh -f
#
# 27 Mar 2014 - script to compile the Distributed Data Interface (DDI)
#
#     This produces a library 'libddi.a' which GAMESS links against,
#     and perhaps, a process kickoff program called 'ddikick.x' and
#     perhaps a test program 'ddi_test.x'.
#
# Please send your issues to the GAMESS public
# issue tracker on GitHub:
# https://github.com/gms-bbg/gamess-issues/issues
#
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" >> /dev/stderr
   exit 4
endif
#
# TO use DDI large messages (e.g., >2GB) you need to set the flag to be DDI_MSG_FACTOR>1.
# The value of the flag should be an integer value and it is used to divide the current message 
# by that value to ensure it fits into the "count" limit of MPI. Common usage:
# set EXTRA_FLAGS=' -DDDI_MSG_FACTOR=8'
# additionally a threashold value is set in src/common.h : DDI_LG_MSG_THRESHOLD
# so only messages greater than the threshold are set up to transfer with the DDI_MSG_FACTOR.
#
# NOTE: This feature does NOT work for 32-bit architectures which can not address memory >2GB. 
#
set EXTRA_FLAGS=''
#
#  the configuration file read just above provides the next two variables,
#  and several other GMS_XXX values that will be used further down.
#
set TARGET=$GMS_TARGET
chdir $GMS_PATH/ddi
#
#  The following "numbers" make choices based primarily on 'config' values,
#  such as the machine target, and communication layer.
#  A human might want to review some of these selections, particularly #2,
#  but if you don't understand something, leave it alone.
#
#  1. Choose DDI communication layer, the only legal values are
#              sockets,mixed,mpi,shmem,lapi,armci
#     Please note that the correct choice is very often "sockets"!
#
#     Use 'serial' to use DDI stubs and build a DDI-free serial binary of GAMESS
#     Use 'sockets' if a TCP/IP stack is available.
#                   Choosing 'sockets' will produce a process kickoff
#                   program 'ddikick.x', as well as the DDI library.
#                   Every Unix has TCP/IP for its Ethernet, and "sockets"
#                   is an highly rational choice for Ethernet clusters.
#     Use 'mixed' or 'mpi' for MPI-based high performance networks.
#           'mixed' uses almost entirely MPI-1 calls, but uses a few
#                   TCP/IP sockets to avoid some of MPI's polling, often
#                   resulting in better wall clock times than 100% MPI.
#             'mpi' runs use 100% MPI-1 calls, but this often bogs down
#                   in polling, and can be much slower than 'mixed'.
#                         Read about MPI in ~/gamess/ddi/readme.ddi
#                         before choosing either 'mixed' or 'mpi'.
#     Use     'mpi' for the IBM Blue Gene/P platform.
#     Use     'mpi' for the Cray XT series.
#     Use     'mpi' for the SGI Altix or ICE, with its 'mpt' MPI library.
#     Use   'armci' only if you like experimenting (not supported well)
#
#     Clusters should use TCP/IP sockets on networks up to and including GE,
#     but probably should use 'mpi' on networks like Infiniband.
#
#  As of Feb. 2011, 'config' usually sets GMS_DDI_COMM to 'sockets'.
#       However, target 'linux64' may set GMS_DDI_COMM to 'mpi',
#  along with collecting information about which flavor of mpi,
#  where the MPI library is located, and so forth.
#
set COMM = $GMS_DDI_COMM
#
#     high end machines are just hardwired as follows.  Don't change them.
#
if ($COMM =~ serial*) then
   echo "##########################################################"
   echo "# DDI communication target is 'serial'. Using DDI-stubs. #"
   echo "# DDI does not need to be compiled.                      #"
   echo "##########################################################"
   cd $GMSPATH
   ./comp serial
   exit 0
endif
if ($TARGET =~ hpe-cray-*)  set COMM=mpi
if ($TARGET == cray-xt)     set COMM=mpi
if ($TARGET == cray-xc)     set COMM=mpi
if ($TARGET == ibm64)       set COMM=mpi

#  2. DDI node sizes
#     a. Maximum number of processors (cores) in your SMP enclosures
#     b. Maximum number of nodes (number of physical SMP enclosures)
#
#        It is OK if these values are larger than your system!
#
set MAXCPUS=128
set MAXNODES=32
#
# core affinity for sockets
# (i.e., run each compute GAMESS process on a fixed core)
# Set SOC_AFF to "no" if your OS does not provide the standard routine,
# which is sched_setaffinit (e.g., you have linking errors).
#
set SOC_AFF=no
#
# VeraChem
#
if ($GMS_VM2 == true) then
   set MAXCPUS=32
   set MAXNODES=1024
endif
#
if ($GMS_PHI != none) then
   set MAXCPUS=512
   set MAXNODES=64
endif
#
# ARM64 ThunderX2
#
if (`uname -p` == aarch64) then
   set MAXCPUS=256
   set MAXNODES=1
endif
#
#      you might want to bump MAXCPUS or MAXNODES to match your HPE Cray system
if ($TARGET =~ hpe-cray-*) then
   if ($GMS_HPC_SYSTEM_TARGET == narwhal) then
      set MAXCPUS=128
      set MAXNODES=2176
   else
      set MAXCPUS=128
      set MAXNODES=128
   endif
endif
#
#      you might want to bump MAXCPUS or MAXNODES to match your XT
if ($TARGET == cray-xt) then
   set MAXCPUS=12
   set MAXNODES=1024
endif
#
# match your XC
if ($TARGET == cray-xc) then
   set MAXCPUS=256
   set MAXNODES=100000
   if ( $?GMS_HPC_SYSTEM_TARGET ) then
      if ( $GMS_HPC_SYSTEM_TARGET == onyx ) then
         # This should give us the high-end on the number of CPU (or threads) for onyx
         #
         # Intel Xeon E5-2699v4 Broadwell nodes (standard/large memory)
         #     - 44 cores/node
         #     - 4,810 total nodes (standard memory)
         #     - 4 total nodes (large memory)
         # Intel Xeon Phi 7230 Knights Landing nodes (KNL)
         #     - 64 cores/node
         #     - 4 hardware threads/core
         #     - 256 hardware threads total
         #     - 32 nodes
         #
         set MAXCPUS=256
         set MAXNODES=4810
      endif
      #
      if ( $GMS_HPC_SYSTEM_TARGET == theta ) then
         # This should give us the high-end on the number of CPU (or threads) for onyx
         #
         # Intel Xeon Phi 7230 Knights Landing nodes
         #     - 64 cores/node
         #     - 4 hardware threads/core
         #     - 256 hardware threads total
         #     - 4,392 nodes
         #
         set MAXCPUS=256
         set MAXNODES=4392
      endif
   endif
endif
#
#      you might want to bump MAXNODES or MAXCPUS to match your BG/Q
#      the P named Mira at Argonne Nat. Lab. has 64 cores, and 49152 nodes,
#      but runs on it are usually done with only 16 cores to quadruple
#      the memory per core.
if ($TARGET == fj-a64fx) then
   set MAXCPUS=48
   set MAXNODES=1024
endif

if ($TARGET == ibm64) then
   set MAXCPUS=168
   set MAXNODES=4608
endif

#  3. Use System V-style shared-memory and semaphores
#        Please see the file readme.ddi for checking and changing
#        System V shared memory/IPC limits for your particular OS.
#        Turn System V memory off if:
#        1) if you are unable to adjust system limits to higher values,
#           which requires cooperation from the 'root' account.
#        2) on some systems with micro-kernels, SysV may be taboo.
#        3) if SysV is just missing, e.g. in Cygwin under Windows.
#
set SYSV = true
if ($COMM == armci)      set SYSV=false
if ($TARGET == win64)    set SYSV=false

#  4. Incomplete POSIX replacement for System V code
#     Always leave SYSV above true, as we continue to use SYSV semaphores!
#        Choose the next as 'sysv'  to use System V shared memory.
#        Choose the next as 'posix' to use POSIX    shared memory.
set SHMTYPE=$GMS_SHMTYPE

#  5. If using MPI, select the path to the include file mpi.h
#     This part is for Linux clusters, where there are many different
#     MPI libraries, and they may be installed almost anywhere.
#
#     For example, Intel MPI version 3.2 installed at the default
#     place would want to define something like
#        set MPI_INCLUDE_PATH = '/opt/intel/impi/3.2/include64'
#     Other scripting changes related to compiling and linking for
#     an MPI library are the "fge hack" in 'comp', and setting up
#     the message libraries in 'lked'.
#     Execution of GAMESS over MPI is left up to the user's ability
#     to modify 'rungms', based on the information about each MPI
#     contained in 'readme.ddi'!
#
set MPI_INCLUDE_PATH = ''
#
#           big iron knows where its MPI is going to be found!
#           initialize this variable (to anything) so we will skip
#           setups for various MPI flavors (mpich2/impi/...) below.
if ($TARGET =~ hpe-cray-*) then
   if (  $?CRAY_MPICH_VERSION ) then
      set GMS_MPI_LIB=HPE-Cray-MPI
   else if ( $?MPT_VERSION ) then
      set GMS_MPI_LIB=mpt
      set GMS_MPI_PATH=$MPI_ROOT
   else if ( $?I_MPI_ROOT ) then
      set GMS_MPI_LIB=impi
      set GMS_MPI_PATH=$I_MPI_ROOT
      endif
   endif
endif
if ($TARGET == cray-xt) set GMS_MPI_LIB=Cray-XT-MPI
if ($TARGET == cray-xc) set GMS_MPI_LIB=Cray-XC-MPI
#
#   This is the end of the user selectable "numbered" options!
#
#
# ------------------------------------------------------------------------- #
# Troubleshooting -- If you should run into any errors while compiling,
# check the following lists of common failures.
#
# 1) socklen_t is not defined.  Uncomment the following line.
#    set SOCKLEN_T = "-Dsocklen_t=int"
#
# 2) turn on debugging output.  set DEBUG_LEVEL to the level of debugging.
#    0 ==> Debugging (e.g. arg checking) compiled in, but no extra output.
#          Note that 0 selects more debugging than the normal compilation.
#    1 ==> Minimum debugging output.
#    5 ==> Standard debugging output.
#   10 ==> Significantly more output.
#   15 ==> Even more ... probably best to avoid this one.
#    One can also call DDI_DEBUG(level) to set the desired level
#    around a troublesome piece of code.
#    Uncomment the following line to enable more argument checking:
#
#    set DEBUG_LEVEL=0
#
# 3) If you want debugging output from only a particular CPU.
#    set DEBUG_CPU=RANK
#    set DEBUG_CPU=0
#
# 4) To make all point to point send/recv messages fully synchronous,
#    add the flag "-DSOC_SYNC" somewhere below, if TCP/IP, of course.
#
# ------------------------------------------------------------------------- #

# ------------------------------------------------------------------------- #
# Here is an index of the options that can be defined to the C compiler
#
# -DDDI_SOC  -- use sockets; requires ddikick if mpi is not used.
# -DDDI_MPI  -- use MPI; must be kicked off with MPI kickoff;
#               assumes 1:1 mapping of compute processes and data servers
# -DDDI_LAPI -- use LAPI for distributed data operations; kickoff using
#               the poe command; NO data servers
# -DUSE_SYSV -- make use of shared-memory, choosing this requires
#    one of the suboptions -DUSE_SHMEM_SYSV or -DUSE_SHMEM_POSIX
#
# -DMACHINE              -- defines the target architecture
# -D_32_BIT              -- specifies a 32-bit machine (64-bit default)
# -DINT_SIZE=(int,long)  -- defines the size of a FORTRAN integer
# -D_UNDERSCORES=(0,1,2) -- number of underscores on a FORTRAN object
# -DF77_UPPERCASE        -- use UPPERCASE names for FORTRAN objects
#
# ------------------------------------------------------------------------- #


echo "       Building the Distributed Data Interface library"
echo "           started at `date`"
echo " "
echo "Compiling for machine type $TARGET,"
echo "using communication model $COMM,"
echo "System V shared memory option set $SYSV,"
echo "with maxima of $MAXCPUS processors/node and $MAXNODES nodes."
echo " "
echo "This compilation should produce the DDI library libddi.a"
#
if ($COMM == sockets) echo "This compilation should also produce the kick-off program ddikick.x"
#
echo " "
#
if (($GMS_DDI_COMM == mpi) || ($GMS_DDI_COMM == mixed)) then
   switch ($GMS_MPI_LIB)
      case impi:
         if ($GMS_TARGET == win64) then
            set MPI_INCLUDE_PATH = "$GMS_MPI_PATH/include"
         else
            if (-f $GMS_MPI_PATH/include/mpi.h ) set MPI_INCLUDE_PATH = "$GMS_MPI_PATH/include"
            if (-f $GMS_MPI_PATH/include64/mpi.h ) set MPI_INCLUDE_PATH = "$GMS_MPI_PATH/include64"
            if (-f $GMS_MPI_PATH/intel64/include/mpi.h ) set MPI_INCLUDE_PATH = "$GMS_MPI_PATH/intel64/include"
            if ($GMS_PHI == knc) set MPI_INCLUDE_PATH = "$GMS_MPI_PATH/mic/include"
         endif
         breaksw
      case fj-mpi:
      case mpich:
      case mpich2:
      case mpt:
      case mvapich2:
      case myrinet:
      case openmpi:
         if ($?MPI_INCLUDE) then
            set MPI_INCLUDE_PATH = "$MPI_INCLUDE"
         else
            set MPI_INCLUDE_PATH = "$GMS_MPI_PATH/include"
         endif
         breaksw
         #            MicroSoft MPI uses a compiler flag, set below, not a pathname
      case msmpi:
         if(($GMS_FORTRAN == pgf77) || ($GMS_FORTRAN == pgfortran) || ($GMS_FORTRAN == ifort)) set MPI_INCLUDE_PATH = " "
         goto skip_mpi_setup
         breaksw
         #            big machines will find their mpi.f, don't check it.
      case IBM-BlueGene-MPI:
      case HPE-Cray-MPI:
         goto skip_mpi_setup
         breaksw
      case Cray-XT-MPI:
         goto skip_mpi_setup
         breaksw
      case Cray-XC-MPI:
         goto skip_mpi_setup
         breaksw
      case spectrum:
         set MPI_INCLUDE_PATH = "$GMS_MPI_PATH/include"
         breaksw
      default:
         echo "unknown MPI software requested" >> /dev/stderr
         exit 4
         breaksw
   endsw
   #         Double check that a valid path to MPI was provided.
   #         Checking omitted in comp/lked as probably compddi will be run 1st.
   if (-e "$MPI_INCLUDE_PATH/mpi.h") then
      echo "The MPI-1 software library to be used is $GMS_MPI_LIB,"
      echo "with the include file 'mpi.h' at $MPI_INCLUDE_PATH"
   else
      echo "Something is wrong with your MPI definitions in 'config'," >> /dev/stderr
      echo "since there is no such file $MPI_INCLUDE_PATH/mpi.h" >> /dev/stderr
      exit 4
   endif
endif
#
skip_mpi_setup:

echo " "

# ------------- set options forced by the choices made above ------------- #

# --------------------- #
# Communication Options #
# --------------------- #
if($COMM == sockets) then
   set DDI_COMM = '-DDDI_SOC'
     if($SOC_AFF == yes) set DDI_COMM = '-DDDI_SOC -DSOC_AFF'
endif

if($COMM == mixed) then
   set DDI_COMM = '-DDDI_MPI -DDDI_SOC'
endif

if($COMM == mpi) then
   set DDI_COMM = '-DDDI_MPI'
endif

if($COMM == armci) then
   set DDI_COMM = "-DDDI_MPI -DDDI_ARMCI"
   set DDI_COMM = "$DDI_COMM -DDDI_ARMCI_IMPLICIT_NBPUT"
   set DDI_COMM = "$DDI_COMM -DDDI_ARMCI_IMPLICIT_NBGET"
   set DDI_COMM = "$DDI_COMM -DDDI_ARMCI_IMPLICIT_NBACC"
   set DDI_COMM = "$DDI_COMM -DDDI_ARMCI_IMPLICIT_WAIT"
endif


# ---------------------- #
# System V Shared-Memory #
# ---------------------- #
if($SYSV == true) then
   set DDI_COMM = "$DDI_COMM -DUSE_SYSV"
   if ($SHMTYPE == sysv)  set DDI_COMM="$DDI_COMM -DUSE_SHMEM_SYSV"
   if ($SHMTYPE == posix) set DDI_COMM="$DDI_COMM -DUSE_SHMEM_POSIX"
endif


# --------------------- #
# Troubleshooting Flags #
# --------------------- #
set DDI_OPTS = "$DDI_COMM"
set DDI_OPTS = "$DDI_OPTS -DMAX_SMP_PROCS=$MAXCPUS -DMAX_NODES=$MAXNODES"

# 1) socklen_t
if($?SOCKLEN_T == 0) set SOCKLEN_T = " "
set DDI_OPTS = "$DDI_OPTS $SOCKLEN_T"

# 2) debugging options
if($?DEBUG_LEVEL == 0) then
   set DEBUG_LEVEL = ""
else
   set DEBUG_LEVEL = "-DDDI_DEBUG=$DEBUG_LEVEL"
endif
set DDI_OPTS = "$DDI_OPTS $DEBUG_LEVEL"

# 3) debugging a particular cpu
if($?DEBUG_CPU == 0) then
   set DEBUG_CPU = ""
else
   set DEBUG_CPU = "-DDDI_DEBUG_CPU=$DEBUG_CPU"
endif
set DDI_OPTS = "$DDI_OPTS $DEBUG_CPU"


# ------------- start of machine specific option selection -------------- #

unset CFLAGS

# -------- #
# HPE Cray #
# -------- #
if ($TARGET =~ hpe-cray-*) then
   #
   set CC='cc'
   #
   if ( $?CRAY_FTN_VERSION ) then
      set GMS_HPE_CRAY_COMP = cray
   endif
   #
   if ( $?INTEL_VERSION ) then
      set GMS_HPE_CRAY_COMP = intel
      if ( $?PE_ENV ) then
         if ( $PE_ENV != "INTEL") set CC = icc
      endif
   endif
   #
   if ( $?GNU_VERSION ) then
      set GMS_HPE_CRAY_COMP = gnu
      if ( $?PE_ENV ) then
         if ( $PE_ENV != "GNU") set CC = gcc
      endif
   endif
   #
   if ( $?NVIDIA_VERSION ) then
      set GMS_HPE_CRAY_COMP = nvidia
      if ( $?PE_ENV ) then
         if ( $PE_ENV != "NVIDIA") set CC = nvcc
      endif
   endif
   #
   if ( $?AOCC_VERSION ) then
      set GMS_HPE_CRAY_COMP = aocc
      if ( $?PE_ENV ) then
         if ( $PE_ENV != "AOCC") set CC = clang
      endif
   endif
   #
   if  ( $?CRAY_CPU_TARGET ) then
      set GMS_HPE_CRAY_ARCH = $CRAY_CPU_TARGET
   else
      set GMS_HPE_CRAY_ARCH = $CPU
   endif
   #
   if ( $?CRAY_MPICH_VERSION ) then
      set CFLAGS="-DLINUX64 -DCRAY_MPI -DUSE_MPI_BARRIER -I$CRAY_MPICH_PREFIX/include"
   else if ( $?MPT_VERSION ) then
      set CFLAGS="-DLINUX64 -DUSE_MPI_BARRIER -I$MPI_ROOT/include"
   else if ( $?I_MPI_ROOT ) then
      set CFLAGS="-DLINUX64 -DUSE_MPI_BARRIER -I$I_MPI_ROOT/include"
   endif
   #
   switch ($GMS_HPE_CRAY_COMP)
      case cray:
         set CFLAGS="$CFLAGS -O3"
         breaksw
      case intel:
         set CFLAGS="$CFLAGS -O3 -m64 -fstrict-aliasing"
         breaksw
      case gnu:
         set CFLAGS="$CFLAGS -O3 -m64 -fstrict-aliasing"
         set CLIBS="-lpthread"
         if ($SHMTYPE == posix) set CLIBS="$CLIBS -lrt"
         set CFLAGS = "$CFLAGS -Dgetarg_=_gfortran_getarg_i4"
         set CFLAGS = "$CFLAGS -Diargc_=_gfortran_iargc"
         breaksw
      case nvidia:
         set CFLAGS=""
         echo "Need to define CFLAGS"
         exit 1
         breaksw
      case aocc:
         set CFLAGS=""
         echo "Need to define CFLAGS"
         exit 1
         breaksw
      default:
         echo "Invalid HPE Cray compiler"
         exit 1
         breaksw
   endsw

   switch ($GMS_HPE_CRAY_ARCH)
      case x86-rome:
      case x86_64:
      default:
         set CFLAGS = "$CFLAGS"
         breaksw
   endsw

   set AR_FLAGS     = 'cr'
   set RANLIB_FLAGS = ' '
   set F77_OPTS = "-DINT_SIZE=long -D_UNDERSCORES=1"
endif

# ------- #
# Cray XT #
# ------- #
if ($TARGET == cray-xt) then
   echo "---------------"
   echo "Loaded modules:"
   echo "---------------"
   module list

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

   set CC='cc'
   set CFLAGS='-DLINUX64 -DCRAY_MPI -DUSE_MPI_BARRIER -I./'

   switch ($GMS_XT_COMP)
      case pgi:
         set CFLAGS="$CFLAGS -O3 -mcmodel=medium -fastsse -Mfpmisalign"
         breaksw
      case gnu:
         set CFLAGS="$CFLAGS -O3 -m64 -fstrict-aliasing"
         breaksw
      case intel:
      case cray:
      default:
         set CFLAGS="$CFLAGS -O3"
         breaksw
   endsw

   switch ($GMS_XT_ARCH)
      case barcelona:
         set CFLAGS = "$CFLAGS -tp barcelona-64 -Mfpmisalign"
         breaksw
      case shanghai:
         set CFLAGS = "$CFLAGS -tp shanghai-64 -Mfpmisalign"
         breaksw
      case i7:
         set CFLAGS = "$CFLAGS -tp x64"
         breaksw
      default:
         breaksw
   endsw

   set AR_FLAGS     = 'cr'
   set RANLIB_FLAGS = ' '
   set F77_OPTS = "-DINT_SIZE=long -D_UNDERSCORES=1"
endif

# ------- #
# Cray XC #
# ------- #
if ($TARGET == cray-xc) then
   echo "---------------"
   echo "Loaded modules:"
   echo "---------------"
   module list

   # ftn from intall.info
   set FORTRAN=$GMS_FORTRAN
   #
   #        Itan2 does not allow -m32/-m64 flags, it's all 64 bit pointers.
   #
   set ARCH='-m64'
   if (`uname -p` == ia64) set ARCH=''

   set GMS_XC_COMP = 'intel'

   set CC='cc'
   set CFLAGS = "-dynamic -mcmodel=large -DLINUX $ARCH -O3 -fstrict-aliasing -I./"
   set CLIBS  = "-lpthread"

   if ( $?GMS_HPC_SYSTEM_TARGET ) then
      if ( $GMS_HPC_SYSTEM_TARGET == onyx ) then
         set CFLAGS="-dynamic -mcmodel=large -DCRAY_MPI -DUSE_MPI_BARRIER $ARCH -O3 -fstrict-aliasing -I./"
      endif
      #
      if ( $GMS_HPC_SYSTEM_TARGET == theta ) then
         if ($GMS_PHI == knl) set CFLAGS="$CFLAGS -xMIC-AVX512"
      endif
   endif

   if ($SHMTYPE == posix) set CLIBS="$CLIBS -lrt"

   switch ($GMS_XC_COMP)
      case intel:
      default:
         set NumUS=1
         set FORT_FLAGS = "-O2 -i8"
         if ( $?GMS_HPC_SYSTEM_TARGET ) then
            if ( $GMS_HPC_SYSTEM_TARGET == onyx ) then
               # Intel Broadwell
               if ($GMS_PHI == none) set FORT_FLAGS="$FORT_FLAGS -xCORE-AVX2"
               if ($GMS_PHI == knl)  set FORT_FLAGS="$FORT_FLAGS -xMIC-AVX512"
            endif
            #
            if ( $GMS_HPC_SYSTEM_TARGET == theta ) then
               if ($GMS_PHI == knl) set FORT_FLAGS="$FORT_FLAGS -xMIC-AVX512"
            endif
         endif
         breaksw
      default:
         echo "Please spell your compiler correctly." >> /dev/stderr
         exit 4
   endsw

   set AR_FLAGS     = 'cr'
   set RANLIB_FLAGS = ' '
   set F77_OPTS = "-DINT_SIZE=long -D_UNDERSCORES=$NumUS"
   set FORT_LIBS = ' '
   # End of Cray-XC
endif

# ----------------------------------------------------------- #
# Fujitsu 64-bit PrimeHPC with A64FX cpus, as FX1000 or FX700 #
# ----------------------------------------------------------- #
if($TARGET == fj-a64fx) then
   set FORTRAN=$GMS_FORTRAN   # might frt or frtpx (cross-compiler)
   set ARCH=''
   if (`uname -p` == aarch64) set ARCH='-mcpu=native'
   set CFLAGS = "-DLINUX $ARCH -Nclang -O3 -fstrict-aliasing -I./"
#   if ($GMS_MSUCC == true) set CFLAGS = "$CFLAGS -Kcmodel=large"
   set CLIBS  = "-lpthread"
   set NumUS=1
   set FORT_FLAGS = "-Kfast"
   if ($SHMTYPE == posix) set CLIBS="$CLIBS -lrt"
   switch ($FORTRAN)
      case frt:
         set CC = 'fcc'
         breaksw
      case frtpx:
         set CC = 'fccpx'
         breaksw
      default:
         echo "Please spell your linux64 FORTRAN compiler correctly." >> /dev/stderr
         exit 4
   endsw
   set F77_OPTS = "-DINT_SIZE=long -D_UNDERSCORES=$NumUS"
   set AR_FLAGS     = 'cr'
   set RANLIB_FLAGS = ' '
   set FORT_LIBS = ' '
endif
# ---------- #
# IBM 64-bit #
# ---------- #
if($TARGET == ibm64) then
   set FORTRAN=$GMS_FORTRAN   # might be gfortran,gfortran, or xlf

   if ($FORTRAN == xlf) then
      if (`uname` == AIX) then
         set CC = 'xlc_r'
         set CFLAGS   = "-O3 -q64 -DIBM64"
         set CFLAGS   = "$CFLAGS -qalias=ansi -qthreaded -qarch=auto"
         set CFLAGS   = "$CFLAGS -qtune=auto  -qstrict -I./"
         set AR_FLAGS = '-r -s -X 64'
      else
         #           set CC       = 'gcc'
         #           set CFLAGS   = '-O2 -m64 -DIBM64 -I./'
         set CC       = 'xlc_r'
         set CFLAGS   = '-O2 -m64 -DIBM64 -I./'
         if ($GMS_LIBCCHEM == true) then
            #set CFLAGS   = "${CFLAGS} -qextname=ddi_smp_nproc,ddi_nproc,ddi_masters_gsumi,ddi_masters_gsumf,ddi_masters_bcast,ddi_dlbnext,ddi_sync,ddi_dlbreset"
         endif
         set AR_FLAGS = 'cr'
      endif

      set CLIBS    = "-lpthread"
      set F77_OPTS = "-DINT_SIZE=long -D_UNDERSCORES=0"
      set RANLIB_FLAGS = ' '

      if(`uname` == AIX) then
         set FORT_FLAGS = '-q64 -qintsize=8'
         set FORT_LIBS  = ' '
      else
         set FORT_FLAGS = '-q64 -qintsize=8'
         if ($GMS_LIBCCHEM == true) then
            set FORT_FLAGS = "$FORT_FLAGS -qextname"
         endif
         set FORT_LIBS  = '-lxlf90_r'
      endif
   else if ($FORTRAN == gfortran) then
      set ARCH='-m64'
      if (`uname -p` == ia64) set ARCH=''
      if (`uname -p` == aarch64) set ARCH='-mcpu=native'

      set CC = 'gcc'
      set CFLAGS = "-DLINUX $ARCH -O3 -fstrict-aliasing -I./"
      set CLIBS  = "-lpthread"

      if ($SHMTYPE == posix) set CLIBS="$CLIBS -lrt"

      set CFLAGS = "$CFLAGS -Dgetarg_=_gfortran_getarg_i4"
      set CFLAGS = "$CFLAGS -Diargc_=_gfortran_iargc"
      set NumUS=1
      set FORT_FLAGS = "-O2 $ARCH -fdefault-integer-8 -std=legacy"

      set F77_OPTS = "-DINT_SIZE=long -D_UNDERSCORES=$NumUS"

      set AR_FLAGS     = 'cr'
      set RANLIB_FLAGS = ' '

      set FORT_LIBS = ' '
   else if (($GMS_FORTRAN == pgfortran) || ($GMS_FORTRAN == pgf77)) then
      set ARCH='-m64'
      if (`uname -p` == ia64) set ARCH=''
      if (`uname -p` == aarch64) set ARCH='-mcpu=native'

      set CLIBS  = "-lpthread"

      if ($SHMTYPE == posix) set CLIBS="$CLIBS -lrt"

      set NumUS=1
      set CC = 'pgcc'
      set CFLAGS = "-DLINUX $ARCH -fast -I./"
      if ( `uname` == Darwin ) then
         set FORT_FLAGS = "-fast $ARCH -i8                "
      else
         set CFLAGS = "$CFLAGS -mcmodel=medium"
         set FORT_FLAGS = "-O2 $ARCH -i8 -mcmodel=medium"
      endif

      set F77_OPTS = "-DINT_SIZE=long -D_UNDERSCORES=$NumUS"

      set AR_FLAGS     = 'cr'
      set RANLIB_FLAGS = ' '

      set FORT_LIBS = ' '
   endif
endif


# ---------------------------------------- #
# Linux for 32 bit systems (Red Hat, etc.) #
# ---------------------------------------- #
#     see comments just above the Linux 64 bit clause just below.
#
if($TARGET == linux32) then

   set FORTRAN=$GMS_FORTRAN   # might be g77, gfortran, ifort, pgf77, f2c

   set CC = 'gcc'
   set CFLAGS = "-m32 -DLINUX -O3 -fstrict-aliasing -I./"
   set CLIBS  = "-lpthread"

   switch ($FORTRAN)
      case g77:
      case pgfortran:
      case pgf77:
      case f2c:
         set NumUS=2
         breaksw
      case gfortran:
         set CFLAGS = "$CFLAGS -Dgetarg_=_gfortran_getarg_i4"
         set CFLAGS = "$CFLAGS -Diargc_=_gfortran_iargc"
         set FORT_FLAGS = '-m32'
         set NumUS=1
         breaksw
      case ifort:
         set NumUS=1
         breaksw
      default:
         echo "Please spell your linux32 FORTRAN compiler correctly." >> /dev/stderr
         exit 4
   endsw

   set F77_OPTS = "-DINT_SIZE=int -D_UNDERSCORES=$NumUS"

   set AR_FLAGS     = 'cr'
   set RANLIB_FLAGS = ' '

   set FORT_FLAGS = "$FORT_FLAGS -O2"
   set FORT_LIBS = ' '
endif

# ---------------------------------------------- #
# Linux for 64 bit PCs (both AMD and Intel chips #
# ---------------------------------------------- #
#       note that DDI is in C, so FORTRAN below is not used except for a
#       test program.  The compiler name also lets this script choose the
#       the number of trailing underscores needed, but more importantly,
#       deals with gfortran's use of names prefixed by _gfortran_ in its
#       library of FORTRAN callable routines.
#
#       Note that in all cases, we use the gcc compiler from GNU for
#       the compilation of DDI, even if a non-GNU FORTRAN is used.
#
if($TARGET == linux64) then

   set FORTRAN=$GMS_FORTRAN   # might be gfortran, ifort, pgf77
   #
   #        Itan2 does not allow -m32/-m64 flags, it's all 64 bit pointers.
   #
   set ARCH='-m64'
   if (`uname -p` == ia64) set ARCH=''
   if (`uname -p` == aarch64) set ARCH='-mcpu=native'

   set CC = 'gcc'
   set CFLAGS = "-DLINUX $ARCH -O3 -fstrict-aliasing -I./"
   set CLIBS  = "-lpthread"

   if ($GMS_PHI == knc) then
      set CC='icc'
      set CFLAGS="$CFLAGS -mmic"
   endif

   if ($SHMTYPE == posix) set CLIBS="$CLIBS -lrt"

   switch ($FORTRAN)
      case armflang:
         set CC = 'armclang'
         set CFLAGS = "$CFLAGS -Dgetarg_=getarg"
         set NumUS=1
         set FORT_FLAGS = "-O2 $ARCH -i8 -std=legacy"
         breaksw
         case aocc
      case gfortran:
         set CFLAGS = "$CFLAGS -Dgetarg_=_gfortran_getarg_i4"
         set CFLAGS = "$CFLAGS -Diargc_=_gfortran_iargc"
         set NumUS=1
         set FORT_FLAGS = "-O2 $ARCH -fdefault-integer-8 -std=legacy"
         breaksw
      case nvfortran:
      case pgfortran:
      case pgf77:
         set NumUS=1
         if ( `uname` == Darwin ) then
            set FORT_FLAGS = "-O2 $ARCH -i8                "
         else
            set CFLAGS = "$CFLAGS -mcmodel=medium"
            set FORT_FLAGS = "-O2 $ARCH -i8 -mcmodel=medium"
         endif
         breaksw
      case oneapi-ifx:
#        set CC = 'icx'
      case oneapi-ifort:
      case ifort:
         set NumUS=1
         set FORT_FLAGS = "-O2 -i8"
         if ($GMS_PHI == knc) set FORT_FLAGS="$FORT_FLAGS -mmic"
         #
         if ($GMS_VM2 == true) set EXTRA_FLAGS = "-DDDI_NO_DATA_SERVERS"
         breaksw
      default:
         echo "Please spell your linux64 FORTRAN compiler correctly."
         exit 4
   endsw

   set F77_OPTS = "-DINT_SIZE=long -D_UNDERSCORES=$NumUS"

   set AR_FLAGS     = 'cr'
   set RANLIB_FLAGS = ' '

   set FORT_LIBS = ' '
endif


# -------- #
# Mac OS X #
# -------- #
# OS X 10.1 will not work at all, 10.2 is primitive, 10.3 needs an edit below.
# for 64 bit mode, we need at least OS X 10.4, but >= 10.5 is more better.
#
  if ($TARGET == mac64) then

   set FORTRAN=$GMS_FORTRAN       # will be g77 or gfortran

   set CC = 'gcc'
     set CFLAGS = "-DLINUX -m64"
   set CFLAGS = "$CFLAGS -O3 -fstrict-aliasing -I./"
   set CLIBS  = "-lpthread"

   #       uncomment next line for OS X 10.3
   #---set CFLAGS = "$CFLAGS -Dsocklen_t=int"

   switch ($FORTRAN)
      case g77:
         set NumUS=2
         set FORT_FLAGS = '-O2'
         set FORT_LIBS  = ''
         breaksw
      case gfortran:
         set CFLAGS = "$CFLAGS -Dgetarg_=_gfortran_getarg_i4"
         set CFLAGS = "$CFLAGS -Diargc_=_gfortran_iargc"
         set NumUS=1
         if($GMS_GFORTRAN_VERNO == 11.0) then
            set FORT_FLAGS = '-O2 -fdefault-integer-8 -std=legacy'
         else
            set FORT_FLAGS = '-O2 -m64 -fdefault-integer-8 -std=legacy'
         endif
         set FORT_LIBS  = ''
         breaksw
      default:
         echo "Your OS X FORTRAN compiler was not correctly specified." >> /dev/stderr
         exit
         breaksw
   endsw

   set F77_OPTS = "-DINT_SIZE=long"
   set F77_OPTS = "$F77_OPTS -D_UNDERSCORES=$NumUS"

   set AR_FLAGS     = 'cr'
   set RANLIB_FLAGS = '-c'

endif

# ----------------- #
# Microsoft Windows #
# ----------------- #
# Currently with only PGI compiler support.

if($TARGET == win64) then

   set FORTRAN=$GMS_FORTRAN     # choose from pgf77 and ifort

   switch ($FORTRAN)
      case oneapi-ifort:
      case ifort:
         set CC = 'icl -Qm64 -W0'
         set CFLAGS = '-DLINUX -DWINDOWS -DWINDOWS64 -DWINTEL -DOLDDDITIMER'
         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 -O3"          # '-fast' will not work with DDI. Linking will fail.
         if ($GMS_WIN_OPT == testing)     set CFLAGS = "$CFLAGS $GMS_INT_OPT"
         set CFLAGS = "$GMS_WIN_TP $CFLAGS $GMS_WIN_FLAGS -I./include"
         set NumUS=0
         set F77_OPTS = "-DF77_UPPERCASE -Dgetarg_=GETARG -Diargc_=IARGC"
         breaksw
      case pgfortran:
      case pgf77:
         set CC = 'pgcc -m64'
         #
         #          For more detailed information as to which flags are being used
         #          by the compiler during the build - uncomment the line below.
         #
         #          set CC = "$CC -v"
         #
         #          set CFLAGS = '-DLINUX -DWINDOWS -DWINDOWS64 -DUSE_DDI_COLLECTIVE_ROUTINES -DOLDDDITIMER'
         set CFLAGS = '-DLINUX -DWINDOWS -DWINDOWS64 -DOLDDDITIMER'
         if ($GMS_WIN_OPT == baseline)    set CFLAGS = "$CFLAGS -Bstatic -Minform=severe -O0"
         if ($GMS_WIN_OPT == linux)       set CFLAGS = "$CFLAGS -Bstatic -Minform=severe -O2"
         if ($GMS_WIN_OPT == samara)      set CFLAGS = "$CFLAGS -Bstatic -Minform=severe -O3"
         if ($GMS_WIN_OPT == fast)        set CFLAGS = "$CFLAGS -Bstatic -Minform=severe -fastsse"
         if ($GMS_WIN_OPT == testing)     set CFLAGS = "$CFLAGS -Bstatic -Minform=severe $GMS_PGI_OPT"
         set CFLAGS = "$GMS_WIN_FLAGS $CFLAGS $GMS_WIN_TP -I./include"
         set NumUS=1
         set F77_OPTS = "-m64"
         if ($COMM == mpi) then
            echo "----------------------------------------------   ATTENTION   ----------------------------------------------"
            echo "- We are using MPI and not TCP/IP sockets so please ignore the following compiler warning message:        -"
            echo "- libddi.a(tcp_sockets.o) : warning LNK####: no public symbols found; archive member will be inaccessible -"
            echo "-----------------------------------------------------------------------------------------------------------"
         endif
         breaksw
      default:
         echo "Please spell your win64 bit FORTRAN compiler name correctly." >> /dev/stderr
         exit 4
   endsw
   set CLIBS  = ' '
   set F77_OPTS = "-DINT_SIZE=__int64 -D_UNDERSCORES=$NumUS $F77_OPTS"
   set AR_FLAGS     = 'cr'
   set RANLIB_FLAGS = ' '
endif

#            this is how we trap for an unsupported machine.
if ($?CFLAGS) then
else
   echo "The compddi script does not select a correct TARGET machine type." >> /dev/stderr
   echo "What you typed when editing this script was $TARGET" >> /dev/stderr
   exit 4
endif

# ------------- end of machine specific option selection -------------- #


echo Attempting to remove binary files from previous DDI build...
set nonomatch
foreach file (src/*.o)
        if (-f "$file" && -w "$file") rm "$file"
end
unset nonomatch
unset file
if (-f libddi.a && -w libddi.a)         rm libddi.a
if (-f kickoff/libddikick.a && -w kickoff/libddikick.a) rm kickoff/libddikick.a
if (-f ddikick.x && -w ddikick.x)       rm ddikick.x
if (-f ddi_test.x && -w ddi_test.x)     rm ddi_test.x
if (-f ddi_server.x && -w ddi_server.x) rm ddi_server.x
echo " "
echo Beginning the DDI compilation at `date`
echo " "

# --------------------------------------------------- #
# append the MPI include path to the C compiler flags
# --------------------------------------------------- #
if($COMM == mixed) set CFLAGS = "$CFLAGS -I$MPI_INCLUDE_PATH"
if($COMM == mpi)   then
   switch ($GMS_MPI_LIB)
      case msmpi:
         if (($GMS_FORTRAN == pgf77) || ($GMS_FORTRAN == pgfortran)) set CFLAGS = "$CFLAGS -Mmpi=msmpi"
         if ($GMS_FORTRAN == ifort) then
            if ($?MSMPI_INC) then
               echo "MPI headers path: $MSMPI_INC"
               # We set EXTRA_FLAGS, instead of CFLAGS as we want to
               # properly pass through spaces in MSMPI_INC to the compiler.
               # Setting -I$MSMPI_INC in CFLAGS causes problems with whitespace
               set EXTRA_FLAGS="-I$MSMPI_INC"
            else
               echo "Set MSMPI_INC to the include path for Microsoft MPI and try again" >> /dev/stderr
               exit 1
            endif
         endif
         breaksw
      case HPE-Cray-MPI:
      case Cray-XT-MPI:
      case Cray-XC-MPI:
      case IBM-BlueGene-MPI:
         breaksw
      default:
         set CFLAGS = "$CFLAGS -I$MPI_INCLUDE_PATH"
         breaksw
   endsw
endif


# ---------------------- #
# Compile common objects #
# ---------------------- #
#   In this context, common means the TCP/IP kickoff program
#   will also be using this code, so save into its library too.
#
#   Here and elsewhere, Microsoft Windows requires -o to name object files
#
#set EXTRA_FLAGS = "$EXTRA_FLAGS -pthread -L/autofs/nccs-svm1_sw/summit/.swci/1-compute/opt/spack/20180914/linux-rhel7-ppc64le/gcc-5.4.0/spectrum-mpi-10.2.0.11-20190201-tzw5hhylirma6hdksxpvenzwpdkbvdwb/lib"
chdir src
foreach OBJ ( soc_create      std_system     tcp_sockets   \
             debug           parse_node_args              \
             )
   echo "Compiling common object: $OBJ.o"
   (set echo; $CC $CFLAGS  $DDI_OPTS -o $OBJ.o -c $OBJ.c $EXTRA_FLAGS)
   if(-e $OBJ.obj) mv -v $OBJ.obj $OBJ.o
   if(-e $OBJ.o) then
      echo "Finished compiling: $OBJ.o"
   else
      echo "Error compiling: $OBJ.o"
      goto bombout
   endif
   ar $AR_FLAGS ../libddi.a   $OBJ.o
   if($COMM == sockets) ar $AR_FLAGS ../kickoff/libddikick.a  $OBJ.o
   echo " "
end
chdir ..

# ------------------- #
# Compile DDI objects #
# ------------------- #
#   The bulk of the DDI library:
chdir src
set OBJ_LIST=(ddi            ddi_send        ddi_recv        \
              ddi_get        ddi_put         ddi_acc         \
              ddi_getacc     ddi_distrib     \
              ddi_subpatch   ddi_index       ddi_recvany     \
              ddi_create     ddi_destroy     ddi_server      \
              ddi_finalize   ddi_memory      ddi_lapi        \
              ddi_isend      ddi_irecv       ddi_wait        \
              ddi_signals    ddi_id          ddi_timer       \
              ddi_gdlb       ddi_dlb         ddi_dlb_proc    \
              ddi_init       shmem_ipc       \
              ddi_comm       ddi_comm_group  ddi_comm_send   \
              ddi_comm_recv  ddi_comm_sync   ddi_comm_gsum   \
              ddi_comm_bcast ddi_smp         ddi_comm_create \
              ddi_comm_nproc ddi_comm_nnode \
              ddi_util       mmath          \
              ddi_arr        ddi_arr_acc     ddi_arr_add     \
              ddi_arr_dot    ddi_arr_scalar  ddi_arr_select  \
              ddi_scattered_data ddi_scatter_acc             )

# ARMCI objects
if ($COMM == armci) then
   set OBJ_LIST=($OBJ_LIST     \
                 ddi_armci      ddi_armci_init  ddi_armci_acc  \
                 ddi_armci_get  ddi_armci_put   ddi_armci_getacc)
endif

  # CPU affinity for sockets
  if ($COMM == sockets && $SOC_AFF == yes) then
    set OBJ_LIST=($OBJ_LIST ddi_aff)
  endif

# Microsoft Windows objects
  if ($TARGET == win64) then
   set OBJ_LIST=($OBJ_LIST windows)
endif

foreach OBJ ($OBJ_LIST)
   echo "Compiling DDI object: $OBJ.o"
   if(($OBJ == windows) && ($GMS_FORTRAN == ifort)) then
      set WFLAGS = '-DLINUX -DWINDOWS -DWINDOWS64 -DWINTEL -DOLDDDITIMER'
      set WFLAGS = "$WFLAGS -Od $GMS_WIN_TP -I./include"
      (set echo; $CC $WFLAGS $EXTRA_FLAGS:q $DDI_OPTS -o $OBJ.o -c $OBJ.c)
   else
      (set echo; $CC $CFLAGS $EXTRA_FLAGS:q $DDI_OPTS -o $OBJ.o -c $OBJ.c)
   endif
   if(-e $OBJ.obj) mv -v $OBJ.obj $OBJ.o
   if(-e $OBJ.o) then
      echo "Finished compiling: $OBJ.o"
   else
      echo "Error compiling: $OBJ.o"
      goto bombout
   endif
   ar $AR_FLAGS ../libddi.a  $OBJ.o
   echo " "
end
chdir ..


# ------------------------ #
# Compile FORTRAN Wrappers #
# ------------------------ #
chdir src
echo "Compiling FORTRAN wrappers for DDI"
(set echo; $CC $CFLAGS $EXTRA_FLAGS:q $DDI_OPTS $F77_OPTS -o ddi_fortran.o -c ddi_fortran.c)
if(-e ddi_fortran.obj) mv -v ddi_fortran.obj ddi_fortran.o
if(-e ddi_fortran.o) then
   echo "Finished compiling: ddi_fortran.o"
else
   echo "Error compiling: ddi_fortran.o"
   goto bombout
endif
ar $AR_FLAGS ../libddi.a  ddi_fortran.o
echo " "
chdir ..


# ------------------------ #
# Compile DDI test program #
# ------------------------ #
#      the test program should work over other communication libraries,
#      such as MPI-1, but for now we create it only for the very easy
#      case of TCP/IP.
#      We would need to add MPI library lists (not just an MPI include path)
#      to this script, in order to correctly build the test program.
#      That is postponed to some other time!
if($COMM == sockets) then
   chdir test
   echo "Creating DDI test program, ddi_test.x"
   (set echo; \
   $FORTRAN $FORT_FLAGS -o ../ddi_test.x ddi_test.f \
            ../libddi.a $FORT_LIBS $CLIBS)
   if (-f ddi_test.o && -w ddi_test.o) rm ddi_test.o
   echo " "
   chdir ..
endif


# --------------------------- #
# Compile data_server program #
# --------------------------- #
#    This program is not used by GAMESS, which acts as its own data server,
#    but might be useful if one wants a light-weight data server.
#    This program is in principle usable with MPI-1, but we have not yet
#    done the scripting to add all the MPI library possibilities here.
if($COMM == sockets) then
   chdir server
   echo "Creating standalone DDI data server program, data_server.x"
   (set echo; \
   $CC $CFLAGS $DDI_OPTS -I../src -o ../data_server.x data_server.c \
       ../libddi.a $CLIBS)
   if (-f data_server.o && -w data_server.o) rm data_server.o
   echo " "
   chdir ..
endif


# ------------------------------------------------------ #
# Compile DDIKICK objects, and then link kickoff program #
# ------------------------------------------------------ #
#     The reason for not inserting a "mv ddikick.x .." command in
#     this script is for situations when rebuilding the library is
#     done later, when one might not want to erase the previous
#     ddikick.x (i.e. if GAMESS jobs are running at the moment).
#
#     note that some of the routines called by ddikick.x
#     have already been compiled above, and saved in
#     the kickoff program's library file.
if($COMM == sockets) then
   chdir kickoff
   echo "TCP/IP communications uses a special kickoff program, ddikick.x"
   echo " "
   foreach OBJ ( ddikick        ddikick_error  accept_tasks   \
                finalize_tasks kickoff_local  kickoff_remote \
                kickoff_pbs \
                )
      echo "Compiling ddikick object: $OBJ.o"
      (set echo; $CC $CFLAGS $DDI_OPTS -I../src -c $OBJ.c)
      if(-e $OBJ.o) then
         echo "Finished compiling: $OBJ.o"
      else
         echo "Error compiling: $OBJ.o"
         goto bombout
      endif
      if ($OBJ != ddikick) ar $AR_FLAGS libddikick.a  $OBJ.o
      echo " "
   end
   #
   ranlib $RANLIB_FLAGS libddikick.a
   #
   echo "Linking DDI kickoff program, ddikick.x"
   (set echo; $CC $CFLAGS -pthread $DDI_OPTS -o ../ddikick.x ddikick.o libddikick.a $CLIBS)
   echo " "
   echo "Don't forget to move ddikick.x up one directory level,"
   echo "by a   'mv ddikick.x ..'   command."
   echo " "
   set nonomatch
   foreach file (*.o)
           if (-f "$file" && -w "$file") rm "$file"
   end
   unset nonomatch
   unset file
   if (-f libddikick.a && -w libddikick.a) rm libddikick.a
   chdir ..
endif

# ------------------------------------------------------------ #
# End of compile script, run "ranlib" on the library and exit. #
# ------------------------------------------------------------ #
finishOK:

ranlib $RANLIB_FLAGS libddi.a

echo "DDI compilation ended successfully."
#rm -f src/*.o
date
time
exit

#    if something bad happens, remove all binary files,
#    to make sure the user realizes compddi crashed out.
bombout:
echo " "
echo "DDI compilation did not finish correctly, please fix, and try again"
echo " "
chdir ..
if (-f libddi.a && -w libddi.a)         rm libddi.a
if (-f kickoff/libddikick.a && -w kickoff/libddikick.a) rm kickoff/libddikick.a
if (-f ddikick.x && -w ddikick.x)       rm ddikick.x
if (-f ddi_test.x && -w ddi_test.x)     rm ddi_test.x
if (-f ddi_server.x && -w ddi_server.x) rm ddi_server.x
set nonomatch
foreach file (src/*.o)
        if (-f "$file" && -w "$file") rm "$file"
end
foreach file (kickoff/*.o)
        if (-f "$file" && -w "$file") rm "$file"
end
foreach file (test/*.o)
        if (-f "$file" && -w "$file") rm "$file"
end
foreach file (server/*.o)
        if (-f "$file" && -w "$file") rm "$file"
end
unset nonomatch
unset file
date
time
exit 8
