#!/bin/csh
#
#   2 Mar 06 - script to compile the Distributed Data Interface (DDI)
#
#     This produces a library 'libddi.a' which GAMESS links against,
#     and on many systems, a process kickoff program called 'ddikick.x'.
#
#     ----- The following statements require your attention -----
#
#         Most sites will only need to pick the machine target!
#
#  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
#
       set TARGET = ibm64

#  2. Choose DDI communication layer, the only legal values are
#                   sockets,mpi,shmem,lapi
#     Please note that the correct choice is almost always "sockets"!
#
#     Use 'sockets' if a TCP/IP stack is available (every Unix has TCP/IP).
#                   Choosing 'sockets' will produce a process kickoff
#                   program 'ddikick.x' as well as the DDI library.
#     Use 'mpi' only for specialized hardware situations, for example if
#             your network is based on the SCI card rather than Ethernet.
#             Do not choose 'mpi' without carefully reading readme.ddi!
#     Use 'lapi' only for the IBM SP platform.
#     Use 'shmem' for the Cray T3E, Cray X1, or Compaq Supercluster.
#     Use 'shmem' for the SGI Origin, or if you like, 'sockets', but 
#             there are problems starting many processes with sockets.
#
       set COMM = sockets
       if ($TARGET == ibm64-sp)     set COMM=lapi
       if ($TARGET == cray-t3e)     set COMM=shmem
       if ($TARGET == cray-x1)      set COMM=shmem
       if ($TARGET == cray-xd1)     set COMM=sockets  # GPSHMEM not ready
       if ($TARGET == compaq-sc)    set COMM=shmem
       if ($TARGET == sgi64)        set COMM=shmem

#  3. DDI options
#     a. Maximum number of processors included in your SMP enclosures
#     b. Maximum number of nodes (number of physical SMP enclosures)
#
         set MAXCPUS  = 8
         set MAXNODES = 32
         if ($TARGET == cray-xd1) set MAXCPUS=2 # use 4 here if dual core
         if ($TARGET == cray-xd1) set MAXNODES=256

#     c. Use shared-memory (0 = no, 1 = yes)
#        Turn this off only if you are unable to adjust system limits
#        to higher values, which may be necessary on some systems,
#        and requires cooperation by the holder of the 'root' account.
#        Please see ~/gamess/ddi/readme.ddi for checking and changing
#        the System V shared memory/IPC limits for your particular OS.
#
         set SYSV = 1

#     d. If you are unable to compile the new DDI source code, because
#        of being unable to reset SystemV memory parameters, or in case
#        your operating system is very old (e.g. > 6 years) you may
#        choose to compile the original version of DDI by picking 'old'.
#        The execution script 'rungms' will need to use the old syntax
#        for ddikick.x, see 'readme.ddi' and inside that script.
#        Note that it may also be possible to run the new DDI without
#        using SystemV memory, by means of SYSV=0 instead.
#
         set DDI_SOURCE=new

#     e. If using MPI, select the path to the MPI include files,
#        for example, on our SCI-based dual Athlon cluster, 
#           set MPI_INCLUDE_PATH = '-I/opt/scali/include'
#        If using MPI, you will need to manually change 'lked' in order
#        to search appropriate MPI libraries, along the lines of
#           set LIBRARIES="$LIBRARIES /opt/scali/lib/libmpi.so"
#
         set MPI_INCLUDE_PATH = ' '
#
#   This is the end of the user selectable 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.
#
# For other compiling issues, please email ryan@si.fi.ameslab.gov with the
# subject "Compiling DDI"
#
# ------------------------------------------------------------------------- #


# ------------------------------------------------------------------------- #
# Here is an index of the options that can be defined on the command-line:
#
# -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 system v shared-memory
#
# -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
#
# ------------------------------------------------------------------------- #


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

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

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

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

  if($COMM == shmem) then
     set DDI_COMM = ' '
  endif


# ---------------------- #
# System V Shared-Memory #
# ---------------------- #
  if($SYSV == 1) set DDI_COMM = "$DDI_COMM -DUSE_SYSV"


# --------------------- #
# Troubleshooting Flags #
# --------------------- #
  set DDI_OPTS = "$DDI_COMM"

# 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

