#! /bin/sh
#
# Script to compile and link MPI programs
# We'd like to just add a few libraries to the link line and an include
# directory to the compile line, but some systems that MPICH supports make
# that difficult because the compiler and the linker are different programs.
# Thus, this code tries to figure out which options go where, at least for
# things that are different between compile and link steps.
#
DoLink=1
DoCompile=0
MPILOG=
Show=eval
allargs=
compileargs=
linkargs=
linkobjs=
gettinglinkarg=0
HasDashC=0
UsesPmpi=0
verbose=0
#proflib=-lp@MPILIBNAME@
#proflibfullname=@MPIR_HOME@/build/@ARCH@/@DEVICE@/lib/libp@MPILIBNAME@.a
#CCBASE="@CC@"
CC=icc
#CC=MPI_C_COMPILER
#CLINKER=MPI_C_LINKER
#CLINKERBASE="@CLINKER@"
CLINKER=icpc
for arg in "$@" ; do
#    echo procssing arg $arg \( $compileargs \)
    # Special processing for -o name
    if [ $gettinglinkarg = 1 ] ; then
	linkargs="$linkargs $arg"
	outputfilename="$arg"
	gettinglinkarg=0
	continue
    fi
    case "$arg" in 
	-c)
	# If -c is NOT specified, then we need to perform a link step.
	allargs="$allargs $arg"
	compileargs="$compileargs $arg"
        # If -o was set, then we need to move the output file option
        # to the compile line (note that this is non-standard, and should
        # not be used in portable codes)
        if [ $DoLink = 1 -a -n "$outputfilename" ] ; then
	    compileargs="$compileargs -o $outputfilename"
        fi
	DoLink=0
	HasDashC=1
	;;
        -o)
	# Need to link
	allargs="$allargs $arg"
        if [ $HasDashC = 1 ] ; then
            # Some BUT NOT ALL compilers support -o with -c.  Allow
            # the user to make use of the feature, IF IT EXISTS.
            compileargs="$compileargs $arg"	
        else
	    linkargs="$linkargs $arg"
	    # Still need to add the target of the -o
	    gettinglinkarg=1
	    DoLink=1
        fi
	;;
	-l*)
	# This SHOULD be the -l<lib> argument.  Only for the linker
	linkargs="$linkargs $arg"
	allargs="$allargs $arg"
	;;
	
	-E|-M)
	# For compilers that support -E as a way to get at the C preprocessor
	# Also, for compilers that support -M for creating dependencies
	allargs="$allargs $arg"
	compileargs="$compileargs $arg"
	DoLink=0
	;;

	-echo)
	set -x
	;;
	-show)
	Show=echo
	;;
	-v)
	verbose=1
	compileargs="$compileargs -v"
	linkargs="$linkargs -v"
	;;
	-help)
	echo "This is a program to compile or link MPI programs"
	echo "In addition, the following special options are supported"
	echo "    -show      - Show the commands that would be used without"
	echo "                 runnning them"
	echo "    -help      - Give this help"
	echo "    -echo      - Show exactly what this program is doing."
	echo "                 This option should normally not be used."
	echo "This should be used just like the usual C compiler"
	echo "For example,"
	echo "   $0 -c foo.c "
	echo "and"
	echo "   $0 -o foo foo.o"
	echo "Combining compilation and linking in a single command"
	echo "   $0 -o foo foo.c"
	echo "may not work on some systems, and is not recommended."
	exit 1
	;;
        *\"*) 
	allargs="$allargs `echo $arg | sed 's/\"/\\\"/g'`"
	compileargs="$compileargs `echo $arg | sed 's/\"/\\\"/g'`"
	linkargs="$linkargs `echo $arg | sed 's/\"/\\\"/g'`"
	;;
        *) allargs="$allargs $arg"
	if [ -s "$arg" ] ; then
	    ext=`expr "$arg" : '.*\(\..*\)'`
	    if [ "$ext" = ".c" ] ; then
	        DoCompile=1
	        compileargs="$compileargs $arg"
	        fname=`basename $arg .c`
	        linkobjs="$linkobjs $fname.o"
	    elif [ "$ext" = ".o" ] ; then
		if [ $HasDashC = 1 ] ; then
	            compileargs="$compileargs $arg"
                else
	            DoLink=1
	            linkobjs="$linkobjs $arg"
                fi
	    else
	        compileargs="$compileargs $arg"
	        linkargs="$linkargs $arg"
	    fi
	else
            compileargs="$compileargs $arg"
	    linkargs="$linkargs $arg"
	fi
	;;
    esac
done
#
status=0
if [ $DoCompile = 1 ] ; then 
    if [ $HasDashC != 1 ] ; then
        compileargs="-c $compileargs"
        #compileargs="$compileargs"
    fi
    $Show $CC -D_REENTRANT -I/opt/scali/include $compileargs
    status=$?
    if [ $status != 0 ] ; then 
	exit $status
    fi
fi
#
# Here's a tricky issue:  If there is no mpi library yet, we can't link,
# but if we are building other tools that need mpicc, they may need to 
# build and run non-mpi programs.  This is currently handled in the configure
# by making a dummy library.
# 
if [ $DoLink = 1 ] ; then
    # If the profiling library doesn't exist, or MPICH_NO_PROF environment
    # variable is set, skip the profiling library.
    if [ -n "$MPICH_NO_PROF" -o ! -s "$proflibfullname" ] ; then
        proflib=""
    fi
    if [ -n "$MPILOG" ] ; then
        $Show $CLINKER -L/opt/scali/lib $linkobjs $MPILOG $linkargs -lmpi
    else
	# linkargs needs to be after linkobjs for things like "-lm"
        $Show $CLINKER -L/opt/scali/lib $linkobjs $linkargs $proflib -lmpi
    fi
    status=$?
fi
exit $status
