#! /bin/sh
#
# mpif90
# 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
#    F90                - Fortran 90 compiler
#    MPI_FFLAGS         - Any special flags needed to compile 
#    MPI_LDFLAGS        - Any special flags needed to link
#    MPILIBNAME         - Name 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 CFLAGS, 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.
# Set from the directory arguments to configure (e.g., --prefix=/usr/local)
prefix=/proj/tpfimos/larour/Libraries/linux/mpich2-gnu/
exec_prefix=${prefix}
sysconfdir=${prefix}/etc
includedir=${prefix}/include
modincdir=${prefix}/include
libdir=${exec_prefix}/lib
#
# Default settings for compiler, flags, and libraries
# Determined by a combination of environment variables and tests within
# configure (e.g., determining whehter -lsocket is needee)
F90="f95"
F90CPP=""
#
# Fortran 90 Compiler characteristics
F90INC="-I"
# f90modinc specifies how to add a directory to the search path for modules.
# Some compilers (Intel ifc version 5) do not support this concept, and 
# instead need 
# a specific list of files that contain module names and directories.
# The F90MODINCSPEC is a more general approach that uses <dir> and <file>
# for the directory and file respectively.
F90MODINC="-I"
F90MODINCSPEC=""
F90EXT="f90"
#
MPI_F90FLAGS=""
MPI_LDFLAGS=" "
MPILIBNAME="mpich"
PMPILIBNAME="pmpich"
MPI_OTHERLIBS="       -lpthread  -lrt   "
NEEDSPLIB="no"
# MPIVERSION is the version of the MPICH2 library that mpicc 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_F90 may be used to override the 
# default choices.
# In addition, if there is a file $sysconfdir/mpif90-$F90name.conf, 
# where F90name is the name of the compiler with all spaces replaced by hyphens
# (e.g., "f90 -64" becomes "f90--64", that file is sources, allowing other
# changes to the compilation environment.  See the variables used by the 
# script (defined above)
if [ -n "$MPICH_F90" ] ; then
    F90="$MPICH_F90"
    F90name=`echo $F90 | sed 's/ /-/g'`
    if [ -s $sysconfdir/mpif90-$F90name.conf ] ; then
        . $sysconfdir/mpif90-$F90name.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 mpif90 (e.g., -show, 
	# -f90=* -config=*
    -echo)
    addarg=no
    set -x
    ;;
    -f90=*)
    F90=`echo A$arg | sed -e 's/A-f90=//g'`
    addarg=no
    ;;
    -show)
    addarg=no
    Show=echo
    ;;
    -config=*)
    addarg=no
    F90name=`echo A$arg | sed -e 's/A-config=//g'`
    if [ -s "$sysconfdir/mpif90-$F90name.conf" ] ; then
        . "$sysconfdir/mpif90-$F90name.conf"
    else
	echo "Configuration file mpif90-$F90name.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 "mpif90 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"'"
    case $arg in 
       -D*)
       cppflags="$cppflags $qarg"
       ;;
    esac
    ;;
    *\'*) 
    qarg='\"'"$arg"'\"'
    case $arg in 
       -D*)
       cppflags="$cppflags $qarg"
       ;;
    esac
    ;;
    # The following are special args used to handle .F files when the
    # Fortran compiler itself does not handle these options
    -I*)
    cppflags="$cppflags $arg"
    ;;
    -D*)
    cppflags="$cppflags $arg"
    ;;
    *.F|*.F90)