# ------------ #
# Compaq Tru64 #
# ------------ #
#    Notes:
#    AXP systems may be labeled Digital, Compaq, or HP depending on age.
#    Similarly the O/S has various names, including OSF/1, Digital Unix.
#    This target is used for all such systems, and works for Linux too.
#
#    Old C compilers ("cc -V | more -5") such as 5.6 require that you 
#    add -Dsocklen_t=int to CFLAGS below, while newer versions such
#    as 5.9, 6.1, ... have the new data type socklen_t present.
#
  if($TARGET == axp64) then

     set UNAME = `uname`
                         set CC = 'cc'
     if($UNAME == Linux) set CC = 'ccc'
                         set NumUS=1
     if($UNAME == Linux) set NumUS=2
 
     set CFLAGS = "-DCOMPAQ -O4 -ansi_alias -std -I./include"
     set CLIBS  = "-lpthread"
     set F77_OPTS = "-DINT_SIZE=long -D_UNDERSCORES=$NumUS"
     set AR_FLAGS     = 'cr'
     set RANLIB_FLAGS = ' '

  endif


# ------------------- #
# Compaq SuperCluster #
# ------------------- #
  if($TARGET == compaq-sc) then
     set AR_FLAGS     = 'cr'
     set RANLIB_FLAGS = ' '
     echo "Compiling: ddishm.o"
     ../comp ddishm
     mv ../object/ddishm.o .
     if(-e ddishm.o) then
        ar cr libddi.a ddishm.o
        rm -f ddishm.o
        echo "Finished compiling: ddishm.o"
        goto done
     else
        echo "Error compiling: ddishm.o"
        goto finish
     endif
  endif


# ------------------------------ #
# Cray parallel vector platforms #
# ------------------------------ #
#    Caution!  This has not been tested, but rather it is an educated guess.
  if($TARGET == cray-pvp) then
     set CC = 'cc'
     set CFLAGS = "-DCRAY -O -I./include"
     set CLIBS  = "-lpthread"
     set F77_OPTS = '-DINT_SIZE=long -D_UNDERSCORES=0 -DF77_UPPERCASE'
     set AR_FLAGS     = 'cr'
     set RANLIB_FLAGS = ' '
  endif


# -------- #
# Cray T3E #
# -------- #
  if($TARGET == cray-t3e) then
     echo "Compiling: ddishm.o"
     ../comp ddishm
     mv ../object/ddishm.o .
     if(-e ddishm.o) then
        ar cr libddi.a ddishm.o
        rm -f ddishm.o
        echo "Finished compiling: ddishm.o"
        goto done
     else
        echo "Error compiling: ddishm.o"
        goto finish
     endif
  endif


# ------- #
# Cray X1 #
# ------- #
  if($TARGET == cray-x1) then
     set RANLIB_FLAGS = ' '
     echo "Compiling: ddishm.o"
     cp shmem/ddishm.src ../source/.
     cd ..
     ./comp ddishm
     cd ddi
     mv ../object/$TARGET/ddishm.o .
     if(-e ddishm.o) then
        ar cr libddi.a ddishm.o
        rm -f ddishm.o
        echo "Finished compiling: ddishm.o"
        goto done
     else
        echo "Error compiling: ddishm.o"
        goto finish
     endif
  endif


# ---------#
# Cray XD1 #
# ---------#
  if (($TARGET == cray-xd1)) then
     set CC = 'pgcc'
     set CFLAGS='-DLINUX -fastsse -tp amd64 -Minfo=all -I./include'
     set CLIBS='-lpthread'
     set AR_FLAGS = "cr"
     set F77_OPTS = " -DINT_SIZE=long -D_UNDERSCORES=1"
     set RANLIB_FLAGS = ' '
  endif


# -------- #
# Cray XT3 #
# -------- #
#   This is so different to every other machine, that we just hard
#   code a special clause.  There's no C code in use, at all, just
#   the original DDI interface entirely in FORTRAN to MPI.  System
#   kickoff routine will be used, so just make libddi.a from ddi.src,
#   noting that the "comp" script will be activating its *MPI code.
#
if ($TARGET == cray-xt3) then
   echo "Cray XT3 uses the original DDI version, running over pure MPI."
   echo "Only one file from the original DDI is used, namely ddi.src."
   echo "Compiling ddi.src, using its *MPI lines..."
   cd ..
   ./comp ddi
   mv object/ddi.o ddi
   chdir ddi
   if(-e ddi.o) then
      echo "Finished compiling ddi.src, creating library..."
   else
      echo "Error compiling ddi.src"
      exit 4
   endif
   ar cr libddi.a ddi.o
   rm -f ddi.o
   echo "Done creating DDI library for Cray XT3."
   echo "There is no kickoff program, we run with system's MPI kickoff."
   exit
