Blame src/env/mpifort.bash.in

Packit 0848f5
#! @BASH_SHELL@
Packit 0848f5
#
Packit 0848f5
# (C) 2006 by Argonne National Laboratory.
Packit 0848f5
#     See COPYRIGHT in top-level directory.
Packit 0848f5
#
Packit 0848f5
# mpifort
Packit 0848f5
# Simple script to compile and/or link MPI programs.
Packit 0848f5
# This script knows the default flags and libraries, and can handle
Packit 0848f5
# alternative C compilers and the associated flags and libraries.
Packit 0848f5
# The important terms are:
Packit 0848f5
#    includedir, libdir - Directories containing an *installed* mpich
Packit 0848f5
#    prefix, execprefix - Often used to define includedir and libdir
Packit 0848f5
#    FC                 - Fortran 90 compiler
Packit 0848f5
#    WRAPPER_FCFLAGS    - Any special flags needed to compile
Packit 0848f5
#    WRAPPER_LDFLAGS    - Any special flags needed to link
Packit 0848f5
#    WRAPPER_LIBS       - Any special libraries needed in order to link
Packit 0848f5
#    FC_OTHER_LIBS      - Yet more libraries, needed just with FC
Packit 0848f5
#
Packit 0848f5
# We assume that (a) the C compiler can both compile and link programs
Packit 0848f5
#
Packit 0848f5
# Handling of command-line options:
Packit 0848f5
#   This is a little tricky because some options may contain blanks.
Packit 0848f5
#
Packit 0848f5
# Special issues with shared libraries - todo
Packit 0848f5
#
Packit 0848f5
# --------------------------------------------------------------------------
Packit 0848f5
# Set the default values of all variables.
Packit 0848f5
#
Packit 0848f5
# Directory locations: Fixed for any MPI implementation.
Packit 0848f5
# Set from the directory arguments to configure (e.g., --prefix=/usr/local)
Packit 0848f5
prefix=__PREFIX_TO_BE_FILLED_AT_INSTALL_TIME__
Packit 0848f5
exec_prefix=__EXEC_PREFIX_TO_BE_FILLED_AT_INSTALL_TIME__
Packit 0848f5
sysconfdir=__SYSCONFDIR_TO_BE_FILLED_AT_INSTALL_TIME__
Packit 0848f5
includedir=__INCLUDEDIR_TO_BE_FILLED_AT_INSTALL_TIME__
Packit 0848f5
libdir=__LIBDIR_TO_BE_FILLED_AT_INSTALL_TIME__
Packit 0848f5
modincdir=@modincdir@
Packit 0848f5
Packit 0848f5
# Default settings for compiler, flags, and libraries
Packit 0848f5
# Determined by a combination of environment variables and tests within
Packit 0848f5
# configure (e.g., determining whehter -lsocket is needee)
Packit 0848f5
FC="@FC@"
Packit 0848f5
FCCPP="@FCCPP@"
Packit 0848f5
#
Packit 0848f5
# Fortran 90 Compiler characteristics
Packit 0848f5
FCINC="@FCINC@"
Packit 0848f5
# f90modinc specifies how to add a directory to the search path for modules.
Packit 0848f5
# Some compilers (Intel ifc version 5) do not support this concept, and
Packit 0848f5
# instead need
Packit 0848f5
# a specific list of files that contain module names and directories.
Packit 0848f5
# The FCMODINCSPEC is a more general approach that uses <dir> and <file>
Packit 0848f5
# for the directory and file respectively.
Packit 0848f5
FCMODINC="@FCMODINCFLAG@"
Packit 0848f5
FCMODINCSPEC="@FCMODINCSPEC@"
Packit 0848f5
FCEXT="@FCEXT@"
Packit 0848f5
Packit 0848f5
MPICH_VERSION="@MPICH_VERSION@"
Packit 0848f5
Packit 0848f5
enable_wrapper_rpath="@enable_wrapper_rpath@"
Packit 0848f5
@fc_shlib_conf@
Packit 0848f5
Packit 0848f5
# Internal variables
Packit 0848f5
# Show is set to echo to cause the compilation command to be echoed instead
Packit 0848f5
# of executed.
Packit 0848f5
Show=
Packit 0848f5
#
Packit 0848f5
# End of initialization of variables
Packit 0848f5
#---------------------------------------------------------------------
Packit 0848f5
# Environment Variables.
Packit 0848f5
# The environment variables MPICH_FC may be used to override the
Packit 0848f5
# default choices.
Packit 0848f5
# In addition, if there is a file $sysconfdir/mpifort-$FCname.conf,
Packit 0848f5
# where FCname is the name of the compiler with all spaces replaced by hyphens
Packit 0848f5
# (e.g., "fc -64" becomes "fc--64", that file is sources, allowing other
Packit 0848f5
# changes to the compilation environment.  See the variables used by the
Packit 0848f5
# script (defined above)
Packit 0848f5
# Added MPICH_FC_OLD, MPICH_FC can be used to prefix FC with external utility,
Packit 0848f5
# e.g. setenv MPICH_FC 'eval linkcache $MPICH_FC_OLD'
Packit 0848f5
if [ -n "$MPICH_FC" ] ; then
Packit 0848f5
    MPICH_FC_OLD="$FC"
