#!/bin/csh -f
############################################################################
# RCS INFORMATION:
#
#       $RCSfile: charmc,v $
#       $Author: sanjeev $      $Locker:  $              $State: Exp $
#       $Revision: 1.11 $        $Date: 1995/04/14 20:04:52 $
#
############################################################################
# DESCRIPTION:
#
############################################################################
# REVISION HISTORY:
#
# $Log: charmc,v $
# Revision 1.11  1995/04/14  20:04:52  sanjeev
# integrating Charm and Converse
#
# Revision 1.10  1995/03/24  18:14:35  sanjeev
# removed qm
#
# Revision 1.9  1995/02/28  21:10:33  jyelon
# *** empty log message ***
#
# Revision 1.8  1995/02/28  20:57:25  jyelon
# Made charmc recognize the -I/include/file/directory and -L/lib/file/dir
# options.
#
# Revision 1.7  1995/02/28  20:41:15  jyelon
# Corrected bug - didn't work if you had CC, CPLUS, etc in your
# environment.  Also corrected bug - didn't always put .o files in
# consistent location.
#
# Revision 1.6  1995/02/16  22:38:23  knauff
# Sending unknown flags to the preprocessor as well.
#
# Revision 1.5  1995/02/03  19:24:11  sanjeev
# put in externs in module_init.c file
#
# Revision 1.4  1994/11/30  18:26:41  narain
# Added code to create a file with calls to module init functions in a single
# function. (Note : The option to directly give a list of modules is not correct
# yet).
#   -- Narain
#
# Revision 1.3  1994/11/18  20:24:55  narain
# Added a -main option to specify that the main is in the user program
#  - Sanjeev and Narain
#
# Revision 1.2  1994/11/09  21:21:36  sanjeev
# separated qm, etc
#
# Revision 1.1  1994/10/14  21:01:27  brunner
# Initial revision
#
############################################################################
#
# Configuration instructions:
#
# DEFAULT_MACHTYPE ---
#
#  The value of this variable is patched in automatically
#  by the INSTALL and SUPER_INSTALL scripts.
#

set DEFAULT_MACHTYPE=sp1

##############################################################################
#
# Initialize the main controlling variables, then setup error handler
#
##############################################################################

set OPTS_CC=
set OPTS_CPLUS=
set OPTS_LD=
set OPTS_CPP=
set FILES=
set OBJECTFILES=
set LIBRARIES=
set CONVERSE=
onintr failure

##############################################################################
#
# The following section attempts to locate charm.
#
##############################################################################

set ORIGDIR=$cwd

# Try to find charm by looking in directory where charmc is

if ($?CHARMROOT == 0) then
    set SCRIPT=$0
    if ($SCRIPT:h != $SCRIPT:t) then
        cd $SCRIPT:h
        set SCRIPT=$SCRIPT:t
    else
        foreach dir ($path)
            if (-x $dir/$SCRIPT && ! -d $dir/$SCRIPT) then
                cd $dir
                break
            endif
        end
    endif
    while (x`find $SCRIPT -type l -print` == x$SCRIPT)
        set SCRIPT=`ls -al ./$SCRIPT:t | sed -e "s@.*-> @@"`
        if ($SCRIPT:h != $SCRIPT:t) then
            cd $SCRIPT:h
            set SCRIPT=$SCRIPT:t
        endif
    end
    if (-e $cwd/.charm-beacon) then
        cd ..
        setenv CHARMROOT $cwd
    endif
endif

# Try to find charm by searching the path

if ($?CHARMROOT == 0) then
    echo -n 'Searching your path for charm... '
    foreach dir ($path)
        if (-e $dir/.charm-beacon) then
            cd $dir/..
            setenv CHARMROOT $cwd
            echo " found $cwd"
            break
        endif
    end
    if ($?CHARMROOT == 0) echo ' failed.'
endif

# Give an error message