endif


# ------------------- #
# Fujitsu Prime Power #
# ------------------- #
  if($TARGET == fuji-pp32) then
     set CC = 'fcc'
     set CFLAGS='-DSUN32 -O -I./include'
     set CLIBS='-lpthread -lsocket -lnsl'
     set F77_OPTS='-DINT_SIZE=int -D_UNDERSCORES=1'
     set AR_FLAGS     = '-cr'
     set RANLIB_FLAGS = '-c'
  endif

  if($TARGET == fuji-pp64) then
     set CC = 'fcc'
     set CFLAGS='-KV9 -DSUN64 -O -I./include'
     set CLIBS='-KV9 -lsocket -lnsl -lpthread'
     set F77_OPTS='-DINT_SIZE=long -D_UNDERSCORES=1'
     set AR_FLAGS     = '-cr'
     set RANLIB_FLAGS = '-c'
  endif


# ----- #
# HP-UX #
# ----- #
  if($TARGET == hpux32) then
     set CC       = 'cc'
     set CFLAGS   = '-DHPUX32 +O3 +DD32 -D_32_BIT +O3 -I./include'
     set CLIBS    = '-lpthread'
     set F77_OPTS = '-DINT_SIZE=int -D_UNDERSCORES=0'
     set AR_FLAGS     = 'cr'
     set RANLIB_FLAGS = '-c'
  endif

#    see the note in readme.ddi if you have trouble compiling with
#    the socklen_t redefinition (this may occur on PA-RISC systems).
  if($TARGET == hpux64) then
     set CC     = 'cc'
     set CFLAGS = '-DHPUX64 +O3 +DD64 -D_REENTRANT -Dsocklen_t=int -I./include'
     set CLIBS  = '-lpthread'
     set F77_OPTS = '-DINT_SIZE=long -D_UNDERSCORES=0'
     set AR_FLAGS     = 'cr'
     set RANLIB_FLAGS = ' '
  endif


# ---------- #
# IBM 32-bit #
# ---------- #
  if($TARGET == ibm32) then

     set CC = 'xlc_r'
     set CFLAGS = "-DIBM32 -D_32_BIT -O3"
     set CFLAGS = "$CFLAGS -qalias=ansi -qthreaded -qarch=auto"
     set CFLAGS = "$CFLAGS -qtune=auto  -qstrict -I./include"
     set CLIBS  = "-lpthread"

     set F77_OPTS = "-DINT_SIZE=int -D_UNDERSCORES=0"

     set AR_FLAGS     = '-r -s -X 32'
     set RANLIB_FLAGS = ' '

  endif


# ---------- #
# IBM 64-bit #
# ---------- #
  if($TARGET == ibm64 || $TARGET == ibm64-sp) then
     if (`uname` == AIX) then
                                  set CC = 'xlc_r'
        if($TARGET   == ibm64-sp) set CC = 'mpcc_r'
        set CFLAGS   = "-O3 -q64 -DIBM64"
        set CFLAGS   = "$CFLAGS -qalias=ansi -qthreaded -qarch=auto"
        set CFLAGS   = "$CFLAGS -qtune=auto  -qstrict -I./include"
        set AR_FLAGS = '-r -s -X 64'
     else
        set CC       = 'gcc'
        set CFLAGS   = '-O2 -m64 -DIBM64 -I./include'
        set AR_FLAGS = 'cr'
     endif

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

  endif


