mpif90-trace-bgp-V1R4.in 14 KB
Newer Older
1
#! /bin/bash
2 3 4 5
#
# (C) 2006 by Argonne National Laboratory.
#     See COPYRIGHT in top-level directory.
#
6
# mpif90
7 8 9 10 11 12
# 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
13 14
#    F90                - Fortran 90 compiler
#    WRAPPER_F90FLAGS      - Any special flags needed to compile 
15 16 17
#    WRAPPER_LDFLAGS       - Any special flags needed to link
#    MPILIBNAME         - Name of the MPI library
#    MPI_OTHERLIBS      - Other libraries needed in order to link 
18
#    F90_OTHER_LIBS     - Yet more libraries, needed just with F90
19 20 21 22 23 24 25 26 27 28 29 30 31
#    
# We assume that (a) the C compiler can both compile and link programs
#
# 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)
32
prefix=/bgsys/drivers/ppcfloor/comm/default
33 34 35
exec_prefix=${prefix}
sysconfdir=${prefix}/etc
includedir=${prefix}/include
36
modincdir=${prefix}/include
37
libdir=${exec_prefix}/lib
38
opalibdir=${exec_prefix}/lib
39 40 41 42 43

CP_WRAPPERS=@CP_WRAPPERS@
DARSHAN_LIB_PATH=@darshan_lib_path@
CP_ZLIB_LINK_FLAGS=@__CP_ZLIB_LINK_FLAGS@

44 45 46 47
#
# 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)
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
F90=" /bgsys/drivers/ppcfloor/gnu-linux/bin/powerpc-bgp-linux-gfortran -fno-underscoring"
F90_LINKPATH_SHL="-Wl,-rpath -Wl,"
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"
#
WRAPPER_F90FLAGS="-I/bgsys/drivers/ppcfloor/comm/sys/include"
65 66 67
WRAPPER_LDFLAGS="  "
MPILIBNAME="mpich.cnk"
PMPILIBNAME="pmpich.cnk"
68
MPI_OTHERLIBS=" -L/bgsys/drivers/ppcfloor/comm/sys/lib -Wl,-rpath,/bgsys/drivers/ppcfloor/comm/sys/lib -ldcmf.cnk      -ldcmfcoll.cnk      -lpthread -L/bgsys/drivers/ppcfloor/runtime/SPI -Wl,-rpath,/bgsys/drivers/ppcfloor/runtime/SPI -lSPI.cna -lrt  "
69
NEEDSPLIB="no"
70 71
# MPIVERSION is the version of the MPICH2 library for which mpif90 is intended
MPIVERSION="1.1"
72 73 74 75 76 77 78 79 80 81
#
#
# Internal variables
# Show is set to echo to cause the compilation command to be echoed instead 
# of executed.
Show=
#
# End of initialization of variables
#---------------------------------------------------------------------
# Environment Variables.
82
# The environment variables MPICH_F90 may be used to override the 
83
# default choices.
84 85 86
# 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
87 88
# changes to the compilation environment.  See the variables used by the 
# script (defined above)
89 90 91 92 93
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
94 95 96
    fi
fi
# Allow a profiling option to be selected through an environment variable
97 98
if [ -n "$MPIF90_PROFILE" ] ; then
    profConf=$MPIF90_PROFILE
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
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=("$@")
argno=0
cppflags=()
for arg in "$@" ; do
    # Set addarg to no if this arg should be ignored by the C compiler
    addarg=yes
    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
    ;;
	# ----------------------------------------------------------------
127 128
	# Options that control how we use mpif90 (e.g., -show, 
	# -f90=* -config=*
129 130 131 132
    -echo)
    addarg=no
    set -x
    ;;
133 134
    -f90=*)
    F90=`echo A$arg | sed -e 's/A-f90=//g'`
135 136 137 138 139 140 141 142
    addarg=no
    ;;
    -show)
    addarg=no
    Show=echo
    ;;
    -config=*)
    addarg=no
143 144 145
    F90name=`echo A$arg | sed -e 's/A-config=//g'`
    if [ -s "$sysconfdir/mpif90-$F90name.conf" ] ; then
        . "$sysconfdir/mpif90-$F90name.conf"
146
    else