if ($?CHARMROOT == 0) then
    echo ''
    echo 'Error - cannot find the charm directories.'
    echo ''
    echo 'Normally, charmc and the other charm scripts automatically'
    echo 'locate the charm directory, using the following strategy:'
    echo ''
    echo '  Step 1. It looks in the directory specified in $CHARMROOT.'
    echo ''
    echo '  Step 2. It looks in the directory from which charmc was run.'
    echo ''
    echo '  Step 3. It looks in every directory in your $PATH'
    echo ''
    echo 'If it searches all these places, and doesnt find the file'
    echo 'called ".charm-beacon" (which is normally hidden in the charm'
    echo 'bin-directory), it will give up.'
    echo ''
    goto failure
    endif
endif

cd $ORIGDIR

##############################################################################
#
# Parse the arguments
#
# Don't do any analysis in here, just the parsing.
#
##############################################################################


set argindex=1
while ($argindex <= $#argv)
    set arg=$argv[$argindex]
    @ argindex = $argindex + 1

    switch ($arg)
        case "-machine"
            set MACHTYPE=$argv[$argindex]
            @ argindex = $argindex + 1
            breaksw

        case "-language"
            set LANGUAGE=$argv[$argindex]
            @ argindex = $argindex + 1
            breaksw

#	case "-qm"
#            set QMSTRAT=$argv[$argindex]
#            @ argindex = $argindex + 1
#            breaksw
#
	case "-balance"
            set BALANCE=$argv[$argindex]
            @ argindex = $argindex + 1
            breaksw

        case "-queue"
            set QUEUE=$argv[$argindex]
            @ argindex = $argindex + 1
            breaksw

        case "-memory"
            set MEMORY=$argv[$argindex]
            @ argindex = $argindex + 1
            breaksw

	case "-execmode"
            set EXECMODE=$argv[$argindex]
            @ argindex = $argindex + 1
            breaksw

        case "-main"
            set MAINOBJ=
            breaksw

        case "-converse"
            set CONVERSE=1
            breaksw

        case "-verbose"
            set VERBOSE=1
            breaksw

        case "-save"
            set SAVE=1
            breaksw

        case "-cc"
            set OVERRIDE_CC=( $argv[$argindex] )
            @ argindex = $argindex + 1
            breaksw

        case "-c++"
            set OVERRIDE_CPLUS=( $argv[$argindex] )
            @ argindex = $argindex + 1
            breaksw

        case "-ld"
            set OVERRIDE_LD=( $argv[$argindex] )
            @ argindex = $argindex + 1
            breaksw

        case "-cc-option"
            set OPTS_CC=( $OPTS_CC $argv[$argindex] )
            @ argindex = $argindex + 1
            breaksw

        case "-c++-option"
            set OPTS_CPLUS=( $OPTS_CPLUS $argv[$argindex] )
            @ argindex = $argindex + 1
            breaksw

        case "-purify"
            set PURIFY = yes
            breaksw

        case "-ld-option"
            set OPTS_LD=( $OPTS_LD $argv[$argindex] )
            @ argindex = $argindex + 1
            breaksw

        case "-c":
            set NOLINK=1
            breaksw

	case "-o":
            set OBJECT=$argv[$argindex]
            @ argindex = $argindex + 1
            breaksw

# Options for specifying modules 
	
	case "-modules":
	    set ACT_MOD_LIST = $argv[$argindex]
	    @ argindex = $argindex + 1
	    breaksw

	case "-module_file":
	    set MOD_FILE_NAME = $argv[$argindex]
	    @ argindex = $argindex + 1
	    breaksw

        case "-D*"
            set OPTS_CPP=( $OPTS_CPP "$arg" )
            set OPTS_CC=( $OPTS_CC "$arg" )
            set OPTS_CPLUS=( $OPTS_CPLUS "$arg" )
            breaksw

        case "-I*"
            set OPTS_CPP=( $OPTS_CPP "$arg" )
            set OPTS_CC=( $OPTS_CC "$arg" )
            set OPTS_CPLUS=( $OPTS_CPLUS "$arg" )
            breaksw

        case "-L*"
            set OPTS_CC=( $OPTS_CC "$arg" )
            set OPTS_CPLUS=( $OPTS_CPLUS "$arg" )
            set OPTS_LD=( $OPTS_LD "$arg" )
            breaksw

        case "-g"
            set OPTS_CC=( $OPTS_CC $arg )
            set OPTS_CPLUS=( $OPTS_CPLUS $arg )
            set OPTS_LD=( $OPTS_LD $arg )
            breaksw

        case "-O*"
            set OPTS_CC=( $OPTS_CC $arg )
            set OPTS_CPLUS=( $OPTS_CPLUS $arg )
            breaksw

        case "-l*"
            set LIBRARIES=( $LIBRARIES $arg )
            breaksw

        case "-s"
            set OPTS_LD=( $OPTS_LD $arg )
            breaksw

        case "-*"
            echo "passing unrecognized option $arg to all compilers and linkers"
            set OPTS_CC=( $OPTS_CC $arg )
            set OPTS_CPLUS=( $OPTS_CPLUS $arg )
            set OPTS_LD=( $OPTS_LD $arg )
	    set OPTS_CPP=( $OPTS_CPP $arg )
            breaksw

        case "*.*"
            set FILES=( $FILES "$arg" )
            breaksw
        
        default:
            echo "Unrecognized argument $arg"
            goto failure

    endsw
end

##############################################################################
#
# Check for valid choice of MACHTYPE
#
##############################################################################

if ($?MACHTYPE == 0) set MACHTYPE=$DEFAULT_MACHTYPE

if (! -e $CHARMROOT/bin/$MACHTYPE.machine.script) then
    echo ''
    echo 'error - there is no installed charm for "'$MACHTYPE'"'
    echo ''
    goto failure
endif

set ROOTMACH=$CHARMROOT/$MACHTYPE

##############################################################################
#
# Check for valid choice of LANGUAGE
#
##############################################################################

if ($?LANGUAGE == 0) set LANGUAGE=charm

switch ($LANGUAGE)
    case "charm"
    case "charm++":
        breaksw
    default:
        echo "charmc: Unrecognized choice of language $LANGUAGE"
endsw



##############################################################################
#
# Check for valid EXECMODE and select compatible options
#
##############################################################################

if ($?EXECMODE == 0) set EXECMODE=notrace

switch ($EXECMODE)
    case "normal":
        echo 'charmc: note - normal has been renamed to notrace. Continuing.'
    case "notrace":
        set EXECMODE=notrace
        set LOGEXEC=nothing
        set EXTRAEXEC=normal
        set EXECDIR=normal
        breaksw
    case "summary":
        set EXECMODE=summary
        set LOGEXEC=summary
        set EXTRAEXEC=normal
        set EXECDIR=normal
        breaksw
    case "projections":
        set EXECMODE=projections
        set LOGEXEC=projections
        set EXTRAEXEC=record
        set EXECDIR=debug
        breaksw
    case "record":
        set EXECMODE=record
        set LOGEXEC=record
        set EXTRAEXEC=record
        set EXECDIR=debug
        breaksw
    case "replay":
        set EXECMODE=replay
        set LOGEXEC=replay
        set EXTRAEXEC=replay
        set EXECDIR=debug
        breaksw
    default:
        echo "Link ERROR : Unknown execmode value"
        goto failure
endsw

# Check that execmode is compatible with queue and balance

if ($EXECMODE == replay) then
    if ($?QUEUE == 0) set QUEUE=replay
    if ($?BALANCE == 0) set BALANCE=replay
#    if ($?QMSTRAT == 0) set QMSTRAT=replay
    if ($QUEUE != replay) then
        echo "Ignoring -queue $QUEUE, incompatible with -execmode replay"
    endif
    if ($BALANCE != replay) then
        echo "Ignoring -balance $BALANCE, incompatible with -execmode replay"
    endif
#    if ($QMSTRAT != replay) then
#        echo "Ignoring -qm $QMSTRAT, incompatible with -execmode replay"
#    endif
    set QUEUE=replay
    set BALANCE=replay
    set QMSTRAT=replay
endif


##############################################################################
#
# Check for valid choice of MEMORY
#
##############################################################################

if ($?MEMORY == 0) set MEMORY=table

switch ($MEMORY)
    case "table":
    case "unix":
    case "leak":
        breaksw;
    default:
        echo "charmc : Unknown memory strategy $MEMORY"
        goto failure
endsw


##############################################################################
#
# Check for valid choice of QUEUE
#
##############################################################################

if ($?QUEUE == 0) set QUEUE=fifo

switch ($QUEUE)
    case "stack":
    case "fifo":
    case "fl":
    case "bfifo":
    case "ififo":
    case "bstack":
    case "istack":
    case "rand":
    case "replay":
        breaksw
    default:
	echo "charmc : Unknown queue strategy $QUEUE"
        goto failure
endsw


##############################################################################
#
# Check for valid choice of BALANCE
#
##############################################################################

if ($?BALANCE == 0) set BALANCE=rand

switch ($BALANCE)
    case "acwn":
    case "rand":
    case "mngr":
    case "tok":
    case "btok":
    case "replay":
    case "recv":
        breaksw
    default:
        echo "charmc : Unknown load balancer $BALANCE"
        goto failure
endsw

if ( $BALANCE == btok ) then
        if ( ($QUEUE != bfifo) && ($QUEUE != bstack) ) then
                echo "WARNING : btok strategy needs bfifo or bstack queueing strategy"
        endif
endif


##############################################################################
#
# Check for valid choice of QMSTRAT
#
##############################################################################

if ($?QMSTRAT == 0) set QMSTRAT=default

switch ($QMSTRAT)
    case "default":
    case "simple":
    case "easy":
    case "replay":
        breaksw
    default:
        echo "charmc : Unknown queue manager $QMSTRAT"
        goto failure
endsw

##############################################################################
#
# Check for empty MAINOBJ and set to default
#
##############################################################################

if ($?MAINOBJ == 0) set MAINOBJ=$ROOTMACH/$EXECDIR/default_main.o

##############################################################################
#
# Load machine-specific configuration data.
#
##############################################################################

source $CHARMROOT/bin/$MACHTYPE.machine.script

# The following switch should not have to be here.

switch ($MACHTYPE)
	case 'solaris-threads':
	case 'uniprocessor-sun':
	case 'uniprocessor-solaris':
	case 'uniprocessor-hp':
	case 'uniprocessor-rs6000':
	case 'sequent':
	case 'max':
	case 'balance':
	case 'fx2800':
            set SHARED=1
            breaksw
	case 'ipsc2':
	case 'i860':
	case 'i860R':
	case 'ncube2':
	case 'sp1':
	case 'ncube3':
	case 'cm5':
	case 'paragon':
	case 'sunmos':
	case 'network-sun':
	case 'network-hp':
	case 'network-rs6000':
	case 'network-solaris':
            set SHARED=0
            breaksw
endsw

# The following switch should not have to be here.

switch ($MACHTYPE)
        case 'network-sun':
        case 'network-solaris':
        case 'network-rs6000':
        case 'network-hp':
        case 'i860R':
        case 'ncube2':
        case 'ipsc2':
            set HAVEHOST=1
            breaksw
        default:
            set HAVEHOST=0
            breaksw
endsw

##############################################################################
#
# Verify correctness of machine-script.
#
##############################################################################


if ($?NODE_LOADS == 0) then
    echo "Configurarion file must set NODE_LOADS"
    goto failure
endif

if ($?NODE_CC == 0) then
    echo "Configuration file must set NODE_CC"
    goto failure
endif

if ($?NODE_CPLUS == 0) then
    echo "Configuration file must set NODE_CPLUS"
    goto failure
endif

if ($?NODE_LD == 0) then
    echo "Configuration file must set NODE_LD"
    goto failure
endif

if ($?NODE_LDPLUS == 0) then
    echo "Configuration file must set NODE_LDPLUS"
    goto failure
endif

set CC = ($NODE_CC)
set CPLUS = ($NODE_CPLUS)
set LD = ($NODE_LD)
set LDPLUS = ($NODE_LDPLUS)

##############################################################################
#
# Handle overrides
#
##############################################################################

if ($?OVERRIDE_CC) then
    set CC[1]=""
    set CC = ( $OVERRIDE_CC $CC )
endif

if ($?OVERRIDE_CPLUS) then
    set CPLUS[1]=""
    set CPLUS = ( $OVERRIDE_CPLUS $CPLUS )
endif

if ($?OVERRIDE_LD) then
    set LD[1]=""
    set LD = ( $OVERRIDE_LD $LD )
endif

if ($?OVERRIDE_LD) then
    set LDPLUS[1]=""
    set LDPLUS = ( $OVERRIDE_LD $LDPLUS )
endif

##############################################################################
#
# Compile all specified files
#
# All temporary files named *.TMP.* for easy cleanup.
#
##############################################################################

foreach FILE ($FILES)
    set BASE=$FILE:r
    set TMP=$BASE:t.TMP
    if ($?OBJECT == 0) set OBJECT=$BASE:t
    if (${BASE}.o != $FILE) then
        echo "compiling $FILE for machine=$MACHTYPE"
        /bin/rm -f $BASE.o $BASE.f.o
    endif
    switch ($FILE)
        case "*.p"
            if ($?VERBOSE) echo "$CPP $OPTS_CPP -I$CHARMROOT/$MACHTYPE/include $BASE.p > $TMP.cpp"
            $CPP $OPTS_CPP -I$CHARMROOT/$MACHTYPE/include $BASE.p > $TMP.cpp
            if ($?VERBOSE) echo "$CHARMROOT/bin/spacefilter.$MACHTYPE < $TMP.cpp | $M4 > $TMP.space"
            $CHARMROOT/bin/spacefilter.$MACHTYPE < $TMP.cpp | $M4 > $TMP.space
            if ($?VERBOSE) echo "$CHARMROOT/bin/translate.$MACHTYPE $TMP.space $TMP.c"
            $CHARMROOT/bin/translate.$MACHTYPE $TMP.space $TMP.c
            if ($status) goto failure
            if ($?VERBOSE) echo "$CC $OPTS_CC $TMP.c -o $TMP.o"
            $CC $OPTS_CC $TMP.c -o $TMP.o
            if ($status) goto failure
            if ($?NOLINK) then
                if ($?VERBOSE) echo "/bin/mv $TMP.o $BASE.o"
                /bin/mv $TMP.o $BASE.o
            else
                set OBJECTFILES=( $OBJECTFILES $TMP.o )
            endif
            breaksw
        case "*.P"
            echo 'extern "C" {' > $TMP.P.c
            echo '#include <stdio.h>' >> $TMP.P.c
            echo '}' >> $TMP.P.c
            echo '#line 1 "'$BASE.P'"' >> $TMP.P.c
            /bin/cat $BASE.P >> $TMP.P.c
            if ($?VERBOSE) echo "$CPLUSPP $OPTS_CPP -I$CHARMROOT/$MACHTYPE/include $TMP.P.c > $TMP.cpp"
            $CPLUSPP $OPTS_CPP -I$CHARMROOT/$MACHTYPE/include $TMP.P.c > $TMP.cpp
            if ($?VERBOSE) echo "$CHARMROOT/bin/spacefilter++.$MACHTYPE $TMP.cpp | $M4 > $TMP.space"
            $CHARMROOT/bin/spacefilter++.$MACHTYPE $TMP.cpp | $M4 > $TMP.space
            if ($?VERBOSE) echo "$CHARMROOT/bin/translate++.$MACHTYPE $TMP.space $TMP.C"
            $CHARMROOT/bin/translate++.$MACHTYPE $TMP.space $TMP.C
            if ($status) goto failure
            if ($?VERBOSE) echo "$CPLUS $OPTS_CPLUS $TMP.C -o $TMP.o"
            $CPLUS $OPTS_CPLUS $TMP.C -o $TMP.o
            if ($status) goto failure
            if ($?NOLINK) then
                if ($?VERBOSE) echo "/bin/mv $TMP.o $BASE.o"
                /bin/mv $TMP.o $BASE.o
            else
                set OBJECTFILES=( $OBJECTFILES $TMP.o )
            endif
            breaksw
        case "*.c"
            echo "compiling $FILE for machine=$MACHTYPE"
            if ($?VERBOSE) echo "$CC $OPTS_CC $BASE.c -o $BASE.o"
            $CC $OPTS_CC $BASE.c -o $BASE.o
            if ($status) goto failure
            if ($?NOLINK == 0) then
                if ($?VERBOSE) echo "/bin/mv $BASE.o $TMP.o"
                /bin/mv $BASE.o $TMP.o
                set OBJECTFILES=( $OBJECTFILES $TMP.o )
            endif
            breaksw
        case "*.C"
            echo "compiling $FILE for machine=$MACHTYPE"
            if ($?VERBOSE) echo "$CPLUS $OPTS_CPLUS $BASE.c -o $BASE.o"
            $CPLUS $OPTS_CPLUS $BASE.C -o $BASE.o
            if ($status) goto failure
            if ($?NOLINK == 0) then
                if ($?VERBOSE) echo "/bin/mv $BASE.o $TMP.o"
                /bin/mv $BASE.o $TMP.o
                set OBJECTFILES=( $OBJECTFILES $TMP.o )
            endif
            breaksw
        case "*.f"
        case "*.F"
        case "*.fc"
        case "*.FC"
            echo "charmc: I'm not yet smart enough to compile $FILE"
            goto failure
        case "*.o"
            set OBJECTFILES=( $OBJECTFILES $BASE.o )
            breaksw
        default:
            echo "file with unrecognized extension $FILE"
            goto failure
    endsw
end


##############################################################################
#
# Link, if so requested, and fetch charmhost (if any).
#
##############################################################################

if ($?NOLINK) goto success


if ( $CONVERSE ) then
    set OBJECTFILES= ( $OBJECTFILES \
			"$ROOTMACH/$EXECDIR/converse.o" \
			"$ROOTMACH/$EXECDIR/fifo.o" \
			"$ROOTMACH/$EXECDIR/ck_objdir/machine.o" \
        		"$ROOTMACH/$EXECDIR/qs_objdir/n_qs_$QUEUE.o"  \
		        "$ROOTMACH/$EXECDIR/ck_objdir/$LOGEXEC.o"  \
        		"$ROOTMACH/$EXECDIR/mem_objdir/n_mem_$MEMORY.o"  \
#        		"$ROOTMACH/$EXECDIR/ldb_objdir/n_ldb.$BALANCE.o"  \
        		$NODE_LOADS \
		     )
    switch ($LANGUAGE)
    case "charm":
	set LOADER="$LD $OPTS_LD"
        breaksw
    case "charm++":
	set LOADER="$LDPLUS $OPTS_LD"
	breaksw
    endsw

    goto link

endif




set MODFILE = module_init_fn

if(-e $MODFILE.c) mv $MODFILE.c $MODFILE.bak

if(-e module.list) then
	set MOD_LIST = module.list
endif
# If the shell variable MODULE_LIST carries a value
if($?MODULE_LIST != 0)	then
	set MOD_LIST = $MODULE_LIST
endif
# If a modules list file was specified in the charmc options
if($?MOD_FILE_NAME != 0) then
	set MOD_LIST = $MOD_FILE_NAME
endif


if($?ACT_MOD_LIST != 0) then

	echo Using the module list : \"$ACT_MOD_LIST\"

        echo " " > $MODFILE.c
        foreach i ( $ACT_MOD_LIST)
                echo 'extern void _CK_'$i'_init();' >> $MODFILE.c
        end
	echo '_CK_module_init_fn()' >> $MODFILE.c
	echo '{' >> $MODFILE.c
	foreach i ( $ACT_MOD_LIST)
		echo '      _CK_'$i'_init();' >> $MODFILE.c
	end
	echo '}' >> $MODFILE.c

else if($?MOD_LIST) then
# No list of modules was specified in the charmc options
	
	echo Using module list in file \"$MOD_LIST\"
	if(!(-e $MOD_LIST)) (echo $MOD_LIST does not exist )#; exit)

        echo " " > $MODFILE.c
        foreach i ( `cat "$MOD_LIST"` )
                echo 'extern void _CK_'$i'_init();' >> $MODFILE.c
        end
	echo '_CK_module_init_fn()' >> $MODFILE.c
	echo '{' >> $MODFILE.c
	foreach i ( `cat "$MOD_LIST"` )
		echo '        _CK_'$i'_init();' >> $MODFILE.c
	end
	echo '}' >> $MODFILE.c

endif

if(-e $MODFILE.c) then
	$CC -c $MODFILE.c
else
	echo "WARNING : File with module init functions not created"
endif

# Uncomment the below line when the module init functions have been implemented
if (-e $MODFILE.o) set OBJECTFILES = ($OBJECTFILES $MODFILE.o)
 
echo "linking $OBJECT with -language $LANGUAGE -machine $MACHTYPE"

switch ($LANGUAGE)
    case "charm":
        set OBJECTFILES= ( $OBJECTFILES "$ROOTMACH/$EXECDIR/ck_objdir/dummy_cplus.o" )
	if($?PURIFY) then
	    set LOADER="purify $LD $OPTS_LD"
	else
	    set LOADER="$LD $OPTS_LD"
	endif
        breaksw
    case "charm++":
        set OBJECTFILES= ( $OBJECTFILES "$ROOTMACH/$EXECDIR/ck_objdir/cplus.o" )
	if($?PURIFY) then
	    set LOADER="purify $LDPLUS $OPTS_LD"
	else
	    set LOADER="$LDPLUS $OPTS_LD"
	endif
	breaksw
endsw

if ($SHARED) then
    set OBJECTFILES= ( $OBJECTFILES \
        "$ROOTMACH/$EXECDIR/ck_objdir/ckobj.o" \
        "$ROOTMACH/$EXECDIR/ck_objdir/$LOGEXEC.o"  \
        "$ROOTMACH/$EXECDIR/ck_objdir/obj_$EXTRAEXEC.o"  \
        "$ROOTMACH/$EXECDIR/mem_objdir/mem_$MEMORY.o"  \
        "$ROOTMACH/$EXECDIR/qs_objdir/qs_$QUEUE.o"  \
        $NODE_LOADS )
else
    set OBJECTFILES= ( $OBJECTFILES \
	"$ROOTMACH/$EXECDIR/converse.o" \
	"$ROOTMACH/$EXECDIR/fifo.o" \
	"$ROOTMACH/$EXECDIR/ck_objdir/machine.o" \
        "$ROOTMACH/$EXECDIR/ck_objdir/$LOGEXEC.o"  \
        "$ROOTMACH/$EXECDIR/mem_objdir/n_mem_$MEMORY.o"  \
        "$ROOTMACH/$EXECDIR/qs_objdir/n_qs_$QUEUE.o"  \
        "$ROOTMACH/$EXECDIR/ck_objdir/ckobj.node.o"  \
        "$ROOTMACH/$EXECDIR/ck_objdir/node_$EXTRAEXEC.o"  \
        "$ROOTMACH/$EXECDIR/ldb_objdir/n_ldb.$BALANCE.o"  \
#        "$ROOTMACH/$EXECDIR/node_qm_$QMSTRAT.o"  \
	$MAINOBJ \
        $NODE_LOADS )
endif


link:

if ($?VERBOSE) echo "$LOADER -o $OBJECT $OBJECTFILES -L$ROOTMACH/userlib $LIBRARIES -lm"
$LOADER -o $OBJECT $OBJECTFILES -L$ROOTMACH/userlib $LIBRARIES -lm
if ($status) goto failure

if ($HAVEHOST) then
    /bin/rm -f charmhost
    /bin/ln -s $CHARMROOT/$MACHTYPE/$EXECDIR/charmhost charmhost
endif

goto success

########################################################################
#
# Clean up files and exit
#
########################################################################


success:
    set STATUS=0
    goto end

failure:
    echo 'charmc: aborting.'
    set STATUS=1
    goto end

end:
    if ($?SAVE == 0) then
        foreach FILE ($FILES)
            set BASE=$FILE:r
            set TMP=$BASE:t.TMP
            /bin/rm -f $TMP.P.c $TMP.C $TMP.cpp $TMP.space $TMP.o
            /bin/rm -f $TMP.c $TMP.c.0.h $TMP.c.1.h $TMP.c.2.h $BASE:t.headers
        end
    endif
    exit ($STATUS)