# If F90CPP is not empty, then we need to do the following:
#    If any input files have the .F or .F90 extension, then    
#        If F90CPP = false, then
#            generate an error message and exit
#        Use F90CPP to convert the file from .F to .f, using 
#            $TMPDIR/f$$-$count.f as the output file name
#            Replace the input file with this name in the args
# This is needed only for very broken systems
#     
    if [ -n "$F90CPP" ] ; then
        if [ "$F90CPP" = "false" ] ; then
            echo "This Fortran compiler does not accept .F or .F90 files"
	    exit 1
        fi
        addarg=no
	# Remove and directory names
        bfile=`basename $arg`
	# Now remove the .F or .F90
	bfile1=`basename $bfile .F90`
	if [ "$bfile1" = "$bfile" ] ; then
	    bfile1=`basename $bfile .F`
        fi
	$bfile=$bfile1
	# 
	TMPDIR=${TMPDIR:-/tmp}
	# Make sure that we use a valid extension for the temp file.
        tmpfile=$TMPDIR/f$$-$bfile.$F90EXT
        if $F90CPP $cppflags $arg > $tmpfile ; then
            # Add this file to the commandline list
	    count=`expr $count + 1`
	    allargs="$allargs $tmpfile"
	    rmfiles="$rmfiles $tmpfile"
        else
	    echo "Aborting compilation because of failure in preprocessing step"
	    echo "for file $arg ."
	    exit 1
        fi
    fi
    # Otherwise, just accept the argument
    ;;
    # - end of special handling for .F files

    *)
    ;;

    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)

#
# The library lib${MPILIBNAME}f90 contains the f90-specific features,
# such as the module objects and the routines defined by them
# (MPI_SIZEOF is handled in lib${MPILIBNAME)f90, for example).
if [ "$NEEDSPLIB" = yes ] ; then
    mpilibs="-l${MPILIBNAME}f90 -l$PMPILIBNAME -l$MPILIBNAME"
else
    mpilibs="-l${MPILIBNAME}f90 -l$MPILIBNAME"
fi
#
# Concatenate the user flags with the ones needed by MPI
F90FLAGS="$F90FLAGS $MPI_F90FLAGS"
LDFLAGS="$LDFLAGS $MPI_LDFLAGS"

# Construct the line to add the include directory (not all compilers 
# use -I, unfortunately)
if [ -z "${F90INC}" ] ; then
    # If there is no path, add a link to the mpif.h file.
    # There *must* be a way to provide the path the any modules (there
    # may be too many to link)
    if [ ! -r mpif.h ] ; then
        #echo "Adding a symbolic link for mpif.h"
	trap "$Show /bin/rm -f mpif.h" 0
	# This should really be the (related) f77includedir (see mpif77).
	$Show ln -s ${includedir}/mpif.h mpif.h
	# Remember to remove this file
	rmfiles="$rmfiles mpif.h"
    fi
    F90INCDIRS=
else
    # Normally, F90INC is just -I, but some compilers have used different
    # command line arguments
    F90INCDIRS=${F90INC}${includedir}
fi

# Handle the specification of the directory containing the modules
# For now, these are in the includedir (no choice argument supported)
moduledir=$modincdir
modulelib=${MPILIBNAME}f90
if [ -n "$F90MODINCSPEC" ] ; then
    newarg=`echo A"$F90MODINCSPEC" | \
	sed -e 's/^A//' -e 's%<dir>%'"$moduledir%g" -e 's/<file>/mpi/g'`
    F90MODDIRS="$newarg"
    F90MODLIBS="-l$modulelib"
elif [ -n "$F90MODINC" ] ; then
    F90MODDIRS="${F90MODINC}$moduledir"
    F90MODLIBS="-l$modulelib"
fi

#
# 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
    $Show $F90 $F90FLAGS $LDFLAGS $allargs $F90INCDIRS $F90MODDIRS -L$libdir $F90MODLIBS $mpilibs $MPI_OTHERLIBS
    rc=$?
else
    $Show $F90 $F90FLAGS $allargs $F90INCDIRS $F90MODDIRS
    rc=$?
fi
if [ -n "$rmfiles" ] ; then
    for file in $rmfiles ; do
        objfile=`basename $file .f`
	if [ -s "${objfile}.o" ] ; then
	    # Rename 
	    destfile=`echo $objfile | sed -e "s/.*$$-//"`
	    mv -f ${objfile}.o ${destfile}.o
	fi
        rm -f $file
    done
    rm -f $rmfiles
fi
exit $rc