147
	echo "Configuration file mpif90-$F90name.conf not found"
148 149 150 151 152 153 154 155 156 157 158 159 160 161
    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.
162 163 164 165 166
    echo "mpif90 for $MPIVERSION"
    # if there is only 1 argument, it must be -v.
    if [ "$#" -eq "1" ] ; then
        linking=no
    fi
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
    ;;
    -profile=*)
    # Pass the name of a profiling configuration.  As
    # a special case, lib<name>.so or lib<name>.la may be used
    # if the library is in $libdir
    profConf=`echo A$arg | sed -e 's/A-profile=//g'`
    addarg=no
    # Loading the profConf file is handled below
    ;;
    -mpe=*)
    # Pass the name of a profiling configuration; this is a special
    # case for the MPE libs.  See -profile
    profConf=`echo A$arg | sed -e 's/A-mpe=//g'`
    profConf="mpe_$profConf"
    addarg=no
    # Loading the profConf file is handled below
    ;;
    -help)
185 186 187 188 189 190 191 192 193 194 195
    NC=`echo "$F90" | sed 's%\/% %g' | awk '{print $NF}' -`
    if [ -f "$sysconfdir/mpixxx_opts.conf" ] ; then
        . $sysconfdir/mpixxx_opts.conf
        echo "    -f90=xxx      - Reset the native compiler to xxx."
    else
        if [ -f "./mpixxx_opts.conf" ] ; then
            . ./mpixxx_opts.conf
            echo "    -f90=xxx      - Reset the native compiler to xxx."
        fi
    fi
    exit 0
196 197 198 199 200 201 202 203 204
    ;;
    # 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"
    ;;
205 206
    *.F|*.F90|.fpp|.FPP)
# If F90CPP is not empty, then we need to do the following:
207
#    If any input files have the .F or .F90 extension, then    
208
#        If F90CPP = false, then
209
#            generate an error message and exit
210
#        Use F90CPP to convert the file from .F to .f, using 
211 212 213 214
#            $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
#     
215 216
    if [ -n "$F90CPP" ] ; then
        if [ "$F90CPP" = "false" ] ; then
217 218 219 220 221 222 223 224 225
            echo "This Fortran compiler does not accept .F or .F90 files"
	    exit 1
        fi
        addarg=no
	# Remove and directory names and extension
	$ext=`expr "$arg" : '.*\(\..*\)'`
        bfile=`basename $arg $ext`
	# 
	TMPDIR=${TMPDIR:-/tmp}
226 227 228
	# Make sure that we use a valid extension for the temp file.
        tmpfile=$TMPDIR/f$$-$bfile.$F90EXT
        if $F90CPP "${cppflags[@]}" $arg > $tmpfile ; then
229 230 231 232 233 234 235 236 237 238 239 240 241
            # 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
242

243 244 245 246 247 248 249 250
    esac
    if [ $addarg = no ] ; then
        unset allargs[$argno]
    fi
    # Some versions of bash do not accept ((argno++))
    argno=`expr $argno + 1`
done

