#!/bin/sh
#/****************************************************************************
#**  SCALASCA    http://www.scalasca.org/                                   **
#*****************************************************************************
#**  Copyright (c) 1998-2012                                                **
#**  Forschungszentrum Juelich GmbH, Juelich Supercomputing Centre          **
#**                                                                         **
#**  See the file COPYRIGHT in the package base directory for details       **
#****************************************************************************/

if [ $# -eq 0 ]
then
  echo "usage: $0 <compile-or-link-command>"
  exit 1
fi

# INITIALIZATION
# -- Variables marked #FE# are relevant on frontend (used in Makefile.cross)
OCC="gcc -g -fPIC -DPIC"

# OPARI2
OPARI2="opari2"                    #FE#
OPARI2_CONFIG="opari2-config"      #FE#
POMP2INC="-I/usr/include -I/usr/include"        #FE#
OOINC=${POMP2INC}                                  #FE#
OLINK=""
OPARI2OPTS="--disable=region --task=abort"

PDTBIN="/usr/bin"            #FE#
PDTPARSER=""
PDTF90PARSER="gfparse"
PDTPARSEOPTS=""
PDTCOPTS="-I/usr/lib64/openmpi/include"       #FE#
PDTCXXOPTS="-I/usr/lib64/openmpi/include"     #FE#
PDTF95OPTS=""
TAUINSTOPTS="-spec /usr/share/doc/scalasca.inst"

DELETE=rm

PDTBIN="/usr/bin"
PDTPARSER=""
PDTF90PARSER="gfparse"
PDTPARSEOPTS=""
PDTCOPTS="-I/usr/lib64/openmpi/include"
PDTCXXOPTS="-I/usr/lib64/openmpi/include"
PDTF95OPTS=""
TAUINSTOPTS="-spec /usr/share/doc/scalasca.inst"

DELETE=rm

PREC=${OBJECT_MODE:-64}
PFLAG=""

FPP=" "
FTR="tr f F"

SEQELGLIB=-lelg.ser
OMPELGLIB=-lelg.omp
MPIELGLIB=-lelg.mpi
HYBELGLIB=-lelg.ompi

INSTMODE=${SKIN_MODE:-"AUTO"}
ADDLIBS=no
COMPONLY=no
USESOMP=no
USESMPI=no
USESOTF=no
USEAPI=no
USETAU=no
CPPARG=no
EXEARG=no
COMP=""
FLIB=""
ULIB="-lcubew3 -L/usr/lib64 -L/usr/lib -lbfd -lz -L/usr/lib64 -L/usr/lib -liberty"

MFILESsrc=""
MFILESobj=""

OBJFILES=""
EXEFILE="a.out"

#V=yes

# PROCESS COMMAND LINE
# - collect source files for opari instrumentation (OFILES)
# - collect compiler output files so they can correctly renamed (MFILESobj)
# - collect opari output files so they can deleted after use (MFILESsrc)
# - make compiler to compile instrumented files (NEWFILE)
# - add opari and scalasca include directory before first file (POMP2INC)
# - collect special arguments to opari (everything up to "--")
for origarg in "-mode=${INSTMODE}" "$@"
do
  # Escape special characters in arguments
  #   -e 's/^x//'              Removes the 'x' at the beginning
  #   -e 's/\\\\/\\\\\\\\/g'   Replaces '\' by '\\'
  #   -e 's/"/\\\"/g'          Replaces '"' by '\"'
  #   -e 's/'\''/\\\'\''/g'    Replaces ''' by '\''
  #   -e 's/ /\\\ /g'          Replaces ' ' by '\ '
  #   -e 's/(/\\\(/g'          Replaces '(' by '\('
  #   -e 's/)/\\\)/g'          Replaces ')' by '\)'
  arg=`echo "x$origarg" | sed -e 's/^x//' \
                              -e 's/\\\\/\\\\\\\\/g' \
                              -e 's/"/\\\"/g' \
                              -e 's/'\''/\\\'\''/g' \
                              -e 's/ /\\\ /g' \
                              -e 's/(/\\\(/g' \
                              -e 's/)/\\\)/g'`
  if [ ${CPPARG} = "yes" ]
  then
    CPPARG=no
    PDTPARSEOPTS="${PDTPARSEOPTS}${arg}"
  fi

  if [ ${EXEARG} = "yes" ]
  then
    EXEARG=no
    EXEFILE="${arg}"
    ARGS="${ARGS} ${arg}"
  else
    case ${arg} in
    *.c|*.C|*.cc|*.CC|*.cpp|*.CPP|*.cxx|*.CXX|*.f|*.F|*.for|*.FOR|*.ftn|*.FTN|*.f90|*.F90|*.f95|*.F95)
           case ${arg} in
           -*) ARGS="${ARGS} ${arg}"
               ;;
           *)  if [ ${INSTMODE} != "NONE" ]
               then
                 BASE=`echo ${arg} | sed -e 's/\.[^\.]*$//'`
                 SUF=`echo ${arg} | sed -e 's/.*\./\./' | ${FTR}`
                 NEWFILE=${BASE}.mod${SUF}
                 TAUFILE=${BASE}.tau${SUF}
                 OFILES="${OFILES} $arg"
                 MFILESsrc="${MFILESsrc} ${NEWFILE} ${TAUFILE} ${arg}.opari.inc"
                 if [ ${USETAU} = "yes" ]
                 then
                   ARGS="${ARGS} ${POMP2INC} ${TAUFILE}"
                   MFILESobj="${MFILESobj} `basename ${BASE}.tau.o`"
                 else
                   ARGS="${ARGS} ${POMP2INC} ${NEWFILE}"
                   MFILESobj="${MFILESobj} `basename ${BASE}.mod.o`"
                 fi
                 POMP2INC=""
               else
                 ARGS="${ARGS} ${arg}"
               fi
               ;;
           esac
           ;;
    *.o)   OBJFILES="${OBJFILES} ${arg}"
           ARGS="${ARGS} ${arg}"
           ;;
    --)    OPARI2OPTS="${OPARI2OPTS} ${COMP} ${ARGS}"
           ARGS=""
           COMP=""
           ;;
    -comp=*)
           echo "Warning: Ignoring unsupported option: $arg"
           ;;
    -mode=*)
           MODE=`echo $arg | sed -e 's/-mode=//' | tr a-z A-Z`
           case ${MODE} in
           AUTO|AUTOMATIC) # default: determine variant automatically
              ;;
           NONE) # disable all instrumentation and measurement preparation
              INSTMODE="NONE"
              ;;
           MPI)
              INSTMODE="MPI"
              USESMPI="yes"
              USESOMP="no"
              ;;
           OMP|OPENMP)
              INSTMODE="OMP"
              USESMPI="no"
              USESOMP="yes"
              USEOPARI="yes"
              ;;
           OMPI|OMP[+,/]MPI|OPENMP[+,/]MPI|MPI[+,/]OPENMP|MPI[+,/]OMP|HYB|HYBRID)
              INSTMODE="OMP+MPI"
              USESMPI="yes"
              USESOMP="yes"
              USEOPARI="yes"
              ;;
           SER|SERIAL|SEQ|SEQUENTIAL)
              INSTMODE="serial"
              USESMPI="no"
              USESOMP="no"
              ;;
           *)
              echo "ERROR: Unsupported instrumentation measurement mode: $MODE"
              exit 44
              ;;
           esac
           ;;
    -pdt|-tau|-opt[A-Z]*)  if [ -z "${PDTBIN}" ]
           then
             echo "ERROR: PDT/TAU instrumentation not supported by this installation of KOJAK/SCALASCA"
             exit 43
           else
             USETAU=yes
             FTR="cat"
  
             case ${arg} in
             -pdt|-tau)
               ;;
             -optVerbose*)
               V=yes
               ;;
             -optPdtCOpts=*)
               PDTOPTS=`echo $arg | sed -e 's/-optPdtCOpts=//'`
               PDTCOPTS="${PDTCOPTS} ${PDTOPTS}"
               unset PDTOPTS
               ;;
             -optPdtCxxOpts=*)
               PDTOPTS=`echo $arg | sed -e 's/-optPdtCxxOpts=//'`
               PDTCXXOPTS="${PDTCXXOPTS} ${PDTOPTS}"
               unset PDTOPTS
               ;;
             -optPdtF95Opts=*)
               PDTOPTS=`echo $arg | sed -e 's/-optPdtf95Opts=//'`
               PDTF95OPTS="${PDTF95OPTS} ${PDTOPTS}"
               unset PDTOPTS
               ;;
             -optPdtUser=*)
               OPTPDTUSER=`echo $arg | sed -e 's/-optPdtUser=//'`
               PDTPARSEOPTS="${PDTPARSEOPTS} ${OPTPDTUSER}"
               unset OPTPDTUSER
               ;;
             -optPdtF90Parser=*)
               PDTF90PARSER=`echo $arg | sed -e 's/-optPdtF90Parser=//'`
               ;;
             -optPdtGnuFortranParser*)
               PDTF90PARSER="gfparse"
               ;;
             -optPdtCleanscapeParser*)
               PDTF90PARSER="f95parse"
               ;;
             -optTauSelectFile=*)
               TAUSELECTFILE=`echo $arg | sed -e 's/-optTauSelectFile=/-f /'`
               TAUINSTOPTS="${TAUINSTOPTS} ${TAUSELECTFILE}"
               unset TAUSELECTFILE
               ;;
             -optTau=*)
               TAUOPTS=`echo $arg | sed -e 's/-optTau=//'`
               TAUINSTOPTS="${TAUINSTOPTS} ${TAUOPTS}"
               unset TAUOPTS
               ;;
             -optKeepFiles*)
               # -- do not remove file by replacing "rm" with "/bin/true"
               DELETE=true
               ;;
             *) echo "INFO: Ignoring PDT/TAU instrumentation option ${arg}."
               ;;
             esac
           fi
           ;;
    -nosrc) OPARI2OPTS="${OPARI2OPTS} --nosrc"
           FPP=""
           FTR="cat"
           ;;
    -otf)  if [ -z "-L/usr/lib64 -L/usr/lib -lotf -lz" ]
           then
             echo "ERROR: OTF generation not supported by this installation of KOJAK/SCALASCA"
             exit 42
           else
             SEQELGLIB=-lelg.otf
             OMPELGLIB=-lelg.otf.omp
             MPIELGLIB=-lelg.otf.mpi
             HYBELGLIB=-lelg.otf.ompi
             USESOTF=yes
           fi
           ;;
    -user) USEAPI=yes
           ;;
    -OpenMPnotAvailable)
           echo "INFO: OpenMP not supported by this installation of KOJAK/SCALASCA"
           exit 0
           ;;
    -fopenmp) #USESOMP#
           if [ "${INSTMODE}" = "AUTO" ]; then
             USESOMP=yes
           fi
           ARGS="${ARGS} ${arg}"
           ;;
    -lhpmpi*|-lscmpi*|-lmpi*) #USESMPI#
           if [ "${INSTMODE}" = "AUTO" ]; then
             USESMPI=yes
             ULIB="${ULIB} ${arg}"
             ADDLIBS=yes
           fi
           ;;
    -l@FLIBINDICATOR@) FLIB="-lmpi_f77 -lmpi_f90"
           if [ ${ADDLIBS} = "yes" ]
           then
             ULIB="${ULIB} ${arg}"
           else
             ARGS="${ARGS} ${arg}"
           fi
           ;;
    -l*)   if [ ${ADDLIBS} = "yes" ]
           then
             ULIB="${ULIB} ${arg}"
           else
             ARGS="${ARGS} ${arg}"
           fi
           ;;
    -o)    EXEARG=yes;
           ARGS="${ARGS} ${arg}"
           ;;
    -I|-D|-U)
           CPPARG=yes
           PDTPARSEOPTS="${PDTPARSEOPTS} ${arg}"
           ARGS="${ARGS} ${arg}"
           ;;
    -I*|-D*|-U*)
           PDTPARSEOPTS="${PDTPARSEOPTS} ${arg}"
           ARGS="${ARGS} ${arg}"
           ;;
  -ffixed-line-length-132)
         PDTF95OPTS="${PDTF95OPTS} -ffixed-line-length-132"
         ARGS="${ARGS} ${arg}"
         ;;
  -ffree-form)
         PDTF95OPTS="${PDTF95OPTS} -R free"
         ARGS="${ARGS} ${arg}"
         ;;
  -ffixed-form)
         PDTF95OPTS="${PDTF95OPTS} -R fixed"
         ARGS="${ARGS} ${arg}"
         ;;
    -@)    V=yes
           ;;
    -c)    COMPONLY=yes
           ARGS="${ARGS} ${arg}"
           ;;
    -E|-P|-EP) INSTMODE="NONE"
           USETAU=no
           USEOPARI=no
           ARGS="${ARGS} ${arg}"
           ;;
    -m64|-q64|-xarch=*64|-xarch=amd64a|-xtarget=*64) PREC=64
           PFLAG=${arg}
           ARGS="${ARGS} ${arg}"
           ;;
    -m32|-q32|-xarch=*|-xtarget=*) PREC=32
           PFLAG=${arg}
           ARGS="${ARGS} ${arg}"
           ;;
    *)     if [ -z "${COMP}" ]
           then
             COMP=${arg}
           else
             ARGS="${ARGS} ${arg}"
           fi
           ;;
    esac
  fi