# ------------- #
# IBM Blue Gene #
# ------------- #
#   This is so different to every other machine, that we just hard
#   code a special clause.  There's no C code in use, at all, just
#   the original DDI interface entirely in FORTRAN to MPI.  System
#   kickoff routine will be used, so just make libddi.a from ddi.src,
#   noting that the "comp" script will be activating its *MPI code.
#
if ($TARGET == ibm-bg) then
   echo "IBM Blue Gene uses the original DDI version, running over pure MPI."
   echo "Only one file from the original DDI is used, namely ddi.src."
   echo "Compiling ddi.src, using its *MPI lines..."
   cd ..
   ./comp ddi
   mv object/ddi.o ddi
   chdir ddi
   if(-e ddi.o) then
      echo "Finished compiling ddi.src, creating library..."
   else
      echo "Error compiling ddi.src"
      exit 4
   endif
   set AR="/bgl/BlueLight/ppcfloor/blrts-gnu/bin/powerpc-bgl-blrts-gnu-ar"
   $AR -cr libddi.a ddi.o
   rm -f ddi.o
   echo "Done creating DDI library for Blue Gene."
   echo "There is no kickoff program, we run with system's MPI kickoff."
   exit
endif


# ---------- #
# Linux-IA64 #
# ---------- #
  if($TARGET == linux-ia64) then

     set CC = 'icc'
     set CFLAGS = "-DLINUX -O3 -ansi_alias -I./include"
     set CLIBS  = "-lpthread"

     set F77_OPTS = "-DINT_SIZE=long -D_UNDERSCORES=1"

     set AR_FLAGS      = "cr"
     set RANLIB_FLAGS  = "  "
  endif


# ----------- #
# Linux-AMD64 #
# ----------- #
#      optimizations "-fastsse -Mipa=fast,safe" caused this
#      code to fail on a Xeon-based cluster with PG compilers
  if($TARGET == amd64) then
     set CC = 'pgcc'
     set CFLAGS = "-DLINUX -I./include"
     set CLIBS  = "-lpthread"

     set F77_OPTS = "-DINT_SIZE=long -D_UNDERSCORES=1"

     set AR_FLAGS      = "cr"
     set RANLIB_FLAGS  = "  "
  endif

# --------------------------------- #
# Linux for x86 PCs (Red Hat, etc.) #
# --------------------------------- #
#       some compilers (e.g. Intel's compiler) may require that you
#       select only one rather than two trailing underscores below.
  if($TARGET == linux-pc) then

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

     set F77_OPTS = '-DINT_SIZE=int -D_UNDERSCORES=2'

     set AR_FLAGS     = 'cr'
     set RANLIB_FLAGS = ' '

  endif


# ----------------------- #
# NEC SX vector processor #
# ----------------------- #
#    Caution!  This has not been tested, but rather it is an educated guess.
  if($TARGET == necsx) then
    
     set CC = 'c++'
     set CFLAGS = "-DNECSX -O -I./include -size_t64"
     set CLIBS  = "-lpthread"
     
     set F77_OPTS = '-DINT_SIZE=long -D_UNDERSCORES=1'

     set AR_FLAGS     = 'cr'
     set RANLIB_FLAGS = ' '

  endif


# ------------------------- #
# Mac OS X (10.2 or 10.3)   #
# Note: 10.1 will not work! #
# ------------------------- #
# gcc 4.0 has more modern include files, but older gcc 3.x requires
# that we use the -Dsocklen incantation.  Rather than try to detect
# the difference between gcc versions, we look to the OS version.
# The command "uname -r" returns certain kernel numbers,
# Panther (10.3.4) responds 7.4.0
#   Tiger (10.4.0) responds 8.0.0 
# which can be used to distinguish between 10.3 & older and 10.4 & newer.
#
  if (($TARGET == mac32) || ($TARGET == macG5)) then
     set os=`uname -r`
     set os=$os:r:r  # burn off the two decimal places

     set CC = 'gcc'
     set CFLAGS = "-DMACOSX -O3 -m32 -fstrict-aliasing -I./include"
     if ($os <= 7) set CFLAGS = "$CFLAGS -Dsocklen_t=int"
     set CLIBS  = "-lpthread"

                           set NumUS=2
     if ($TARGET == macG5) set NumUS=1
     set F77_OPTS = "-DINT_SIZE=int -D_UNDERSCORES=$NumUS"

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

  endif


# ---------- #
# SGI 32-bit #
# ---------- #
  if($TARGET == sgi32) then
     set CC='cc'
     set CFLAGS='-DSGI32 -n32 -i4 -O -I./include'
     set CLIBS='-lpthread'
     set F77_OPTS='-DINT_SIZE=int -D_UNDERSCORES=1'
     set AR_FLAGS='-cr'
     set RANLIB_FLAGS=' '
  endif