Packit 0848f5
    FC="$MPICH_FC"
Packit 0848f5
    FCname=`echo $FC | sed 's/ /-/g'`
Packit 0848f5
    if [ -s $sysconfdir/mpifort-$FCname.conf ] ; then
Packit 0848f5
        . $sysconfdir/mpifort-$FCname.conf
Packit 0848f5
    fi
Packit 0848f5
fi
Packit 0848f5
# Allow a profiling option to be selected through an environment variable
Packit 0848f5
if [ -n "$MPIFORT_PROFILE" ] ; then
Packit 0848f5
    profConf=$MPIFORT_PROFILE
Packit 0848f5
fi
Packit 0848f5
#
Packit 0848f5
# ------------------------------------------------------------------------
Packit 0848f5
# Argument processing.
Packit 0848f5
# This is somewhat awkward because of the handling of arguments within
Packit 0848f5
# the shell.  We want to handle arguments that include spaces without
Packit 0848f5
# loosing the spacing (an alternative would be to use a more powerful
Packit 0848f5
# scripting language that would allow us to retain the array of values,
Packit 0848f5
# which the basic (rather than enhanced) Bourne shell does not.
Packit 0848f5
#
Packit 0848f5
# Look through the arguments for arguments that indicate compile only.
Packit 0848f5
# If these are *not* found, add the library options
Packit 0848f5
Packit 0848f5
linking=yes
Packit 0848f5
allargs=("$@")
Packit 0848f5
argno=0
Packit 0848f5
cppflags=()
Packit 0848f5
interlib_deps=yes
Packit 0848f5
for arg in "$@" ; do
Packit 0848f5
    # Set addarg to no if this arg should be ignored by the C compiler
Packit 0848f5
    addarg=yes
Packit 0848f5
    case "$arg" in
Packit 0848f5
	# ----------------------------------------------------------------
Packit 0848f5
	# Compiler options that affect whether we are linking or no
Packit 0848f5
    -c|-S|-E|-M|-MM)
Packit 0848f5
    # The compiler links by default
Packit 0848f5
    linking=no
Packit 0848f5
    ;;
Packit 0848f5
	# ----------------------------------------------------------------
Packit 0848f5
	# Options that control how we use mpifort (e.g., -show,
Packit 0848f5
	# -fc=* -config=*
Packit 0848f5
    -static)
Packit 0848f5
    interlib_deps=no
Packit 0848f5
    ;;
Packit 0848f5
    -echo)
Packit 0848f5
    addarg=no
Packit 0848f5
    set -x
Packit 0848f5
    ;;
Packit 0848f5
    -fc=*)
Packit 0848f5
    FC=`echo A$arg | sed -e 's/A-fc=//g'`
Packit 0848f5
    addarg=no
Packit 0848f5
    ;;
Packit 0848f5
    -fc=*)
Packit 0848f5
    FC=`echo A$arg | sed -e 's/A-fc=//g'`
Packit 0848f5
    addarg=no
Packit 0848f5
    ;;
Packit 0848f5
    -show)
Packit 0848f5
    addarg=no
Packit 0848f5
    Show=echo
Packit 0848f5
    ;;
Packit 0848f5
    -config=*)
Packit 0848f5
    addarg=no
Packit 0848f5
    FCname=`echo A$arg | sed -e 's/A-config=//g'`
Packit 0848f5
    if [ -s "$sysconfdir/mpifort-$FCname.conf" ] ; then
Packit 0848f5
        . "$sysconfdir/mpifort-$FCname.conf"
Packit 0848f5
    else