done

# No instrumentation, execute "original" command line
if [ "${INSTMODE}" = "NONE" ]
then
  [ -n "${V}" ] && echo "+++ ${COMP} ${ARGS}"
  eval ${COMP} ${ARGS} || exit
  exit 0
fi

# Adjust PDT parser options to include '_OPENMP' if OpenMP is enabled
if [ "${USESOMP}" = "yes" ]
then
  PDTCOPTS="${PDTCOPTS} -D_OPENMP"
  PDTCXXOPTS="${PDTCXXOPTS} -D_OPENMP"
  PDTF95OPTS="${PDTF95OPTS} -D_OPENMP"
fi

if [ -z "${COMP}" ]
then
  echo "ERROR: No compiler or linker specified."
  exit 22
fi

PCOMP=`which ${COMP}`
BCOMP=`basename ${COMP}`

# Disable OPARI declarations for broken compilers
PATHSCALE=`${COMP} -v 2>&1 | grep -c ^PathScale`
if [ ${PATHSCALE} -gt 0 ]; then
  OPARI2OPTS="--nodecl ${OPARI2OPTS}"
fi
PGIV=`${COMP} -V 2>&1 | grep ^pg | cut -d' ' -f2`
case "${PGIV}" in
  [78].*) OPARI2OPTS="--nodecl ${OPARI2OPTS}"
      ;;