# ---------- #
# SGI 64-bit #
# ---------- #
#    There is a bit of trouble with the Origin:
#    The SHMEM option dies upon touching AO integral files
#    The Sockets option usually can't start 16 or more processes
#    The old version has to start data servers which are awkward on Origin
  if($TARGET == sgi64) then
     switch ($COMM)
        case shmem:
           echo "Compiling: ddio3k.o"
           ../comp ddio3k
           mv ../object/ddio3k.o .
           if(-e ddio3k.o) then
              ar -cr libddi.a ddio3k.o
              rm -f ddio3k.o
              echo "Finished compiling: ddio3k.o"
              goto done
           else
              echo "Error compiling: ddio3k.o"
              goto finish
           endif
           breaksw
        case sockets:
           set CC = 'cc -64 -mips4'
           set CFLAGS = '-DSGI64 -O -I./include'
           set CLIBS='-lpthread'
           set F77_OPTS='-DINT_SIZE=long -D_UNDERSCORES=1'
           set AR_FLAGS     = '-cr'
           set RANLIB_FLAGS = '-c'
           breaksw
        default:
           echo SGI 64 bits requires COMM of sockets or shmem.
           exit 3
           breaksw
     endsw
  endif


# ---------- #
# Sun 32-bit #
# ---------- #
  if($TARGET == sun32) then

     set CC = 'cc'
     if(`uname -p` == sparc) set ARCH='-xarch=v8plus'
     if(`uname -p` == i386)  set ARCH='-xarch=pentium_pro'
     set CFLAGS = "$ARCH -DSUN32 -O -Dsocklen_t=int -D_32_BIT -I./include"
     set CLIBS  = '-lsocket -lnsl -lpthread'

     set F77_OPTS = "-DINT_SIZE=int -D_UNDERSCORES=1"

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

  endif