251 252 253 254 255 256
if [ $# -eq 0 ] ; then
    echo "Error: Command line argument is needed!"
    "$0" -help
    exit 1
fi

257 258 259 260
# -----------------------------------------------------------------------
# Derived variables.  These are assembled from variables set from the
# default, environment, configuration file (if any) and command-line
# options (if any)
261 262 263 264 265

#
# 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).
266
if [ "$NEEDSPLIB" = yes ] ; then
267
    mpilibs="-l${MPILIBNAME}f90 -l$PMPILIBNAME -l$MPILIBNAME -lopa"
268
else
269
    mpilibs="-l${MPILIBNAME}f90 -l$MPILIBNAME -lopa"
270 271 272
fi
#
# Init with the ones needed by MPI
273
F90FLAGS="$WRAPPER_F90FLAGS"
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
LDFLAGS="$WRAPPER_LDFLAGS"
#
# Handle the case of a profile switch
if [ -n "$profConf" ] ; then
    profConffile=
    if [ -s "$libdir/lib$profConf.a" -o -s "$libdir/lib$profConf.so" ] ; then
	mpilibs="-l$profConf $mpilibs"
    elif [ -s "$sysconfdir/$profConf.conf" ] ; then
	profConffile="$sysconfdir/$profConf.conf"
    elif [ -s "$profConf.conf" ] ; then
        profConffile="$profConf.conf"
    else
        echo "Profiling configuration file $profConf.conf not found in $sysconfdir"
    fi
    if [ -n "$profConffile" -a -s "$profConffile" ] ; then
	. $profConffile
	if [ -n "$PROFILE_INCPATHS" ] ; then
291
	    F90FLAGS="$PROFILE_INCPATHS $F90FLAGS"
292 293 294 295 296 297 298 299 300
        fi
        if [ -n "$PROFILE_PRELIB" ] ; then
	    mpilibs="$PROFILE_PRELIB $mpilibs"
        fi
        if [ -n "$PROFILE_POSTLIB" ] ; then
	    mpilibs="$mpilibs $PROFILE_POSTLIB"
        fi
    fi
fi
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336

# 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 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

337 338 339 340 341
#
# 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
342

343
if [ "$linking" = yes ] ; then
344
    if [ -n "$F90_LINKPATH_SHL" ] ; then
345
        # Prepend the path for the shared libraries to the library list
346
        mpilibs="$F90_LINKPATH_SHL$libdir $mpilibs"
347
    fi
348 349 350 351 352 353 354 355 356 357 358

    # Trial run to generate a symbol listing.  We only enable Darshan if:
    # a) MPI is used
    # b) PMPI is _not_ used
    #
    # We want Darshan to get out of the way if the user is doing a runtime
    # test in configure (in which case MPI objects break the executable) or
    # if the user is using another PMPI based tool
    
    stock_link_cmd="$F90 ${allargs[@]} $F90INCDIRS $F90MODDIRS $F90FLAGS $LDFLAGS -L$libdir -L$opalibdir $F90MODLIBS $mpilibs $MPI_OTHERLIBS"

359 360 361 362
    # if allargs includes any libraries, then we need to get 
    # -ldarshan-mpi-io in there first
    newallargs=`echo ${allargs[@]} | sed 's/ -l/ -ldarshan-mpi-io -l/'`
    darshan_link_cmd="$F90 ${CP_WRAPPERS} $newallargs -L${DARSHAN_LIB_PATH} ${CP_ZLIB_LINK_FLAGS} -lfmpich.cnk -ldarshan-posix -ldarshan-mpi-io $F90INCDIRS $F90MODDIRS $F90FLAGS $LDFLAGS -L$libdir -L$opalibdir $F90MODLIBS $mpilibs $MPI_OTHERLIBS -L${DARSHAN_LIB_PATH} -ldarshan-posix -ldarshan-mpi-io -lz"
363 364 365 366 367 368

    used_darshan=0

    # create a temporary file
    tmpfile=`mktemp`
    # generate a map of the symbol names
369 370 371
    # don't use -shared for this step
    stock_link_cmd_no_shared=`echo $stock_link_cmd | sed 's/ -shared / /g'`
    $stock_link_cmd_no_shared -Wl,-Map,$tmpfile $LDFLAGS -o /dev/null >& /dev/null
372 373 374
    # is MPI in there?
    grep MPI $tmpfile >& /dev/null
    rc_mpi=$?
375 376
    # is PMPI being used for any init, finalize, or MPI-IO calls?
    grep -E \(PMPI_File_\)\|\(PMPI_Init\)\|\(PMPI_Finalize\) $tmpfile |grep -v mpich.cnk.a |grep \(PMPI >& /dev/null
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
    rc_pmpi=$?
    rm $tmpfile >& /dev/null

    # disable darshan if the executable is not an MPI program (this feature
    # mainly allows runtime configure tests to work
    if [ $rc_mpi -eq 0 ] ; then
        # disable darshan if something else is already using PMPI; we don't
        # want to cause any symbol conflicts
        if [ $rc_pmpi -ne 0 ] ; then
            $Show $darshan_link_cmd
            used_darshan=1
        fi
    fi

    # otherwise use the normal command line
    if [ "$used_darshan" = "0" ] ; then
        $Show $stock_link_cmd
394
    fi
395 396
    rc=$?
else
397
    $Show $F90 "${allargs[@]}" $F90INCDIRS $F90MODDIRS $F90FLAGS
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
    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