esac

# Make sure Fortran can handle C preprocessor
case ${BCOMP} in
*f*) COMP="${COMP}${FPP}"
     ;;
esac

# Determine whether Fortran wrapper library is required
case ${BCOMP} in
*f*) FLIB="-lmpi_f77 -lmpi_f90"
     ;;
esac

if [ "${PREC}" = "64" ]
then
  # -- note: "then" block needs to be marked with #COPY# for 32/64 combine
  if [ ${USESOTF} = "yes" ]; then ULIB="${ULIB} -L/usr/lib64 -L/usr/lib -lotf -lz"; fi  #COPY#
  ELGLIB="-L/usr/lib64"                                          #COPY#
  EXTRALIBS="${ULIB} -L/usr/lib -Wl,-rpath /usr/lib -lpapi64   -lsz0"            #COPY#
else
  echo "ERROR: ${PREC}bit-mode (${PFLAG}) not supported by this installation of KOJAK/SCALASCA"
  exit 32
fi

# DETERMINE INSTRUMENTATION MODE
if [ "${INSTMODE}" = "AUTO" ]; then
  # - determine whether MPI is used (by checking COMP)
  case ${BCOMP} in
  mp*) USESMPI=yes
       ;;
  esac
  case ${PCOMP} in
    /opt/cray/*) # Cray compiler wrapper
       if [ ${MPT_DIR:+1} ] || [ ${CRAY_MPICH2_DIR:+1} ]; then
         USESMPI=yes # wrapper will add -lmpich when linking
       fi
       ;;
  esac
fi

# - select the right EPILOG library depending on OpenMP/MPI usage
if [ ${USESOMP} = "yes" ]
then
  if [ ${USESMPI} = "yes" ]
  then
    VARIANT="OMP+MPI"
    ELIB="${ELGLIB} ${FLIB} ${HYBELGLIB}  ${EXTRALIBS}"
    LIBA=`echo ${ELGLIB} | cut -c3-`/lib`echo ${HYBELGLIB} | cut -c3-`.so
  else
    VARIANT="OMP"
    ELIB="${ELGLIB} ${OMPELGLIB} ${EXTRALIBS}"
    LIBA=`echo ${ELGLIB} | cut -c3-`/lib`echo ${OMPELGLIB} | cut -c3-`.so
  fi
else
  if [ ${USESMPI} = "yes" ]
  then
    VARIANT="MPI"
    ELIB="${ELGLIB} ${FLIB} ${MPIELGLIB}  ${EXTRALIBS}"
    LIBA=`echo ${ELGLIB} | cut -c3-`/lib`echo ${MPIELGLIB} | cut -c3-`.so
  else
    VARIANT="serial"
    ELIB="${ELGLIB} ${SEQELGLIB} ${EXTRALIBS}"
    LIBA=`echo ${ELGLIB} | cut -c3-`/lib`echo ${SEQELGLIB} | cut -c3-`.so
  fi
fi

# verify measurement library/support exists
if [ ! -f ${LIBA} ]
then
  [ -n "${V}" ] && echo "INFO: ${LIBA} not found!"
  echo "ERROR: ${VARIANT} mode not supported by this installation of Scalasca!"
  exit 66
else
  [ -n "${V}" ] && echo "INFO: ${VARIANT} instrumentation mode ..."
fi

# PREPARE PARSING SETUP
if [ ${USESOMP} = "no" ]
then
  # enable only POMP instrumentation for non-OpenMP
  OPARI2OPTS="--disable=omp ${OPARI2OPTS}"
fi

# TAU instrumentor parsers cannot handle #line directives well
if [ ${USETAU} = "yes" ]
then
  OPARI2OPTS="${OPARI2OPTS} --nosrc"
fi

# INSTRUMENTATION
if [ "${COMPONLY}" = "no" -a "${USEOPARI}" = "yes" ]; then
  cat /dev/null > pompregions_$$_src.txt
fi
# - run opari on every collected source file
for ofile in ${OFILES}
do
    [ -n "${V}" ] && echo "+++ ${OPARI2} ${OPARI2OPTS} $ofile"
    eval ${OPARI2} ${OPARI2OPTS} $ofile || exit

    if [ ${COMPONLY} = "no" ]; then
      # Collect POMP2_Init_regions routines (Fortran: mangled name: small_!)
      case ${ofile} in
      *.c|*.C|*.cc|*.CC|*.cpp|*.CPP|*.cxx|*.CXX)
        grep -i 'void POMP2_Init_regions' ${ofile}.opari.inc | \
        sed -e 's/^.*POMP2/POMP2/' -e 's/()//' >> pompregions_$$_src.txt
        ;;
      *)
        BASE=`echo ${ofile} | sed -e 's/\.[^\.]*$//'`
        SUF=`echo ${ofile} | sed -e 's/.*\./\./' | ${FTR}`
        NEWFILE=${BASE}.mod${SUF}
        grep 'subroutine POMP2_Init_regions' ${NEWFILE} | \
        sed -e 's/^.*POMP2/POMP2/' -e 's/()/_/' | \
        tr '[A-Z]' '[a-z]' >> pompregions_$$_src.txt
        ;;
      esac
    fi

  if [ ${USETAU} = "yes" ]
  then
    # Parse source file with PDT
    BASE=`echo ${ofile} | sed -e 's/\.[^\.]*$//'`
    SUF=`echo ${ofile} | sed -e 's/.*\./\./' | ${FTR}`
      NEWFILE=${BASE}.mod${SUF}
      PDBFILE=`basename ${BASE}.mod.pdb`
    MFILESsrc="${MFILESsrc} ${PDBFILE}"
    TAUFILE=${BASE}.tau${SUF}
    case ${ofile} in
    *.f|*.F|*.for|*.FOR|*.ftn|*.FTN|*.f90|*.F90|*.f95|*.F95) PDTPARSER=${PDTF90PARSER}
      PDTPARSEOPTS="${PDTPARSEOPTS} ${PDTF95OPTS}" ;;
    *.c) PDTPARSER=cparse
      PDTPARSEOPTS="${PDTPARSEOPTS} ${PDTCOPTS}" ;;
    *) PDTPARSER=cxxparse
      PDTPARSEOPTS="${PDTPARSEOPTS} ${PDTCXXOPTS}" ;;
    esac
    [ -n "${V}" ] && echo "+++ ${PDTBIN}/${PDTPARSER} ${NEWFILE} ${OOINC} ${PDTPARSEOPTS}"
    eval ${PDTBIN}/${PDTPARSER} ${NEWFILE} ${OOINC} ${PDTPARSEOPTS} || exit

    # Instrument source with TAU
    [ -n "${V}" ] && echo "+++ ${PDTBIN}/tau_instrumentor ${PDBFILE} ${NEWFILE} -o ${TAUFILE} ${TAUINSTOPTS}"
    eval ${PDTBIN}/tau_instrumentor ${PDBFILE} ${NEWFILE} -o ${TAUFILE} ${TAUINSTOPTS} || exit
  fi
done

# enable manual user instrumentation macros
if [ ${USEAPI} = "yes" ]
then
  DOPT="-D"
  case ${BCOMP} in
  *f*) DOPT="-D" ;;
  esac
  IOPT="${IOPT} ${DOPT}EPIK ${POMP2INC}"
fi

# set additional linker flag for OPARI2 instrumentation if necessary
KERNEL=`uname -s`
if [ "${KERNEL}" = "AIX" ]
then
  [ -n "${V}" ] && echo "INFO: AIX linker ..."
  OLINK="-bexpfull"
fi

# AUTOMATIC FUNCTION INSTRUMENTATION
IOPT=""

# EXECUTING MODIFIED COMMAND
if [ ${COMPONLY} = "yes" ]
then
  # COMPILATION ONLY
  [ -n "${V}" ] && echo "+++ ${COMP} ${IOPT} ${ARGS}"
  eval ${COMP} ${IOPT} ${ARGS} || exit
else
  # COMPILATION + LINKING

  OREGFILE=""

    # Create opari2 initialization file and compile
    if [ -z "${OBJFILES}" ]
    then
      # no object files: create initialization file from sources
      [ -n "${V}" ] && echo "+++ awk -f `${OPARI2_CONFIG} --awk-script` pompregions_$$_src.txt"
      awk -f `${OPARI2_CONFIG} --awk-script` pompregions_$$_src.txt > pompregions_$$.c
    else
      [ -n "${V}" ] && echo "+++" ${OPARI2_CONFIG} --create-pomp2-regions ${OBJFILES} 
      eval `${OPARI2_CONFIG} --create-pomp2-regions ${OBJFILES}` > pompregions_$$.c || exit
    fi
    [ -n "${V}" ] && echo "+++ ${OCC} pompregions_$$.c -c"
    eval ${OCC} pompregions_$$.c -c || exit
    grep -i "extern void POMP2_Init_regions" pompregions_$$.c | sort \
         > pompregions_$$_1.txt
    OREGFILE="pompregions_$$.o"

  [ -n "${V}" ] && echo "INFO: Linking executable for ${VARIANT} measurement ..."

  # MODIFIED COMPILING/LINKING STEP
  [ -n "${V}" ] && echo "+++ ${COMP} ${IOPT} ${OREGFILE} ${ARGS} ${ELIB} ${OLINK}"
  eval ${COMP} ${IOPT} ${OREGFILE} ${ARGS} ${ELIB} ${OLINK} || exit

    # FIND OPARI2 INITILIAZATIONS IN EXECUTABLE, RECREATE INITIALIZATION FILE
    [ -n "${V}" ] && echo "+++" ${OPARI2_CONFIG} --create-pomp2-regions ${EXEFILE} 
    eval `${OPARI2_CONFIG} --create-pomp2-regions ${EXEFILE}` > pompregions_$$.c || exit
    grep -i "extern void POMP2_Init_regions" pompregions_$$.c | sort \
         > pompregions_$$_2.txt
  
    if diff pompregions_$$_1.txt pompregions_$$_2.txt > /dev/null 2>&1
    then
      [ -n "${V}" ] && echo "+++ ignoring relink"
    else
      [ -n "${V}" ] && echo "+++ ${OCC} pompregions_$$.c -c"
      eval ${OCC} pompregions_$$.c -c || exit
      OREGFILE="pompregions_$$.o"
    
      # RELINK 
      [ -n "${V}" ] && echo "+++ ${COMP} ${IOPT} ${OREGFILE} ${ARGS} ${ELIB}"
      eval ${COMP} ${IOPT} ${OREGFILE} ${ARGS} ${ELIB} || exit
  fi

  # Report successful build of instrumented executable
  echo "INFO: Instrumented executable for ${VARIANT} measurement"
  if [ ! -s pompregions_$$_2.txt ]
  then
    echo "WARNING: No POMP2 instrumented regions defined!"
  fi

  # CLEANUP INTERMEDIATE FILES (in non-verbose mode)
  if [ -z "${V}" ]
  then
      ${DELETE} -f pompregions_$$*
  fi
fi

# RENAME COMPILER OUTPUT TO ORIGINAL FILE NAME
for mfile in ${MFILESobj}
do
  if [ -f ${mfile} ]
  then
    TARGET=`echo ${mfile} | sed -e 's/\.mod//' -e 's/\.tau//'`
    [ -n "${V}" ] && echo "+++ mv ${mfile} ${TARGET}"
    eval mv ${mfile} ${TARGET}
  fi
done

# DELETE INTERMEDIATE OPARI OUTPUT (in non-verbose mode)
for mfile in ${MFILESsrc}
do
  if [ -f ${mfile} ]
  then
    [ -z "${V}" ] && ${DELETE} ${mfile}
  fi
done
exit 0