Packit 0848f5
	echo "Configuration file mpifort-$FCname.conf not found"
Packit 0848f5
    fi
Packit 0848f5
    ;;
Packit 0848f5
    -compile-info|-compile_info)
Packit 0848f5
    # -compile_info included for backward compatibility
Packit 0848f5
    Show=echo
Packit 0848f5
    addarg=no
Packit 0848f5
    ;;
Packit 0848f5
    -link-info|-link_info)
Packit 0848f5
    # -link_info included for backward compatibility
Packit 0848f5
    Show=echo
Packit 0848f5
    addarg=no
Packit 0848f5
    ;;
Packit 0848f5
    -v)
Packit 0848f5
    # Pass this argument to the compiler as well.
Packit 0848f5
    echo "mpifort for MPICH version $MPICH_VERSION"
Packit 0848f5
    # if there is only 1 argument, it must be -v.
Packit 0848f5
    if [ "$#" -eq "1" ] ; then
Packit 0848f5
        linking=no
Packit 0848f5
    fi
Packit 0848f5
    ;;
Packit 0848f5
    -profile=*)
Packit 0848f5
    # Pass the name of a profiling configuration.  As
Packit 0848f5
    # a special case, lib<name>.so or lib<name>.la may be used
Packit 0848f5
    # if the library is in $libdir
Packit 0848f5
    profConf=`echo A$arg | sed -e 's/A-profile=//g'`
Packit 0848f5
    addarg=no
Packit 0848f5
    # Loading the profConf file is handled below
Packit 0848f5
    ;;
Packit 0848f5
    -nativelinking)
Packit 0848f5
    # Internal option to use native compiler for linking without MPI libraries
Packit 0848f5
    nativelinking=yes
Packit 0848f5
    addarg=no
Packit 0848f5
    ;;
Packit 0848f5
    -help)
Packit 0848f5
    NC=`echo "$FC" | sed 's%\/% %g' | awk '{print $NF}' -`
Packit 0848f5
    if [ -f "$sysconfdir/mpixxx_opts.conf" ] ; then
Packit 0848f5
        . $sysconfdir/mpixxx_opts.conf
Packit 0848f5
        echo "    -fc=xxx       - Reset the native compiler to xxx."
Packit 0848f5
    else
Packit 0848f5
        if [ -f "./mpixxx_opts.conf" ] ; then
Packit 0848f5
            . ./mpixxx_opts.conf
Packit 0848f5
            echo "    -fc=xxx       - Reset the native compiler to xxx."
Packit 0848f5
        fi
Packit 0848f5
    fi
Packit 0848f5
    exit 0
Packit 0848f5
    ;;
Packit 0848f5
    # The following are special args used to handle .F files when the
Packit 0848f5
    # Fortran compiler itself does not handle these options
Packit 0848f5
    -I*)