# ---------- #
# Sun 64-bit #
# ---------- #
  if($TARGET == sun64) then

     set CC = 'cc'
     if(`uname -p` == sparc) set ARCH='-xarch=v9'
     if(`uname -p` == i386)  set ARCH='-xarch=amd64'
     set CFLAGS = "$ARCH -DSUN64 -O -I./include"
     set CLIBS  = '-lsocket -lnsl -lpthread'

     set F77_OPTS = "-DINT_SIZE=long -D_UNDERSCORES=1"

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

  endif

  if ($?CFLAGS) then
  else
    echo The compddi script does not select a correct TARGET machine type.
    echo What you typed when editing this script was $TARGET
    exit 4
  endif

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


  echo Beginning the DDI compilation at `date`
  rm -f *.a *.x ./obj/*.o

# -------------------- #
# Compile object files #
# -------------------- #
  if($COMM == mpi) set CFLAGS = "$CFLAGS $MPI_INCLUDE_PATH"
  set CC_CMD = "$CC $CFLAGS $DDI_OPTS"
  set CC_CMD = "$CC_CMD -DMAX_SMP_PROCS=$MAXCPUS -DMAX_NODES=$MAXNODES"


  if($DDI_SOURCE == old) goto old

# ---------------------- #
# Compile common objects #
# ---------------------- #
  foreach OBJ ( soc_create      std_system     tcp_sockets   \
                debug           parse_node_args              \
              )
    echo "Compiling common object: $OBJ.o"
    set echo
    $CC_CMD -c ./src/$OBJ.c -o ./obj/$OBJ.o
    unset echo
    if(-e ./obj/$OBJ.o) then
       echo "Finished compiling: $OBJ.o"
    else
       echo "Error compiling: $OBJ.o"
       echo "Abort."
       goto finish
    endif
    ar $AR_FLAGS libddi.a      ./obj/$OBJ.o
    ar $AR_FLAGS libddikick.a  ./obj/$OBJ.o
    echo
  end


# ------------------- #
# Compile DDI objects #
# ------------------- #
  foreach OBJ ( ddi            ddi_send       ddi_recv      \
                ddi_get        ddi_put        ddi_acc       \
                ddi_nproc      ddi_nnode      ddi_distrib   \
                ddi_subpatch   ddi_index      ddi_recvany   \
                ddi_create     ddi_destroy    ddi_server    \
                ddi_gsum       ddi_bcast      ddi_sync      \
                ddi_dlb        ddi_init      \
                ddi_finalize   ddi_memory     ddi_lapi      \
                ddi_isend      ddi_irecv      ddi_wait      \
                ddi_signals    ddi_id         ddi_timer     \
                ddi_getacc     ddi_group      ddi_gdlb      \
                sysv_ipc       ddi_dlb_proc                 \
              )
    echo "Compiling DDI object: $OBJ.o"
    set echo
    $CC_CMD -c ./src/$OBJ.c -o ./obj/$OBJ.o
    unset echo
    if(-e ./obj/$OBJ.o) then
       echo "Finished compiling: $OBJ.o"
    else
       echo "Error compiling: $OBJ.o"
       echo "Abort."
       goto finish
    endif
    ar $AR_FLAGS libddi.a      ./obj/$OBJ.o
    echo
  end 


# ----------------------- #
# Compile DDIKICK objects #
# ----------------------- #
  if($COMM == sockets) then
  foreach OBJ ( ddikick        ddikick_error  accept_tasks   \
                finalize_tasks kickoff_local  kickoff_remote \
                kickoff_pbs \
              )
    echo "Compiling ddikick object: $OBJ.o"
    set echo
    $CC_CMD -c ./tools/ddikick/$OBJ.c -o ./obj/$OBJ.o
    unset echo
    if(-e ./obj/$OBJ.o) then
       echo "Finished compiling: $OBJ.o"
    else
       echo "Error compiling: $OBJ.o"
       echo "Abort."
       goto finish
    endif
    ar $AR_FLAGS libddikick.a  ./obj/$OBJ.o
    echo
  end
  endif
 

# ------------------------ #
# Compile FORTRAN Wrappers #
# ------------------------ #
  echo "Compiling FORTRAN wrappers"
  set echo
  $CC_CMD $F77_OPTS -c ./src/ddi_fortran.c -o ./obj/ddi_fortran.o
  unset echo
    if(-e ./obj/ddi_fortran.o) then
       echo "Finished compiling: ddi_fortran.o"
    else
       echo "Error compiling: ddi_fortran.o"
       echo "Abort."
       goto finish
    endif
  ar $AR_FLAGS libddi.a  ./obj/ddi_fortran.o
  echo


# ----------------------- #
# Compile kickoff program #
# ----------------------- #
  if($COMM == sockets) then
     ranlib $RANLIB_FLAGS libddikick.a
     echo " "
     echo "Compiling DDI kickoff program"
     set echo
     $CC $CFLAGS -o ddikick.x ./obj/ddikick.o -L./ -lddikick $CLIBS
     unset echo
     echo " "
     rm -f libddikick.a
     rm -f obj/*.o
  else
     rm -f libddikick.a
     rm -f obj/*.o
  endif


# --------------------------- #
# Compile OLD DDI source code #
# --------------------------- #
old:
  if($DDI_SOURCE == old) then

      # ddisoc.c
        echo "Compiling: ddisoc.o"
        set echo
        $CC $CFLAGS -c ./oldsrc/ddisoc.c -o ./obj/ddisoc.o
        unset echo
        if(-e ./obj/ddisoc.o) then
           echo "Finished compiling: ddisoc.o"
        else
           echo "Error compiling: ddisoc.c"
           goto finish
        endif
        ar $AR_FLAGS libddi.a  ./obj/ddisoc.o
        echo ""

      # ddi.src
        echo "Compiling: ddi.o"
        ../comp ddi
        mv ../object/ddi.o .
        if(-e ddi.o) then
           echo "Finished compiling: ddi.o"
        else 
           echo "Error compiling: ddi.o"
           goto finish
        endif
        ar $AR_FLAGS libddi.a ddi.o
        rm -f ddi.o
        echo ""

      # ddikick.x
        if(-e ddikick.x) rm -f ddikick.x
        echo "Compiling: ddikick.x"
        set echo
        $CC $CFLAGS -o ddikick.x oldsrc/ddikick.c $CLIBS
        unset echo
        echo ""
  endif
 

# --------------------------- #
# Run RANLIB on the libraries #
# --------------------------- #
#   SGI has ranlib built into its ar command (very rational)
done:
  if (($TARGET != sgi64) && ($TARGET != sgi32)) then
     ranlib $RANLIB_FLAGS libddi.a
  endif


# --------------------- #
# End of compile script #
# --------------------- #
finish:
  echo "End of DDI compile script"
  date
  time
