#! /bin/sh
#
# mpicxx
# Simple script to compile and/or link MPI programs.
# This script knows the default flags and libraries, and can handle
# alternative C++ compilers and the associated flags and libraries.
# The important terms are:
#    includedir, libdir - Directories containing an *installed* mpich2
#    prefix, execprefix - Often used to define includedir and libdir
#    CXX                - C compiler
#    MPI_CXXFLAGS       - Any special flags needed to compile 
#    MPI_LDFLAGS        - Any special flags needed to link
#    MPILIBNAME         - Name of the MPI library
#    MPICXXLIBNAME      - Name of the C++ binding part of the MPI library
#    MPI_OTHERLIBS      - Other libraries needed in order to link 
#    
# We assume that (a) the C++ compiler can both compile and link programs
# We use MPI_xxx so that the user may continue to use CXXFLAGS, LIBS, etc
# to modify the behavior of the compiler and linker.
#
# Handling of command-line options:
#   This is a little tricky because some options may contain blanks.
#
# Special issues with shared libraries - todo
#
# --------------------------------------------------------------------------
# Set the default values of all variables.
#
# Directory locations: Fixed for any MPI implementation
prefix=/proj/tpfimos/larour/Libraries/linux/mpich2-gnu/
exec_prefix=${prefix}
sysconfdir=${prefix}/etc
includedir=${prefix}/include
libdir=${exec_prefix}/lib
#
# Default settings for compiler, flags, and libraries
CXX="c++"
CXX_LINKPATH_SHL="-Wl,-rpath -Wl,"
MPI_CXXFLAGS=""
MPI_LDFLAGS=" "
MPILIBNAME="mpich"
PMPILIBNAME="pmpich"
MPICXXLIBNAME="mpichcxx"
MPI_OTHERLIBS="       -lpthread  -lrt   "
NEEDSPLIB="no"
# MPIVERSION is the version of the MPICH2 library that mpicxx is intended for
MPIVERSION="1.0.2"
#
# Internal variables
# Show is set to echo to cause the compilation command to be echoed instead 
# of executed.
Show=eval
#
# End of initialization of variables
#---------------------------------------------------------------------
# Environment Variables.
# The environment variables MPICH_CXX may be used to override the 
# default choices.
# In addition, if there is a file $sysconfdir/mpicxx-$CXXname.conf, 
# where CXXname is the name of the compiler with all spaces replaced by hyphens
# (e.g., "CC -64" becomes "CC--64", that file is sources, allowing other
# changes to the compilation environment.  See the variables used by the 
# script (defined above)
if [ -n "$MPICH_CXX" ] ; then
    CXX="$MPICH_CXX"
    CXXname=`echo $CXX | sed 's/ /-/g'`
    if [ -s $sysconfdir/mpicxx-$CXXname.conf ] ; then
        . $sysconfdir/mpicxx-$CXXname.conf
    fi
fi
#
# ------------------------------------------------------------------------
# Argument processing.
# This is somewhat awkward because of the handling of arguments within
# the shell.  We want to handle arguments that include spaces without 
# loosing the spacing (an alternative would be to use a more powerful
# scripting language that would allow us to retain the array of values, 
# which the basic (rather than enhanced) Bourne shell does not.  
#
# Look through the arguments for arguments that indicate compile only.
# If these are *not* found, add the library options

linking=yes
allargs=""
for arg in "$@" ; do
    # Set addarg to no if this arg should be ignored by the C compiler
    addarg=yes
    qarg=$arg
    case $arg in 
 	# ----------------------------------------------------------------
	# Compiler options that affect whether we are linking or no
    -c|-S|-E|-M|-MM)
    # The compiler links by default
    linking=no
    ;;
	# ----------------------------------------------------------------
	# Options that control how we use mpicxx (e.g., -show, 
	# -cxx=* -config=*
    -echo)
    addarg=no
    set -x
    ;;
    -cxx=*)
    CXX=`echo A$arg | sed -e 's/A-cxx=//g'`
    addarg=no
    ;;
    -show)
    addarg=no
    Show=echo
    ;;
    -config=*)
    addarg=no
    CXXname=`echo A$arg | sed -e 's/A-config=//g'`
    if [ -s "$sysconfdir/mpicxx-$CXXname.conf" ] ; then
        . "$sysconfdir/mpicxx-$CXXname.conf"
    else
	echo "Configuration file mpicxx-$CXXname.conf not found"
    fi
    ;;
    -compile-info|-compile_info)
    # -compile_info included for backward compatibility
    Show=echo
    addarg=no
    ;;
    -link-info|-link_info)
    # -link_info included for backward compatibility
    Show=echo
    addarg=no
    ;;
    -v)
    # Pass this argument to the compiler as well.
    echo "mpicxx for $MPIVERSION"
    ;;
	# -help
    -help)
    ;;
        # -----------------------------------------------------------------
	# Other arguments.  We are careful to handle arguments with 
	# quotes (we try to quote all arguments in case they include 
	# any spaces)
    *\"*) 
    qarg="'"$arg"'"
    ;;
    *\'*) 
    qarg='\"'"$arg"'\"'
    ;;
    *)
    ;;

    esac
    if [ $addarg = yes ] ; then
        allargs="$allargs $qarg"
    fi
done

# -----------------------------------------------------------------------
# Derived variables.  These are assembled from variables set from the
# default, environment, configuration file (if any) and command-line
# options (if any)
if [ "$NEEDSPLIB" = yes ] ; then
    mpilibs="-l$PMPILIBNAME -l$MPILIBNAME"
else
    mpilibs="-l$MPILIBNAME"
fi
cxxlibs=
if [ "$MPICXXLIBNAME" != "$MPILIBNAME" ] ; then
    cxxlibs="-l$MPICXXLIBNAME"
fi
#
# Concatenate the user flags with the ones needed by MPI
CXXFLAGS="$CXXFLAGS $MPI_CXXFLAGS"
LDFLAGS="$LDFLAGS $MPI_LDFLAGS"
#
# A temporary statement to invoke the compiler
# Place the -L before any args incase there are any mpi libraries in there.
# Eventually, we'll want to move this after any non-MPI implementation 
# libraries
if [ "$linking" = yes ] ; then
    if [ -n "$CXX_LINKPATH_SHL" ] ; then
        # Prepend the path for the shared libraries to the library list
        shllibpath="$CXX_LINKPATH_SHL$libdir"
    fi
    $Show $CXX $CXXFLAGS $LDFLAGS $allargs -I$includedir -L$libdir $shllibpath $cxxlibs $mpilibs $MPI_OTHERLIBS
    rc=$?
else
    $Show $CXX $CXXFLAGS $allargs -I$includedir
    rc=$?
fi

exit $rc