Packit 0848f5
    cppflags[${#cppflags}]="$arg"
Packit 0848f5
    ;;
Packit 0848f5
    -D*)
Packit 0848f5
    cppflags[${#cppflags}]="$arg"
Packit 0848f5
    ;;
Packit 0848f5
    *.F|*.F90|.fpp|.FPP)
Packit 0848f5
# If FCCPP is not empty, then we need to do the following:
Packit 0848f5
#    If any input files have the .F or .F90 extension, then
Packit 0848f5
#        If FCCPP = false, then
Packit 0848f5
#            generate an error message and exit
Packit 0848f5
#        Use FCCPP to convert the file from .F to .f, using
Packit 0848f5
#            $TMPDIR/f$$-$count.f as the output file name
Packit 0848f5
#            Replace the input file with this name in the args
Packit 0848f5
# This is needed only for very broken systems
Packit 0848f5
#
Packit 0848f5
    if [ -n "$FCCPP" ] ; then
Packit 0848f5
        if [ "$FCCPP" = "false" ] ; then
Packit 0848f5
            echo "This Fortran compiler does not accept .F or .F90 files"
Packit 0848f5
	    exit 1
Packit 0848f5
        fi
Packit 0848f5
        addarg=no
Packit 0848f5
	# Remove and directory names and extension
Packit 0848f5
	$ext=`expr "$arg" : '.*\(\..*\)'`
Packit 0848f5
        bfile=`basename $arg $ext`
Packit 0848f5
	#
Packit 0848f5
	TMPDIR=${TMPDIR:-/tmp}
Packit 0848f5
	# Make sure that we use a valid extension for the temp file.
Packit 0848f5
        tmpfile=$TMPDIR/f$$-$bfile.$FCEXT
Packit 0848f5
        if $FCCPP "${cppflags[@]}" $arg > $tmpfile ; then
Packit 0848f5
            # Add this file to the commandline list
Packit 0848f5
	    count=`expr $count + 1`
Packit 0848f5
	    allargs[${#allargs}]="$tmpfile"
Packit 0848f5
	    rmfiles="$rmfiles $tmpfile"
Packit 0848f5
        else
Packit 0848f5
	    echo "Aborting compilation because of failure in preprocessing step"
Packit 0848f5
	    echo "for file $arg ."
Packit 0848f5
	    exit 1
Packit 0848f5
        fi
Packit 0848f5
    fi
Packit 0848f5
    # Otherwise, just accept the argument
Packit 0848f5
    ;;
Packit 0848f5
    # - end of special handling for .F files
Packit 0848f5
Packit 0848f5
    esac
Packit 0848f5
    if [ $addarg = no ] ; then
Packit 0848f5
        unset allargs[$argno]
Packit 0848f5
    fi
Packit 0848f5
    # Some versions of bash do not accept ((argno++))
Packit 0848f5
    argno=`expr $argno + 1`
Packit 0848f5
done
Packit 0848f5
Packit 0848f5
if [ $# -eq 0 ] ; then
Packit 0848f5
    echo "Error: Command line argument is needed!"
Packit 0848f5
    "$0" -help
Packit 0848f5
    exit 1
Packit 0848f5
fi
Packit 0848f5
Packit 0848f5
# -----------------------------------------------------------------------
Packit 0848f5
# Derived variables.  These are assembled from variables set from the
Packit 0848f5
# default, environment, configuration file (if any) and command-line
Packit 0848f5
# options (if any)
Packit 0848f5
Packit 0848f5
#
Packit 0848f5
# The library lib${MPILIBNAME}fort contains the f90-specific (or later) features,
Packit 0848f5
# such as the module objects and the routines defined by them
Packit 0848f5
# (MPI_SIZEOF is handled in lib${MPILIBNAME)fort, for example).
Packit 0848f5
Packit 0848f5
PROFILE_FOO=
Packit 0848f5
# Handle the case of a profile switch
Packit 0848f5
if [ -n "$profConf" ] ; then
Packit 0848f5
    profConffile=
Packit 0848f5
    if [ -s "$libdir/lib$profConf.a" -o -s "$libdir/lib$profConf.so" ] ; then
Packit 0848f5
	PROFILE_FOO="-l$profConf"
Packit 0848f5
    elif [ -s "$sysconfdir/$profConf.conf" ] ; then
Packit 0848f5
	profConffile="$sysconfdir/$profConf.conf"
Packit 0848f5
    elif [ -s "$profConf.conf" ] ; then
Packit 0848f5
        profConffile="$profConf.conf"
Packit 0848f5
    else
Packit 0848f5
        echo "Profiling configuration file $profConf.conf not found in $sysconfdir"
Packit 0848f5
    fi
Packit 0848f5
    if [ -n "$profConffile" -a -s "$profConffile" ] ; then
Packit 0848f5
	. $profConffile
Packit 0848f5
    fi
Packit 0848f5
fi
Packit 0848f5
Packit 0848f5
# Construct the line to add the include directory (not all compilers
Packit 0848f5
# use -I, unfortunately)
Packit 0848f5
if [ -z "${FCINC}" ] ; then
Packit 0848f5
    # If there is no path, add a link to the mpif.h file.
Packit 0848f5
    # There *must* be a way to provide the path the any modules (there
Packit 0848f5
    # may be too many to link)
Packit 0848f5
    if [ ! -r mpif.h ] ; then
Packit 0848f5
        #echo "Adding a symbolic link for mpif.h"
Packit 0848f5
	trap "$Show rm -f mpif.h" 0
Packit 0848f5
	# This should really be the (related) f77includedir.
Packit 0848f5
	$Show ln -s ${includedir}/mpif.h mpif.h
Packit 0848f5
	# Remember to remove this file
Packit 0848f5
	rmfiles="$rmfiles mpif.h"
Packit 0848f5
    fi
Packit 0848f5
    FCINCDIRS=
Packit 0848f5
else
Packit 0848f5
    # Normally, FCINC is just -I, but some compilers have used different
Packit 0848f5
    # command line arguments
Packit 0848f5
    FCINCDIRS=${FCINC}${includedir}
Packit 0848f5
fi
Packit 0848f5
Packit 0848f5
# Handle the specification of the directory containing the modules
Packit 0848f5
if [ -n "$FCMODINCSPEC" ] ; then
Packit 0848f5
    newarg=`echo A"$FCMODINCSPEC" | \
Packit 0848f5
	sed -e 's/^A//' -e 's%<dir>%'"$includedir%g" -e 's/<file>/mpi/g'`
Packit 0848f5
    FCMODDIRS="$newarg"
Packit 0848f5
elif [ -n "$FCMODINC" ] ; then
Packit 0848f5
    FCMODDIRS="${FCMODINC}$modincdir"
Packit 0848f5
fi
Packit 0848f5
Packit 0848f5
final_fcflags="@MPICH_MPIFORT_FCFLAGS@ @WRAPPER_FCFLAGS@"
Packit 0848f5
final_ldflags="@MPICH_MPIFORT_LDFLAGS@ @WRAPPER_LDFLAGS@"
Packit 0848f5
final_libs="@MPICH_MPIFORT_LIBS@"
Packit 0848f5
if test "@INTERLIB_DEPS@" = "no" -o "${interlib_deps}" = "no" ; then
Packit 0848f5
    final_ldflags="${final_ldflags} @LDFLAGS@"
Packit 0848f5
    final_libs="${final_libs} @LIBS@ @WRAPPER_LIBS@"
Packit 0848f5
fi
Packit 0848f5
Packit 0848f5
# A temporary statement to invoke the compiler
Packit 0848f5
# Place the -L before any args incase there are any mpi libraries in there.
Packit 0848f5
# Eventually, we'll want to move this after any non-MPI implementation
Packit 0848f5
# libraries
Packit 0848f5
if [ "$linking" = yes ] ; then
Packit 0848f5
    # Attempt to encode rpath info into the executable if the user has not
Packit 0848f5
    # disabled rpath usage and some flavor of rpath makes sense on this
Packit 0848f5
    # platform.
Packit 0848f5
    # TODO configure and config.rpath are computing more sophisticated rpath
Packit 0848f5
    # schemes than this simple one.  Consider updating this logic accordingly.
Packit 0848f5
    if test "X$enable_wrapper_rpath" = "Xyes" ; then
Packit 0848f5
        # prepend the path for the shared libraries to the library list
Packit 0848f5
        eval rpath_flags=\"${hardcode_libdir_flag_spec}\"
Packit 0848f5
    else
Packit 0848f5
	rpath_flags=""
Packit 0848f5
    fi
Packit 0848f5
Packit 0848f5
    if [ "$nativelinking" = yes ] ; then
Packit 0848f5
        $Show $FC $PROFILE_INCPATHS ${final_fcflags} ${final_ldflags} "${allargs[@]}"
Packit 0848f5
        rc=$?
Packit 0848f5
    else
Packit 0848f5
        $Show $FC $PROFILE_INCPATHS ${final_fcflags} ${final_ldflags} "${allargs[@]}" $FCINCDIRS $FCMODDIRS -L$libdir -l@MPIFCLIBNAME@ $PROFILE_PRELIB $PROFILE_FOO $rpath_flags -l@MPILIBNAME@ @LPMPILIBNAME@ $PROFILE_POSTLIB ${final_libs} @FC_OTHER_LIBS@
Packit 0848f5
        rc=$?
Packit 0848f5
    fi
Packit 0848f5
else
Packit 0848f5
    $Show $FC $PROFILE_INCPATHS ${final_fcflags} "${allargs[@]}" $FCINCDIRS $FCMODDIRS
Packit 0848f5
    rc=$?
Packit 0848f5
fi
Packit 0848f5
if [ -n "$rmfiles" ] ; then
Packit 0848f5
    for file in $rmfiles ; do
Packit 0848f5
        objfile=`basename $file .f`
Packit 0848f5
	if [ -s "${objfile}.o" ] ; then
Packit 0848f5
	    # Rename
Packit 0848f5
	    destfile=`echo $objfile | sed -e "s/.*$$-//"`
Packit 0848f5
	    mv -f ${objfile}.o ${destfile}.o
Packit 0848f5
	fi
Packit 0848f5
        rm -f $file
Packit 0848f5
    done
Packit 0848f5
    rm -f $rmfiles
Packit 0848f5
fi
Packit 0848f5
exit $rc