#!/bin/bash
#****************************************************************************
#*                        TAU Portable Profiling Package                           **
#*                        http://www.cs.uoregon.edu/research/tau                   **
#****************************************************************************
#*    Copyright 1997-2024                                                  **
#*    Department of Computer and Information Science, University of Oregon **
#*    Advanced Computing Laboratory, Los Alamos National Laboratory        **
#*    Research Center Juelich, ZAM Germany                                 **
#****************************************************************************
#*
#*  Permission to use, copy, modify, and distribute this software and its
#*  documentation for any purpose and without fee is hereby granted,
#*  provided that the above copyright notice appear in all copies and that
#*  both that copyright notice and this permission notice appear in
#*  supporting documentation, and that the name of University of Oregon (UO)
#*  and Los Alamos National Laboratory (LANL) not be
#*  used in advertising or publicity pertaining to distribution of
#*  the software without specific, written prior permission.  The
#*  University of Oregon and LANL makes no representations about the
#*  suitability of this software for any purpose.  It is provided "as is"
#*  without express or implied warranty.
#*
#*  THE UNIVERSITY OF OREGON AND LANL DISCLAIMS ALL WARRANTIES WITH REGARD TO
#*  THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
#*  FITNESS, IN NO EVENT SHALL THE UNIVERSITY OF OREGON OR LANL BE LIABLE FOR
#*  ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
#*  RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
#*  CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
#*  CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#****************************************************************************

cwd=`pwd`

download()
{
  src="$1"
  dst="$2"
  local="$cwd/external_dependencies/$dst"
  echo "Looking for $local..."
  if [ -e $local ] ; then
    echo "Found $local, using it."
    cp $local $dst
  elif which wget >/dev/null 2>&1 ; then
    echo "wget: $src ==> $dst"
    wget --no-check-certificate -O "$dst" "$src"
  elif which curl >/dev/null 2>&1 ; then
    echo "curl: $src ==> $dst"
    curl -L "$src" > "$dst"
  else
    echo "ERROR: neither curl nor wget found in your path."
    exit 199
  fi
}


my_fail_check()
{
        retval=$?
        if [ $retval -ne 0 ];
          then
                  echo "Command failed"
              exit 1
          fi
}


clone()
{
  src="$1"
  dst="$2"
  echo "git clone $src" "$dst"
  git clone "$src" "$dst"
}

# This function is to extract the cflags from mpi compiler wrappers
# and try to be more sophisticated than:
# args=`mpicxx -show | cut -d' ' -f2- | sed -e 's@ @#@g'`

get_cflags() {
    compiler=$1
    option=$2
    args=`$compiler $option | cut -d' ' -f2-`
    #echo "$compiler flags: $args"
    _got_cflags=""

    for word in $args; do
        #echo "$word"
        if [[ "$word" =~ ^-Wl,.* ]]; then
            : # do nothing
        elif [[ "$word" =~ ^-l.* ]]; then
            : # do nothing
        elif [[ "$word" =~ ^-L.* ]]; then
            : # do nothing
        else
            _got_cflags="$_got_cflags $word"
        fi
    done
    _got_cflags=`echo $_got_cflags | sed -e 's@ @#@g'`
    #echo "Found MPI compiler flags: $_got_cflags"
}

# This function is to extract the ldflags from mpi compiler wrappers
# and try to be more sophisticated than:
# args=`mpicxx -show | cut -d' ' -f2- | sed -e 's@ @#@g'`
get_ldflags() {
    compiler=$1
    option=$2
    args=`$compiler $option | cut -d' ' -f2-`
    #echo "$compiler flags: $args"
    _got_ldflags=""

    for word in $args; do
        #echo "$word"
        if [[ "$word" =~ ^-Wl,.* ]]; then
            _got_ldflags="$_got_ldflags $word"
        elif [[ "$word" =~ ^-l.* ]]; then
            _got_ldflags="$_got_ldflags $word"
        elif [[ "$word" =~ ^-L.* ]]; then
            _got_ldflags="$_got_ldflags $word"
        elif [[ "$word" =~ ^-I.* ]]; then
            : # do nothing
        else
            _got_ldflags="$_got_ldflags $word"
        fi
    done
    _got_ldflags=`echo $_got_ldflags | sed -e 's@ @#@g'`
    #echo "Found MPI linker flags: $_got_ldflags"
}

#get_cflags "mpicxx" "-show"
#get_ldflags "mpicxx" "-show"
#echo "cflags = $_got_cflags"
#echo "ldflags = $_got_ldflags"
#get_cflags "cc" "--cray-print-opts=all"
#get_ldflags "cc" "--cray-print-opts=all"
#echo "cflags = $_got_cflags"
#echo "ldflags = $_got_ldflags"

get_cray_mpiflags() {
    my_mpicxx_exe=CC
    my_mpif90_exe=ftn
    mpishow_option="--cray-print-opts=all"
    yet_another_option="-I$CRAY_MPICH_PREFIX/include#"
    get_cflags $my_mpicxx_exe $mpishow_option
    get_ldflags $my_mpicxx_exe $mpishow_option
    mpiinc="${yet_another_option}${_got_cflags}"
    mpilibargs=$_got_ldflags
    # We have to make sure we set the mpilibrary variable here, because later
    # mpilib might get reset.
    mpilibrary=$_got_ldflags
    get_ldflags $my_mpif90_exe $mpishow_option
    mpiflibargs=`echo $_got_ldflags | sed  -e 's@-qthreaded@@g'`
    echo "Found $mpicxx_exe cflags: $mpiinc"
    echo "Found $mpicxx_exe ldflags: $mpilibargs"
    echo "Found $mpif90_exe ldflags: $mpiflibargs"

    mpilibargs="-L$tautoplevel/$machine/lib#-lTauMpi\$(TAU_CONFIG)#$mpilibargs"
    mpiflibargs="-L$tautoplevel/$machine/lib#-lTauMpi\$(TAU_CONFIG)#$mpiflibargs"
    fixmakeargs="$fixmakeargs mpilibargs=$mpilibargs mpiflibargs=$mpiflibargs mpiincargs=$mpiinc"
    mpilibargs_added_to_fixmakeargs=yes
}

# This function checks the cmake version and even does a "module load cmake" if we don't have a required version. It takes 3 args: major minor "string" and returns 0 if success
# and 1 if error
#check_cmake 3 14 "LLVM plugin"
#result=$?
#if [ $result -eq 0 ]; then
#  echo "Success: building LLVM plugin"
#fi
function check_cmake() {
  #check if CMake exists
  cmake_major_version=`cmake --version | head -1 |  sed "s/[^0-9.]*\([0-9.]*\).*/\1/" | cut -d. -f1`
  cmake_minor_version=`cmake --version | head -1 |  sed "s/[^0-9.]*\([0-9.]*\).*/\1/" | cut -d. -f2 `
  cmake_cmd=`which cmake`

  echo "CMAKE VERSION: $cmake_major_version $cmake_minor_version"
  echo "WHICH CMAKE: $cmake_cmd"
  load_cmake=0

  if [ "x$cmake_cmd" = "x" ] ||  (($cmake_major_version < $1))  ; then
    load_cmake=1
  else
    if (($cmake_major_version == $1))  &&  (($cmake_minor_version < $2)) ; then
      load_cmake=1
    fi
  fi

  # Try loading the cmake module if it exists
  if (($load_cmake == 1)); then
      echo "ERROR: cmake version $1.$2+ is required for $3. Trying module load cmake."
      module avail cmake 1> /dev/null 2>&1
      result=$?
      if [ $result -eq 0 ]; then
        module load cmake;
      fi;
  fi
  cmake_major_version=`cmake --version | head -1 |  sed "s/[^0-9.]*\([0-9.]*\).*/\1/" | cut -d. -f1`; cmake_minor_version=`cmake --version | head -1 |  sed "s/[^0-9.]*\([0-9.]*\).*/\1/" | cut -d. -f2 `; cmake_cmd=`which cmake`

  if [ "x$cmake_cmd" = "x" ] || [ -z "$cmake_major_version" ]; then
    echo "ERROR: Failed to determine cmake version. Is cmake in your PATH and executable?"
    return 1
  fi

  if (($cmake_major_version == $1)) ; then
    if (($cmake_minor_version >= $2)) ; then
      echo "TAU: CMAKE version check passes. Using $cmake_major_version.$cmake_minor_version  "
      return 0
    else
      echo "ERROR: cmake version $1.$2+ is required for $3. Please load cmake version $1.$2+ and retry"
      return 1
    fi
  else
# cmake major is != $1
    if (($cmake_major_version < $1)); then
      echo "ERROR: cmake version $1.$2+ is required for $3. Even after module load cmake, this version is inadequate. Please load cmake version $1.$2+ and retry"
      return 1
    else
# cmake major > $1
      echo "Success: Using CMAKE version: $cmake_major_version.$cmake_minor_version"
      return 0
    fi
  fi
# should not have reached here
  return 1
}


# Install SOS dependencies such as EVPath
installsosdeps()
{
 installdir="$1"


 if [ -d $installdir ]; then
         echo "sos install directory exists, trying to remove sos_flow to compile it again"
         if [ -d $installdir/sos_flow_master ]; then
                 rm -rf $installdir/sos_flow_master
         fi
 else
         mkdir $installdir
 fi

 cd $installdir
 P=`pwd`

 #check if CMake exists
 check_cmake 3 0 "SOS"
 result=$?
 if [ $result -eq 1 ]; then
   echo 'ERROR: cmake version 3 or higher required to build SOS dependencies, exiting.'
   exit 1;
 fi

 echo "Install SOS deps"
 if [ -d chaos ]; then
         echo "chaos directory exists"
 else
         mkdir chaos
 fi
 cd chaos
 currdir=`pwd`


 #wget https://www.sqlite.org/2017/sqlite-autoconf-3210000.tar.gz

 if [ -f sqlite3/bin/sqlite3 ]  &&  [ -f sqlite3/include/sqlite3.h ] &&  [ -f sqlite3/lib/libsqlite3.a ] ; then
         echo "sqlite3 already installed"
 else
         echo "Install Sqlite3"
         mkdir sqlite3
         download http://www.cs.uoregon.edu/research/paracomp/tau/tauprofile/dist/sos/sqlite-autoconf-3210000.tar.gz sqlite-autoconf-3210000.tar.gz
         tar -xvzf sqlite-autoconf-3210000.tar.gz
         rm sqlite-autoconf-3210000.tar.gz
         cd sqlite-autoconf-3210000
         ./configure --prefix=$currdir/sqlite3
         my_fail_check
         make -j
         make install
         my_fail_check
         cd ..
         rm -rf sqlite-autoconf-3210000
 fi

 if [ $sos_comm = evpath ]
 then
        #wget http://www.cc.gatech.edu/systems/projects/EVPath/chaos_bootstrap.pl
        #export FLEXPATH_DIR=$currdir/inst
        #perl ./chaos_bootstrap.pl adios-1.13 $currdir/inst
        #perl ./chaos_build.pl
        mkdir build_area
        cd build_area

        echo "Download SOS dependencies: $2 $3"
        download "$2" "$3"
        echo "Extract SOS dependencies: $3"
        tar -xvf "$3"
        rm -f "$3"

        #Install dill
        cd dill/source
        cmake  -DENABLE_TESTING=0 -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=$currdir/inst $currdir/build_area/dill/source
        my_fail_check
        make all install -j
        my_fail_check
        cd ../..

        #Install cercs_env
        cd cercs_env/source
        cmake  -DENABLE_TESTING=0 -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=$currdir/inst $currdir/build_area/cercs_env/source
        my_fail_check
        make all install -j
        my_fail_check
        cd ../..

        #Install atl
        cd atl/source
        cmake  -DENABLE_TESTING=0 -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=$currdir/inst $currdir/build_area/atl/source
        my_fail_check
        make all install -j
        my_fail_check
        cd ../..

        #Install ffs
        cd ffs/source
        cmake  -DENABLE_TESTING=0 -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=$currdir/inst $currdir/build_area/ffs/source
        my_fail_check
        make all install -j
        my_fail_check
        cd ../..

        #Install enet
        cd enet/source
        $currdir/build_area/enet/source/configure  CFLAGS='-g -O0' CPPFLAGS='-g -O0' --prefix=$currdir/inst
        my_fail_check
        make install -j
        my_fail_check
        cd ../..

        #Install evpath
        cd evpath/source
        cmake  -DENABLE_TESTING=0 -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=$currdir/inst $currdir/build_area/evpath/source
        my_fail_check
        make all install -j
        my_fail_check
        cd ../../..

         # set path to Flexpath/EVPath (see above)
         export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:${FLEXPATH_DIR}/lib/pkgconfig

         # Copy EVPath cmake file to its inst directory
         cd inst/lib/cmake/EVPath
         cp EVPathConfig.cmake ../../../
         cp EVPathTargets.cmake ../../../
         cd ../../../..


    echo "Install libfabric"
     #git clone https://github.com/ofiwg/libfabric.git
     download http://www.cs.uoregon.edu/research/paracomp/tau/tauprofile/dist/sos/libfabric.zip libfabric.zip
     unzip libfabric.zip
     cd libfabric
     chmod +x autogen.sh
     ./autogen.sh
     currdir=`pwd`
     ./configure --prefix=$currdir/inst
     my_fail_check
     make install -j
     my_fail_check
     cd inst/lib
     cp libfabric.* ../../../inst/lib
     cd ../../../..
 fi

 cd $P

 set +x

}

# Install SOS
installsos()
{
 echo "installsos - current dir: `pwd`"
 #src="$1"
 dst="$1"
 #cd ../..
 #cd $dst
 currdir=`pwd`

 # Check if SQLITE3 is available
 #sqlite3_cmd=`which sqlite3`

 #if [ "x$sqlite3_cmd" = "x" ] ; then
 #  echo 'ERROR: SQLITE3 required to build SOS, exiting.'
 #  exit 1;
 #fi

 cd $dst
 #git checkout merge_of_doom
 mkdir build-linux
 cd build-linux

 export sosdir=$currdir/$dst/inst
 echo "EVPath Inst dir: $targetdir/$architecture/sos/chaos/inst"
 echo "$sos_comm"
 echo "$c_compiler"
 echo "$cxx_compiler"
 case $sos_comm in

    sockets)
             echo "Compile SOS with sockets"
             cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_INSTALL_PREFIX=$currdir/$dst/inst -DSOS_ENABLE_PYTHON=OFF -DSQLite3_DIR=$targetdir/$architecture/sos/chaos/sqlite3 -DCMAKE_C_COMPILER=$c_compiler -DCMAKE_CXX_COMPILER=$cxx_compiler -DSOS_CLOUD_SYNC_WITH_SOCKET=ON -DSOS_CLOUD_SYNC_WITH_MPI=OFF -DSOS_CLOUD_SYNC_WITH_ZEROMQ=OFF -DSOS_CLOUD_SYNC_WITH_EVPATH=OFF ..
       retval=$?
               ;;
    mpi)
       echo "Compile SOS with mpi"
             cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_INSTALL_PREFIX=$currdir/$dst/inst -DSOS_ENABLE_PYTHON=OFF -DSQLite3_DIR=$targetdir/$architecture/sos/chaos/sqlite3 -DCMAKE_C_COMPILER=$c_compiler -DCMAKE_CXX_COMPILER=$cxx_compiler -DSOS_CLOUD_SYNC_WITH_SOCKET=OFF -DSOS_CLOUD_SYNC_WITH_MPI=ON -DSOS_CLOUD_SYNC_WITH_ZEROMQ=OFF -DSOS_CLOUD_SYNC_WITH_EVPATH=OFF ..
       retval=$?
        ;;
    evpath)
       echo "Compile SOS with EVPath"
             cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_INSTALL_PREFIX=$currdir/$dst/inst -DSOS_ENABLE_PYTHON=OFF -DEVPath_DIR=$targetdir/$architecture/sos/chaos/inst -DSQLite3_DIR=$targetdir/$architecture/sos/chaos/sqlite3 -DCMAKE_C_COMPILER=$c_compiler -DCMAKE_CXX_COMPILER=$cxx_compiler -DSOS_CLOUD_SYNC_WITH_SOCKET=OFF -DSOS_CLOUD_SYNC_WITH_MPI=OFF -DSOS_CLOUD_SYNC_WITH_ZEROMQ=OFF -DSOS_CLOUD_SYNC_WITH_EVPATH=ON ..
       retval=$?

        ;;

#    zeromq)
#        echo "Compile SOS with zeromq"
#        cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_INSTALL_PREFIX=$currdir/$dst/inst -DSOS_ENABLE_PYTHON=OFF -DEVPath_DIR=$targetdir/$architecture/sos/chaos/inst -DSQLite3_DIR=$targetdir/$architecture/sos/chaos/sqlite3 -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ -DSOS_CLOUD_SYNC_WITH_SOCKET=OFF -DSOS_CLOUD_SYNC_WITH_MPI=OFF -DSOS_CLOUD_SYNC_WITH_ZEROMQ=ON -DSOS_CLOUD_SYNC_WITH_EVPATH=OFF ..
#        ;;
 esac


  if [ $retval -eq 0 ];
  then
      echo "Compiling sos_flow..."
  else
      echo "Error cmake SOS_FLOW"
      exit 1
  fi


# cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_INSTALL_PREFIX=$currdir/$dst/inst -DEVPath_DIR=$targetdir/$architecture/sos/chaos/inst -DSQLite3_DIR=$targetdir/$architecture/sos/chaos/sqlite3 -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ -DSOS_CLOUD_SYNC_WITH_EVPATH=TRUE ..

  make -j ;
  retval=$?
  if [ $retval -eq 0 ];
  then
      echo "Compiled sos_flow"
  else
      echo "Error compiling SOS_FLOW"
      exit 1
  fi
 make install
}


fix_tilde_at_the_start_of_cube_path()
{
    cubedir=`echo $cubedir | sed -e "s,^~,$HOME,"g`
}

restore_perfdmf_and_paraprof()
{
    restore_paraprof
    restore_perfdmf
}


correction_perfdmf_and_paraprof()
{
    correction_paraprof
    correction_perfdmf
}



copy_own_cube_reader_if_selected()
{
    if test "x$cubedir" = "xown"; then
        confirmation_copy_of_own_jar_file
    fi
}

save_configure_env()
{
  # assuming configure is called from same directory
  # tau's configure doesn't work anyway from another directory
  _tauroot="."
  confenvdir="$_tauroot/.configure_env"

  md5sum="`which md5sum 2>/dev/null || which md5 2>/dev/null`"
  if [ "x$md5sum" == "x" ] ; then
    echo "Warning: can't find md5sum or md5."
    return
  fi
  argsum=`echo "$all_args" | $md5sum | awk '{print $1}'`

  mkdir -p "$confenvdir"
  confenvfile="$confenvdir/$argsum"
  set > "$confenvfile"
  fixmakeargs="$fixmakeargs confenvfile=$confenvfile"
}


select_cube_reader_jar()
{
    if test "x$cubedir" != "xown"; then

        check_another_cube_installation
        check_cube_installation

        #at that point cubedir is either correct or empty
        if test "x$cubedir" = "x"; then
            warning_about_missing_cube_installation
        fi

    else
        # select the own classpath
        cubeclasspath='${TAUROOT}/${MACHINE}/lib/CubeReader.jar'
        makecubeclasspath="../contrib/CubeReader.jar"
    fi
    return 0
}


check_another_cube_installation()
{
    # check if CUBE in PATH
    testline=`cube_config.sh --name 2>&1`
    another_cube_present="no"
    if test "x$testline" = "xCUBE"; then
        another_cube_present="yes"
        another_cube_dir=`cube_config.sh --cube-dir`
        if test "x$another_cube_dir" != "x$cubedir"; then
            warning_about_another_cube_installation $another_cube_dir
            return 1
        fi
    fi

    # check if CUBE is not in PATH , but CUBE_DIR is set
    if test "x$CUBE_DIR" != "x"; then
        testline=`$CUBE_DIR/bin/cube_config.sh --name 2>&1`
        another_cube_present="no"
        if test "x$testline" = "xCUBE"; then
            another_cube_present="yes"
            another_cube_dir=`$CUBE_DIR/bin/cube_config.sh --cube-dir`
            if test "x$another_cube_dir" != "x$cubedir"; then
                warning_about_another_cube_installation $another_cube_dir
                return 1
            fi
        fi
    fi
    return 0
}


check_cube_installation()
{

    # check if cubedir points to CUBE
    if test "x$cubedir" != "x"; then
        if test "x$cubedir" = "xext"; then
            if test "x$another_cube_dir" = "x"; then
                warning_about_general_missing_cube_installation
            else
                cubedir=$another_cube_dir
            fi
        fi

        fix_tilde_at_the_start_of_cube_path

        testline=`$cubedir/bin/cube_config.sh --name 2>&1`
        cuberoot=$cubedir
        #in the case one spezified /lib/directory of CUBE installation
        if test "x$testline" != "xCUBE"; then
            testline=`$cubedir/../bin/cube_config.sh --name 2>&1`
            cuberoot=$cubedir/../
        fi
        if test "x$testline" = "xCUBE"; then
            #check if the cube installation was done with java reader
            cubeclasspath=`$cuberoot/bin/cube_config.sh --classpath`
            makecubeclasspath=$cubeclasspath
            if [ "x$cubeclasspath" != "x" -a  -f $cuberoot/lib/CubeReader.jar ] ; then
                confirm_proper_cube_installation $cuberoot
            fi
        else
            warning_about_missing_cube_installation $cubedir
        fi
    fi
    return 0
}



correction_paraprof()
{
    # assuming configure is called from same directory
    # tau's configure doesn't work anyway from another directory

    cd ${tauroot}

    _tauroot="."
    echo "Adjust ParaProf to use CubeReader..."
    cat    ${_tauroot}/tools/src/paraprof/bin/paraprof.skel |
    sed -e 's,@CUBECLASSPATH@,'$cubeclasspath','  >   ${_tauroot}/tools/src/paraprof/bin/paraprof.new
    mv   ${_tauroot}/tools/src/paraprof/bin/paraprof.new   ${_tauroot}/tools/src/paraprof/bin/paraprof.skel

    cat   ${_tauroot}/tools/src/paraprof/Makefile |
    sed -e 's,@CUBECLASSPATH@,'$makecubeclasspath','  >  ${_tauroot}/tools/src/paraprof/Makefile.new
    mv ${_tauroot}/tools/src/paraprof/Makefile.new  ${_tauroot}/tools/src/paraprof/Makefile


}

correction_perfdmf()
{
    echo "Adjust PerfDmf to use CubeReader..."
    cat  ${_tauroot}/tools/src/perfdmf/Makefile.skel |
    sed -e 's,@CUBECLASSPATH@,'$makecubeclasspath','  >${_tauroot}/tools/src/perfdmf/Makefile.new
    mv ${_tauroot}/tools/src/perfdmf/Makefile.new ${_tauroot}/tools/src/perfdmf/Makefile
}



restore_paraprof()
{
    # assuming configure is called from same directory
    # tau's configure doesn't work anyway from another directory
    _tauroot="."
    echo "Unset ParaProf's cubeclasspath..."
    cat   ${_tauroot}/tools/src/paraprof/bin/paraprof.skel |
    sed -e 's,^CUBE_JAVA_READER=.*$,CUBE_JAVA_READER=@CUBECLASSPATH@,'  >  ${_tauroot}/tools/src/paraprof/bin/paraprof.new
    mv  ${_tauroot}/tools/src/paraprof/bin/paraprof.new  ${_tauroot}/tools/src/paraprof/bin/paraprof.skel

    cat  ${_tauroot}/tools/src/paraprof/Makefile |
    sed -e 's,^CUBE_JAVA_READER=.*$,CUBE_JAVA_READER=@CUBECLASSPATH@,'  > ${_tauroot}/tools/src/paraprof/Makefile.new
    mv ${_tauroot}/tools/src/paraprof/Makefile.new ${_tauroot}/tools/src/paraprof/Makefile


}

restore_perfdmf()
{
    # assuming configure is called from same directory
    # tau's configure doesn't work anyway from another directory
    _tauroot="."
    _tauroot="."
    echo "Unset Perfdmf cubeclasspath..."
    cat  ${_tauroot}/tools/src/perfdmf/Makefile |
    sed -e 's,^CUBE_JAVA_READER=.*$,CUBE_JAVA_READER=@CUBECLASSPATH@,'  > ${_tauroot}/tools/src/perfdmf/Makefile.new
    cp ${_tauroot}/tools/src/perfdmf/Makefile.new ${_tauroot}/tools/src/perfdmf/Makefile
}




confirmation_copy_of_own_jar_file()
{
    echo "===================================================="
    if [ -f ${tauroot}/tools/src/contrib/CubeReader.jar ]  ; then
        echo " Copy ${tauroot}/tools/src/contrib/CubeReader.jar to $JARTARGET"
        cp ${tauroot}/tools/src/contrib/CubeReader.jar $JARTARGET
    else
        echo "ERROR: Tau doesn't have own copy of CubeReader.jar under ${tauroot}/tools/src/contrib/"
        echo "       Please check the recent TAU distribution and report this bug to tau-bugs@cs.uoregon.edu"
        exit -1
    fi
    echo "===================================================="
    return 0

}



warning_about_another_cube_installation()
{
    echo "===================================================="
    echo "WARNING: CUBE is detected under $1"
    echo "===================================================="
    return 0
}


confirm_proper_cube_installation()
{
    echo "===================================================="
    echo "CUBE Reader is found under $1/lib."
    echo "CLASSPATH of it is $cubeclasspath"
    echo "===================================================="
    return 0
}

warning_about_missing_cube_installation()
{
    echo "===================================================="
    echo "ERROR: Cannot find CUBE installation under $1"
    echo "       Please check your option -cube=$cubedir"
    echo "Abort."
    echo "===================================================="
    exit -1
}

warning_about_general_missing_cube_installation()
{
    echo "===================================================="
    echo "ERROR: Cannot find CUBE installation."
    echo "       Please specify precise place of CUBE using option -cube=$cubedir"
    echo "Abort."
    echo "===================================================="
    exit -1
}


usage() {
    echo "TAU Configuration Utility "
    echo "***********************************************************************"
    echo "Usage: configure [OPTIONS]"
    echo "  where [OPTIONS] are:"
    echo ""
    echo "Compiler Options:"
    echo "-c++=<compiler>  ............................ specify the C++ compiler."
    echo "    options [CC|KCC|g++|*xlC*|cxx|pgc++|pgcpp|FCC|guidec++|aCC|c++|ecpc|"
    echo "    armclang++|clang++|bgclang++|hcc|g++-*|icpc|icpx|scgcc|scpathCC|pathCC"
    echo "    nvcpp|orCC|hipcc|amdclang++]."
    echo "-cc=<compiler> ................................ specify the C compiler."
    echo "     options [cc|gcc|gcc-*|clang|bgclang|gcc4|scgcc|KCC|pgcc|guidec|*xlc*|ecc|"
    echo "         armclang|pathcc|orcc|pgc|hipcc|amdclang]."
    echo "-fortran=<compiler> ..................... specify the Fortran compiler."
    echo "   options    [gnu|sgi|ibm|ibm64|hp|cray|pgi|absoft|fujitsu|sun|compaq|"
    echo "   flang|f18|xlflang|armflang|g95|open64|kai|nec|hitachi|intel|absoft|lahey|"
    echo "                nagware|pathscale|gfortran|gfortran-*|gfortran4|amdflang]"
    echo "-pdt=<dir> ........ Specify location of PDT (Program Database Toolkit)."
    echo "-pdt_c++=<compiler>  ............ specify a different PDT C++ compiler."
    echo "    options [CC|KCC|g++|*xlC*|cxx|pgc++|pgcpp|FCC|guidec++|aCC|c++|ecpc|"
    echo "                                     g++-*|g++4|icpc|icpx|scgcc|pathCC|orCC]."
    echo "-useropt='<param>' .......... arguments to compilers (defaults to -O2)."
    echo ""
    echo "Installation Options:"
    echo "-prefix=<dir> ................ Specify a target installation directory."
    echo "-bfd=<dir | download> ....... Specify a binutils directory or download."
    echo "    Note: 'download' will download and build the library automatically."
    echo "-unwind=<dir | download> ... Specify a libunwind directory or download."
    echo "    Note: 'download' will download and build the library automatically."
    echo "-unwind_gcc ............ Specify gcc as the compiler to build libuwind."
    echo "-static_libunwind ............ Use libunwind.a instead of libunwind.so."
    echo "-zlib=<dir | download> .......... Specify a zlib directory or download."
    echo "    Note: 'download' will download and build the library automatically."
    echo ""
    echo "MPI Options:"
    echo "-mpi .......................... Specify use of TAU MPI wrapper library."
    echo "-mpiinc=<dir> ............. Specify location of MPI include dir and use"
    echo "                           the TAU MPI Profiling and Tracing Interface."
    echo "-mpilib=<dir> ............. Specify location of MPI library dir and use"
    echo "                           the TAU MPI Profiling and Tracing Interface."
    echo "-mpilibrary=<library> ................ Specify a different MPI library."
    echo "            e.g., -mpilibrary=-lmpi_r                                  "
    echo ""
    echo "OpenMP Options:"
    echo "-openmp ........................................... Use OpenMP threads."
    echo "-opari .................................................... Use Opari2."
    echo "-ompt ............................................ Enable OMPT support."
    echo ""
    echo "OpenACC Options:"
    echo "-openacc ................................................. Use OpenACC."
    echo "-openacclib=<dir> .... Specify location of the OpenACC library and use."
    echo "                           the TAU MPI Profiling and Tracing Interface."
    echo ""
    echo "GPGPU Options:"
    echo "-cuda=<dir> ............... Specify location of the top level CUDA SDK."
    echo "directory with include subdirectory. Enables OpenCL and CUDA profiling."
    echo "-rocm[=<dir>] ..... Specify alternate path to ROCm (/opt/rocm default)."
    echo "-rocprofiler=<dir> .............. Specify location of ROCm rocprofiler."
    echo "-rocprofv2 .......................................... Enable rocprofv2."
    echo "-rocprofsdk[=<dir>] ................................Enable rocprof-sdk."
    echo "                      -elfutils is required to Enable ROCm PC Sampling."
    echo "-rocmsmi=<dir> ..................... Specify location of RoCm rocm_smi."
#   echo "-rocprofilerinc=<dir> .... Specify location of rocprofiler include dir."
#   echo "-rocprofilerlibrary=<library> .... Specify name of rocprofiler library."
    echo "-level_zero[=<dir>] ................. Specify path to Intel Level Zero."
    echo "-level_metrics[=<dir>]... Specify path to Level Zero Metrics Discovery."
    echo "-force_new_l0 .................... Force the use of the new L0 profiler"
    echo "-force_legacy_l0 ...............Force the use of the legacy L0 profiler"
    echo "-roctracer=<dir> .................. Specify location of ROCm roctracer."
    echo "-hip=<dir> .................... Specify location of ROCm HIP directory."
    echo ""
    echo "Other Options:"
    echo "-iowrapper .................................... Build POSIX IO Wrapper."
    echo "-pthread .................................. Use pthread thread package."
    echo "-no_pthread_create ................... Suppress pthread_create wrapper."
    echo "-papi=<dir> ............... Specify location of PAPI (Performance API)."
    echo "-likwid=<dir> ............................. Specify location of LIKWID."
    echo "-otf=<dir> ....... Specify location of Open Trace Format (OTF) Package."
    echo "-perfetto ............................... Enable Perfetto trace output."
    echo "-darshan=<dir> ............... Specify location of Darshan I/O package."
    echo "-python .. Automatically choose python options based on Python in path."
    echo "-python=<interp> .... Choose Python options based on named interpreter."
    echo "-python3  Automatically choose python options based on Python3 in path."
    echo "-pythoninc=<dir> ........ Specify location of Python include directory."
    echo "-pythonlib=<dir> ............ Specify location of Python lib directory."
    echo "-pythonlibrary=<library> ......... Specify name of Python library file."
    echo "                                   e.g. libpython3.10.so"
    echo "-julia ......................... Install Julia instrumentation library."
    echo "-tag=<unique name> ........ Specify a tag to identify the installation."
    echo "-mpit ............................... Enable MPI-T profiling interface."
    echo "-starpu[=<dir>] ................................ Enable StarPU support."
    echo "-phiprof ................................... Activate Phiprof bindings."
    echo "-llvm_src=<dir> ......................... Path to the LLVM source tree."
    echo "                            (necessary for the instrumentation plugin)."
    echo "-llvm_cxx=<dir................. Compiler that was used to compile LLVM."
    echo "                            (necessary for the instrumentation plugin)."
    echo "-syscall ......................................  Build SYSCALL Wrapper."
    echo ""
    echo "-help ...................................... display this help message."
    echo "-fullhelp .............................. display the full help message."
    echo ""
    echo "More advanced (and uncommon) options are available, use -fullhelp to see them."
}

default=false

for arg in "$@"; do
    case $arg in
        default)
            default=true
            ;;
        -help)
            usage
            exit
            ;;
        -h)
            usage
            exit
            ;;
        --help)
            usage
            exit
            ;;

        -fullhelp)
        echo "TAU Configuration Utility "
        echo "***********************************************************************"
        echo "Usage: configure [OPTIONS]"
        echo "  where [OPTIONS] are:"
        echo "-c++=<compiler>  ............................ specify the C++ compiler."
        echo "   options [CC|KCC|g++|*xlC*|cxx|pgc++|pgcpp|FCC|guidec++|aCC|c++|ecpc|"
        echo "       armclang++|clang++|bgclang++|hcc|hipcc|icpc|icpx|scgcc|scpathCC|"
        echo "       pathCC|orCC|hipcc|amdclang++].                                  "
        echo "-cc=<compiler> ................................ specify the C compiler."
        echo "   options [cc|gcc|gcc4|scgcc|clang|hcc|pgcc|guidec|*xlc*|ecc|pathcc|  "
        echo "           orcc| armclang|hipcc|amdclang].                             "
        echo "-fortran=<compiler> ..................... specify the Fortran compiler."
        echo "   options    [gnu|sgi|ibm|ibm64|hp|cray|pgi|absoft|fujitsu|sun|compaq|"
        echo "   g95|open64|kai|nec|hitachi|intel|absoft|lahey|nag|nagware|pathscale|"
        echo "   flang|f18|xlflang|armflang|gfortran|gfortran-*|gfortran4|amdflang]  "
        echo "-upc=<compiler> ............................. specify the UPC compiler."
        echo "     options [upc|gcc(GNU UPC) |upcc (Berkeley UPC) | cc (Cray CCE UPC)"
        echo "-ansic ............................. Use ANSI C language specification."
        echo "-cfront ................................................... DEPRECATED."
        echo "-dec ............................ Use system default cc and CC for DEC."
        echo "-gnu .......................... Explicitly use GNU C and C++ compilers."
        echo "-ibm .......................... Explicitly use IBM C and C++ compilers."
        echo "-sgi ............................ Use system default cc and CC for SGI."
        echo "-host=<host-type> ................ the type of system on which TAU runs"
        echo "                                     this is used for cross-compilation"
        echo "-host_sysroot=<path> ............................ host compiler sysroot"
        echo "-hostutils..............................build TAU utils for <host-type>"
        echo "                                    this is used for cross-compilation."
        echo "-pdt=<dir> ........ Specify location of PDT (Program Database Toolkit)."
        echo "-pdt_c++=<compiler>  ............ specify a different PDT C++ compiler."
        echo "   options [CC|KCC|g++|*xlC*|cxx|pgc++|pgcpp|FCC|guidec++|aCC|c++|ecpc|"
        echo "                   clang++|bgclang++|g++4|icpc|icpx|scgcc|pathCC|orCC]."
        echo "-pdtcompdir=<compilerdir> . specify a different PDT compiler directory."
        echo "-pdtarchdir=<archdir> . specify a different PDT architecture directory."
        echo "-useropt='<param>' .......... arguments to compilers (defaults to -O2)."
        echo "-opt ................................................. Same as -useropt"
        echo "-extrashlibopts='<parameters>' ...... arguments for building libTAU.so."
        echo "-prefix=<dir> ................ Specify a target installation directory."
        echo "-exec-prefix=<arch> .......... Specify a target architecture directory."
        echo "-arch=<architecture> ................... Specify a target architecture."
        echo "       options      [xt3|craycnl|bgp|bgq|bgl|ibm64|ibm64linux|sunx86_64"
        echo "           nec-sx-aurora|crayxmt|solaris2-64|mips32|sgin32|sgi64|sgio32"
        echo "                                                 arm_linux|arm_android]"
        echo "-iowrapper .................................... Build POSIX IO Wrapper."
        echo "-ugni ..................................... Build UGNI network wrapper."
        echo "-chapel .................... Build Cray Chapel network atomics wrapper."
        echo "-ittnotify  .......................... Build Intel ITTNotify collector."
        echo "-gptl  ............................................ Build GPTL wrapper."
        echo "-gptl=e3sm  ............... Build GPTL wrapper with E3SM compatibility."
        echo "-pthread .................................. Use pthread thread package."
        echo "-no_pthread_create ................... Suppress pthread_create wrapper."
        echo "-papithread .................................. Use PAPI thread package."
        echo "-charm=<dir> .............................. Use charm++ thread package."
        echo "-sproc .................................. Use SGI sproc thread package."
        echo "-openmp ........................................... Use OpenMP threads."
        echo "-opari .................................................... Use Opari2."
        echo "-opari1 .................................................... Use Opari."
        echo "-opari_region .........Only report performance data for OpenMP regions."
        echo "-opari_construct .. Only report performance data for OpenMP constructs."
        echo "-oparicomp=<compiler>...Specify which compiler sutie to compile Opari2."
        echo "     options [gcc|ibm|intel|pathscale|pgi|studio]."
        echo "-ompt ............................................ Enable OMPT support."
        echo "-omptlib=<dir> ...................... Specify location of OMPT library."
        echo "-omptlibrary ......................... OMPT library name for link line."
        echo "-papi=<dir> ............... Specify location of PAPI (Performance API)."
        echo "-likwid=<dir> ............................. Specify location of LIKWID."
        echo "-beacon=<dir> ............................. Specify location of BEACON."
        echo "-pdt=<dir> ........ Specify location of PDT (Program Database Toolkit)."
        echo "-pin=<dir> ..... Specify location of Intel PIN instrumentation toolkit."
        echo "-jdk=<dir>  Build a library for profiling Java code with specified jdk."
        echo "-android_sdk=<dir> ................ Android SDK used for APK injection."
        echo "-android_version=VERSION ................. Android version, e.g. '4.4'."
        echo "-android_platform=VERSION ........ Android platform version, e.g. '19'."
        echo "-asmdex=<dir | download> ...... Specify a asmdex directory or download."
        echo "-boost=<dir> ....................... Specify location of Boost package."
        echo "-apex ......................................... Build the APEX package."
        echo "-zerosum ................................... Build the ZeroSum package."
        echo "-mochi ............................ Add MOCHI backend database support."
        echo "-dyninst=<dir> ................... Specify location of DynInst package."
        echo "-dyninstinc=<dir> ............ Specify location of DynInst include dir."
        echo "-dyninstlib=<dir> ................ Specify location of DynInst lib dir."
        echo "-dwarf=<dir> ................ Specify location of the libdwarf package."
        echo "-dwarflib=<dir> ... Specify location of the libdwarf library directory."
        echo "-elf=<dir> .................... Specify location of the libelf package."
        echo "-bfd=<dir | download> ....... Specify a binutils directory or download."
        echo "    Note: 'download' will download and build the library automatically."
        echo "-unwinder ....... Stack unwinder [libunwind,stackealker,backtrace,none]"
        echo "-unwindextras ............................. Link options for -unwinder."
        echo "-unwindinc=<dir> ...... Specify location of unwinder include directory."
        echo "-unwindlib=<dir> ................ Specify location of unwinder library."
        echo "-unwind=<dir | download> ... Specify a libunwind directory or download."
        echo "    Note: 'download' will download and build the library automatically."
        echo "-unwind_gcc ............ Specify gcc as the compiler to build libuwind."
        echo "-static_libunwind ............ Use libunwind.a instead of libunwind.so."
        echo "-zlib=<dir | download> .......... Specify a zlib directory or download."
        echo "    Note: 'download' will download and build the library automatically."
        echo "-cube=<dir> | own.| ext..... Specify location of CUBE or use TAU's jar."
        echo "-vtf=<dir> ......... Specify location of VTF3 Trace Generation Package."
        echo "-otf=<dir> ....... Specify location of Open Trace Format (OTF) Package."
        echo "-otfinc=<dir> ....... Specify location of stand-alone OTF header files."
        echo "-otflib=<dir> ...... Specify location of stand-alone OTF library files."
        echo "-ebs2otf ............... Enable conversion of EBS traces to OTF format."
        echo "-mpi .......................... Specify use of TAU MPI wrapper library."
        echo "-mpiinc=<dir> ............. Specify location of MPI include dir and use"
        echo "                           the TAU MPI Profiling and Tracing Interface."
        echo "-mpilib=<dir> ............. Specify location of MPI library dir and use"
        echo "                           the TAU MPI Profiling and Tracing Interface."
        echo "-mpilibrary=<library> ................ Specify a different MPI library."
        echo "            e.g., -mpilibrary=-lmpi_r                                  "
        echo "-openacc .................................................. Use OpenACC"
        echo "-openacclib=<dir> ..... Specify location of the OpenACC library and use"
        echo "                       the TAU OpenACC Profiling and Tracing Interface."
        echo "-shmem[=pshmem].............. Specify use of TAU SHMEM wrapper library."
        echo "-shmeminc=<dir> ......... Specify location of SHMEM include dir and use"
        echo "                         the TAU SHMEM Profiling and Tracing Interface."
        echo "-shmemlib=<dir> ......... Specify location of SHMEM library dir and use"
        echo "                           the TAU MPI Profiling and Tracing Interface."
        echo "-shmemlibrary=<library> ............ Specify a different SHMEM library."
        echo "            e.g., -shmemlibrary=-lsmac                                 "
        echo "-shmemmpiinc=<dir> ... Specify location of SHMEM MPI include directory."
        echo "-nocomm  ........ Disable tracking communication events in MPI library."
        echo "-cuda=<dir> ................ Specify location of the top level CUDA SDK"
        echo "directory with include subdirectory. Enables OpenCL and CUDA profiling."
        echo "-cudalibrary=<dir> ................ Additional options or library      "
        echo "            locations to add when linking to the cuda driver libraries."
        echo "-opencl=<dir> ............ Specify location of the top level OpenCL SDK"
        echo "         Note: This option is needed when used with -cuda=<dir> option."
        echo "-rocm[=<dir>] ..... Specify alternate path to ROCm (/opt/rocm default)."
        echo "-rocprofiler=<dir> .............. Specify location of ROCm rocprofiler."
        echo "-rocprofv2 .......................................... Enable rocprofv2."
        echo "-rocprofsdk[=<dir>] ................................Enable rocprof-sdk."
        echo "                      -elfutils is required to Enable ROCm PC Sampling."
        echo "-rocmsmi=<dir> ..................... Specify location of RoCm rocm_smi."
        echo "-rocm-core=<dir> ...... Specify location of ROCm Core to detect rocmv6."
        #echo "-rocprofilerinc=<dir> .... Specify location of rocprofiler include dir."
        #echo "-rocprofilerlibrary=<library> .... Specify name of rocprofiler library."
        echo "-roctracer=<dir> .................. Specify location of ROCm roctracer."
        echo "-rocm-core=<dir> ....... Specify location of ROCm Core to detect rocmv6"
        echo "-hip=<dir> .................... Specify location of ROCm HIP directory."
        echo "-level_zero[=<dir>]  ................ Specify path to Intel Level Zero."
        echo "-level_metrics[=<dir>] .. Specify path to Level Zero Metrics Discovery."
	    echo "-force_new_l0 .................... Force the use of the new L0 profiler"
	    echo "-force_legacy_l0 ...............Force the use of the legacy L0 profiler"
        echo "-hpctoolkit-src-dir=<dir> .......... Specify patched HPCToolkit source."
        echo "-hpctoolkit-install-dir=<dir> . Specify patched HPCToolkit Install dir."
        echo "-scalasca=<dir>  ................ Specify location of SCALASCA package."
        echo "-epilog=<dir>  ............ Specify location of EPILOG Tracing package."
        echo "-epiloglib=<dir> ........... Specify full path to EPILOG lib directory."
        echo "-epilogbin=<dir> ........... Specify full path to EPILOG bin directory."
        echo "-epiloginc=<dir> ....... Specify full path to EPILOG include directory."
        echo "-vampirtrace=<dir>  .. Specify location of VampirTrace Tracing package."
#    echo "-mon-mpi ..... Specify MPI as TAU monitoring transport layer.          "
#    echo "-mon-mrnet=<dir> .. Specify location to MRNet package as TAU monitoring"
#    echo "                    transport layer (ToM).                             "
#    echo "-mon-mrnet-lib=<dir> ............... Specify full path to MRNet library"
#    echo "                                     installation.                     "
#    echo "-mon-mrnet-use-lightweight ..... Instructs ToM to use MRNet lightweight"
#    echo "                                 interface.                            "
        echo "-scorep=<dir>  .................... Specify location of SCOREP package."
        echo "-upcnetwork=<value> .. Specify network (mpi, udp,...) for Berkeley UPC."
        echo "-gasnet=<dir>  ............ Specify location of GASNET for UPC wrapper."
        echo "-python .. Automatically choose python options based on Python in path."
        echo "-python=<interp> .... Choose Python options based on named interpreter."
        echo "-python3  Automatically choose python options based on Python3 in path."
        echo "-pythoninc=<dir> ........ Specify location of Python include directory."
        echo "-pythonlib=<dir> ............ Specify location of Python lib directory."
        echo "-pythonlibrary=<library> ......... Specify name of Python library file."
        echo "-julia ......................... Install Julia instrumentation library."
        echo "-starpu[=<dir>] ................................ Enable StarPU support."
        echo "-phiprof ................................... Activate Phiprof bindings."
        echo "-llvm_cc   Compiler to build TAU LLVM selective instrumentation plugin."
        echo "-llvm_src=<dir> ......................... Path to the LLVM source tree "
        echo "                             (necessary for the instrumentation plugin)"
        echo "-llvm_cxx=<dir.................. Compiler that was used to compile LLVM"
        echo "                             (necessary for the instrumentation plugin)"
        echo "-tag=<unique name> ........ Specify a tag to identify the installation."
        echo "-perfinc=<dir> ............. Specify a Perflib[LANL] include directory."
        echo "-perflib=<dir> ................. Specify a Perflib[LANL] lib directory."
        echo "-perflibrary=<library> ... Specify a different perflib runtime library."
        echo "-ktau ........................................... Kernel TAU Profiling."
        echo "-ktau_shctr ........................ Kernel TAU Profiling with shared- "
        echo "                                                   OS counters support."
        echo "-ktauinc=<dir> ............... Specify location of Linux Kernel source."
        echo "-ktauincuser=<dir> ............... Specify location of KTAU user incls."
        echo "-ktaulib=<dir> ............ Specify location of KTAU library libktau.a."
        echo "-ktausym=<path to System.map>  ...... Specify location of Linux Kernel."
        echo "                                                          Symbol Table."
        echo "-ktau-ng ....................... Enable new Generation Kernel Profiling"
        echo "-ktau_merge ...... Use Kernel Profiling with merging user-space profile"
        echo "-stff=<dir> .... Location of RENCI Scalable Time-series filter library."
        echo "-sddf=<dir> ...... SDDF library location; Only required for RENCI STFF."
        echo "-sos=<dir | download>................. Use Scalable Observation System."
        echo "-soscomm=<comm>..communicatiors between SOS daemons[mpi,sockets,evpath]"
            #    echo "-TRACE ..................................... Generate TAU event traces."
            #    echo "-MPITRACE ... Generate event traces for MPI events and their ancestors."
            #    echo "-PROFILE ............ Generate profiles (summary statistics) (default)."
            #    echo "-PROFILECALLPATH ......................... Generate call path profiles."
        echo "-PROFILEPARAM .... Generate profiles with parameter mapped event data ."
        echo "-PROFILEPATHS ........ Generate profiles with MPI path based profiling."
        echo "-PROFILECOMMUNICATORS ... Generate profiles with MPI communicator info."
        echo "-PROFILEPHASE .......................... Generate phase based profiles."
            #    echo "-DEPTHLIMIT ........... Disable instrumentation beyond a certain depth."
            #    echo "-PROFILEMEMORY .. Track heap memory utilization at each function entry."
            #    echo "-PROFILEHEADROOM .. Track memory free (or headroom) at each func entry."
            #    echo "-COMPENSATE ........ Compensate for profiling measurement perturbation."
        echo "-BGLTIMERS .... Use fast low-overhead timers on IBM BlueGene/L systems."
        echo "-BGPTIMERS .... Use fast low-overhead timers on IBM BlueGene/P systems."
        echo "-BGQTIMERS .... Use fast low-overhead timers on IBM BlueGene/Q systems."
        echo "-SGITIMERS .......... Use fast nanosecond timers on SGI R10000 systems."
        echo "-CRAYTIMERS ............ Use fast nanosecond timers on Cray X1 systems."
        echo "-LINUXTIMERS ......... Use low overhead TSC Counter for wallclock time."
        echo "-CPUTIME .......... Use usertime+system time instead of wallclock time."
        echo "-PAPIWALLCLOCK ........ Use PAPI to access wallclock time. Needs -papi."
        echo "-PAPIVIRTUAL   .......... Use PAPI for virtual (user) time calculation."
        echo "-INTELCXXLIBICC   ......... Use Intel -cxxlib-icc option for compiling."
        echo "-noex .................. Use no exceptions while compiling the library."
        echo "-noplugins ...................... Do not build support for TAU plugins."
        echo "-nolm ................. On Ubuntu, do not add -lm while linking in TAU."
        echo "-disable-no-pie-on-ubuntu .On Ubuntu, do not add -no-pie while linking."
        echo "-DISABLE_MEMORY_MANAGER ..................... Disable TAU heap manager."
        echo "                       (Note: also disables sampling, marker tracking)."
        echo "-DISABLESHARED .................. Do not build libTAU.so shared object."
        echo "-PMI ...................... Enable Cray PMI for tracking topology info."
        echo "-ENABLE_EXPORT_DYNAMIC .................... Force -Wl,--export-dynamic."
        echo "-ALPHATIMERS . Use Alpha clock_gettime for low overhead wallclock time."
        echo "-COMPENSATE ... Enable online compensation of performance perturbation."
        echo "-DEBUGPROF ............... Enable Debugging Mode for Profiling Library."
        echo "-DEPTHLIMIT  Limit instrumentation based calling routine's stack depth."
        echo "-DISABLE_OPENCL ....................................... Disable OpenCL."
        echo "-ENABLE_TAUENV ............... Build tau_env environment reporter tool."
        echo "-MPITRACE ........... Generate event traces for MPI calls and routines."
        echo "-MULTIPLECOUNTERS .. Track multiple sources (counters, CPU time, etc.)."
        echo "-PROFILE .............. Generate summary profile files after execution."
        echo "-PROFILECALLPATH ........................ Generates call path profiles."
        echo "-PROFILECALLS ............................................. DEPRECATED."
        echo "-PROFILECALLSTACK ......................................... DEPRECATED."
        echo "-PROFILEHEADROOM .................. Track memory available in the heap."
        echo "-PROFILEMEMORY ...... sample memory utilization at each function entry."
        echo "-PROFILESTATS .................. Enable standard deviation calculation."
        echo "-TRACE ....... Generate event-trace logs, rather than summary profiles."
        echo "-zeptodir=<dir> ............................. ZeptoOS install location."
        echo "                              See http://www-unix.mcs.anl.gov/zeptoos/."
        echo "-adios=<dir> ....................... Specify location of adios library."
        echo "-armci ................................. Track ARMCI events via PARMCI."
        echo "-cupti=<dir> ................ Specify location of NVIDIA CUPTI library."
        echo "-darshan ..................... Specify location of Darshan I/O package."
        echo "-dmapp ............................................. Enable Cray DMAPP."
        echo "-forceshared ............................. Always use shared libraries."
        echo "-gpi ........................ Specify use of TAU's GPI wrapper library."
        echo "-mpit ............................... Enable MPI-T profiling interface."
        echo "-perfsuite=<dir> ....................... Specify location of PerfSuite."
        echo "-setnode0 ........................ Set default node to 0 instead of -1."
        echo "-slog2=<dir> ...... Location of the SLOG2 SDK trace generation package."
        echo "-sqlite3 ................................. Specify location of SQLite3."
        echo "-tbb ......................................... Specify location of TBB."
        echo "-xlsmp ............................. Specify location of xlsmp library."
        echo "-syscall ....................................... Build SYSCALL Wrapper."
        echo "-help ...................................... display this help message."
        echo "-fullhelp .............................. display the full help message."
        exit
        ;;
    esac
done

# this causes `make install` to clean, since configure has been run
rm -f .clean

testlastcon=""
testlaststu=""
if [ -f .last_config ] ; then
    testlastcon=`cat .last_config`
fi

if [ -f .active_stub ] ; then
    testlaststu=`cat .active_stub`
fi


if [ $default = false ] ; then

    rm -f .last_config
    rm -f .active_stub
fi

# This method will escape spaces so that you can use ./configure `cat .last_config` when you've used them
for arg in "$@"; do
    # Thanks to Bernd Mohr for the following that handles quotes and spaces
    # -e 's/^x//'          removes the x at the start
    # -e 's/"/\\\"/g'      replaces " with \"
    # -e s,\',%@%\',g      replaces ' with %@%'
    # -e 's/%@%/\\\/g'     replaces %@% \
    # -e 's/ /\\\ /g'      replaces space with \space
    modarg=`echo "x$arg" | sed -e 's/^x//' -e 's/"/\\\"/g' -e s,\',%@%\',g -e 's/%@%/\\\/g' -e 's/ /\\\ /g'`
    modargs="$modargs $modarg"
done

if [ $default = false ] ; then
    #    echo "configured with:"
    #    echo ""
    #    echo "$modargs"
    #    echo ""
    if [ "$modargs" != "$testlastcon" ] ; then

        echo "$modargs" >> .all_configs

    fi
    echo "$modargs" > .last_config
fi

# test if we're using the bash shell
usingbash=no
readlink=`which readlink`
if [ "x$readlink" != "x" ] ; then
    if [ -x "$readlink" ] ; then
        symlink=`readlink /bin/sh`
        if [ "x$symlink" = "xbash" ] ; then
            usingbash=yes
        fi
    fi
fi

# write tau_config.h that's used as meta-data containing the TAU configuration
if [ "$usingbash" = "yes" ] ; then
    modmodargs=`echo "x$modargs" | sed -e 's/^x//' -e 's#\\\#\\\\\\\\#g'`
else
    modmodargs=`echo "x$modargs" | sed -e 's/^x//' -e 's#\\\#\\\\\\\\\\\#g'`
fi

echo "#define TAU_CONFIG \"$modmodargs\"" > include/tau_config.h

START_DIR=`pwd`

# Default compilers and options
c_compiler=default
cxx_compiler=default
orig_c_compiler=default
orig_cxx_compiler=default
orig_fortran_compiler=default
cxx_compiler_specified=no
c_compiler_specified=no
fortran_compiler_specified=no
full_fortran_compiler=default
pdt_cxx_compiler=default
fixmakeargs=""
useropt=-O2#-g
tag=no
pathscale=no
tautag=
orig_useropt="-O2 -g"
mpi=no
mpi2=no
mpiio=no
mpiinc=
mpilib=
mpilibargs_added_to_fixmakeargs=no
mpilibrary=no
mon_mpi=no
mrnet=no
mrnet_use_lightweight=no
mrnet_root=
mrnet_lib=
ibmmpi=no
shmem=no
pshmem=no
gpi=no
gpidir=
gpiinc=
gpilib=
gpilibrary=no
shmeminc=
shmemlib=
shmemlibrary=no
threadsafe=no
additional_mpiincs=
additional_mpilibs=
extra_linker_args=
pythoninc=
pythonlib=
python=no
python3=no
pythonlibrary=
pythonintv=python
python_ver_specified=no
sicortex=no
extradir=
extrashlibopts=
pthread=no
julia=no
suppress_pthread_create_wrapper=no
tbb=no
papithread=no
papipfm=no
gupc=no
papiperfctr=no
papisubdir=
charm=no
comm=yes
sproc=no
noex=no
fx_aarch64=no
plugins=yes
disableshared=no
pmi=no
setnode0=no
compensate=no
smarts=no
papi=no
likwid=no
beacon=no
beacondir="/"
darshan=no
tau_cray_arm64=no
enable_export_dynamic=no
perfsuite=no
perfsuitedir=
bfd=no
elf=no
systemlibelf=no
bfddir=
zlib=no
zlibdir=
elfdir=
turnbfdoff=no
perf=no
perfincdir=
perflibdir=
perflibrary=
tcltk=yes
tcltkfile="/"
kai=no
pgi=no
aocc=no
cray_nvhpc=no
nvhpc=no
craymic=no
fujitsu=no
guidef90=no
stdcxxlib=no
intelcxxlibicc=no
intelifort=no
intel_icpx=no
intel=no
inteloneapi=no
llvm=no
mpiwarnings=
gnu=no
sol2cc=no
suncc=no
compaq=no
open64=no
profile=no
profilestats=no
profilememory=no
profileheadroom=no
callpath=no
profileparam=no
profilepaths=no
profilecommunicators=no
depthlimit=no
phase=no
trace=no
mpit=no
mpitrace=no
debugprof=no
bgltimers=no
bgptimers=no
bgqtimers=no
sgitimers=no
craytimers=no
linuxtimers=no
alphatimers=no
cputime=no
papiwallclock=no
papivirtual=no
use_ansic=no
tauarch=unknown
host=
host_prefix=
host_sysroot=
pdtcompdir=
pdtarchdir=unknown
tauprefix=unknown
execprefix=unknown
taugcclibdir=unknown
tautoplevel=
tauoptions=
pdt_root_dir="."
pdt=no
pin=no
armci=no
vtf=no
otf=no
otfdir=
otfinc=
otflib=
download_otf=no
perfetto=no
download_perfetto=no
download_likwid=no
cubedir=own
ebs2otf=no
ebs_has_rt=no
ebs_clock_res="-1"
unwinder="none"
unwind_inc=
unwind_lib=
unwind_flag=
unwind_lib_flag=
unwind_extras=
unwind_gcc=no
static_libunwind=no
download_unwind=no
download_scorep=no
tau_links_rt=no
opari=no
mpc=no
opari1=no
oparicomp=no
epilog=no
hpctoolkit=no
hpctoolkit_src_dir=
hpctoolkit_install_dir=
scalasca=no
scoreplibs=
scorep=no
epilogextralinkcmd=
epiloglib=no
epilogbin=no
epiloginc=no
epilogdir=no
epiloglibdir=no
epilogbindir=bin
epilogincdir=
vampirtrace=no
vampirtraceshow=no
upnetwork=unknown
upc=no
upc_compiler=default
forceshared=no
opari_region=no
opari_construct=no
boost=no
sos=no
sosdir=
download_sos=no
install_sos=no
sos_comm=mpi
adios=no
adiosdir=
adios2=no
adios2dir=
sqlite3=no
mochi=no
mochi_sdskv_dir=
mochi_symbiomon_dir=
mochi_reducer_dir=
sqlite3dir=
#caliper=no
#caliperdir=
apex=no
zerosum=no
dyninst=no
dyninstinc=no
dyninstlib=no
dwarf=no
elfutils=no
elfutilsdir=
elfutilslib=
elfutilsinc=
java=no
asmdex=download
openmp=no
ompt=no
omptlib=
omptlibrary=-lomp
omptenabled=no
ompt_compiler_support=no
download_ompt=no
download_ompt_5_0=no
ompt_version_selected=no
ompt_compiler_support=no
install_ompt=no
xlsmp=no
ibmxlc=no
ibmxlc_r=no
fortran_compiler=no
ktau=no
ktau_ng=no
ktau_merge=no
ktau_shctr=no
ktauinc=
ktauincuser=
ktaulib=
ktausym=
catamount=no
gpshmem=no
stff=no
stffdir=
sddf=no
sddfdir=
tau_uselm=yes
tau_use_no_pie=yes
iowrapper=no
dmapp=no
ugni=no
tauoptions_has_ugni=no
chapel=no
ittnotify=no
gptl=no
gptl_e3sm=no
zepto=no
zeptodir=
cuda=no
level_zero=no
level_zero_legacy=no
level_zero_new=no
mdisc_dir=""
rocm=no
rocmv6=no
rocmcore_dir=""
rocprofiler=no
rocprofv2=no
rocprofsdk=no
rocprofsdk_dir=""
comgr_dir=""
roctracer=no
rocmsmi=no
hip=no
use_opencl=no
use_openacc=no
openacc_opt=""
cupti=no
build_llvm_plugin=no
strsignal_ok=no
android_version=4.4
android_platform=19
disable_memory_manager=no
llvm_src_dir=""
llvm_cxx=""
starpu=""
phiprof=""
syscall=no
compiler_for_dyninst=""
compilerxx_for_dyninst=""
restore_perfdmf_and_paraprof

handle_python_option() {
    local arg_value="$1" #full path to interpreter if provided
    local default_interpreter="$2" #python or python3

    if [[ -n "$arg_value" ]]; then
        pythonintv="$arg_value"
        if [[ ! -x "$pythonintv" ]]; then
            echo "Error: Specified Python interpreter '$pythonintv' is not executable or does not exist."
            exit 1
        fi
    else
        pythonintv=$(command -v "$default_interpreter")

        # If -python was given and 'python' doesn't exist, fallback to python3
        if [[ -z "$pythonintv" && "$default_interpreter" == "python" ]]; then
            pythonintv=$(command -v python3)
        fi

        if [[ -z "$pythonintv" ]]; then
            echo "Error: No suitable Python interpreter found for '$default_interpreter'."
            exit 1
        fi
    fi

    pythonbin=$(dirname "$pythonintv")
    python=yes
    pthread=yes  # Python can spawn threads

    if [[ "$default_interpreter" == "python3" ]]; then
        python3=yes
    fi

    python_ver_specified=$([[ -n "$arg_value" ]] && echo "yes" || echo "no")
}

###############################################################
# PARSE OPTIONS
###############################################################
all_args="$@"
for arg in "$@"; do
    case $arg in

        -cc=*)
            myarg=`echo $arg | sed 's/-cc=//'`
            if [ $myarg = cc -o $myarg = gcc -o $myarg = gcc4 -o $myarg = gcc-5 -o $myarg = gcc-6 -o $myarg = gcc-7 -o $myarg = gcc-8 -o $myarg = KCC -o $myarg = pgcc -o $myarg = guidec -o $myarg = xlc -o $myarg = ecc -o $myarg = icc -o $myarg = icx -o $myarg = powerpc64-linux-gcc -o $myarg = pathcc -o $myarg = fcc -o $myarg = orcc -o $myarg = opencc -o $myarg = qk-pgcc -o $myarg = scgcc -o $myarg = scpathcc -o $myarg = mips64el-gentoo-linux-gnu-gcc -o $myarg = powerpc64-bgq-linux-gcc -o $myarg = powerpc-bgp-linux-gcc -o $myarg = x86_64-w64-mingw32-gcc -o $myarg = mpiicc -o $myarg = mpifcc -o $myarg = mpifccpx -o $myarg = clang -o $myarg = parascc -o $myarg = hcc -o $myarg = bgclang -o $myarg = mpc_cc -o $myarg = mpc_icc -o $myarg = mpicc -o $myarg = mcc -o $myarg = oshcc -o $myarg = armclang  -o $myarg = ncc -o $myarg = hipcc -o $myarg = nvc -o $myarg = nvcc -o $myarg = amdclang ] || [[ $myarg == "gcc-"* ]]
            then
                c_compiler=$myarg
                c_compiler_specified=yes

                if [  $c_compiler = mpc_cc -o  $c_compiler = mpc_icc  ]
                then
                   mpc_c_compiler=`$myarg -show | awk '{ print $1; }'`
                fi
            else
                testxlc=`echo $myarg | sed -e 's/xlc//'`
                testmp=`echo $myarg | sed -e 's/^mp//'`
                if [ "y$testxlc" = "y$myarg"  -a "y$testmp" = "y$myarg" ]
                then
                    echo "WARNING: valid options for cc are 'cc', 'gcc', 'gcc4', 'gcc-*', 'KCC', 'guidec', '*xlc*', 'ecc', 'icc', 'icx', 'pathcc', 'fcc', 'orcc', 'opencc', 'qk-pgcc', 'powerpc64-bgq-linux-gcc', 'powerpc-bgp-linux-gcc', 'x86_64-w64-mingw32-gcc', 'mcc', 'mpiicc', 'pgcc' , 'mpc_cc', 'mpc_icc', 'mpifcc', 'mpifccpx', 'clang', 'parascc', 'hcc', 'bgclang', 'mpicc', 'mcc', 'oshcc', 'armclang', 'ncc', 'hipcc' 'nvc', 'nvcc', or 'amdclang'"
                    #       exit 1
                else
                    if [ "y$testxlc" != "y$myarg" ]
                    then
                        c_compiler=$myarg
                    else
                        c_compiler=`$myarg -show | awk '{ print $1; }'`
                    fi
                fi
            fi
            shift
            ;;

        -c++=*)
            myarg=`echo $arg | sed 's/-c++=//'`
            if [ $myarg = CC -o $myarg = KCC -o $myarg = g++ -o $myarg = g++4 -o $myarg = g++-5 -o $myarg = g++-6 -o $myarg = g++-7 -o $myarg = g++-8 -o $myarg = cxx  -o $myarg = NCC -o $myarg = pgc++ -o $myarg = pgCC  -o $myarg = pgcpp -o $myarg = egcs -o $myarg = FCC -o $myarg = guidec++ -o $myarg = aCC -o $myarg = c++ -o $myarg = ecpc -o $myarg = icpc -o $myarg = icpx -o $myarg = powerpc64-linux-g++ -o $myarg = pathCC -o $myarg = orCC -o $myarg = openCC -o $myarg = qk-pgCC -o $myarg = scg++ -o $myarg = scpathCC -o $myarg = mips64el-gentoo-linux-gnu-g++ -o $myarg = powerpc-bgp-linux-g++ -o $myarg = powerpc64-bgq-linux-g++ -o $myarg = mpicxx -o $myarg = mpic++ -o $myarg = x86_64-w64-mingw32-g++  -o $myarg = mpiicpc -o $myarg = mpiicpx -o $myarg = mpiFCC -o $myarg = mpiFCCpx -o $myarg = mpc_cxx -o $myarg = mpc_icpc -o $myarg = mpc_icpx -o $myarg = clang++ -o $myarg = hcc -o $myarg = hipcc -o $myarg = bgclang++ -o $myarg = parascc -o $myarg = mpicxx -o $myarg = mcxx -o $myarg = oshCC -o $myarg = oshc++ -o $myarg = oshcxx  -o $myarg = armclang++ -o $myarg = nc++  -o $myarg = hipcc -o $myarg = nvc++ -o $myarg = nvcc -o $myarg = amdclang++ ] || [[ $myarg == "g++-"* ]]
            then
                cxx_compiler=$myarg
                cxx_compiler_specified=yes
                if [  $cxx_compiler = mpc_cxx -o $cxx_compiler = mpc_icpc  -o $cxx_compiler = mpc_icpx  ]
                then
                  mpc_include=`mpc_cxx -show | grep -o '[^ ]*include/mpcframework[^ ]*'`
                   mpc_cxx_compiler=`$myarg -show | awk '{ print $1; }'`
                fi

            else
                testxlc=`echo $myarg | sed -e 's/xl//'`
                testmp=`echo $myarg | sed -e 's/^mp//'`
                if [ "y$testxlc" = "y$myarg" -a "y$testmp" = "y$myarg" ]
                then
                    echo "WARNING: valid options for c++ are 'CC', 'KCC', 'g++', 'g++4', 'g++-*', '*xlC*', 'cxx' , 'NCC', 'egcs', 'pgCC', 'pgcpp', 'FCC', 'guidec++', 'aCC', 'ecpc', 'icpc', 'icpx', 'pathCC', 'c++', 'qk-pgCC', 'x86_64-w64-mingw32-g++', 'mpiicpc', 'mpiicpx', 'mpiFCC', 'mpiFCCpx', 'orCC', 'openCC', 'mpc_cxx',  'mpc_icpc', 'mpc_icpx', 'clang++', 'parascc', 'hcc', 'hipcc', 'bgclang++', 'mpicxx', 'mpic++', 'mcxx', 'oshCC', oshcxx, 'oshc++', 'armclang++', 'nc++', 'hipcc', or 'amdclang++' "
                    #       exit 1
                else
                    if [ "y$testxlc" != "y$myarg" ]
                    then
                        cxx_compiler=$myarg
                    else
                        cxx_compiler=`$myarg -show | awk '{ print $1; }'`
                    fi
                fi
            fi
            shift
            ;;

        -pdt_c++=*)
            pdt_cxx_full_path=`echo $arg | sed 's@-pdt_c++=@@g'`
            myarg=`echo $arg |  echo $arg | sed 's@-pdt_c++=@@g' | sed 's@.*/@@g'`
            pdt_cxx_compiler=$myarg

            shift
            ;;

        -fortran=*)
            myarg=`echo $arg | sed 's/-fortran=//'`
            if [ $myarg = gnu -o $myarg = sgi -o $myarg = ibm -o $myarg = ibm64 -o $myarg = hp -o $myarg = cray -o $myarg = pgi -o $myarg = pgf90 -o $myarg = absoft -o $myarg = fujitsu -o $myarg = sun -o $myarg = compaq -o $myarg = kai -o $myarg = hitachi -o $myarg = intel  -o $myarg = ifort -o $myarg = ifx -o $myarg = nec -o $myarg = absoft -o $myarg = lahey -o $myarg = nagware -o $myarg = nag -o $myarg = pathscale -o $myarg = gfortran -o $myarg = gfortran-5 -o $myarg = gfortran-6 -o $myarg = gfortran-7 -o $myarg = gfortran-8 -o $myarg = gfortran4 -o $myarg = g95 -o $myarg = open64 -o $myarg = openf90 -o $myarg = mpiifort -o $myarg = mpiifx -o $myarg = mpifrtpx -o $myarg = mpifrt -o $myarg = frt -o $myarg = mpif90 -o $myarg = mpifort -o $myarg = mpc_f77 -o $myarg = mpc_ifort -o $myarg = mpc_gfortran -o $myarg = oshfort -o $myarg = caf -o $myarg = armflang -o $myarg = xlflang -o $myarg = ftn -o $myarg = flang -o $myarg = f18 -o $myarg = nfort -o $myarg = mpinfort -o $myarg = nvfortran -o $myarg = amdflang -o $myarg = flang-new ] || [[ $myarg == "gfortran-"* ]]  ; then
                fortran_compiler_specified=yes
                if [ $myarg = gnu ] ; then
                    fortran_compiler=gfortran
                else
                    fortran_compiler=$myarg
                fi
            else
                echo "WARNING: valid options for fortran are 'gnu', 'sgi', 'ibm', 'ibm64', 'hp', 'cray', 'pgi', 'pgf90', 'absoft', 'fujitsu', 'sun', 'compaq', 'kai', 'hitachi', 'intel', 'ifort', 'nec' , 'absoft', 'lahey', 'nag/nagware', 'pathscale', 'gfortran', 'gfortran-*', 'g95', 'open64', 'openf90', 'mpiifort', 'mpifrtpx', 'frtpx', 'frt', 'mpifrt', 'mpif90', 'mpifort', 'mpc_f77', or 'mpc_ifort', 'mpc_gfortran', 'oshfort', 'caf', 'ftn', 'armflang', 'flang', 'f18', 'nfort' , 'mpinfort', or 'amdflang'"
                exit 1
            fi
            shift
            ;;
        -upc=*)
            myarg=`echo $arg | sed 's/-upc=//'`
            if [ $myarg = cc -o $myarg = upc -o $myarg = gcc -o $myarg = upcc -o $myarg = xlupc ]
            then
                upc_compiler=$myarg
                upc=yes
            else
                echo "WARNING: valid options for upc are 'cc (Cray CCE UPC compiler)', 'upc (GNU UPC compiler)', 'upcc (Berkeley UPC compiler)', 'xlupc (IBM XL UPC compiler)', and 'gcc'"
                exit 1
            fi
            shift
            ;;

        -gnu)
            c_compiler=gcc
            cxx_compiler=g++
            shift
            ;;

        -dec)
            c_compiler=cc
            cxx_compiler=g++
            shift
            ;;

        -nolm)
            tau_uselm=no
            shift
            ;;

        -disable-no-pie-on-ubuntu)
            tau_use_no_pie=no
            shift
            ;;

        -ibm)
            c_compiler=xlc_r
            cxx_compiler=xlC_r
            shift
            ;;

        -cfront)
            c_compiler=cc
            cxx_compiler=CC
            shift
            ;;

        -sgi)
            c_compiler=cc
            cxx_compiler=CC
            shift
            ;;

        -tag=*)
            tag=yes
            tautag=`echo $arg | sed -e 's/-tag=//'`
            shift
            ;;

        -pthread=*)
            pthread=yes
            ptdir=`echo $arg | sed -e 's/-pthread=//'`
            shift
            ;;

        -no_pthread_create)
            suppress_pthread_create_wrapper=yes
            shift
            ;;

        -pthread)
            pthread=yes
            shift
            ;;

        -tbb)
            pthread=yes
            tbb=yes
            shift
            ;;

        -papithread)
            papithread=yes
            shift
            ;;

        -iowrapper)
            iowrapper=yes
            shift
            ;;

        -dmapp)
            dmapp=yes
            shift
            ;;

        -ugni)
            ugni=yes
            shift
            ;;

        -chapel)
            chapel=yes
            shift
            ;;

        -ittnotify)
            ittnotify=yes
            shift
            ;;

        -julia)
            julia=yes
            shift
            ;;

        -gptl)
            gptl=yes
            shift
            ;;

        -gptl=e3sm)
            gptl=yes
            gptl_e3sm=yes
            shift
            ;;

        -charm=*)
            pthread=yes
            charm=yes
            charmdir=`echo $arg | sed -e 's/-charm=//'`
            shift
            ;;

        -COMPENSATE)
            compensate=yes
            shift
            ;;


        -forceshared)
            forceshared=yes
            fixmakeargs="$fixmakeargs FORCESHARED"
            shift
            ;;

        -ktau)
            ktau=yes
            shift
            ;;

        #voorhees
        -ktau-ng)
            ktau_ng=yes
            shift
            ;;

        -ktau_merge)
            ktau_merge=no
            shift
            ;;

        -ktau_shctr)
            ktau_shctr=yes
            shift
            ;;

        -ktauinc=*)
            ktauinc=`echo $arg | sed -e 's/-ktauinc=//' -e 's/ /#/g'`
            if [ ! -d $ktauinc ]
            then
                echo "Error: Cannot access KTAU include directory $ktauinc"
                exit 1
            fi
            shift
            ;;

        -ktauincuser=*)
            ktauincuser=`echo $arg | sed -e 's/-ktauincuser=//' -e 's/ /#/g'`
            if [ ! -d $ktauincuser ]
            then
                echo "Error: Cannot access KTAU user include directory $ktauincuser"
                exit 1
            fi
            shift
            ;;

        -ktaulib=*)
            ktaulib=`echo $arg | sed -e 's/-ktaulib=//' -e 's/ /#/g'`
            if [ ! -d $ktaulib ]
            then
                echo "Error: Cannot access KTAU user lib $ktaulib"
                exit 1
            fi
            shift
            ;;

        -ktausym=*)
            ktausym=`echo $arg | sed -e 's/-ktausym=//' -e 's/ /#/g'`
            if [ ! -e $ktausym ]
            then
                echo "Warning: Cannot access KTAU Linux Kernel Symbol Table $ktausym locally (Does it exist on the compute nodes?). Continuing."
            fi
            shift
            ;;


        -mon-mpi)
            mon_mpi=yes
            shift
            ;;

        -mon-mrnet=*)
            mrnet=yes
            mrnet_root=`echo $arg | sed -e 's/-mon-mrnet=//' -e 's/ /#/g'`
            if [ ! -e $mrnet_root ]
            then
                echo "Error: Cannot access MRNet root directory $mrnet_root"
                exit 1
            fi
            shift
            ;;

        -mon-mrnet-lib=*)
            mrnet=yes
            mrnet_lib=`echo $arg | sed -e 's/-mon-mrnet-lib=//' -e 's/ /#/g'`
            if [ ! -e $mrnet_lib ]
            then
                echo "Error: Cannot access MRNet library directory $mrnet_lib"
                exit 1
            fi
            shift
            ;;

        -mon-mrnet-use-lightweight)
            mrnet_use_lightweight=yes
            shift
            ;;

        -nocomm)
            comm=no
            shift
            ;;

        -sproc)
            sproc=yes
            shift
            ;;

        -beacon=*)
            beacon=yes
            beacondir=`echo $arg | sed -e 's/-beacon=//'`
            if [ ! -d $beacondir ] ; then
                echo "Error: Cannot access BEACON directory $beacondir"
                exit 1
            fi
            shift
            ;;
        -papi=*)
            papi=yes
            papidir=`echo $arg | sed -e 's/-papi=//'`
            if [ ! -d $papidir ] ; then
                echo "Error: Cannot access PAPI directory $papidir"
                exit 1
            fi
            if [ "$papidir" = "/usr" ]; then
                  fixmakeargs="$fixmakeargs PAPINOINC"
            fi
            if [ -r /sys/devices/power/events/energy-pkg.scale ] ; then
                if [ -r /proc/sys/kernel/perf_event_paranoid ] ; then
                    paranoid=`cat /proc/sys/kernel/perf_event_paranoid`
                    if [ "${paranoid}" == "-1" ] ; then
                                fixmakeargs="$fixmakeargs PAPI_PERF_RAPL"
                    else
                        echo "NOTE: /proc/sys/kernel/perf_event_paranoid has a value greater than -1, RAPL power measurement disabled."
                    fi
                fi
            fi

            shift
            ;;

        -llvm_src=*)
            llvm_src_dir=`echo $arg | sed -e 's/-llvm_src=//'`
            fixmakeargs="$fixmakeargs llvm_src_dir=$llvm_src_dir LLVM_SRC_DIR"
            shift
            ;;

        -llvm_cxx=*)
            llvm_cxx=`echo $arg | sed -e 's/-llvm_cxx=//'`
            fixmakeargs="$fixmakeargs llvm_cxx=$llvm_cxx LLVM_CXX"
            shift
            ;;

        -llvm_cc=*)
            llvm_cc=`echo $arg | sed -e 's/-llvm_cc=//'`
            fixmakeargs="$fixmakeargs llvm_cc=$llvm_cc LLVM_CC"
            shift
            ;;


        -likwid=download*)
            echo "Downloading LIKWID..."
            likwid=yes
            download_likwid=yes
            shift
            ;;

         -likwid=*)
            likwid=yes
            likwiddir=`echo $arg | sed -e 's/-likwid=//'`
            if [ ! -d $likwiddir ] ; then
                echo "Error: Cannot access LIKWID directory $likwiddir"
                exit 1
            fi
            shift
            ;;

        -gpi=*)
            gpi=yes
            gpidir=`echo $arg | sed -e 's/-gpi=//'`
            if [ ! -d $gpidir ] ; then
                echo "Error: Cannot access GPI directory $papidir"
                exit 1
            fi
            fixmakeargs="$fixmakeargs GPI gpidir=$gpidir"
            tauoptions="${tauoptions}-gpi"
            shift
            ;;

        -darshan=*)
            darshan=yes
            darshandir=`echo $arg | sed -e 's/-darshan=//'`
            if [ ! -d $darshandir ] ; then
                echo "Error: Cannot access Darshan directory $papidir"
                exit 1
            fi
            shift
            ;;

        -perfsuite=*)
            perfsuite=yes
            perfsuitedir=`echo $arg | sed -e 's/-perfsuite=//'`
            fixmakeargs="$fixmakeargs TAU_PERFSUITE"
            fixmakeargs="$fixmakeargs perfsuitelink=-L$perfsuitedir/lib#-lpshwpc"
            fixmakeargs="$fixmakeargs perfsuiteinclude=-I$perfsuitedir/include"
            if [ ! -d $perfsuitedir ] ; then
                echo "Error: Cannot access PerfSuite directory $perfsuitedir"
                exit 1
            fi
            shift
            ;;

        -bfd=*)
            bfd=yes
            bfddir=`echo $arg | sed -e 's/-bfd=//'`
            shift
            ;;

        -elf=*)
            elf=yes
            elfdir=`echo $arg | sed -e 's/-elf=//'`
	    fixmakeargs="$fixmakeargs elfdir=$elfdir ELF"
            shift
            ;;

        -tbb=*)
            tbbdir=`echo $arg | sed -e 's/-tbb=//'`
	    fixmakeargs="$fixmakeargs useropt=-I${tbbdir}/include "
            shift
            ;;

        -unwinder=*)
            unwinder=`echo $arg | sed -e 's/-unwinder=//'`
            if [ "x$unwinder" = "xlibunwind" ] ; then
                unwind_flag="-DTAU_USE_LIBUNWIND"
                unwind_lib_flag="-lunwind"
            elif [ "x$unwinder" = "xstackwalker" ] ; then
                unwind_flag="-DTAU_USE_STACKWALKER"
                unwind_lib_flag="-lstackwalk"
            elif [ "x$unwinder" = "xbacktrace" ] ; then
                unwind_flag="-DTAU_USE_BACKTRACE"
                #unwind_lib_flag="-lgcc_s"
            elif [ "x$unwinder" = "xnone" ] ; then
                echo "No Unwinding Support."
            else
                echo "Error: Invalid Stack Unwinder $unwinder. Valid options are:"
                echo "libunwind, stackwalker, backtrace (libgcc_s), none (default)."
                exit 1
            fi
            shift
            ;;

        -unwind=download*)
            echo "Downloading libunwind..."
            # So that the user doesn't have to specify the unwinder,
            # use libunwind as the default.
            unwinder="libunwind"
            unwind_flag="-DTAU_USE_LIBUNWIND"
            unwind_lib_flag="-lunwind"
            download_unwind=yes
            shift
            ;;

	-unwind_gcc)
            echo "Using GCC to compile libunwind"
	    unwind_gcc=yes
            shift
	    ;;

	-static_libunwind)
            echo "Using libunwind.a instead of libunwind.so"
	    static_libunwind=yes
            shift
	    ;;

        -unwind=*)
            unwind_dir=`echo $arg | sed -e 's/-unwind=//'`
            if [ $unwind_dir != "/usr" ]; then
              unwind_inc="$unwind_dir/include"
              unwind_lib="$unwind_dir/lib"
              if [ -d "$unwind_dir/lib64" ]; then
                      unwind_lib="$unwind_dir/lib64"
              fi
            fi
            # So that the user doesn't have to specify the unwinder,
            # use libunwind as the default.
            if [ "x$unwinder" = "xnone" ] ; then
                unwinder="libunwind"
                unwind_flag="-DTAU_USE_LIBUNWIND"
                unwind_lib_flag="-lunwind"
            fi
            shift
            ;;

        -unwindinc=*)
            unwind_inc=`echo $arg | sed -e 's/-unwind_inc=//'`
            shift
            ;;

        -unwindlib=*)
            unwind_lib=`echo $arg | sed -e 's/-unwind_lib=//'`
            shift
            ;;

        -unwindextras=*)
            unwind_extras=`echo $arg | sed -e 's/-unwind_extras=//'`
            shift
            ;;

        -zlib=*)
            zlib=yes
            zlibdir=`echo $arg | sed -e 's/-zlib=//'`
            shift
            ;;

        -perfinc=*)
            perf=yes
            perfincdir=`echo $arg | sed -e 's/-perfinc=//'`
            if [ ! -d $perfincdir ]
            then
                echo "Error: Cannot access PERF include directory $perfincdir"
                exit 1
            fi
            shift
            ;;

        -perflib=*)
            perf=yes
            perflibdir=`echo $arg | sed -e 's/-perflib=//'`
            if [ ! -d $perflibdir ] ; then
                echo "Error: Cannot access PERF lib directory $perflibdir"
                exit 1
            fi
            shift
            ;;

        -perflibrary=*)
            perf=yes
            perflibrary=`echo $arg | sed -e 's/-perflibrary=//' -e 's/ /#/g'`
            shift
            ;;

        -oparicomp=*)
            opari=yes
            openmp=yes
            oparicomp=`echo $arg | sed -e 's/-oparicomp=//' -e 's/ /#/g'`
            shift
            ;;

        -opari)
            opari=yes
            openmp=yes
            shift
            ;;

        -opari=*)
            opari=yes
            oparidir=`echo $arg | sed -e 's@-opari=@@' -e 's@ @#@g'`
            openmp=yes
            shift
            ;;
        -opari1)
            opari1=yes
            openmp=yes
            shift
            ;;

        -opari1=*)
            opari1=yes
            openmp=yes
            shift
            ;;

        -opari_region)
            opari_region=yes
            shift
            ;;

        -opari_construct)
            opari_construct=yes
            shift
            ;;

        -vampirtrace=*)
            vampirtrace=yes
            trace=yes
            vampirtracedir=`echo $arg | sed -e 's/-vampirtrace=//'`
            if [ ! -d $vampirtracedir ] ; then
                echo "Error: Cannot access VAMPIRTRACE directory $vampirtracedir"
                exit 1
            fi
            shift
            ;;

        -upcnetwork=*)
            upc=yes
            upcnetwork=`echo $arg | sed -e 's/-upcnetwork=//'`
            shift
            ;;
        -gasnet=*)
            gasnet=yes
            gasnetdir=`echo $arg | sed -e 's/-gasnet=//'`
            fixmakeargs="$fixmakeargs gasnet=$gasnetdir GASNET"
            shift
            ;;

        -epilog=*)
            epilog=yes
            trace=yes
            epilogdir=`echo $arg | sed -e 's/-epilog=//'`
            if [ ! -d $epilogdir ] ; then
                echo "Error: Cannot access EPILOG directory $epilogdir"
                exit 1
            fi
            shift
            ;;

        -scalasca=*)
            epilog=yes
            trace=yes
            epilogdir=`echo $arg | sed -e 's/-scalasca=//'`
            if [ ! -d $epilogdir ] ; then
                echo "Error: Cannot access SCALASCA directory $epilogdir"
                exit 1
            fi
            if [ ! -r $epilogdir/bin/skin ]; then


                scorepdir=`which scorep | sed -e 's@bin/scorep@@g'`
                echo "scorepdir=$scorepdir"
                if [ -d $scorepdir ]; then
                    echo "Using the Score-P measurement substrate instead of Scalasca for vScalasca v2.0+"
                    echo "Using Score-P directory $scorepdir"
                    scalasca=no
                    epilog=no
                    epilogdir=no

                    trace=no

                    scorep=yes
                else
                    echo "Please configure TAU with the Score-P measurement substrate instead of Scalasca for vScalasca v2.0+. See http://www.score-p.org"
                fi
            fi
            shift
            ;;

        -hpctoolkit-src-dir=*)
            hpctoolkit=yes
            hpctoolkit_src_dir=`echo $arg | sed -e 's/-hpctoolkit-src-dir=//'`
            if [ ! -d $hpctoolkit_src_dir ] ; then
                echo "Error: Cannot access HPCTOOLKIT directory $hpctoolkit_src_dir"
                exit 1
            fi
            shift
            ;;

        -hpctoolkit-install-dir=*)
            hpctoolkit=yes
            hpctoolkit_install_dir=`echo $arg | sed -e 's/-hpctoolkit-install-dir=//'`
            if [ ! -d $hpctoolkit_install_dir ] ; then
                echo "Error: Cannot access HPCTOOLKIT directory $hpctoolkit_install_dir"
                exit 1
            fi
            shift
            ;;

    -scorep=download*)
        echo "Downloading Score-P ..."
        download_scorep=yes
        scorep=yes
        shift
        ;;

        -scorep=*)
            scorep=yes
            scorepdir=`echo $arg | sed -e 's/-scorep=//'`
            if [ ! -d $scorepdir ] ; then
                echo "Error: Cannot access SCOREP directory $scorepdir"
                exit 1
            fi
        if [ "x$otf" != xyes ] ; then
           otf=yes
           otfdir=${scorepdir}
        fi
            shift
            ;;

        -epiloglib=*)
            epilog=yes
            epiloglib=yes
            trace=yes
            epiloglibdir=`echo $arg | sed -e 's/-epiloglib=//'`
            shift
            ;;

        -epilogbin=*)
            epilog=yes
            epilogbin=yes
            trace=yes
            epilogbindir=`echo $arg | sed -e 's/-epilogbin=//'`
            shift
            ;;

        -epiloginc=*)
            epilog=yes
            epiloginc=yes
            trace=yes
            epilogincdir=`echo $arg | sed -e 's/-epiloginc=//'`
            shift
            ;;

        -pdt=*)
            pdt=yes
            pdtdir=`echo $arg | sed -e 's/-pdt=//'`
            if [ ! -d $pdtdir ] ; then
                echo "Error: Cannot access PDT directory $pdtdir"
                exit 1
            fi
            shift
            ;;

        -pin=*)
            pin=yes
            pindir=`echo $arg | sed -e 's/-pin=//'`
            if [ $pindir != "download" -a ! -d $pindir ] ; then
                echo "Error: Cannot access Intel PIN directory $pindir"
                exit 1
            fi
            shift
            ;;

        -armci=*)
            armci=yes
            armcidir=`echo $arg | sed -e 's/-armci=//'`
            if [ ! -d $armcidir ] ; then
                echo "Error: Cannot access ARMCI directory $armcidir"
                exit 1
            fi
            shift
            ;;

        -vtf=*)
            vtf=yes
            trace=yes
            vtfdir=`echo $arg | sed -e 's/-vtf=//'`
            if [ ! -d $vtfdir ]
            then
                echo "Error: Cannot access VTF3 directory $vtfdir"
                exit 1
            fi
            shift
            ;;

        -cube=*)
            cubedir=`echo $arg | sed -e 's/-cube=//'`
            shift
            ;;

     -otf=download*)
        echo "Downloading libotf2..."
        otf=yes
        download_otf=yes
        shift
        ;;

        -otf=*)
            otf=yes
            # don't set this! it will enable tracing by default. Just because
            # specified an OTF generation library doesn't mean TRACING should be
            # the default measurement!
            # trace=yes
            otfdir=`echo $arg | sed -e 's/-otf=//'`
            if [ ! -d $otfdir ]
            then
                echo "Error: Cannot access OTF directory $otfdir"
                exit 1
            fi
            shift
            ;;

        -otfinc=*)
            otfinc=`echo $arg | sed -e 's/-otfinc=//'`
            if [ ! -d $otfinc ]
            then
                echo "Error: Cannot access OTF include directory $otfinc"
                exit 1
            fi
            shift
            ;;

        -otflib=*)
            otflib=`echo $arg | sed -e 's/-otflib=//'`
            if [ ! -d $otflib ]
            then
                echo "Error: Cannot access OTF library directory $otflib"
                exit 1
            fi
            shift
            ;;

	-perfetto)
		perfetto=yes
		fixmakeargs="$fixmakeargs PERFETTO"
		shift
		;;

        -ebs2otf)
            ebs2otf=yes
            shift
            ;;

        -apex)
            apex=yes
            shift
            ;;

        -zerosum)
            zerosum=yes
            shift
            ;;

        -boost=download)
            boost=yes
        echo "Downloading Boost..."
            shift
            ;;

        -boost=*)
            boost=yes
            boostdir=`echo $arg | sed -e 's/-boost=//'`
            if [ ! -d $boostdir ]
            then
                echo "Error: Cannot access BOOST directory $boostdir"
                exit 1
            fi
            shift
            ;;

        -sos=*)
            sos=yes
        pthread=yes
            sosdir=`echo $arg | sed -e 's/-sos=//'`
            shift
            ;;

        -soscomm=*)
            myarg=`echo $arg | sed -e 's/-soscomm=//'`
            #if [ $myarg = sockets -o $myarg = mpi -o $myarg = evpath -o $myarg = zeromq ]
            if [ $myarg = sockets -o $myarg = mpi -o $myarg = evpath ]
            then
                sos_comm=$myarg
            else
                echo "ERROR: valid options for soscomm are 'sockets', 'mpi', 'evpath'"
                exit 1
            fi
            shift
            ;;


        -adios=*)
            adiosdir=`echo $arg | sed -e 's/-adios=//'`
            if [ ! -d $adiosdir ] ; then
                    echo "Error: Cannot access ADIOS directory $adiosdir"
                    exit 1
        else
            # Check ADIOS version
            if [ -f ${adiosdir}/bin/adios_config ] ; then
                    adios=yes
                    adios2=no
            elif [ -f ${adiosdir}/bin/adios2-config ] ; then
                    adios=no
                    adios2=yes
                    adios2dir=${adiosdir}
            fi
            fi
        pthread=yes
            shift
            ;;

    -sqlite3=*)
        sqlite3dir=`echo $arg | sed -e 's/-sqlite3=//'`
        if [ ! -d $sqlite3dir ] ; then
            echo "Error: Cannot access SQLite3 directory $sqlite3dir"
            exit 1
        else
            sqlite3=yes
        fi
        shift
        ;;

    -mochi)
        if [ `command -v pkg-config &> /dev/null; echo $?` == "1" ] ; then
            echo "Error: Cannot find any pkg-config installation! pkg-config is required to detect a mochi installation."
            exit 1
        fi

        if [ `pkg-config --exists soma-client soma-server soma-admin uuid; echo $?` == "1" ] ; then
            echo "Error: Cannot access soma-client, soma-server, or soma-admin install directory! Please verify that soma-client and dependent modules are loaded."
            echo "Run: 'pkg-config --exists soma-client soma-server soma-admin' for more info."
            exit 1
        else
            mochi=yes
        fi
        shift
        ;;

#        -caliper=*)
#            caliper=yes
#            caliperdir=`echo $arg | sed -e 's/-caliper=//'`
#            if [ ! -d $caliperdir ]
#            then
#                echo "Error: Cannot access CALIPER directory $caliperdir"
#                exit 1
#            fi
#            shift
#            ;;

        -dyninst=download)
            dyninst=yes
            boost=yes
            tbb=yes
            download_dyninst=yes
            echo "Downloading Dyninst..."
            shift
            ;;

        -dyninst=*)
            dyninst=yes
            dyninstdir=`echo $arg | sed -e 's/-dyninst=//'`
            if [ ! -d $dyninstdir ]
            then
                echo "Error: Cannot access DyninstAPI directory $dyninstdir"
                exit 1
            fi
            shift
            ;;

        -dyninstinc=*)
            dyninstinc=yes
            dyninstincdir=`echo $arg | sed -e 's/-dyninstinc=//'`
            if [ ! -d $dyninstincdir ]
            then
                echo "Error: Cannot access DyninstAPI include directory $dyninstincdir"
                exit 1
            fi
            shift
            ;;

        -dyninstlib=*)
            dyninstlib=yes
            dyninstlibdir=`echo $arg | sed -e 's/-dyninstlib=//'`
            if [ ! -d $dyninstlibdir ]
            then
                echo "Error: Cannot access DyninstAPI lib directory $dyninstlibdir"
                exit 1
            fi
            shift
        ;;

    -dwarf=download)
        dwarf=yes
        download_dwarf=yes
        echo "Downloading libdwarf..."
        shift
        ;;

        -dwarf=*)
            dwarf=yes
            dwarfdir=`echo $arg | sed -e 's/-dwarf=//'`
            if [ ! -d $dwarfdir ]
            then
                echo "Error: Cannot access libdwarf directory $dwarfdir"
                exit 1
            fi
            shift
            ;;

        -dwarflib=*)
            dwarfdir=`echo $arg | sed -e 's/-dwarflib=//'`
            if [ ! -d $dwarfdir ]
            then
                echo "Error: Cannot access libdwarf directory $dwarfdir"
                exit 1
            fi
            fixmakeargs="$fixmakeargs dwarfopts=-L$dwarfdir#-Wl\,-rpath\,$dwarfdir"
            dwarfdir=""
            shift
            ;;

        -elfutils=download)
            elfutils=yes
            download_elfutils=yes
            echo "Downloading elfutils"
            shift
            ;;

        -elfutils=*)
            elfutils=yes
            elfutilsdir=`echo $arg | sed -e 's/-elfutils=//'`
            if [ ! -d $elfutilsdir ]
            then
                echo "Error: Cannot access elfutils directory $elfutilsdir"
                exit 1
            fi
            shift
            ;;

        -openmp)
            openmp=yes
            shift
            ;;

        -openacc)
            openacc=yes
            use_openacc=yes
            shift
            ;;

        -openacclib=*)
            openacc=yes
            use_openacc=yes
            openacc_opt="-L"`echo $arg | sed -e 's/-openacclib=//'`
            shift
            ;;

        -omptlib=*)
            ompt=yes
            openmp=yes
            omptlib=`echo $arg | sed -e 's/-omptlib=//'`
            if [ ! -d $omptlib ]
            then
                echo "Error: Cannot access OpenMP runtime library directory $omptlib"
                exit 1
            fi
            shift
            ;;

        -omptlibrary=*)
            openmp=yes
            omptlibrary=`echo $arg | sed -e 's/-omptlibrary=//'`
            shift
            ;;

        -ompt=download)
        # Don't download all the time - let the later check see
        # if the compiler has OMPT support built-in
            openmp=yes
            ompt=yes
        #echo "ompt=download"
            #download_ompt=yes
            shift
            ;;

        -ompt=*)
            openmp=yes
            ompt=yes
            ompt_dir=`echo $arg | sed -e 's/-ompt=//'`
        if [ ! -d $ompt_dir ]
        then
          echo "Error: OMPT directory $ompt_dir cannot be accessed"
          exit 1
        fi
            omptlib="${ompt_dir}/lib"
            shift
            ;;

        -ompt*)
            openmp=yes
            ompt=yes
            shift
            ;;


        -xlsmp=*)
            xlsmp=yes
            xlsmpdir=`echo $arg | sed -e 's/-xlsmp=//'`
            if [ ! -d $xlsmpdir ]
            then
                echo "Error: Cannot access xlsmp directory $xlsmpdir"
                exit 1
            fi
            shift
            ;;

        -jdk=*)
            java=yes
            jdkdir=`echo $arg | sed -e 's/-jdk=//'`
            #echo "jdkdir is $jdkdir"
            if [ ! -d $jdkdir ]
            then
                echo "Error: Cannot access JDK directory $jdkdir"
                exit 1
            fi

            shift
            ;;

        -android_sdk=*)
            android_sdk=`echo $arg | sed -e 's/-android_sdk=//'`
            if [ ! -d $android_sdk ]
            then
                echo "Error: Cannot access Android SDK directory $android_sdk"
                exit 1
            fi

            shift
            ;;

        -android_version=*)
            android_version=`echo $arg | sed -e 's/-android_version=//'`
            shift
            ;;

        -android_platform=*)
            android_platform=`echo $arg | sed -e 's/-android_platform=//'`
            shift
            ;;

        -asmdex=*)
            asmdex=`echo $arg | sed -e 's/-asmdex=//'`
            shift
            ;;

        -slog2=*)
            trace=yes
            shift
            ;;


        -slog2)
            trace=yes
            shift
            ;;

        -zeptodir=*)
            zeptodir=`echo $arg | sed -e 's/-zeptodir=//' -e 's/ /#/g'`
            if [ ! -e $zeptodir ]
            then
                echo "ERROR: Cannot access the Zepto directory provided [$zeptodir]. Aborting."
                exit 1
            fi
            zepto=yes
            disableshared=yes
            c_compiler=/bgsys/drivers/ppcfloor/gnu-linux/bin/powerpc-bgp-linux-gcc
            cxx_compiler=/bgsys/drivers/ppcfloor/gnu-linux/bin/powerpc-bgp-linux-g++
            fortran_compiler=yes
            fixmakeargs="$fixmakeargs ZEPTO_GFORTRAN"
            shift
            ;;

        -useropt=*)
            # Note you might be passed -g#-O3 which becomes -g -O3
            orig_useropt=`echo $arg | sed -e 's/-useropt=//' -e 's/#/ /g'`
            # Note you might be passed -g -O3 which becomes -g#-O3 here
            useropt=`echo $arg | sed -e 's/-useropt=//' -e 's/ /#/g'`
            echo "NOTE: Compiler optimization must be specified manually with useropt"
            shift
            ;;

        -extrashlibopts=*)
            # Note you might be passed -g#-O3 which becomes -g -O3
            extrashlibopts=`echo $arg | sed -e 's/-extrashlibopts=//' -e 's/ /#/g'`
            # Note you might be passed -g -O3 which becomes -g#-O3 here
            shift
            ;;

        -mpit)
            mpit=yes
            shift
            ;;

        -mpi)
            mpi=yes
            shift
            ;;

        -mpiinc=*)
            mpi=yes
            mpiinc=`echo $arg | sed -e 's/-mpiinc=//' -e 's/ /#/g'`
            if [ ! -d $mpiinc ] ; then
                echo "Error: Cannot access MPI include directory $mpiinc"
                exit 1
            fi
            shift
            ;;

        -mpilib=*)
            mpi=yes
            mpilib=`echo $arg | sed -e 's/-mpilib=//' -e 's/ /#/g'`
            if [ ! -d $mpilib ] ; then
                echo "Error: Cannot access MPI lib directory $mpilib, trying ${mpilib}64"
                if [ ! -d ${mpilib}64 ]; then
                  echo "Error: Cannot access MPI lib directory ${mpilib}64"
                  exit 1
                else
                  mpilib="${mpilib}64"
                  echo "Using MPI lib as $mpilib"
                fi
            fi
            shift
            ;;

        -mpilibrary=*)
            # you may be passed -lmpich#-L/usr/opt/gm#-lgm#-lpthread#-ldl by installtau
            mpilibrary=`echo $arg | sed -e 's/-mpilibrary=//' -e 's/#/ /g'`
            shift
            ;;

        -shmem)
            shmem=yes
            shift
            ;;

        -shmem=*)
            shmem=yes
            tmp=`echo $arg | sed -e 's/-shmem=//' -e 's/#/ /g'`
            if [ "$tmp" == "pshmem" ] ; then
                 pshmem=yes
            fi
            shift
            ;;

        -ENABLE_TAUENV)
            tauenv=yes
            fixmakeargs="$fixmakeargs TAUENV"
            shift
            ;;

        -shmeminc=*)
            shmeminc=`echo $arg | sed -e 's/-shmeminc=//' -e 's/ /#/g'`
            if [ ! -d $shmeminc ]
            then
                echo "Error: Cannot access SHMEM include directory $shmeminc"
                exit 1
            fi
            shmem=yes
            shift
            ;;

        -shmemlib=*)
            shmemlib=`echo $arg | sed -e 's/-shmemlib=//' -e 's/ /#/g'`
            if [ ! -d $shmemlib ]
            then
                echo "Error: Cannot access SHMEM lib directory $shmemlib"
                exit 1
            fi
            shmem=yes
            shift
            ;;

        -shmemlibrary=*)
            shmemlibrary=`echo $arg | sed -e 's/-shmemlibrary=//' -e 's/#/ /g'`
            shmem=yes
            shift
            ;;

    -shmemmpiinc=*)
            shmemmpiinc=`echo $arg | sed -e 's/-shmemmpiinc=//' -e 's/#/ /g'`
            if [ ! -d $shmemmpiinc ]
            then
                echo "Error: Cannot access SHMEM include directory $shmemmpiinc"
                exit 1
            fi
            shmem=yes
            shift
            ;;

        -stff=*)
            stffdir=`echo $arg | sed -e 's/-stff=//' -e 's/ /#/g'`
            if [ ! -d $stffdir ] ; then
                echo "Error: couldn't find RENCI STFF library in $stffdir"
                exit 1
            fi
            stff=yes
            shift
            ;;

        -sddf=*)
            sddfdir=`echo $arg | sed -e 's/-sddf=//' -e 's/ /#/g'`
            if [ ! -d $sddfdir ] ; then
                echo "Error: couldn't find sddf library in $sddfdir"
                exit 1
            fi
            sddf=yes
            shift
            ;;

		-python3=*)
			handle_python_option "${arg#*=}" "python3"
			shift
			;;
		-python3)
			handle_python_option "" "python3"
			shift
			;;
		-python=*)
			handle_python_option "${arg#*=}" "python"
			shift
			;;
		-python)
			handle_python_option "" "python"
			shift
			;;

        -pythoninc=*)
            pythoninc=`echo $arg | sed -e 's/-pythoninc=//' -e 's/ /#/g'`
            if [ ! -d $pythoninc ]
            then
                echo "Error: Cannot access Python include directory $pythoninc"
                exit 1
            fi
            python=yes
        # Python can/will spawn threads
            pthread=yes
            shift
            ;;

        -pythonlib=*)
            pythonlib=`echo $arg | sed -e 's/-pythonlib=//' -e 's/ /#/g'`
            if [ ! -d $pythonlib ]
            then
                echo "Error: Cannot access Python lib directory $pythonlib"
                exit 1
            fi
            python=yes
        # Python can/will spawn threads
            pthread=yes
            shift
            ;;

        -pythonlibrary=*)
            pythonlibrary=`echo $arg | sed -e 's/-pythonlibrary=//' -e 's/#/ /g'`
            shift
            ;;


        -opt=*)
            useropt=`echo $arg | sed -e 's/-opt=//' -e 's/ /_/g'`
            shift
            ;;

        -ansic)
            use_ansic=yes
            shift
            ;;

        -PROFILE)
            profile=yes
            shift
            ;;

        -MULTIPLECOUNTERS)
            echo "-MULTIPLECOUNTERS is no longer necessary, all builds support multiple counters"
            shift
            ;;

        -PROFILECALLS)
            echo "-PROFILECALLS is no longer supported, try downgrading to TAU 2.18.1"
            exit 0
            ;;

        -PROFILEPHASE)
            phase=yes
            shift
            ;;

        -PROFILECALLPATH)
            profile=yes
            callpath=yes
            shift
            ;;

        -PROFILEPARAM)
            profile=yes
            profileparam=yes
            shift
            ;;

        -PROFILEPATHS)
            profile=yes
            profileparam=yes
            profilepaths=yes
            mpit=yes
            shift
            ;;

        -PROFILECOMMUNICATORS)
            profile=yes
            profilecommunicators=yes
            shift
            ;;

        -DEPTHLIMIT)
            depthlimit=yes
            shift
            ;;

        -PROFILESTATS)
            echo "-PROFILESTATS is no longer supported, try downgrading to TAU 2.18.1"
            exit 0
            ;;

        -PROFILEMEMORY)
            profile=yes
            profilememory=yes
            shift
            ;;

        -PROFILEHEADROOM)
            profile=yes
            profileheadroom=yes
            shift
            ;;

        -PROFILECALLSTACK)
            echo "-PROFILECALLSTACK is no longer supported, try downgrading to TAU 2.18.1"
            exit 0
            ;;

        -TRACE)
            trace=yes
            shift
            ;;

        -MPITRACE)
            mpitrace=yes
            shift
            ;;

        -DEBUGPROF)
            debugprof=yes
            shift
            ;;

        -BGLTIMERS)
            bgltimers=yes
            shift
            ;;

        -BGPTIMERS)
            bgptimers=yes
            shift
            ;;

        -BGQTIMERS)
            bgqtimers=yes
            shift
            ;;

        -SGITIMERS)
            sgitimers=yes
            shift
            ;;

        -CRAYTIMERS)
            craytimers=yes
            shift
            ;;

        -LINUXTIMERS)
            linuxtimers=yes
            shift
            ;;

        -ALPHATIMERS)
            alphatimers=yes
            shift
            ;;

        -CPUTIME)
            cputime=yes
            shift
            ;;

        -PAPIWALLCLOCK)
            papiwallclock=yes
            shift
            ;;

        -PAPIVIRTUAL)
            papivirtual=yes
            shift
            ;;

        -INTELCXXLIBICC)
            intelcxxlibicc=yes
            shift
            ;;

        -noex)
            # NO exceptions to be used while building the lib.
            noex=yes
            shift
            ;;

        -noplugins)
            # Do not build plugins
            plugins=no
            shift
            ;;

        -DISABLESHARED)
            # Disable shared library creation
            disableshared=yes
            shift
            ;;

        -DISABLE_MEMORY_MANAGER)
            # Disable heap management for sampling, signal handling
            disable_memory_manager=yes
            shift
            ;;

        -PMI)
            # Enable tracking Cray topology information using PMI
            pmi=yes
            shift
            ;;

        -ENABLE_EXPORT_DYNAMIC)
            # Turn on -Wl,--export-dynamic flag during linking
            enable_export_dynamic=yes
            shift
            ;;

        -setnode0)
            # When this option is invoked, TAU sets the default node no to 0 instead
            # of using -1. So, profile.0.0.0 is created even if TAU_PROFILE_SET_NODE(0)
            # is not called in the application. Do not use with tracing
            setnode0=yes
            shift
            ;;

        -arch=*)
            tauarch=`echo $arg | sed -e 's/-arch=//' -e 's/ /_/g'`
            shift
            if [ $tauarch = i386_linux ]; then
                orig_useropt="$orig_useropt -m32"
            fi
            ;;

        -host=*)
            host=`echo $arg | sed -e 's/-host=//' -e 's/ /_/g'`
            host_prefix=${host}-
            fixmakeargs="$fixmakeargs host=$host"
            shift
            ;;

        -hostutils)
            fixmakeargs="$fixmakeargs HOSTUTILS"
            shift
            ;;

        -host_sysroot=*)
            host_sysroot=`echo $arg | sed -e 's/-host_sysroot=//'`
            fixmakeargs="$fixmakeargs host_sysroot=$host_sysroot"
            shift
            ;;

        -pdtcompdir=*)
            pdtcompdir=`echo $arg | sed -e 's/-pdtcompdir=//' -e 's/ /_/g'`
            shift
            ;;

        -pdtarchdir=*)
            pdtarchdir=`echo $arg | sed -e 's/-pdtarchdir=//' -e 's/ /_/g'`
            shift
            ;;

        -prefix=*)
            tauprefix=`echo $arg | sed -e 's/-prefix=//' -e 's/ /_/g'`
            fixmakeargs="$fixmakeargs tauprefix=$tauprefix"
        echo "Testing directory $tauprefix "
        mkdir -p ${tauprefix}
        rc=$?
        if [ $rc -ne 0 ] ; then
            echo "Failed to create installation directory: ${tauprefix}"
            exit $rc
        fi
            shift
            ;;

        -exec-prefix=*)
            execprefix=`echo $arg | sed -e 's/-exec-prefix=//' -e 's/ /_/g'`
            fixmakeargs="$fixmakeargs execprefix=$execprefix"
            shift
            ;;

        -cuda=*)
            cudainclude=`echo $arg | sed -e 's/-cuda=//' -e 's/ /#/g'`/include
            if [ ! -d $cudainclude ] ; then
                echo "Error: Cannot access GPU include directory $cudainclude"
                exit 1
            fi
            #                useropt="$useropt#-DTAU_MAX_THREADS=32"
            cuda=yes
	        pthread=yes
            shift
            ;;
        -cudalibrary=*)
            cudalibrary=`echo $arg | sed -e 's/-cudalibrary=//' -e 's/ /#/g'`
            fixmakeargs="$fixmakeargs cudalibrary=$cudalibrary"
            shift
            ;;

        -level_zero)
            level_zero=yes
            tauoptions="${tauoptions}-level_zero"
            pthread=yes
            ld_lib_path=`echo $LD_LIBRARY_PATH | sed -e "s@:@ @g" `
            for d in $ld_lib_path
            do
              if [ -r $d/libze_loader.so ]; then
                l0_lib_dir=$d
                l0dir=$d/..
                fixmakeargs="$fixmakeargs l0dir=$l0dir l0_lib_dir=$l0_lib_dir"
                break;
              fi
            done

            if [ "x$l0dir" = "x" ]; then
              if [ -r /usr/lib64/libze_loader.so ]; then
                l0dir="/usr"
                l0_lib_dir="/usr/lib64"
                fixmakeargs="$fixmakeargs l0dir=$l0dir l0_lib_dir=$l0_lib_dir"
              elif [ -r /usr/lib/x86_64-linux-gnu/libze_loader.so ]; then
                l0dir="/usr"
                l0_lib_dir="/usr/lib/x86_64-linux-gnu"
                fixmakeargs="$fixmakeargs l0dir=$l0dir l0_lib_dir=$l0_lib_dir"
              elif [ -r /usr/local/lib/libze_loader.so ]; then
                l0dir="/usr/local"
                l0_lib_dir="/usr/local/lib"
                fixmakeargs="$fixmakeargs l0dir=$l0dir l0_lib_dir=$l0_lib_dir"
              else
                echo "TAU: libze_loader.so not found. Please specify -level_zero=<dir> and reconfigure TAU."
                exit 1
              fi
            fi
            shift
            ;;

        -level_zero=*)
            l0dir=`echo $arg | sed -e 's/-level_zero=//' -e 's/ /#/g'`
            if [ ! -d $l0dir ]; then
              echo  "Error: Cannot access Level Zero directory $l0dir"
              exit 1
            else
              fixmakeargs="$fixmakeargs l0dir=$l0dir"
              level_zero=yes
            fi
            if [ -r $l0dir/lib64/libze_loader.so ]; then
             l0_lib_dir="$l0dir/lib64"
            elif [ -r $l0dir/lib/libze_loader.so ]; then
             l0_lib_dir="$l0dir/lib"
            elif [ -r $l0dir/lib/x86_64-linux-gnu/libze_loader.so ]; then
             l0_lib_dir="$l0dir/lib/x86_64-linux-gnu"
            else
              echo  "Error: Cannot access Level Zero libze_loader.so in $l0dir/lib or $l0dir/lib64 or $l0dir/x86_64-linux-gnu"
              exit 1
            fi
            fixmakeargs="$fixmakeargs l0_lib_dir=$l0_lib_dir"
            tauoptions="${tauoptions}-level_zero"
            pthread=yes
            shift
            ;;

      	-level_metrics=*)
           mdisc_dir=`echo $arg | sed -e 's/-level_metrics=//' -e 's/ /#/g'`
           if [  -r ${mdisc_dir}/include/metrics_discovery_api.h ]; then
              fixmakeargs="$fixmakeargs mdisc_dir=$mdisc_dir LEVEL_ZERO_METRICS"
              tauoptions="${tauoptions}-L0Metrics"
           else
             echo "TAU: metrics_discovery_api.h not found. Install metrics discovery or disable flag."
             exit 1
           fi

      	shift
      	;;

        -force_new_l0)
            level_zero_new=yes
        shift
        ;;

        -force_legacy_l0)
            level_zero_legacy=yes
        shift
        ;;


        -rocm)
            rocmdir=/opt/rocm
            fixmakeargs="$fixmakeargs rocmdir=$rocmdir ROCM"
            rocm=yes
            tauoptions="${tauoptions}-rocm"
            pthread=yes
            shift
            ;;

        -rocm=*)
            rocmdir=`echo $arg | sed -e 's/-rocm=//' -e 's/ /#/g'`
            if [ ! -d $rocmdir ]; then
              echo  "Error: Cannot access ROCm directory $rocmdir"
              exit 1
            else
              fixmakeargs="$fixmakeargs rocmdir=$rocmdir ROCM"
              rocm=yes
            fi
            tauoptions="${tauoptions}-rocm"
            pthread=yes
            shift
            ;;

    	-rocm-core=*)
    	    rocmcore_dir=`echo $arg | sed -e 's/-rocm-core=//' -e 's/ /#/g'`
    	    if [ ! -d $rocmcore_dir ]; then
                  echo  "Error: Cannot access ROCm Core directory $rocmcore_dir"
                  exit 1
    	    fi
    	    shift
    	    ;;

    	-rocprofv2)
            fixmakeargs="$fixmakeargs ROCPROFILERV2"
            rocprofv2=yes
            tauoptions="${tauoptions}-rocprofv2"
            pthread=yes
            shift
            ;;

       -rocprofsdk)
            fixmakeargs="$fixmakeargs ROCPROFILERSDK"
            rocprofsdk=yes
            tauoptions="${tauoptions}-rocprofsdk"
            pthread=yes
            shift
            ;;

        -rocprofsdk=*)
            fixmakeargs="$fixmakeargs ROCPROFILERSDK"
            rocprofsdk=yes
            tauoptions="${tauoptions}-rocprofsdk"
            pthread=yes
            rocprofsdk_dir=`echo $arg | sed -e 's/-rocprofsdk=//' -e 's/ /#/g'`
            if [ ! -d $rocprofsdk_dir ]; then
                echo "Error: Cannot access ROCprofiler-SDK directory $rocprofsdk_dir"
                exit 1
            else
                if [ -r $rocprofsdk_dir/include/rocprofiler-sdk/version.h ]; then
                    fixmakeargs="$fixmakeargs rocprofsdk_dir=$rocprofsdk_dir"
                else
                    echo "Error: Cannot find version.h in directory $rocprofsdk_dir/include/rocprofiler-sdk/"
                    exit 1
                fi
            fi
            shift
            ;;

        -comgr=*)
            comgr_dir=`echo $arg | sed -e 's/-comgr=//' -e 's/ /#/g'`
            if [ -r $comgr_dir/include/amd_comgr/amd_comgr.h ]; then
                    fixmakeargs="$fixmakeargs comgr_dir=$comgr_dir"
            else
                echo "Error: Cannot find amd_comgr.h in directory $comgr_dir/include/amd_comgr/"
                exit 1
            fi
            shift
            ;;

	   -rocmsmi=*)
            rocmsmidir=`echo $arg | sed -e 's/-rocmsmi=//' -e 's/ /#/g'`
            if [ ! -d $rocmsmidir ]; then
              echo  "Error: Cannot access ROCm smi directory $rocmsmidir"
              exit 1
            else
              if [ -r $rocmsmidir/include/rocm_smi/rocm_smi.h ]; then
                fixmakeargs="$fixmakeargs rocmsmidir=$rocmsmidir"
                rocmsmi=yes
                fixmakeargs="$fixmakeargs ROCMSMI"
              fi
            fi
            shift
            ;;

        -hip=*)
            hipdir=`echo $arg | sed -e 's/-hip=//' -e 's/ /#/g'`
            if [ ! -d $hipdir ]; then
              echo  "Error: Cannot access hip directory $hipdir"
              exit 1
            else
              fixmakeargs="$fixmakeargs hipdir=$hipdir HIP"
              hip=yes
              tauoptions="${tauoptions}-hip"
            fi
            shift
            ;;
        -roctracer=*)
            roctracerdir=`echo $arg | sed -e 's/-roctracer=//' -e 's/ /#/g'`
            if [ ! -d $roctracerdir ]; then
              echo  "Error: Cannot access roctracer directory $roctracerdir"
              exit 1
            else
              fixmakeargs="$fixmakeargs roctracerdir=$roctracerdir ROCTRACER TAU_USE_GPU"
              roctracer=yes
              tauoptions="${tauoptions}-roctracer"
              # Check the roctracerlib directory. Is it in build or lib? Source/installed package?
              tryroctracerlib=$roctracerdir/lib
              if [ -r $tryroctracerlib/libroctracer64.so ]; then
                fixmakeargs="$fixmakeargs roctracerlibdir=$tryroctracerlib"
              else
                tryroctracerlib=$roctracerdir/build
                if [ -r $tryroctracerlib/libroctracer64.so ]; then
                  fixmakeargs="$fixmakeargs roctracerlibdir=$tryroctracerlib"
                fi
              fi
              # Check the hsa header in roctracer. Sometimes it is not installed correctly.
              if [ -r $roctracerdir/inc/roctracer_hsa.h -o -r $roctracerdir/include/roctracer/roctracer_hsa.h ]; then
                  fixmakeargs="$fixmakeargs ROCTRACER_HSA"
              fi
            fi
            shift
            ;;
        -rocprofiler=*)
            rocprofilerdir=`echo $arg | sed -e 's/-rocprofiler=//' -e 's/ /#/g'`
            if [ ! -d $rocprofilerdir ]; then
              echo  "Error: Cannot access rocprofiler directory $rocprofilerdir"
              exit 1
            else
              fixmakeargs="$fixmakeargs rocprofilerdir=$rocprofilerdir TAU_USE_GPU"
              rocprofiler=yes
              tauoptions="${tauoptions}-rocprofiler"
              pthread=yes
            fi
            shift
            ;;

        -rocprofilerlibrary=*)
            rocprofilerlibrary=`echo $arg | sed -e 's/-rocprofilerlibrary=//' -e 's/ /#/g'`
            fixmakeargs="$fixmakeargs rocprofilerlibrary=$rocprofilerlibrary"
            shift
            ;;

        -rocprofilerinc=*)
            rocprofilerinc=`echo $arg | sed -e 's/-rocprofilerinc=//' -e 's/ /#/g'`
            if [ ! -d $rocprofilerinc ]; then
              echo  "Error: Cannot access rocprofiler directory $rocprofilerinc"
              exit 1
            else
              fixmakeargs="$fixmakeargs rocprofilerinc=$rocprofilerinc"
            fi
            shift
            ;;

        -DISABLE_OPENCL)
            opencl=disable
            use_opencl=no
            shift
            ;;

        -opencl)
            use_opencl=yes
            shift
            ;;
        -opencl=*)
            opencldir=`echo $arg | sed -e 's/-opencl=//' -e 's/ /#/g'`
            openclinclude=$opencldir/include
            if [ ! -d $openclinclude ] ; then
                echo "Error: Cannot access GPU include directory $openclinclude"
                if [ `uname -s ` = "Darwin" ];  then
                  echo "Adding -useropt=-framework openCL"
                  useropt="-framework#openCL"
                  fixmakeargs="$fixmakeargs OPENCL TAU_USE_GPU "
                  use_opencl=yes
                else
                  exit 1
                fi
            fi
            if [ -f "$openclinclude/CL/cl.h" ] ; then
                #                        useropt="$useropt#-DTAU_MAX_THREADS=32"
                fixmakeargs="$fixmakeargs OPENCL TAU_USE_GPU openclinclude=$openclinclude"
                use_opencl=yes
            else
                if [ -f "$openclinclude/sycl/CL/cl.h" ]; then
                    fixmakeargs="$fixmakeargs OPENCL TAU_USE_GPU openclinclude=$openclinclude/sycl"
                    use_opencl=yes
                else
                    echo "Error: Cannot find GPU headers, TAU currently supports either CUDA or OpenCL"
                    exit 0
                fi
            fi
            opencllib="libOpenCL.so"
            for d in "OpenCL" "amdocl64" ; do
                libname="lib${d}.so"
                echo "Looking for ${libname} in ${opencldir}"
                exists=`find ${opencldir} -name ${libname} | head -n 1`
                if [ "x$d" == "xamdocl64" -a -r ${opencldir}/lib/libamdocl64.so ]; then 
                    exists=${opencldir}/lib/libamdocl64.so 
                fi
                if [ -r "${exists}" ]; then
                    echo "found $exists"
                    opencllib=${exists}
                    fixmakeargs="$fixmakeargs OPENCL opencllib=${opencllib}"
                    break
                fi
            done
            shift
            ;;

        -cupti=*)
            tauoptions="${tauoptions}-cupti"
            cupti_path=`echo $arg | sed -e 's/-cupti=//' -e 's/ /#/g'`
            if [ ! -d ${cupti_path} ] ; then
                echo "Error: Cannot access CUPTI include directory ${cupti_path}"
                exit 1
            fi
            if [ -f "${cupti_path}/include/cupti_events.h" ] ; then
                fixmakeargs="$fixmakeargs CUPTI cuptiinclude=${cupti_path}/include"
                cupti=yes
                if [ -f "${cupti_path}/lib/libpcsamplingutil.so" ]; then
                    fixmakeargs="$fixmakeargs CUPTIPC"
                fi
                if [ -f "${cupti_path}/lib64/libpcsamplingutil.so" ]; then
                    fixmakeargs="$fixmakeargs CUPTIPC"
                fi
            else
                echo "Error: Cannot find CUPTI headers."
                exit 1
            fi
            shift
            ;;

        -starpu)
            starpu=yes
            fixmakeargs="$fixmakeargs STARPU"
            tauoptions="${tauoptions}-starpu"
            pthread=yes
            shift
            ;;

        -starpu=*)
            starpudir=`echo $arg | sed -e 's/-starpu=//' -e 's/ /#/g'`
            if [ ! -d $starpudir ]; then
              echo  "Error: Cannot access StarPU directory $starpudir"
              exit 1
            else
	        if [ -d ${starpudir}/include/starpu/1.4 ]; then
		    echo "Found StarpU 1.4 and using it"
		    starpudir=${starpudir}/include/starpu/1.4
	        else
		    if [ -d ${starpudir}/include/starpu/1.3 ]; then
			echo "Found StarpU 1.3 and using it"
			starpudir=${starpudir}/include/starpu/1.3
		    else
		        echo  "Error: Cannot find StarPU 1.3 or 1.4 in $starpudir"
			exit 1
		    fi
		fi
              fixmakeargs="$fixmakeargs starpudir=$starpudir STARPU"
              starpu=yes
            fi
            tauoptions="${tauoptions}-starpu"
            pthread=yes
            shift
            ;;

        -phiprof)
            phiprof=yes
            fixmakeargs="$fixmakeargs PHIPROF"
#            fixmakeargs="$fixmakeargs GNU"
            tauoptions="${tauoptions}-phiprof"
            shift
            ;;

        -syscall)
            syscall=yes
            shift
            ;;

        default)
            echo "Fixing Makefiles"
            ;;
        '')
            #echo "NULL switch!"
            # Required for HP/Compaq Tru64 machines.
            ;;
        *)
            echo "ERROR: Command line switch \`$arg' not recognized" 1>&2
            echo "Run configure with -help to see a list of options" 1>&2
            exit 1
            ;;
        default)
            echo "Fixing Makefiles"
            ;;
        '')
            #echo "NULL switch!"
            # Required for HP/Compaq Tru64 machines.
            ;;
        *)
            echo "ERROR: Command line switch \`$arg' not recognized" 1>&2
            echo "Run configure with -help to see a list of options" 1>&2
            exit 1
            ;;
    esac
done

# make sure we have the perfstubs headers
if [ ! -d perfstubs ] || [ ! -f perfstubs/CMakeLists.txt ] ; then
    git submodule update --init --recursive perfstubs
fi

if [ "x$python_ver_specified" = "xno" -a "x$pythoninc" != "x" ]; then
  if [ -f $pythoninc/../bin/python ]; then
    pythonintv=$pythoninc/../bin/python
    pythonbin=$pythonbin/../bin
    python_ver_specified=yes
  fi
fi

if [ "x$python_ver_specified" = "xno" -a "x$pythonlib" != "x" ]; then
  if [ -f $pythonlib/../bin/python ]; then
    pythonintv=$pythonlib/../bin/python
    pythonbin=$pythonlib/../bin
  fi
fi

# Check for Python
if [ "x$python3"  = "xyes" ] ; then
  if [ "x$pythoninc" = "x" ] ; then
    # First try to get pythoninc by querying the python interpreter
    echo "Checking for Python include paths from interpreter ${pythonintv}..."
    pythoninc=`${pythonintv} ./utils/python_get_paths.py inc | sed -e 's/ .*//g'`
    if [ "x$pythoninc" = "x" ] ; then
        echo "Checking for Python include paths relative to ${pythonbin}..."
        # If that fails, fall back to searching relative to the interpreter
        pythoninc=`echo ${pythonbin}../include/python3* | sed -e 's/ .*//g'`
    fi
    if [ -d $pythoninc ] ; then
        echo "Using Python3 include files from: $pythoninc"
    fi
    if [ ! -d $pythoninc ] ; then
        echo "Error: Cannot access Python include directory $pythoninc"
        exit 1
    fi
  fi
  if [ "x$pythonlib" = "x" ] ; then
    # First try to get pythonlib by querying the python interpreter
    echo "Checking for Python library path from interpreter ${pythonintv}..."
    pythonlib=`${pythonintv} ./utils/python_get_paths.py libdir | sed -e 's/ .*//g'`
    if [ "x$pythonlib" = "x" ] ; then
        echo "Checking for Python library path relative to ${pythonbin}..."
        # If that fails, fall back to searching relative to the interpreter
        for libdir in "lib64" "lib32" "lib" ; do
        pythonlib=`echo ${pythonbin}../${libdir}/python3* | sed -e 's/ .*//g'`
        if [ -d "$pythonlib" ] ; then
            echo "Using Python3 library files from: $pythonlib"
            break
        fi
        done
    else
        echo "Using Python3 library files from: $pythonlib"
    fi
    if [ ! -d $pythonlib ] ; then
      echo "Error: Cannot access Python lib directory $pythonlib"
      exit 1
    fi
  fi
else

if [ "x$python"  = "xyes" ] ; then
  if [ "x$pythoninc" = "x" ] ; then
    # First try to get pythoninc by querying the python interpreter
    echo "Checking for Python include paths from interpreter ${pythonintv}..."
    pythoninc=`${pythonintv} ./utils/python_get_paths.py inc | sed -e 's/ .*//g'`
    if [ "x$pythoninc" = "x" ] ; then
        # If that fails, fall back to searching relative to the interpreter
        echo "Checking for Python include paths relative to ${pythonbin}..."
        pythoninc=`echo ${pythonbin}../include/python* | sed -e 's/ .*//g'`
    fi
    if [ -d $pythoninc ] ; then
        echo "Using Python include files from: $pythoninc"
    fi
    if [ ! -d $pythoninc ] ; then
        echo "Error: Cannot access Python include directory $pythoninc"
        exit 1
    fi
  fi
  if [ "x$pythonlib" = "x" ] ; then
    echo "Checking for Python library path from interpreter ${pythonintv}..."
    # First try to get pythonlib by querying the python interpreter
    pythonlib=`${pythonintv} ./utils/python_get_paths.py libdir | sed -e 's/ .*//g'`
    if [ "x$pythonlib" = "x" ] ; then
        # If that fails, fall back to searching relative to the interpreter
        echo "Checking for Python library path relative to ${pythonbin}..."
        for libdir in "lib64" "lib32" "lib" ; do
        pythonlib=`echo ${pythonbin}../${libdir}/python* | sed -e 's/ .*//g'`
        if [ -d "$pythonlib" ] ; then
            echo "Using Python library files from: $pythonlib"
            break
        fi
        done
    else
        echo "Using Python library files from: $pythonlib"
    fi
    if [ ! -d $pythonlib ] ; then
      echo "Error: Cannot access Python lib directory $pythonlib"
      exit 1
    fi
  fi
fi

fi

#Check for rocm version, rocm-6 has files includes and library files in
#a different location compared to older versions, include/rocm-core
#should only exist in rocm-6
if [ "x$rocm" = "xyes" ]; then

cat <<EOF > tau_rocm6_vtest.c
#include <rocm_version.h>

#if (ROCM_VERSION_MAJOR < 6)
#error "ROCM version is lower than 6"
#endif
EOF
	if [ "x$rocmcore_dir" = "x" ]; then
		rocmcore_dir=$rocmdir
	fi
	if [ -d $rocmcore_dir/include/rocm-core ]; then
		if [ $c_compiler = default ]
	        then
        	    c_compiler=cc
	        fi
		if  $c_compiler $orig_useropt -c tau_rocm6_vtest.c -I$rocmcore_dir/include/rocm-core 1> /dev/null 2>&1
		then
			rm tau_rocm6_vtest.o
			rocmv6=yes
			fixmakeargs="$fixmakeargs TAU_USE_GPU"
            		pthread=yes
            		tauoptions="${tauoptions}-rocmv6"
		else
			rocmv6=no
		fi
		rm tau_rocm6_vtest.c
	else
		rocmv6=no
	fi
fi

if [ "x$rocm" = "xyes" -a "$rocprofv2" = "yes" -a "$rocmv6" = "no" ]; then
    echo "For rocprofv2 use rocm/6 or newer!"
    exit 1
fi


if [ "x$rocm" = "xyes" -a "$rocprofsdk" = "yes" -a "$rocmv6" = "no" ]; then
    echo "For rocprof-sdk use rocm/6 or newer!"
    exit 1
fi

if [ "x$rocm" = "xyes" -a "$rocprofv2" = "yes" -a "$rocmv6" = "yes" ]; then
    pthread=yes
    if [ "x$rocprofilerdir" = "x" ]; then
        rocprofilerdir=$rocmdir
        fixmakeargs="$fixmakeargs rocprofilerdir=$rocprofilerdir"
    fi

    if [ "x$rocprofilerinc" = "x" ]; then
        rocprofilerinc="$rocprofilerdir/include/"
        fixmakeargs="$fixmakeargs rocprofilerinc=$rocprofilerinc"
    fi
fi

if [ "x$rocm" = "xyes" -a "$rocprofsdk" = "yes" -a "$rocmv6" = "yes" ]; then
    pthread=yes
    if [ "x$rocprofsdk_dir" = "x" ]; then
        rocprofsdk_dir=$rocmdir
        fixmakeargs="$fixmakeargs rocprofsdk_dir=$rocprofsdk_dir"
    fi
    if [  "$elfutils" = "yes" ]; then
        fixmakeargs="$fixmakeargs ROCPROFILERSDK_PC"
        if [ "x$comgr_dir" = "x" ]; then
            comgr_dir=$rocmdir
            if [ -r $comgr_dir/include/amd_comgr/amd_comgr.h ]; then
                    fixmakeargs="$fixmakeargs comgr_dir=$comgr_dir"
                    #echo "Found amd_comgr.h in directory $comgr_dir/include/amd_comgr/"
            else
                echo "Error: Cannot find amd_comgr.h in directory $comgr_dir/include/amd_comgr/"
                exit 1
            fi
        fi
    else
        echo "NOTE: TAU's ROCm ROCPROFILER SDK PC Sampling Interface Unavailable."
        echo "      Configure with -elfutils=download to enable PC Sampling."
    fi
fi

if [ "$rocmv6" = "yes" -a "$rocprofv2" = "no" -a "$rocprofsdk" = no ] ; then
    if [ "$roctracer" = yes ]; then
        fixmakeargs="$fixmakeargs ROCTRACERROCM6"
        if [ -r $roctracerdir/include/roctracer/roctracer_hsa.h ]; then
            fixmakeargs="$fixmakeargs ROCTRACERROCM6_HSA"
        fi

    fi

    if [ "x$rocprofilerdir" = "x" ]; then
        rocprofilerdir=$rocmdir
    fi

    if [ "$rocprofiler" = yes -a -d $rocprofilerdir/include/rocprofiler ]; then
        fixmakeargs="$fixmakeargs rocprofilerdir=$rocprofilerdir  ROCPROFILER ROCPROFILERROCM6"
    fi

    if [ "x$rocprofilerlibrary" = "x" ]; then
        rocprofilerlibrary=librocprofiler64.so
        fixmakeargs="$fixmakeargs rocprofilerlibrary=$rocprofilerlibrary"
    fi

    if [ "x$rocprofilerinc" = "x" ]; then
        rocprofilerinc="$rocprofilerdir/include/rocprofiler"
        fixmakeargs="$fixmakeargs rocprofilerinc=$rocprofilerinc"
    fi
fi

if [ "$rocmv6" = "no" ] ; then

    if [ "x$rocm" = "xyes" -a "x$rocprofiler" = "xyes" -a "x$rocprofilerdir" = "x" -a -d $rocmdir/rocprofiler -a "x$roctracerdir" = "x" ]; then
      rocprofilerdir=$rocmdir/rocprofiler
      fixmakeargs="$fixmakeargs rocprofilerdir=$rocprofilerdir ROCPROFILER TAU_USE_GPU"
      rocprofiler=yes
      tauoptions="${tauoptions}-rocprofiler"
      pthread=yes
    elif [ "x$rocprofiler" = "xyes" ]; then
      fixmakeargs="$fixmakeargs ROCPROFILER"
    fi
    if [ "x$rocm" = "xyes" -a "x$rocmsmi" = "xno" ]; then
      if [ -r $rocmdir/include/rocm_smi/rocm_smi.h ]; then
        echo "No rocmsmi specified, using $rocmdir as $rocmdir/include/rocm_smi/rocm_smi.h is found"
        fixmakeargs="$fixmakeargs rocmsmidir=$rocmdir"
        fixmakeargs="$fixmakeargs ROCMSMI"
      fi
    fi
    # AMD ROCm rocprofiler directories
    if [ "$rocprofiler" = yes -a "$rocm" = "no" ]; then
       if [ "x$rocmdir" = "x" -a -d /opt/rocm ]; then
         rocmdir=/opt/rocm
         fixmakeargs="$fixmakeargs rocmdir=$rocmdir ROCM"
       fi
    fi
    if [ "$rocprofiler" = yes -a "x$rocprofilerinc" = "x" ]; then
       if [ "x$rocprofilerlibrary" = "x" ];
       then
         rocprofilerlibrary=librocprofiler64.so
         fixmakeargs="$fixmakeargs rocprofilerlibrary=$rocprofilerlibrary"
       fi
       if [ "x$rocprofilerinc" = "x" ]; then
         rocprofilerinc="$rocprofilerdir/inc"
         fixmakeargs="$fixmakeargs rocprofilerinc=$rocprofilerinc"
       fi
    fi
fi

if [ "x$use_opencl" = "xyes" -a "x$openclinclude" = "x" ]; then
  ld_lib_path=`echo $LD_LIBRARY_PATH | sed -e "s@:@ @g" `
  for d in $ld_lib_path ; do
    echo "OPENCL: CHECKING $d"
    if [ -r $d/libOpenCL.so ]; then
      echo "checking $d"
      openclinclude=`echo $d | sed -e "s@/lib64@@g" -e "s@loader@headers@g" `/include
      if [ ! -d $openclinclude ]; then
              openclinclude=`echo $d | sed -e "s@/lib@@g"`/include
      fi
      echo "openclinclude=$openclinclude"
      if [ -f "$openclinclude/CL/cl.h" ] ; then
      #                       useropt="$useropt#-DTAU_MAX_THREADS=32"
        fixmakeargs="$fixmakeargs OPENCL TAU_USE_GPU openclinclude=$openclinclude"
        break;
      else
        if [ -f "$openclinclude/sycl/CL/cl.h" ]; then
          fixmakeargs="$fixmakeargs OPENCL TAU_USE_GPU openclinclude=$openclinclude/sycl"
        else
            echo "Error: Cannot find GPU headers, TAU currently supports either CUDA or OpenCL"
            exit 0
        fi
      fi
    fi
  done
fi

if [ "x$upcnetwork" = "x" ] ; then
    if [ "$mpi" = "no" ] ; then
        #echo "NOTE: UPC network and MPI not specified.  Assuming -upcnetwork=smp."
        upcnetwork=smp
    else
        #echo "NOTE: UPC network not specified.  Assuming -upcnetwork=mpi."
        upcnetwork=mpi
    fi
else
    if [ "$upcnetwork" == mpi ] && [ "$mpi" == "no" ] ; then
        #echo "NOTE: Assuming -mpi because -upcnetwork=mpi."
        mpi=yes
    fi
fi

# -- some functions

configure_java () {

    taushell=sh
    if [ $machine = solaris2* -o $machine = alpha ] ; then
        taushell=ksh
    fi

}

configure_perfexplorer () {



    cat ${tauroot}/tools/src/perfdmf/bin/configure.skel |
    sed -e 's,@TAUROOTDIR@,'$targetdir','  -e 's,@SHELL@,'$taushell',' -e 's,@ARCH@,'$architecture',' \
        > ${targetdir}/${architecture}/bin/perfdmf_configure
    chmod a+rx ${targetdir}/${architecture}/bin/perfdmf_configure
    cd ${targetdir}/${architecture}/bin
    /bin/rm -f taudb_configure
    ln -s perfdmf_configure taudb_configure
    cd ${tauroot}

    cat ${tauroot}/tools/src/perfdmf/bin/createapp.skel |
    sed -e 's,@TAUROOTDIR@,'$targetdir','  -e 's,@SHELL@,'$taushell',' -e 's,@ARCH@,'$architecture',' \
        > ${targetdir}/${architecture}/bin/perfdmf_createapp
    chmod a+rx ${targetdir}/${architecture}/bin/perfdmf_createapp

    cat ${tauroot}/tools/src/perfdmf/bin/createexp.skel |
    sed -e 's,@TAUROOTDIR@,'$targetdir','  -e 's,@SHELL@,'$taushell',' -e 's,@ARCH@,'$architecture',' \
        > ${targetdir}/${architecture}/bin/perfdmf_createexp
    chmod a+rx ${targetdir}/${architecture}/bin/perfdmf_createexp

    cat ${tauroot}/tools/src/perfdmf/bin/loadtrial.skel |
    sed -e 's,@TAUROOTDIR@,'$targetdir','  -e 's,@SHELL@,'$taushell',' -e 's,@ARCH@,'$architecture',' \
        > ${targetdir}/${architecture}/bin/perfdmf_loadtrial
    chmod a+rx ${targetdir}/${architecture}/bin/perfdmf_loadtrial
    cd ${targetdir}/${architecture}/bin
    /bin/rm -f taudb_loadtrial
    ln -s perfdmf_loadtrial taudb_loadtrial
    cd ${tauroot}

    cat ${tauroot}/tools/src/perfdmf/bin/phaseconvert.skel |
    sed -e 's,@TAUROOTDIR@,'$targetdir','  -e 's,@SHELL@,'$taushell',' -e 's,@ARCH@,'$architecture',' \
        > ${targetdir}/${architecture}/bin/phaseconvert
    chmod a+rx ${targetdir}/${architecture}/bin/phaseconvert

    cat ${tauroot}/tools/src/tau_user_setup.sh.skel |
    sed -e 's,@TAUROOTDIR@,'$targetdir','  -e 's,@SHELL@,'$taushell',' -e 's,@ARCH@,'$architecture',' \
        > ${targetdir}/${architecture}/bin/tau_user_setup.sh
    chmod a+rx ${targetdir}/${architecture}/bin/tau_user_setup.sh

        # stuff for SSL key configuration
    cp ${tauroot}/tools/src/perfdmf/bin/taudb_keygen ${targetdir}/${architecture}/bin/.
    cp ${tauroot}/tools/src/perfdmf/bin/taudb_install_cert ${targetdir}/${architecture}/bin/.

    # copy these files to the etc directory for tau_user_setup.sh to find and use
    mkdir -p ${targetdir}/etc
    cp ${tauroot}/tools/src/perfdmf/bin/derby.properties.skel ${targetdir}/etc
    cp ${tauroot}/tools/src/contrib/jython.registry ${targetdir}/etc

    cat ${tauroot}/tools/src/perfexplorer/bin/configure.skel |
    sed -e 's,@TAUROOTDIR@,'$tauroot',' \
        -e 's,@SHELL@,'$taushell',' \
        -e 's,@ARCH@,'$architecture',' \
        -e 's,@TARGETDIR@,'$targetdir',' \
        > ${tauroot}/tools/src/perfexplorer/configure
    chmod a+rx ${tauroot}/tools/src/perfexplorer/configure

    cat ${tauroot}/tools/src/perfexplorer/bin/perfexplorer_configure.skel |
    sed -e 's,@TAUROOTDIR@,'$targetdir',' \
        -e 's,@SHELL@,'$taushell',' \
        -e 's,@ARCH@,'$architecture',' \
        -e 's,@TARGETDIR@,'$targetdir',' \
        > ${targetdir}/${architecture}/bin/perfexplorer_configure
    chmod a+rx ${targetdir}/${architecture}/bin/perfexplorer_configure

    cat ${tauroot}/tools/src/java-1.4/configure-1.4.skel |
    sed -e 's,@TAUROOTDIR@,'$targetdir',' \
        -e 's,@SHELL@,'$taushell',' \
        -e 's,@ARCH@,'$architecture',' \
        -e 's,@TARGETDIR@,'$targetdir',' \
        > ${targetdir}/${architecture}/bin/configure-1.4
    chmod a+rx ${targetdir}/${architecture}/bin/configure-1.4


    if [ ! -d ${targetdir}/etc ] ; then
        mkdir -p ${targetdir}/etc
    fi
    cp ${tauroot}/tools/src/perfexplorer/bin/perfexplorer.skel ${targetdir}/etc/.
    cp ${tauroot}/tools/src/perfexplorer/etc/dbschema.derby ${targetdir}/etc/.
    cp ${tauroot}/tools/src/perfexplorer/etc/dbschema.h2 ${targetdir}/etc/.
#    cp ${tauroot}/tools/src/perfexplorer/etc/dbschema.mysql ${targetdir}/etc/.
    cp ${tauroot}/tools/src/perfexplorer/etc/dbschema.oracle ${targetdir}/etc/.
    cp ${tauroot}/tools/src/perfexplorer/etc/dbschema.postgresql ${targetdir}/etc/.
    cp ${tauroot}/tools/src/perfexplorer/etc/dbschema.db2 ${targetdir}/etc/.
    cp ${tauroot}/tools/src/perfexplorer/etc/java.policy ${targetdir}/etc/.
    if [ ! -d ${targetdir}/etc/topology ]; then
        cp -r ${tauroot}/etc/topology ${targetdir}/etc
        cp ${tauroot}/etc/blacklist.txt ${targetdir}/etc/.
    fi

    # copy PerfDMF schema files
    cp ${tauroot}/tools/src/perfdmf/etc/* ${targetdir}/etc/.

    # copy PTTS base html files
    if [ ! -r ${targetdir}/etc/ptts.html.index ] ; then
      cp ${tauroot}/etc/ptts.html.* ${targetdir}/etc/.
    fi

    if [ ! -d ${targetdir}/tools/src/perfdmf/etc ] ; then
        mkdir -p ${targetdir}/tools/src/perfdmf/etc
    fi
    cp ${tauroot}/tools/src/perfdmf/etc/dbschema*.txt ${targetdir}/tools/src/perfdmf/etc/. 2>/dev/null


    BINTARGET=${targetdir}/${architecture}/bin
    JARTARGET=${targetdir}/${architecture}/lib


    #it needed JARTARGET
    #it modifies paraprof.skel
    copy_own_cube_reader_if_selected


    # install paraprof
    cat ${tauroot}/tools/src/paraprof/bin/paraprof.skel |
    sed -e 's,@TAUROOTDIR@,'$targetdir','  -e 's,@SHELL@,'$taushell',' -e 's,@ARCH@,'$architecture',' > $BINTARGET/paraprof
    chmod a+rx $BINTARGET/paraprof

    # install ppscript
    cat ${tauroot}/tools/src/ppscript.skel |
    sed -e 's,@TAUROOTDIR@,'$targetdir','  -e 's,@SHELL@,'$taushell',' -e 's,@ARCH@,'$architecture',' > $BINTARGET/ppscript
    chmod a+rx $BINTARGET/ppscript

    # install portal utils
    cat ${tauroot}/tools/src/tau_portal/bin/perfdmfdb.py.skel | sed -e 's,@BINDIR@,'$BINTARGET',' > $BINTARGET/perfdmfdb.py
    cat ${tauroot}/tools/src/tau_portal/bin/portal.py.skel | sed -e 's,@BINDIR@,'$BINTARGET',' > $BINTARGET/portal.py
    cp ${tauroot}/tools/src/tau_portal/bin/tau_portal.py $BINTARGET
    chmod a+rx $BINTARGET/tau_portal.py $BINTARGET/portal.py $BINTARGET/perfdmfdb.py

    cp include/tauarch.h.default include/tauarch.h
    if [ $default = "true" ] ; then
        echo "#define TAU_BIN_DIR \"default\"" >> include/tauarch.h
    else
        echo "#define TAU_BIN_DIR \"$BINTARGET\"" >> include/tauarch.h
        echo "#define TAU_LIB_DIR \"$targetdir/$machine/lib/shared$tauoptions\"" >> include/tauarch.h
    fi

    cat ${tauroot}/tools/src/perfexplorer/bin/perfexplorer.skel |
    sed -e 's,@TAUROOT@,'$targetdir',' \
        -e 's,@TAUSHELL@,'$taushell',' \
        -e 's,@TAUARCH@,'$architecture',' \
        -e 's,@RROOT@,'$rroot',' \
        -e 's,@ANALYSIS_ENGINE@,'weka',' \
        -e 's,@SERVER_HOSTNAME@,'$serverhostname',' \
        -e 's,@SERVER_OBJECT_PORT@,'$objectport',' \
        -e 's,@SERVER_RMIREGISTRY_PORT@,'$registryport',' \
        -e 's,@CONFIGFILE@,'$configfile',' \
        > $BINTARGET/perfexplorer
    chmod a+rx $BINTARGET/perfexplorer

    # install tau_gen_wrapper
    cp ${tauroot}/tools/src/tau_gen_wrapper $BINTARGET

    # install tau_macro.sh
    cp ${tauroot}/tools/src/tau_macro.sh $BINTARGET


    if [ ! -f $BINTARGET/jracy ] ; then
        cd $BINTARGET/; ln -s paraprof jracy; cd ${tauroot};
    fi

    #installs the tau_java handeler
    cat ${tauroot}/tools/src/tau_java.skel | sed -e 's,@JARTARGET@,'$JARTARGET',' -e 's,@JDKDIR@,'$jdkdir',' > $BINTARGET/tau_java
    chmod a+rx $BINTARGET/tau_java


    cp ${tauroot}/tools/src/paraprof/bin/paraprof.jar $JARTARGET
    cp ${tauroot}/tools/src/perfdmf/bin/perfdmf.jar $JARTARGET
    cp ${tauroot}/tools/src/perfexplorer/perfexplorer.jar $JARTARGET
    cp ${tauroot}/src/Profile/TAU.jar $JARTARGET
    cp ${tauroot}/tools/src/perfexplorer/perfexplorer-1.4.jar $JARTARGET
    cp ${tauroot}/tools/src/vis/bin/vis.jar $JARTARGET
    cp ${tauroot}/tools/src/vis-jogl2/bin/vis-jogl2.jar $JARTARGET
    cp ${tauroot}/tools/src/common/bin/tau-common.jar $JARTARGET
    cp ${tauroot}/tools/src/contrib/jargs.jar $JARTARGET
    cp ${tauroot}/tools/src/contrib/google-gson-2.1/gson-2.1.jar $JARTARGET
    cp ${tauroot}/tools/src/contrib/batik-combined.jar $JARTARGET
    cp ${tauroot}/tools/src/contrib/jfreechart-1.0.12.jar $JARTARGET
    cp ${tauroot}/tools/src/contrib/jcommon-1.0.15.jar $JARTARGET
    cp ${tauroot}/tools/src/contrib/jgraph.jar $JARTARGET
    cp ${tauroot}/tools/src/contrib/jython.jar $JARTARGET
    cp ${tauroot}/tools/src/contrib/jogl/jogl.jar $JARTARGET
    #Used for jogl2
    cp ${tauroot}/tools/src/contrib/jogl/jogl-all.jar $JARTARGET
    cp ${tauroot}/tools/src/contrib/jogl/gluegen-rt.jar $JARTARGET

    cp ${tauroot}/tools/src/contrib/derby.jar $JARTARGET
    cp ${tauroot}/tools/src/contrib/h2.jar $JARTARGET
#    cp ${tauroot}/tools/src/contrib/mysql.jar $JARTARGET
    cp ${tauroot}/tools/src/contrib/postgresql.jar $JARTARGET
    cp ${tauroot}/tools/src/contrib/xerces.jar $JARTARGET
    cp ${tauroot}/tools/src/contrib/mesp.jar $JARTARGET
    cp ${tauroot}/tools/src/common/resources/tau16x16.gif $JARTARGET

    # install the perl scripts for ebs post-processing in the bin dir
    cp ${tauroot}/tools/src/tau_ebs_process.pl $BINTARGET
    cp ${tauroot}/utils/ebs2otf/tau_ebs2otf.pl $BINTARGET
    cp ${tauroot}/tools/src/pycoolr/bin/pycoolr $BINTARGET
    chmod a+rx $BINTARGET/pycoolr
    cp -r ${tauroot}/tools/src/pycoolr/src/pycoolrgui $BINTARGET

    # install post-mortem address translation tool
    cp ${tauroot}/tools/src/tau_resolve_addresses.py $BINTARGET
    chmod +x $BINTARGET/tau_resolve_addresses.py

    # install tau_baseline toool
    cp ${tauroot}/tools/src/tau_baseline $BINTARGET
    chmod +x $BINTARGET/tau_baseline

    case $machine in

        apple )
            gunzip -c tools/src/contrib/jogl/apple/libjogl.jnilib.gz > $JARTARGET/libjogl.jnilib
            gunzip -c tools/src/contrib/jogl/apple/libjogl_awt.jnilib.gz > $JARTARGET/libjogl_awt.jnilib
            gunzip -c tools/src/contrib/jogl/apple/libjogl_cg.jnilib.gz > $JARTARGET/libjogl_cg.jnilib
            cp tools/src/contrib/jogl/apple/gluegen-rt-natives*.jar $JARTARGET #/gluegen-rt-natives.jar
            cp tools/src/contrib/jogl/apple/jogl-all-natives*.jar $JARTARGET #/jogl-all-natives.jar

            ;;

         arm64_apple)
            tar -xzf tools/src/contrib/jogl/arm64_apple/jogl-glob.tgz -C $JARTARGET

            ;;

        i386_linux)
            gunzip -c tools/src/contrib/jogl/i386_linux/libjogl.so.gz > $JARTARGET/libjogl.so
            gunzip -c tools/src/contrib/jogl/i386_linux/libjogl_awt.so.gz > $JARTARGET/libjogl_awt.so
            gunzip -c tools/src/contrib/jogl/i386_linux/libjogl_cg.so.gz > $JARTARGET/libjogl_cg.so
            gunzip -c tools/src/contrib/jogl/i386_linux/libjogl_drihack.so.gz > $JARTARGET/libjogl_drihack.so
            cp tools/src/contrib/jogl/i386_linux/gluegen-rt-natives*.jar $JARTARGET #/gluegen-rt-natives.jar
            cp tools/src/contrib/jogl/i386_linux/jogl-all-natives*.jar $JARTARGET #/jogl-all-natives.jar

            ;;

        arm64_linux)
            gunzip -c tools/src/contrib/jogl/arm64_linux/libgluegen-rt.so.gz > $JARTARGET/libgluegen-rt.so
            gunzip -c tools/src/contrib/jogl/arm64_linux/libjogl_desktop.so.gz > $JARTARGET/libjogl_desktop.so
            gunzip -c tools/src/contrib/jogl/arm64_linux/libnativewindow_awt.so.gz > $JARTARGET/libnativewindow_awt.so
            gunzip -c tools/src/contrib/jogl/arm64_linux/libnativewindow_x11.so.gz > $JARTARGET/libnativewindow_x11.so

            cp tools/src/contrib/jogl/arm64_linux/gluegen-rt.jar $JARTARGET #/gluegen-rt-natives.jar
            cp tools/src/contrib/jogl/arm64_linux/jogl-all.jar $JARTARGET #/jogl-all-natives.jar
            ;;

        arm_linux)

            gunzip -c tools/src/contrib/jogl/arm_linux/jogl.jar.gz > $JARTARGET/jogl.jar
            gunzip -c tools/src/contrib/jogl/arm_linux/libjogl.so.gz > $JARTARGET/libjogl.so
            gunzip -c tools/src/contrib/jogl/arm_linux/libjogl_awt.so.gz > $JARTARGET/libjogl_awt.so

            cp tools/src/contrib/jogl/arm_linux/gluegen-rt-natives*.jar $JARTARGET #/gluegen-rt-natives.jar
            cp tools/src/contrib/jogl/arm_linux/jogl-all-natives*.jar $JARTARGET #/jogl-all-natives.jar

            if [ -r tools/src/contrib/jogl/arm_linux/libjogl_drihack.so.gz ] ; then
              gunzip -c tools/src/contrib/jogl/arm_linux/libjogl_drihack.so.gz > $JARTARGET/libjogl_drihack.so
            fi

            if [ ! -r /usr/lib/libGL.so.1 ]
            then
                echo "NOTE: Did not find OpenGL library, copying TAU's Mesa libGL.so.1 to arm_linux/lib directory"
                gunzip -c tools/src/contrib/jogl/arm_linux/libGL.so.1.gz > $JARTARGET/libGL.so.1
            fi


            if [ -r tools/src/contrib/jogl/arm_linux/gluegen.jar.gz -a -r tools/src/contrib/jogl/arm_linux/libgluegen-rt.so.gz ] ; then
              gunzip -c tools/src/contrib/jogl/arm_linux/libgluegen-rt.so.gz > $JARTARGET/libgluegen-rt.so
              gunzip -c tools/src/contrib/jogl/arm_linux/gluegen.jar.gz > $JARTARGET/gluegen.jar
            fi

            ;;

        crayxmt)
            gunzip -c tools/src/contrib/jogl/x86_64/libjogl.so.gz > $JARTARGET/libjogl.so
            gunzip -c tools/src/contrib/jogl/x86_64/libjogl_awt.so.gz > $JARTARGET/libjogl_awt.so
            gunzip -c tools/src/contrib/jogl/x86_64/libjogl_drihack.so.gz > $JARTARGET/libjogl_drihack.so
            gunzip -c tools/src/contrib/jogl/x86_64/libjogl_cg.so.gz > $JARTARGET/libjogl_cg.so
            ;;

        nec-sx-aurora)
            gunzip -c tools/src/contrib/jogl/x86_64/libjogl.so.gz > $JARTARGET/libjogl.so
            gunzip -c tools/src/contrib/jogl/x86_64/libjogl_awt.so.gz > $JARTARGET/libjogl_awt.so
            gunzip -c tools/src/contrib/jogl/x86_64/libjogl_drihack.so.gz > $JARTARGET/libjogl_drihack.so
            gunzip -c tools/src/contrib/jogl/x86_64/libjogl_cg.so.gz > $JARTARGET/libjogl_cg.so
            ;;

        craycnl|sparc64fx)
            if [ $tau_cray_arm64 = no ]; then
              gunzip -c tools/src/contrib/jogl/x86_64/libjogl.so.gz > $JARTARGET/libjogl.so
              gunzip -c tools/src/contrib/jogl/x86_64/libjogl_awt.so.gz > $JARTARGET/libjogl_awt.so
              gunzip -c tools/src/contrib/jogl/x86_64/libjogl_drihack.so.gz > $JARTARGET/libjogl_drihack.so
              gunzip -c tools/src/contrib/jogl/x86_64/libjogl_cg.so.gz > $JARTARGET/libjogl_cg.so
            fi
            ;;

        xt3)
            gunzip -c tools/src/contrib/jogl/x86_64/libjogl.so.gz > $JARTARGET/libjogl.so
            gunzip -c tools/src/contrib/jogl/x86_64/libjogl_awt.so.gz > $JARTARGET/libjogl_awt.so
            gunzip -c tools/src/contrib/jogl/x86_64/libjogl_drihack.so.gz > $JARTARGET/libjogl_drihack.so
            gunzip -c tools/src/contrib/jogl/x86_64/libjogl_cg.so.gz > $JARTARGET/libjogl_cg.so
            ;;

        x86_64)
            gunzip -c tools/src/contrib/jogl/x86_64/libjogl.so.gz > $JARTARGET/libjogl.so
            gunzip -c tools/src/contrib/jogl/x86_64/libjogl_awt.so.gz > $JARTARGET/libjogl_awt.so
            gunzip -c tools/src/contrib/jogl/x86_64/libjogl_drihack.so.gz > $JARTARGET/libjogl_drihack.so
            gunzip -c tools/src/contrib/jogl/x86_64/libjogl_cg.so.gz > $JARTARGET/libjogl_cg.so
            cp tools/src/contrib/jogl/x86_64/gluegen-rt-natives*.jar $JARTARGET/gluegen-rt-natives.jar
            cp tools/src/contrib/jogl/x86_64/jogl-all-natives*.jar $JARTARGET/jogl-all-natives.jar
            ;;

        mic_linux)
            gunzip -c tools/src/contrib/jogl/x86_64/libjogl.so.gz > $JARTARGET/libjogl.so
            gunzip -c tools/src/contrib/jogl/x86_64/libjogl_awt.so.gz > $JARTARGET/libjogl_awt.so
            gunzip -c tools/src/contrib/jogl/x86_64/libjogl_drihack.so.gz > $JARTARGET/libjogl_drihack.so
            gunzip -c tools/src/contrib/jogl/x86_64/libjogl_cg.so.gz > $JARTARGET/libjogl_cg.so
            ;;


        ia64)
            gunzip -c tools/src/contrib/jogl/ia64/libjogl.so.gz > $JARTARGET/libjogl.so
            gunzip -c tools/src/contrib/jogl/ia64/libjogl_awt.so.gz > $JARTARGET/libjogl_awt.so
            gunzip -c tools/src/contrib/jogl/ia64/libjogl_drihack.so.gz > $JARTARGET/libjogl_drihack.so
            ;;

        solaris2)
            gunzip -c tools/src/contrib/jogl/solaris2/libjogl.so.gz > $JARTARGET/libjogl.so
            gunzip -c tools/src/contrib/jogl/solaris2/libjogl_awt.so.gz > $JARTARGET/libjogl_awt.so
            gunzip -c tools/src/contrib/jogl/solaris2/libjogl_drihack.so.gz > $JARTARGET/libjogl_drihack.so
            ;;

        solaris2-64)
            gunzip -c tools/src/contrib/jogl/solaris2-64/libjogl.so.gz > $JARTARGET/libjogl.so
            gunzip -c tools/src/contrib/jogl/solaris2-64/libjogl_awt.so.gz > $JARTARGET/libjogl_awt.so
            gunzip -c tools/src/contrib/jogl/solaris2-64/libjogl_drihack.so.gz > $JARTARGET/libjogl_drihack.so
            ;;

        sun386i)
            gunzip -c tools/src/contrib/jogl/sun386i/libjogl.so.gz > $JARTARGET/libjogl.so
            gunzip -c tools/src/contrib/jogl/sun386i/libjogl_awt.so.gz > $JARTARGET/libjogl_awt.so
            gunzip -c tools/src/contrib/jogl/sun386i/libjogl_drihack.so.gz > $JARTARGET/libjogl_drihack.so
            ;;

        sunx86_64)
            gunzip -c tools/src/contrib/jogl/sunx86_64/libjogl.so.gz > $JARTARGET/libjogl.so
            gunzip -c tools/src/contrib/jogl/sunx86_64/libjogl_awt.so.gz > $JARTARGET/libjogl_awt.so
            gunzip -c tools/src/contrib/jogl/sunx86_64/libjogl_drihack.so.gz > $JARTARGET/libjogl_drihack.so
            ;;

        ppc64)
            gunzip -c tools/src/contrib/jogl/ppc64/jogl.jar.gz > $JARTARGET/jogl.jar
            gunzip -c tools/src/contrib/jogl/ppc64/libjogl.so.gz > $JARTARGET/libjogl.so
            gunzip -c tools/src/contrib/jogl/ppc64/libjogl_awt.so.gz > $JARTARGET/libjogl_awt.so
            gunzip -c tools/src/contrib/jogl/ppc64/libjogl_drihack.so.gz > $JARTARGET/libjogl_drihack.so
            cp tools/src/contrib/jogl/ppc64/gluegen-rt-natives*.jar $JARTARGET/gluegen-rt-natives.jar
            cp tools/src/contrib/jogl/ppc64/jogl-all-natives*.jar $JARTARGET/jogl-all-natives.jar
            ;;

        ibm64linux)
            cp tools/src/contrib/jogl/ppc64/gluegen-rt-natives*.jar $JARTARGET/gluegen-rt-natives.jar
            cp tools/src/contrib/jogl/ppc64/jogl-all-natives*.jar $JARTARGET/jogl-all-natives.jar
            ;;

        bgp)
            gunzip -c tools/src/contrib/jogl/ppc64/jogl.jar.gz > $JARTARGET/jogl.jar
            gunzip -c tools/src/contrib/jogl/ppc64/libjogl.so.gz > $JARTARGET/libjogl.so
            gunzip -c tools/src/contrib/jogl/ppc64/libjogl_awt.so.gz > $JARTARGET/libjogl_awt.so
            gunzip -c tools/src/contrib/jogl/ppc64/libjogl_drihack.so.gz > $JARTARGET/libjogl_drihack.so
            ;;

        bgq)
            gunzip -c tools/src/contrib/jogl/bgq/jogl.jar.gz > $JARTARGET/jogl.jar
            gunzip -c tools/src/contrib/jogl/bgq/libjogl.so.gz > $JARTARGET/libjogl.so
            gunzip -c tools/src/contrib/jogl/bgq/libjogl_awt.so.gz > $JARTARGET/libjogl_awt.so
            gunzip -c tools/src/contrib/jogl/bgq/libjogl_drihack.so.gz > $JARTARGET/libjogl_drihack.so
            if [ -r ${JAVA_HOME}/lib/ppc64/libjawt.so ]; then
                /bin/rm -f $JARTARGET/libjawt.so
                ln -s ${JAVA_HOME}/lib/ppc64/libjawt.so $JARTARGET/libjawt.so
            fi
            ;;

        *)
            echo "Platform does not support JOGL, no 3d displays will be available in ParaProf"

            ;;
    esac


}

make_directories () {
    if [ "$tauprefix" = "unknown" ] ; then
        fixmakeargs="$fixmakeargs tauprefix=$tauroot"
    fi

    # make bin dir if it does not exist
    if [ ! -d $tautoplevel/$architecture ] ; then
        mkdir -p $tautoplevel/$architecture
    fi
    if [ ! -d $tautoplevel/$architecture/bin ] ; then
        mkdir -p $tautoplevel/$architecture/bin
    fi

    # make lib dir if it does not exist
    if [ ! -d $tautoplevel/$architecture/lib ] ; then
        mkdir -p $tautoplevel/$architecture/lib
    fi
}


# -- set up portable echo command
case "`echo 'x\c'`" in
    'x\c')  echo="echo -n"  nnl= ;;     #BSD
    x)      echo="echo"     nnl="\c";;  #SysV
    *)      echo 'Cannot setup echo. What weird machine do you have?' 1>2&
        echo="echo"  nnl="\c" ;;
esac

# lth@cs.uoregon.edu: don't mess with the compiler if it is 'default'.
# The user may define TAU_CCOM as an environment variable or let it be blank;
# if blank, it is defaulted in archfind.

if [ "$c_compiler" != "" -a "$c_compiler" != "default" ]; then
    TAU_CCOM="$c_compiler"          # for 'archfind'
    if [ "$c_compiler" = pgcc -o "$c_compiler" = hcc -o "$c_compiler" = hipcc -o "$c_compiler" = nvc ]
    then
       TAU_CCOM=""
    fi
    if [ "$c_compiler" = mpc_cc ]
    then
      TAU_CCOM="$mpc_c_compiler"
    fi
    export TAU_CCOM
fi

if [ "$cxx_compiler" = "hipcc" ]; then
  suppress_pthread_create_wrapper=yes
  fixmakeargs="$fixmakeargs HIPCC"
fi


echo "-------------------- TAU configure script ------------------"


tauroot=`pwd | sed -e 's,^/tmp_mnt/nfs,,' -e 's,^/tmp_mnt,,'`

if [ ${tauprefix} = unknown ] ; then
    targetdir=${tauroot}
    tautoplevel=$tauroot
else
    targetdir=${tauprefix}
    tautoplevel=$tauprefix
fi

cp include/tauroot.h.default include/tauroot.h
echo \#define TAUROOT \"$tauroot\" > include/tauroot.h
if [ $tauprefix != unknown ] ; then
    echo \#define TAUROOT \"$tauprefix\" > include/tauroot.h
fi

echo "  The TAU source code has just been configured to use the"
echo "  tau root directory $tauroot."
echo "  If you move the Tau distribution, you must run configure"
echo "  again and recompile"
echo "------------------------------------------------------------"

#
# Save configuration environment for future reference
#
md5sum="`which md5sum 2>/dev/null || which md5 2>/dev/null`"
if [ "x$md5sum" = "x" ] ; then
  echo "Warning: can't find md5sum or md5."
else
  confenvdir="$tauroot/.configure_env"
  argsum=`echo "$all_args" | $md5sum | awk '{print $1}'`
  mkdir -p "$confenvdir"
  confenvfile="$confenvdir/$argsum"
  set > "$confenvfile"
  fixmakeargs="$fixmakeargs confenvfile=$confenvfile"
fi

fixmakeargs="$fixmakeargs tauroot=$tauroot"
# Try and figure out architecture
detectarch=unknown
cd utils
detectarch=`./archfind`
detectxdev=`./archfind -x`
cd ..
if [ "x$detectarch" = "x" ] ; then
  detectarch=`TAU_CCOM=gcc utils/archfind`
  detectxdev=`TAU_CCOM=gcc utils/archfind -x`
  if [ "x$detectarch" = "x" ] ; then
    detectarch=unknown
  fi
fi

if [ $tauarch = unknown -a "x$detectarch" = "xrs6000" ]; then
# use 64 bit mode on AIX as default instead of 32 (rs6000)
  detectarch="ibm64"
fi

case "$tauarch" in
  i386_linux|i386_solaris|arm_android|arm_linux|mpis32|ppc64)
    fixmakeargs="$fixmakeargs arch_width=32bit"
    ;;
  *)
    fixmakeargs="$fixmakeargs arch_width=64bit"
    ;;
esac

###############################################################
# if there are no arguments, GUESS at system configuration
if [ $tauarch = unknown -o $tauarch = none ]
then
    if [ $# = 1 ]
    then
        if [ $1 = default ]
        then
            machine=$1
        fi
    else
        echo Attempting to auto-configure system, determining architecture...
        machine=$detectarch
        if [ $machine = unknown ]
        then
            echo I could not determine the architecture of this host
            echo You must give me a hint.
            echo Perhaps, the C compiler is not working.
            echo Please check the licenses.
            exit 0
        fi
        echo I think this is a $machine...
    fi
else
    machine=$tauarch
    if [ $detectarch != unknown -a $machine != unknown ] ; then
        detectxdev=$machine
        if [ $detectarch != $machine ] ; then
            echo WARNING\!\! Auto-detect:$detectarch overridden with $machine
        fi
    fi
fi

#detech the java version.
if [ "x$java" = "xyes" ]
then
    if [ "x$machine" = "xapple" ]
    then
        #echo "${jdkdir}/Commands/java -version"
        jv=`${jdkdir}/Commands/java -version 2>&1 | grep version | awk '{ print $3; }'| sed -e s/\"//g`
    else
        jv=`${jdkdir}/bin/java -version 2>&1 | grep version | awk '{ print $3; }'| sed -e s/\"//g`
    fi
    #echo "jv = $jv"
    major=`echo $jv | sed 's/\([0-9]*\).*/\1/g'`
    minor=`echo $jv | sed 's/'${major}'.\([0-9]*\).*/\1/g'`
    #echo "major = $major, minor = $minor"
    if [ "x$major" = "x1" ]
    then
        if [ $minor -lt 5 ]
        then
            echo "Error: Please specify a JDK version 1.5 or later to use JVMTI based instrumentation"
        else
            fixmakeargs="$fixmakeargs JVMTI jdkdir=$jdkdir"
        fi
    fi
fi


# use -m32 when specifying 32 bit on an Opteron/EM64T
if [ "x$detectarch" = "xx86_64" -a "x$machine" = "xi386_linux" ] ; then
    fixmakeargs="$fixmakeargs FORCEIA32"
fi


if [ $machine = mips ] ; then
    sicortex=yes
fi
if [ $machine = mips32 ] ; then
    sicortex=yes
fi

if [ $sicortex = yes ] ; then
    sicortexlink=""
    sicortexmpi=""
    if [ $epilog = yes ] ; then
        epiloglibdir=$epilogdir/lib
        elglibrary="-lelg"
        if [ $mpi = yes -a $openmp = yes ] ; then
            # hybrid
            sicortexlink="-L$epiloglibdir#-Wl,-whole-archive#-lfmpi#-lelg.ompi#-Wl,-no-whole-archive#-Wl,-Bstatic#-lbfd#-liberty#-Wl,-Bdynamic#-lscmpi"
        elif [ $mpi = yes ] ; then
            # mpi
            sicortexlink="-L$epiloglibdir#-lfmpi#-lelg.mpi#-Wl,-Bstatic#-lbfd#-liberty#-Wl,-Bdynamic#-lscmpi"
        elif [ $openmp = yes ] ; then
            # openmp
            sicortexlink="-L$epiloglibdir#-Wl,-whole-archive#-lelg.omp#-Wl,-no-whole-archive#-Wl,-Bstatic#-lbfd#-liberty#-Wl,-Bdynamic#-lscmpi"
        else
            # serial
            sicortexlink="-L$epiloglibdir#-lelg#-Wl,-Bstatic#-lbfd#-liberty#-Wl,-Bdynamic#-lscmpi"
        fi

    fi
    if [ $vampirtrace = yes ] ; then
        vampirtracelibdir=lib
        if [ $mpi = yes -a $openmp = yes ] ; then
            # hybrid
            sicortexlink="-L$vampirtracedir/$vampirtracelibdir#-Wl,-whole-archive#-lvt.fmpi#-lvt.ompi#-lotf#-Wl,-no-whole-archive#-Wl,-Bstatic#-lbfd#-liberty#-Wl,-Bdynamic#-lscmpi"
        elif [ $mpi = yes ] ; then
            # mpi
            sicortexlink="-L$vampirtracedir/$vampirtracelibdir#-lvt.fmpi#-lvt.mpi#-lotf#-Wl,-Bstatic#-lbfd#-liberty#-Wl,-Bdynamic#-lscmpi"
        elif [ $openmp = yes ] ; then
            # openmp
            sicortexlink="-L$vampirtracedir/$vampirtracelibdir#-Wl,-whole-archive#-lvt.omp#-lotf#-Wl,-no-whole-archive#-Wl,-Bstatic#-lbfd#-liberty#-Wl,-Bdynamic#-lscmpi"
        else
            # serial
            sicortexlink="-L$vampirtracedir/$vampirtracelibdir#-lvt#-lotf#-Wl,-Bstatic#-lbfd#-liberty#-Wl,-Bdynamic#-lscmpi"
        fi

    fi

    fixmakeargs="$fixmakeargs SICORTEX sicortexlink=${sicortexlink}"
fi


######################################################################
# If the default gcc/g++ is not used, Modify the Makefiles...
# Choose the c++ and cc compiler

case $machine in
    tc2000 | ksr1 | c90 | cray | t3e | crayx1 | craysv1)
        if [ $cxx_compiler = default ]
        then
            cxx_compiler=CC
        fi

        if [ $c_compiler = default ]
        then
            c_compiler=cc
        fi
        ;;

    nec)
        if [ $cxx_compiler = default ]
        then
            cxx_compiler=c++
        fi

        if [ $c_compiler = default ]
        then
            c_compiler=cc
        fi
        ;;
    arm64_linux)
        if [ "x$cxx_compiler" = "xdefault" -a "x$mpi" = "xyes" ]; then
          # Are we on a Fujitsu FX aarch64 system with Fujitsu compilers in the path?
          found_compiler=`which mpiFCC 2>/dev/null`
          if [ "x$found_compiler" != "x" -a -x "$found_compiler" ]; then
             echo "TAU: Using mpiFCC, mpifcc, and mpifrt as C++, C, and Fortran compilers respectively."
             cxx_compiler=mpiFCC
             c_compiler=mpifcc
             fortran_compiler=mpifrt
          else
	    found_compiler=`which mpicxx 2>/dev/null`
	    if [ "x$found_compiler" != "x" -a -x "$found_compiler" ]; then
               echo "TAU: Using mpicxx, mpicc, and mpif90 as C++, C, and Fortran compilers respectively."
               cxx_compiler=mpicxx
               c_compiler=mpicc
	       found_compiler=`which gfortran 2>/dev/null`
               if [ "x$found_compiler" != "x" -a -x "$found_compiler" ]; then
	          taugcclibdir=`g++ $tau_opt_bits -print-libgcc-file-name | sed -e 's,[^/]*$,,'`
	          fixmakeargs="$fixmakeargs taugcclibdir=$taugcclibdir"
		  fortran_compiler=gfortran
	       fi
            fi
          fi
	else
            if [ "x$cxx_compiler" = "xdefault" ]; then
	      found_compiler=`which g++ 2>/dev/null`
              if [ "x$found_compiler" != "x" -a -x "$found_compiler" ]; then
                echo "TAU: Using g++, gcc, and gfortran as C++, C, and Fortran compilers respectively."
                cxx_compiler=g++
                c_compiler=gcc
                fortran_compiler=gfortran
	        taugcclibdir=`g++ $tau_opt_bits -print-libgcc-file-name | sed -e 's,[^/]*$,,'`
	        fixmakeargs="$fixmakeargs taugcclibdir=$taugcclibdir"
              fi
            fi
        fi
        ;;

    apple | arm64_apple )
        if [ "$cxx_compiler" = "default" ]
        then
            cxx_compiler=g++
        fi

        if [ "$c_compiler" = "default" ]
        then
            c_compiler=gcc
        fi
        ;;


    cygwin)
        if [ $cxx_compiler = default ]
        then
            cxx_compiler=g++
        fi

        if [ $c_compiler = default ]
        then
            c_compiler=gcc
        fi
        ;;

    alpha)
        if [ $cxx_compiler = default ]
        then
            # If it has the HP/Compaq cxx compiler, use it
            if [ -d /lib/cmplrs/cxx ]
            then
                cxx_compiler=cxx
            else
                cxx_compiler=g++
            fi
            c_compiler=cc
        fi

        if [ $c_compiler = default ]
        then
            c_compiler=gcc
        fi
        ;;

    rs6000 | ibm64)
        if [ $cxx_compiler = default ]
        then
            # If it has the IBM xlC compiler, use it
            if [ -f /usr/bin/xlC -o -f /usr/vacpp/bin/xlC ]
            then
                cxx_compiler=xlC_r
                c_compiler=xlc_r
            else
                cxx_compiler=g++
            fi
        fi

        if [ $c_compiler = default ]
        then
            c_compiler=gcc
        fi
        ;;

    bgl)
        if [ $cxx_compiler = default ]
        then
            cxx_compiler=blrts_xlC
            c_compiler=blrts_xlc
        fi
        ;;

    bgp|bgq)
        if [ $cxx_compiler = default ] ; then
            cxx_compiler=mpixlcxx_r
            c_compiler=mpixlc_r

            # test compiler version for 09.00.0000.0006 PMR bug fix
            $echo "Checking version of IBM Compilers... ${nnl}"
            version=`mpixlc -qversion | tail -n 1 | awk '{ print $2;}'`
            echo "$version"
            eval v1=${version/./;v2=}
            eval v2=${v2/./;v3=}
            eval v3=${v3/./;v4=}

            pmr0006=no
            if [ $v1 -gt 9 ] ; then pmr0006=yes ; fi
            if [ $v1 -ge 9 -a $v2 -gt 0 ] ; then pmr0006=yes ; fi
            if [ $v1 -ge 9 -a $v2 -ge 0 -a $v3 -gt 0 ] ; then pmr0006=yes ; fi
            if [ $v1 -ge 9 -a $v2 -ge 0 -a $v3 -ge 0 -a $v4 -ge 6 ] ; then pmr0006=yes ; fi
            if [ $pmr0006 = yes ] ; then
                echo "new"
            else
                echo "old"
            fi



        elif [ $cxx_compiler = mpicxx ] ; then
            cxx_compiler=mpicxx
            #      c_compiler=mpicc
            #      gnu=yes
            #      if [ $machine = bgp ]; then
            #        fixmakeargs="$fixmakeargs GNU BGP_GFORTRAN COMPINST_GNU"
            #      else
            #        fixmakeargs="$fixmakeargs GNU BGQ_GFORTRAN COMPINST_GNU GNU46PLUS"
            #      fi
        fi


        # default to this BFD installation
        # disabling for now, the BFD on BGP causes problems for the
        # shared library version of TAU (gpaw python)
        # if [ $bfd = no ] ; then
        #         bfd=yes
        #         bfddir=/bgsys/drivers/ppcfloor/gnu-linux
        # fi

        ;;

    crayxmt)
        cxx_compiler=c++
        c_compiler=cc
        fixmakeargs="$fixmakeargs CRAYXMT"
        if [ $pdt_cxx_compiler = default ] ; then
            pdt_cxx_full_path=/usr/bin/g++
            pdt_cxx_compiler=/usr/bin/g++
        fi
        ;;

    nec-sx-aurora)
        if [ $cxx_compiler = default ]; then
          cxx_compiler=mpinc++
        fi
        if [ $c_compiler = default ]; then
          c_compiler=mpincc
        fi
        default_fortran=mpinfort
        if [ $fortran_compiler = no ] ; then
            fortran_compiler=mpinfort
        fi
        fixmakeargs="$fixmakeargs NEC_SX_AURORA"
        ;;

    xt3)
        catamount=yes
        cxx_compiler=CC
        c_compiler=cc
        default_fortran=pgi
        if [ "x$PE_ENV" = xPGI ] ; then
            #tauoptions="${tauoptions}-pgi"
            pgi=yes
            default_fortran=pgi
            fixmakeargs="$fixmakeargs COMPINST_PGI"
        elif [ "x$PE_ENV" = xGNU ] ; then
            gnu=yes
            tauoptions="${tauoptions}-gnu"
            default_fortran=gfortran
            fixmakeargs="$fixmakeargs COMPINST_GNU"
        elif [ "x$PE_ENV" = xPATHSCALE ] ; then
            tauoptions="${tauoptions}-pathscale"
            default_fortran=pathscale
            pathscale=yes
            fixmakeargs="$fixmakeargs COMPINST_GNU"
        fi
        if [ $fortran_compiler = no ] ; then
            fortran_compiler=$default_fortran
        fi

        # unless specified, use g++
        if [ $pdt_cxx_compiler = default ] ; then
            pdt_cxx_full_path=/usr/bin/g++
            pdt_cxx_compiler=/usr/bin/g++
        fi
        ;;

    craycnl)
        if [ $cxx_compiler_specified = no ]; then
          cxx_compiler=CC
        fi
        if [ $c_compiler_specified = no ]; then
          c_compiler=cc
        fi
        default_fortran=pgi
        if [ "x$CRAY_CPU_TARGET" = "xknc" ] ; then
            fixmakeargs="$fixmakeargs CRAY_MIC"
            tauoptions="${tauoptions}-mic"
            craymic=yes
        fi

        if [ "x$PE_ENV" = xPGI ] ; then
            #tauoptions="${tauoptions}-pgi"
            pgi=yes
            default_fortran=pgi
            fixmakeargs="$fixmakeargs PGI COMPINST_PGI"
        elif [ "x$PE_ENV" = xNVIDIA ] ; then
            tauoptions="${tauoptions}-nvidia"
            cray_nvhpc=yes
            default_fortran=nvfortran
            fixmakeargs="$fixmakeargs NV_FORTRAN"
        elif [ "x$PE_ENV" = xAOCC ] ; then
            tauoptions="${tauoptions}-aocc"
            aocc=yes
            default_fortran=flang
            fixmakeargs="$fixmakeargs AOCC COMPINST_LLVM"
        elif [ "x$PE_ENV" = xAMD ] ; then
            tauoptions="${tauoptions}-amd"
            aocc=yes
            default_fortran=flang
            fixmakeargs="$fixmakeargs AOCC COMPINST_LLVM"
        elif [ "x$PE_ENV" = xGNU -a "x$CLANG_HOME" != "x" ]; then
            llvm=yes
            tauoptions="${tauoptions}-llvm"
            default_fortran=gfortran
            fixmakeargs="$fixmakeargs COMPINST_LLVM"
        elif [ "x$PE_ENV" = xGNU ] ; then
            gnu=yes
            tauoptions="${tauoptions}-gnu"
            default_fortran=gfortran
            fixmakeargs="$fixmakeargs COMPINST_GNU"
        elif [ "x$PE_ENV" = xPATHSCALE ] ; then
            tauoptions="${tauoptions}-pathscale"
            default_fortran=pathscale
            pathscale=yes
            fixmakeargs="$fixmakeargs COMPINST_GNU USE_PATHCC"
        elif [ "x$PE_ENV" = xINTEL ] ; then
            intel=yes
            tauoptions="${tauoptions}-intel"
            default_fortran=intel
            fixmakeargs="$fixmakeargs COMPINST_INTEL9"
        elif [ "x$PE_ENV" = xALLINEA ] ; then
            intel=yes
            tauoptions="${tauoptions}-allinea"
            default_fortran=gfortran
            fixmakeargs="$fixmakeargs CLANG"
        elif [ "x$PE_ENV" = xCRAY ] ; then
            tauoptions="${tauoptions}-cray"
            default_fortran=cray
            if [ $dmapp = no -a "x$CRAY_DMAPP_INCLUDE_OPTS" != "x" ]; then
                fixmakeargs="$fixmakeargs DMAPP"
            fi
            if [ $ugni = no -a "x$CRAY_DMAPP_INCLUDE_OPTS" != "x" ]; then
                fixmakeargs="$fixmakeargs UGNI"
                tauoptions="${tauoptions}-ugni"
                tauoptions_has_ugni=yes
            fi

            # *CWL* - if bfd path is not explicitly specified, point
            #         to the correct preset bfd path.
            if [ "x$bfd" = "xno" ] ; then
                if [ "x$bfddir" = "x" ] ; then
                    bfddir="$CRAY_BINUTILS_ROOT"
                    # Do not try to enable bfd implicitly if we cannot
                    #    find a preset path for Cray compilers. Instead
                    #    disable implicit support for BFD. Cray compilers
                    #    currently have a problem with its library search
                    #    path in that it is different when the -shared
                    #    flag is used. As a result, the -lbfd link flags
                    #    previously discovered at configure time fail
                    #    at build time.
                    if [ "x$bfddir" != "x" ] ; then
                        bfd=yes
                    fi
                fi
            fi

            # *CWL* - this is a horrid hack, but the CCE compilers on
            #         the Cray XE6 behave differently when building
            #         shared objects (see above) and it is not happy
            #         with -lmpichcxx which is static. -lmpichcxx_cray
            #         has to be used instead.
            if [ "x$PE_MVAPICH2_FIXED_PRGENV" = "x" ]; then
              if [ -r $mpiinc/cray_version.h ]; then
                fixmakeargs="$fixmakeargs CRAYCNL_CCE"
              fi
            else
              if [ "x$CRAY_MPICH_PREFIX" = "x" ]; then
                fixmakeargs="$fixmakeargs CRAYCNL_MVAPICH2"
              fi
            fi

            touch conf_test.c
            crayarchdir=`uname -m`
            extradir=`CC -v conf_test.c 2>&1 | grep cray-c++ | sed -e 's/ /\n/g' | grep "/opt/cray/pe/cce" | grep "$crayarchdir/lib$" | uniq | grep -v rpath`
            #echo "EXTRADIR=$extradir"
            fixmakeargs="$fixmakeargs extradir=$extradir"

            rm -f conf_test.c conf_test.o

            # not implemented yet
            if [ "x$CRAY_PE_USE_CLANG" = "x" ]; then
              fixmakeargs="$fixmakeargs COMPINST_CRAYCC"
            else
              if [ -r $mpiinc/cray_version.h ]; then
                fixmakeargs="$fixmakeargs COMPINST_CRAYPE_CLANG"
              fi
            fi
            extradir=`echo $CRAY_PE_USE_CLANG | sed -e 's@bin/clang@lib@'`
            srun_dir=`which srun 2>/dev/null`
            if [ "x$srun_dir" != "x" ]; then
              slurm_dir=`echo $srun_dir | sed -e 's@bin/srun@@g' `
              if [ "x$slurm_dir" != "x" -a -d $slurm_dir -a -r $slurm_dir/lib64/libslurm.so ]; then
                echo "TAU: Found $slurm_dir/lib64/libslurm.so"
                fixmakeargs="$fixmakeargs slurmdir=$slurm_dir CRAYPE_CLANG_SLURM"
              fi
            fi
        fi
        if [ $fortran_compiler = no ] ; then
            fortran_compiler=$default_fortran
        fi

        # unless specified, use g++
        if [ $pdt_cxx_compiler = default ] ; then
            # DO NOT SPECIFY THE PATH! If the user has compiled
            # pdt with a different g++ than the one in /usr/bin,
            # problems will happen. TRUST that /usr/bin is in the
            # user's path, and if not, they don't want that g++.
            pdt_cxx_full_path=g++
            pdt_cxx_compiler=g++
        fi
        ;;
    hitachi)
        if [ $cxx_compiler = default ]
        then
            cxx_compiler=KCC
            c_compiler=cc
        fi

        if [ $fortran_compiler = no ]
        then
            fixmakeargs="$fixmakeargs HITACHI_FORTRAN"
            fortran_compiler=yes
        fi
        ;;

    sgi4k | sgi8k | sgin32 | sgi64)
        if [ $cxx_compiler = default ]
        then
            # SGI has no 64 bit C++ compiler except "CC", so if we are an
            # R8K, we have to use "CC"
            if [ $machine = sgi8k -o $machine = sgin32 -o $machine = sgi64 ]
            then
                cxx_compiler=CC
                c_compiler=cc
            else
                # We are an SGI R4K-based machine, use NCC if available
                if [ -f /bin/NCC ]
                then
                    cxx_compiler=NCC
                    c_compiler=cc
                else
                    cxx_compiler=g++
                fi
            fi
        fi
        if [ $c_compiler = default ]
        then
            c_compiler=gcc
        fi
        ;;

    *)
        # If the user didn't specify a compiler, assume GCC.
        # However, if they did specify -mpi, then assume
        # MPI compilers of mpicc and mpicxx.
        # That way, we can use the -show option to get all
        # the correct MPI flags.
        if [ $cxx_compiler = default ] ; then
            if [ $mpi = yes ] ; then
                cxx_compiler=mpicxx
            else
                cxx_compiler=g++
            fi
            if [ $fortran_compiler = no ] ; then
                fortran_compiler=gfortran
            fi
        fi

        if [ $c_compiler = default ] ; then
            if [ $mpi = yes ] ; then
                c_compiler=mpicc
            else
                c_compiler=gcc
            fi
        fi
        ;;

esac

if [ "x$fortran_compiler" = "xarmflang" ]; then
   fixmakeargs="$fixmakeargs ARMFLANG"
fi

if [ "x$fortran_compiler" = "xnvfortran" ]; then
   fixmakeargs="$fixmakeargs NV_FORTRAN"
fi

if [ "x$fortran_compiler" = "xxlflang" ]; then
   fixmakeargs="$fixmakeargs XLFLANG"
fi

if [ "x$fortran_compiler" = "xflang" -o "x$fortran_compiler" = "xf18" -o "x$fortran_compiler" = "xmdflang" -o "-x$fortran_compiler" = "xflang-new" ]; then
   fixmakeargs="$fixmakeargs FLANG"
   touch foo_compinst.f
   test_mx_flang=`$fortran_compiler -c foo_compinst.f -Mx,129,0x800   2>/dev/null`
   if [ $? -eq 1 ]; then
     # New flang doesn't support  -Mx,129,0x800 for compiler-based instrumentation
     fixmakeargs="$fixmakeargs FLANG_NO_MX_OPTS"
   fi
   /bin/rm -f foo_compinst.f
fi

if [ "x$fortran_compiler" = "xno" ]; then
    echo "Not configuring for fortran."
    fixmakeargs="$fixmakeargs NOFORTRAN"
fi

if [ $cxx_compiler = clang++ -o $cxx_compiler = bgclang++ -o $cxx_compiler = hcc  -o $cxx_compiler = hipcc  -o $cxx_compiler = parascc ]; then
    fixmakeargs="$fixmakeargs CLANG"
fi

if [ $c_compiler = hcc ]; then
    fixmakeargs="$fixmakeargs HCC"
fi


if [ "$machine" = "apple" -o "$machine" = "arm64_apple" ] ; then
    mkdir -p $machine/lib
    cp tools/src/common/resources/tau-medium.png $machine/lib/
    compiler_name=`which $cxx_compiler | awk '{print $1;}'`
    if [ "$compiler_name" = "no" -o "$compiler_name" = "" ]; then
        echo "NOTE: No compilers found, please use option '-c++=<compilername>' if you have a C++ compiler"
        architecture=$machine
        make_directories
        configure_java
        configure_perfexplorer
        exit 0
    fi
fi

if [ "$machine" = "arm64_apple" ]; then
  fixmakeargs="$fixmakeargs ARM64_APPLE"
fi




if [ $machine = craycnl -o $machine = bgq -o $machine = bgp -o $machine = bgl ] ; then
    config_compiler=`which $cxx_compiler`
    comp_minus_darshan=`echo $config_compiler | sed -e 's/darshan//g'`
    darshan_module_loaded=`env | grep LOADEDMODULES | grep darshan | wc -l`


    if [ $config_compiler = $comp_minus_darshan -a $darshan_module_loaded -eq 0 ] ; then
        echo "NOTE: Darshan test successful"
    else
        echo "NOTE: --------------------------------------------------------------"
        echo "NOTE: Darshan may not be used with TAU. Please use module unload darshan and re-configure TAU."
        echo "Darshan's wrapper libraries will clash with TAU wrappers. Exiting..."
        echo "NOTE: --------------------------------------------------------------"
        exit 1;
    fi
fi

if [ $machine = craycnl ] ; then

    cat <<EOF > conftest_shared.c
void foo() {
}
EOF
    $echo "Checking for CrayCNL shared library compatibility... ${nnl}"
    shared_opt="-shared -fPIC"

    if $c_compiler $orig_useropt $shared_opt -o conftest_shared.so conftest_shared.c -fPIC 1> /dev/null 2>&1 ; then
        echo "yes"
        fixmakeargs="$fixmakeargs CRAYCNL_SHARED"
    else
        echo "no"
        fixmakeargs="$fixmakeargs NOSHARED"
    fi

    /bin/rm -f conftest_shared.o conftest_shared.c conftest_shared.so
fi

######################################################################
# For template instantiations in the library -ptused for CC compilers
if [ $machine = sgi8k -o $machine = sgin32 -o $machine = sgi64 ]
then
    if [ $cxx_compiler = CC ]
    then
        fixmakeargs="$fixmakeargs SGICC"
        if [ $pdt = yes -a $pdt_cxx_compiler = default ]
        then
             fixmakeargs="$fixmakeargs PDTSGICC"
        fi
    fi
fi

if [ $machine = hitachi ]
then
    fixmakeargs="$fixmakeargs HITACHI"
fi

if [ $machine = t3e -a $cxx_compiler = CC ]
then
    fixmakeargs="$fixmakeargs CRAYCC"
fi

if [ $machine = crayx1 -o $machine = craysv1 -a $cxx_compiler = CC ]
then
    fixmakeargs="$fixmakeargs CRAYX1CC"
fi

######################################################################
# Set default Profiling Options in Makefiles
if [ $profile = no -a $trace = no -a $mpitrace = no  -a $machine != default ] ; then
    profile=yes
fi

if [ $profile = yes ] ; then
    fixmakeargs="$fixmakeargs PROFILE"
fi

if [ $java = yes ] ; then
    tauoptions="${tauoptions}-jdk"
fi

if [ $profilestats = yes ] ; then
    fixmakeargs="$fixmakeargs PROFILESTATS"
fi

if [ $profilememory = yes ] ; then
    fixmakeargs="$fixmakeargs PROFILEMEMORY"
    tauoptions="${tauoptions}-memory"
fi

if [ $profileheadroom = yes ] ; then
    fixmakeargs="$fixmakeargs PROFILEHEADROOM"
    tauoptions="${tauoptions}-headroom"
fi

if [ $tag = yes ] ; then
    tauoptions="${tauoptions}-${tautag}"
fi

if [ $callpath = yes ] ; then
    fixmakeargs="$fixmakeargs PROFILECALLPATH"
    tauoptions="${tauoptions}-callpath"
fi

if [ $profileparam = yes ] ; then
    fixmakeargs="$fixmakeargs PROFILEPARAM"
    tauoptions="${tauoptions}-param"
fi

if [ $profilepaths = yes ] ; then
    fixmakeargs="$fixmakeargs PROFILEPATHS"
    tauoptions="${tauoptions}-paths"
fi

if [ $profilecommunicators = yes ] ; then
    fixmakeargs="$fixmakeargs PROFILECOMMUNICATORS PROFILEPARAM"
    tauoptions="${tauoptions}-communicators"
fi

if [ $depthlimit = yes ] ; then
    fixmakeargs="$fixmakeargs DEPTHLIMIT"
    tauoptions="${tauoptions}-depthlimit"
fi

if [ $phase = yes ] ; then
    fixmakeargs="$fixmakeargs PROFILECALLPATH PROFILEPHASE"
    tauoptions="${tauoptions}-phase"
    if [ $callpath = yes ] ; then
        echo "ERROR: Incompatible options specified."
        echo "You must choose between -PROFILECALLPATH and -PROFILEPHASE. You cannot choose both!!"
        exit 1
    fi
fi

if [ $setnode0 = yes -a $trace = no ] ; then
    fixmakeargs="$fixmakeargs SETNODE0"
    tauoptions="${tauoptions}-setnode"
fi

if [ $comm = no ] ; then
    fixmakeargs="$fixmakeargs NOCOMM"
    tauoptions="${tauoptions}-nocomm"
fi

if [ $iowrapper = yes ] ; then
    if [ $machine = rs6000 -o $machine = ibm64 ] ; then
        echo "WARNING!! Architecture does not support iowrapper"
    else
        fixmakeargs="$fixmakeargs IOWRAPPER"
    fi
fi

if [ "x$PE_ENV" = "xCRAY" -o $dmapp = yes ]; then
    if [ "x$CRAY_DMAPP_INCLUDE_OPTS" = "x" ] ; then
        echo "TAU: Please load the dmapp module on Cray to set CRAY_DMAPP_INCLUDE_OPTS environment variable and reconfigure TAU with the -dmapp configure option"
    else
        fixmakeargs="$fixmakeargs DMAPP"
    fi
fi

if [ "x$PE_ENV" = "xCRAY" -o $ugni = yes ]; then
    if [ "x$CRAY_UGNI_INCLUDE_OPTS" = "x" ] ; then
        echo "TAU: Please load the dmapp module on Cray to set CRAY_DMAPP_INCLUDE_OPTS environment variable and reconfigure TAU with the -dmapp configure option"
    else
        if [ $tauoptions_has_ugni = no ]; then
            fixmakeargs="$fixmakeargs UGNI"
            tauoptions="${tauoptions}-ugni"
        fi
    fi
fi

if [ $chapel = yes ] ; then
    fixmakeargs="$fixmakeargs CHAPEL"
    tauoptions="${tauoptions}-chapel"
fi

if [ $ittnotify = yes ] ; then
    fixmakeargs="$fixmakeargs ITTNOTIFY"
    tauoptions="${tauoptions}-ittnotify"
fi

if [ $julia = yes ] ; then
    fixmakeargs="$fixmakeargs JULIA"
    tauoptions="${tauoptions}-julia"
fi

if [ $gptl = yes ] ; then
    fixmakeargs="$fixmakeargs GPTL"
    tauoptions="${tauoptions}-gptl"
    if [ $gptl_e3sm = yes ] ; then
        fixmakeargs="$fixmakeargs E3SM"
    fi
fi

if [ $stff = yes ] ; then
    fixmakeargs="$fixmakeargs RENCI_STFF stffdir=${stffdir} sddfdir=${sddfdir}"
    tauoptions="${tauoptions}-stff"
    if [ $sddf = no -o "x$sddfdir" = "x" ]; then
        echo "Error: Must specify -sddf=/path/to/sddflib along with -stff=/path/to/stfflib!"
    fi
fi


if [ $trace = yes ] ; then
    cat <<EOF > tau_type_test.c
  #include <include/Profile/tau_types.h>

  int main(int argc, char **argv) {
    if (sizeof(x_int8) != 1 ||
        sizeof(x_int16) != 2 ||
        sizeof(x_int32) != 4 ||
        sizeof(x_int64) != 8) {
        return -1;
    }
    return 0;
  }
EOF

    if [ $catamount = no -a ! $machine = bgl -a ! $machine = bgp -a ! $machine = bgq -a ! $machine = mips -a ! $machine = mips32 ] ; then
        $echo "Checking type sizes... ${nnl}"
        if $c_compiler -I. $orig_useropt tau_type_test.c -o tau_type_test 1> /dev/null 2>&1 ; then
            if ./tau_type_test 1> /dev/null 2>&1 ; then
                echo "ok"
            else
                echo ""
                echo ""
                echo "Warning: couldn't find correct type sizes!"
                echo "Please contact tau-bugs@cs.uoregon.edu with your system details"
                echo ""
            fi
        else
            echo ""
            echo ""
            echo "Warning: couldn't find correct type sizes!"
            echo "Please contact tau-bugs@cs.uoregon.edu with your system details"
            echo ""
        fi
        rm -f tau_type_test.c tau_type_test
    fi

    fixmakeargs="$fixmakeargs TRACE"
fi

if [ $mpitrace = yes ] ; then
    fixmakeargs="$fixmakeargs MPITRACE"
fi


if [ $alphatimers = yes ] ; then
    fixmakeargs="$fixmakeargs ALPHATIMERS"
    tauoptions="${tauoptions}-alphatimers"
fi


if [ $bgltimers = yes ] ; then
    fixmakeargs="$fixmakeargs BGLTIMERS"
    tauoptions="${tauoptions}-bgltimers"
fi

if [ $bgptimers = yes ] ; then
    fixmakeargs="$fixmakeargs BGPTIMERS"
    tauoptions="${tauoptions}-bgptimers"
fi

if [ $bgqtimers = yes ] ; then
    fixmakeargs="$fixmakeargs BGQTIMERS"
    tauoptions="${tauoptions}-bgqtimers"
fi

if [ $craytimers = yes ] ; then
    fixmakeargs="$fixmakeargs CRAYTIMERS"
    tauoptions="${tauoptions}-craytimers"
fi

if [ $cputime = yes ] ; then
    fixmakeargs="$fixmakeargs CPUTIME"
fi


if [ $machine = sgi8k -o $machine = sgin32 -o $machine = sgi64 ] ; then
    if [ $cxx_compiler = g++ ] ; then
        fixmakeargs="$fixmakeargs SGIGNU"
    fi
fi

if [ $machine = freebsd ] ; then
    fixmakeargs="$fixmakeargs FREEBSD"
fi

if [ $machine = mic_linux ] ; then
    fixmakeargs="$fixmakeargs MIC_LINUX"
    orig_useropt="-xMIC-AVX512 -O2 "
fi

if [ $machine = apple -o $machine = arm64_apple ] ; then
    if [[ "$cxx_compiler" == g++* || "$cxx_compiler" == clang++ || "$cxx_compiler" == c++  || "$cxx_compiler" == icpc  || "$cxx_compiler" == icpx ]] ; then
        fixmakeargs="$fixmakeargs APPLECXX"
    fi
    if [ $cxx_compiler = pgCC -o $cxx_compiler = pgcpp ] ; then
        fixmakeargs="$fixmakeargs APPLEPGI"
    fi
    if [ $fortran_compiler = ibm -o $fortran_compiler = ibm64 ] ; then
        fixmakeargs="$fixmakeargs IBMXLFAPPLE"
        extradir=`which xlf90 | sed s/xlf90/../`
        fortran_compiler=yes
    fi
fi

if [ $machine = ppc64 -o $machine = bgl -o $machine = bgp -o $machine = bgq ] ; then
    if [ $cxx_compiler = g++ ] ; then
        if [ $fortran_compiler = no ] ; then
            extradir=`which xlf90 | sed s/xlf90/../`
            fixmakeargs="$fixmakeargs IBM_FORTRAN"
        fi
    fi
fi

if [ $machine = ibm64 -o $machine = ibm64linux ] ; then
    if [ $cxx_compiler = g++ -o $cxx_compiler = powerpc64-linux-g++ ] ; then
        if [ $fortran_compiler = no ] ; then
            extradir=`which xlf90 | sed s/xlf90/../`
            fixmakeargs="$fixmakeargs IBM64_FORTRAN"
        fi
    fi
fi

if [ $machine = nec-sx-aurora ]; then
    if [ $cxx_compiler = nc++ -o $cxx_compiler = mpinc++ ]; then
        nec_extradirs=`which nfort | sed s@bin/nfort@@`nfort/*/lib
        for i in $nec_extradirs
        do
             if [ -r $i/libnfort.a ]; then
                  extradir=$i
             fi
        done
        if [ $fortran_compiler = mpinfort ]; then
             fixmakeargs="$fixmakeargs NEC_SX_MPI_FORTRAN"
        fi
    fi

    mpiinc_search=`which mpincc  | sed -e 's@//@/@g'| sed -e 's/\/bin\/mpincc$//' -e 's/\/bin64\/mpincc$//'`/include
    if [ -d $mpiinc_search -a -r $mpiinc_search/mpi.h -a "x$mpiinc" = "x" ]; then
      echo "Checking $mpiinc_search/../lib/ve"
      if [ $mpi = yes -a -d $mpiinc_search/../lib/ve -a -r $mpiinc_search/../lib/ve/libmpi.a ]; then
        mpilib=`echo $mpiinc_search | sed -e 's@include@lib/ve@'`
        echo "Using fixed MPI lib directory $mpilib"
        fixmakeargs="$fixmakeargs mpilib=$mpilib"
      fi
    fi
fi

if [ $cxx_compiler = x86_64-w64-mingw32-g++ ]; then
    fixmakeargs="$fixmakeargs TAU_WINDOWS MINGW GNU"
fi

if [ $machine = t3e -a $cxx_compiler = KCC ] ; then
    fixmakeargs="$fixmakeargs CRAYKAI"
    if [ $fortran_compiler = no ] ; then
        fixmakeargs="$fixmakeargs CRAY_FORTRAN"
        fortran_compiler=yes
    fi
fi

if [ $machine = crayx1 -o $machine = craysv1 ] ; then
    if [ $fortran_compiler = no ] ; then
        fixmakeargs="$fixmakeargs CRAY_X1_FORTRAN"
        fortran_compiler=yes
    fi
fi


if [ $debugprof = yes ] ; then
    fixmakeargs="$fixmakeargs DEBUGPROF"
fi

if [ $syscall = yes ] ; then
    # if [ $machine = something ] ; then
    #     echo "WARNING!! Architecture does not support ptrace for syscall"
    # else
        fixmakeargs="$fixmakeargs SYSCALL"
    # fi
fi

####################################################################
# FIRST TRY TO CONFIGURE MPI!
# Check to see if mpicxx is in the user's path, and if so, use it to
# auto-detect the MPI settings and the compiler to use.
####################################################################

# mpicxx may change the compilers!
orig_c_compiler=$c_compiler
orig_cxx_compiler=$cxx_compiler
orig_fortran_compiler=$fortran_compiler

# If Intel is detected, check for mpiicpc not mpicxx
mpicxx_exe=mpicxx
mpicc_exe=mpicc
mpif90_exe=mpif90
intel_cxx=`which mpiicpc 2>/dev/null | wc -l`
if [ $intel_cxx = 1 ] ; then
    echo "detected Intel in path"
    if [ "x$cxx_compiler" = "xicpc" -o "x$cxx_compiler" = "xmpiicpc" ] ; then
        echo "using Intel MPI wrappers (with Intel compilers)"
        mpicxx_exe=mpiicpc
        mpicc_exe=mpiicc
        mpif90_exe=mpiifort
    elif [ "x$cxx_compiler" = "xicpx" -o "x$cxx_compiler" = "xmpiicpx" ] ; then
        echo "using Intel OneAPI MPI wrappers (with Intel OneAPI compilers)"
        mpicxx_exe=mpiicpx
        mpicc_exe=mpiicx
        mpif90_exe=mpiifx

    else
        echo "WARNING: Intel compilers found in your path, but C++ compiler not specified.  Whatever is detected in mpicxx will be used as the compiler.  To force Intel compilers, please pass -c++=icpc, -c++=icpx, or -c++=mpiicpc"
        issue_intel_warning=yes
    fi
fi
case "x$cxx_compiler" in
  "xmpicxx")
    mpi_wrapper_mpicxx_or_mpiicpc=yes ;;
  "xmpic++")
    mpi_wrapper_mpicxx_or_mpiicpc=yes
    mpicxx_exe=mpic++ ;;
  "xmpiicpc")
    mpi_wrapper_mpicxx_or_mpiicpc=yes ;;
  "xicpc")
    mpi_wrapper_mpicxx_or_mpiicpc=yes ;;
  "xicpx")
    mpi_wrapper_mpicxx_or_mpiicpc=yes ;;
  "xxlC"*)
    mpi_wrapper_mpicxx_or_mpiicpc=yes ;;
  *)
    mpi_wrapper_mpicxx_or_mpiicpc=no ;;
esac

# Check for regular ol' mpicxx
any_cxx=`which mpicxx 2>/dev/null | wc -l`
if [ $any_cxx = 1 ] ; then
    echo "detected MPI in path"
    mpi_wrapper_mpicxx_or_mpiicpc=yes
fi

# DEbugging...
#echo "mpi_wrapper_mpicxx_or_mpiicpc = ($mpi_wrapper_mpicxx_or_mpiicpc)"
#echo "mpi = ($mpi)"
#echo "mpiinc = ($mpiinc)"
#kill -9 $$

get_compilers_if_necessary() {
    # don't overwrite user's choice
    if [ "x$c_compiler" == "x" ] ; then
        c_compiler=`$mpicc_exe -show | awk '{ print $1;}' | sed -e 's@/.*/@@g'`
    fi
    if [ "x$cxx_compiler" == "x" ] ; then
        cxx_compiler=`$mpicxx_exe -show | awk '{ print $1;}' | sed -e 's@/.*/@@g'`
    fi
    if [ $f90loaded = 1 ] ; then
        echo "NOTE: Found $mpif90_exe in the path...trying to auto-configure compilers and flags..."
        if [ "x$fortran_compiler" == "x" ] ; then
            fortran_compiler=`$mpif90_exe -show | awk '{ print $1;}' | sed -e 's@/.*/@@g'`
        fi
    fi
}

if [ $mpi = yes -a "x$mpiinc" = "x" -a "x$mpi_wrapper_mpicxx_or_mpiicpc" = "xyes" ] ; then
    cxxloaded=`which $mpicxx_exe 2>/dev/null | wc -l`
    mpi=yes
    if [ $cxxloaded = 1 ] ; then
        echo "NOTE: Found $mpicxx_exe in the path...trying to auto-configure compilers and flags..."
        f90loaded=`which $mpif90_exe 2>/dev/null | wc -l`
        get_compilers_if_necessary # see above
        if [ $f90loaded = 1 ] ; then
            echo "NOTE: detected $fortran_compiler"
            if [ "x$fortran_compiler" = "xifort" -o "x$fortran_compiler" = "xifx" ]; then
                fortran_compiler=intel
            fi
            if [ "x$fortran_compiler" = "xxlf_r" -o "x$fortran_compiler" = "xxlf90_r"  -o "x$fortran_compiler" = "xxlf90" -o "x$fortran_compiler" = "xxlf2008_r" ]; then
                fixmakeargs="$fixmakeargs IBM64LINUX_XLF"
            fi
            if [ "x$fortran_compiler" = "xxlflang" ]; then
                fixmakeargs="$fixmakeargs XLFLANG"
            fi
            if [ "x$fortran_compiler" = "xarmflang" ]; then
                fixmakeargs="$fixmakeargs ARMFLANG"
            fi
            if [ "x$fortran_compiler" = "xflang" -o "x$fortran_compiler" = "xf18" -o "x$fortran_compiler" = "xmdflang" -o "x$fortran_compiler" = "xflang-new" ]; then
                fixmakeargs="$fixmakeargs FLANG"
                touch foo_compinst.f
                test_mx_flang=`$fortran_compiler -c foo_compinst.f -Mx,129,0x800   2>/dev/null`
                if [ $? -eq 1 ]; then
                # New flang doesn't support  -Mx,129,0x800 for compiler-based instrumentation
                  fixmakeargs="$fixmakeargs FLANG_NO_MX_OPTS"
                fi
                /bin/rm -f foo_compinst.f
            fi
            if [ "x$orig_fortran_compiler" = "xcaf" ]; then
                fortran_compiler=caf
            fi
            if [ "x$orig_fortran_compiler" = "xpgfortran" ]; then
                fortran_compiler=pgfortran
            fi
        else
            echo "Default fortran compiler was '$orig_fortran_compiler'..."
            if [ $f90loaded = 1 ] ; then
                fortran_compiler=`$mpif90_exe -show | awk '{ print $1;}' | sed -e 's@/.*/@@g'`
            else
                if [ "x$orig_fortran_compiler" = "xdefault" ]; then
                  fortran_compiler=no
                fi
            fi
            echo "Setting fortran compiler to '$fortran_compiler'..."
        fi

        if [ $machine = bgq -o $machine = bgp ]; then
            c_compiler=mpicc
            cxx_compiler=mpicxx
            is_gnu_compiler=`$cxx_compiler -show  2>/dev/null | awk ' { print $1;}' | xargs grep g++ | wc -l`
            if [ $is_gnu_compiler = 1 ]; then
              gnu=yes
              tauoptions="${tauoptions}-gnu"
            fi
        else
            # All other machines besides IBM BG*
            is_internal_compiler_loaded=`which $cxx_compiler 2>/dev/null | wc -l`
            if [ $is_internal_compiler_loaded = 0 ]; then
                # Compiler is not loaded, use full path to it
                c_compiler=`$mpicc_exe -show | awk '{ print $1;}' `
                cxx_compiler=`$mpicxx_exe -show | awk '{ print $1;}' `
                fortran_compiler=`$mpif90_exe -show | awk '{ print $1;}' `
            fi
            if [ "x$mpilibrary" = "xno" -a "x$scorep" = "xno" ]; then
                #mpiinc=`$mpicxx_exe -show | cut -d' ' -f2- | sed -e 's@ @#@g'`
                #mpilibargs=`$mpicxx_exe -show | cut -d' ' -f2- | sed -e 's@ @#@g'`
                #mpiflibargs=`$mpif90_exe -show | cut -d' ' -f2- | sed -e 's@ @#@g' -e 's@-qthreaded@@g'`
                if [ "x$machine" == "xcraycnl" -a "x$orig_cxx_compiler" = "xCC" ] ; then
                    my_mpicxx_exe=CC
                    my_mpif90_exe=ftn
                    mpishow_option="--cray-print-opts=all"
                    yet_another_option="-I$CRAY_MPICH_PREFIX/include#"
                else
                    my_mpicxx_exe=$mpicxx_exe
                    my_mpif90_exe=$mpif90_exe
                    mpishow_option="-show"
                    yet_another_option=""
                fi
                get_cflags $my_mpicxx_exe $mpishow_option
                get_ldflags $my_mpicxx_exe $mpishow_option
                mpiinc="${yet_another_option}${_got_cflags}"
                mpilibargs=$_got_ldflags
                # We have to make sure we set the mpilibrary variable here, because later
                # mpilib might get reset.
                mpilibrary=$_got_ldflags
                get_ldflags $my_mpif90_exe $mpishow_option
                mpiflibargs=`echo $_got_ldflags | sed  -e 's@-qthreaded@@g' -e 's@-fallow-argument-mismatch@@g'`
                echo "Found $mpicxx_exe cflags: $mpiinc"
                echo "Found $mpicxx_exe ldflags: $mpilibargs"
                echo "Found $mpif90_exe ldflags: $mpiflibargs"

                tmp_mpilibargs="-L$tautoplevel/$machine/lib#-lTauMpi\$(TAU_CONFIG)#$mpilibargs"
                tmp_mpiflibargs="-g#-L$tautoplevel/$machine/lib#-lTauMpi\$(TAU_CONFIG)#$mpiflibargs"
                fixmakeargs="$fixmakeargs mpilibargs=$tmp_mpilibargs mpiflibargs=$tmp_mpiflibargs mpiincargs=$mpiinc"
                mpilibargs_added_to_fixmakeargs=yes
            fi
            full_cxx_compiler=`$mpicxx_exe -show | tail -n 1 | awk '{ print $1; }'`
            full_cc_compiler=`$mpicc_exe -show | tail -n 1 | awk '{ print $1; }'`
            fixmakeargs="$fixmakeargs fullcxx=$full_cxx_compiler fullcc=$full_cc_compiler"
            mpi=yes
        fi
        # Do not use the internal compiler from mpicxx when BGP/BGQ is used.
    else
        echo "NOTE: Didn't find mpicxx in the path, using default values"
        gnu=yes
        tauoptions="${tauoptions}-gnu"
        if [ $machine = bgq ] ; then
            if [ -r /bgsys/drivers/ppcfloor/comm/gcc/bin/mpicxx -a -r /bgsys/drivers/ppcfloor/comm/gcc/bin/mpicc ] ; then
                echo "NOTE: Using /bgsys/drivers/ppcfloor/comm/gcc/bin/mpicxx"
                cxx_compiler=/bgsys/drivers/ppcfloor/comm/gcc/bin/mpicxx
                c_compiler=/bgsys/drivers/ppcfloor/comm/gcc/bin/mpicc
            else
                echo "ERROR: Please set your path to include mpicxx and reconfigure TAU."
                exit 1
            fi
        elif [ $machine = bgp ]; then
            if [ -r /soft/apps/gcc-4.3.2/comm/default/bin/mpicxx -a -r /soft/apps/gcc-4.3.2/comm/default/bin/mpicc ]; then
                echo "NOTE: Using /soft/apps/gcc-4.3.2/comm/default/bin/mpicxx"
                cxx_compiler=/soft/apps/gcc-4.3.2/comm/default/bin/mpicxx
                c_compiler=/soft/apps/gcc-4.3.2/comm/default/bin/mpicc
            else
                echo "ERROR: Please set your path to include mpicxx and reconfigure TAU."
                exit 1
            fi
        elif [ "x$machine" == "xcraycnl" ] ; then
            if [ "x$mpilibrary" = "xno" -a "x$scorep" = "xno" -a "x$orig_cxx_compiler" = "xCC" ]; then
                get_cray_mpiflags
            fi
        fi
    fi
    # Check for BG/P or BG/Q for GNU
    #    is_gnu_compiler=`$cxx_compiler -show  2>/dev/null | awk ' { print $1;}' | xargs grep g++ | wc -l'
    #    if [ "x$is_gnu_compiler" = "x1" ]; then
    #      gnu=yes
    #      if [ $machine = bgp ]; then
    #          fixmakeargs="$fixmakeargs GNU BGP_GFORTRAN COMPINST_GNU"
    #          tauoptions="${tauoptions}-gnu"
    #      elif [ $machine = bgq ]; then
    #          fixmakeargs="$fixmakeargs GNU BGQ_GFORTRAN COMPINST_GNU GNU46PLUS"
    #          NOTE: FOUND GNU configuration"
    #          tauoptions="${tauoptions}-gnu"
    #      fi
    #    fi
elif [ $mpi = yes -a "x$mpiinc" = "x" ] ; then
    if [ "x$machine" == "xcraycnl" ] ; then
        if [ "x$mpilibrary" = "xno" -a "x$scorep" = "xno" -a "x$orig_cxx_compiler" = "xCC" ]; then
            get_cray_mpiflags
        fi
    fi
fi

######################################################################
# Set default C++ compiler in all Makefiles
case $cxx_compiler in
    CC)
        echo "Default C++ compiler will be CC"
        fixmakeargs="$fixmakeargs USE_CFRONT"
	$cxx_compiler --version | grep -q clang
	isclang=$?
	if [ $isclang = 0 ]; then
	  if [ $openmp = yes ]; then
            fixmakeargs="$fixmakeargs CLANG_OPENMP"
          fi
	  #llvm=yes
	  #build_llvm_plugin=yes
	  #fixmakeargs="$fixmakeargs COMPINST_CRAYPE_CLANG"
	  fixmakeargs="$fixmakeargs FLANG_NO_MX_OPTS" #COMPINST_LLVM"  #CLANG
	fi
        ;;
    *llvm*|*clang* | hcc | hipcc | parascc )
        echo "Default C++ compiler will be clang++ (or variant)"
        llvm=yes
        fixmakeargs="$fixmakeargs CLANG COMPINST_LLVM"
        tauoptions="${tauoptions}-clang"
        if [ $machine = apple -o $machine = arm64_apple ]; then
            gnu=yes
            fixmakeargs="$fixmakeargs taugcclibdir=/usr/lib taugccstdcxxlibdir=/usr/lib"
            fixmakeargs="$fixmakeargs APPLECXX"
        else
        # We currently do not support building the TAU LLVM plugin for macOS. This may change in the future.
            build_llvm_plugin=yes
        fi
        if [ $openmp = yes ]; then
          fixmakeargs="$fixmakeargs CLANG_OPENMP"
        fi
        ;;

    *g++)
        echo "Default C++ compiler will be " \
            `$cxx_compiler -v 2>&1 | tail -1`
        gnu=yes
        # No fixmakeargs needed because it is the default
        ;;

    g++-5)
        echo "Default C++ compiler will be " \
            `gcc-5 -v 2>&1 | tail -1 | sed 's/gcc-5/g++-5/g'`
        gnu=yes
        fixmakeargs="$fixmakeargs c_compiler=gcc-5 cxx_compiler=g++-5 GNU"
        # No fixmakeargs needed because it is the default
        ;;


    g++-6)
        echo "Default C++ compiler will be " \
            `gcc-6 -v 2>&1 | tail -1 | sed 's/gcc-6/g++-6/g'`
        gnu=yes
        fixmakeargs="$fixmakeargs c_compiler=gcc-6 cxx_compiler=g++-6 GNU"
        # No fixmakeargs needed because it is the default
        ;;

    g++-7)
        echo "Default C++ compiler will be " \
            `gcc-7 -v 2>&1 | tail -1 | sed 's/gcc-7/g++-7/g'`
        gnu=yes
        fixmakeargs="$fixmakeargs c_compiler=gcc-7 cxx_compiler=g++-7 GNU"
        # No fixmakeargs needed because it is the default
        ;;

    g++-8)
        echo "Default C++ compiler will be " \
            `gcc-8 -v 2>&1 | tail -1 | sed 's/gcc-8/g++-8/g'`
        gnu=yes
        fixmakeargs="$fixmakeargs c_compiler=gcc-8 cxx_compiler=g++-8 GNU"
        # No fixmakeargs needed because it is the default
        ;;

    *-g++|g++-*)
        echo "Default C++ compiler will be " \
            `$cxx_compiler -v 2>&1 | tail -1 | sed 's/gcc/g++/g'`
        #cxx_compiler=g++
        gnu=yes
        fixmakeargs="$fixmakeargs c_compiler=$c_compiler cxx_compiler=$cxx_compiler GNU"
        ;;

    g++4)
        echo "Default C++ compiler will be " \
            `gcc4 -v 2>&1 | tail -1 | sed 's/gcc4/g++4/g'`
        gnu=yes
        # No fixmakeargs needed because it is the default
        ;;


    egcs)
        echo "Default C++ compiler will be " \
            `egcs -v 2>&1 | tail -1 | sed 's/gcc/egcs/g'`
        # No fixmakeargs needed because it is the default
        ;;

    cxx)
        echo "Default C++ compiler will be the HP Tru64 cxx C++ compiler"
        fixmakeargs="$fixmakeargs USE_DECCXX"
        if [ $fortran_compiler = no ]
        then
            f90loaded=`which f90 | sed -e 's/f90/../g' | grep "^no"`
            if [ "x$f90loaded" = "x" ]
            then
                    # f90 module has been loaded and which f90 returns a path
                extradir="`which f90 | sed s/f90/../`"
                echo "Found f90 in `which f90`"
            fi
            fixmakeargs="$fixmakeargs COMPAQ_FORTRAN"
            fortran_compiler=yes
        fi
        ;;

    *xl*)
        echo "Default C++ compiler will be IBMs xlC C++ compiler"
        fixmakeargs="$fixmakeargs USE_IBMXLC COMPINST_XL"
        if [ "$machine" = "apple" ]
        then
            fixmakeargs="$fixmakeargs IBMXLCAPPLE"
            if [ $fortran_compiler = no ]
            then
                fixmakeargs="$fixmakeargs IBMXLFAPPLE"
            fi
        fi
        ibmxlc=yes

        if [ "$machine" = "bgp" -o "$machine" = "bgq" ] ; then
            ibm_compiler=gxlc++
        else
           if [ "$machine" = ibm64linux ] ; then
            ibm_compiler=$c_compiler
           else
            ibm_compiler=$cxx_compiler
           fi
        fi
        compilerLocation=`which $ibm_compiler`

        if [ "x$compilerLocation" = "x/usr/bin/$ibm_compiler" -o "x$compilerLocation" = "x/bin/$ibm_compiler" ] ; then
            # if xlc or blrts_xlc is found in /usr/bin, it is likely a
            # symlink.  Since we use ../lib to locate the libraries
            # we will dereference the symlink once.  Don't do it more
            # than once, or you'll end up with the wrong thing

            # check for readlink first
            readlink=`which readlink`
            if [ "x$readlink" != "x" ] ; then
                if [ -x "$readlink" ] ; then
                    if [ -h $compilerLocation ] ; then
                        compilerLocation=`readlink -f $compilerLocation`
                    fi
                fi
            fi
        fi
        if [ "x$compilerLocation" = "x/usr/local/tools/check_compile_line/bgxlc++" ] ; then
            compilerLocation=`grep bgxlc++ /usr/local/tools/check_compile_line/bgxlc++ | awk '{ print $3;}'`
        fi


        extradircxx=`echo $compilerLocation | sed -e 's/$ibm_compiler/../' -e 's@bin/xlc$@bin/..@'`

        if [ "$machine" = ibm64linux ] ; then
           if [ ! -r $extradircxx/lib/libibmc++.a ]; then
              cxxpath=`find $extradircxx -name libibmc++.a -print | tail -1`
              extradircxx=`echo $cxxpath | sed -e 's@lib/libibmc++.a@@g' -e 's@alllibs/libibmc++.a@@g'`
           fi
        fi

        extradir=`which xlf90 | sed s/xlf90/../`
        if [ $fortran_compiler = no ]
        then
            if [ "$machine" = "ibm64" -o "$machine" = "ibm64linux" ]
            then
                fixmakeargs="$fixmakeargs IBM64_FORTRAN"
            else
                fixmakeargs="$fixmakeargs IBM_FORTRAN"
            fi
            fortran_compiler=yes
        fi
        threadtest=`echo $ibm_compiler | sed -e 's/_r//'`
        if [ "y$threadtest" = "y$ibm_compiler" ]
        then
            echo "Not using the thread-safe version of the IBM compiler"
        else
            ibmxlc_r=yes
            fixmakeargs="$fixmakeargs THREADSAFE_COMPILERS"
            echo "Using thread-safe version of the IBM compiler"
            threadsafe=yes
        fi
        ;;

    NCC)
        echo "Default C++ compiler will be SGI's Delta C++ compiler"
        fixmakeargs="$fixmakeargs USE_SGINCC"
        ;;

    KCC)
        echo "Default C++ compiler will be KAI KCC C++ Compiler"
        kai=yes
        fixmakeargs="$fixmakeargs KAI"
        ;;

    mcxx)
        echo "Default C++ compiler will be Mercurium C++ Compiler"
        gnu=yes
        shared_opt="-shared -fPIC"
        fixmakeargs="$fixmakeargs MERCURIUM"
        ;;

    pgc++|pgCC|pgcpp)
        echo "Default C++ compiler will be PGI pgCC C++ Compiler"
        pgi=yes
        pgiflag=`pgcc -help | head -1 | grep 1.7 | wc -l`

        fixmakeargs="$fixmakeargs COMPINST_PGI"

        if [ $pgiflag = 1 ]
        then
            echo  "Using PGI ver 1.7 Compiler"
            fixmakeargs="$fixmakeargs PGI PGI1.7"
        else
            fixmakeargs="$fixmakeargs PGI PGICC"
        fi

        if [ "x$fortran_compiler" = "xno" ]
        then
            extradir=`which pgcc | sed s/pgcc/../`
            fixmakeargs="$fixmakeargs PGI_FORTRAN"
            fortran_compiler=yes
        fi
        ;;

    nvc++)
        echo "Default C++ compiler will be NVIDIA nvc++ C++ Compiler"
        nvhpc=yes
        pgi=yes

        fixmakeargs="$fixmakeargs COMPINST_PGI PGINOPRELINK"

        fixmakeargs="$fixmakeargs PGI PGICC"

        if [ "x$fortran_compiler" = "xno" ]
        then
            extradir=`which nvc | sed s/nvc/../`
            fixmakeargs="$fixmakeargs PGI_FORTRAN"
            fortran_compiler=yes
        fi
        ;;

    nvcc)
        echo "Default C++ compiler will be NVIDIA nvcc C++ Compiler"
        nvhpc=yes
        pgi=yes
	nvidia=yes
        extradir=`which nvc | sed s/nvc/../`

        fixmakeargs="$fixmakeargs COMPINST_PGI PGINOPRELINK"
        fixmakeargs="$fixmakeargs NVIDIA PGICC"

        ;;



    FCC|mpiFCCpx|mpiFCC)
        echo "Default C++ compiler will be Fujitsu C++ Compiler"
        fujitsu=yes
        orig_useropt="-O2"
        useropt="-O2"
        # -g interferes with optimizations for Fujitsu.
        if [ $machine = arm64_linux ]; then
          fixmakeargs="$fixmakeargs FX_AARCH64"
          fx_aarch64=yes
        else
          fixmakeargs="$fixmakeargs FX"
        fi
        #mpiinc=`which mpiFCCpx | sed -e 's/\/bin\/mpiFCCpx$//'`/include
        #mpiinc=`which mpiFCCpx | sed -e 's/\/bin\/mpiFCCpx$//'`/include/mpi/fujitsu
        mpiinc=`which $cxx_compiler | sed -e "s@/bin/$cxx_compiler@@"`/include/mpi/fujitsu
        if [ ! -d $mpiinc -a -r /opt/FJSVfxlang/1.2.0/include/mpi/fujitsu/mpi.h ]
        then
            mpiinc=/opt/FJSVfxlang/1.2.0/include/mpi/fujitsu/
        fi

        if [ -d $mpiinc ]
        then
            echo "Using MPI include directory $mpiinc"
        fi
        fixmakeargs="$fixmakeargs mpiincargs=-I$mpiinc"
        #echo "Adding -I$mpiinc to MPI include"
        if [ $fortran_compiler = no -o $fortran_compiler = mpifrtpx -o $fortran_compiler = fujitsu ]
        then
            extradir=`which mpiFCCpx | sed s/mpiFCCpx/../`
            #fixmakeargs="$fixmakeargs FUJITSU_FORTRAN"
            fortran_compiler=yes
        fi
        if [ $machine = solaris2 -o $machine = solaris2-64 ]
        then
            extradir=`which FCC | sed s/FCC/../`
            fixmakeargs="$fixmakeargs FUJITSU_SOLARIS"
            fortran_compiler=yes
        fi
        ;;

    guidec++)
        echo "Default C++ compiler will be KAI KAP/Pro OpenMP guidec++ Compiler"
        kai=yes
        fixmakeargs="$fixmakeargs KAI GUIDE OPENMP"
        openmp=yes
        tauoptions="${tauoptions}-guide"
        if [ $fortran_compiler = no ]
        then
            fixmakeargs="$fixmakeargs KAI_FORTRAN"
            guidef90=yes
            fortran_compiler=yes
        fi

        ;;

    aCC)
        echo "Default C++ compiler will be HP aCC Compiler"
        fixmakeargs="$fixmakeargs ACC"
        tauoptions="${tauoptions}-acc"
        if [ $fortran_compiler = no ]
        then
            fixmakeargs="$fixmakeargs HP_FORTRAN"
            fortran_compiler=yes
        fi

        ;;
    openCC|orCC)
        echo "Default C++ compiler will ORC Open64 orCC compiler"
        open64=yes
        fixmakeargs="$fixmakeargs OPEN64ORC"
        tauoptions="${tauoptions}-open64"
        if [ $c_compiler = default ]
        then
            c_compiler=orcc
        fi

        if [ $fortran_compiler = no ]
        then
            fixmakeargs="$fixmakeargs OPEN64ORC_FORTRAN"
            fortran_compiler=yes
        fi

        if [ $openmp = yes ]
        then
            fixmakeargs="$fixmakeargs OPEN64_OPENMP"
        fi

        ;;

    c++)
        if [ $machine = nec ]
        then
            echo "Default C++ compiler will be NEC c++ compiler"
            fixmakeargs="$fixmakeargs USE_NECCXX"
            tauoptions="${tauoptions}-cxx"
            if [ $fortran_compiler = no ]
            then
                extradir=`which f90 | sed s/f90/../`
                fixmakeargs="$fixmakeargs NEC_FORTRAN"
                fortran_compiler=yes
            fi
        else
            if [ $machine = crayxmt ]
            then
                echo "Default C++ compiler will be Cray c++ compiler"
            else
                echo "Default C++ compiler will be Apple c++ compiler"
                fixmakeargs="$fixmakeargs APPLECXX"
                tauoptions="${tauoptions}-cxx"
            fi
        fi

        ;;

    ecpc)
        echo "Default C++ compiler will be Intel ecpc C++ compiler"
        fixmakeargs="$fixmakeargs USE_INTELCXX"
        tauoptions="${tauoptions}-ecpc"
        intel=yes
        if [ $fortran_compiler = no ]
        then
            fixmakeargs="$fixmakeargs INTEL_FORTRAN"
            fortran_compiler=intel
            extradir=`which efc | sed s/efc/../`
        fi

        if [ $intelcxxlibicc = yes ]
        then
            fixmakeargs="$fixmakeargs INTELCXXLIBICC"
        fi

        ;;

    icpc)
        echo "Default C++ compiler will be Intel icpc C++ compiler"
        fixmakeargs="$fixmakeargs USE_INTELCXX COMPINST_INTEL9"
        tauoptions="${tauoptions}-icpc"
        intel=yes
        if [ $fortran_compiler = no ]
        then
            intelifort=`which ifort 2>/dev/null`
            if [ "y$intelifort" != "y"  -a -x "$intelifort" ] ; then
                fixmakeargs="$fixmakeargs INTEL32_FORTRAN"
                fortran_compiler=intel
                extradir=`which ifort | sed -e 's/\/bin\/ifort$//'`
            fi
        fi
        if [ $intelcxxlibicc = yes ]
        then
            fixmakeargs="$fixmakeargs INTELCXXLIBICC"
        fi

        ;;
    icpx)
        echo "Default C++ compiler will be Intel icpx C++ compiler"
        fixmakeargs="$fixmakeargs USE_INTELCXX COMPINST_INTEL9"
        tauoptions="${tauoptions}-icpx"
        intel=yes
        inteloneapi=yes
        if [ $fortran_compiler = no ]
        then
            intelifort=`which ifx 2>/dev/null`
            if [ "y$intelifort" != "y"  -a -x "$intelifort" ] ; then
                fixmakeargs="$fixmakeargs INTEL_IFX"
                fortran_compiler=intel
                extradir=`which ifx | sed -e 's/\/bin\/ifx$//'`
            fi
        fi
        if [ $intelcxxlibicc = yes ]
        then
            fixmakeargs="$fixmakeargs INTELCXXLIBICC"
        fi

        ;;
    mpiicpc)
        echo "Default C++ compiler will be Intel mpiicpc C++ compiler"
        fixmakeargs="$fixmakeargs MPIICPC USE_INTELCXX"
        tauoptions="${tauoptions}-icpc"
        intel=yes
        if [ $fortran_compiler = no  -o $fortran_compiler = mpiifort -o $fortran_compiler = intel ]
        then
            intelifort=`which mpiifort 2>/dev/null`
            if [ "y$intelifort" != "y"  -a -x "$intelifort" ] ; then
                fortran_compiler=intel
                extradir=`which mpiifort | sed -e 's/\/bin\/mpiifort$//'`
                fixmakeargs="$fixmakeargs MPIIFORT"
            fi
        fi
        ;;
    mpiicpx)
        echo "Default C++ compiler will be Intel mpiicpx C++ compiler"
        fixmakeargs="$fixmakeargs MPIICPC USE_INTELCXX COMPINST_INTEL9"
        tauoptions="${tauoptions}-icpx"
        intel=yes
        inteloneapi=yes
        if [ $fortran_compiler = no  -o $fortran_compiler = mpiifort -o $fortran_compiler = intel ]
        then
            intelifort=`which mpiifort 2>/dev/null`
            if [ "y$intelifort" != "y"  -a -x "$intelifort" ] ; then
                fortran_compiler=intel
                extradir=`which mpiifort | sed -e 's/\/bin\/mpiifort$//'`
                fixmakeargs="$fixmakeargs MPIIFORT"
            fi
        fi
	if [ $fortran_compiler = mpiifx ] ; then
            intelmifx=`which mpiifx 2>/dev/null`
	    echo $intelmifx
            if [ "y$intelmifx" != "y"  -a -x "$y$intelmifx" ] ; then
                fortran_compiler=intel
                extradir=`which mpiifx | sed -e 's/\/bin\/mpiifort$//'`
                fixmakeargs="$fixmakeargs INTEL_MPIIFX"
            fi
        fi
        ;;
    pathCC|scpathCC)
        echo "Default C++ compiler will be PathScale pathCC C++ compiler"
        fixmakeargs="$fixmakeargs USE_PATHCC COMPINST_GNU"
        tauoptions="${tauoptions}-pathcc"
        pathscale=yes
        if [ $fortran_compiler = no ]
        then
            fixmakeargs="$fixmakeargs PATHSCALE_FORTRAN"
            fortran_compiler=pathscale
        fi

        ;;

    qk-pgCC)
        echo "Default C++ compiler will be PGI pgCC C++ Compiler"
        pgi=yes
        fixmakeargs="$fixmakeargs PGI PGICC COMPINST_PGI"
        fixmakeargs="$fixmakeargs PGINOPRELINK"

        if [ $fortran_compiler = no ]
        then
            extradir=`which pgcc | sed s/pgcc/../`
            fixmakeargs="$fixmakeargs PGI_FORTRAN PGI_CATAMOUNT"
            fortran_compiler=yes
        fi
        ;;

    mpc_cxx)
        if [ $machine = x86_64 -o $machine = mic_linux -o $machine = arm64_linux ] ; then
            #mpc_include=`mpc_cxx -show | grep '-I'`
            #mpc_include=`mpc_cxx -show | grep -o '[^ ]-I[^ ]*'`
            mpc_include=`mpc_cxx -show | grep -o '[^ ]*include/mpcframework[^ ]*'`
            full_cxx_compiler=`mpc_cxx -show | awk '{print $1;}'`
            full_cc_compiler=`mpc_cc -show | awk '{ print $1;}'`
            fixmakeargs="tau_build_cc=$full_cc_compiler tau_build_cxx=$full_cxx_compiler $fixmakeargs"
            #mpilibrary=`mpc_cxx -show | sed -e "s@$full_cxx_compiler@@g" | sed -e "s/ /#/g" -e "s/''//g"`
            mpilibrary=`mpc_cxx -show | sed -e "s@$full_cxx_compiler@@g" | sed -e "s/ /#/g" -e "s/''//g"`
            #full_cxx_compiler=`mpc_cxx -show -Dmain=mpc_user_main__ | sed -e "s@-fmpc-privatize@@g" -e "s/ /#/g" -e "s/''//g"`
            #full_cc_compiler=`mpc_cc -show -Dmain=mpc_user_main__ | sed -e "s@-fmpc-privatize@@g" -e "s/ /#/g" -e "s/''//g" `
            #echo "CC=$cc_compiler"
            #echo "CXX=$cxx_compiler"
            #echo "mpilibrary=$mpilibrary"
            tauoptions="${tauoptions}-mpc"
            fixmakeargs="$fixmakeargs MPC"
            mpc=yes
            mpi=yes
        fi
        ;;

    mpc_icpc)
    if [ $machine = x86_64 -o $machine = mic_linux ] ; then
            full_cxx_compiler=`mpc_icpc -show | awk '{print $1;}'`
            full_cc_compiler=`mpc_icc -show | awk '{ print $1;}'`
            fixmakeargs="tau_build_cc=$full_cc_compiler tau_build_cxx=$full_cxx_compiler $fixmakeargs"
            mpilibrary=`mpc_icpc -show | sed -e "s@$full_cxx_compiler@@g" | sed -e "s/ /#/g"`
            full_cxx_compiler=`mpc_icpc -show -Dmain=mpc_user_main__ | sed -e "s@-mSYMTAB_mpc_privatize @@g" -e "s/ /#/g"`
            full_cc_compiler=`mpc_icc -show -Dmain=mpc_user_main__ | sed -e "s@-mSYMTAB_mpc_privatize @@g" -e "s/ /#/g"`
        #echo "CC=$cc_compiler"
        #echo "CXX=$cxx_compiler"
        #echo "mpilibrary=$mpilibrary"
        tauoptions="${tauoptions}-mpc-intel"
            fixmakeargs="$fixmakeargs MPC MPCINTEL"
            mpc=yes
        fi
        ;;

    mpc_icpx)
    if [ $machine = x86_64 -o $machine = mic_linux ] ; then
            full_cxx_compiler=`mpc_icpx -show | awk '{print $1;}'`
            full_cc_compiler=`mpc_icc -show | awk '{ print $1;}'`
            fixmakeargs="tau_build_cc=$full_cc_compiler tau_build_cxx=$full_cxx_compiler $fixmakeargs"
            mpilibrary=`mpc_icpx -show | sed -e "s@$full_cxx_compiler@@g" | sed -e "s/ /#/g"`
            full_cxx_compiler=`mpc_icpx -show -Dmain=mpc_user_main__ | sed -e "s@-mSYMTAB_mpc_privatize @@g" -e "s/ /#/g"`
            full_cc_compiler=`mpc_icc -show -Dmain=mpc_user_main__ | sed -e "s@-mSYMTAB_mpc_privatize @@g" -e "s/ /#/g"`
        #echo "CC=$cc_compiler"
        #echo "CXX=$cxx_compiler"
        #echo "mpilibrary=$mpilibrary"
        tauoptions="${tauoptions}-mpc-intel"
            fixmakeargs="$fixmakeargs MPC MPCINTEL"
            mpc=yes
        fi
        ;;

    oshc++ | oshCC | oshcxx )
        echo "Using OpenSHMEM C++ compiler"
        fixmakeargs="$fixmakeargs GNU COMPINST_GNU GNU46PLUS"
        fixmakeargs="$fixmakeargs OPENSHMEM"
        gcclibdir=`oshcc  -print-libgcc-file-name | sed -e 's,[^/]*$,,'`
        stdcxxlibdir=`oshcc  -print-file-name=libstdc++.a | sed -e 's,[^/]*$,,'`
        fixmakeargs="$fixmakeargs taugcclibdir=$gcclibdir taugccstdcxxlibdir=$stdcxxlibdir"
        ;;

    mpinc++ | nc++)
        if [ "$tauarch" = "unknown" -a "$machine" = "x86_64" ]; then
          echo "NOTE: Using GNU compilers for NEC VH. Please specify -arch=nec-sx-aurora if you wish to use VE."
          c_compiler=gcc
          cxx_compiler=g++
          fixmakeargs="$fixmakeargs GNU COMPINST_GNU GNU46PLUS TAU_NEC_MPI_VH_SX"
          if [ "x$mpilib" = "x" ]; then
            mpilib_search=`mpincc -vh -show | sed -e 's@ @\n@g' | grep "^-L" | sed -e 's@-L@@'`
            mpilib=$mpilib_search
            echo "NOTE: Using NEC VH mode for x86_64 (as -arch=nec-sx-aurora was not specified) with mpilib=$mpilib"
            echo "-------------------------------------------------------------------------------------------------"
          fi
        else
          tauoptions="${tauoptions}-ncc"
          echo "Using NEC Compilers"
          fixmakeargs="$fixmakeargs NEC_COMPILERS"
        fi
        ;;

    *mpicxx | *mpic++)
        internal_compiler=`$cxx_compiler -show | awk '{ print $1;}'  | awk -F'/' '{ print $NF}'`
	internal_c_compiler=`$c_compiler -show | awk '{ print $1;}' | awk -F'/' '{ print $NF}'`
        echo "Detected MPI C++ internal compiler = $internal_compiler MPI C internal compiler = $internal_c_compiler"
        case $internal_compiler in
            *icpc | *icpx )
                fixmakeargs="$fixmakeargs USE_INTELCXX"
                intel=yes
                inteloneapi=yes
                ;;
            *clang* | hcc | hipcc | parascc )
                tauoptions="${tauoptions}-clang"
                llvm=yes
                echo "Using CLANG Compilers"
                fixmakeargs="$fixmakeargs CLANG"
                if [ $machine = apple -o $machine = arm64_apple ]; then
                    # MPI on Apple OS X has issues with shared objects
                    fixmakeargs="$fixmakeargs NOSHARED"
                else
                # We currently do not support building the TAU LLVM plugin for macOS. This may change in the future.
                    build_llvm_plugin=yes
                fi
                ;;
            *bgclang++)
                tauoptions="${tauoptions}-clang"
                if [ $machine = bgq ] ; then
                    fixmakeargs="$fixmakeargs CLANG BGCLANG"
                fi
                ;;
            *g++* | c++)
                if [ $machine = bgq ] ; then
                    echo "Using GNU Compilers for BGQ"
                    fixmakeargs="$fixmakeargs GNU BGQ_GFORTRAN COMPINST_GNU GNU46PLUS"
                elif [ $machine = bgp ] ; then
                    echo "Using GNU Compilers for BGP"
                    fixmakeargs="$fixmakeargs GNU BGP_GFORTRAN COMPINST_GNU"
                else
                    fixmakeargs="$fixmakeargs GNU COMPINST_GNU"
                fi
                gnu=yes
                ;;
            *xlC*)
                echo "Using IBM Compilers"
                ibmxlc=yes
                fixmakeargs="$fixmakeargs USE_IBMXLC COMPINST_XL"
                extradir=`which xlf90 | sed s/xlf90/../`
                      fixmakeargs="$fixmakeargs IBM_FORTRAN"

                ;;
            nvc++)
              echo "Default C++ compiler will be NVIDIA nvc++ C++ Compiler"
              nvhpc=yes
              pgi=yes
              fortran_compiler=nvfortran
              extradir=`which nvc | sed s/nvc/../`
              fortran_compiler=yes

              fixmakeargs="$fixmakeargs PGI_FORTRAN NV_FORTRAN"
              fixmakeargs="$fixmakeargs COMPINST_PGI PGINOPRELINK"
              fixmakeargs="$fixmakeargs PGI PGICC"
	      ;;

            nvcc)
              echo "Default C++ compiler will be NVIDIA nvcc C++ Compiler"
              nvhpc=yes
              pgi=yes
              extradir=`which nvc | sed s/nvc/../`

              fixmakeargs="$fixmakeargs COMPINST_PGI PGINOPRELINK"
              fixmakeargs="$fixmakeargs PGI PGICC"
              ;;

            *)
                echo "ERROR Unknown C++ compiler in mpicxx/mpic++: $internal_compiler"
                ;;
        esac
        ;;
    *)
        echo "ERROR Unknown C++ compiler: $cxx_compiler"
        exit 1
        ;;

esac

# Should we build the TAU selective instrumentation plugin for LLVM?
if [ $build_llvm_plugin = yes ]; then
  check_cmake 3 14 "LLVM plugin"
  result=$?
  if [ $result -eq 0 ]; then
    fixmakeargs="$fixmakeargs tau_cmake=$cmake_cmd TAU_BUILD_LLVM_PLUGIN"
  # proceed with building the LLVM plugin. But check if the compiler or the LLVM source directories have been specified
    theclang=`command -v clang`
    theamdclang=`command -v clang`
    thegcc=`command -v clang`
    if [ "x$llvm_src_dir" = "x" ]; then
      if [ "x$theclang" != "x" ] ; then
        llvm_src_dir=`which clang | sed -e 's@bin/clang@@' `
        echo "TAU: Using LLVM source directory -llvm_src=$llvm_src_dir for building the TAU LLVM plugin."
        fixmakeargs="$fixmakeargs llvm_src_dir=$llvm_src_dir LLVM_SRC_DIR"
      fi
    fi
    if [ "x$llvm_src_dir" = "x" ]; then
      if [ "x$theamdclang" != "x" ] ; then
        llvm_src_dir=`which amdclang | sed -e 's@bin/amdclang@@' `
        echo "TAU: Using LLVM source directory -llvm_src=$llvm_src_dir for building the TAU LLVM plugin."
        fixmakeargs="$fixmakeargs llvm_src_dir=$llvm_src_dir LLVM_SRC_DIR"
      fi
    fi
    if [ "x$llvm_cxx" = "x" ]; then
      llvm_cxx=`which g++ `
      llvm_cc=`which gcc `
      echo "TAU: Using -llvm_cxx=$llvm_cxx as the compiler to build the TAU LLVM selective instrumentation plugin. "
      echo "TAU: Using -llvm_cc=$llvm_cc as the compiler to build the TAU LLVM selective instrumentation plugin. "
      fixmakeargs="$fixmakeargs llvm_cxx=$llvm_cxx LLVM_CXX"
      fixmakeargs="$fixmakeargs llvm_cc=$llvm_cc LLVM_CC"
    fi
  fi
fi

if [ $mpc = yes ]; then
  plugins=no
fi
######################################################################
# Set default F90 compiler in all Makefiles
echo "Setting F90 compiler based on requested: $fortran_compiler"
case $fortran_compiler in
    gnu)
        echo "Default Fortran compiler will be GNU"
        fixmakeargs="$fixmakeargs GNU_FORTRAN"
        ;;

    gfortran|gfortran-*)
        echo "Default Fortran compiler will be GNU gfortran"
        if [ $cxx_compiler = scg++ ] ; then
            fixmakeargs="$fixmakeargs SC_GFORTRAN"
        else
            gfortran=`which $fortran_compiler 2>&1`
            retval=$?
            # when gfortran is not found, it returns a value of 1, otherwise it returns 0.
            if [ "x$gfortran" != "x" -a "x$retval" != "x1" ] ; then
                if [ -x $gfortran ] ; then
                    taugfortranlibdir=`$fortran_compiler -print-libgcc-file-name | sed s/libgcc.a//`
                fi
                fixmakeargs="$fixmakeargs taugfortranlibdir=$taugfortranlibdir"
                fixmakeargs="$fixmakeargs GNU_GFORTRAN"
            fi
        fi
        if [ $machine = apple ]; then
            fixmakeargs="$fixmakeargs APPLE_GFORTRAN"
        fi
        ;;

    caf)
        echo "Default Fortran compiler will be caf"
        fixmakeargs="$fixmakeargs CAF"
        ;;

    gfortran4)
        echo "Default Fortran compiler will be GNU gfortran4"
        gfortran4=`which gfortran4 2>&1`
        if [ "x$gfortran4" != "x" ] ; then
            if [ -x $gfortran4 ] ; then
                taugfortranlibdir=`gfortran4 -print-libgcc-file-name | sed s/libgcc.a//`
            fi
            fixmakeargs="$fixmakeargs taugfortranlibdir=$taugfortranlibdir"
            fixmakeargs="$fixmakeargs GNU_GFORTRAN4"
        fi
        ;;

    g95)
        echo "Default Fortran compiler will be G95"
        fixmakeargs="$fixmakeargs G95_FORTRAN"
        extradir=`g95 -print-libgcc-file-name | sed s/libgcc.a//`
        echo extra=$extradir
        ;;

    sgi)
        echo "Default Fortran compiler will be SGI f90"
        fixmakeargs="$fixmakeargs SGI_FORTRAN"
        ;;

    ibm)
        echo "Default Fortran compiler will be IBM xlf90"
        extradir=`which xlf90 | sed s/xlf90/../`
        fixmakeargs="$fixmakeargs IBM_FORTRAN"
        ;;

    ibm64)
        echo "Default Fortran compiler will be IBM xlf90 (64 bits)"
        extradir=`which xlf90 | sed s/xlf90/../`
        fixmakeargs="$fixmakeargs IBM64_FORTRAN"
        ;;

    hp)
        echo "Default Fortran compiler will be HP f90"
        extradir=`which f90 | sed s/f90/../`
        fixmakeargs="$fixmakeargs HP_FORTRAN"
        ;;

    cray|ftn)
        echo "Default Fortran compiler will be Cray f90"
        if [ $machine = crayx1 -o $machine = craysv1 ]
        then
            fixmakeargs="$fixmakeargs CRAY_X1_FORTRAN"
        else
            fixmakeargs="$fixmakeargs CRAY_FORTRAN"
        fi
        ;;

    pgi|pgf90)
        echo "Default Fortran compiler will be PGI pgf90"
        pgi=yes
        extradir=`which pgcc | sed s/pgcc/../`
        fixmakeargs="$fixmakeargs PGI_FORTRAN"
        ;;

    nvfortran)
        echo "Default Fortran compiler will be NVIDIA nvfortran"
        pgi=yes
        extradir=`which nvc | sed s/nvc/../`
        fixmakeargs="$fixmakeargs PGI_FORTRAN NV_FORTRAN"
        ;;

    pgfortran)
        echo "Default Fortran compiler will be PGI pgfortran"
        pgi=yes
        extradir=`which pgcc | sed s/pgcc/../`
        fixmakeargs="$fixmakeargs PGI_FORTRAN_OTHER"
        ;;

    absoft)
        echo "Default Fortran compiler will be Absoft f90"
        extradir="`which f90 | sed s/f90/../`"
        fixmakeargs="$fixmakeargs ABSOFT_FORTRAN"
        ;;

    fujitsu)
        echo "Default Fortran compiler will be Fujitsu F90"
        extradir=`which FCC | sed s/FCC/../`
        fixmakeargs="$fixmakeargs FUJITSU_FORTRAN"
        ;;

    nec)
        echo "Default Fortran compiler will be NEC f90"
        extradir=`which f90 | sed s/f90/../`
        fixmakeargs="$fixmakeargs NEC_FORTRAN"
        ;;

    lahey)
        echo "Default Fortran compiler will be Lahey lf95"
        extradir=`which lf95 | sed s/lf95/../`
        if [ -d $extradir/lib64 ]
        then
            fixmakeargs="$fixmakeargs LAHEY64_FORTRAN"
        else
            fixmakeargs="$fixmakeargs LAHEY_FORTRAN"
        fi
        ;;

    nag*)
        echo "Default Fortran compiler will be Nagware nagfor"
        extradir=`which nagfor | sed s@\/nagfor@\/..@`
        fixmakeargs="$fixmakeargs NAGWARE_FORTRAN"
        ;;

    pathscale)
        echo "Default Fortran compiler will be Pathscale pathf95"
        if [ $cxx_compiler = scpathCC -o $cxx_compiler = scg++ ]
        then
            extradir=`which scpathf95 | sed s@\/scpathf95@\/..@`
            fixmakeargs="$fixmakeargs SC_PATHSCALE"
        else
            extradir=`which pathf95 | sed s@\/pathf95@\/..@`
            fixmakeargs="$fixmakeargs PATHSCALE_FORTRAN"
        fi
        ;;

    sun)
        echo "Default Fortran compiler will be SUN f90"
        fixmakeargs="$fixmakeargs SUN_FORTRAN"
        ;;

    compaq)
        echo "Default Fortran compiler will be HP Tru64 f90"
        f90loaded=`which f90 | sed -e 's/f90/../g' | grep "^no"`
        if [ "x$f90loaded" = "x" ]
        then
            # f90 module has been loaded and which f90 returns a path
            extradir="`which f90 | sed s/f90/../`"
            echo "Found f90 in `which f90`"
        fi
        fixmakeargs="$fixmakeargs COMPAQ_FORTRAN"
        ;;

    kai)
        echo "Default Fortran compiler will be KAI guidef90"
        fixmakeargs="$fixmakeargs KAI_FORTRAN"
        guidef90=yes
        ;;

    open64|openf90)
        echo "Default Fortran compiler will be ORC Open64 orf90"
        fixmakeargs="$fixmakeargs OPEN64ORC_FORTRAN"
        ;;

    intel|ifort)
        if [ $cxx_compiler = ecpc ]
        then
            echo "Default Fortran compiler will be Intel efc"
            fixmakeargs="$fixmakeargs INTEL_FORTRAN"
        else
            echo "Default Fortran compiler will be Intel ifort"
            fixmakeargs="$fixmakeargs INTEL32_FORTRAN"
            intelifort=`which ifort 2>/dev/null`
            if [ "y$intelifort" != "y" -a -x "$intelifort" ]
            then
                extradir=`which ifort | sed -e 's/\/bin\/ifort$//'`
            fi
        fi
        ;;
    ifx)
        if [ $cxx_compiler = icpx ]
        then
          echo "Default Fortran compiler will be Intel ifx"
          fixmakeargs="$fixmakeargs INTEL_IFX"
          intelifort=`which ifx 2>/dev/null`
          if [ "y$intelifort" != "y" -a -x "$intelifort" ]
          then
            extradir=`which ifx | sed -e 's/\/bin\/ifx$//'`
          fi
        fi
        ;;
    no)
        ;;

    *)
        ;;

esac

# Encode the full path of the C++/C compiler in the stub Makefile
if [ "x$full_cxx_compiler" = "x" ]
then
    full_cxx_compiler=`which $host_prefix$cxx_compiler`
    full_cc_compiler=`which $host_prefix$c_compiler`
    if [ $fujitsu = yes ]
    then
        # Fujitsu does not want full path to point to the absolute paths due to
        # frequent updates to the compiler on that system.
        full_cxx_compiler=$cxx_compiler
        full_cc_compiler=$c_compiler
    fi
fi
# This takes care of mpc_cxx setting this above
if [ $shmem = yes -a "x$cxx_compiler" = "xdefault" ]
then
    oshcc_var=`which oshcc 2>/dev/null`
    if [ "x$oshcc_var" != "x" -a $? = 0 ]
    then
        full_cxx_compiler=`which oshCC`
        if [ $? = 1 ]; then
          full_cxx_compiler=`which oshc++`
          if [ $? = 1 ]; then
            full_cxx_compiler=`which oshcxx`
          fi
        fi
        full_cc_compiler=`which oshcc`
        gcclibdir=`oshcc  -print-libgcc-file-name | sed -e 's,[^/]*$,,'`
        stdcxxlibdir=`oshcc  -print-file-name=libstdc++.a | sed -e 's,[^/]*$,,'`
        fixmakeargs="$fixmakeargs taugcclibdir=$gcclibdir taugccstdcxxlibdir=$stdcxxlibdir"

    fi
fi


# PGI sanity check for -latomic
if [ $pgi = yes -a -d "$extradir" -a -r "$extradir/libatomic.a" ]; then
   fixmakeargs="$fixmakeargs PGI_ATOMIC"
fi

# PGI sanity check for -lC
if [ $pgi = yes -a -d "$extradir" -a -r "$extradir/libC.a" ]; then
   fixmakeargs="$fixmakeargs PGI_C"
fi

if [ $scorep = yes ]; then
  swap_full_cxx_compiler=$full_cxx_compiler
  full_cxx_compiler=`which $orig_cxx_compiler`
  swap_full_c_compiler=$swap_full_c_compiler
  full_cc_compiler=`which $orig_c_compiler`
fi

full_fortran_compiler=`which $fortran_compiler 2>/dev/null`
if [ $fortran_compiler != $orig_fortran_compiler ]; then
  full_fortran_compiler=`which $orig_fortran_compiler 2>/dev/null`
fi
if [ $fortran_compiler_specified = yes -a "x$full_fortran_compiler" != "x" ]; then
  fixmakeargs="$fixmakeargs fullfortran=$full_fortran_compiler FORTRAN_SPECIFIED"
fi
fixmakeargs="$fixmakeargs fullcxx=$full_cxx_compiler fullcc=$full_cc_compiler"

if [ $scorep = yes ]; then
  full_cxx_compiler=$swap_full_cxx_compiler
  full_cc_compiler=$swap_full_cc_compiler
fi




######################################################################
# Set MPI options. First set the include dir if it is not specified
if [ $mpi = yes -a $machine = ibm64 ]
then
    ibmxlc_r=yes
    echo "Enabling threaded MPI libraries for IBM64"
    if [ $threadsafe = no ]
    then
        fixmakeargs="$fixmakeargs THREADSAFE_COMPILERS"
        threadsafe=yes
    fi
fi

if [ $machine = apple -a $cxx_compiler = icpc ] ; then
  fixmakeargs="$fixmakeargs APPLEINTEL"
fi

if [ $mpi = yes -a "x$mpiinc" = "x" ]
then
    case $machine in
        ppc64|ibm64linux)
            if [ -r /opt/ibmhpc/ppe.poe/include/mpi.h ]
            then
                mpiinc=/opt/ibmhpc/ppe.poe/include
                mpiincf90dir='-I/opt/ibmhpc/ppe.poe/include/thread'
                if [ $machine = ibm64linux ]
                then
                    mpiincf90dir='-I/opt/ibmhpc/ppe.poe/include/thread64'
                fi
                additional_mpiincs="$additional_mpiincs $mpiincf90dir"
            fi

            if [ -r /opt/ibmhpc/ppe.poe/include/ibmmpi/mpi.h ]
            then
                mpiinc=/opt/ibmhpc/ppe.poe/include/ibmmpi
                mpiincf90dir='-I/opt/ibmhpc/ppe.poe/include/ibmmpi/thread'
                if [ $machine = ibm64linux ]
                then
                    mpiincf90dir='-I/opt/ibmhpc/ppe.poe/include/ibmmpi/thread64'
                fi
                additional_mpiincs="$additional_mpiincs $mpiincf90dir"
            fi

            if [ -r /opt/ibmhpc/pecurrent/pempi/include/mpi.h ]
            then
                mpiinc=/opt/ibmhpc/pecurrent/pempi/include
                mpiincf90dir='-I/opt/ibmhpc/pecurrent/pempi/include/thread'
                if [ $machine = ibm64linux ]
                then
                    mpiincf90dir='-I/opt/ibmhpc/pecurrent/pempi/include/thread64'
                fi
                additional_mpiincs="$additional_mpiincs $mpiincf90dir"
            fi

            mpiinc_search=`which mpirun  | sed -e 's@//@/@g'| sed -e 's/\/bin\/mpirun$//' -e 's/\/bin64\/mpcrun$//'`/include
                if [ -d $mpiinc_search -a -r $mpiinc_search/mpi.h -a "x$mpiinc" = "x" ]
                then
                    mpiinc=$mpiinc_search
                    echo "Using MPI include directory $mpiinc"
                    echo "machine is $machine"
                else
                    mpiinc_search=`which mpicc | sed -e 's/\/bin\/mpicc$//' -e 's/\/bin64\/mpicc$//'`/include
                    if [ -d $mpiinc_search -a -r $mpiinc_search/mpi.h -a "x$mpiinc" = "x" ]
                    then
                      mpiinc=$mpiinc_search
                      echo "Using MPI include directory $mpiinc"
                    else
                      mpiinc_search=`which mpicc | sed -e 's/\/bin\/mpicc$//' -e 's/\/bin64\/mpicc$//'`/include64
                      if [ -d $mpiinc_search -a -r $mpiinc_search/mpi.h -a "x$mpiinc" = "x" ]
                      then
                        mpiinc=$mpiinc_search
                        echo "Using MPI include directory $mpiinc"
                        echo "machine is $machine"
                      fi
                    fi
                fi
                if [ "x$mpiinc" = "x" -a "x$mpilib" = "x" ]; then
                  mpiinc_search=`$orig_c_compiler -show | sed -e 's@ @\n@g' | grep "^-I" | sed -e 's@-I@@'`
                  mpilib_search=`$orig_c_compiler -show | sed -e 's@ @\n@g' | grep "^-L" | sed -e 's@-L@@'`
                  if [ -d $mpiinc_search -a -r $mpiinc_search/mpi.h ];
                  then
                    mpiinc=$mpiinc_search
                    echo "Using MPI include directory $mpiinc"
                  fi
                  if [ -d $mpilib_search -a -r $mpilib_search/mpi.mod ]; then
                    mpilib=$mpilib_search
                    echo "Using MPI lib directory $mpilib"
                  fi
                fi
            ;;
        rs6000|ibm64)
            mpiinc=/usr/lpp/ppe.poe/include
            mpiincf90dir='-I/usr/lpp/ppe.poe/include/thread'
            if [ $machine = ibm64 ]
            then
                mpiincf90dir='-I/usr/lpp/ppe.poe/include/thread64'
            fi

            if [ $ibmxlc_r = yes ]
            then
                additional_mpiincs='-I/usr/lpp/ssp/css/include'
            fi
            additional_mpiincs="$additional_mpiincs $mpiincf90dir"
            ;;
        bgl)
            mpiinc=/bgl/BlueLight/ppcfloor/bglsys/include
            ;;
        bgp)
            mpiinc=/bgsys/drivers/ppcfloor/comm/xl/include
            if [ $cxx_compiler = mpicxx ]; then
                mpiinc=/bgsys/drivers/ppcfloor/comm/include
            fi
            ;;
        bgq)
            mpiinc=/bgsys/drivers/ppcfloor/comm/xl/include
            if [ $cxx_compiler = mpicxx ]; then
                mpiinc=/bgsys/drivers/ppcfloor/comm/gcc/include
            fi
            ;;
        t3e|crayx1|craysv1)
            mpiinc=/opt/ctl/mpt/mpt/include
            ;;
        xt3)
            mpiinc=/opt/xt-mpt/default/mpich2-64/P2/include
            ;;
        craycnl)
            #        mpiinc=/opt/xt-mpt/default/mpich2-64/P/include
            if [ "x$CRAY_MPICH2_DIR" = "x" -a "x$CRAY_MPICH_DIR" = "x" ]; then
              cat <<EOF > mpiincl.c
#include <mpi.h>
EOF
              mpiinc=`$c_compiler -E -c mpiincl.c | grep include | grep mpi.h | head -1  | awk '{ print $3;}'  | sed -e 's@"@@g'  | sed -e 's@/mpi.h@@g'`
              /bin/rm -f mpiincl.c
              echo "Found MPI include dir = $mpiinc"
              findmpi=`echo $mpiinc | sed -e 's@include$@lib@'`
              if [ -d $findmpi ]; then
                mpilib=$findmpi
                echo "And found MPI lib in $mpilib"
              else
                findmpi=`echo $mpiinc | sed -e 's@include$@lib64@'`
                if [ -d $findmpi ]; then
                  mpilib=$findmpi
                  echo "And found MPI lib in $mpilib"
                fi
              fi
            else
              mpiinc=$CRAY_MPICH2_DIR/include
            fi
            if [ ! -r $mpiinc ] ; then
              if [ "x$CRAY_MPICH_DIR" != "x" ]; then
                mpiinc=$CRAY_MPICH_DIR/include
              elif [ "x$CRAY_MPICH_PREFIX" != "x" ]; then
                mpiinc=$CRAY_MPICH_PREFIX/include
              fi
            fi
            if [ ! -r $mpiinc ] ; then
                    echo "ERROR: $CRAY_MPICH_DIR/include (CRAY_MPICH(2)_DIR/include) is not readable. Please configure with -mpiinc=<dir> to point to the MPI include directory."
                exit 1
            fi
            echo "Using MPI INCLUDE dir from $mpiinc"
            ;;
        solaris2*)
            mpiinc=/opt/SUNWhpc/include
            ;;
        ppc)
            if [ $zepto = yes ]
            then
                mpiinc=$zeptodir/include
                if [ ! -e $mpiinc ]
                then
                    echo "ERROR: Cannot access Zepto MPI include directory [$mpiinc]. Aborting."
                    exit 1
                fi
                additional_mpiincs="$additional_mpiincs -DUSE_MPI -D__ZCL__ -DZCL_AD_HOC -I$zeptodir/include/ "
            fi
            ;;
        mic_linux)
            mpiinc_search=`which mpirun  | sed -e 's@//@/@g' | sed -e 's@intel64/bin/mpirun$@mic@' -e 's@bin/mpirun$@@' -e 's@bin64/mpirun$@@'`/include
            if [ -d $mpiinc_search -a "x$mpiinc" = "x" ]
            then
                mpiinc=$mpiinc_search
                echo "Using MPI include directory $mpiinc"
            fi
            ;;
        *)
            if [ $mpc = yes ]
            then
                mpiinc=`which mpcrun | sed -e 's/\/bin\/mpcrun$//' -e 's/\/bin64\/mpcrun$//'`/include
              if [ -r $mpiinc/../mpc-libsctk_arch-dev/include/sctk_config_arch.h ]
              then
                echo "NOTE: Found sctk_config_arch.h in $mpiinc/mpc-libsctk_arch-dev/include"
                additional_mpiincs="-I$mpiinc/../mpc-libsctk_arch-dev/include"
              else
                if [ -r $mpiinc/sctk_config_arch.h -a $mpiinc/mpcframework/mpi.h ]
                then
                  echo "NOTE: Found sctk_config_arch.h in $mpiinc/mpc-libsctk_arch-dev/include"
                  additional_mpiincs="-I$mpiinc/mpcframework -I$mpiinc"
                fi
              fi
            else
                mpiinc_search=`which mpirun  | sed -e 's@//@/@g'| sed -e 's/\/bin\/mpirun$//' -e 's/\/bin64\/mpcrun$//'`/include
                if [ -d $mpiinc_search -a -r $mpiinc_search/mpi.h -a "x$mpiinc" = "x" ]
                then
                    mpiinc=$mpiinc_search
                    echo "Using MPI include directory $mpiinc"
                    echo "machine is $machine"
                else
                    mpiinc_search=`which mpiexec | sed -e 's/\/bin\/mpiexec$//' -e 's/\/bin64\/mpiexec$//'`/include
                    if [ -d $mpiinc_search -a -r $mpiinc_search/mpi.h -a "x$mpiinc" = "x" ]
                    then
                        mpiinc=$mpiinc_search
                        echo "Using MPI include directory $mpiinc"
                    else
                        if [ -d $mpiinc_search -a ! -r $mpiinc_search/mpi.h ] ; then
                            if [ -r $mpiinc_search/mpich2/mpi.h -a "x$mpiinc" = "x" ] ; then
                                mpiinc=$mpiinc_search/mpich2
                                echo "Correction: Using MPI include directory $mpiinc instead"
                            else
                                if [ -r $mpiinc_search/openmpi/mpi.h -a "x$mpiinc" = "x" ] ; then
                                    mpiinc=$mpiinc_search/openmpi
                                    echo "Correction: Using MPI include directory $mpiinc instead"
                                else
                                    mpiinc=""
                                fi
                            fi
                        else
                            mpiinc=""
                        fi
                    fi
                fi
                if [ ! -r $mpiinc/mpi.h -a -r $mpiinc/mpich2/mpi.h ] ; then
                    mpiinc=$mpiinc/mpich2
                    echo "Correction: Using MPI include directory $mpiinc instead"
                fi
            fi
            ;;
    esac
fi

# Add threaded include dir to the MPI include dir for IBM
if [ "x$mpiinc" != "x" ]
then
    case $machine in
        rs6000)
            if [ -d /usr/lpp/ppe.poe/include/thread ]
            then
                mpiincf90dir='-I/usr/lpp/ppe.poe/include/thread'
            fi
            if [ $ibmxlc_r = yes ]
            then
                additional_mpiincs='-I/usr/lpp/ssp/css/include'
            fi
            additional_mpiincs="$additional_mpiincs $mpiincf90dir"
            ;;
        ibm64)
            if [ -d /usr/lpp/ppe.poe/include/thread64 ]
            then
                mpiincf90dir='-I/usr/lpp/ppe.poe/include/thread64'
            fi
            if [ $ibmxlc_r = yes ]
            then
                additional_mpiincs='-I/usr/lpp/ssp/css/include'
            fi
            additional_mpiincs="$additional_mpiincs $mpiincf90dir"
            ;;
        *)
            ;;
    esac
fi


if [ $mpi = yes -a "x$mpilib" = "x" ]
then
    case $machine in
        ppc64)
            mpilib=no
            if [ -r /opt/ibmhpc/ppe.poe/lib/libmpi/libmpi_ibm.so ]
            then
                mpilib=/opt/ibmhpc/ppe.poe/lib/libmpi
            fi
            if [ -r /opt/ibmhpc/pecurrent/ppe.poe/lib/libmpi/libmpi_ibm.so ]
            then
                mpilib=/opt/ibmhpc/pecurrent/ppe.poe/lib/libmpi
            fi
            if [ "x$mpilib" != "xno" ]; then
                additional_mpilibs='-lpoe -llapi'
                mpilibrary="-lmpi_ibm $additional_mpilibs"
            fi
            ;;
        ibm64linux_deprecated)
# This logic no longer applies - use `which mpirun` to find mpilib directory.
            mpilib=no
            if [ -r /opt/ibmhpc/ppe.poe/lib/libmpi64/libmpi_ibm.so ]
            then
                mpilib=/opt/ibmhpc/ppe.poe/lib/libmpi64
                additional_mpilibs='-lpoe -llapi'
                mpilibrary="-lmpi_ibm $additional_mpilibs"
            fi

            if [ -r /opt/ibmhpc/ppe.poe/lib64/libmpi_ibm.so ]
            then
                mpilib=/opt/ibmhpc/ppe.poe/lib64/
                additional_mpilibs='-lpoe -llapi'
            fi
            if [ -r /opt/ibmhpc/pecurrent/pempi/gnu/lib/libmpi64/libmpi_ibm.so -a -r /opt/ibmhpc/pecurrent/base/gnu/lib64/libpami.so ]
            then
                mpilib=/opt/ibmhpc/pecurrent/pempi/gnu/lib/libmpi64
                additional_mpilibs='-lpoe -L/opt/ibmhpc/pecurrent/base/gnu/lib64 -lpami'
            fi

            if [ "x$mpilib" != "xno" ]; then
                mpilibrary="-lmpi_ibm $additional_mpilibs"
                echo "Adding MPI libs from $mpilib"
                # Cases
                if [ -r $mpilib/libmpi_ibm.so ]; then
                    echo "NOTE: IBM MPI found!"
                    if [ -f $mpilib/libmpif90.a -o -f $mpilib/libmpif90.so ] ; then
                        echo "Fortran libmpif90.* found"
                        mpiflibargs="-L$mpilib -lmpif90 "
                    fi
                fi
            fi
            ;;

        rs6000|ibm64)
            mpilib=/usr/lpp/ppe.poe/lib
            if [ $ibmxlc_r = yes ]
            then
                additional_mpilibs='-L/usr/lpp/ppe.poe/lib/threads -L/usr/lpp/ssp/css/lib -llapi_r'
            fi
            ;;
        bgl)
            mpilib=/bgl/BlueLight/ppcfloor/bglsys/lib
            additional_mpilibs='-lmsglayer.rts -ldevices.rts -lrts.rts -ldevices.rts'
            ;;
        bgp)
            mpilib=/bgsys/drivers/ppcfloor/comm/xl/lib
            if [ $cxx_compiler = mpicxx ]; then
                mpilib=/bgsys/drivers/ppcfloor/comm/lib
            fi
            additional_mpilibs='-ldcmfcoll.cnk -ldcmf.cnk -lpthread -L/bgsys/drivers/ppcfloor/runtime/SPI -lSPI.cna'
            ;;
        bgq)
            mpilib=/bgsys/drivers/ppcfloor/comm/xl/lib
            if [ $cxx_compiler = mpicxx ]; then
                mpilib=/bgsys/drivers/ppcfloor/comm/lib
            fi
            #additional_mpilibs='-ldcmfcoll.cnk -ldcmf.cnk -lpthread -L/bgsys/drivers/ppcfloor/runtime/SPI -lSPI.cna'
            additional_mpilibs='-L/bgsys/drivers/ppcfloor/gnu-linux/powerpc64-bgq-linux/lib -lc -lnss_files -lnss_dns -lresolv'
            ;;
        t3e|crayx1|craysv1)
            mpilib=/opt/ctl/mpt/mpt/lib/
            ;;
        xt3)
            mpilib=/opt/xt-mpt/default/mpich2-64/P2/lib
            ;;
        craycnl)
            if [ "x$MPICH_DIR" = "x" ]; then
              if [ "x$CRAY_MPICH_PREFIX" != "x" -a "x$orig_cxx_compiler" = "xCC" ]; then
                mpilib=$CRAY_MPICH_PREFIX/lib
              else
                echo "TAU: mpilib is not set."
              fi
            else
              if [ "x$orig_cxx_compiler" = "xCC" ]; then
                mpilib=$MPICH_DIR/lib
              fi
            fi
            ;;
        solaris2*)
            mpilib=/opt/SUNWhpc/lib
            ;;
        ppc)
            if [ $zepto = yes ]
            then
                mpilib=$zeptodir/lib
                if [ ! -e $mpilib ]
                then
                    echo "ERROR: Cannot access Zepto MPI lib directory [$mpilib]. Aborting"
                    exit 1
                fi
                additional_mpilibs="-L$zeptodir/lib -lmpich.zcl -ldcmfcoll.zcl -ldcmf.zcl -lpthread -lSPI.zcl -lzcl -lzoid_cn -lpthread -lrt -lm"
                mpilibrary="$additional_mpilibs"
            fi
            ;;
        mic_linux)
            if [ $mpc = yes ]
            then
                mpilib=`which mpcrun | sed -e 's/\/bin\/mpcrun$//'`/../mic/lib
            else
                mpilib_search=`which mpirun  | sed -e 's@//@/@g'| sed -e 's@intel64/bin/mpirun$@mic@' -e 's@bin/mpirun$@@' -e 's@bin64/mpirun$@@g'`/lib
                if [ ! -d $mpilib_search ] ; then
                    # Try the ".../lib64" directory instead of ".../lib"
                    mpilib_search=`which mpirun  | sed -e 's@//@/@g'| sed -e 's@intel64/bin/mpirun$@mic@' -e 's@bin/mpirun$@@' -e 's@bin64/mpirun$@@g'`/lib64
                fi
                if [ -d $mpilib_search -a "x$mpilib=x" ] ; then
                    mpilib=$mpilib_search
                    echo "Using MPI lib directory $mpilib"
                else
                     mpilib=""
                fi
            fi
            ;;
        *)
            if [ $mpc = yes ]
            then
                mpilib=`which mpcrun | sed -e 's/\/bin\/mpcrun$//'`/lib
            else
                mpilib_search=`which mpirun  | sed -e 's@//@/@g' | sed -e 's/\/bin\/mpirun$//' -e 's/\/bin64\/mpirun$//'`/lib
                if [ ! -d $mpilib_search ] ; then
                    # Try the ".../lib64" directory instead of ".../lib"
                    mpilib_search=`which mpirun | sed -e 's@//@/@g' | sed -e 's/\/bin\/mpirun$//' -e 's/\/bin64\/mpirun$//'`/lib64
                fi
                if [ -d $mpilib_search -a "x$mpilib" = "x" ] ; then
                    mpilib=$mpilib_search
                    echo "Using MPI lib directory $mpilib"
                else
                     mpilib=""
                fi
            fi
            ;;
    esac
fi

if [ "x$mpi" = "xyes" -a "x$mpiinc" = "x" ]; then
    echo "MPIINC is not set!"
    mpiinc_search=`which mpirun | sed -e 's@//@/@g' | sed -e 's/\/bin\/mpirun$//' -e 's/\/bin64\/mpcrun$//'`/include
    if [ ! -d $mpiinc_search ] ; then
      # Try the ".../include64" directory instead of ".../include"
      mpiinc_search=`which mpirun  | sed -e 's@//@/@g'| sed -e 's@intel64/bin/mpirun$@mic@' -e 's@bin/mpirun$@@' -e 's@bin64/mpirun$@@g'`/include64
    fi

    if [ -d $mpiinc_search -a -r $mpiinc_search/mpi.h -a "x$mpiinc" = "x" ]
    then
         mpiinc=$mpiinc_search
         echo "Using MPI include directory $mpiinc"
         echo "machine is $machine"
    else
         mpiinc_search=`which mpicc | sed -e 's/\/bin\/mpicc$//' -e 's/\/bin64\/mpicc$//'`/include
         if [ -d $mpiinc_search -a -r $mpiinc_search/mpi.h -a "x$mpiinc" = "x" ]
         then
           mpiinc=$mpiinc_search
           echo "Using MPI include directory $mpiinc"
         else
           if [ -d $mpiinc_search -a ! -r $mpiinc_search/mpi.h ] ; then
             if [ -r $mpiinc_search/mpich2/mpi.h -a "x$mpiinc" = "x" ] ; then
               mpiinc=$mpiinc_search/mpich2
               echo "Correction: Using MPI include directory $mpiinc instead"
             else
               if [ -r $mpiinc_search/openmpi/mpi.h -a "x$mpiinc" = "x" ] ; then
                 mpiinc=$mpiinc_search/openmpi
                 echo "Correction: Using MPI include directory $mpiinc instead"
               else
                 mpiinc=""
               fi
             fi
           else
             mpiinc=""
           fi
         fi
    fi
    if [ ! -r $mpiinc/mpi.h -a -r $mpiinc/mpich2/mpi.h ] ; then
        mpiinc=$mpiinc/mpich2
        echo "Correction: Using MPI include directory $mpiinc instead"
    fi
    if [ $machine = nec-sx-aurora ]; then
      mpiinc_search=`which mpincc | sed -e 's/\/bin\/mpincc$//' -e 's/\/bin64\/mpincc$//'`/include
      if [ -d $mpiinc_search -a -r $mpiinc_search/mpi.h -a "x$mpiinc" = "x" ]
      then
        mpiinc=$mpiinc_search
        echo "Using MPI include directory $mpiinc"
      fi
    fi

  if [ "x$mpilib" = "x" ] ; then
    mpilib=""
    mpilib_search=`which mpirun | sed -e 's@//@/@g' | sed -e 's/\/bin\/mpirun$//' -e 's/\/bin64\/mpirun$//'`/lib
    if [ ! -d $mpilib_search ] ; then
    # Try the ".../lib64" directory instead of ".../lib"
      mpilib_search=`which mpirun | sed -e 's/\/bin\/mpirun$//' -e 's/\/bin64\/mpirun$//'`/lib64
    fi
    if [ -d $mpilib_search -a "x$mpilib" = "x" ] ; then
      mpilib=$mpilib_search
      echo "Using MPI lib directory $mpilib"
    else
      mpilib_search=`which mpicc | sed -e 's/\/bin\/mpicc$//' -e 's/\/bin64\/mpicc$//'`/lib
      if [ -d $mpilib_search -a "x$mpilib" = "x" ] ; then
        mpilib=$mpilib_search
        echo "Using MPI lib directory $mpilib"
      else
        mpilib=""
      fi
    fi
  fi

fi
# echo "MPI include and libs checks done"



if [ "x$mpiinc" != "x" ] ; then
    # Check for OpenMPI and add additional include directory
    if [ -d "$mpiinc/openmpi" ] ; then
        echo "NOTE: Using OpenMPI"
        additional_mpiincs="-I$mpiinc/openmpi -I$mpiinc/openmpi/ompi"
    fi

    if [ -r $mpilib/mpi.mod ]; then
        echo "NOTE: Found mpi.mod in $mpilib, adding to include path"
        additional_mpiincs="$additional_mpiincs -I$mpilib"
    fi

    if [ -d "$mpiinc" ] ; then
        #echo "Include dir found. adding -I$mpiinc to INC"
        mpiincargs="$mpiincargs -I$mpiinc $additional_mpiincs"
        mpiincargs=`echo $mpiincargs | sed  -e 's/ /#/g'`
        fixmakeargs="$fixmakeargs mpiincargs=$mpiincargs"
    fi
fi


if [ $perf = yes ] ; then
    fixmakeargs="$fixmakeargs PERFLIB"
    tauoptions="${tauoptions}-perf"
    if [ "x$perfincdir" != "x" ] ; then
        fixmakeargs="$fixmakeargs perfincdir=$perfincdir"
    fi
    if [ "x$perflibdir" != "x" ] ; then
        fixmakeargs="$fixmakeargs perflibdir=$perflibdir"
    fi
    if [ "x$perflibrary" != "x" ] ; then
        fixmakeargs="$fixmakeargs perflibrary=$perflibrary"
    else
        if [ -f $perflibdir/libpapi.a ] ; then
            perflibrary="-lperfrt#-lpapi"
        else
            perflibrary="-lperfrt"
        fi
        fixmakeargs="$fixmakeargs perflibrary=$perflibrary"
    fi
fi

if [ $epilog = yes -a $epiloginc = no -a $epilogdir != no ] ; then
    if [ -f $epilogdir/include/elg_rw.h ] ; then
        epilogincdir=$epilogdir/include
    else
        if [ -f $epilogdir/include/be/elg_rw.h ] ; then
            epilogincdir=$epilogdir/include/be
        else
            epilogincdir=$epilogdir/src
        fi
    fi
fi

if [ $epilog = yes -a $epilogbin = no -a $epilogdir != no ] ; then
    if [ -f $epilogdir/bin/opari -o -f $epilogdir/bin/opari2 ] ; then
        epilogbindir=$epilogdir/bin
    else
        epilogbindir=$epilogdir/src
    fi
fi

if [ $epilog = yes -a -f $epilogbindir/skin ] ; then
    fixmakeargs="$fixmakeargs SCALASCA"
    scalasca=yes
    if [ $machine = craycnl -a -d $epilogdir/include/fe -a -d $epilogdir/lib/be ] ; then
        epiloglibdir=$epilogdir/lib/be
    fi
fi

if [ $epilog = yes -a $epiloglib = no -a $epilogdir != no ] ; then
    if [ -f $epilogdir/lib/libelg.base.a ] ; then
        epiloglibdir=$epilogdir/lib
    elif [ -f $epilogdir/src/libelg.base.a ] ; then
        epiloglibdir=$epilogdir/src
    elif [ -f $epilogdir/lib/be/libelg.base.a ] ; then
        epiloglibdir=$epilogdir/lib/be
    elif [ -f $epilogdir/lib/fe/libelg.base.a ] ; then
        epiloglibdir=$epilogdir/lib/fe
    elif [ -f $epilogdir/lib64/libelg.base.a ] ; then
        epiloglibdir=$epilogdir/lib64
    elif [ -f $epilogdir/lib32/libelg.base.a ] ; then
        epiloglibdir=$epilogdir/lib32
    else
        echo "ERROR: PLEASE SPECIFY A VALID -epiloglib=<dir> option."
    fi
fi


if [ $vampirtrace = yes ] ; then
    # use vtcc/vtf90 -vt:show to get -I's and -L/-l's


    # prefer vtf90 over vtcc
    vtshow="no"
    if [ -f "$vampirtracedir/bin/vtcc" ] ; then
        vtshow="$vampirtracedir/bin/vtcc"
    fi
    if [ -f "$vampirtracedir/bin/vtf90" ] ; then
        vtshow="$vampirtracedir/bin/vtf90"
    fi

    if [ $vtshow != "no" ] ; then

        # test VampirTrace version
        $echo "Checking version of VampirTrace... ${nnl}"
        version=`$vampirtracedir/bin/vtcc -vt:version 2>&1`
        echo "$version"
        eval major=${version/./;minor=}
        minor=`echo $minor | sed 's/\([0-9]*\).*/\1/g'`

        # check if 5.7 or later
        if [ "x$major" = "x5" -a "x$minor" = "x7" ] ; then
            fixmakeargs="$fixmakeargs VAMPIRTRACE_5_7_API"
        else
            fixmakeargs="$fixmakeargs VAMPIRTRACE_5_12_API"
        fi

        vtserchk="seq"
        vtparchk="mpi"

        if [ $openmp = yes ] ; then
            vtserchk="mt"
            vtparchk="hyb"
        fi

        tmp=`$vtshow -vt:mpi -vt:show 2>&1`
        if [[ "$elements" == vtf90:\ unknown\ option* ]] ; then
            if [ $openmp = yes ] ; then
                vtserchk="omp"
            fi
        fi

        # first, find MPI libs
        elements=`$vtshow -vt:$vtparchk -vt:show 2>&1`
        if [[ "$elements" == vtf90:\ unknown\ option* ]] ; then
            elements=`$vtshow -vt:$vtparchk -vt:showme 2>&1`
        fi
        vampirtraceincs=""
        vampirtracempilibs=""
        for arg in $elements ; do
            case $arg in
                -I*)
                    vampirtraceincs="$vampirtraceincs#$arg"
                    ;;
                -L*)
                    vampirtracempilibs="$vampirtracempilibs#$arg"
                    ;;
                -l*)
                    vampirtracempilibs="$vampirtracempilibs#$arg"
                    ;;
            esac
        done

        # now, find serial libs
        elements=`$vtshow -vt:show -vt:$vtserchk 2>&1`
        if [[ "$elements" == vtf90:\ unknown\ option* ]] ; then
            elements=`$vtshow -vt:showme -vt:$vtserchk 2>&1`
        fi
        vampirtracelibs=""
        for arg in $elements ; do
            case $arg in
                -L*)
                    vampirtracelibs="$vampirtracelibs#$arg"
                    ;;
                -l*)
                    vampirtracelibs="$vampirtracelibs#$arg"
                    ;;
            esac
        done


        fixmakeargs="$fixmakeargs vampirtraceincs=$vampirtraceincs"
        fixmakeargs="$fixmakeargs vampirtracelibs=$vampirtracelibs"
        fixmakeargs="$fixmakeargs vampirtracempilibs=$vampirtracempilibs"

        vampirtraceshow=yes
    fi

    if [ -f $vampirtracedir/lib/libvt.a ] ; then
        vampirtracelibdir=lib
    else
        vampirtracelibdir=vtlib
    fi
fi

if [ $beacon = yes -a -d $beacondir -a -d $beacondir/include -a -d $beacondir/lib ]; then
    fixmakeargs="$fixmakeargs BEACON beacondir=$beacondir"
    tauoptions="${tauoptions}-beacon"
fi

if [ $papi = yes ] ; then
    fixmakeargs="$fixmakeargs PAPI papidir=$papidir"
    tauoptions="${tauoptions}-papi"
    p=$papidir/lib
    papisubdir=lib
    if [ $machine = "mips32" ] ; then
        papisubdir=lib32
        fixmakeargs="$fixmakeargs papisubdir=lib32"
    else
        if [ -f $p/libpapi.so -o -f $p/libpapi.a -o -f $p/libpapi64.so -o -f $p/libpapi64.a -o -f $p/libpapi.rts.a ] ; then
            papisubdir=lib
            fixmakeargs="$fixmakeargs papisubdir=lib"
        else
            if [ -f $p/cnos64/libpapi.so ] ; then
                papisubdir=lib/cnos64
                fixmakeargs="$fixmakeargs papisubdir=lib/cnos64"
            else
                if [ -f $p/xt-cnl/lib/libpapi.so ] ; then
                    papisubdir=xt-cnl/lib
                    fixmakeargs="$fixmakeargs papisubdir=xt-cnl/lib"
                else
                    if [ -f $papidir/lib64/libpapi.so ] ; then
                        papisubdir=lib64
                        fixmakeargs="$fixmakeargs papisubdir=lib64"
                    else
                        papisubdir=src
                        fixmakeargs="$fixmakeargs papisubdir=src"
                    fi
                fi
            fi
        fi
    fi


    if [ -f $papidir/lib/libpfm.a ] ; then
        cat <<EOF > tau_papi_test.c
#include <papi.h>
int main (int argc, char **argv) {
  int papi_ver;
  papi_ver = PAPI_library_init(PAPI_VER_CURRENT);
}
EOF

        if $c_compiler $orig_useropt tau_papi_test.c -I$papidir/include -L$papidir/$papisubdir -lpfm -lpapi -lpfm -o tau_papi_test 1> /dev/null 2>&1 ; then
            fixmakeargs="$fixmakeargs PAPIPFM"
            papipfm=yes
            papiperfctr=no
        else
          if $c_compiler $orig_useropt tau_papi_test.c -I$papidir/include -L$papidir/$papisubdir -lpfm -lpapi -lpfm -lperfctr -o tau_papi_test 1> /dev/null 2>&1 ; then
            fixmakeargs="$fixmakeargs PAPIPFMPERFCTR"
            papipfm=yes
            papiperfctr=yes
          fi
        fi

        if [ $machine = craycnl ] ; then
            $c_compiler $orig_useropt -c -fPIC tau_papi_test.c -I$papidir/include
            if $c_compiler $orig_useropt tau_papi_test.o -I$papidir/include -shared -o libtau_papi_test.so $papidir/$papisubdir/libpapi.a 1> /dev/null 2>&1 ; then
                echo "PAPI library links properly"
            else
                if $c_compiler $orig_useropt tau_papi_test.o -I$papidir/include -shared -o libtau_papi_test.so $papidir/$papisubdir/libpapi.so 1> /dev/null 2>&1 ; then
                          echo "PAPI library links properly with .so"
                    fixmakeargs="$fixmakeargs CRAYCNL_PAPI_SHARED"
                else
                    echo "NOTE: libpapi.a can't be called in a TAU shared object. Using -DISABLESHARED"
                    fixmakeargs="$fixmakeargs NOSHARED"
                fi
            fi
        fi
        /bin/rm -f tau_papi_test.c tau_papi_test tau_papi_test.o libtau_papi_test.so
    fi
fi

if [ $darshan = yes ] ; then
    fixmakeargs="$fixmakeargs DARSHAN darshandir=$darshandir"
    tauoptions="${tauoptions}-darshan"
fi



######################################################################
# Set default CC compiler in all Makefiles

if [ "$mpi" = "yes" -a "$ibmxlc_r" = "no" -a "$ibmxlc" = "yes" -a "$machine" = "ibm64" ]
then
    echo "WARNING: PLEASE use xlC_r instead of xlC with MPI in 64 bit mode!! Reconfigure."
    echo "----------------------------------------------------------------"
fi

# Now check for SHMEM options
if [ $shmem = yes ]
then
    if [ "x$shmeminc" != "x" ]
    then
        if [ -d $shmeminc ]
        then
            shmem_h="$shmeminc/shmem.h"
            shmemdir="$shmeminc/.."
            #echo "Include dir found. adding -I$mpiinc to INC"
            shmemincargs="-I$shmeminc"
            if [ -r $shmeminc/mpp/shmem.h ]
            then
                shmemincargs="-I$shmeminc#-I$shmeminc/mpp"
            fi
            gpshmem=no
            if [ -r $shmeminc/gpshmem.h ]
            then
                if [ $pshmem = yes ] ; then
                    fixmakeargs="$fixmakeargs GPSHMEM_PSHMEM"
                    gpshmem_pshmem=yes
                else
                    fixmakeargs="$fixmakeargs GPSHMEM"
                    gpshmem=yes
                fi
            fi
            if [ -r $shmeminc/shmemx.h ]
            then
                fixmakeargs="$fixmakeargs SHMEMX_H"
            fi
            shmembindir=`echo $shmeminc | sed -e 's@include@bin@g'`
            if [ -r $shmembindir/oshcc ]
            then
                if [ $pshmem = yes ] ; then
                    openshmem_pshmem=yes
                    fixmakeargs="$fixmakeargs OPENSHMEM_PSHMEM"
                else
                    openshmem=yes
                    fixmakeargs="$fixmakeargs OPENSHMEM"
                fi
            fi
            if [ -r $shmembindir/mpiexec_mpt ]
            then
                if [  `nm -A $MPI_ROOT/lib/libsma.so | grep pshmem | wc -l` != 0 ] && [ $pshmem = yes ] ; then
                  fixmakeargs="$fixmakeargs SGI_MPT_PSHMEM"
                else
                  fixmakeargs="$fixmakeargs SGI_MPT_SHMEM"
                fi
            fi
        fi
    else
        openshmem=no
        oshcc_var=`which oshcc 2>/dev/null`
  retval=$?
        if [ "x$oshcc_var" != "x" -a $retval = 0 ]
        then
            openshmem_include_dir=`echo $oshcc_var | sed -e 's@bin/oshcc@include@g'`
            shmemdir="$openshmem_include_dir/.."
            if [ -r $openshmem_include_dir/mpp/shmem.h -a -r $openshmem_include_dir/../bin/mpiexec_mpt ]
            then
                shmemincargs="-I$openshmem_include_dir#-I$openshmem_include_dir/mpp"
                if [  `nm -A $MPI_ROOT/lib/libsma.so | grep pshmem | wc -l` != 0 ] && [ $pshmem = yes ] ; then
                  fixmakeargs="$fixmakeargs SGI_MPT_PSHMEM"
                else
                  fixmakeargs="$fixmakeargs SGI_MPT_SHMEM"
                fi
            else
                if [ -r $oshcc_var ]
                then
                    if [ -r $openshmem_include_dir/mpp/shmem.h ]
                    then
                        shmemincargs="-I$openshmem_include_dir#-I$openshmem_include_dir/mpp"
                    fi
                    if [ $pshmem = yes ] ; then
                        openshmem_pshmem=yes
                        fixmakeargs="$fixmakeargs OPENSHMEM_PSHMEM"
                    else
                        openshmem=yes
                        fixmakeargs="$fixmakeargs OPENSHMEM"
                    fi
                fi
            fi
            shmem_h="$openshmem_include_dir/shmem.h"
            if [ -r $openshmem_include_dir/shmemx.h ]
            then
                fixmakeargs="$fixmakeargs SHMEMX_H"
            fi
        fi
        if [ $catamount = yes ]
        then
            shmemincargs="-I/opt/xt-mpt/default/include/mpp#-I/opt/xt-mpt/default/include"
            shmemdir="/opt/xt-mpt/default"
        fi
        if [ $machine = craycnl ]
        then
            if [ "x$MPT_DIR" = "x" -a "x$CRAY_SHMEM_DIR" != "x" ]
            then
                shmemdir="$CRAY_SHMEM_DIR"
                if [ -r $CRAY_SHMEM_DIR/include/mpp/shmem.h ]
                then
                    shmemincargs="-I$CRAY_SHMEM_DIR/include/mpp/#-I$CRAY_SHMEM_DIR/include"
                fi
                shmem_h="$CRAY_SHMEM_DIR/include/mpp/shmem.h"
            else
                shmemdir="$MPT_DIR/sma"
                if [ -r $MPT_DIR/sma/include/mpp/shmem.h ]
                then
                    shmemincargs="-I$MPT_DIR/sma/include/mpp/#-I$MPT_DIR/sma/include/"
                    shmem_h="$MPT_DIR/sma/include/mpp/shmem.h"
                else
                    if [ -r $CRAY_MPICH2_BASEDIR/sma64/include/shmem.h ] ; then
                      shmemincargs="-I$CRAY_MPICH2_BASEDIR/sma64/include/mpp#-I$CRAY_MPICH2_BASEDIR/sma64/include"
                      shmem_h="$CRAY_MPICH2_BASEDIR/sma64/include/mpp/shmem.h"
                    else
                      shmemincargs="-I/opt/mpt/default/xt/sma64/include/mpp#-I/opt/mpt/default/xt/sma64/include"
                      shmem_h="/opt/mpt/default/xt/sma64/include/mpp/shmem.h"
                    fi
                    # old options
                fi
            fi
            fixmakeargs="$fixmakeargs CRAYSHMEM"
        fi
        if [ $machine = x86_64 -o $machine = ia64 ]
        then
            mpiexec_mpt_var=`which mpiexec_mpt 2>/dev/null`
            if [ "x$mpiexec_mpt_var" != "x" ]
            then
                libsmafile=`echo $mpiexec_mpt_var | sed -e 's@bin/mpiexec_mpt@lib/libsma.so@g'`
                shmeminclude=`echo $mpiexec_mpt_var | sed -e 's@bin/mpiexec_mpt@include@g'`
                shmemdir="$shmeminclude/.."
                shmem_h="$shmeminclude/shmem.h"
                if [ -r $shmeminclude/shmemx.h ]
                then
                    fixmakeargs="$fixmakeargs SHMEMX_H"
                fi
                if [ -r $libsmafile -a -d $shmeminclude -a "x$shmeminclude" != "x" ]
                then
                    shmemlibdir=`echo $mpiexec_mpt_var | sed -e 's@bin/mpiexec_mpt@lib@g'`
                    echo "Detected SGI MPT: Using $shmeminclude as include dir and $shmemlibdir as SHMEM lib dir"
                    if [  `nm -A $MPI_ROOT/lib/libsma.so | grep pshmem | wc -l` != 0 ] && [ $pshmem = yes ] ; then
                      fixmakeargs="$fixmakeargs SGI_MPT_PSHMEM"
                    else
                      fixmakeargs="$fixmakeargs SGI_MPT_SHMEM"
                    fi
                    shmemincargs="-I$shmeminclude#-I$shmeminclude/mpp"
                    if [ $shmemlibrary = no -a "x$shmemlib" = "x" ]
                    then
                        shmemlib="$shmemlibdir"
                        shmemlibrary=-lsma#-lmpi
                    fi
                fi
            fi
        fi
    fi

    if [ "x$shmemlibrary" != "x" -a "$shmemlibrary" = "no" ]
    then
        shmemlibrary=-lsma
        if [ $gpshmem = yes ]
        then
            shmemlibrary=-lgpshmem
        fi
    fi
    if [ "x$oshcc_var" = "x" ]
    then
        oshcc_var=`which oshcc  2>/dev/null`
        if [ "x$oshcc_var" != "x" -a "$?" = 0 ]
        then
            if [ $pshmem = yes ] ; then
                openshmem_pshmem=yes
                     fixmakeargs="$fixmakeargs OPENSHMEM_PSHMEM"
            else
                fixmakeargs="$fixmakeargs OPENSHMEM"
                openshmem=yes
            fi
        fi
    fi
    if [ "x$oshcc_var" != "x" ]
    then
        openshmem_lib_dir=`echo $oshcc_var | sed -e 's@bin/oshcc@lib@g'`
        if [ -r $openshmem_lib_dir/libopenshmem.so -o -r $openshmem_lib_dir/libopenshmem.a ]
        then
            shmemlibargs="$shmemlibargs -L$openshmem_lib_dir"
            if [ "$shmemlibrary" = "no"  -o "$shmemlibrary" = "-lsma" ]
            then
                shmemlibrary=-lopenshmem
            fi
        fi
        if [ -r $openshmem_lib_dir/liboshmem.so -o -r $openshmem_lib_dir/liboshmem.a ]
        then
            shmemlibargs="$shmemlibargs -L$openshmem_lib_dir"
            if [ "$shmemlibrary" = "no"  -o "$shmemlibrary" = "-lsma" ]
            then
                shmemlibrary=-loshmem
                if [ $pshmem = yes ] ; then
                    fixmakeargs="$fixmakeargs OPENMPI_SHMEM"
                fi
            fi
        fi
        if [ -r $openshmem_lib_dir/libshmem.so -o -r $openshmem_lib_dir/libshmem.a ]
        then
            shmemlibargs="$shmemlibargs -L$openshmem_lib_dir"
            if [ "$shmemlibrary" = "no"  -o "$shmemlibrary" = "-lsma" ]
            then
                shmemlibrary=-lshmem
                if [ $pshmem = yes ] ; then
                    fixmakeargs="$fixmakeargs OPENMPI_SHMEM"
                fi
            fi
        fi
    if [ -r $openshmem_lib_dir/libsma.so -o -r $openshmem_lib_dir/libsma.a ]
    then
        shmemlibargs="$shmemlibargs -L$openshmem_lib_dir"
    fi
    fi

    if [ "x$shmemlib" != "x" ]
    then
        if [ -d $shmemlib ]
        then
            #echo "Lib dir found. adding -L$shmemlib to LIB"
            shmemlibargs="$shmemlibargs -L$shmemlib"
        fi
        shmembindir=`echo $shmemlib | sed -e 's@lib@bin@g'`
        if [ -r $shmembindir/oshcc ]
        then
            if [ $pshmem = yes ] ; then
                openshmem_pshmem=yes
                     fixmakeargs="$fixmakeargs OPENSHMEM_PSHMEM"
            else
                openshmem=yes
                     fixmakeargs="$fixmakeargs OPENSHMEM"
            fi
        fi
        if [ -r $shmembindir/mpiexec_mpt ]
        then
            if [  `nm -A $MPI_ROOT/lib/libsma.so | grep pshmem | wc -l` != 0 ] && [ $pshmem = yes ] ; then
              fixmakeargs="$fixmakeargs SGI_MPT_PSHMEM"
            else
              fixmakeargs="$fixmakeargs SGI_MPT_SHMEM"
            fi
            shmemlibrary=-lsma#-lmpi
        fi

    else
        if [ $catamount = yes ]
        then
            shmemlibargs="$shmemlibargs -L/opt/xt-mpt/default/lib/cnos64"
        fi
        if [ $machine = craycnl ]
        then
            if [ -r $MPT_DIR/sma/lib64/libsma.a ]
            then
                shmemlibargs="$shmemlibargs -L$MPT_DIR/sma/lib64/"
            else
                if [ -r $CRAY_SHMEM_DIR/lib64/libsma.a ] ; then
                  shmemlibargs="$shmemlibargs -L$CRAY_SHMEM_DIR/lib64/"
                else
                  shmemlibargs="$shmemlibargs -L/opt/mpt/default/xt/sma64/lib"
                fi
            fi
            if [ $pshmem = yes ] ; then
                craycnl_pshmem = yes
                fixmakeargs="$fixmakeargs CRAYCNL_PSHMEM"
            fi
        fi
    fi

    if [ "x$shmemmpiinc" != "x" ] ; then
            shmemmpiincargs="-I$shmemmpiinc"
    fi

    if [ "x$shmem_h" != "x" -a -r "$shmem_h" ] ; then
      shmem_minor_version=`grep SHMEM_MINOR_VERSION "$shmem_h" | grep -v CRAY | grep -v OSHMEM | grep -v _SHMEM | awk '{print $3}'`
      if [ "x$shmem_minor_version" = "x" ]; then
        shmem_minor_version=`grep SHMEM_MINOR_VERSION "$shmem_h" | grep -v CRAY | grep -v OSHMEM | awk '{print $3}'`
      fi
      shmem_major_version=`grep SHMEM_MAJOR_VERSION "$shmem_h" | grep -v CRAY | grep -v OSHMEM | grep -v _SHMEM | awk '{print $3}'`
      if [ "x$shmem_major_version" = "x" ]; then
        shmem_major_version=`grep SHMEM_MAJOR_VERSION "$shmem_h" | grep -v CRAY | grep -v OSHMEM | awk '{print $3}'`
      fi
      echo "OpenSHMEM minor version: $shmem_minor_version major version: $shmem_major_version"
      if [ "$shmem_major_version" -lt 2 -a "$shmem_minor_version" -le 2 ] ; then
        orig_useropt="$orig_useropt -DSHMEM_1_2"
        useropt="$useropt#-DSHMEM_1_2"
        echo "Detected OpenSHMEM version 1.2 or earlier."
      fi
    fi


    shmemlibargs="$shmemlibargs $shmemlibrary"
    shmemlibargs=`echo $shmemlibargs | sed  -e 's/ /#/g'`
    fixmakeargs="$fixmakeargs SHMEM"
    tauoptions="${tauoptions}-shmem"
    fixmakeargs="$fixmakeargs shmemlibargs=$shmemlibargs shmemincargs=$shmemincargs shmemdirargs=$shmemdir shmemmpiincargs=$shmemmpiincargs"
fi


gcclibdir=unknown
case $c_compiler in
  gcc|gcc4|gcc-*)
    echo "Default C compiler will be " `$c_compiler -v 2>&1 | tail -1`
    # I must set the gcclibdir in the Makefiles if we combine gcc, cc, and CC
    gcclibdir=`$c_compiler -v 2>&1 | awk '{print $4;}' | \
       awk -F/ '{for (i=2; i<NF; i++) printf("/%s",$i); printf("\n"); }'`
    ;;
  *)
    echo Default C compiler will be $c_compiler
    fixmakeargs="$fixmakeargs USE_CC"
    ;;
esac

if [ $upc_compiler = gcc -o $upc_compiler = upc ]
then
    gupc=yes
fi

if [ $upc_compiler = xlupc ]
then
    fixmakeargs="$fixmakeargs XLUPC"
fi

######################################################################
# Set default compiler options




if [ $scorep = yes ]
then
  swap_c_compiler=$c_compiler
  c_compiler=$orig_c_compiler
  swap_cxx_compiler=$cxx_compiler
  cxx_compiler=$orig_cxx_compiler
  swap_fortran_compiler=$fortran_compiler
  fortran_compiler=$orig_fortran_compiler
fi
fixmakeargs="c_compiler=$c_compiler cxx_compiler=$cxx_compiler useropt=$useropt extradir=$extradir extradircxx=$extradircxx extrashlibopts=$extrashlibopts $fixmakeargs"
# We really need mpicc as the linker for Score-P.

if [ $scorep = yes ]
then
  c_compiler=$swap_c_compiler
  cxx_compiler=$swap_cxx_compiler
  fortran_compiler=$swap_fortran_compiler
fi

######################################################################
# discover the paths for standard system include directories

cat << EOF > get_stdinc.C
#include <stddef.h>
#include <iostream>
EOF

cat << EOF > find_stdinc.awk
BEGIN {cinc=""; cppinc="";}
/^#[0123456789]+ ".+\/stddef.h"/ {cinc = substr(\$2, 2, length(\$2)-11);}
/^#line [0123456789]+ ".+\/stddef.h"/ {cinc = substr(\$3, 2, length(\$3)-11);}
/^# [0123456789]+ ".+\/stddef.h"/ {cinc = substr(\$3, 2, length(\$3)-11);}
/^#[0123456789]+ ".+\/iostream.h"/ {cppinc = substr(\$2, 2, length(\$2)-13);}
/^#line [0123456789]+ ".+\/iostream.h"/ {cppinc = substr(\$3, 2, length(\$3)-13);}
/^# [0123456789]+ ".+\/iostream.h"/ {cppinc = substr(\$3, 2, length(\$3)-13);}
END {printf("-I%s -I%s\n", cinc, cppinc);}
EOF

if [ $machine = hitachi ]
then
    stdinc="-I/usr/include"
else
    stdinc=`$cxx_compiler  -E get_stdinc.C  2>/dev/null | awk -f find_stdinc.awk `
fi

rm -f get_stdinc.C find_stdinc.awk


######################################################################
# Checking gcc 4.6+ version. It needs -lmpi to be added after -ltau
cat << EOF > gcc46test.C
#if (__GNUC__ > 4 )
#error "Using GCC 4.6+"
#elif ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 6))
#error "Using GCC 4.6+"
#endif
EOF

if $cxx_compiler $orig_useropt -c gcc46test.C 1> /dev/null 2>&1
then
    echo "Not using GNU 4.6+ fixes" > /dev/null
else
    echo "NOTE: Using GNU 4.6+ fixes" > /dev/null
    fixmakeargs="$fixmakeargs GNU46PLUS"
fi
/bin/rm -f gcc46test.*


######################################################################

if [ $use_openacc = yes ] ; then
    echo -n "Checking OpenACC compiler support:"
    touch openacc_test.c
    cat <<EOF > openacc_test.c
#include <stdio.h>
#include <acc_prof.h>

#define VERSION 0.1

void prof_inc( acc_prof_info* prof_info, acc_event_info* event_info, acc_api_info* api_info ){
  printf( "line %d\n", __LINE__ );
}

void acc_register_library(acc_prof_reg reg, acc_prof_reg unreg,
                          acc_prof_lookup lookup) {
  reg( acc_ev_compute_construct_start, &prof_inc, acc_reg );
}

int main(){
  int arr[2] = {10, 11};
#pragma acc parallel copy(arr) num_gangs(1)
  for( int j = 0; j < 2; ++j )
    printf( "arr[%d]=%d\n", j, arr[j] );
  return 0;
}
EOF

    if [ "$llvm" == "yes" ]; then openacc_flag=-fopenacc; fi
    if [ "$pgi" == "yes" ];  then openacc_flag=-acc; fi
    if [ "$nvhpc" == "yes" ];  then openacc_flag=-acc; fi

    #This deletion is here in case the file was left behind from a previous call to configure
    rm -f ./openacc_test

    $c_compiler $openacc_opt $openacc_flag -o openacc_test openacc_test.c 1>/dev/null 2>&1
    result=$?
    rm -f ./openacc_test openacc_test.c
    if [ $result = "0" ]; then
        echo " yes"
        openacc_compiler_support=yes
        fixmakeargs="$fixmakeargs OPENACC"
        use_openacc=yes
        tauoptions="${tauoptions}-acc"
    else
        echo " no"
	echo "Error: reconfigure without -openacc or use a different compiler"
	exit -1
    fi
fi


######################################################################
if [ $cxx_compiler = pgCC -o $cxx_compiler = pgc++ -o $cxx_compiler = pgcpp -o "x$PE_ENV" = "xPGI" ]
then
    cat <<EOF > pgitest.C
int foo(int x)
{
  return x;
}
EOF
    if $cxx_compiler $orig_useropt -c pgitest.C 1> /dev/null 2>&1
    then
        if $cxx_compiler $orig_useropt --prelink_objects pgitest.o 1> /dev/null 2>&1
        then
            echo "PGI compiler supports the --prelink_objects option"
        else
            echo "PGI compiler should not use --prelink_objects option for making libs"
            fixmakeargs="$fixmakeargs PGINOPRELINK"
        fi
    else
        echo "ERROR: Your PGI compiler is not working! Please check the license file"
    fi
    /bin/rm -f pgitest.o pgitest.C libpgitest.a
cat <<EOF > pgiacctest.C
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <openacc.h>
#include "pgi_acc_prof.h"
extern "C" void
acc_register_library(acc_prof_reg reg, acc_prof_reg unreg, acc_prof_lookup lookup)
{
return ;
}
EOF
    if $cxx_compiler $orig_useropt -c pgiacctest.C 1> /dev/null 2>&1
    then
      echo "PGI compiler supports OpenACC profiling interface. Enabling OpenACC."
      fixmakeargs="$fixmakeargs PGI_OPENACC TAU_USE_GPU"
      use_openacc=yes
    else
      echo "PGI compiler doesn't support OpenACC profiling interface."
    fi
    /bin/rm -f pgiacctest.C pgiacctest.o

cat <<EOF > pgiacc15.c
#include <sys/types.h>
#include <openacc.h>
#include "pgi_acc_prof.h"

static void
Tau_openacc_callback( acc_prof_info* prof_info, acc_event_info* event_info, acc_api_info* api_info )
{
    int x;
    switch (prof_info->event_type) {
      case acc_ev_implicit_wait_start         :  x = 1;
      default: x = 2;
    }
    return;
}
EOF
    if $cxx_compiler $orig_useropt -c pgiacc15.c 1> /dev/null 2>&1
    then
      echo "PGI compiler supports PGI v15.x OpenACC API (acc_ev_implicit_wait_start)"
      fixmakeargs="$fixmakeargs PGI_OPENACC_15x"
   fi
    /bin/rm -f pgiacc15.c pgiacc15.o

fi

if [ $pgi = yes ]; then
  if [ -d $extradir/lib -a ! -r $extradir/lib/libstd.a ]; then
    echo "NOTE: PGI 16+ compiler does not provide libstd.* files in $extradir/lib"
    fixmakeargs="$fixmakeargs PGI_NO_LIBSTD"
  fi
fi

if [ $machine = craycnl -a $pmi = no ]; then
  echo "Testing for <pmi.h> header... ${nnl}"
cat <<EOF > pmitest.c
#include <pmi.h>
#include <ugni.h>
EOF
  if $cxx_compiler -c pmitest.c 1> /dev/null 2>&1
  then
    echo "yes"
    fixmakeargs="$fixmakeargs CRAYCNL_PMI_FOUND"
  else
    echo "no."
    echo "WARNING! PMI not found. You may need to reconfigure TAU with -PMI if there are missing references during linking."
    echo "============="
  fi
fi
/bin/rm -f pmitest.c


######################################################################
# If epilog is used, check and see if it supports v2.1s write interface
if [ $epilog =  yes ]
then
    echo "Checking epilog write interface:"
    cat << EOF > elgtest.C
#include <elg_rw.h>
int foo(ElgOut *out, elg_ui4 eventc)
{
  return ElgOut_write_NUM_EVENTS(out, eventc);
}
EOF
    if $cxx_compiler $orig_useropt -I$epilogincdir -c elgtest.C 1> /dev/null 2>&1
    then
        fixmakeargs="$fixmakeargs TAU2EPILOG"
    fi
    rm -f elgtest.C elgtest.o
fi



######################################################################
# Set some more Profiling options

if [ $stdcxxlib = yes ]
then
    fixmakeargs="$fixmakeargs STDCXXLIB"
fi


######################################################################
if [ $machine = craycnl ]
then
    #echo "This looks like a Cray Compute Node Linux (CNL) system..."
    fixmakeargs="$fixmakeargs CRAYCNL"
fi

######################################################################
if [ $catamount = yes ]
then
    #echo "This looks like a CATAMOUNT Cray RS system..."
    fixmakeargs="$fixmakeargs CATAMOUNT"

    if [ $fortran_compiler = pgi ]
    then
        fixmakeargs="$fixmakeargs PGI_CATAMOUNT"
    fi
fi

######################################################################

real_cxx_compiler=$cxx_compiler
if [ $real_cxx_compiler = mpicxx ]; then
    real_cxx_compiler=$internal_compiler
fi

if [[ $real_cxx_compiler = g++ || $real_cxx_compiler = g++4 || $real_cxx_compiler = g++-* || $real_cxx_compiler = egcs || $real_cxx_compiler = powerpc64-linux-g++ || $real_cxx_compiler = c++ || $real_cxx_compiler = scg++ || $real_cxx_compiler = mips64el-gentoo-linux-gnu-g++ || $real_cxx_compiler = powerpc-bgp-linux-g++ || $real_cxx_compiler = x86_64-w64-mingw32-g++ || $real_cxx_compiler = powerpc64-bgq-linux-g++ ]]
then
    if [ $machine != crayxmt ]
    then
        fixmakeargs="$fixmakeargs GNU COMPINST_GNU"

    fi
    if [ $machine = ibm64 -o $machine = rs6000 -o $machine = ppc64 -o $machine = bgl -o $machine = bgp -o $machine = ibm64linux -o $machine = bgq ]
    then
        tauoptions="${tauoptions}-gnu"
    fi
    taugcclibdir=`$host_prefix$c_compiler -print-libgcc-file-name | sed -e 's,[^/]*$,,'`
    echo "Using GNU lib dir as $taugcclibdir"
    taugccstdcxxlibdir=`$host_prefix$c_compiler -print-file-name=libstdc++.a | sed -e 's,[^/]*$,,'`
    if [ $machine = apple -a "x$taugcclibdir" = "x" -a "x$taugccstdcxxlibdir" = "x" ]; then
# if it is blank, please use TAU libs so we don't have a -L<blank> -lgcc_s.1
      taugcclibdir=${tauroot}/${machine}/lib
      taugccstdcxxlibdir=${tauroot}/${machine}/lib
      echo "taugcclibdir = $taugcclibdir taugccstdcxxlibdir=$taugccstdcxxlibdir"
    fi
    echo "Using GNU stdc++ lib dir as $taugccstdcxxlibdir"
    fixmakeargs="$fixmakeargs taugcclibdir=$taugcclibdir taugccstdcxxlibdir=$taugccstdcxxlibdir"
fi

######################################################################
# Checking the availability of the LD auditor. We need glibc 2.4 or greater and
# link.h and dlfn.h
cat << EOF > gccAuditorTest.c
#include <features.h>
#include <dlfcn.h>
#include <link.h>
#if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 4) || __GLIBC__ > 2)
#else
#error "GLIBC LD AUDITOR available."
#endif
EOF

$host_prefix$c_compiler $orig_useropt -c gccAuditorTest.c 1> /dev/null 2>&1
if [ "$?" = "0" ] && [ $machine != "arm64_linux" ]; then
    echo "Using LD AUDITOR."
    fixmakeargs="$fixmakeargs LD_AUDITOR_AVAILABLE"
else
    echo "LD AUDITOR unavailable, disabling"
fi
/bin/rm -f gccAuditorTest.*


######################################################################
# Do we need -lm Ubuntu does!
if [ $machine != apple -a $machine != arm64_apple ]; then
  if [ -f /etc/issue -a `grep Ubuntu /etc/issue | wc -l ` = 1 ]; then
    if [ "x$tau_uselm" != "xno" ]; then
      tau_uselm=yes
      fixmakeargs="$fixmakeargs TAU_USELM"
    fi
    if [ "x$tau_use_no_pie" != "xno" ]; then
      fixmakeargs="$fixmakeargs TAU_USE_NO_PIE_ON_UBUNTU"
    fi
  fi
fi

######################################################################
# TEST FOR ALL MACHINE TYPES
case $machine in
    hp9000s700 | hp9000s800)
        echo Found a \"$machine\" configuration definition
        fixmakeargs="arch=$machine NO_RANLIB HP $fixmakeargs"
        # HP-ALLOCA HP_CFLAGS

        if [ `/bin/uname -r | cut -d. -f2` = 10 ] ; then
            echo This machine uses HPUX Version 10
            fixmakeargs="HPUX10 $fixmakeargs"
        fi

        if [ $c_compiler = cc ] ; then
            fixmakeargs="HP_CFLAGS HP-ALLOCA $fixmakeargs"
        fi

        if [ $cxx_compiler = g++ ] ; then
            fixmakeargs="HPGNU $fixmakeargs"
        fi

        if [ $pdt_cxx_compiler = aCC ] ; then
            fixmakeargs="PDTHPACC $fixmakeargs"
        fi

        ;;

    decstation)
        echo Found a \"$machine\" configuration definition
        #        fixmakeargs="arch=$machine MIPS_CC $fixmakeargs"
        # with gcc, there are no changes needed...
        fixmakeargs="arch=$machine $fixmakeargs"
        ;;

    ppc64)
        echo "Found an IBM PPC-64 Linux configuration definition"
        fixmakeargs="arch=$machine PPC64 $fixmakeargs"
        linuxtimers=yes
        if [ $papi = yes ] ; then
            fixmakeargs="$fixmakeargs PPC64PAPI"
        fi
        if [ $c_compiler = xlc -o $c_compiler = xlc_r ] ; then
            fixmakeargs="$fixmakeargs PPC64XLC"
        fi
        if [ $fortran_compiler = ibm -o $fortran_compiler = ibm64 ] ; then
            fixmakeargs="$fixmakeargs PPC64XLF"
        fi
        ;;

    bgp)
        echo "Found an IBM PPC-64 BG/P Linux configuration definition"
        fixmakeargs="arch=$machine BGP $fixmakeargs"
        if [ $papi = yes ] ; then
            p=$papidir/lib
            if [ -f $p/libpapi.a ] ; then
                fixmakeargs="$fixmakeargs BGPPAPI"
            fi
        fi
        ;;
    bgq)
        echo "Found an IBM PPC-64 BG/Q Linux configuration definition"
        fixmakeargs="arch=$machine BGQ $fixmakeargs"
        if [ $papi = yes ] ; then
            p=$papidir/lib
            if [ -f $p/libpapi.a ] ; then
                fixmakeargs="$fixmakeargs BGQPAPI"
            fi
        fi
        ;;
    bgl)
        echo "Found an IBM PPC-64 BGL Linux configuration definition"
        fixmakeargs="arch=$machine BGL $fixmakeargs"
        if [ $papi = yes ] ; then
            p=$papidir/lib
            if [ -f $p/libpapi.a ] ; then
                fixmakeargs="$fixmakeargs BGLPAPI"
            else
                fixmakeargs="$fixmakeargs BGLPAPI_RTS"
            fi
        fi
        ;;

    ia64)
        echo "Found an IA-64 configuration definition"
        fixmakeargs="arch=$machine $fixmakeargs"
        linuxtimers=yes
        if [ $papi = yes ] ; then
            fixmakeargs="$fixmakeargs IA64PAPI"
        fi
        ;;

    i386_linux)
        echo "Found a 32-bit x86 linux machine"
        linuxtimers=yes
        fixmakeargs="arch=$machine $fixmakeargs"
        ;;

    crayxmt)
        echo "Found a Cray XMT configuration"
        fixmakeargs="arch=$machine $fixmakeargs"
        ;;

    x86_64|xt3|craycnl)
        echo "Found an x86_64 configuration definition"
        fixmakeargs="arch=$machine $fixmakeargs"
        linuxtimers=yes

        if [ $papi = yes ] ; then
            if [ $papiperfctr = yes ] ; then
                fixmakeargs="$fixmakeargs X86_64PAPI"
            else
                fixmakeargs="$fixmakeargs X86_64PAPI_NEW"
            fi
        if [ "x$papidir" = "x/usr" -o "x$papidir" = "x/usr/" ]; then
                  fixmakeargs="$fixmakeargs PAPI_WITHOUT_LIB_INC"
        fi

        fi
        ;;
    ibm64)
        echo Found a \"$machine\" configuration definition
        if [ `uname -s` = AIX ]
        then
            fixmakeargs="arch=$machine $fixmakeargs SP1 IBM64"
        else
            if [ `uname -s ` = Linux ]
            then
                fixmakeargs="arch=$machine $fixmakeargs PPC64 IBM64"
            else
                fixmakeargs="arch=$machine $fixmakeargs IBM64"
            fi
        fi

        if [ $papi = yes ]
         then
            fixmakeargs="$fixmakeargs IBM64PAPI"
        fi
        ;;
    ibm64linux)
        echo Found a \"$machine\" configuration definition
        fixmakeargs="arch=$machine $fixmakeargs PPC64 IBM64LINUX"
        linuxtimers=yes
        if [ $papi = yes ] ; then
            fixmakeargs="$fixmakeargs IBM64PAPILINUX"
        fi

        if [ $c_compiler = xlc -o $c_compiler=xlc_r ] ; then
            fixmakeargs="$fixmakeargs IBM64LINUX_XLC"
        fi

        if [ $fortran_compiler = ibm -o $fortran_compiler = ibm64 -o $fortran_compiler = xlf ] ; then
            fixmakeargs="$fixmakeargs IBM64LINUX_XLF"
        fi

        ;;

    mips32)
        echo Found a \"$machine\" configuration definition
        fixmakeargs="arch=$machine $fixmakeargs MIPS32 MIPS32LINUX"
        sicortex=yes
        if [ $papi = yes ]
         then
            fixmakeargs="$fixmakeargs MIPS32PAPI"
        fi
        ;;

    mips)
        echo Found a \"$machine\" configuration definition
        fixmakeargs="arch=$machine $fixmakeargs MIPS64 MIPS64LINUX"
        sicortex=yes
        if [ $papi = yes ]
         then
            fixmakeargs="$fixmakeargs MIPS64PAPI"
        fi
        ;;

    rs6000)
        echo Found a \"$machine\" configuration definition
        fixmakeargs="arch=$machine $fixmakeargs SP1"

        if [ $papi = yes ]
         then
            fixmakeargs="$fixmakeargs IBMPAPI"
        fi
        ;;

    alpha)
        if [ $papi = yes ]
         then
            fixmakeargs="$fixmakeargs ALPHAPAPI"
        fi
        echo Found a \"$machine\" configuration definition
        fixmakeargs="arch=$machine $fixmakeargs"
        fixmakeargs="COMPAQ_ALPHA $fixmakeargs"
        if [ $pdt_cxx_compiler = cxx ]
        then
            fixmakeargs="$fixmakeargs PDTALPHACXX"
        fi
        if [ $guidef90 = yes ]
        then
            f90loaded=`which f90 | sed -e 's/f90/../g' | grep "^no"`
            if [ "x$f90loaded" = "x" ]
            then
                    # f90 module has been loaded and which f90 returns a path
                extradir="`which f90 | sed s/f90/../`"
                echo "Found f90 in `which f90`"
            fi
            fixmakeargs="$fixmakeargs extradir=$extradir COMPAQ_GUIDEF90"
        fi
        ;;

    sgi4k | sgi8k | sgi32 | sgin32 | sgi64)
        echo Found a \"$machine\" configuration definition
        fixmakeargs="arch=$machine $fixmakeargs"
        if [ $c_compiler = cc ]
        then
            # Check CPU type for compiler optimization flags... (-mips2) (-mips4)
            if [ $machine = sgi4k ]
            then
                fixmakeargs="$fixmakeargs MIPS_CC"
                fixmakeargs="$fixmakeargs MIPSR4K"
                echo "NOTE: *** This is an SGI Challenge (R4K) ***"
            else
                fixmakeargs="$fixmakeargs MIPSR8K"
                echo "NOTE: *** This is an SGI POWER Challenge/Origin (R8K, R10K) ***"
            fi
        fi
        if [ $machine = sgin32 ]
        then
            fixmakeargs="$fixmakeargs ENABLEN32BIT"
        fi

        if [ $machine = sgi64 ]
        then
            fixmakeargs="$fixmakeargs ENABLE64BIT"
            if [ $papi = yes ]
            then
                if [ -f $papidir/lib/libpapi64.so -o -f $papidir/src/libpapi64.so ]
                then
                        fixmakeargs="$fixmakeargs SGI64PAPI"
                fi
            fi
        fi

        if [ $machine = sgi32 ]
        then
            fixmakeargs="$fixmakeargs ENABLE32BIT"
        fi

        # Check for sgimp xdev enviroment
        if [ $detectxdev = sgimp -o $machine = sgi8k -o $machine = sgin32 -o $machine = sgi64 ]
        then
            fixmakeargs="$fixmakeargs SGIMP"
            if [ $fortran_compiler = no ]
            then
                fixmakeargs="$fixmakeargs SGI_FORTRAN"
                fortran_compiler=yes
            fi

        fi
        ;;

    solaris2*)
        echo Found a \"$machine\" configuration definition
        fixmakeargs="arch=$machine SOL2 $fixmakeargs"
        if [ "$tauarch" = "solaris2-64" ]
        then
            fixmakeargs="$fixmakeargs SOLARIS64"
        fi

        if [ $papi = yes ]
         then
            fixmakeargs="$fixmakeargs SOL2PAPI"
        fi
        if [ $cxx_compiler = CC ]
        then
            sol2cc=yes
            fixmakeargs="$fixmakeargs SOL2CC"
            if [ $openmp = yes ]
            then
                fixmakeargs="$fixmakeargs SOL2CC_OPENMP"
            fi
            if [ $fortran_compiler = no ]
            then
                fixmakeargs="$fixmakeargs SUN_FORTRAN"
                fortran_compiler=yes
            fi
        fi
        ;;

    sun386i)
        echo Found a \"$machine\" configuration definition
        fixmakeargs="arch=$machine SUN386I $fixmakeargs"
        if [ $cxx_compiler = CC ]
        then
            fixmakeargs="$fixmakeargs SOL2CC"
            if [ $openmp = yes ]
            then
                fixmakeargs="$fixmakeargs SOL2CC_OPENMP"
            fi
            if [ $fortran_compiler = no ]
            then
                fixmakeargs="$fixmakeargs SUN_FORTRAN"
                fortran_compiler=yes
            fi
        fi
        ;;

    mic_linux)
        echo Found a \"$machine\" configuration definition
        fixmakeargs="arch=$machine $fixmakeargs"
        if [ $cxx_compiler = mpiicpc -o $cxx_compiler=icpc ]; then
            fixmakeargs="$fixmakeargs MMIC"
        fi
        ;;

    sunx86_64)
        echo Found a \"$machine\" configuration definition
        fixmakeargs="arch=$machine $fixmakeargs SUNX86_64"
        if [ $cxx_compiler = CC ] ; then
            fixmakeargs="$fixmakeargs SOL2CC"
            if [ $openmp = yes ] ; then
                fixmakeargs="$fixmakeargs SOL2CC_OPENMP"
            fi
            if [ $fortran_compiler = no ] ; then
                fixmakeargs="$fixmakeargs SUN_FORTRAN"
                fortran_compiler=yes
            fi
        fi
        ;;


    sun4)
        echo Found a \"$machine\" configuration definition
        fixmakeargs="arch=$machine $fixmakeargs"
        ;;

    c90)
        echo Found a \"$machine\" configuration definition
        fixmakeargs="arch=$machine C90 CRAY-ALLOCA $fixmakeargs"
        # Check for T3D xdev enviroment
        if [ $detectxdev = t3d ]
        then
            fixmakeargs="$fixmakeargs T3D"
        fi
        ;;

    t3e)
        echo Found a \"$machine\" configuration definition
        fixmakeargs="arch=$machine T3E CRAY-ALLOCA $fixmakeargs"
        if [ $fortran_compiler = no ]
        then
            fixmakeargs="$fixmakeargs CRAY_FORTRAN"
            fortran_compiler=yes
        fi
        ;;

    apple)
        if [ "x$unwind_lib_flag" = "x-lunwind" ] ; then
            unwind_inc="/usr/include"
            unwind_lib="/usr/lib/system"
            unwind_lib_flag="-framework System"
            echo "Using Apple's system provided libunwind umbrella framework..."
        fi
        fixmakeargs="arch=$machine  $fixmakeargs"
        ;;

    arm_linux)
        echo Found a \"$machine\" configuration definition
        fixmakeargs="arch=$machine $fixmakeargs"
        if [ $papi = yes ] ; then
            fixmakeargs="ARMLINUXPAPI $fixmakeargs"
        fi
        ;;

    arm_android)
        echo Found a \"$machine\" configuration definition
        if [ "x$android_sdk" = "x" ]; then
            echo "Error: No Android SDK specified. You may want to specify \"-android_sdk=<dir>\", \"-android_version=VERSION\", and \"-android_platform=VERSION\""
            exit 1
        fi

        fixmakeargs="arch=$machine ANDROID android_sdk=$android_sdk android_version=$android_version android_platform=$android_platform $fixmakeargs"
        if [ $papi = yes ] ; then
            fixmakeargs="ARMLINUXPAPI $fixmakeargs"
        fi
        ;;

    unknown)
        echo unknown architecture.
        fixmakeargs="arch=$machine $fixmakeargs"
        ;;

    default)
        #echo "fix make args $fixmakeargs"
        fixmakeargs="arch=$machine $fixmakeargs"
        ;;

    -*)
        echo "Invalid option \`$1'" 1>&2
        exit 1
        ;;

    *)
        echo No special modifications found for architecture \"$machine\"
        fixmakeargs="arch=$machine  $fixmakeargs"
        ;;
esac

if [ $machine = x86_64 -o $machine = i386_linux -o $machine = ia64 -o $machine = suni386 -o $machine = sunx86_64 ] ; then
    if [ $cxx_compiler = CC ] ; then
        suncc=yes
        fixmakeargs="$fixmakeargs SUNCC"
        if [ $openmp = yes ] ; then
            fixmakeargs="$fixmakeargs SUNCC_OPENMP"
        fi
        if [ $fortran_compiler = no ] ; then
            fixmakeargs="$fixmakeargs SUN_FORTRAN"
            fortran_compiler=yes
        fi
    fi
fi

if [ $tauarch = none ] ; then
    architecture=
    fixmakeargs="$fixmakeargs arch= "
else
    if [ "x$execprefix" = "xunknown" ] ; then
        architecture=$machine
    else
        architecture=$execprefix
    fi
fi




# write out .h file to define this architecture __easily__
echo \#define TAU_$machine > ./include/tauarch.h
echo \#define TAU_ARCH \"$machine\" >> ./include/tauarch.h


make_directories



#####################################################################
# Is LINUXTIMERS available?
#####################################################################

if [ $cxx_compiler = x86_64-w64-mingw32-g++ ] ; then
    linuxtimers=no
fi
if [ $linuxtimers = yes ] ; then
    echo "LINUX_TIMERS enabled"
    fixmakeargs="$fixmakeargs LINUXTIMERS"
fi

######################################################################
# Install libunwind if requested
######################################################################
libunwind=libunwind-1.3.1
libunwind=libunwind-1.8.1
libunwindtgz=$libunwind.tar.gz
libunwindurl=http://www.cs.uoregon.edu/research/paracomp/tau/tauprofile/dist/$libunwindtgz
if [ "x$unwind_dir" = "x" ]; then
  libunwinddir=${targetdir}/${architecture}
else
  libunwinddir=$unwind_dir
fi

libunwind_name=unwind
if [ $machine = mic_linux -o $craymic = yes ] ; then
  libunwind=libunwind-mic-1.1
  libunwindtgz=$libunwind.tgz
  libunwindurl=http://www.cs.uoregon.edu/research/paracomp/tau/tauprofile/dist/$libunwindtgz
fi

if [ $machine = riscv64 ]; then
  libunwind=libunwind-1.6.2
  libunwindtgz=$libunwind.tar.gz
  libunwindurl=http://www.cs.uoregon.edu/research/paracomp/tau/tauprofile/dist/$libunwindtgz
fi

if [ $machine = arm64_linux ] ; then
  libunwind=libunwind-1.3-rc1
  libunwindtgz=$libunwind.tar.gz
  libunwindurl=http://www.cs.uoregon.edu/research/paracomp/tau/tauprofile/dist/$libunwindtgz
fi

if [ "x$unwind_dir" != "x" -a -d "$unwind_dir" ] ; then
  if [ -r $unwind_dir/$libunwindtgz ]; then
    echo "NOTE: Copying $unwind_dir/$libunwindtgz "
    cp $unwind_dir/$libunwindtgz .
    download_unwind=yes
  fi
fi

if [ "x$download_unwind" = xyes ] ; then
  if [ $machine = craycnl ] ; then
    unwind_dir=${libunwinddir}/${libunwind}-${c_compiler}-${PE_ENV}
  else
     if [ ! -z ${internal_c_compiler} ]
     then
         unwind_dir=${libunwinddir}/${libunwind}-${internal_c_compiler}
     else
         unwind_dir=${libunwinddir}/${libunwind}-${c_compiler}
     fi
  fi
  prebfddir=`pwd`

  if [ -r "$unwind_dir/lib/lib${libunwind_name}.so" -o \
       -r "$unwind_dir/lib/lib${libunwind_name}.a" -o \
       -r "$unwind_dir/lib/lib${libunwind_name}.dylib" -o \
       -r "$unwind_dir/lib64/lib${libunwind_name}.so" -o \
       -r "$unwind_dir/lib64/lib${libunwind_name}.a" -o \
       -r "$unwind_dir/lib64/lib${libunwind_name}.dylib" ] ; then
    echo "Found: lib${libunwind_name}"
    echo "libunwind download already found, skipping"
  else
    echo "Not found in ${unwind_dir}: lib${libunwind_name}"
    # get the tar file
    if [ ! -f $libunwindtgz ] ; then
      download "$libunwindurl" "$libunwindtgz"
    fi

    # check the tar file
    if [ ! -f $libunwindtgz ] ; then
      echo "$libunwindtgz did not download.  Please download it manually from: "
      echo "$libunwindurl"
      echo "Please enter the directory containing the tarball: ${nnl}"
      read RESPONSE
      cp $RESPONSE/$libunwindtgz .
    else
      echo "expanding $libunwindtgz..."
      tar -xzf $libunwindtgz
      echo "removing $libunwindtgz..."
      rm $libunwindtgz
      if [ ${c_compiler} = mpc_cc -o ${c_compiler} = mpc_icc -o ${c_compiler} = hcc -o "x$PE_ENV" = "xCRAY" -o "x${unwind_gcc}" = "xyes" ]
      then
	echo "NOTE: Using gcc/g++ to compile libunwind"
        unwind_c_compiler=gcc
        unwind_cxx_compiler=g++
      else
	if [ ! -z ${internal_compiler} ]
	then
           unwind_c_compiler=${internal_c_compiler}
           unwind_cxx_compiler=${internal_compiler}
	else
	   unwind_c_compiler=${c_compiler}
           unwind_cxx_compiler=${cxx_compiler}
	fi
	echo "NOTE: Using unwind_c_compiler=$unwind_c_compiler and unwind_cxx_compiler=$unwind_cxx_compiler to compile libunwind"
      fi

      if [ $craymic = no -a $machine != mic_linux ]; then
        configresult=0
        makeresult=0
        libunwind_log=configure.log
        cd $libunwind
        echo "configuring $libunwind... (see `pwd`/${libunwind_log} for log)..."
        if [ "x$machine" = "xbgq" ] ; then
          ./configure --prefix=${unwind_dir} CC=${unwind_c_compiler} CXX=${unwind_cxx_compiler} --disable-shared > ${libunwind_log} 2>&1
          cp src/unwind/Resume.c tmp.c
          sed 's/_Unwind_Resume/_Unwind_Resume_other/' tmp.c > src/unwind/Resume.c
          rm tmp.c
        elif [ "$machine" = "craycnl" ] ; then
          if [ "x$CRAY_PE_USE_CLANG" != "x" ]; then
# use Cray PE clang compiler
           # unwind_c_compiler=clang
           # unwind_cxx_compiler=clang++
            unwind_c_compiler=${CRAY_PE_USE_CLANG}
            unwind_cxx_compiler=${CRAY_PE_USE_CLANG}++
          fi
          ./configure --prefix=${unwind_dir}  CC=${unwind_c_compiler} CXX=${unwind_cxx_compiler} CFLAGS=-fPIC CXXFLAGS=-fPIC --disable-shared --disable-minidebuginfo > ${libunwind_log} 2>&1
         else
          if [ "x$static_libunwind" = xyes ]; then
              ./configure --prefix=${unwind_dir} CC=${unwind_c_compiler} CXX=${unwind_cxx_compiler}  CFLAGS=-fPIC CXXFLAGS=-fPIC --disable-shared > ${libunwind_log}  2>&1
          else
              ./configure --prefix=${unwind_dir} CC=${unwind_c_compiler} CXX=${unwind_cxx_compiler} > ${libunwind_log}  2>&1
          fi
        fi
        configresult=$?
        if [ $configresult -eq 0 ] ; then
          libunwind_log=build.log
          echo "building ${libunwind}... (see `pwd`/${libunwind_log} for log)..."
          make  > $libunwind_log  2>&1
          makeresult=$?
          if [ $makeresult -eq 0 ] ; then
          libunwind_log=install.log
          echo "installing ${libunwind} to ${unwind_dir}... (see `pwd`/${libunwind_log} for log)..."
            make install > $libunwind_log  2>&1
          makeresult=$?
          else
          echo "libunwind build failed"
	  exit 1
          fi
        fi
        if [ $machine = arm64_linux ]; then
          configresult=0
          makeresult=0
          mv ../${libunwind}  ${unwind_dir}
        fi
        if [ $configresult -eq 0 ] && [ $makeresult -eq 0 ] ; then
          # Some systems install to lib64 instead of lib
          if [ -d "$unwind_dir/lib64" -a ! -d "$unwind_dir/lib" ] ; then
            tdir=$unwind_dir
            pwddir=${PWD}
            cd $tdir
            ln -s lib64 lib
            cd $pwddir
          fi
          echo "...Success."
        else
          echo "ERROR BUILDING $libunwind!"
          echo "Please check `pwd`/config.log or `pwd`/${libunwind_log}"
        fi
      else
        # We need to install $libunwind
        mv ${libunwind} ${unwind_dir}
        echo "NOTE: Moving ${libunwind} to ${unwind_dir}"
      fi
    fi
  fi
  if [ $unwind_dir != "/usr" ]; then
    unwind_inc="$unwind_dir/include"
    if [ "x$machine" = "xbgq" ] ; then
      unwind_lib="$unwind_dir/lib64"
    else
      unwind_lib="$unwind_dir/lib"
    fi
  fi
  cd $prebfddir
fi #unwind=download

if [ $ompt = yes -a $download_ompt = no ] ; then
#checking OMP compiler OMPT support
echo "Checking Compiler OMPT support : "
touch ompt_ver_test.cpp
cat <<EOF > ompt_ver_test.cpp
#include "omp-tools.h"
#include "omp.h"
#include "stdio.h"

#if !defined(VERTEST_OMPT_5_0)
#error "No OMPT Version was chosen"
#endif

void on_ompt_callback_mutex_acquire(
ompt_mutex_t kind,
unsigned int hint,
unsigned int impl,
ompt_wait_id_t wait_id,
const void *codeptr_ra)
{
    printf("Useless Callback\n");
}

int ompt_initialize(
ompt_function_lookup_t lookup,
int initial_device_num,
ompt_data_t* tool_data)
{
    ompt_set_callback_t ompt_set_callback = (ompt_set_callback_t) lookup("ompt_set_callback");
    int ret = ompt_set_callback(ompt_callback_mutex_acquire, (ompt_callback_t)on_ompt_callback_mutex_acquire);
    return 1;
}

void ompt_finalize(ompt_data_t* tool_data)
{
    return;
}

/* This function should probably check the version returned by omp_version - although clang 8.0 is returning 201611... */
ompt_start_tool_result_t * ompt_start_tool(
unsigned int omp_version,
const char *runtime_version)
{
    static ompt_start_tool_result_t result;
    result.initialize = &ompt_initialize;
    result.finalize = &ompt_finalize;
    result.tool_data.value = 0L;
    result.tool_data.ptr = NULL;
    return &result;
}

int main (int argc, char * argv[])
{
    #if defined(VERTEST_OMPT_5_0)
    printf("Testing Version 5.0\n");
    #endif /* VERTEST_OMPT_5_0 */
    #pragma omp parallel
    {
        printf ("Hi from thread %d\n", omp_get_thread_num());
    }
    return 0;
}
EOF

#find openmp flag
if [ "$sol2cc" = "yes" ]; then openmp_flag=-xopenmp; fi
if [ "$suncc" = "yes" ]; then openmp_flag=-xopenmp=parallel; fi
if [ "$compaq" = "yes" ]; then openmp_flag=-omp; fi
if [ "$ibmxlc" = "yes" ]; then openmp_flag=-qsmp=omp; fi
if [ "$open64" = "yes" ]; then openmp_flag=-mp; fi
if [ "$gnu" = "yes" ]; then openmp_flag=-fopenmp; fi
if [ "$llvm" = "yes" ]; then openmp_flag=-fopenmp; fi
if [ "$pgi" = "yes" ]; then openmp_flag=-mp=ompt; fi
if [ "$fujitsu" = "yes" ]; then openmp_flag=-kopenmp; fi
if [ "$default_fortran" = "cray" ]; then
  if [ "x$CRAY_PE_USE_CLANG" = "x" -a "x$PE_ENV" = "xCRAY" ]; then
    openmp_flag=-h#omp;
  else
    openmp_flag=-fopenmp;
  fi
fi

if [ "$intel" = "yes" ]; then openmp_flag=-qopenmp; fi

if [ "$ompt_dir" != "" ]; then
    if [ "$intel" = "yes" ]; then
        openmp_lib="-liomp5"
    else
        openmp_lib="-lomp"
    fi
    openmp_LINK="-L$omptlib"
    openmp_INC="-I$ompt_dir/include"
fi

#This deletion is here in case the file was left behind from a previous call to configure
rm -f ./ompt_ver_test
#checking for OMPT Version 5.0
echo -ne "\t Checking for version 5.0 ..."
echo $cxx_compiler $openmp_flag $openmp_INC $openmp_LINK $orig_useropt -DVERTEST_OMPT_5_0 ompt_ver_test.cpp -o ompt_ver_test $openmp_lib
$cxx_compiler $openmp_flag $openmp_INC $openmp_LINK $orig_useropt -DVERTEST_OMPT_5_0 ompt_ver_test.cpp -o ompt_ver_test $openmp_lib 1>/dev/null 2>&1
result=$?
if [ $machine != craycnl ] ; then
  LD_LIBRARY_PATH=$omptlib:$LD_LIBRARY_PATH ./ompt_ver_test 1>/dev/null 2>&1
  result=$?
fi
if [ $result = "0" ]; then
    echo " yes"
    ompt_compiler_support=yes
    download_ompt=no
    # Before we set this, let us check and see if the C compiler also supports OMPT
    $c_compiler $openmp_flag $openmp_INC $openmp_LINK $orig_useropt -DVERTEST_OMPT_5_0 ompt_ver_test.cpp -o ompt_ver_test $openmp_lib 1>/dev/null 2>&1
    result=$?
    if [ $result = "0" ]; then
      echo "TAU: Both C and C++ compilers support OMPT!"
    else
      echo "TAU: C++ compiler supports OMPT but C compiler does not!"
      fixmakeargs="$fixmakeargs TAU_C_COMPILER_NO_OMPT"
    fi
else
    echo " no"
fi
rm -f ./ompt_ver_test ./ompt_ver_test.cpp

if [ $ompt_compiler_support = no ] ; then
        echo -n "Compiler does not support OMPT ... "
        if [ $gnu = yes -o $llvm = yes -o $intel = yes ] ; then
            echo "Downloading LLVM-OpenMP"
            download_ompt=yes
        else
            echo "LLVM-OpenMP does not support detected compiler"
            echo "OpenMP support is only available with -opari for the specified compilers $c_compiler and $cxx_compiler."
            (>&2 echo 'ERROR: OMPT Requested, but there is no way to provide it.')
            exit 1;
        fi
elif [ "x$ompt_dir" != "x" ] ; then
    install_ompt=yes
fi
fi

######################################################################
# Install Scalable Observation System (SOS) if requested
######################################################################

if [ "x$download_ompt" = xyes -a $sos = yes ] ; then
  echo "SOS (Scalable Observation System) installation requested"
fi

if [ "x$download_legacy_ompt" = xyes -a $sos = yes ] ; then
  echo "SOS (Scalable Observation System) installation requested"
fi

######################################################################
# Install Clang/LLVM OpenMP runtime with OMPT if requested
######################################################################

if [ "x$download_ompt" = "xyes" -a "x$ompt_use_5_0_version" = "xyes" ] ; then
  download_ompt_5_0=yes
  ompt_version_selected=yes
fi

#Default to ompt 5.0
if [ "x$download_ompt" = "xyes" -a "x$ompt_version_selected" = "xno" ] ; then
  ompt_use_5_0_version=yes
  download_ompt_5_0=yes
fi

if [ $ompt = yes ]; then
  tauoptions="${tauoptions}-ompt"
  if [ "x$CRAY_CPU_TARGET" != "x" -a "x$PE_ENV" = "xINTEL" ] ; then
    fixmakeargs="$fixmakeargs INTEL_CRAY_MIC_KNL_OMPT"
  fi
fi

if [ "x$download_ompt_5_0" = "xyes" ] ; then
  libomp_oss=LLVM-openmp-8.0
  libomp_osstgz=${libomp_oss}.tar.gz
  libomp_ossurl=http://tau.uoregon.edu/${libomp_osstgz}
fi

libomp_oss_name=iomp5

if [ "${omptlib}" = "" ] ; then
    libomp_ossdir_shared=${targetdir}/${architecture}/lib/shared-disable
    libomp_ossdir_static=${targetdir}/${architecture}/lib/static-disable
else
    libomp_ossdir_shared=${omptlib}
    libomp_ossdir_static=${omptlib}
fi
libomp_ossinc=${targetdir}/include
libomp_cmake_instdir=${targetdir}/${architecture}

if [ ! -r ${libomp_ossdir_shared} ] ; then
  mkdir -p ${libomp_ossdir_shared}
fi
if [ ! -r ${libomp_ossdir_static} ] ; then
  mkdir -p ${libomp_ossdir_static}
fi
if [ ! -r ${libomp_ossinc} ] ; then
  mkdir -p ${libomp_ossinc}
fi

if [ "x$ompt_dir" != "x" -a -d "$ompt_dir" -a "$libomp_osstgz" != "" ] ; then
  if [ -r $ompt_dir/$libomp_osstgz ]; then
    echo "NOTE: Copying $ompt_dir/$libomp_osstgz"
    cp $ompt_dir/$libomp_osstgz .
  fi
fi

    ompt_c_compiler=$c_compiler
    ompt_cxx_compiler=$cxx_compiler
    if [ $gnu = yes ] ; then
      ompt_c_compiler=gcc
      ompt_cxx_compiler=g++
      libomp_oss_name=omp
    fi
    if [ "${architecture}" = "craycnl" ] ; then
      if [ "${intel}" = "yes" ] ; then
        ompt_c_compiler=icc
        ompt_cxx_compiler=icpc
        libomp_oss_name=omp
      fi
    fi

if [ "x$download_ompt" = "xyes" ] ; then
  install_ompt=yes
  if [ ! -f "${libomp_ossdir_shared}/lib${libomp_oss_name}.so" -o  "${libomp_ossdir_shared}/lib${libomp_oss_name}.so" -ot "${libomp_oss}"  ]; then
    preomptdir=`pwd`

    #check Cmake exists
    check_cmake 2 8 "OMPT support"
    result=$?
    if [ $result -eq 1 ]; then
      echo 'ERROR: cmake version 2.8 or higher required to build OMPT support.'
      exit 1;
    fi

    echo "Note: Building LLVM-OpenMP with ${ompt_c_compiler} and ${ompt_cxx_compiler}"

    if [ -r "${libomp_ossdir_static}/lib${libomp_oss_name}.so" -a ${libomp_ossdir_shared}/lib${libomp_oss_name}.so -ot ${libomp_oss} ] ; then
      echo "NOTE: ${libomp_ossdir_shared}/lib${libomp_oss_name}.so is older than ${libomp_oss} directory. Deleting and rebuilding."
      /bin/rm -rf ${libomp_oss}
    fi
    # get the tar file
    if [ ! -d $libomp_oss ]; then
    if [ ! -f $libomp_osstgz ]; then
    echo "downloading $libomp_osstgz..."
      download "$libomp_ossurl" "$libomp_osstgz"
    fi

    # check the tar file
    if [ ! -f $libomp_osstgz ] ; then
      echo "$libomp_osstgz did not download.  Please download it manually from: "
      echo "$libomp_ossurl"
      echo "Please enter the directory containing the tarball: ${nnl}"
      read RESPONSE
      cp $RESPONSE/$libomp_osstgz .
    else
      echo "expanding $libomp_osstgz..."
      tar -xzf $libomp_osstgz
      echo "removing $libomp_osstgz..."
      #rm $libomp_osstgz
    fi
    fi # ! -d $libomp_oss
      cd $libomp_oss
      libomp_oss_log=build.log
      specialflags="-DCMAKE_C_FLAGS=-fPIC -DCMAKE_CXX_FLAGS=-fPIC"
      if [ "x$machine" = "xmic_linux" ] ; then
        libomp_oss_log=build-mic.log
        specialflags="-DLIBOMP_ARCH=mic"
          fi

          # fix the expected path for bin
      if [ -r $ompt_dir/${libomp_oss_name}.so ]; then
        /bin/rm -f $ompt_dir/${libomp_oss_name}.so
      fi

      echo "building $libomp_oss (see `pwd`/build-${ompt_cxx_compiler}/${libomp_oss_log} for log)..."
          rm -rf build-${ompt_cxx_compiler}
          mkdir build-${ompt_cxx_compiler}
          cd build-${ompt_cxx_compiler}
      libomp_cmake_instdir=`pwd`
      if [ "x$cuda" == "xyes" ] ; then
        # It probably doesn't make sense to have target offload support.
            #cmake_cmd="cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER=${ompt_c_compiler} -DCMAKE_CXX_COMPILER=${ompt_cxx_compiler} -DCMAKE_INSTALL_PREFIX=${libomp_cmake_instdir} -DCLANG_OPENMP_NVPTX_DEFAULT_ARCH=sm_70 -DLIBOMPTARGET_ENABLE_DEBUG=on -DLIBOMPTARGET_NVPTX_COMPUTE_CAPABILITIES=35,60,70 ${specialflags} .. "
            cmake_cmd="cmake -DCMAKE_DISABLE_FIND_PACKAGE_CUDA=true -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER=${ompt_c_compiler} -DCMAKE_CXX_COMPILER=${ompt_cxx_compiler} -DCMAKE_INSTALL_PREFIX=${libomp_cmake_instdir} ${specialflags} .. "
      else
            cmake_cmd="cmake -DCMAKE_DISABLE_FIND_PACKAGE_CUDA=true -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER=${ompt_c_compiler} -DCMAKE_CXX_COMPILER=${ompt_cxx_compiler} -DCMAKE_INSTALL_PREFIX=${libomp_cmake_instdir} ${specialflags} .. "
      fi
      echo $cmake_cmd
      $cmake_cmd > $libomp_oss_log 2>&1
      result=$?
      if [ $result -eq 0 ] ; then
        echo make clean
            make clean
        echo make libomp-needed-headers
        make libomp-needed-headers >> $libomp_oss_log 2>&1
        echo make
            make >> $libomp_oss_log 2>&1
        result=$?
        if [ $result -eq 0 ] ; then
          echo make install
              make install >> $libomp_oss_log 2>&1
          # copy the header to our source area, anyway - we need it.
          echo cp ./include/*.h ${START_DIR}/include/.
          cp ./include/*.h ${START_DIR}/include/.
          libomp_ossdir_shared=${libomp_cmake_instdir}/lib
          libomp_ossdir_static=${libomp_cmake_instdir}/lib
          echo "...Success."
        else
          echo "ERROR BUILDING OMPT support!"
          echo "Please check `pwd`/${libomp_oss_log}"
                  exit 1;
        fi
      else
        echo "ERROR BUILDING OMPT support!"
        echo "Please check `pwd`/${libomp_oss_log}"
                exit 1;
      fi
    #fi
  else #Still need to copy the header to be sure it's not from the wrong version
      echo cp $libomp_oss/build-${ompt_cxx_compiler}/include/*.h ${START_DIR}/include/.
      cp $libomp_oss/build-${ompt_cxx_compiler}/include/*.h ${START_DIR}/include/.
  fi
  omptlib=${libomp_ossdir_shared}
  omptlibrary=-l${libomp_oss_name}
  cd $preomptdir
elif [ $ompt_compiler_support = no -o $install_ompt = yes ] ; then
  # not building OMPT, but...
  install_ompt=yes
  if [ "$ompt" = "yes" ] ; then
    # we need the ompt header for building TAU
    echo cp ${omptlib}/../include/${ompt_header} ${START_DIR}/include/.
    cp ${omptlib}/../include/${ompt_header} ${START_DIR}/include/.
  fi
elif [ $ompt_compiler_support = yes -a $install_ompt = no ] ; then
  # Compiler support OMPT, removes previously copied header
  rm -f ${START_DIR}/include/omp-tools.h
fi #ompt=download


if [ $machine = bgq -o $machine = bgp -o $machine = bgl ] ; then
    if [ $ompt = yes -o $xlsmp = yes ]; then
        comp=`which bgxlc_r`
        if [ "x$xlsmpdir" = "x" ]; then
            if [ -x $comp ]; then
                xlsmpdir=`echo $comp | sed -e 's@bgxlc_r@../../../../xlsmp@' `
                if [ ! -d $xlsmpdir ]; then
                    echo "NOTE: Could not locate the xlsmp directory using the location of bgxlc_r in your path."
                    echo "To enable ompt support in TAU using IBM XL compilers, please configure with -xlsmp=<dir> option to specify the full path to the xlsmp directory inside the compiler directory".
                    exit 1;
                fi
            fi
            if [ -d $xlsmpdir -a -r $xlsmpdir/bg/*/bglib64/ompt_full/libxllomp.a -a -r $xlsmpdir/bg/*/include/lomp/omp.h ]; then
                lompinc=`echo $xlsmpdir/bg/*/include `
                lomplib=`echo $xlsmpdir/bg/*/bglib64/ompt_full `
                echo "NOTE: Found LOMP include directory $lompinc"
                echo "NOTE: Found LOMP lib directory $lomplib"
                omptenabled=yes
                tauoptions="${tauoptions}-ompt"
                fixmakeargs="$fixmakeargs IBMOMPT lompinc=$lompinc lomplib=$lomplib"
            else
                echo "NOTE: Did not find lomp/omp.h or lib64/libxllomp.a in the xlsmp subdirectory. Not enabling OMPT support for IBM compilers."
            fi
        else
            echo "bgxlc_r not found in the path. Cannot test for OMPT support."
        fi
    fi
else # intel OMPT support
    if [ $ompt = yes ]; then
        omptlinking="-Wl,-rpath,$omptlib#$omptlibrary"
        echo "Using OMPT 5"
        fixmakeargs="$fixmakeargs TAU_OMPT_5_0 omptlib=$omptlinking TAU_USE_GPU"
    fi
fi


echo "***********************************************************************"

#####################################################################
# Compile/Link/Run checks
#####################################################################

if [ $machine = apple ] ; then
cat <<EOF > apple_mach_port.cpp
#include <Profile/Profiler.h>
#include <Profile/TauMetaData.h>
EOF
  echo "Checking: Mac OS X mach_port bug. Is it ok?... ${nnl}"
  cp ./include/TAU.h.default ./include/TAU.h
if $cxx_compiler -DTAU_DOT_H_LESS_HEADERS -DPROFILING_ON -I./include -c apple_mach_port.cpp  1> /dev/null 2>&1 ; then
    echo "yes"
  else
    echo "no ${nnl}"
    if $cxx_compiler -DTAU_DOT_H_LESS_HEADERS -DPROFILING_ON -DTAU_APPLE_MACH_PORT_BUG -I./include -c apple_mach_port.cpp   1> /dev/null 2>&1; then
      echo " - it is fixed by APPLE_MACH_PORT_BUG"
      fixmakeargs="$fixmakeargs APPLE_MACH_PORT_BUG"
    else
      echo " can't be fixed. TAU may not build."
    fi
  fi
  /bin/rm -f apple_mach_port.cpp apple_mach_port.o ./include/TAU.h
fi

#####################################################################
# MPI Checks
#####################################################################


# Next, check the properties of the MPI implementation.
if [ "x$mpiinc" != "x" -o "x$mpilib" != "x" -o $mpi = yes ] ; then
    #echo "MPI is specified"
    mpi=yes
    fixmakeargs="$fixmakeargs MPI"
    tauoptions="${tauoptions}-mpi"

    mpicompiler=$c_compiler

    # The Intel compiler doesn't give an error, but a warning
    # if [ "$machine" = "craycnl" -a "$intel" == "yes" ]
    if [ "$intel" = "yes" ]
    then
        mpicompiler=$c_compiler
        mpiwarnings="-Werror"
    fi
    if [ "x$llvm" = "xyes" ]
    then
        mpicompiler=$c_compiler
        mpiwarnings="-Werror"
    fi
    if [ $c_compiler = "xlc" -o $c_compiler = "xlc_r" ]
    then
        mpiwarnings="-qhalt=w"
    fi
    if [ $c_compiler = "clang" -o $c_compiler = "armclang" -o $c_compiler = "amdclang" ]
    then
        mpiwarnings="-Werror"
    fi
    if [ $c_compiler = "ncc" -o $c_compiler = "mpincc" ]
    then
        mpiwarnings="-Werror"
    fi
    if [ $c_compiler = "gcc" -a $machine = apple ]
    then
        mpiwarnings="-Werror"
    fi

  #echo "mpiinc is specified as: $mpiinc"
  if [ "x$mpiinc" != "x" ]
  then
      if [[ $mpiinc == \-I* ]] ; then
        mpi_include=$mpiinc
      else
        mpi_include=-I$mpiinc
      fi
  fi

  if [ "x$mpilib" != "x" ] ; then
      if [[ $mpilib == \-L* ]] ; then
        mpi_libpath=$mpilib
      else
        mpi_libpath=-L$mpilib
      fi
  else
        mpi_libpath=$mpilibargs
  fi


###setup wrapperlib###


    #Check for SGI MPI
    mpimptloc=`which mpiexec_mpt 2>/dev/null`
    hasMPT=$?
    if [ $hasMPT = 0 ] ; then
        echo "Found SGI MPT MPI"
        fixmakeargs="$fixmakeargs SGI_MPT_MPI"
    fi


    wrapperlib="-L$tautoplevel/$machine/lib -lTauMpi\$(TAU_CONFIG)"

    # Epilog
    if [ $epilog = yes -a $mpi = yes ] ; then
        if [ $sicortex = yes ] ; then
            wrapperlib="-L$epiloglibdir -Wl,-whole-archive -lelg.mpi -Wl,-no-whole-archive"
        else
            wrapperlib="-L$epiloglibdir -lelg.mpi"
        fi

        if [ $openmp = yes ] ; then
            if [ $sicortex = yes ] ; then
                wrapperlib="-L$epiloglibdir -Wl,-whole-archive -lelg.ompi -Wl,-no-whole-archive -Wl,-Bstatic#-lbfd#-liberty#-Wl,-Bdynamic"
            else
                wrapperlib="-L$epiloglibdir -lelg.ompi"
            fi
        fi
    fi

    # VampirTrace
    if [ $vampirtrace = yes ] ; then
        if [ $vampirtraceshow = yes ] ; then
            wrapperlib="\$(VAMPIRTRACEMPILIBS)"
        else
            vtfortranwrapper=""
            if [ -f $vampirtracedir/$vampirtracelibdir/libvt.fmpi.a ] ; then
                vtfortranwrapper="-lvt.fmpi"
            fi

            if [ -f $vampirtracedir/$vampirtracelibdir/libvt-fmpi.a ] ; then
                vtfortranwrapper="-lvt-fmpi"
            fi

            vtmpilib="-lvt.mpi"
            if [ -f "$vampirtracedir/$vampirtracelibdir/libvt-mpi.a" ] ; then
                vtmpilib="-lvt-mpi"
            fi

            vtompilib="-lvt.ompi"
            if [ -f "$vampirtracedir/$vampirtracelibdir/libvt-ompi.a" ] ; then
                vtompilib="-lvt-ompi"
            fi

            wrapperlib="-L$tautoplevel/$machine/lib -ltau\$(TAU_CONFIG) -L$vampirtracedir/$vampirtracelibdir $vtfortranwrapper $vtmpilib -lotf -lz"
            if [ $openmp = yes ] ; then
                # should we have a -lvt.fmpi here?
                fixmakeargs="useropt=$useropt extrashlibopts=$extrashlibopts $fixmakeargs"
                if [ $sicortex = yes ] ; then
                    wrapperlib="$sicortexlink"
                else
                    wrapperlib="-L$tautoplevel/$machine/lib -ltau\$(TAU_CONFIG) -L$vampirtracedir/$vampirtracelibdir -Wl,-whole-archive $vtfortranwrapper $vtompilib -lotf -Wl,-no-whole-archive -lz"
                fi
            fi
        fi
    fi

    if [ $scorep = yes ] ; then
        wrapperlib="\$(SCOREPMPILIBS)"
        if [ $mpi = yes ]; then
          wrapperlib="$wrapperlib -Wl,-rpath,\$(TAU_MPILIB_DIR)"
        fi
    fi

    if [ $perf = yes ] ; then
        wrapperlib="-L$\(PERFLIBDIR) \$(PERFLIBRARY)"
    fi


    mpilibargs="$wrapperlib $mpilibargs"
    wrapperC=$wrapperlib

    if [ "x$mpilib" != "x" ] ; then
        if [ -d $mpilib ] ; then
            #echo "Lib dir found. adding -L$mpilib to LIB"
            mpilibargs="$mpilibargs -L$mpilib"
            if [ -r $mpilib/libmpi_r.a -a -r $mpilib/libmpi.a ] ; then
                fixmakeargs="$fixmakeargs IBMMPI"
                ibmmpi=yes
            fi

            if [ -f $mpilib/libfmpi.a -o -f $mpilib/libfmpi.so ] ; then
                #echo "Fortran fmpi.* found"
                mpiflibargs="-L$mpilib -lfmpi $wrapperlib"
            else
                if [ $epilog = yes -a -f $epiloglibdir/libfmpi.a ] ; then
                    mpiflibargs="-lfmpi"
                    wrapperlib="$wrapperlib -lfmpi"
                fi
                if [ $epilog = yes -a -f $epiloglibdir/libelgfmpi.a ] ; then
                    mpiflibargs="-lelgfmpi"
                    wrapperlib="$wrapperlib -lelgfmpi"
                fi
                if [ $epilog = yes -a -f $epiloglibdir/libepk.ad.fmpi.a ] ; then
                    mpiflibargs="-lepk.ad.fmpi"
                    wrapperlib="$wrapperlib -lepk.ad.fmpi"
                fi
                # None of these options matter. It uses the include/Makefile options
                #echo "NOTE: ADDING -lfmpich to the Scalasca link line"
                if [ $epilog = yes -a -f $epiloglibdir/libcubew3.a ] ; then
                    wrapperlib="$wrapperlib -lcubew3"
                fi

                if [ -f $mpilib/libfmpich.cnk.a -o -f $mpilib/libfmpich.cnk.so ] ; then
                    mpiflibargs="-lfmpich.cnk"
                    #  wrapperlib="$wrapperlib -lfmpich.cnk"
                fi

                if [ -f $mpilib/libfmpich.a -o -f $mpilib/libfmpich.so ] ; then
                    #echo "Fortran fmpich.* found"
                    if [ $mpi2 = "yes" ] ; then
                        #mpich2, must not use -lfmpich
                        if [ -f /usr/lib/librt.a -a $catamount = no ] ; then
                            # mpich2 uses librt for aio_* routines
                            mpiflibargs="-L$mpilib $wrapperlib -lpthread -lrt"
                            mpilibargs="$mpilibargs -lpthread -lrt"
                        else
                            mpiflibargs="-L$mpilib $wrapperlib"
                        fi
                    else
                        #mpich1, must use -lfmpich
                        mpiflibargs="-L$mpilib -lfmpich $wrapperlib"
                    fi
                    # Does MPI_Init have weak bindings for Fortran (Infiniband MPI)
                    weakmpi=T
                    # by default it has a T for defined symbol in nm output */
            if [ -f $mpilib/libfmpich.a ] ; then
              weakmpi=`nm $mpilib/libfmpich.a | grep mpi_init | awk '{ print $2 ; }' | head -n 1 `
            else
              if [ -f $mpilib/libfmpich.so ] ; then
                weakmpi=`nm $mpilib/libfmpich.so | grep mpi_init | awk '{ print $2 ; }' | head -n 1 `
              fi
            fi
                    if [ "x$weakmpi" = "xW" ] ; then
                          echo "MPI has weak bindings for mpi_init Fortran bindings..."
                          fixmakeargs="$fixmakeargs WEAKMPIINIT"
                    fi
                else
                    if [ -f $mpilib/liblamf77mpi.a -o -f $mpilib/liblamf77mpi.so ] ; then
                        #echo "Fortran liblamf77mpi.* found"
                        mpiflibargs="-L$mpilib -llamf77mpi $wrapperlib"
                    else
                        if [ -f $mpilib/libmpi_f77.so ] ; then
                            #echo "Fortran libmpi_f77.* found"
                            mpiflibargs="-L$mpilib -lmpi_f77 $wrapperlib"
                        else
                            #echo "No Fortran interface found"
                            mpiflibargs="-g $wrapperlib $mpiflibargs  -L$mpilib"
                        fi
                    fi
                fi
            fi

            # Cases
            if [ -r $mpilib/libmpi.a -o -r $mpilib/libmpi.so -o -r $mpilib/libmpi.dylib ] ; then
                #echo "libmpi.* found!!"
                # OpenMPI
                mpilibrary_current=""
                if [ "x$mpilibrary" != "xno" ] ; then
                    # *CWL* - Set a temporary variable to hold any previously set value of mpilibrary
                    mpilibrary_current="$mpilibrary"
                fi

                # *CWL* - Work only on the temporary variable in this section
                if [ -r $mpilib/libmpi_cxx.so -o -r $mpilib/libmpi_cxx.dylib ] ; then
                    mpilibrary_current="$mpilibrary_current -lmpi -lmpi_cxx"
                fi

                if [ -r $mpilib/libmpi_f90.so -o -r $mpilib/libmpi_f90.dylib ] ; then
                    mpilibrary_current="$mpilibrary_current -lmpi_f90"
                fi

                if [ -r $mpilib/libmpi_f77.so -o -r $mpilib/libmpi_f77.dylib ] ; then
                    mpilibrary_current="$mpilibrary_current -lmpi_f77"
                fi

                # *CWL* - Propagate any changes to the temporary variable back to mpilibrary
                if [ "x$mpilibrary_current" != "x" ] ; then
                    mpilibrary="$mpilibrary_current"
                fi

                if [ "x$mpilibrary" = "xno" ] ; then
                    if [ $openmp = yes -o $pthread = yes ] ; then
                      if [ -r $mpilib/libmpi_mt.a -o -r $mpilib/libmpi_mt.so -o -r $mpilib/libmpi_mt.dylib ] ; then
                        mpilibrary=-lmpi_mt
                      else
                       mpilibrary=-lmpi
                      fi
                    else
                      mpilibrary=-lmpi
                    fi
                    echo "SETTING mpilibrary=$mpilibrary"
                    if [ -r $mpilib/libmpi++.so ] ; then
                        mpilibrary='-lmpi -lmpi++'
                        # SGI MPT
                    fi

                    if [ -r $mpilib/libopen-pal.a -a ! -r $mpilib/libmpi_cxx.so ]; then
                        if [ -r $mpilib/libmpi_cxx.a ]; then
                                LIBMPICXX='-lmpi_cxx'
                        fi

                        echo "NOTE: OpenMPI configured with --disable_shared"
                        mpilibrary="$LIBMPICXX -lmpi -lopen-rte -lopen-pal -ldl -lpthread -lutil"
                        # check for -libverbs, sometimes necessary with OpenMPI
                        if [ $machine != bgq -a $machine != craycnl ] ; then
                            if [ -r /usr/lib64/libibverbs.a -o -r /usr/lib64/libibverbs.so ] ; then
                                mpilibrary="$mpilibrary -libverbs"
                            fi
                        fi # BGQ has /usr/lib64/libibverbs.so but we do not want to link it.

                        # check for -libverbs, sometimes necessary with OpenMPI
                        if [ -r /usr/lib64/libnuma.a -o -r /usr/lib64/libnuma.so ] ; then
                            mpilibrary="$mpilibrary -lnuma"
                        fi

                        if [ -r $mpilib/libmpi_f90.a ] ; then
                            mpilibrary="$mpilibrary -lmpi_f90"
                        fi

                        if [ -r $mpilib/libmpi_f77.a ] ; then
                            mpilibrary="$mpilibrary -lmpi_f77"
                        fi
                    fi

                    # See if threaded MPI should be used on AIX
                    if [ $ibmxlc_r = yes -a -r $mpilib/libmpi_r.a ] ; then
                        mpilibrary="-lmpi_r $additional_mpilibs"
                    fi
                fi
                if [ -r $mpilib/libpmpi.a -o -r $mpilib/libpmpi.so ] ; then
                    #echo "libpmpi.* found "
                    mpilibargs="$mpilibargs -lpmpi $mpilibrary"
                    mpiflibargs="$mpiflibargs -lpmpi $mpilibrary"
                else
                    #echo "libpmpi.* not found -lmpi probably contains PMPI routines"
                    mpilibargs="$mpilibargs $mpilibrary"
                    mpiflibargs="$mpiflibargs $mpilibrary"
                fi
            else
                #echo "libmpi.* not found!! Checking for mpich.*..."
                if [ -r $mpilib/libmpich.a -o -r $mpilib/libpmpich.so -o -r $mpilib/libmpich.rts.a -o -r $mpilib/libmpich.cnk.a ] ; then
                    fixmakeargs="$fixmakeargs MPICH_IGNORE_CXX_SEEK"
                    if [ $mpi2 = yes ] ; then
                        fixmakeargs="$fixmakeargs MPICH2_MPI_INPLACE"
                    fi
                    #echo "MPICH found "
                    if [ "x$mpilibrary" = "xno" ] ; then
                        if [ -r $mpilib/libmpich.rts.a ] ; then
                             # For BG/L
                            mpilibrary="-lmpich.rts $additional_mpilibs"
                        else
                            if [ -r $mpilib/libmpich.cnk.a ]; then
                                # For BG/P
                                mpilibrary="-lmpich.cnk $additional_mpilibs"
                            else
                                if [ $machine = craycnl ]; then
                                          if [ -r $mpilib/libmpich_cray.a ] ; then
                                        mpilibrary="-lmpich_cray $additional_mpilibs"
                                    elif [ -r $mpilib/libmpich_intel.a ]; then
                                        mpilibrary="-lmpich_intel $additional_mpilibs"
                                    elif [ -r $mpilib/libmpich_gnu.a ]; then
                                        mpilibrary="-lmpich_gnu $additional_mpilibs"
                                    elif [ -r $mpilib/libmpich_pgi.a ]; then
                                        mpilibrary="-lmpich_pgi $additional_mpilibs"
                                    else
                                        if [ "$craymic" = "no" ]; then
                                          mpilibrary="-lmpich $additional_mpilibs"
                                        else
                                          mpilibrary="$additional_mpilibs"
                                        fi
                                        #echo "Craymic=$craymic "
                                    fi
                                    echo "Using mpilibrary=$mpilibrary"
                                else
                                    mpilibrary="-lmpich $additional_mpilibs"
                                fi
                            fi
                        fi
                    fi

                    if [ -r $mpilib/libpmpich++.a -o -r $mpilib/libpmpich++.so ] ; then
                        #echo "pmpich++ found"
                        mpilibrary="-lpmpich++ $mpilibrary"
                    fi


                    if [ $machine = craycnl ]; then
                        if [ -r $mpilib/libmpichcxx_cray.a ] ; then
                            mpilibrary="-lmpichcxx_cray $mpilibrary"
                        elif [ -r $mpilib/libmpichcxx_intel.a ]; then
                            mpilibrary="-lmpichcxx_intel $mpilibrary"
                        elif [ -r $mpilib/libmpichcxx_gnu.a ]; then
                            mpilibrary="-lmpichcxx_gnu $mpilibrary"
                        elif [ -r $mpilib/libmpichcxx_pgi.a ]; then
                            mpilibrary="-lmpichcxx_pgi $mpilibrary"
                        fi
                    else
                        if [ -r $mpilib/libmpichcxx.a -o -r $mpilib/libmpichcxx.so ] ; then
                            #echo "mpichcxx found"
                            mpilibrary="-lmpichcxx $mpilibrary"
                        fi
                    fi

                    if [ -r $mpilib/libpmpich.a -o -r $mpilib/libpmpich.so ] ; then
                        #echo "pmpich found"
                        mpilibargs="$mpilibargs -lpmpich $mpilibrary"
                        mpiflibargs="$mpiflibargs -lpmpich $mpilibrary"
                    else
                        #echo "pmpich not found"
                        mpilibargs="$mpilibargs $mpilibrary"
                        mpiflibargs="$mpiflibargs $mpilibrary"
                    fi

                    if [ -r /usr/lib/librt.a -o -r /usr/lib/librt.so ] ; then
                        #echo "librt found"
                        mpilibargs="$mpilibargs -lrt"
                        mpiflibargs="$mpiflibargs -lrt"
                    fi

                    if [ $machine != bgq -a $machine != craycnl ] ; then
                        if [ -r /usr/lib64/libibverbs.a -o -r /usr/lib64/libibverbs.so ] ; then
                            #echo "libibverbs found"
                            mpilibargs="$mpilibargs -libverbs"
                            mpiflibargs="$mpiflibargs -libverbs"
                        fi
                    fi

                    if [ -r $mpilib/libscdma.a -a -r $mpilib/libpmi.a ] ; then
                        # echo "add -lscdma -lpmi to the SiCortex link line"
                        mpilibargs="$mpilibargs -lscdma -lpmi"
                        mpiflibargs="$mpiflibargs -lscdma -lpmi"
                    fi
                else
                    if [ "x$mpilibrary" != "xno" ] ; then
                        mpilibargs="$mpilibargs $mpilibrary"
                        mpiflibargs="$mpiflibargs $mpilibrary"
                    fi
                fi
            fi

            # libmpi.* found. But libpmpi* not found.
            # both found.
            # libmpi.* not found - look for libmpich.*
            # For fortran libfmpich.* look for libpmpi

            # Check for libmpio.a
            if [ -r $mpilib/libmpio.a ]
            then
                mpilibargs="$mpilibargs -lmpio"
                mpiflibargs="$mpiflibargs -lmpio"
            fi

            # Special check for LAM MPI
            if [ -r $mpilib/liblam.a ]
            then
                echo "This looks like a LAM MPI installation. Adding -llam to link command line"

                if [ -r $mpilib/liblammpi++.a ] ; then
                    mpilibargs="$mpilibargs -llammpi++ -lmpi"
                    mpiflibargs="$mpiflibargs -llammpi++ -lmpi"
                fi
                mpilibargs="$mpilibargs -llam"
                mpiflibargs="$mpiflibargs -llam"
                if [ -f /usr/lib/libutil.a ]
                then
                    # This is a Linux box, add -lutil to the mpi libraries
                    echo "Adding -lutil to link command line for LAM MPI"
                    mpilibargs="$mpilibargs -lutil -ldl -lpthread"
                    mpiflibargs="$mpiflibargs -lutil -ldl -lpthread"
                fi
            fi
            # Special check for Intel MPI
            if [ -r $mpilib/libmpiif.a ]
            then
                echo "This looks like an Intel MPI installation."
                mpilibargs="$mpilibargs -lmpiif -ldl -lrt"
                mpiflibargs="$mpiflibargs -lmpiif -ldl -lrt"
            fi
            # Special check for HPMPI
            if [ -r $mpilib/libmpi_hmp.a ] ; then
                echo "This looks like an HP/MPI installation."
                mpilibargs="$mpilibargs -ldl"
                mpiflibargs="$mpiflibargs -ldl"
            fi
            # Special check for ChaMPIon/Pro
            if [ -r $mpilib/libcmpi.a ] ; then
                echo "This looks like a ChaMPIon/Pro installation."
                mpiextraargs=`cmpicc -v -echo | cut -c8- `
                mpilibargs="$mpilibargs $mpiextraargs"
                mpiflibargs="$mpiflibargs $mpiextraargs -lcmpi_fort"
            fi
            # Special check for VMI [NCSA] MPICH
            if [ -r $mpilib/libvmi.a ] ; then
                echo "This looks like a VMI MPICH installation. Adding -lvmi -lpthread -ldl to link command line"
                mpilibargs="$mpilibargs -lvmi -lpthread -ldl"
                mpiflibargs="$mpiflibargs -lvmi -lpthread -ldl"
            fi

            mpilibargs=`echo $mpilibargs | sed  -e 's/ /#/g'`
            mpiflibargs=`echo $mpiflibargs | sed  -e 's/ /#/g'`
            if [ $papipfm = yes -a "x$mpilib" = "x/usr/lib" ] ; then
                echo "NOTE: Adding -L<papidir> before -L<mpidir> to use PAPI's libpfm"
                mpilibargs="-L$papidir/$\(PAPISUBDIR\)#$mpilibargs"
                mpiflibargs="-L$papidir/$\(PAPISUBDIR\)#$mpiflibargs"
            fi
            if [ $mpi = yes -a $machine != ibm64 ]; then
              mpilibargs="$mpilibargs#-Wl,-rpath,\$(TAU_MPILIB_DIR)"
              mpiflibargs="$mpiflibargs#-Wl,-rpath,\$(TAU_MPILIB_DIR)"
            fi
            fixmakeargs="$fixmakeargs mpilibargs=$mpilibargs mpiflibargs=$mpiflibargs mpilib=$mpilib"
            mpilibargs_added_to_fixmakeargs=yes

            # now throw in non-wrapped MPI library lines
            wrapperF=$wrapperlib
            nowrapC=`echo $wrapperC | sed  -e 's/ /#/g'`
            nowrapF=`echo $wrapperF | sed  -e 's/ /#/g'`
            notaumpilibargs=`echo $mpilibargs | sed -e "s@$nowrapC@@"`
            notaumpiflibargs=`echo $mpiflibargs | sed -e "s@$nowrapF@@"`
            fixmakeargs="$fixmakeargs mpinowraplibargs=$notaumpilibargs mpinowrapflibargs=$notaumpiflibargs mpilib=$mpilib"

            # Check to see if the MPI library contains a _r
            mpithreadtest=`echo $mpilibrary | sed -e 's/_r//'`
            if [ "y$mpithreadtest" = "y$mpilibrary" ] ; then
                echo "NOTE: MPI library does not have a threaded _r suffix "
            else
                echo "NOTE: MPI library has a threaded _r suffix "
                fixmakeargs="$fixmakeargs MPI_R_SUFFIX"
            fi # mpi _r check

        fi # If -d mpilib
else # if mpilib is specified
    if [ $openmp = yes ] ; then
        if [ $epilog = yes ] ; then
            wrapperlib="-L$epiloglibdir#-lelg.omp#-ldl"
            if [ -f $epiloglibdir/libcubew3.a ] ; then
                wrapperlib="$wrapperlib#-lcubew3"
            fi
            fixmakeargs="extrashlibopts=$wrapperlib $fixmakeargs"
        elif [ $vampirtrace = yes ] ; then
            wrapperlib="-L$vampirtracedir/$vampirtracelibdir#-Wl,-whole-archive#-lvt.omp#-lotf#-Wl,-no-whole-archive#-lz#-ldl"
            fixmakeargs="extrashlibopts=$wrapperlib $fixmakeargs"
        fi
    else
        if [ $epilog = yes ] ; then
            wrapperlib="-L$epiloglibdir#-lelg.ser#-ldl"
            if [ -f $epiloglibdir/libcubew3.a ] ; then
                wrapperlib="$wrapperlib#-lcubew3"
            fi
            fixmakeargs="extrashlibopts=$wrapperlib $fixmakeargs"
        elif [ $vampirtrace = yes ] ; then
            wrapperlib="-L$vampirtracedir/$vampirtracelibdir#-lvt#-lotf#-lz#-Wl,-Bstatic#-lbfd#-liberty#-Wl,-Bdynamic#-ldl"
            fixmakeargs="extrashlibopts=$wrapperlib $fixmakeargs"
        fi
    fi
fi

  mpi_libraries=""
  if [ "x$mpilibrary" != "xno" ] ; then
      mpi_libraries=$mpilibrary
  fi
  mpi_include="$mpi_include $mpiincargs $mpi_libpath $mpi_libraries"
  mpi_include=`echo $mpi_include | sed  -e 's/#/ /g'`


if [ $machine = sgi64 -a $cxx_compiler = CC ]
then
    orig_useropt="$orig_useropt -64"
fi

$echo "Checking for thread-safe MPI interface... ${nnl}"
if [ "x$c_compiler" = "xicx" ]; then
  mpiwarnings=""
fi
#echo "$mpicompiler tools/src/tests/mpi_thread_test.c $mpiwarnings $orig_useropt $mpi_include"
$mpicompiler tools/src/tests/mpi_thread_test.c $mpiwarnings $orig_useropt $mpi_include
if $mpicompiler tools/src/tests/mpi_thread_test.c $mpiwarnings $orig_useropt $mpi_include  1> /dev/null 2>&1 ; then
    echo "yes"
    fixmakeargs="$fixmakeargs MPI_THREADED"
else
    echo "no"
fi


$echo "Checking for MPI 3 const interface... ${nnl}"
if $mpicompiler tools/src/tests/mpi3const.c $mpiwarnings $orig_useropt $mpi_include  1> /dev/null 2>&1 ; then
    if [ "x$PE_ENV" = "xCRAY" -a "x$CRAY_MPICH_BASEDIR" != "x" ]; then
      echo "yes"
      fixmakeargs="$fixmakeargs MPICH3_CONST"
    else
      echo "no"
    fi
else
  if $mpicompiler tools/src/tests/mpi3const.c $mpiwarnings $orig_useropt $mpi_include  -DTAU_MPICH3_CONST=const 1> /dev/null 2>&1 ; then
    echo "yes"
    fixmakeargs="$fixmakeargs MPICH3_CONST"
  else
    if [ "x$PE_ENV" = "xCRAY" -a "x$CRAY_MPICH_BASEDIR" != "x" ]; then
      echo "yes"
      fixmakeargs="$fixmakeargs MPICH3_CONST"
    else
      echo "no"
    fi
  fi
fi

if [ "x$PE_ENV" = "xCRAY" -a "x$CRAY_MPICH_DIR" = "x" -a "x$CRAY_MPICH_PREFIX" != "x" ]; then
  CRAY_MPICH_DIR=$CRAY_MPICH_PREFIX
fi

if [ "x$PE_ENV" = "xCRAY" -a "x$CRAY_MPICH_DIR" != "x" -a -r $mpiinc/cray_version.h ]; then
  if [ -r $CRAY_MPICH_DIR/lib/libmpi_cray.a ]; then
    echo "Found libmpi_cray.a"
    fixmakeargs="$fixmakeargs CRAY_MPICH_LIBMPI_CRAY"
  else
    if [ -r $CRAY_MPICH_DIR/lib/libmpich_crayclang.a ]; then
      echo "Found libmpich_crayclang.a"
      fixmakeargs="$fixmakeargs CRAY_MPICH_LIBMPICH_CRAYCLANG"
    fi
  fi
fi

$echo "Checking for OpenMPI 3 const interface..... ${nnl}"
if $mpicompiler tools/src/tests/openmpi3const.c $mpiwarnings $orig_useropt $mpi_include -DTAU_MPICH3_CONST=const 1> /dev/null 2>&1 ; then
# MPICH requires the const keyword
  echo "yes"
  fixmakeargs="$fixmakeargs OPENMPI3_CONST"
else
  echo "no"
fi

$echo "Checking for MPI_Type_hindexed const interface... ${nnl}"
if $mpicompiler tools/src/tests/mpihindex1.c $mpiwarnings $orig_useropt $mpi_include  1> /dev/null 2>&1 ; then
  echo "not needed"
else
  echo "yes"
  if $mpicompiler tools/src/tests/mpihindex2.c $mpiwarnings $orig_useropt $mpi_include 1> /dev/null 2>&1 ; then
    echo "Using const in MPI_Type_hindexed interface "
      fixmakeargs="$fixmakeargs TAU_MPI_HINDEX_CONST"
  fi
fi
rm -f mpihindex1.c mpihindex2.c mpihindex1.o mpihindex2.o


if [ -d "$mpiinc/ulm" ] ; then
    echo "LA-MPI detected, working around broken MPI_Request_c2f()"
    fixmakeargs="$fixmakeargs LAMPI"
fi

$echo "Checking for MPI-2 interface... ${nnl}"
if $mpicompiler tools/src/tests/mpi2.c  $mpiwarnings $orig_useropt $mpi_include 1> /dev/null 2>&1 ; then
    echo "yes"
    if [ -r $mpilib/libmpi.so -a -r $mpilib/libmpi++.so -a -r $mpilib/libsma.so -a -r $mpilib/libxmpi.so ] ; then
        # Do not enable this for SGI Altix. Their MPI2 is not complete
        # SGI MPI2 is ok now. We can enable it. Perhaps...
        useropt="$useropt#-DTAU_MPI_F_STATUSES_IGNORE_ABSENT"
        fixmakeargs="$fixmakeargs MPI2 useropt=$useropt"
        echo "SGI MPI detected. Disabling MPI2 wrappers for SGI MPI"
        fixmakeargs="$fixmakeargs SGI_MPT_MPI"
    elif [ -r $mpilib/libfmpi.so -a -r $mpilib/libmpio.so  -a -r $mpilib/libtvscampi.so ] ; then
        # Scali does not use MPI2 PMPI interface properly.
        echo "Scali MPI detected. Disabling MPI2 wrappers for Scali MPI"
    elif [ -r $mpilib/liblam.a -o -r $mpilib/liblam.la ] ; then
        # LAM MPI's MPI2 PMPI interface is broken.
        echo "LAM MPI detected. Disabling MPI2 wrappers for LAM MPI"
    elif [ "$mpilibrary" = "-lscmpi" ] ; then
        # SiCortex's MPI2 interface is broken.
        echo "SiCortex detected. Disabling MPI2 wrappers"
        fixmakeargs="$fixmakeargs MPICH_IGNORE_CXX_SEEK"
    elif [ "$tauarch" = "bgp" -o "$tauarch" = "bgq" ] ; then
        echo "BGP MPI detected. Disabling MPI2 wrappers"
        fixmakeargs="$fixmakeargs MPICH_IGNORE_CXX_SEEK"
    else
        fixmakeargs="$fixmakeargs MPI2"
        mpi2=yes
    fi
else
    echo "no"
fi


if [ $mpi2 = yes ] ; then

    if [ $mpit = yes ]; then
      $echo "Checking for MPI-T  interface... ${nnl}"
      if $mpicompiler tools/src/tests/mpit.c $mpiwarnings $orig_useropt $mpi_include  1> /dev/null 2>&1 ; then
              echo "yes"
          fixmakeargs="$fixmakeargs MPI_T"
      else
          echo "no"
      fi
    fi

    $echo "Checking for MPI-2 Grequest interface... ${nnl}"
    if $mpicompiler tools/src/tests/mpi2grequest.c $mpiwarnings $orig_useropt $mpi_include  1> /dev/null 2>&1 ; then
        echo "yes"
        fixmakeargs="$fixmakeargs MPIGREQUEST"
    else
        echo "no"
    fi

    $echo "Checking for MPI-2 MPIO_Request interface... ${nnl}"
    if [ -f $mpilib/liblam.a -o -f $mpilib/liblammpi++.a ] ; then
        if $mpicompiler tools/src/tests/mpi2iorequest.c $mpiwarnings $orig_useropt $mpi_include  1> /dev/null 2>&1 ; then
            echo "yes"
            fixmakeargs="$fixmakeargs MPIOREQUEST"
            mpiio=yes
        else
            echo "no"
        fi
    else
        echo "no"
    fi

    $echo "Checking for MPI_Datarep_conversion_function interface... ${nnl}"
    if $mpicompiler tools/src/tests/mpi2datarep.c $mpiwarnings $orig_useropt $mpi_include  1> /dev/null 2>&1 ; then
        echo "yes"
        fixmakeargs="$fixmakeargs MPIDATAREP"
    else
        echo "no"
    fi

    $echo "Checking for Comm_create_errhandler interface... ${nnl}"
    if $mpicompiler tools/src/tests/mpi2errhandler.c $mpiwarnings $orig_useropt $mpi_include  1> /dev/null 2>&1 ; then
        echo "yes"
        fixmakeargs="$fixmakeargs MPIERRHANDLER"
    else
        echo "no"
    fi

    $echo "Checking if MPI_Info_set takes const char * instead of char * args... ${nnl}"
    if $mpicompiler tools/src/tests/mpi2hpconst.c $mpiwarnings $orig_useropt $mpi_include  1> /dev/null 2>&1 ; then
        echo "yes"
        fixmakeargs="$fixmakeargs MPICONSTCHAR"
    else
        echo "no"
    fi
fi

if $mpicompiler tools/src/tests/mpi_f_statuses_ignore.c $mpiwarnings $orig_useropt $mpi_include  1> /dev/null 2>&1 ; then
    echo "MPI F_STATUSES_IGNORE types are defined..."
else
    echo "MPI F_STATUSES_IGNORE types are NOT defined..."
    fixmakeargs="$fixmakeargs MPI_F_STATUSES_IGNORE_ABSENT"
fi

if $mpicompiler tools/src/tests/mpi2attr.c $mpiwarnings $orig_useropt $mpi_include  1> /dev/null 2>&1 ; then
    echo "MPI-2 MPI*attr_functions are defined..."
    fixmakeargs="$fixmakeargs MPIATTR"
fi

if $mpicompiler tools/src/tests/mpi2file.c $mpiwarnings $orig_useropt $mpi_include  1> /dev/null 2>&1 ; then
    echo "MPI-2 MPI_File functions are defined..."
    fixmakeargs="$fixmakeargs MPIFILE"
else
    echo "MPI-2 MPI_File functions are NOT defined..."
    #echo $mpicompiler tools/src/tests/mpi2file.c $mpiwarnings $orig_useropt $mpi_include
fi

if $mpicompiler tools/src/tests/mpi2typeex.c $mpiwarnings $orig_useropt $mpi_include  1> /dev/null 2>&1 ; then
    echo "MPI-2 MPI_Type_dup and MPI_Exscan are defined..."
    fixmakeargs="$fixmakeargs MPITYPEEX"
fi

if $mpicompiler tools/src/tests/mpi2add.c $mpiwarnings $orig_useropt $mpi_include  1> /dev/null 2>&1 ; then
    echo "MPI-2 MPI_Add_error* functions are defined..."
    fixmakeargs="$fixmakeargs MPIADDERROR"
fi

if $mpicompiler tools/src/tests/mpistatus.c $mpiwarnings $orig_useropt $mpi_include  1> /dev/null 2>&1 ; then
    echo "MPI_Status f2c/c2f found..."
else
	# In mvapich2 3.0a MPI_Status_f2c is only available as PMPI_Status_f2c. We need to check if -c works
	# if linking fails.
    if $mpicompiler -c tools/src/tests/mpistatus.c $mpiwarnings $orig_useropt $mpiinc  1> /dev/null 2>&1 ; then
      echo "TAU: Using PMPI_Status_f2c/c2f instead of MPI_Status_f2c/c2f"
      fixmakeargs="$fixmakeargs USE_PMPI_STATUS_F2C"
    else
      echo "MPI_Status c2f/f2c functions are needed"
      fixmakeargs="$fixmakeargs MPINEEDSTATUSCONV"
    fi
fi

/bin/rm -rf tools/src/tests/*.o tools/src/tests/a.out

else
    if [ $mpi = yes -a $machine != ibm64 ]; then
      mpilibargs="$mpilibargs#-Wl,-rpath,\$(TAU_MPILIB_DIR)"
      mpiflibargs="$mpiflibargs#-Wl,-rpath,\$(TAU_MPILIB_DIR)"
    fi
    fixmakeargs="$fixmakeargs mpiincargs=$mpiincargs mpilibargs=$mpilibargs mpiflibargs=$mpiflibargs mpilib=$mpilib"
    mpilibargs_added_to_fixmakeargs=yes
fi



if [ $mpi = yes ] ; then
# Checking if we need to add -lmpifort here:
if [ $mpi = yes -a $mpilibargs_added_to_fixmakeargs = yes ]; then
  if [ $cxx_compiler = icpx -a -r $mpilib/libmpifort.so ]; then
    mpilibargs="${mpilibargs}#-lmpifort"
    fixmakeargs="$fixmakeargs mpilibargs=${mpilibargs}"
  fi
fi

# Special magic for xt3 and craycnl
if [ $mpi = yes -a $mpilibargs_added_to_fixmakeargs = no ] ; then
    if [ $epilog = no -a $vampirtrace = no -a $scorep = no ]; then
        # On Crays, Pathscale compilers require  a -lmpichcxx for C and Fortran
        if [ $pathscale = yes ] ; then
            mpilibargs="-L$tautoplevel/$machine/lib#-lTauMpi\$(TAU_CONFIG)#-lmpichcxx"
        else
            mpilibargs="-L$tautoplevel/$machine/lib#-lTauMpi\$(TAU_CONFIG)"
        fi
        fixmakeargs="$fixmakeargs mpilibargs=$mpilibargs mpiflibargs=$mpiflibargs"
    else
        mpilibargs=""
        mpiflibargs=$mpilibargs
        fixmakeargs="$fixmakeargs mpilibargs=$mpilibargs mpiflibargs=$mpiflibargs"
    fi
fi

fi #mpi=yes

#####################################################################
# Test for C++ string library
#####################################################################
$echo "Checking for standard C++ library <string>... ${nnl}"
cat <<EOF > /tmp/tau$$.C
#include <string>
#include <vector>
#include <list>
#include <map>
using std::string;
using std::vector;
using std::pair;
using std::list;
using std::map;
void foo(void) { string s1; return; }
EOF

if $cxx_compiler -c /tmp/tau$$.C 1> /dev/null 2>&1 ; then
    echo "yes"
    stdcxxlib=yes
else
    echo "no (using <bstring.h> instead)"
fi
rm -f /tmp/tau$$.C tau$$.o

#####################################################################
# Test for explicit strsignal support in string library (TauInit.cpp)
#####################################################################
$echo "Check if valid to declare strsignal with <string.h>... ${nnl}"
cat <<EOF > /tmp/tau$$.cpp
#include <string.h>
extern "C" char *strsignal(int sig);
EOF

if $cxx_compiler -c /tmp/tau$$.cpp 1> /dev/null 2>&1 ; then
    echo "yes"
    strsignal_ok=yes
    fixmakeargs="$fixmakeargs TAU_STRSIGNAL_OK"
else
    echo "no"
fi
rm -f /tmp/tau$$.cpp tau$$.o



#####################################################################
# Check Runtime type info
#####################################################################
cat <<EOF > get_typeinfo1.C
#include <typeinfo>
using std::type_info;
#ifdef __GNUC__
#include <cxxabi.h>
using namespace std;
#endif /* GNUC */

EOF

$echo "Checking if C++ compiler supports Std Runtime Type Information... ${nnl}"
if $cxx_compiler $orig_useropt -c get_typeinfo1.C 1> /dev/null 2>&1 ; then
    echo "yes"
else
    echo "no"
    cat <<EOF > get_typeinfo2.C
#include <typeinfo.h>
EOF

$echo "Checking if C++ compiler supports Runtime Type Information (RTTI)... ${nnl}"
if $cxx_compiler $orig_useropt -c get_typeinfo2.C 1> /dev/null 2>&1 ; then
    echo "yes"
    fixmakeargs="$fixmakeargs RTTI"
else
    echo "no"
    fixmakeargs="$fixmakeargs NORTTI"
fi
fi
rm -f get_typeinfo1.C get_typeinfo2.C get_typeinfo*.o *.ti *.ii



#####################################################################
# Check if the open call accepts O_LARGEFILE
#####################################################################
cat << EOF > f1.c
      #define  _LARGEFILE64_SOURCE    1
      #include <sys/types.h>
      #include <sys/stat.h>
      #include <fcntl.h>

      int main(int argc, char** argv) {
         int file = open ("asdf", O_LARGEFILE, 0600);
      }
EOF


$echo "Checking if open takes O_LARGEFILE... ${nnl}"
if $c_compiler $orig_useropt -c f1.c 1> /dev/null 2>&1 ; then
    fixmakeargs="$fixmakeargs TAU_LARGEFILE"
    echo "yes"
else
    echo "no"
fi
/bin/rm -f f1.o f1.c


##########################################################################
# Check if weak symbols are supported
##########################################################################
cat <<EOF > tau_weak_test.c
#pragma weak omp_get_thread_num=tau_omp_get_thread_num
int tau_omp_get_thread_num() {
  return 0;
}
EOF

$echo "Checking for weak symbols... ${nnl}"
if $c_compiler $orig_useropt tau_weak_test.c -c 1> /dev/null 2>&1 ; then
    weakfound=`nm tau_weak_test.o |  awk '{ print $2 ; }' | head -n 1`
    if [ "x$weakfound" = "xW" ] ; then
        echo "yes"
        fixmakeargs="$fixmakeargs TAU_WEAK_SUPPORTED"
    else
        echo "no"
    fi
else
    echo "no"
fi
/bin/rm -f tau_weak_test.o tau_weak_test.c
##########################################################################

######################################################################
# Install asmdex if we are building for Android
######################################################################
asmdextarball=asmdex-r1707.tar.bz2
asmdexurl=http://www.cs.uoregon.edu/research/paracomp/tau/tauprofile/dist/$asmdextarball

if [ "x$machine" = "xarm_android" ]; then
    if [ -r "$targetdir/$architecture/lib/asmdex.jar" ]; then
        echo "Found: $targetdir/$architecture/lib/asmdex.jar"
        echo "asmdex.jar already found, skipping"
    else
        echo "asmdex.jar doesn't found, trying to build one..."
        # Check for ant before doing anything
        if ! which ant >/dev/null 2>&1 ; then
          echo "ERROR: Apache ant not found in your path.  Can't build asmdex."
          exit 199
        fi
        tmpdir="`pwd`/.tmp_asmdex_$$"
        mkdir -p "$tmpdir"
        cd "$tmpdir"
        if [ "x$asmdex" = "xdownload" ]; then
            download "$asmdexurl" "$asmdextarball"
            if [ ! -f $asmdextarball ]; then
                echo "$asmdextarball did not download. Please download it manually from:"
                echo "$asmdexurl"
                echo "Please enter the directory containing asmdex: ${nnl}"
                read RESPONSE
                cp -r $RESPONSE .
                cd `basename $RESPONSE`
            else
                tar jxf $asmdextarball
                for p in $START_DIR/tools/src/android/dexInjector/asmdex-*.patch; do
                    patch -p0 < $p
                done
                cd asmdex
            fi
        else
            if [ ! -d "$asmdex" ]; then
                echo "$asmdex is not a valid directory."
                echo "Please enter the directory containing asmdex: ${nnl}"
                read RESPONSE
                cp -r $RESPONSE .
                cd `basename $RESPONSE`
            else
                cp -r $asmdex .
                cd `basename $asmdex`
            fi
        fi

        ant -f build-main.xml jar
        mkdir -p $targetdir/$architecture/lib
        cp asmdex.jar $targetdir/$architecture/lib
        cd ../..
        rm -rf "$tmpdir"
    fi
fi
######################################################################
# Install PIN if requested
######################################################################
PINURL_x86_64=http://software.intel.com/sites/landingpage/pintool/downloads/pin-3.2-81205-gcc-linux.tar.gz
if [ "x$pindir" = "xdownload" ]; then
  if [ $machine = craycnl -o $machine = x86_64 -o $machine = i386_linux ]; then
    currdir=${PWD}
    cd $targetdir/$machine;
    if [ ! -L $targetdir/$machine/pin ] ; then
# symlink does not exist, download PIN
      wget $PINURL_x86_64; tar zxf pin-*.tar.gz; /bin/rm -f pin-*.tar.gz; ln -s pin-* pin;
    fi
    pindir=`readlink -f pin`; cd $currdir;
  fi
fi

if [ $pin = yes -a "x$pindir" != "x" ]; then
    fixmakeargs="$fixmakeargs PIN pindir=$pindir"
    tauoptions="${tauoptions}-pin"
fi

######################################################################
# Install SOS if requested
######################################################################
#sostgz=sos.tgz
#sostgz=sos_021318.tgz
sostgz=sos_flow.tar.gz
sosdepstgz=sosdeps.tgz
#sosdepsurl=http://tau.uoregon.edu/$sosdepstgz
sosdepsurl=http://www.cs.uoregon.edu/research/paracomp/tau/tauprofile/dist/sos/$sosdepstgz
sosurl=http://www.cs.uoregon.edu/research/paracomp/tau/tauprofile/dist/sos/$sostgz
#sosurl=http://tau.uoregon.edu/$sostgz
#sosurl=https://github.com/cdwdirect/sos_flow.git
sosmaindir=sos
sosrepo=sos_flow_master


echo "$sosdir"
if [ "x$sos" = "xyes" ] ; then
  if [ "x$sosdir" = xdownload ] ; then
    echo "Install SOS with communication $sos_comm"
    p=`pwd`
    cd $targetdir/$architecture/;
    installsosdeps "$sosmaindir" "$sosdepsurl" "$sosdepstgz";
    download "$sosurl" "$sostgz" ;
    tar zxf "$sostgz";
    rm -f "$sostgz" ;
    installsos "$sosrepo"
    #cd $targetdir/$architecture/; installsosdeps "$sosmaindir" "$sosdepsurl" "$sosdepstgz"; clone "$sosurl" "$sosrepo"; installsos "$sosrepo"
    cd $p
  else
    if [ ! -d "$sosdir" ] ; then
      echo "Error: Cannot access SOS directory $sosdir"
      exit 1
    fi
  fi
fi
######################################################################
# Install ZLIB if requested
######################################################################
zlibs=zlib-1.3.1
zlibtgz=$zlibs.tar.gz
zliburl=https://www.zlib.net/$zlibtgz
build_zlib() {
  if [ "x$zlibdir" = xdownload ]; then
    zlibdir="$targetdir/$architecture/$zlibs"
  fi
  if [ ! -d "$zlibdir" ]; then
    echo "Building zlib..."
    download $zliburl $zlibtgz
    tar xf $zlibtgz
    cd $zlibs
    #./configure --prefix=$zlibdir
    CC=$c_compiler ./configure --prefix=$zlibdir
    echo "Installing in $zlibdir"
#    sed -e 's/gcc/ncc/g' Makefile > Makefile~
#    mv Makefile~ Makefile
    make
    make install
    cd ..
    echo "Built zlib"
  else
    echo "Found zlib installation"
  fi
}

echo "$zlibdir"
if [ "x$zlib" = "xno" ]; then
  echo "zlib disabled."
else
if [ "x$zlibdir" = xdownload ] ; then
  echo "Install zlibdir"
  build_zlib
fi
  zlibinclude="-I$zlibdir/include#-I$zlibdir/include/extra"
  zliblink="-L$zlibdir/lib#-L$zlibdir/lib64#-Wl\,-rpath\,$zlibdir/lib#-Wl\,-rpath\,$zlibdir/lib64#-L$zlibdir/lib"
  zliblibs="-lz"
  fixmakeargs="$fixmakeargs zlibinclude=$zlibinclude zliblink=$zliblink zliblibs=$zliblibs"
fi

######################################################################
# Install BFD if requested
######################################################################
# If binutils-2.36 doesn't build, please comment the 2.36 line and use
# the binutils 2.23.2 instead. If you are using C++ lamda functions in
# your code, you probably need binutils 2.36 to demangle C++ names.
binutils=binutils-2.23.2
binutils=binutils-2.36
binutils=binutils-2.40
binutilstgz=$binutils.tar.gz
binutilsurl=http://www.cs.uoregon.edu/research/paracomp/tau/tauprofile/dist/$binutilstgz

if [ $machine = nec-sx-aurora ]; then
  binutils=binutils-2.23.2
  binutilstgz=$binutils.tar.gz
  binutilsurl=http://www.cs.uoregon.edu/research/paracomp/tau/tauprofile/dist/$binutilstgz
fi

if [ "x$bfddir" = xnone ]; then
  echo "BFD disabled."
  turnbfdoff=yes
fi

if [ "x$bfd" = "xyes" -a ! "$turnbfdoff" = yes ] ; then
  bfdtar=""
  prebfddir="`pwd`"
  if [ ! -e "$bfddir/lib" ] && [ -f "$bfddir/$binutilstgz" ]; then
    #if we do not have an install already at this location but we do have a valid tar file, build with the tar file here
    bfdtar="yes"
  fi

  if [ "x$bfddir" = xdownload ] || [ "x$bfdtar" = xyes ] ; then
    if [ "x$bfddir" = xdownload ]; then
      #only change the bfddir if we are downloading
      bfddir="$targetdir/$architecture/$binutils"
      if [ $machine = craycnl -a $craymic = yes ]; then
        bfddir="$targetdir/$architecture/$binutils-mic"
      fi
    fi
    if [ -r "$bfddir/lib/libbfd.a" ] ; then
      echo "Found: $bfddir/lib/libbfd.a"
      echo "BFD download already found, skipping"
    else
      if [ ! "x$bfdtar" = xyes ]; then
        #only download if we're set to download, otherwise we'll just build in the directory with the tarfile
        tempdir="`pwd`/.tmp_bfdbuild_$$"
        if [ -d /dev/shm -a -w /dev/shm ] ; then
          # some linux distributions set noexec flag when mount tmpfs on /dev/shm
          # make sure we can exec under /dev/shm
          execheck="/dev/shm/tau_$$"
          touch $execheck
          chmod +x $execheck
          if [ -x $execheck ]; then
            tempdir="/dev/shm/tau_bfdbuild_$$"
          fi
          rm -f $execheck
        fi
        mkdir -p "$tempdir"
        cd "$tempdir"
        download "$binutilsurl" "$binutilstgz"
        if [ $? != 0 -a "x$execheck" != "x" ]; then
          echo "Couldn't write to /dev/shm. Trying local directory."
          cd $prebfddir
          tempdir="`pwd`/.tmp_bfdbuild_$$"
          mkdir -p "$tempdir"
          cd "$tempdir"
          download "$binutilsurl" "$binutilstgz"
        fi

        if [ ! -f "$binutilstgz" ] ; then
          echo "$binutilstgz did not download.  Please download it manually from: "
          echo "$binutilsurl"
          echo "Please enter the directory containing the tarball: ${nnl}"
          read RESPONSE
          cp "$RESPONSE/$binutilstgz" .
        fi
      else # bfdtar = yes
        cd "$bfddir"
        tempdir="$bfddir"
        # TAU uses -prefix=bfddir while configuring BFD. So, it must be set to TAU's dir
        bfddir=$targetdir/$architecture/$binutils
      fi

      # Extract binutils archive
      tar -xzf $binutilstgz
      cd $binutils

      # Disable packages that don't build without dependencies on Texinfo
      # and that are not required to use BFD in TAU
      BINUTILS_DISABLED_PKGS="--disable-gas --disable-gprof --disable-gprofng"

      # Configure binutils
      echo "Configuring binutils (see $tempdir/$binutils/tau_configure.log for details)..."
      case $machine in
      bgp)
        ./configure \
            CFLAGS=-fPIC CXXFLAGS=-fPIC \
            CC=/bgsys/drivers/ppcfloor/gnu-linux/bin/powerpc-bgp-linux-gcc \
            CXX=/bgsys/drivers/ppcfloor/gnu-linux/bin/powerpc-bgp-linux-g++ \
            --prefix=$bfddir \
            --disable-nls --disable-werror $BINUTILS_DISABLED_PKGS --enable-shared > tau_configure.log 2>&1
        err=$?
        ;;
      bgq)
        ./configure \
            CFLAGS=-fPIC CXXFLAGS=-fPIC \
            CC=/bgsys/drivers/ppcfloor/gnu-linux/bin/powerpc64-bgq-linux-gcc \
            CXX=/bgsys/drivers/ppcfloor/gnu-linux/bin/powerpc64-bgq-linux-g++ \
            --prefix=$bfddir \
            --disable-nls --disable-werror $BINUTILS_DISABLED_PKGS --enable-shared > tau_configure.log 2>&1
        err=$?
        ;;
      rs6000)
        ./configure \
            CFLAGS=-fPIC CXXFLAGS=-fPIC \
            --prefix=$bfddir \
            --disable-nls --disable-werror --disable-largefile $BINUTILS_DISABLED_PKGS --enable-shared > tau_configure.log 2>&1
        err=$?
        ;;
      ibm64)
        ./configure \
            CFLAGS="-maix64 -fPIC" CXXFLAGS="-maix64 -fPIC" AR="/usr/bin/ar -X64 "\
            --prefix=$bfddir \
            --disable-nls --disable-werror --disable-largefile $BINUTILS_DISABLED_PKGS --enable-shared > tau_configure.log 2>&1
        err=$?
        ;;

      arm_android)
        patch -p1 <$START_DIR/android.binutils-2.23.2.patch && \
        ./configure \
            CFLAGS=-fPIC CXXFLAGS=-fPIC \
            --host=$host \
            --prefix=$bfddir \
            --disable-nls --disable-werror  --disable-largefile $BINUTILS_DISABLED_PKGS --enable-shared  > tau_configure.log 2>&1
        err=$?
        ;;
      mic_linux)
        # Note: may need to search other paths than just /usr/linux-k1om-*
        k1om_bin="`ls -1d /usr/linux-k1om-* | sort -r | head -1`/bin"
        export PATH=$k1om_bin:$PATH
        ./configure \
            CFLAGS=-fPIC CXXFLAGS=-fPIC \
            --host=x86_64-k1om-linux \
            --prefix=$bfddir \
            --disable-nls --disable-werror $BINUTILS_DISABLED_PKGS --enable-shared > tau_configure.log 2>&1
        err=$?
        ;;
      apple)
        export CFLAGS="-Wno-error=unused-value -Wno-error=deprecated-declarations -fPIC"
        export CXXFLAGS="-Wno-error=unused-value -Wno-error=deprecated-declarations -fPIC"
        ./configure --prefix=$bfddir --disable-nls --disable-werror $BINUTILS_DISABLED_PKGS --enable-shared > tau_configure.log 2>&1
        err=$?
        ;;
      sparc64fx)
        fccpxpath=`which fccpx | sed 's/\/bin\/fccpx$//'`
        if [ -r $fccpxpath/util/bin/sparc64-unknown-linux-gnu-ar ]; then
          echo "NOTE: Using ar from $fccpxpath/util/bin/sparc64-unknown-linux-gnu-ar"
          export PATH=$fccpxpath/util/bin:$PATH
        fi
        ./configure \
            CFLAGS="-fPIC -Xg" CXXFLAGS="-fPIC -Xg" \
                CC=$fccpxpath/bin/fccpx \
                CXX=$fccpxpath/bin/FCCpx \
                AR=sparc64-unknown-linux-gnu-ar \
                --host=sparc64-unknown-linux-gnu \
                    --prefix=$bfddir \
                --disable-nls --disable-werror $BINUTILS_DISABLED_PKGS --enable-shared > tau_configure.log 2>&1
        err=$?
        ;;
      craycnl)
        unset CC CXX
        if [ $craymic = yes ]; then
          # Note: may need to search other paths than just /usr/linux-k1om-*
          k1om_bin="`ls -1d /usr/linux-k1om-* | sort -r | head -1`/bin"
          export PATH=$k1om_bin:$PATH
          ./configure \
            CFLAGS='-xMIC-AVX512 -fPIC' CXXFLAGS='-xMIC-AVX512 -fPIC' \
            --host=x86_64-k1om-linux \
            --prefix=$bfddir \
            --disable-nls --disable-werror $BINUTILS_DISABLED_PKGS --enable-shared > tau_configure.log 2>&1
          err=$?
        else
          ./configure \
            CFLAGS=-fPIC CXXFLAGS=-fPIC \
            --prefix=$bfddir \
            --disable-nls --disable-werror $BINUTILS_DISABLED_PKGS --enable-shared > tau_configure.log 2>&1
          err=$?
        fi
        ;;
      nec-sx-aurora)
        echo "Building BFD for nec-sx-aurora. Checking for zlib."
        # We need zlib built from source code.
        if [ "x$zlibdir" != xdownload ]; then
          echo "STANDARD INSTALLATION"
          zliburl=https://www.zlib.net/zlib-1.2.11.tar.gz
          download $zliburl zlib-1.2.11.tar.gz
          tar xf zlib-1.2.11.tar.gz; cd ./zlib-1.2.11; ./configure --prefix=$bfddir; sed -e 's/gcc/ncc/g' Makefile > Makefile~; mv Makefile~ Makefile; make; make install; cd ..
          echo "Built zlib"
        elif [ -d $bfddir/../zlib-1.2.11 ]; then
          echo "Found zlib-1.2.11"
        elif [ -d $zlibdir ]; then
          echo "Found zlib-1.2.11"
        fi

        ./configure CC=ncc CXX=nc++ --prefix=$bfddir --disable-nls --disable-werror \
        CFLAGS="-fPIC -Wno-error" CXXFLAGS="-fPIC -Wno-error" $BINUTILS_DISABLED_PKGS --enable-shared > tau_configure.log 2>&1
        err=$?
        ;;
      *)
        #echo $c_compiler
        base_c_compiler="`basename "$c_compiler"`"
        base_env_c_compiler="`basename "$CC"`"

        if [ "$base_c_compiler" = mpc_cc ]
        then
           ./configure \
            CFLAGS="-fPIC -Wno-error" CXXFLAGS="-fPIC -Wno-error" \
            CC=$mpc_c_compiler CXX=$mpc_cxx_compiler \
            --prefix=$bfddir \
            --disable-nls --disable-werror $BINUTILS_DISABLED_PKGS --enable-shared > tau_configure.log 2>&1
            err=$?
        else
          if [ "$base_c_compiler" = pgcc ] || [ "$base_env_c_compiler" = pgcc ]
          then
            ./configure \
              CFLAGS="-fPIC -noswitcherror" CXX=pgc++ CXXFLAGS=-fPIC \
              --prefix=$bfddir \
              --disable-nls --disable-werror $BINUTILS_DISABLED_PKGS --enable-shared > tau_configure.log 2>&1
              err=$?
          else
            ./configure \
              CFLAGS=-fPIC CXXFLAGS=-fPIC \
              --prefix=$bfddir \
              --disable-nls --disable-werror $BINUTILS_DISABLED_PKGS --enable-shared > tau_configure.log 2>&1
              err=$?
          fi
        fi
        ;;
      esac

      # Build and install binutils if configure successful
      if [ $err -ne 0 ] ; then
        echo "Error: BFD configuration failed. Cannot proceed with -bfd=download"
        echo "Error: Please try editing configure and use binutils-2.23.2 instead of 2.36."
        exit 1
      else
        echo "Building (see $tempdir/$binutils/tau_build.log for details)..."
        make -j8 > tau_build.log 2>&1
        err=$?
        if [ $err -ne 0 ] ; then
          echo "Error: BFD build failed. Cannot proceed with -bfd=download"
          exit 1
        fi
        echo "Installing (see $tempdir/$binutils/tau_build.log for details)..."
        make install > tau_install.log 2>&1
        err=$?
        if [ $err -ne 0 ] ; then
          echo "Error: BFD installation failed. Cannot proceed with -bfd=download"
          exit 1
        fi
      fi

      # Some systems use lib64 instead of lib
      if [ -d "$bfddir/lib64" -a ! -d "$bfddir/lib" ] ; then
        pushd "$bfddir"
        ln -s lib64 lib
        popd
      fi

      # Install additional headers
      cp bfd/*.h "$bfddir/include"
      cp -R include/* "$bfddir/include"

      # Install additional libraries
      if [ -r libiberty/libiberty.a ] ; then
        cp libiberty/libiberty.a "$bfddir/lib"
      fi
      if [ -r opcodes/libopcodes.a ] ; then
        cp opcodes/libopcodes.a "$bfddir/lib"
      fi

      # Fix bfd.h
      sed -e 's/#if !defined PACKAGE && !defined PACKAGE_VERSION/#if 0/' "$bfddir/include/bfd.h" > "$tempdir/bfd.h"
      mv "$tempdir/bfd.h" "$bfddir/include"

      # Fix library paths on snow leopard
      if [ -r "$bfddir/lib/x86_64/libiberty.a" ] ; then
        cp "$bfddir/lib/x86_64/libiberty.a" "$bfddir/lib/libiberty.a"
      fi

      if [ ! "x$bfdtar" = xyes ] ; then
        #If we downloaded go back and remove the tempdir, otherwise just go back
        cd ../..
        rm -rf "$tempdir"
      fi
      cd "$prebfddir"

    fi # -r $bfddir/lib/libbfd.a
  fi # $bfddir = download || bfdtar = yes

  # One way or another, we now have a working BFD at $bfddir or we give up
  if [ ! -d $bfddir ] ; then
    echo "Error: Cannot access BFD directory $bfddir"
    exit 1
  fi
  if [ "$pin" = "no" ]; then
    bfdinclude="-I$bfddir/include#-I$bfddir/include/extra"
    fixmakeargs="$fixmakeargs bfdinclude=$bfdinclude"
    bfdinclude="-I$bfddir/include -I$bfddir/include/extra"
    bfdlink="-L$bfddir/lib -L$bfddir/lib64 -Wl\,-rpath\,$bfddir/lib -Wl\,-rpath\,$bfddir/lib64 -L$zlibdir/lib"
  fi

  # The libintl.dylib library is in /opt/local/lib... is that everywhere?
  if [ "x$machine" = "xapple" ] ; then
    bfdlink="-L$bfddir/lib"
# do not use lib64 for BFD on Mac.
    bfdlink="$bfdlink -L/opt/local/lib"
    fixmakeargs="$fixmakeargs bfdlink=-L$bfddir/lib#-L$bfddir/lib64#-L/opt/local/lib"
  else
    if [ "$pin" = "no" ]; then
      fixmakeargs="$fixmakeargs bfdlink=-L$bfddir/lib#-L$bfddir/lib64#-Wl\,-rpath\,$bfddir/lib#-Wl\,-rpath\,$bfddir/lib64"
    fi
  fi
else
  fixmakeargs="$fixmakeargs bfdinclude= bfdlink= "
fi # bfd = yes && turnbfdoff != yes
bfdlink="-L$bfddir/lib -L$bfddir/lib64 -Wl,-rpath,$bfddir/lib -Wl,-rpath,$bfddir/lib64"

if [ $machine = ibm64 ]; then
  if [ "x$bfddir" = "x" -o "x$bfddir" = "x/opt/freeware" -o "x$bfddir" = "x/opt/freeware/" ]; then
    echo "Using /opt/freeware/lib64 for bfd"
    bfdlink="-L/opt/freeware/lib64#/opt/freeware/lib64/libbfd.a#-liberty#-lintl#-lz"
    fixmakeargs="$fixmakeargs bfdlink=$bfdlink"
  fi
fi




######################################################################
# Check for BFD
######################################################################
cat <<EOF > conftest_bfd.c
#define PACKAGE TAU
#define PACKAGE_VERSION 2.28.1
#include <bfd.h>
//#ifdef __APPLE__
//#include <crt_externs.h>
//char *** environ(void) { return (*_NSGetEnviron()); }
//#endif

void foo() {
   bfd_init();
   bfd *BfdImage = bfd_openr("/proc/self/exe", 0);
   if (!BfdImage) {}
   return;
}
EOF
$echo "Checking for bfd.h... ${nnl}"
if $c_compiler $orig_useropt -c conftest_bfd.c $bfdinclude 1> /dev/null 2>&1 ; then
    if [ "$turnbfdoff" = "yes" ] ; then
        # *CWL* this is for systems where BFD cannot be supported because
        # of whatever reasons. An example is the Cray XE6 (hopper) where
        # the Intel environment introduces a conflict between its own
        # stdio.h and libiberty.h
        echo "no"
    else
        if [ "$pin" = "no" ]; then
          fixmakeargs="$fixmakeargs TAU_BFD"
          echo "yes"
        fi
    fi
else
    echo "no"
fi
/bin/rm -f conftest_bfd.o conftest_bfd.c


######################################################################

######################################################################
# Check for ELF-BFD
######################################################################
cat <<EOF > conftest_elf_bfd.c
#define PACKAGE TAU
#define PACKAGE_VERSION 2.28.1
#include <stddef.h>
#include <bfd.h>
#include <elf-bfd.h>

void foo() {
    elf_symbol_type * symbol_type = NULL;
    (void)symbol_type;
    return;
}
EOF
$echo "Checking for elf-bfd.h... ${nnl}"
if $c_compiler $orig_useropt -c conftest_elf_bfd.c $bfdinclude 1> /dev/null 2>&1 ; then
    if [ "$turnbfdoff" = "yes" ] ; then
        echo "no"
    else
        if [ "$pin" = "no" ]; then
          fixmakeargs="$fixmakeargs TAU_ELF_BFD"
          echo "yes"
        fi
    fi
else
    echo "no"
fi
/bin/rm -f conftest_elf_bfd.o conftest_elf_bfd.c


######################################################################


######################################################################
# Check what is needed for linking with BFD
######################################################################
cat <<EOF > conftest_bfd.c
#ifdef MPC_MODULE_MPC_Config
#include <mpc.h>
#endif
#define PACKAGE TAU
#define PACKAGE_VERSION 2.28.1
#include <bfd.h>
int main(int argc, char **argv) {
   bfd_init();
   bfd *BfdImage = bfd_openr("/proc/self/exe", 0);
   if (!BfdImage) {}
   return 0;
}
EOF
$echo "Checking for linking dependencies for libbfd... ${nnl}"

BFDLIBS="-lbfd"

if [ $c_compiler = mpiicc ]; then
  BFDLIBS="-lbfd -liberty -ldl -lz"
fi
if [ $machine = apple ]; then
  BFDLIBS="-lbfd -liberty -lintl -ldl -lz"
fi

bfd_worked="no"
if $c_compiler $orig_useropt -o conftest_bfd conftest_bfd.c $bfdinclude $bfdlink $BFDLIBS 1> /dev/null 2>&1 ; then
    bfd_worked="yes"
else
    BFDLIBS="-lbfd -liberty "  # -lz is not always available (e.g., BGQ)
    if $c_compiler $orig_useropt -o conftest_bfd conftest_bfd.c $bfdinclude $bfdlink $BFDLIBS 1> /dev/null 2>&1 ; then
        bfd_worked="yes"
    else
        BFDLIBS="-lbfd -liberty -lz" # Next, check with -lz.
        if $c_compiler $orig_useropt -o conftest_bfd conftest_bfd.c $bfdinclude $bfdlink $BFDLIBS 1> /dev/null 2>&1 ; then
            bfd_worked="yes"
        else
          BFDLIBS="-lbfd -liberty -lz -ldl" # Next, check with -lz -ldl.
          if $c_compiler $orig_useropt -o conftest_bfd conftest_bfd.c $bfdinclude $bfdlink $BFDLIBS 1> /dev/null 2>&1 ; then
            bfd_worked="yes"
          else
            BFDLIBS="-lbfd -liberty -lintl"
            if $c_compiler $orig_useropt -o conftest_bfd conftest_bfd.c $bfdinclude $bfdlink $BFDLIBS 1> /dev/null 2>&1 ; then
                bfd_worked="yes"
            else
                BFDLIBS="-lbfd -liberty -lintl -lz"
                if $c_compiler $orig_useropt -o conftest_bfd conftest_bfd.c $bfdinclude $bfdlink $BFDLIBS 1> /dev/null 2>&1 ; then
                    bfd_worked="yes"
                else
                  BFDLIBS="-lbfd -liberty -lintl -lz -ldl"
                  if $c_compiler $orig_useropt -o conftest_bfd conftest_bfd.c $bfdinclude $bfdlink $BFDLIBS 1> /dev/null 2>&1 ; then
                    bfd_worked="yes"
                  else
                    BFDLIBS="-lbfd -lsframe -liberty -lz -ldl"
                    if $c_compiler $orig_useropt -o conftest_bfd conftest_bfd.c $bfdinclude $bfdlink $BFDLIBS 1> /dev/null 2>&1 ; then
                      bfd_worked="yes"
                    else
                      BFDLIBS="-lbfd -lsframe -liberty -lintl -lz -ldl"
                      if $c_compiler $orig_useropt -o conftest_bfd conftest_bfd.c $bfdinclude $bfdlink $BFDLIBS 1> /dev/null  2>&1 ; then
                        bfd_worked="yes"
                      else
                        BFDLIBS="-lbfd -lz"
# Mac BFD
                        if $c_compiler $orig_useropt -o conftest_bfd conftest_bfd.c $bfdinclude $bfdlink $BFDLIBS 1> /dev/null 2>&1 ; then
                          bfd_worked="yes"
                        else
                          BFDLIBS="-lbfd -lintl"
                          if $c_compiler $orig_useropt -o conftest_bfd conftest_bfd.c $bfdinclude $bfdlink $BFDLIBS 1> /dev/null 2>&1 ; then
                            bfd_worked="yes"
 		  	  fi
                        fi
                      fi
                    fi
                  fi
                fi
            fi
          fi
        fi
    fi
fi
if [ "$pin" = "yes" ]; then
   BFDLIBS=""
fi
if [ $bfd_worked = "yes" -a $machine = bgq ] ; then
    BFDLIBS=`echo $BFDLIBS | sed -e 's@-liberty@${TAU_BFD_DIR}/lib/libiberty.a@g'`
    fixmakeargs="$fixmakeargs bfddir=$bfddir"
fi

if [ $cxx_compiler = x86_64-w64-mingw32-g++ ]; then
    BFDLIBS="$BFDLIBS -lpsapi"
fi

if [ $bfd_worked = "no" -a "$machine" != "apple" -a $pin = no ] ; then
  if [ "$machine" != "ibm64" ]; then
    echo "failed. Using -lbfd -liberty -lz."
    echo ""
    echo ""
    echo "VERY IMPORTANT NOTE: Please configure TAU with -bfd=download. If a firewall prevents wget from accessing this binutils tarball"
    echo "Please download http://tau.uoregon.edu/ext.tgz and untar it in this TAU directory and please use -bfd=download."
    echo "***********************************************************************"
    echo ""
    echo ""
    BFDLIBS="-lbfd -liberty -lz"
    if [ $tauarch = i386_linux ]; then
        BFDLIBS=""
    fi
  fi
else
    echo $BFDLIBS
fi

BFDLIBS=`echo $BFDLIBS | sed -e 's/ /#/g'`

# *CWL* - We do not want to use the discovered libraries if we are
#         using the Cray compilers *and* fail to find the preset
#         directories. The explanation for this can be found in
#         the script section for craycnl.
if [ "x$PE_ENV" = xCRAY -a "x$bfddir" = x ] ; then
    echo "WARNING: Failed to find BFD path for CCE compiler. Implicit BFD support disabled."
else
    fixmakeargs="$fixmakeargs bfdlibs=$BFDLIBS"
fi

/bin/rm -f conftest_bfd.o conftest_bfd.c conftest_bfd
######################################################################

######################################################################
# Check for demangle.h
######################################################################
cat <<EOF > conftest_demangle.c
#include <demangle.h>
void foo() {
   char *bar;
   bar = cplus_demangle(0, DMGL_PARAMS | DMGL_ANSI | DMGL_VERBOSE | DMGL_TYPES);
   return;
}
EOF
$echo "Checking for demangle.h... ${nnl}"
demangle=""
if $c_compiler $orig_useropt -c conftest_demangle.c $bfdinclude 1> /dev/null 2>&1 ; then
    demangle="-DHAVE_GNU_DEMANGLE"
    fixmakeargs="$fixmakeargs TAU_DEMANGLE"
    echo "yes"
else
    echo "no"
fi

/bin/rm -f conftest_demangle.o conftest_demangle.c


######################################################################
# Check if BFD can be linked into a shared library
######################################################################
cat <<EOF > conftest_bfd.c
#define PACKAGE TAU
#define PACKAGE_VERSION 2.28.1
#include <bfd.h>
#  if defined(HAVE_GNU_DEMANGLE) && HAVE_GNU_DEMANGLE
#    include <demangle.h>
#  endif /* HAVE_GNU_DEMANGLE */

#ifdef __APPLE__
#include <crt_externs.h>
char *** environ(void) { return (*_NSGetEnviron()); }
#endif
void foo() {
   bfd_init();
   bfd *BfdImage = bfd_openr("/proc/self/exe", 0);
   if (!BfdImage) {}

#if defined(HAVE_GNU_DEMANGLE) && HAVE_GNU_DEMANGLE
      cplus_demangle(NULL, DMGL_PARAMS | DMGL_ANSI
                        | DMGL_VERBOSE | DMGL_TYPES);
#endif /* HAVE_GNU_DEMANGLE */

   return;
}
EOF
$echo "Checking for shared library compatible libbfd... ${nnl}"
if [ $c_compiler = xlc -o $c_compiler = xlc_r -o $c_compiler = mpixlc -o $c_compiler = mpixlc_r ] ; then
    shared_opt="-qmkshrobj"
else
    shared_opt="-shared -fPIC"
fi

bfdlibs=`echo $BFDLIBS | sed -e 's/#/ /g'`
if [ "$turnbfdoff" = 'no' ]; then
    if $c_compiler $demangle $orig_useropt $shared_opt -o conftest_bfd.so conftest_bfd.c -fPIC $bfdinclude $bfdlink $bfdlibs 1> /dev/null 2>&1 ; then
        if [ "$pin" = "no" ]; then
          fixmakeargs="$fixmakeargs TAU_BFDSHAREDLINK"
          echo "yes"
        fi
    else
        echo "no"
    fi
fi
/bin/rm -f conftest_bfd.o conftest_bfd.c conftest_bfd.so
######################################################################

######################################################################
# Install Dyninst and its dependencies, if requested
######################################################################
libboost=boost_1_79_0
libboosttgz=$libboost.tar.gz
libboosturl=http://www.cs.uoregon.edu/research/paracomp/tau/tauprofile/dist/$libboosttgz

libdyninst=dyninst-13.0.0
libdyninsttgz=$libdyninst.tar.gz
libdyninsturl=http://www.cs.uoregon.edu/research/paracomp/tau/tauprofile/dist/$libdyninsttgz

dynlibiberty=dynlibiberty
dynlibibertytgz=${dynlibiberty}.tar.gz
dynlibibertyurl=http://www.cs.uoregon.edu/research/paracomp/tau/tauprofile/dist/$dynlibibertytgz

#dynelfutils=elfutils-0.189
dynelfutils=elfutils-latest
dynelfutilsbz=${dynelfutils}.tar.bz2
dynelfutilsurl=http://www.cs.uoregon.edu/research/paracomp/tau/tauprofile/dist/$dynelfutilsbz

dyntbb=intel-tbb-2021.9
dyntbbtgz=${dyntbb}.tar.gz
dyntbburl=http://www.cs.uoregon.edu/research/paracomp/tau/tauprofile/dist/$dyntbbtgz
#dyntbburl=https://github.com/oneapi-src/oneTBB/archive/v2021.9.0.tar.gz

if [ "x$dyninstdir" = "x" ]; then
  libdyninstdir=${targetdir}/${architecture}
else
  libdyninstdir=$dyninstdir
fi

libdyninst_name=dyninst

if [ "x$dyninstdir" != "x" -a -d "$dyninstdir" ] ; then
  if [ -r $dyninstdir/$libdyninsttgz ]; then
    echo "NOTE: Copying $dyninstdir/$libdyninsttgz "
    cp $dyninstdir/$libdyninsttgz .
    download_dyninst=yes
  fi
fi


if [ "x$download_dyninst" = xyes ] ; then
	compiler_for_dyninst=gcc
	compilerxx_for_dyninst=g++
cat <<EOF > test.c
int main() {
}
EOF

cat <<EOF > test.cpp
int main() {
}
EOF

	if $compilerxx_for_dyninst -o test test.cpp 1> /dev/null 2>&1 ; then
		rm test.cpp
		rm test
	else
		rm test.cpp
		echo "Unable to compile with $compilerxx_for_dyninst (`which $compilerxx_for_dyninst`)"
		exit
	fi

	if $compiler_for_dyninst -o test test.c 1> /dev/null 2>&1 ; then
		rm test
		rm test.c
	else
		rm test.c
		echo "Unable to compile with $compiler_for_dyninst (`which $compiler_for_dyninst`)"
		exit
	fi
fi

## Check for tbb
if [ "x$tbbdir" != "x" -a -d "$tbbdir" ] ; then
  dyninsttbbdir=${tbbdir}
else
  dyninsttbbdir=${libdyninstdir}/${dyntbb}
  tbbdir=${dyninsttbbdir}/install
fi
if [ "x$dyninsttbbdir" != "x" -a -d "$dyninsttbbdir" ] ; then
  echo "TBB : found : $dyninsttbbdir"
  #curr_path=`pwd`
  #cd $dyninsttbbdir
  #compiler_for_dyninst=`grep "CMAKE_C_COMPILER" -rhw build/CMakeFiles/ | awk -F'"' '{print $2}'`
  #compilerxx_for_dyninst=`grep "CMAKE_CXX_COMPILER" -rhw build/CMakeFiles/ | awk -F'"' '{print $2}'`
  #cd $curr_path
  #unset curr_path
  #Need to find an alternative way, if possible. Do not remove previous commented lines.
  fixmakeargs="$fixmakeargs useropt=-I${dyninsttbbdir}/include#${useropt}"
else
  if [ "x$tbb" = xyes ] ; then
    echo "TBB : not found, downloading..."
    download_tbb=yes
  elif [ "x$dyninst" = xyes ] ; then
    echo "ERROR: Dyninst requires TBB and it was not found in the Dyninst directory. Reconfigure TAU with TBB."
    exit 1
  fi
fi

## Download and install tbb if necessary
if [ "x$download_tbb" = xyes ] ; then
  prebfddir=`pwd`
  # get the tar file
  if [ ! -f $dyntbbtgz ] ; then
    download "$dyntbburl" "$dyntbbtgz"
  fi
  # check the tar file
  if [ ! -f $dyntbbtgz ] ; then
    echo "$dyntbbtgz did not download.  Please download it manually from: "
    echo "$dyntbburl"
    echo "Please enter the directory containing the tarball: ${nnl}"
    read RESPONSE
    cp $RESPONSE/$dyntbbtgz .
  else
    echo "expanding $dyntbbtgz..."
    rm -rf $dyninsttbbdir
    mkdir $dyninsttbbdir
    tar -xzf $dyntbbtgz -C $dyninsttbbdir --strip-components=1
    echo "removing $dyntbbtgz..."
    rm $dyntbbtgz
    cd $dyninsttbbdir
	mkdir build
	mkdir install
	cd build
	#cmake is removing the runtime path, which generate issues when using tbb libraries, the two last flags fix this problem
	CC=$compiler_for_dyninst CXX=$compilerxx_for_dyninst cmake .. -DCMAKE_BUILD_TYPE:STRING=Release -DCMAKE_INSTALL_PREFIX=${dyninsttbbdir}/install -DTBB_TEST=OFF -DCMAKE_INSTALL_RPATH_USE_LINK_PATH=True -DCMAKE_INSTALL_RPATH=${dyninsttbbdir}/install/lib
	make -j4
	make install
	#compiler_for_dyninst=`grep "CMAKE_C_COMPILER" -rhw CMakeFiles/ | awk -F'"' '{print $2}'`
	#compilerxx_for_dyninst=`grep "CMAKE_CXX_COMPILER" -rhw CMakeFiles/ | awk -F'"' '{print $2}'`
	cd $dyninsttbbdir
	cd ..
    fixmakeargs="$fixmakeargs useropt=-I${dyninsttbbdir}/include#${useropt}"
  fi
  cd $prebfddir
fi #end dowload_tbb

## Check for boost
if [ "x$boostdir" != "x" -a -d "$boostdir" ] ; then
  dyninstboostdir=${boostdir}
else
  dyninstboostdir=${libdyninstdir}/${libboost}
  boostdir=${dyninstboostdir}
fi
if [ "x$dyninstboostdir" != "x" -a -d "$dyninstboostdir" ] ; then
  echo "Boost : found : $dyninstboostdir"
  fixmakeargs="$fixmakeargs boostinc=-I${dyninstboostdir}/include"
else
  if [ "x$boost" = xyes ] ; then
    echo "Boost : not found, downloading..."
    download_boost=yes
  fi
fi

## Download and install boost if necessary
if [ "x$download_boost" = xyes ] ; then
  prebfddir=`pwd`
  # get the tar file
  if [ ! -f $libboosttgz ] ; then
    download "$libboosturl" "$libboosttgz"
  fi
  # check the tar file
  if [ ! -f $libboosttgz ] ; then
    echo "$libboosttgz did not download.  Please download it manually from: "
    echo "$libboosturl"
    echo "Please enter the directory containing the tarball: ${nnl}"
    read RESPONSE
    cp $RESPONSE/$libboosttgz .
  else
    echo "expanding $libboosttgz..."
    rm -rf $dyninstboostdir
    mkdir $dyninstboostdir
    tar -xzf $libboosttgz -C $dyninstboostdir --strip-components=1
    echo "removing $libboosttgz..."
    rm $libboosttgz
    cd $dyninstboostdir
    ./bootstrap.sh --prefix=${dyninstboostdir} # dyninst wants boost to be built in its src dir
    ./b2 install -j8
    fixmakeargs="$fixmakeargs boostinc=-I${dyninstboostdir}/include"
  fi
  cd $prebfddir
fi #end dowload_boost

#set -x
if [ "x$download_dyninst" = xyes ] ; then

  dyninstdir=${libdyninstdir}/${libdyninst}
  prebfddir=`pwd`

  #check Cmake exists
  check_cmake 2 9 "Dyninst"
  result=$?
  if [ $result -eq 1 ]; then
    echo 'ERROR: cmake version 2.9 or higher required to build Dyninst.'
    exit 1;
  fi

  if [ -r "$dyninstdir/lib/libdyninstAPI.so" -o \
       -r "$dyninstdir/lib/libdyninstAPI_RT.so" -o \
       -r "$dyninstdir/lib/libdynElf.so" ] ; then
    echo "Found: dyninst"
    echo "dyninst download already found, skipping"
  else
    echo "Not found in ${dyninstdir}: ${libdyninst_name}"
    # get the tar file
    if [ ! -f $libdyninsttgz ] ; then
      download "$libdyninsturl" "$libdyninsttgz"
    fi

    # check the tar file
    if [ ! -f $libdyninsttgz ] ; then
      echo "$libdyninsttgz did not download.  Please download it manually from: "
      echo "$libdyninsturl"
      echo "Please enter the directory containing the tarball: ${nnl}"
      read RESPONSE
      cp $RESPONSE/$libdyninsttgz .
    else
      echo "expanding $libdyninsttgz..."
      rm -rf $libdyninst
      mkdir $libdyninst
      tar -xzf $libdyninsttgz -C $libdyninst --strip-components=1
      echo "removing $libdyninsttgz..."
      rm $libdyninsttgz
      rm -rf $dyninstdir # remove and make the build dir
      mkdir $dyninstdir
      cd $dyninstdir     # go to the build dir

      # Get libiberty
      mkdir -p ${dyninstdir}/libiberty/lib
      libiberty_root_dir=${dyninstdir}/libiberty/
      if [ -r "$bfddir/lib/libiberty.a"  ] ; then
	      cp "$bfddir/lib/libiberty.a" ${dyninstdir}/libiberty/lib/
	      libiberty_root_dir=$bfddir # We need the headers of libiberty, see LibIberty.cmake in libdyninst
      else
	      ## Build shared libiberty
	      download "$dynlibibertyurl" "$dynlibibertytgz"
	      tar -xzf $dynlibibertytgz
	      cd $dynlibiberty/libiberty
	      CC=$compiler_for_dyninst CXX=$compilerxx_for_dyninst ./configure --enable-shared --prefix=$PWD # It wrongly says that enable-shared is an unknow flag
	      make -j8
	      ar rcs libiberty.a pic/*.o
	      cp libiberty.a ${dyninstdir}/libiberty/lib/
	      cd ../
	      cp -r include/ ${dyninstdir}/libiberty/
	      cd ../
	      rm -rf $dynlibiberty $dynlibibertytgz
      fi

      ## Build shared elfutils
      mkdir ${dyninstdir}/elfutils
      download "$dynelfutilsurl" "$dynelfutilsbz"
      tar -xf $dynelfutilsbz -C ${dyninstdir}/elfutils --strip-components=1
      cd ${dyninstdir}/elfutils
      CC=$compiler_for_dyninst CXX=$compilerxx_for_dyninst  ./configure --prefix=$PWD --disable-libdebuginfod --disable-debuginfod
      make -j8
      make -j8 install
      cd ..
      rm -rf $dynelfutilsbz


      # Here error when using -bfd=download because it searches for /opt/intel/tbb/ by default
      # Workaround
      old_CPATH=$CPATH
      old_CMAKE_PREFIX_PATH=$CMAKE_PREFIX_PATH
      export CPATH=${dyninsttbbdir} # To prevent using wrong tbb/include when building
      export CMAKE_PREFIX_PATH="" # To prevent looking for tbb installation by bfd
      ## Now we can make Dyninst
      CC=$compiler_for_dyninst CXX=$compilerxx_for_dyninst cmake ${prebfddir}/${libdyninst} -DBoost_ROOT_DIR=${dyninstboostdir} -DCMAKE_INSTALL_PREFIX=${dyninstdir} -DLibIberty_ROOT_DIR=${libiberty_root_dir} -DLibIberty_LIBRARIES=${dyninstdir}/libiberty/lib/libiberty.a -DElfUtils_ROOT_DIR=${dyninstdir}/elfutils/ -DTBB_ROOT_DIR=${tbbdir}
      make -j8
      make install
      export CPATH=$old_CPATH
      export CMAKE_PREFIX_PATH=$old_CMAKE_PREFIX_PATH

      mkdir -p ${dyninstdir}/lib/
      if [ -f ${dyninstdir}/elfutils/lib/libdw.so ]; then
          cp ${dyninstdir}/elfutils/lib/libdw* ${dyninstdir}/lib/
          mv ${dyninstdir}/lib/libdw.so ${dyninstdir}/lib/libdwarf.so
      fi
      if [ -f ${dyninstdir}/elfutils/lib/libelf.so ]; then
          cp ${dyninstdir}/elfutils/lib/libelf* ${dyninstdir}/lib/
      fi
      if [ -f ${dyninstdir}/libiberty/lib/libiberty.a ]; then
          cp ${dyninstdir}/libiberty/lib/libiberty.a ${dyninstdir}/lib/
      fi
      if [ -f ${dyninsttbbdir}/lib/libtbb.so ]; then
          cp ${dyninsttbbdir}/lib/libtbb* ${dyninstdir}/lib/
      fi
    fi
  fi
  cd $prebfddir
fi #dyninst=download

#exit 0

######################################################################
# Install elfutils if requested
######################################################################

if [ "x$elfutilsdir" = "x" ]; then
  libelfutilsdir=${targetdir}/${architecture}
fi



#dynelfutils=elfutils-0.189
dynelfutils=elfutils-latest
dynelfutilsbz=${dynelfutils}.tar.bz2
dynelfutilsurl=http://www.cs.uoregon.edu/research/paracomp/tau/tauprofile/dist/$dynelfutilsbz

if [ "x$download_elfutils" = xyes ] ; then

    predowndir=`pwd`
    elfutilsdir=$libelfutilsdir/elfutils

    if [ -r "$elfutilsdir/lib/libdw.so" -a -r "$elfutilsdir/include/elfutils/libdw.h" ]; then
        echo "Found elfutils"
        echo "elfutils download skipping"
    else
        ## Build shared elfutils
        mkdir ${libelfutilsdir}/${dynelfutils}
        mkdir ${elfutilsdir}
        download "$dynelfutilsurl" "$dynelfutilsbz"
        tar -xf $dynelfutilsbz -C ${libelfutilsdir}/${dynelfutils} --strip-components=1
        cd ${libelfutilsdir}/${dynelfutils}
        CC=gcc CXX=g++  ./configure --prefix=${elfutilsdir} --disable-libdebuginfod --disable-debuginfod
        make -j8
        make -j8 install
        cd ..
        rm -rf $dynelfutilsbz
        rm -rf $dynelfutils
    fi
    cd $predowndir
fi

if [ "$elfutils" = "yes" ]; then
    if [ -r "$elfutilsdir/include/elfutils/libdw.h" ]; then
        elfutilsinc=$elfutilsdir/include
    else
        echo "Could not find elfutils $elfutilsdir/include/elfutils/libdw.h"
        exit 1
    fi
    if [ -r "$elfutilsdir/lib/libdw.so" ]; then
        elfutilslib=$elfutilsdir/lib
    else
        echo "Could not find elfutils $elfutilsdir/lib/libdw.so"
        exit 1
    fi


    fixmakeargs="$fixmakeargs elfutilsincdir=$elfutilsinc"
    fixmakeargs="$fixmakeargs elfutilslibdir=$elfutilslib"
fi ##download elfutils



######################################################################
# Check that the Sampling allocator code is supported by compiler
######################################################################
cat <<EOF > conftest_ss_alloc.cpp
#include <Profile/TauSsAllocator.h>
#include <map>
#include <stddef.h>
using namespace std;

int main() {
map<int, int, std::less<int>, tau_ss_allocator< std::pair<int, int> > > *test =
new map<int, int, std::less<int>, tau_ss_allocator< std::pair<int, int> > >();
}
EOF
$echo "Checking for ss_allocator support... ${nnl}"
if $cxx_compiler $orig_useropt -c conftest_ss_alloc.cpp -I$tauroot/include 1> /dev/null 2>&1 ; then
    if [ $cxx_compiler = "pgCC" -o $cxx_compiler = "pgc++" ]
    then
        # PGI compilers do not play nice with the current allocator.
        echo "no"
    else
        fixmakeargs="$fixmakeargs TAU_SS_ALLOC_SUPPORT"
        echo "yes"
    fi
else
    echo "no"
fi
/bin/rm -f conftest_ss_alloc.o conftest_ss_alloc.cpp
######################################################################

######################################################################
# Check that basic librt linkage is supported by this system
#    Precise functionality support is system-dependent and should
#    be controlled by macros inside code accordingly.
######################################################################
cat <<EOF > conftest_rt_link_test.cpp
int main() {
}
EOF
$echo "Checking for librt linkage support ... ${nnl}"
if $host_prefix$cxx_compiler $orig_useropt -o conftest_rt_link_test conftest_rt_link_test.cpp -lrt 1> /dev/null 2>&1 ; then
    tau_links_rt=yes
    fixmakeargs="$fixmakeargs TAU_LINKS_RT"
    echo "yes"
else
    echo "no"
fi
/bin/rm -f conftest_rt_link_test conftest_rt_link_test.cpp

######################################################################
# Check that basic librt functionality is supported by this system
######################################################################
# *CWL* Don't bother checking for functionality if we cannot even link
#       the library.
if [ $tau_links_rt = "yes" ] ; then
    cat <<EOF > conftest_rt_test.cpp
#include <time.h>
#include <stdio.h>
int main() {
  double time_value = 0.0;
  struct timespec timeVal;
  int ret = clock_getres(CLOCK_REALTIME, &timeVal);
  if (ret != 0) {
    printf("-1");
    return -1;
  }
  time_value = (double)(timeVal.tv_sec)*1000000 +
               (double)(timeVal.tv_nsec)/1000.0;
  if (time_value <= 0) {
    printf("-1");
    return -1;
  } else if (time_value < 1.0) {
    time_value = 1.0; // 1us is the smallest resolution we will support
  }
  printf("%ld", (long)time_value);
}
EOF
    $echo "Checking for Basic librt Real Time Clock Signal support... ${nnl}"
    if $cxx_compiler $orig_useropt -o conftest_rt_test conftest_rt_test.cpp -lrt 1> /dev/null 2>&1 ; then
        ebs_has_rt=yes
        echo "yes"
    else
        echo "no"
    fi
    # CWL - bgp does not have an accurate return value for clock_getres()
    if [ $ebs_has_rt = "yes" -a $machine != "bgp" ] ; then
        # CWL - watch out for the lack of awk support on rare platforms.
        #       This is necessary to check that the compiled config test
        #       returns either the empty string "" or some numeric value.
        ebs_clock_res=`./conftest_rt_test | awk '/^[0-9]+$/'`
        # CWL - we cannot assume the above command actually succeeds
        if [ x$ebs_clock_res != "x" ] ; then
            if [ $ebs_clock_res -le 0 ] ; then
                ebs_has_rt=no
                echo "No Sane Clock Signal Resolution. No effective Real Time support."
            else
                echo "Smallest Sane Clock Signal Resolution = ${ebs_clock_res} microseconds"
            fi
        else
            ebs_has_rt=no
            echo "No Sane Clock Signal Resolution. No effective Real Time support."
        fi
    fi
    #echo $tau_rt_value
    #echo $?

    /bin/rm -f conftest_rt_test conftest_rt_test.cpp
fi
######################################################################

######################################################################
# Check for tr1/unordered_map
######################################################################
cat <<EOF > conftest_hash_map.cc
#include <tr1/unordered_map>

int main() {
  std::tr1::unordered_map<int,int> theMap;
  theMap[5] = 6;
  std::tr1::unordered_map<int,int>::const_iterator it;
}
EOF
$echo "Checking for tr1/unordered_map... ${nnl}"
if $cxx_compiler $orig_useropt -c conftest_hash_map.cc 1> /dev/null 2>&1 ; then
    fixmakeargs="$fixmakeargs TAU_TR1_HASH_MAP"
    echo "yes"
else
    echo "no"
fi

/bin/rm -f conftest_hash_map.o conftest_hash_map.cc


######################################################################
# Check for pthread wrappable routines
######################################################################
if [ $pthread = yes ] ; then
    cat <<EOF > conftest_pthread.c
#include <pthread.h>

void foo() {
  int rc;
  pthread_mutexattr_t   mta;
  pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  pthread_spinlock_t spinlock;
  pthread_barrier_t barrier;
  pthread_cond_t cond;

  rc = pthread_mutexattr_init(&mta);
  rc = pthread_mutex_init(&mutex, NULL);
  rc = pthread_mutex_lock(&mutex);
  rc = pthread_mutex_unlock(&mutex);
  rc = pthread_mutex_destroy(&mutex);

  rc = pthread_spin_init(&spinlock, PTHREAD_PROCESS_SHARED);
  rc = pthread_spin_lock(&spinlock);
  rc = pthread_spin_unlock(&spinlock);
  rc = pthread_spin_destroy(&spinlock);

  rc = pthread_cond_init(&cond, NULL);
  rc = pthread_cond_broadcast(&cond);
  rc = pthread_cond_wait(&cond, &mutex);
  rc = pthread_cond_signal(&cond);
  rc = pthread_cond_destroy(&cond);


  rc = pthread_barrier_init(&barrier, NULL, 0);
  rc = pthread_barrier_wait(&barrier);
}
EOF
  $echo "Checking for pthread wrappable routines... ${nnl}"
  if $c_compiler $orig_useropt -c conftest_pthread.c $bfdinclude 1> /dev/null 2>&1 ; then
      fixmakeargs="$fixmakeargs TAU_PTHREAD_WRAP"
      echo "yes"
  else
      echo "no"
  fi

  /bin/rm -f conftest_pthread.o conftest_pthread.c
fi

######################################################################
# Check for TLS, or Thread Local Storage
######################################################################

tlsfound=no

if [ $openmp = yes ] || [ $pthread = yes ] ; then
    cat <<EOF > conftest_tls.cc
__thread int number = -1;
int main() {
  return number;
}
EOF
  $echo "Checking for thread local storage support... ${nnl}"
  if $cxx_compiler $orig_useropt -c conftest_tls.cc 1> /dev/null 2>&1 ; then
      fixmakeargs="$fixmakeargs TLS_AVAILABLE"
      echo "yes"
      tlsfound=yes
  else
      echo "no"
  fi

  /bin/rm -f conftest_tls.o conftest_tls.cc
fi

######################################################################
# Check for declspec(thread) support
######################################################################
if [ $tlsfound = no ] ; then
  if [ $openmp = yes ] || [ $pthread = yes ] ; then
    cat <<EOF > conftest_dtls.cc
__declspec(thread) int number = -1;
int main() {
  return number;
}
EOF
    $echo "Checking for __declspec(thread) support... ${nnl}"
    if $cxx_compiler $orig_useropt -c conftest_dtls.cc 1> /dev/null 2>&1 ; then
        fixmakeargs="$fixmakeargs DTLS_AVAILABLE"
        echo "yes"
        tlsfound=yes
    else
        echo "no"
    fi

    /bin/rm -f conftest_dtls.o conftest_dtls.cc
  fi
fi

######################################################################
# Check for pthread_getspecific() support
######################################################################
if [ $tlsfound = no ] ; then
  if [ $openmp = yes ] || [ $pthread = yes ] ; then
    cat <<EOF > conftest_pgs.cc
#include "pthread.h"
static pthread_key_t key;
int number = 1;

int main() {
  pthread_key_create(&key, NULL);
  pthread_setspecific(key, &number);
  int *newnum;
  newnum = (int*)pthread_getspecific(key);
  return *newnum;
}
EOF
    $echo "Checking for pthread_getspecific() support... ${nnl}"
    if $cxx_compiler $orig_useropt -c conftest_pgs.cc 1> /dev/null 2>&1 ; then
        fixmakeargs="$fixmakeargs PGS_AVAILABLE"
        echo "yes"
        tlsfound=yes
    else
        echo "no"
    fi

    /bin/rm -f conftest_pgs.o conftest_pgs.cc
  fi
fi


######################################################################

if [ $suppress_pthread_create_wrapper = yes ]; then
    fixmakeargs="$fixmakeargs SUPPRESS_PTHREAD_CREATE_WRAPPER"
fi
# If TBB support is requested, then make that the tag, not pthread
if [ $tbb = yes ] ; then
    fixmakeargs="$fixmakeargs PTHREAD_AVAILABLE ptdir=$ptdir"
    fixmakeargs="$fixmakeargs TBB_AVAILABLE"
    tauoptions="${tauoptions}-tbb"
else
  if [ $pthread = yes ] ; then
      fixmakeargs="$fixmakeargs PTHREAD_AVAILABLE ptdir=$ptdir"
      tauoptions="${tauoptions}-pthread"
  fi
fi

if [ $ompt = yes -a $opari = yes ] ; then
  echo "ERROR: Please enable either OMPT or Opari for instrumentation, but not both"
  exit 1
fi

# GOMP wrapping is only available for openmp
if [ $openmp = yes ] ; then
    # GOMP wrapping is only available for GCC
    if  $c_compiler --version 2>/dev/null | grep -q "gcc"  || [ $machine = craycnl -a "x$PE_ENV" = xGNU ] ; then
          # GOMP wrapping is not compatible with Opari.
          if [ $opari = no ] ; then
              if [ $opari1 = no ] ; then
                if [ $ompt = no ] ; then
                      fixmakeargs="$fixmakeargs GOMP_AVAILABLE ptdir=$ptdir"
                else
                      echo "NOT using GOMP wrapper (OMPT)"
                        fi
                  else
                    echo "NOT using GOMP wrapper (Opari1)"
              fi
          else
              echo "NOT using GOMP wrapper (Opari2)"
          fi
    fi
fi

if [ $papithread = yes ] ; then
    if [ $papi = no ] ; then
        echo ""
        echo "Error: To use the PAPI thread layer, you must specify -papi=<dir>"
        echo ""
        exit
    fi

    if [ ! `uname -s ` = Linux ] ; then
        echo ""
        echo "Error: The PAPI thread layer only works on Linux"
        echo ""
        exit
    fi

    fixmakeargs="$fixmakeargs TAU_PAPI_THREADS"
    tauoptions="${tauoptions}-papithread"
fi

if [ $charm = yes ] ; then
    fixmakeargs="$fixmakeargs TAU_CHARM charmdir=$charmdir"
    tauoptions="${tauoptions}-charm"
fi

if [ $compensate = yes ] ; then
    fixmakeargs="$fixmakeargs COMPENSATE"
    tauoptions="${tauoptions}-compensate"
fi

if [ $sproc = yes ] ; then
    fixmakeargs="$fixmakeargs TAU_SPROC"
    tauoptions="${tauoptions}-sproc"
fi

if [ $python = yes ] ; then
    if [ "x$pythonlibrary" = "x" ] ; then
        echo "Checking for Python library name from interpreter ${pythonintv}... "
        pythonlibrary=`${pythonintv} ./utils/python_get_paths.py libname | sed -e 's/ .*//g'`
    fi
    if [ "x$pythonlibrary" != "x" ] ; then
	#If the python library directory exists but the library is not found we may be in a
	#virtual environment. Query the library and include paths directly.
        if [ ! -f $pythonlib/$pythonlibrary ] ; then
		pythonlib=`${pythonintv} ./utils/python_get_paths.py libdir | sed -e 's/ .*//g'`
		pythoninc=`${pythonintv} ./utils/python_get_paths.py inc | sed -e 's/ .*//g'`
	fi

	if [ ! -f $pythonlib/$pythonlibrary ] ; then
                echo "ERROR: Python library $pythonlib/$pythonlibrary not found."
                exit 1
        fi
            fixmakeargs="$fixmakeargs pythonlibrary=$pythonlibrary"
    fi
    fixmakeargs="$fixmakeargs PYTHON pythoninc=$pythoninc pythonlib=$pythonlib"
    tauoptions="${tauoptions}-python"
    if [ $machine = rs6000 -a "x$pythonlib" = "x" ] ; then
        echo "ERROR: For IBM systems, please specify the -pythonlib=<dir> flag where *.py files and the config directory are located!"
        exit 1
    fi
fi
if [ $cuda = yes ] ; then
    if [ $disableshared = no ] ; then
        if [ -f "$cudainclude/cuda.h" ] ; then
            fixmakeargs="$fixmakeargs CUDA TAU_USE_GPU cudainclude=$cudainclude"
            if [ "$opencl" != "disable" ] ; then
                if [ "$use_opencl" = "no" -a -f "$cudainclude/CL/cl.h" ] ; then
                    $echo "Checking $cxx_compiler compatibility with OpenCL... ${nnl}"
                    cat <<EOF>opencl.cpp
#include "CL/cl.h"
EOF
                    if $cxx_compiler -I$cudainclude -c opencl.cpp 1>/dev/null 2>&1 ; then
                        echo "yes"
                        fixmakeargs="$fixmakeargs OPENCL TAU_USE_GPU openclinclude=$cudainclude"
                        use_opencl=yes
                    else
                        echo "no"
                    fi
                    rm -f opencl.cpp opencl.o
                fi
                cudadir=`dirname $cudainclude`
                cuda_version=$(cat "$cudadir"/include/cuda.h | grep "define CUDA_VERSION")
                vers_arr=($cuda_version)
                cupti_event_path=""
                cuda_arch=`arch | awk '{print tolower($0)}'`
                cuda_uname=`uname | awk '{print tolower($0)}'`
                if [ -f "$cudadir/extras/CUPTI/include/cupti_events.h" ] ; then
                    # check if usual path exists, otherwise use fallback
                    cupti_event_path="$cudadir/extras/CUPTI/include/cupti_events.h"
                else
                    # Check in new location for 10.2+
                    if [ -f "$cudadir/targets/${cuda_arch}-${cuda_uname}/include/cupti_events.h" ] ; then
                        cupti_event_path="$cudadir/targets/${cuda_arch}-${cuda_uname}/include/cupti_events.h"
            else
                        # Check for special cases
                        cupti_event_path="$cudadir/extras/CUPTI.orig/include/cupti_events.h"
                        if [ -f "$cudadir/targets/aarch64-linux/include/cupti_events.h" ]; then
                            cupti_event_path="$cudadir/targets/aarch64-linux/include/cupti_events.h"
                    fi
                fi
                fi
                if [ $cupti = no -a -f $cupti_event_path ] ; then
                    tauoptions="${tauoptions}-cupti"
                    cupti_path=""
                    if [ -d "$cudadir/extras/CUPTI" ] ; then
                        # check if usual path exists, otherwise use fallback
                        cupti_path="$cudadir/extras/CUPTI"
                    else
                        cupti_path="$cudadir/extras/CUPTI.orig"
                        if [ -f "$cudadir/targets/aarch64-linux/include/cupti_events.h" ]; then
                          cupti_path="$cudadir/targets/aarch64-linux"
                        fi
                    fi
                    fixmakeargs="$fixmakeargs CUPTI TAU_USE_GPU cuptiinclude=${cupti_path}/include"
                    echo "Using CUPTI library found in $cupti."
		    cupti=yes
                    if [ $machine = craycnl ]; then
                        # check if -lcuda is available to the linker somehow
                      fixmakeargs="$fixmakeargs CUPTI_NOCUDA"
                    fi
                    if [ ! -f $cudadir/lib64/stubs/libnvidia-ml.so ]; then
                      fixmakeargs="$fixmakeargs CUPTI_NONVML"
                    fi
                    if [ -f "${cupti_path}/lib/libpcsamplingutil.so" ]; then
                        fixmakeargs="$fixmakeargs CUPTIPC"
                    fi
                    if [ -f "${cupti_path}/lib64/libpcsamplingutil.so" ]; then
                        fixmakeargs="$fixmakeargs CUPTIPC"
                    fi
                fi
            fi
        else
            echo "Error: Cannot find GPU headers, TAU currently supports either CUDA or OpenCL"
        fi
    else
        echo "Error: Cannot configure CUDA with DISABLESHARED set!"
        exit 1
    fi
fi


#PYCUDA is not needed. Leave the code in case it is needed at some point
configure_pycuda() {
if [ $python = yes -a $cuda = yes ] ; then
    #We need the boost/python libraries to build the TAU's pycuda interface.
    echo "Checking for boost/python ... ${nnl}"
    cat <<EOF>boost.cpp
#include "boost/python.hpp"
EOF
    pythoninc_cmd="$(echo $pythoninc | sed -e "s/ / -I/g")"
    #echo $cxx_compiler -I$pythoninc_cmd -c boost.cpp
    if $cxx_compiler -I$pythoninc_cmd -c boost.cpp 1>/dev/null 2>&1 ; then
        echo "yes"
        fixmakeargs="$fixmakeargs PYCUDA"
    else
        echo "boost/python libraries not found, skipping TAU's pycuda interface"
    fi
    rm -f boost.cpp boost.o
fi

if [ $python = yes -a $cuda = yes -a $boost = yes ] ; then
        fixmakeargs="$fixmakeargs PYCUDA_BOOST"
fi
}
#END of PYCUDA disabled part


if [ ! $cupti = no ]; then
    echo "Checking CUPTI version at ${cupti_path}/include ... ${nnl}"
    cat <<EOF > cupti_version.C
#include <cupti_version.h>
#if CUPTI_API_VERSION >= 2
#error CUPTI version >= 4.1 found.
#endif
EOF
    if $cxx_compiler -I${cupti_path}/include -c cupti_version.C 1>/dev/null 2>&1 ; then
        echo "CUPTI version 4.0 found."
    else
        echo "CUPTI version >= 4.1 found."
        fixmakeargs="$fixmakeargs CUPTI_ACTIVITY TAU_USE_GPU"
    fi
    rm -f cupti_version.C cupti_version.o
fi

if [ $zepto = yes ] ; then
    tauoptions="${tauoptions}-zepto"
fi

if [ $ktau = yes ] ; then
    fixmakeargs="$fixmakeargs KTAU ktauinc=$ktauinc ktauincuser=$ktauincuser ktaulib=$ktaulib ktausym=$ktausym"
    tauoptions="${tauoptions}-ktau"
fi

if [ $ktau_ng = yes ] ; then
    fixmakeargs="$fixmakeargs KTAU_NG"
    tauoptions="${tauoptions}-ktau-ng"
fi

if [ $ktau_merge = yes ] ; then
    fixmakeargs="$fixmakeargs KTAU_MERGE ktauinc=$ktauinc ktauincuser=$ktauincuser ktaulib=$ktaulib ktausym=$ktausym"
    tauoptions="${tauoptions}-ktau_merge"
fi

if [ $ktau_shctr = yes ] ; then
    fixmakeargs="$fixmakeargs KTAU_SHCTR ktauinc=$ktauinc ktauincuser=$ktauincuser ktaulib=$ktaulib ktausym=$ktausym"
    tauoptions="${tauoptions}-ktau_shctr"
fi

if [ $vtf = yes ] ; then
    fixmakeargs="$fixmakeargs VTF vtfdir=$vtfdir"
fi

if [ "x$unwinder" != "xnone" ] ; then
    if [ $c_compiler = gcc -o $c_compiler = icc -o $c_compiler = icx -o $c_compiler = pgcc -o $c_compiler = mpc_icc -o $c_compiler = mpc_cc -o $c_compiler = clang -o $c_compiler = mpicc -o $c_compiler=mpiicc -o $c_compiler = mpiicx -o $c_compiler = hipcc -o $c_compiler = nvc -o $c_compiler = nvcc -o $c_compiler = icx -o $c_compiler = amdclang ] ; then
        if [ "x$unwind_lib" != "x" ] ; then
            unwind_extras="-Wl\,-rpath\,$unwind_lib"
        fi
    fi
    if [ "x$static_libunwind" = "xyes" ]; then
      unwind_lib_flag="$unwind_lib/libunwind.a"
      echo "NOTE: Using unwind_lib_flag=$unwind_lib_flag"
    fi
    if [ "x$unwind_inc" != "x" ] ; then
      unwind_inc="-I${unwind_inc}"
    fi
    if [ "x$unwind_lib" != "x" ] ; then
      unwind_lib="-L${unwind_lib}"
    fi
    if [ "x$unwinder" = "xlibunwind" ] ; then
          if [ $machine = apple ] ; then
            unwind_lib_flag="-framework#System"
            unwind_extras=""
          fi
          #if [ $machine = craycnl -a "x$CRAY_TARGET_PE" = "xcray" ]; then
          #  echo "WARNING: TAU does not currently support libunwind with Cray CCE compilers on AARCH64."
          #else
          fixmakeargs="$fixmakeargs TAU_UNWIND TAU_UNWIND_LIBUNWIND unwind_flag=$unwind_flag unwind_inc=$unwind_inc unwind_lib=$unwind_lib unwind_lib_flag=$unwind_lib_flag unwind_extras=$unwind_extras"
          #fi
    else
          fixmakeargs="$fixmakeargs TAU_UNWIND TAU_UNWIND_STACKWALKER unwind_flag=$unwind_flag unwind_inc=$unwind_inc unwind_lib=$unwind_lib unwind_lib_flag=$unwind_lib_flag unwind_extras=$unwind_extras"
    fi
fi

if [ $ebs2otf = yes ] ; then
    if [ -f utils/ebs2otf/ebs2otf.so ] ; then
        touch utils/ebs2otf/ebs2otf.so
    fi
    fixmakeargs="$fixmakeargs EBS2OTF "
    if [ $otf = yes ] ; then
        echo "Warning: -otf option enables TAU Tracing as a default. EBS will only work with TAU profiles. Please make sure Profiles are enabled before use."
    fi
fi

if [ $ebs_has_rt = yes ] ; then
    fixmakeargs="$fixmakeargs EBS_HAS_RT ebs_clock_res=$ebs_clock_res"
fi

if [ $pdt_cxx_compiler != default ] ; then
    # Check if they specified -pdt_c++=/usr/bin/g++ instead of just g++
    if [ "x$pdt_cxx_full_path" != "x$pdt_cxx_compiler" ] ; then
        pdt_cxx_used=$pdt_cxx_full_path
    else
        pdt_cxx_used=$pdt_cxx_compiler
    fi

    fixmakeargs="$fixmakeargs PDTCXX pdtcxx=$pdt_cxx_used"
    if [ $machine = sgi8k -o $machine = sgin32 -o $machine = sgi64 ] ; then
        if [ $pdt_cxx_compiler = CC ] ; then
            fixmakeargs="$fixmakeargs PDTSGICC"
        fi
    fi
fi


if [ $pdt = yes ] ; then
    fixmakeargs="$fixmakeargs PDT pdtdir=$pdtdir"
    tauoptions="${tauoptions}-pdt"
    if [ "x$pdtcompdir" != "x" ] ; then
        fixmakeargs="$fixmakeargs PDTARCH pdtcompdir=$pdtcompdir"
    fi

    if [ $pdtarchdir != unknown ] ; then
        fixmakeargs="$fixmakeargs PDTARCHITECTURE pdtarchdir=$pdtarchdir"
    fi

    if [ $machine = mic_linux -a $pdtarchdir = unknown ]; then
        fixmakeargs="$fixmakeargs PDTARCHITECTURE pdtarchdir=x86_64"
    fi

    # Check if PDT has Fortran statement level information
    cat <<EOF > checkpdt.cpp
  #include <pdbAll.h>

  int IsFortranDoStatement(pdbStmt::stmt_t k)
  {
        if (k == pdbStmt::ST_FDO) return 1;
        else return 0;
  }
EOF
    if [ $machine = alpha -a $cxx_compiler = cxx ]; then
        orig_useropt="$orig_useropt -x c++ -D__USE_STD_IOSTREAM"
    fi
    if $cxx_compiler $orig_useropt -I$pdtdir/include -c checkpdt.cpp 1> /dev/null 2>&1 ; then
        echo "PDT supports Fortran Loop Level information"
    else
        echo "IMPORTANT NOTE: Your PDT does not support Fortran Loop Level information."
        echo "***************You may want to upgrade to a newer release."
        echo "See http://www.cs.uoregon.edu/research/pdt"
        fixmakeargs="$fixmakeargs PDTNOFSTMTS"
    fi
    /bin/rm -f checkpdt.o checkpdt.cpp

    # Check if PDT has UPC statement level information
    cat <<EOF > checkpdt.cpp
  #include <pdbAll.h>

  int IsUPCStmt(pdbStmt::stmt_t k)
  {
        if (k == pdbStmt::ST_UPC_FORALL) return 1;
        else return 0;
  }
EOF
    if [ $machine = alpha -a $cxx_compiler = cxx ]; then
        orig_useropt="$orig_useropt -x c++ -D__USE_STD_IOSTREAM"
    fi
    if $cxx_compiler $orig_useropt -I$pdtdir/include -c checkpdt.cpp 1> /dev/null 2>&1 ; then
        echo "PDT supports UPC information"
    else
        echo "IMPORTANT NOTE: Your PDT does not support UPC statement level information."
        echo "***************You may want to upgrade to a newer release."
        echo "See http://www.cs.uoregon.edu/research/pdt"
        fixmakeargs="$fixmakeargs PDTNOUPC"
    fi
    /bin/rm -f checkpdt.o checkpdt.cpp
fi


if [ $armci = yes ]; then
    cat <<EOF>armcitest.c
#include <stdio.h>
#include <sys/types.h>
#include "parmci.h"
#include "armci.h"

int
ARMCI_AccV (int op, void *scale, armci_giov_t * darr, int len, int proc)
{
  int rval;
  rval = PARMCI_AccV (op, scale, darr, len, proc);
  return rval;
}
EOF
    if $cxx_compiler $orig_useropt -I$armcidir/include -c armcitest.c 1> /dev/null 2>&1 ; then
        echo "ARMCI check successful"
        fixmakeargs="$fixmakeargs ARMCI armcidir=$armcidir"
    else
        echo "ARMCI check unsuccessful. Does not support PARMCI interface found in GA 5.0+."
    fi
    /bin/rm -f armcitest.c armcitest.o
fi

if [ $vampirtrace = yes ]; then
    cat <<EOF>vttest.cpp
#include "Profile/Profiler.h"
#include "Profile/TauVampirTrace.h"
int foo(void)
{
  uint64_t x;
  return 0;
}
EOF
    if $cxx_compiler $orig_useropt -Iinclude -I$vampirtracedir/include -c vttest.cpp -DTRACING_ON -DTAU_DOT_H_LESS_HEADERS 1> /dev/null 2>&1 ; then
        echo "VampirTrace check successful"
    else
        echo "Using VampirTrace options for compiling"
        fixmakeargs="$fixmakeargs VAMPIRTRACEINTS"
    fi
    /bin/rm -f vttest.cpp vttest.o
fi

build_apex() {
    cd ${START_DIR}
    check_cmake 3 20 "APEX"
    result=$?
    if [ $result -eq 1 ]; then
        echo 'ERROR: cmake version 3.20 or higher required to build APEX.'
        exit 1;
    fi
    echo "NOTE: Building APEX"
    if [ ! -d apex ] || [ ! -f apex/CMakeLists.txt ] ; then
        git submodule update --init --recursive apex
    fi
    apex_include_file=apex/apex.mk
    rm -f ${apex_include_file}
    touch ${apex_include_file}
    echo "CC=${c_compiler}" >> ${apex_include_file}
    echo "CXX=${cxx_compiler}" >> ${apex_include_file}
    echo "PREFIX=${targetdir}" >> ${apex_include_file}
    echo "TAUARCH=${architecture}" >> ${apex_include_file}
    echo "TAUOPTIONS=${tauoptions}" >> ${apex_include_file}
    if [ ! "x${bfddir}" = "x" ] ; then
        echo "BFDOPT=-DAPEX_WITH_BFD=TRUE -DBFD_ROOT=${bfddir}" >> ${apex_include_file}
    else
        if [ ${bfd_worked} = "yes" ] ; then
            echo 'BFDOPT=-DAPEX_WITH_BFD=TRUE' >> ${apex_include_file}
        fi
    fi
    if [ ! "x${omptlib}" = "x" ] ; then
        echo "TAUOMPT=-DAPEX_WITH_OMPT=TRUE" >> ${apex_include_file}
    fi
    if [ "${cuda}" = "yes" ] ; then
        echo "TAUCUDA=-DAPEX_WITH_CUDA=TRUE -DCUDAToolkit_ROOT=${cudadir}" >> ${apex_include_file}
    fi
    if [ "${rocm}" = "yes" ] ; then
        echo "TAUHIP=-DAPEX_WITH_HIP=TRUE" >> ${apex_include_file}
    fi
    if [ "${level_zero}" = "yes" ] ; then
        echo "TAUSYCL=-DAPEX_WITH_LEVEL0=TRUE" >> ${apex_include_file}
    fi
    if [ "${mpi}" = "yes" ] ; then
        echo "TAUMPI=-DAPEX_WITH_MPI=TRUE" >> ${apex_include_file}
    fi
    echo "TAUOPENCL=-DAPEX_WITH_OPENCL=TRUE" >> ${apex_include_file}
    cp etc/Makefile.apex apex/Makefile
    fixmakeargs="$fixmakeargs APEX "
    make -C apex clean
    make -C apex
}

build_zerosum() {
    cd ${START_DIR}
    check_cmake 3 20 "ZeroSum"
    result=$?
    if [ $result -eq 1 ]; then
        echo 'ERROR: cmake version 3.20 or higher required to build ZeroSum.'
        exit 1;
    fi
    echo "NOTE: Building ZeroSum"
    if [ ! -d zerosum ] || [ ! -f zerosum/CMakeLists.txt ] ; then
        git submodule update --init --recursive zerosum
    fi
    zerosum_include_file=zerosum/zerosum.mk
    rm -f ${zerosum_include_file}
    touch ${zerosum_include_file}
    echo "CC=${c_compiler}" >> ${zerosum_include_file}
    echo "CXX=${cxx_compiler}" >> ${zerosum_include_file}
    echo "PREFIX=${targetdir}" >> ${zerosum_include_file}
    echo "TAUARCH=${architecture}" >> ${zerosum_include_file}
    echo "TAUOPTIONS=${tauoptions}" >> ${zerosum_include_file}
    if [ ! "x${omptlib}" = "x" ] ; then
        echo "TAUOMPT=-DZeroSum_WITH_OMPT=TRUE" >> ${zerosum_include_file}
    fi
    if [ "${cuda}" = "yes" ] ; then
        echo "TAUCUDA=-DZeroSum_WITH_CUDA=TRUE -DCUDAToolkit_ROOT=${cudadir}" >> ${zerosum_include_file}
    fi
    if [ "${rocm}" = "yes" ] ; then
        echo "TAUHIP=-DZeroSum_WITH_HIP=TRUE" >> ${zerosum_include_file}
    fi
    if [ "${level_zero}" = "yes" ] ; then
        echo "TAUSYCL=-DZeroSum_WITH_SYCL=TRUE" >> ${zerosum_include_file}
    fi
    if [ "${mpi}" = "yes" ] ; then
        echo "TAUMPI=-DZeroSum_WITH_MPI=TRUE" >> ${zerosum_include_file}
    else
        echo "TAUMPI=-DZeroSum_WITH_MPI=FALSE" >> ${zerosum_include_file}
    fi
    cp etc/Makefile.zerosum zerosum/Makefile
    fixmakeargs="$fixmakeargs ZeroSum "
    make -C zerosum clean
    make -C zerosum
}

build_perfstubs() {
    cd ${START_DIR}
    check_cmake 3 20 "PerfStubs"
    result=$?
    if [ $result -eq 1 ]; then
        echo 'ERROR: cmake version 3.20 or higher required to build PerfStubs.'
        exit 1;
    fi
    echo "NOTE: Building PerfStubs with Python3.12+ support"
    if [ ! -d perfstubs ] || [ ! -f perfstubs/CMakeLists.txt ] ; then
        git submodule update --init --recursive perfstubs
    fi
    perfstubs_include_file=perfstubs/perfstubs.mk
    Python3_BIN_DIR=`dirname ${pythonintv}`
    Python3_ROOT_DIR=`dirname ${Python3_BIN_DIR}`
    rm -f ${perfstubs_include_file}
    touch ${perfstubs_include_file}
    echo "CC=${c_compiler}" >> ${perfstubs_include_file}
    echo "CXX=${cxx_compiler}" >> ${perfstubs_include_file}
    echo "PREFIX=${targetdir}" >> ${perfstubs_include_file}
    echo "TAUARCH=${architecture}" >> ${perfstubs_include_file}
    echo "TAUOPTIONS=${tauoptions}" >> ${perfstubs_include_file}
    # Let CMake find Python
    echo "TAUPYTHON=-DPERFSTUBS_WITH_PYTHON=TRUE -DPython3_FIND_STRATEGY=LOCATION -DCMAKE_PREFIX_PATH=${Python3_ROOT_DIR}" >> ${perfstubs_include_file}
    cp etc/Makefile.perfstubs perfstubs/Makefile
    fixmakeargs="$fixmakeargs PerfStubs "
    make -C perfstubs clean
    make -C perfstubs
}

if [ $java = yes ] ; then
    fixmakeargs="$fixmakeargs JAVA jdkdir=$jdkdir"
fi

if [ $openmp = yes ] ; then


    #find openmp flag
    if [ "$sol2cc" = "yes" ]; then openmp_flag=-xopenmp; fi
    if [ "$suncc" = "yes" ]; then openmp_flag=-xopenmp=parallel; fi
    if [ "$compaq" = "yes" ]; then openmp_flag=-omp; fi
    if [ "$ibmxlc" = "yes" ]; then openmp_flag=-qsmp=omp; fi
    if [ "$open64" = "yes" ]; then openmp_flag=-mp; fi
    if [ "$gnu" = "yes" ]; then openmp_flag=-fopenmp; fi
    if [ "$llvm" = "yes" ]; then openmp_flag=-fopenmp; fi
    if [ "$pgi" = "yes" ]; then openmp_flag=-mp; fi
    if [ "$fujitsu" = "yes" ]; then openmp_flag=-Kopenmp; fi
    if [ "$default_fortran" = "cray" ]; then openmp_flag=-h#omp; fi
    if [ "$intel" = "yes" ]; then openmp_flag=-qopenmp; fi

    #checking OMP compiler nested supported
    echo -n "Checking OMP NESTED support..."
    touch omp_nested_test.c
    cat <<EOF>omp_nested_test.c
#include <omp.h>
#include <stdio.h>
int main()
{
 omp_get_thread_num();
 omp_get_level();
 omp_get_team_size(0);
 omp_get_ancestor_thread_num(0);
}
EOF

    $c_compiler $openmp_flag $orig_useropt omp_nested_test.c -o omp_nested_test > /dev/null 2>&1
    ./omp_nested_test > /dev/null 2>&1
    result=$?
    rm -f ./omp_nested_test ./omp_nested_test.c
    if [ $result = "0" ]; then
        echo "yes."
        fixmakeargs="$fixmakeargs TAU_OPENMP_NESTED"
    else
        echo "no."
        echo "OpenMP nested regions are not supported by this compiler."
    fi
    fixmakeargs="$fixmakeargs OPENMP"
    tauoptions="${tauoptions}-openmp"
fi

if [ $opari1 = yes ] ; then
    fixmakeargs="$fixmakeargs KOJAKOPARI"
    fixmakeargs="$fixmakeargs OPARI oparidir=$oparidir"
    tauoptions="${tauoptions}-opari"
fi

if [ $opari = yes ] ; then
    fixmakeargs="$fixmakeargs OPARI2 oparidir=$oparidir"
    if [ $oparicomp = no ] ; then
        if [ "x$fortran_compiler" = "xintel" ] ; then
            oparicomp="$fortran_compiler"
            if [ "x$cxx_compiler" = "xicpx" ] ; then
                    oparicomp="oneapi"
            fi
        fi
        if [ "x$fortran_compiler" = "xpathscale" ] ; then
            oparicomp="$fortran_compiler"
        fi
        if [ "x$fortran_compiler" = "xxlf90_r" ] ; then
            oparicomp="ibm"
        fi
        if [ "x$fortran_compiler" = "xpgi" -o "x$fortran_compiler" = "xpgfortran" ] ; then
            oparicomp="gcc"
        fi
        if [ "x$fortran_compiler" = "xgfortran" -o x$fortran_compiler" = "xgfortran-5" -o x$fortran_compiler" = "xgfortran-6" -o x$fortran_compiler" = "xgfortran-7" -o x$fortran_compiler" = "xgfortran-8" ] ; then
            oparicomp="gcc"
        fi
    fi
    if [ $oparicomp = no ] ; then
        oparicomp="gcc"
# some sane default to prevent it from giving an error in the Opari build.
    fi
    fixmakeargs="$fixmakeargs oparicomp=$oparicomp"
    tauoptions="${tauoptions}-opari"
fi

if [ $vampirtrace = yes ] ; then
    fixmakeargs="$fixmakeargs VAMPIRTRACE vampirtracedir=$vampirtracedir"
    tauoptions="${tauoptions}-vampirtrace"
    if [ $mpi = yes ] ; then
        if [ $openmp = yes ] ; then
            fixmakeargs="$fixmakeargs VAMPIRTRACEOMPI"
        else
            fixmakeargs="$fixmakeargs VAMPIRTRACEMPI"
        fi
    else
        if [ $openmp = yes ] ; then
            fixmakeargs="$fixmakeargs VAMPIRTRACEOMP"
        fi
    fi
fi


######################################################################
# SCOREP HANDLER
######################################################################
if [ $scorep = yes ] ; then
######################################################################
# Install scorep if requested
######################################################################
tauscorep=scorep
tauscoreptgz=$tauscorep.tgz
tauscorepurl=http://tau.uoregon.edu/$tauscoreptgz
if [ "x$scorepdir" = "x" ]; then
  tauscorepdir=${targetdir}/${architecture}
else
  tauscorepdir=$scorepdir
fi
tauscorep_name=scorep

if [ "x$tauscorepdir" != "x" -a -d "$tauscorepdir" ] ; then
  if [ -r $tauscorepdir/$tauscoreptgz ]; then
    echo "NOTE: Copying $tauscorepdir/$tauscoreptgz "
    cp $tauscorepdir/$tauscoreptgz .
    download_scorep=yes
  fi
fi

if [ "x$download_scorep" = xyes ] ; then
  if [ $machine = craycnl ] ; then
    scorepdir=${tauscorepdir}/${tauscorep}-${c_compiler}-${PE_ENV}
  else
    scorepdir=${tauscorepdir}/${tauscorep}-${c_compiler}
  fi

  if [ $tag = yes ] ; then
    scorepdir=${scorepdir}-$tautag
  fi

  if [ "x$otf" != xyes ] ; then
    otf=yes
    otfdir=${scorepdir}
  fi


  prescorepdir=`pwd`

  if [ -r "$scorepdir/bin/scorep" ]; then
    echo "Found: Score-P"
    echo "Score-P download already found, skipping"
  else
    echo "Not found in ${scorepdir}: ${tauscorep}"
    # get the tar file
    if [ ! -f $tauscoreptgz ] ; then
      download "$tauscorepurl" "$tauscoreptgz"
    fi

    # check the tar file
    if [ ! -f $tauscoreptgz ] ; then
      echo "$tauscoreptgz did not download.  Please download it manually from: "
      echo "$tauscorepurl"
      echo "Please enter the directory containing the tarball: ${nnl}"
      read RESPONSE
      if [ -r $RESPONSE/$tauscoreptgz ]; then
        cp $RESPONSE/$tauscoreptgz .
      else
        echo "ERROR: Couldn't copy Score-P tarball from $RESPONSE/$tauscoreptgz"
        exit 1;
      fi
    fi
    /bin/rm -rf scorep-*;
    echo "expanding $tauscoreptgz ..."
    tar -xzf $tauscoreptgz
    echo "removing $tauscoreptgz ..."
    rm $tauscoreptgz
  # Now we build Score-P
    cd scorep-*; mkdir -p build; cd build
    tau_scorep_config_options="../configure --prefix=${scorepdir} --enable-shared --without-opari2 --without-gui --with-libgotcha=download ${TAU_SCOREP_CONFIGURATION_OPTIONS}"
    if [ $papi = yes ]; then
      tau_scorep_config_options="${tau_scorep_config_options} --with-papi=$papidir --with-papi-header=$papidir/include --with-papi-lib=$papidir/$papisubdir"
    fi
    if [ $pdt = yes ]; then
      tau_scorep_config_options="${tau_scorep_config_options} --with-pdt=$pdtdir/${architecture}/bin"
    fi
    if [ $mpi = no ]; then
      tau_scorep_config_options="${tau_scorep_config_options} --without-mpi"
    fi
    if [ $shmem = no ]; then
      tau_scorep_config_options="${tau_scorep_config_options} --without-shmem"
    fi
	if [ $cuda = yes ]; then
		tau_scorep_config_options="${tau_scorep_config_options} --with-libcudart=$(dirname $cudainclude)"
	  fi
	if [ $rocm = yes ] ; then
		tau_scorep_config_options="${tau_scorep_config_options} --with-rocm=${rocmdir}"
	  fi
	
    if [ "x$bfddir" != "x" ] ; then
      tau_scorep_config_options="${tau_scorep_config_options} --with-libbfd=$bfddir"

    fi
    if [ "x$unwind_dir" != "x" ] ; then
      tau_scorep_config_options="${tau_scorep_config_options} --with-libunwind=$unwind_dir"
    fi
    # Add other non-cross compile architectures here!
    if [ $machine = x86_64 -o $machine = ibm64linux -o $machine = ibm64 ]; then
      if [ $gnu = yes ]; then
        tau_scorep_config_options="${tau_scorep_config_options} --with-nocross-compiler-suite=gcc"
      fi
      if [ $intel = yes -a $inteloneapi = no ]; then
        tau_scorep_config_options="${tau_scorep_config_options} --with-nocross-compiler-suite=intel"
      fi
      if [ $intel = yes -a $inteloneapi = yes ]; then
        tau_scorep_config_options="${tau_scorep_config_options} --with-nocross-compiler-suite=oneapi"
      fi
      if [ $ibmxlc = yes  -o $ibmxlc_r = yes ]; then
        tau_scorep_config_options="${tau_scorep_config_options} --with-nocross-compiler-suite=ibm"
      fi
      if [ $pgi = yes ]; then
        tau_scorep_config_options="${tau_scorep_config_options} --with-nocross-compiler-suite=pgi"
      fi
      if [ $c_compiler = amdclang ]; then
        tau_scorep_config_options="${tau_scorep_config_options} --with-nocross-compiler-suite=amdclang"
      fi
      if [ $c_compiler = clang ]; then
        tau_scorep_config_options="${tau_scorep_config_options} --with-nocross-compiler-suite=clang"
      fi

      # Add MS studio here
    fi # machine  = x86_64 or ibm64linux
    echo "Score-P will be configured using $tau_scorep_config_options"
    /bin/rm -f ./x
    echo $tau_scorep_config_options > ./x
    tauscorep_log=tauscorep_config.log
    bash x > ${tauscorep_log} 2>&1 ;
    configresult=$?
    if [ $configresult -eq 0 ] ; then
      tauscorep_log=build.log
      echo "building ${tauscorep}... (see `pwd`/${tauscorep_log} for log)..."
      make -j 32 > ${tauscorep_log} 2>&1 ;
      makeresult=$?
      if [ $makeresult -eq 0 ] ; then
        tauscorep_log=install.log
        echo "installing ${tauscorep} to ${scorepdir}... (see `pwd`/${tauscorep_log} for log)..."
        make install > ${tauscorep_log} 2>&1 ;
      else
        echo "Error building ${tauscorep}... (see `pwd`/${tauscorep_log} for log)..."
	exit 1;
      fi
    else
      echo "ERROR: Score-P configure failed. See `pwd`/${tauscorep_log} for log"
      exit 1;
    fi
  fi
  cd $prescorepdir
fi # -scorep=download

######################################################################
# SCOREP Configuration
######################################################################
    #Version check
    scorep_version=$(${scorepdir}/bin/scorep-config --version)
    scorep_major_version=$(echo "$scorep_version" | cut -d. -f1)
    if [ "$scorep_major_version" -lt 2 ]; then
        echo "ERROR: Unsupported Score-P Version Detected: ${scorep_version}" >&2
        exit 1
    fi
    echo "NOTE: Detected Score-P ${scorep_version}."
    fixmakeargs="$fixmakeargs SCOREP scorepdir=$scorepdir"
    tauoptions="${tauoptions}-scorep"

    #Metadata check and activation
    scorepmetadata=$(nm -A ${scorepdir}/lib/*.a ${scorepdir}/lib/*.so 2>/dev/null | grep -c SCOREP_Tau_AddLocationProperty)
    if [ "$scorepmetadata" != 0 ]; then
      fixmakeargs="$fixmakeargs SCOREPMETADATA"
    fi

    ###Construct adapter arguments
    adapter_string="--user"

    # MPP (MPI/SHMEM)
    if [ "$mpi" = "yes" ]; then
      adapter_string="$adapter_string --mpp=mpi"
    elif [ "$shmem" = "yes" ]; then
      adapter_string="$adapter_string --mpp=shmem"
    else
      adapter_string="$adapter_string --mpp=none"
    fi

    # Accelerator flags
	#use_component naming scheme helps with iteration
    use_cuda="${cuda}"
	use_hip="${rocm}"
    for component in cuda opencl openacc hip; do
  # read use_<component> into requested
  eval requested=\$use_${component}

  if [ "x${requested}" = "xyes" ]; then
    # Try to enable: only keep it if scorep-config accepts it for a real command
    if "${scorepdir}/bin/scorep-config" --libs ${adapter_string} --${component} >/dev/null 2>&1; then
      adapter_string="${adapter_string} --${component}"
    else
      echo "WARNING: '${component}' requested, but Score-P cannot enable it; forcing --no${component}." >&2
      if "${scorepdir}/bin/scorep-config" --libs ${adapter_string} --no${component} >/dev/null 2>&1; then
        adapter_string="${adapter_string} --no${component}"
      fi
    fi
  else
    # Not requested.
    if "${scorepdir}/bin/scorep-config" --libs ${adapter_string} --no${component} >/dev/null 2>&1; then
      adapter_string="${adapter_string} --no${component}"
    fi
  fi
	done

    # Threads
    if [ "$pthread" = "yes" ]; then
      adapter_string="$adapter_string --thread=pthread"
    fi

    # OpenMP policy:
    # - If opari=yes: honor it, detect dir, and set SCOREPOPARI2
    # - Else if openmp=yes: prefer non-Opari backend
    #   If scorep-config tries to provide Opari/POMP, do not pass --thread=omp to adapter-init.
    #   Instead, add the basic fork-join OpenMP lib manually to the link line.
    extra_manual_libs=""
    use_manual_forkjoin_omp="no"
    if [ "$openmp" = "yes" ]; then
      if [ "$opari" = "yes" ]; then
        echo "NOTE: Using Opari2 backend for OpenMP."
        adapter_string="$adapter_string --thread=omp --pomp"
        # Opari2 bin dir detection/specification
        scorep_opari_support=$(${scorepdir}/bin/scorep-info config-summary 2>/dev/null | grep -iE 'opari[2-9] support')
        if echo "$scorep_opari_support" | grep -q "internal"; then
          scorep_opari_bin_dir="${scorepdir}/bin"
        else
          scorep_opari_bin_dir=$(echo "$scorep_opari_support" | sed -e 's@.*via @@g' -e 's@opari2-config@@g')
        fi
        if [ -x "$scorep_opari_bin_dir/opari2" ]; then
          fixmakeargs="$fixmakeargs scorep_opari_bin_dir=$scorep_opari_bin_dir SCOREPOPARI2"
        fi
      else
        # Probe what scorep-config would do for --thread=omp
        test_libs=$(${scorepdir}/bin/scorep-config --libs ${adapter_string} --thread=omp 2>/dev/null)
        if echo "${test_libs}" | grep -qiE 'opari|pomp'; then
          echo "NOTE: scorep-config emits Opari/POMP for OpenMP; avoiding --thread=omp and using fork-join OpenMP wrapper."
          use_manual_forkjoin_omp="yes"
          # Do NOT append --thread=omp to adapter_string; avoid Opari in adapter-init
          extra_manual_libs="-lscorep_thread_fork_join_omp"
        else
          echo "NOTE: Using Score-P OpenMP wrapper"
          adapter_string="$adapter_string --thread=omp"
        fi
      fi
    fi
    ###Adapter arguments complete.

    ###Collect flags from scorep-config
    rpath_fix_expr='s@-Wl,-rpath[[:space:]]-Wl,/@-Wl,-rpath,/@g'
    scorepLDflags=$(${scorepdir}/bin/scorep-config --ldflags ${adapter_string} | sed -e "$rpath_fix_expr" -e 's@ @#@g')

    scorepLibs=$(${scorepdir}/bin/scorep-config --libs ${adapter_string})
    # If we drop --thread=omp to avoid Opari add the fork-join OpenMP lib explicitly
    if [ "$use_manual_forkjoin_omp" = "yes" ]; then
      scorepLibs="${scorepLibs} ${extra_manual_libs}"
    fi
    scorepLibs=$(echo "$scorepLibs" | sed -e 's@ @#@g')

    scorepIncs=$(${scorepdir}/bin/scorep-config --cppflags | sed -e 's@ @#@g')

    ###Set make variables
    fixmakeargs="$fixmakeargs scorepmpilibs=\$(TAU_LIBS)\#${scorepLDflags}\#${scorepLibs}"
    if [ "$mpi" = "no" ]; then
      fixmakeargs="$fixmakeargs scoreplibs=-L${scorepdir}/lib\#${scorepLDflags}\#${scorepLibs}"
    fi
    fixmakeargs="$fixmakeargs scorepincs=${scorepIncs}"

    ### Generate scorep adapter code
    fixmakeargs="$fixmakeargs SCOREP_ADAPTER_INIT"
    echo "${scorepdir}/bin/scorep-config ${adapter_string} --adapter-init"
    ${scorepdir}/bin/scorep-config ${adapter_string} --adapter-init > src/Profile/TauScorePAdapterInit.c
fi


######################################################################
# Install likwid if requested
######################################################################
liblikwid=likwid-update
liblikwidtgz=$liblikwid.tgz
liblikwidurl=http://www.cs.uoregon.edu/research/tau/$liblikwidtgz

if [ "x$likwid" = "xyes" ]; then
    if [ "x$likwiddir" = "x" ]; then
        liblikwiddir=${targetdir}/${architecture}/likwid
    else
        liblikwiddir=$likwiddir
    fi
    echo "liblikwiddir = $liblikwiddir"

    if [ "x$likwiddir" != "x" -a -d "$likwiddir" ]; then
        if [ -r $likwiddir/$liblikwidtgz ]; then
            echo "NOTE: Copying $likwiddir/$liblikwidtgz "
            cp $likwiddir/$liblikwidtgz .
            download_likwid=yes
        fi
    fi

    if [ $download_likwid = yes ]; then
        echo "Downloading likwid"
        if [ -r "$liblikwiddir/lib/liblikwid.so" ]; then
            echo "Found: liblikwid.so"
            echo "liblikwid download already found, skipping"
        else
            echo "Not found in ${likwiddir}: liblikwid.so"
            if [ ! -f $liblikwidtgz ]; then
                download "$liblikwidurl" "$liblikwidtgz"
            fi

            # check the tar file
            if [ ! -f $liblikwidtgz ]; then
                echo "$liblikwidtgz did not download. Please download it manually from:"
                echo "$liblikwidurl"
                echo "Please enter the directory containing the tarball: ${nnl}"
                read RESPONSE
                cp $RESPONSE/$liblikwidtgz .
            fi
            echo "expanding $liblikwidtgz..."
            tar -zxf  $liblikwidtgz
            echo "removing $liblikwidtgz..."
            /bin/rm -f $liblikwidtgz
            cd likwid*; make PREFIX=$liblikwiddir BUILDDAEMON=false ACCESSMODE=perf_event BUILDFREQ=false MAX_NUM_THREADS=288; make PREFIX=$liblikwiddir BUILDDAEMON=false ACCESSMODE=perf_event BUILDFREQ=false MAX_NUM_THREADS=288 install  ; cd ..

        fi

    fi

    if [ -r $liblikwiddir/lib/liblikwid.so ]; then
        echo "Success..."
        fixmakeargs="$fixmakeargs TAU_LIKWID likwiddir=$liblikwiddir"
        tauoptions="${tauoptions}-likwid"
    fi
fi



######################################################################
# Install libotf2 if requested
######################################################################
libotf2=otf2
libotf2_dirname=otf2-3.0.3
libotf2tgz=otf2-3.0.3.tgz
libotf2url=http://www.cs.uoregon.edu/research/tau/$libotf2tgz

if [ "x$otfdir" = "x" ]; then
    libotf2dir=${targetdir}/${architecture}
else
    libotf2dir=$otfdir
fi

libotf2_name=otf2
if [ "x$otfdir" != "x" -a -d "$otfdir" ] ; then
    if [ -r $otfdir/$libotf2tgz ]; then
        echo "NOTE: Copying $otfdir/$libotf2tgz "
        cp $otfdir/$libotf2tgz .
        download_otf=yes
    fi
fi


if [ "x$download_otf" = xyes ] ; then
    if [ $machine = craycnl ] ; then
        otfdir=${libotf2dir}/${libotf2}-${c_compiler}-${PE_ENV}
    else
        otfdir=${libotf2dir}/${libotf2}-${c_compiler}
    fi
    preotfdir=`pwd`


    if [ -r "$otfdir/lib/lib${libotf2_name}.so" -o \
        -r "$otfdir/lib/lib${libotf2_name}.a" -o \
        -r "$otfdir/lib/lib${libotf2_name}.dylib" -o \
        -r "$otfdir/lib64/lib${libotf2_name}.so" -o \
        -r "$otfdir/lib64/lib${libotf2_name}.a" -o \
        -r "$otfdir/lib64/lib${libotf2_name}.dylib" ] ; then
        echo "Found: lib${libotf2_name}"
        echo "libotf2 download already found, skipping"
    else
        echo "Not found in ${otfdir}: lib${libotf2_name}"
        # get the tar file
        if [ ! -f $libotf2tgz ] ; then
            download "$libotf2url" "$libotf2tgz"
        fi

        # check the tar file
        if [ ! -f $libotf2tgz ] ; then
            echo "$libotf2tgz did not download.  Please download it manually from: "
            echo "$libotf2url"
            echo "Please enter the directory containing the tarball: ${nnl}"
            read RESPONSE
            cp $RESPONSE/$libotf2tgz .
        else
            echo "expanding $libotf2tgz..."
            tar -xzf $libotf2tgz
            echo "removing $libotf2tgz..."
            rm $libotf2tgz
            if [ ${c_compiler}=mpc_cc -o ${c_compiler}=mpc_icc ]
            then
                otf2_c_compiler=gcc
                otf2_cxx_compiler=g++
            else
                otf2_c_compiler=${c_compiler}
                otf2_cxx_compiler=${cxx_compiler}
            fi

            configresult=0
            makeresult=0
            libotf2_log=configure.log
            cd $libotf2_dirname
            echo "configuring $libotf2... (see `pwd`/${libotf2_log} for log)..."
            PYTHON_FOR_GENERATOR=: ./configure --prefix=${otfdir} CC=${otf2_c_compiler} CXX=${otf2_cxx_compiler} --disable-PYTHON > ${libotf2_log} 2>&1
            configresult=$?
            if [ $configresult -eq 0 ] ; then
                libotf2_log=build.log
                echo "building ${libotf2}... (see `pwd`/${libotf2_log} for log)..."
                make -i > $libotf2_log  2>&1
                makeresult=$?
                if [ $makeresult -eq 0 ] ; then
                    libotf2_log=install.log
                    echo "installing ${libotf2} to ${otfdir}... (see `pwd`/${libotf2_log} for log)..."
                    make -i install > $libotf2_log  2>&1
                    makeresult=$?
                fi
            fi
            if [ $configresult -eq 0 ] && [ $makeresult -eq 0 ] ; then
                # Some systems install to lib64 instead of lib
                if [ -d "$otfdir/lib64" -a ! -d "$otfdir/lib" ] ; then
                    tdir=$otfdir
                    pwddir=${PWD}
                    cd $tdir
                    ln -s lib64 lib
                    cd $pwddir
                fi
                echo "...Success."
            else
                echo "ERROR BUILDING $libotf2!"
                echo "Please check `pwd`/config.log or `pwd`/${libotf2_log}"
            fi
        fi
    fi
    cd $preotfdir
fi

if [ $otf = yes ] ; then
    if [ "$otflib" = "" ] ; then
        if [ -d $otfdir/lib ]; then
            otflib="$otfdir/lib"
        else
            otflib="$otfdir/${architecture}/lib"
        fi
        if [ ! -d $otflib ]; then
          if [ -d $otfdir/lib64 ]; then
            otflib="$otfdir/lib64"
          else
            otflib="$otfdir/${architecture}/lib64"
          fi
        fi
        if [ -d $oflib ]; then
          echo "OTF Library implicitly set to $otflib"
        fi
    fi
    if [ "$otfinc" = "" ] ; then
        if [ -r $otfdir/include/otf.h ]; then
            otfinc="$otfdir/include"
        else
            if [ -r $otfdir/include/open-trace-format/otf.h ]; then
                otfinc="$otfdir/include/open-trace-format"
            fi
        fi
    if [ "$otfinc" = "" ] ; then
        if [ -r $otfdir/include/otf2/otf2.h ]; then
            otfinc="$otfdir/include"
        fi
    fi
        echo "OTF Header directory implicitly set to $otfinc"
    fi
    if [ ! -d $otflib ]
    then
        echo "Error: No available OTF support. Library not found at $otflib."
        exit 1
    fi
    if [ "$otfinc" = "" ] || [ ! -d $otfinc ]
    then
	if [ $scorep = yes ] ; then
	echo "Warning: OTF support not detected with scorep. Disabling OTF support."
	otf=no
	else
        echo "Error: No available OTF support. Headers not found at $otfinc."
        exit 1
	fi
    fi
    if [ $ebs2otf = yes -a ! -f $otfinc/OTF_Version.h ] ; then
        echo "Error: OTF_Version.h cannot be found. EBS will not support this version of OTF."
        exit 1
        # *CWL* Need version control code here. Still trying to decide the
        #    best way about it. The above conditional will at least trim away
        #    ancient versions of OTF.
    fi

    fixmakeargs="$fixmakeargs otflib=$otflib otfinc=$otfinc"
fi

if [ $epilog = yes ] ; then

    if [ -f $epiloglibdir/libcubew3.a ] ; then
        epilogextralinkcmd="$epilogextralinkcmd#-lcubew3"
    fi

    if [ -f $epiloglibdir/libepk.util.a ] ; then
        epilogextralinkcmd="$epilogextralinkcmd#-lepk.util"
    fi
    if [ $scalasca = yes -a -f /usr/lib/libswclock.a ] ; then
        epilogextralinkcmd="$epilogextralinkcmd#-lswclock"
    fi

    if [ -f $epiloglibdir/libsz.a ] ; then
        epilogextralinkcmd="$epilogextralinkcmd#-lsz"
    fi

    if [ -f $epiloglibdir/libsc.z.a ] ; then
        epilogextralinkcmd="$epilogextralinkcmd#-lsc.z"
    fi

    fixmakeargs="$fixmakeargs EPILOG epilogdir=$epilogdir epiloglibdir=$epiloglibdir epilogbindir=$epilogbindir epilogextralinkcmd=$epilogextralinkcmd epilogincdir=$epilogincdir"
    tauoptions="${tauoptions}-epilog"
    if [ $scalasca = yes ] ; then
        tauoptions="${tauoptions}-scalasca"
    fi
    if [ $mpi = yes ] ; then
        if [ $openmp = yes ] ; then
            fixmakeargs="$fixmakeargs EPILOGOMPI"
        else
            fixmakeargs="$fixmakeargs EPILOGMPI"
        fi
    else
        if [ $openmp = yes ] ; then
            fixmakeargs="$fixmakeargs EPILOGOMP"
        fi
    fi
fi


if [ -d $otfdir/include/otf2 ]; then
    fixmakeargs="$fixmakeargs OTF2 otfdir=$otfdir"
else
    if [ $otf = yes ] ; then
	if [ -r $otfinc/otf.h ]; then
        fixmakeargs="$fixmakeargs OTF otfdir=$otfdir"
        if [ ! -d $otfdir/${architecture} -a -d $otfdir/src ] ; then
            fixmakeargs="$fixmakeargs OTFSRC"
        fi
        if [ -r $otfdir/lib/libopen-trace-format.a ]; then
            fixmakeargs="$fixmakeargs OTFLIB_LONGNAME"
        fi
	else
		echo "WARNING: OTF include file not found. Not using OTF."
	fi
    fi
fi


if [ $dyninstinc = yes ] ; then
    fixmakeargs="$fixmakeargs dyninstinc=$dyninstincdir DYNINST_INC"
    #dyninstdir=$dyninstincdir/..
    dyninst=yes
fi

if [ $dyninstlib = yes ] ; then
    fixmakeargs="$fixmakeargs dyninstlib=$dyninstlibdir DYNINST_LIB"
    dyninst=yes
fi

if [ $boost = yes ] ; then
    fixmakeargs="$fixmakeargs BOOST "
    if [ "x$boostdir" != "x" -a -d $boostdir -a -d $boostdir/include ]; then
      fixmakeargs="$fixmakeargs boostinc=-I$boostdir/include"
    fi
    if [ "x$boostdir" != "x" -a -d $boostdir -a -d $boostdir/lib ]; then
      fixmakeargs="$fixmakeargs boostlib=-L$boostdir/lib#-Wl,-rpath,$boostdir/lib"
    fi
fi

if [ $sos = yes ] ; then
    tauoptions="${tauoptions}-sos"
    fixmakeargs="$fixmakeargs SOS "
    fixmakeargs="$fixmakeargs sosdir=$sosdir"
fi

if [ $adios = yes ] ; then
        echo "Detected ADIOS..."
    tauoptions="${tauoptions}-adios"
    fixmakeargs="$fixmakeargs ADIOS "
    fixmakeargs="$fixmakeargs adiosdir=$adiosdir"
fi

if [ $adios2 = yes ] ; then
        echo "Detected ADIOS2..."
    adios2_config_cmd=${adios2dir}/bin/adios2-config
    echo "Querying ADIOS2 CXX flags..."
    adios2cxxflags=`${adios2_config_cmd} --c-flags | grep -v ADIOS2_DIR | sed -e 's/[[:space:]]*$//' | sed 's/ /#/g'`
    echo "Querying ADIOS2 LIB flags..."
    adios2libs=`${adios2_config_cmd} --cxx-libs | sed -e 's/[[:space:]]*$//' | sed 's/ /#/g' | sed 's/,/\\\\,/g'`
    tauoptions="${tauoptions}-adios2"
    fixmakeargs="$fixmakeargs ADIOS2"
    fixmakeargs="$fixmakeargs adios2dir=$adios2dir"
    fixmakeargs="$fixmakeargs adios2cxxflags=${adios2cxxflags}"
    fixmakeargs="$fixmakeargs adios2libs=${adios2libs}"
fi

if [ $sqlite3 = yes ] ; then
        echo "Detected SQLITE3..."
    fixmakeargs="$fixmakeargs SQLITE3 "
    fixmakeargs="$fixmakeargs sqlite3dir=$sqlite3dir"
fi

if [ $mochi = yes ] ; then
    echo "Detected Mochi..."
    mochiincludes=`pkg-config --cflags-only-I soma-client soma-server soma-admin uuid | sed -e 's/[[:space:]]*$//' | sed 's/ /#/g'`
    # The uuid library is stupid, it's pkgconfig is broken. trim the last /uuid off the end if necessary.
    uuidincludes=`pkg-config --cflags-only-I uuid`
    if [[ "$uuidincludes" == */uuid ]] ; then
        uuidincludes=${uuidincludes::-5}
    fi
    mochiincludes="${mochiincludes}#${uuidincludes}"
    mochilibs=`pkg-config --libs soma-client soma-server soma-admin | sed -e 's/[[:space:]]*$//' | sed 's/ /#/g'`
    mochirpath=`pkg-config --libs soma-client soma-server soma-admin | sed -e 's/[[:space:]]*$//' | sed 's/ /#/g'`
    replace="-L"
    replacewith="-Wl,-rpath,"
    mochirpath="${mochirpath//${replace}/${replacewith}}"
    echo ${mochirpath}
    #mochilibs="${mochilibs}#${mochirpath}"
    fixmakeargs="$fixmakeargs MOCHI "
    fixmakeargs="$fixmakeargs mochiincludes=${mochiincludes}#-DTAU_MOCHI"
    fixmakeargs="$fixmakeargs mochilibs=${mochilibs}"
fi

#if [ $caliper = yes ] ; then
#    tauoptions="${tauoptions}-caliper"
#    fixmakeargs="$fixmakeargs CALIPER "
#    fixmakeargs="$fixmakeargs caliperdir=$caliperdir"
#fi

if [ $dyninst = yes ] ; then
    fixmakeargs="$fixmakeargs DYNINST "
    if [ "x$dyninstdir" != "x" ]; then
      fixmakeargs="$fixmakeargs dyninstdir=$dyninstdir DYNINSTDIR"
      if [ ! -d $dyninstdir/lib ] ; then
            fixmakeargs="$fixmakeargs DYNINST41"
      fi
    fi
    if [ "x$dyninstdir" != "x" ]; then
        if [ `grep isStaticExecutable $dyninstdir/include/* | wc -l` = 0 ]; then
        fixmakeargs="$fixmakeargs DYNINST_STATIC_REWRITING_UNSUPPORTED"
        fi
    else
        if [ `grep isStaticExecutable $dyninstincdir/* | wc -l` = 0 ]; then
        fixmakeargs="$fixmakeargs DYNINST_STATIC_REWRITING_UNSUPPORTED"
        fi
    fi
    if [ ! -d $dyninstdir/src/include -o ! -d $dyninstdir/include ] ; then
        fixmakeargs="$fixmakeargs DYNINST5.2"
    fi
    if [ -r $dyninstdir/$PLATFORM/lib/libcommon.so -o -r $dyninstdir/lib/libcommon.so -o -r $dyinstlibdir/libcommon.so ] ; then
        fixmakeargs="$fixmakeargs DYNINST6"
    fi
    if [ -r $dyninstdir/$PLATFORM/lib/libparseAPI.so -o -r $dyninstdir/lib/libparseAPI.so -o $dyninstlibdir/libparseAPI.so ] ; then
        fixmakeargs="$fixmakeargs DYNINST7"
    fi
    if [ -r $dyninstdir/include/walker.h -o $dyninstincdir/walker.h ] ; then
        fixmakeargs="$fixmakeargs DYNINST8"
    fi
    if [ -r $dyninstdir/include/version.h ] ; then
        fixmakeargs="$fixmakeargs DYNINST9"
    fi
    if [ -r $dyninstdir/include/dyninstversion.h ] ; then
        fixmakeargs="$fixmakeargs DYNINST12"
    fi
    if [ ! -f $dyninstdir/$PLATFORM/lib/libdynDwarf.so ] ; then
        fixmakeargs="$fixmakeargs DYNINST14"
    fi
    if [ $machine = arm64_linux ]; then
      fixmakeargs="$fixmakeargs DYN_ARMLINUX64"
    fi
fi


######################################################################
# Install libdwarf if requested
######################################################################
libdwarf=libdwarf
#libdwarf_dirname=libdwarf-20181024
#libdwarf_dirname=libdwarf-0.6.0
libdwarf_dirname=libdwarf-0.7.0
libdwarftgz=$libdwarf_dirname.tar.gz
libdwarfurl=http://www.cs.uoregon.edu/research/paracomp/tau/tauprofile/dist/$libdwarftgz

libelf_dirname=libelf-devel-0.158-6.1.x86_64

libelftgz=$libelf_dirname.tgz
libelfurl=http://www.cs.uoregon.edu/research/paracomp/tau/tauprofile/dist/$libelftgz
if [ "x$elfdir" = "x" ]; then
  libelfdir=${targetdir}/${architecture}/${libelf_dirname}
else
  libelfdir=$elfdir
  if [ -r ${libelfdir}/lib64/libelf.so ]; then
    libelflibdir=lib64
  fi
  if [ -r ${libelfdir}/lib/libelf.so ]; then
    libelflibdir=lib
  fi
fi


if [ "x$dwarfdir" = "x" ]; then
    libdwarfdir=${targetdir}/${architecture}
else
    libdwarfdir=$dwarfdir
fi

libdwarf_name=dwarf
if [ "x$dwarfdir" != "x" -a -d "$dwarfdir" ] ; then
    if [ -r $dwarfdir/$libdwarftgz ]; then
        echo "NOTE: Copying $dwarfdir/$libdwarftgz "
        cp $dwarfdir/$libdwardtgz .
        download_dwarf=yes
    fi
fi


if [ "x$download_dwarf" = xyes ] ; then
    if [ $machine = craycnl ] ; then
        dwarfdir=${libdwarfdir}/${libdwarf}-${c_compiler}-${PE_ENV}
    else
        dwarfdir=${libdwarfdir}/${libdwarf}-${c_compiler}
    fi
    predwarfdir=`pwd`


    if [ -r "$dwarfdir/lib/lib${libdwarf_name}.so" -o \
        -r "$dwarfdir/lib/lib${libdwarf_name}.a" -o \
        -r "$dwarfdir/lib/lib${libdwarf_name}.dylib" -o \
        -r "$dwarfdir/lib64/lib${libdwarf_name}.so" -o \
        -r "$dwarfdir/lib64/lib${libdwarf_name}.a" -o \
        -r "$dwarfdir/lib64/lib${libdwarf_name}.dylib" ] ; then
        echo "Found: lib${libdwarf_name}"
        echo "libdwarf download already found, skipping"
    else
        echo "Not found in ${dwarfdir}: lib${libdwarf_name}"
        # get the tar file
        if [ ! -f $libdwarftgz ] ; then
            download "$libdwarfurl" "$libdwarftgz"
        fi

        # check the tar file
        if [ ! -f $libdwarftgz ] ; then
            echo "$libdwarftgz did not download.  Please download it manually from: "
            echo "$libdwarfurl"
            echo "Please enter the directory containing the tarball: ${nnl}"
            read RESPONSE
            cp $RESPONSE/$libdwarftgz .
        else
            echo "expanding $libdwarftgz..."
            tar -xzf $libdwarftgz
            echo "removing $libdwarftgz..."
            rm $libdwarftgz
            if [ ${c_compiler} = mpc_cc -o ${c_compiler} = mpc_icc ]
            then
                dwarf_c_compiler=gcc
                dwarf_cxx_compiler=g++
            else
                dwarf_c_compiler=${c_compiler}
                dwarf_cxx_compiler=${cxx_compiler}
            fi

            configresult=0
            makeresult=0
            libdwarf_log=configure.log
            cd $libdwarf_dirname
            echo "configuring $libdwarf... (see `pwd`/${libdwarf_log} for log)..."
# Before configuring libdwarf we need to check if libelf is there!
            if [ ! -f /usr/include/libelf.h ]; then
              if [ ! -d ${libelfdir} ]; then
                if [ "x`uname -m`" = "xx86_64" ]; then
                  echo "TAU: Dwarf needs libelf.h, but this package (libelf-devel) is not installed. Using a binary libelf package for x86_64."
                  download "$libelfurl" "$libelftgz"
                  tar -zxf $libelftgz
                  echo "removing $libelftgz..."
                  rm $libelftgz
                  mv $libelf_dirname ${targetdir}/${architecture}
                else
                  echo "TAU: Dwarf needs libelf.h, but this package (libelf-devel) is not installed."
                  echo "Please configure TAU with and additional -elf=<dir> configuration option after installing it from the source code available from:"
                  echo "[https://sourceware.org/elfutils/ftp/elfutils-latest.tar.bz2] using ./configure --enable-shared --prefix=<dir>; make install."
                fi
              else
                echo "TAU: Found libelf in ${libelfdir}. Using it."
              fi
              elf=yes
# Dwarf needs a libelf.h in its libdwarf directory and libelf.so in the lib dir. Symlinks.
              if [ ! -L src/lib/libdwarf/libelf.h ]; then
                pushd src/lib/libdwarf; ln -s ${libelfdir}/include/libelf.h . ; ln -s ${libelfdir}/include/libelf . ; popd;
              fi
              if [ ! -L ${dwarfdir}/lib/libelf.so ]; then
                if [ -r ${libelfdir}/lib64/libelf.so ]; then
                  libelflibdir=lib64
                fi
                if [ -r ${libelfdir}/lib/libelf.so ]; then
                  libelflibdir=lib
                fi
                mkdir -p ${dwarfdir}; mkdir -p ${dwarfdir}/lib; ln -s ${libelfdir}/${libelflibdir}/libelf.so* ${dwarfdir}/lib; ln -s ${libelfdir}/${libelflibdir}/libelf.a ${dwarfdir}/lib;
              fi
            else
              echo "Found /usr/include/libelf.h"
              libelfdir=/usr
              systemlibelf=yes
            fi
# End of libelf check. Continue with libdwarf build.
            if [ $elf=yes -a $systemlibelf=no ]; then
              if [ $machine = x86_64 -o $machine = craycnl ]; then
                orig_cflags=`echo $CFLAGS`
                export CFLAGS="$orig_cflags -I${libelfdir}/include"
                export DWARF_LIBS="-L${libelfdir}/lib64 -L${libelfdir}/lib"
                echo "TAU: Using CFLAGS=$CFLAGS to compile libdwarf"
              fi
            fi
            ./configure --prefix=${dwarfdir} --enable-static --enable-shared CC=${dwarf_c_compiler} CXX=${dwarf_cxx_compiler} > ${libdwarf_log} 2>&1
            configresult=$?
            if [ $configresult -eq 0 ] ; then
                libdwarf_log=build.log
                echo "building ${libdwarf}... (see `pwd`/${libdwarf_log} for log)..."
                make -i > $libdwarf_log  2>&1
                makeresult=$?
                if [ $makeresult -eq 0 ] ; then
                    libdwarf_log=install.log
                    echo "installing ${libdwarf} to ${dwarfdir}... (see `pwd`/${libdwarf_log} for log)..."
                    make -i install > $libdwarf_log  2>&1
                    makeresult=$?
                fi
            fi
            if [ $configresult -eq 0 ] && [ $makeresult -eq 0 ] ; then
                # Some systems install to lib64 instead of lib
                if [ -d "$dwarfdir/lib64" -a ! -d "$dwarfdir/lib" ] ; then
                    tdir=$dwarfdir
                    pwddir=${PWD}
                    cd $tdir
                    ln -s lib64 lib
                    cd $pwddir
                fi
                echo "...Success."
            else
                echo "ERROR BUILDING $libdwarf!"
                echo "Please check `pwd`/config.log or `pwd`/${libdwarf_log}"
            fi
            if [ $elf=yes ]; then
              export CFLAGS=$orig_cflags
            fi
        fi
    fi
    cd $predwarfdir
fi

if [ $dwarf = yes ] ; then
    if [ ! -r $dwarfdir/lib/libdwarf.a ] ; then
        echo "ERROR: $dwarfdir/lib/libdwarf.a not found"
    else
    echo "Using libdwarf at $dwarfdir"
        fixmakeargs="$fixmakeargs dwarfopts=-L$dwarfdir/lib#-Wl\,-rpath\,$dwarfdir/lib"

    if [ -r $dwarfdir/include/libdwarf.h ] ; then
        fixmakeargs="$fixmakeargs dwarfinc=-I$dwarfdir/include"
        echo "Using libdwarf.h at $dwarfdir/include"
    elif [ -r $dwarfdir/include/libdwarf-0/libdwarf.h ] ; then
        fixmakeargs="$fixmakeargs dwarfinc=-I$dwarfdir/include/libdwarf-0"
        echo "Using libdwarf.h at $dwarfdir/include/libdwarf-0"
    else
        echo "error: couldn't find libdwarf.h"
    fi

    if [ -r ${libelfdir}/${libelflibdir}/libelf.so ] ; then
       echo "Using libelf at $libelfdir"
       elfline="-L${libelfdir}/lib#-Wl\,-rpath\,${libelfdir}/lib#"
    fi

    fixmakeargs="$fixmakeargs dwarflib=$elfline-L$dwarfdir/lib#-Wl\,-rpath\,$dwarfdir/lib#-ldwarf#-lz#-lelf"
    fixmakeargs="$fixmakeargs TAU_DWARF"
    fi
fi

if [ $papi = yes ] ; then
    if [ $pthread = yes -o $openmp = yes ] ; then
        fixmakeargs="$fixmakeargs PAPIPTHREAD"
    fi
fi

if [ $opari = yes ] ; then
    if [ $pthread = yes -a $openmp = no -o $smarts = yes -o $java = yes ] ; then
        echo "ERROR: -opari option requires OpenMP threads package."
        echo "*****************************************************"
        exit 1
    fi
    if [ $opari_construct = yes -a $opari_region = no ] ; then
        fixmakeargs="$fixmakeargs OPARI_CONSTRUCT"
    fi
    if [ $opari_construct = no -a $opari_region = yes ] ; then
        fixmakeargs="$fixmakeargs OPARI_REGION"
    fi
fi
if [ $opari1 = yes ] ; then
    if [ $pthread = yes -o $smarts = yes -o $java = yes ] ; then
        echo "ERROR: -opari option requires OpenMP threads package."
        echo "*****************************************************"
        exit 1
    fi
    if [ $opari_construct = yes -a $opari_region = no ] ; then
        fixmakeargs="$fixmakeargs OPARI_CONSTRUCT"
    fi
    if [ $opari_construct = no -a $opari_region = yes ] ; then
        fixmakeargs="$fixmakeargs OPARI_REGION"
    fi
fi

if [ $upc_compiler = cc -o $upc_compiler = default ]; then
    if [ $machine = craycnl -a "x$PE_ENV" = xCRAY -a "x$CRAY_DMAPP_INCLUDE_OPTS" != "x" ]; then
        fixmakeargs="$fixmakeargs CRAY_UPC"
    fi
fi

if [ $machine = arm64_linux -a "x$CRAY_CPU_TARGET" != "x" ]; then
  echo "ERROR: This system was detected as a Cray not arm64_linux!"
  echo "Please configure with -arch=craycnl instead of -arch=arm64_linux"
  echo "----------------------------------------------------------------"
  echo " "
  exit 0;
fi

if [ $machine = craycnl -a "x`uname -m`" == "xaarch64" ]; then
  fixmakeargs="$fixmakeargs TAU_CRAY_AARCH64"
  tau_cray_arm64=yes
  # we need to copy the right jogl files here for ARM64
  JARTARGET=${targetdir}/${architecture}/lib
  gunzip -c tools/src/contrib/jogl/arm64_linux/libgluegen-rt.so.gz > $JARTARGET/libgluegen-rt.so
  gunzip -c tools/src/contrib/jogl/arm64_linux/libjogl_desktop.so.gz > $JARTARGET/libjogl_desktop.so
  gunzip -c tools/src/contrib/jogl/arm64_linux/libnativewindow_awt.so.gz > $JARTARGET/libnativewindow_awt.so
  gunzip -c tools/src/contrib/jogl/arm64_linux/libnativewindow_x11.so.gz > $JARTARGET/libnativewindow_x11.so
  cp tools/src/contrib/jogl/arm64_linux/gluegen-rt.jar $JARTARGET/gluegen-rt-natives.jar
  cp tools/src/contrib/jogl/arm64_linux/jogl-all.jar $JARTARGET/jogl-all-natives.jar

fi

if [ $upc = yes ]; then
    # If MPI is used -mpi already appears in the name. If it is not, add it
    if [ "x$upcnetwork" != "xmpi" ]; then
        tauoptions="${tauoptions}-$upcnetwork"
    fi

    if [ "$upc_compiler" == "upcc" ] ; then
        tauoptions="${tauoptions}-bupc"
        fixmakeargs="$fixmakeargs BUPC upcnetwork=$upcnetwork"
    elif [ $upc_compiler = gcc -o $upc_compiler = upc ]; then
        tauoptions="${tauoptions}-gupc"
        fixmakeargs="$fixmakeargs GUPC upcnetwork=$upcnetwork"
    else
        # default case: probably Cray upc
        tauoptions="${tauoptions}-upc"
    fi
fi

if [ $trace = yes ] ; then
    if [ $profile = yes ] ; then
        tauoptions="${tauoptions}-profile"
    fi
    if [ $profilestats = yes ] ; then
        tauoptions="${tauoptions}-profilestats"
    fi
    tauoptions="${tauoptions}-trace"
fi

if [ $mpitrace = yes ] ;  then
    tauoptions="${tauoptions}-mpitrace"
fi

if [ $mpit = yes ] ; then
    tauoptions="${tauoptions}-mpit"
fi


if [ $kai = yes ] ; then
    tauoptions="${tauoptions}-kcc"
fi

if [ $pgi = yes ] ; then
    if [ $nvhpc = yes ]; then
      tauoptions="${tauoptions}-nvhpc"
    else
      if [ $cray_nvhpc = no ]; then  # Cray nvhpc already sets -nvidia
        tauoptions="${tauoptions}-pgi"
      fi
    fi
    if [ $openmp = yes ] ; then
        if [ $ompt = yes ] ; then
            echo "Enabling OpenMP Tool support for PGI/NVHPC compiler with -mp=ompt"
                fixmakeargs="$fixmakeargs PGIOPENMPT"
        else
                fixmakeargs="$fixmakeargs PGIOPENMP"
        fi
    fi
fi

if [ $intel = yes ] ; then
    if [ $openmp = yes ] ; then
cat <<EOF > intel_openmptest.c
#include <omp.h>
int main() {
  return omp_get_max_threads();
}
EOF
        if  $c_compiler intel_openmptest.c -qopenmp -o intel_openmptest 1> /dev/null 2>&1 ; then
          fixmakeargs="$fixmakeargs INTELOPENMP"
        else
          if $c_compiler intel_openmptest.c -fopenmp -o intel_openmptest 1> /dev/null 2>&1 ;  then
            fixmakeargs="$fixmakeargs INTEL_FOPENMP"
          else
            echo "Warning: INTEL : Neither -qopenmp nor -fopenmp options work. Going with the older -fopenmp option for now."
            fixmakeargs="$fixmakeargs INTEL_FOPENMP"
          fi
        fi
        /bin/rm -f intel_openmptest.c intel_openmptest

    fi
fi


if [ $gnu = yes ] ; then
    if [ $openmp = yes ] ; then
        fixmakeargs="$fixmakeargs GNUOPENMP"
    fi
fi



if [ "x$fortran_compiler" = "xintel" ] ; then
    intelifort=`which ifort 2>/dev/null`
    if [ "y$intelifort" != "y" ] ; then
        if [ -x $intelifort ]
        then
            echo "Using Intel ifort as the Fortran Compiler"
            fixmakeargs="$fixmakeargs INTELIFORT"
        fi
    fi
fi


# CHECK if Intel 8.0 compilers are available.
if [ $cxx_compiler = icpc -o $cxx_compiler = mpiicpc ] ; then
    echo "Checking version (8.0/8.1+) of Intel compilers"
    # Intel compilers changed from -cxxlib-icc to -cxxlib-gcc (default) in 8.1
    cat << EOF > f1.cpp
#include <iostream>
using namespace std;

extern "C" {
  void foo(int *x) {
    cout << "x = "<<*x<<endl;
  }
}
EOF

    cat << EOF > f2.c
int foo();
int main(int argc, char **argv) {
  return foo();
}
EOF

    $cxx_compiler -c f1.cpp
    icc -c f2.c
    if icc f1.o f2.o -o foo.exe -lcprts 1>/dev/null 2>&1 ; then
        echo "Intel v8.0 compilers used"
    else
        $cxx_compiler -c -cxxlib-icc f1.cpp 1>/dev/null 2>&1
        $cxx_compiler -c f1.cpp
        tau_opt_bits=
        if [ `uname -m` = x86_64 -a $machine = i386_linux ]; then
            # if someone is using the 32-bit intel compilers on an x86_64
            # then we need to use -m32 with g++ to get the 32-bit libstdc++
            tau_opt_bits=-m32
            fixmakeargs="$fixmakeargs INTEL32_ON_64"
        fi
        taugcclibdir=`g++ $tau_opt_bits -print-libgcc-file-name | sed -e 's,[^/]*$,,'`
        taugcclib=`g++ $tau_opt_bits -print-libgcc-file-name `
        taugcclib2dir=`which g++ | sed s/g++/../`
        if icc f1.o f2.o -o foo.exe -L$taugcclib2dir/lib -L$taugcclibdir -lstdc++ $taugcclib 1>/dev/null 2>&1 ; then
            #          echo "Confirmed  - links with gcc libs"
            echo "Intel v8.1+ compilers used"
            if [ "x$taugcclib2dir" != "x/usr/bin/../lib" ] ; then
                if [ "x$machine" = "xx86_64" ] ; then
                    tauextralibopts="taugcclibopts=-L$taugcclib2dir/lib64"
                else
                      tauextralibopts="taugcclibopts=-L$taugcclib2dir/lib"
                fi
                # Here a different version of gcc is used (other than the one installed in /usr/bin)
            fi
            fixmakeargs="$fixmakeargs $tauextralibopts taugcclibdir=$taugcclibdir INTEL81FIX"
        fi
    fi
    /bin/rm -f f1.o f2.o f1.cpp f2.c foo.exe
    # Check over!
fi

if [ $cxx_compiler = icpx -o $cxx_compiler = mpiicpx ] ; then
   fixmakeargs="$fixmakeargs INTEL_ICPX "
   intel_icpx=yes
fi

if [ $cxx_compiler = icpc -o $cxx_compiler = icpx -o $cxx_compiler = mpiicpx -o $cxx_compiler = mpiicpc ] ; then
    echo "Checking version (10.0+) of Intel compilers"
    version=`${cxx_compiler} -v 2>&1`
    s="mpi"
    t="Version 1"
    t1="${cxx_compiler#$s} version 1"
    t2="${cxx_compiler#$s}.orig version 1"
    #Test for versions 20+ as well
    t3="${cxx_compiler#$s} version 2"
    testarg=${version//$t/}
    testarg1=${version//$t1/}
    testarg2=${version#$t2}
    testarg3=${version#$t3}


    if [ "y$testarg" != "y$version" -o "y$testarg1" != "y$version" -o "y$testarg2" != "y$version" -o "y$testarg3" != "y$version" ] ; then
        echo "Intel v10.0+ compilers found"
        fixmakeargs="$fixmakeargs INTEL10FIX COMPINST_GNU"
    else
        echo "Intel v10.0+ compilers NOT found"
    fi

    if [ "x$intelifort" = "xifort" ]; then
      var=`which ifort | sed 's/\/bin\/ifort$//'`
      libdir="$var/lib"
      if [ ! -d "$var" ] ; then
        var=`which ifort | sed 's/\/bin\/intel64\/ifort$//'`
        libdir="$var/lib/intel64"
      fi
      if [ ! -d "$var" ] ; then
        var=`which ifort | sed 's/\/bin\/ia32\/ifort$//'`
        libdir="$var/lib/ia32"
      fi
      if [ ! -d "$var" ] ; then
        var=`which ifort | sed 's/\/bin\/ia64\/ifort$//'`
        libdir="$var/lib/ia64"
      fi
    fi 
    if [ ! -d "$var" ] ; then
        var=`which ifx | sed 's/\/bin\/ifx$//'`
        libdir="$var/lib"
	if [ ! -d "$var" ]; then 
          echo "Error: Unable to identify Intel Fortran lib directory"
	fi
    fi
    if   [ -f "$libdir/for_main.o" ] ; then
       echo "Found $libdir/for_main.o"
    elif [ -f "$libdir/intel64/for_main.o" ] ; then
        libdir="$libdir/intel64"
       echo "Found $libdir/for_main.o"
    elif [ -f "$var/compiler/lib/mic/for_main.o" -a $machine = mic_linux ] ; then
        libdir="$var/compiler/lib/mic"
        echo "Found $libdir/for_main.o"
    elif [ -f "$var/compiler/lib/intel64/for_main.o" -a $machine = x86_64 ] ; then
        libdir="$var/compiler/lib/intel64"
       echo "Found $libdir/for_main.o"
    elif [ -f "$libdir/ia32/for_main.o" ] ; then
        libdir="$libdir/ia32"
    elif [ -f "$libdir/ia64/for_main.o" ] ; then
        libdir="$libdir/ia64"
    fi

    fixmakeargs="$fixmakeargs ifortlibdir=$libdir"
fi

if [ $cxx_compiler = icpc -o $cxx_compiler = mpiicpc ] ; then
    echo "Checking version (12.x) of Intel compilers"
    version=`${cxx_compiler} -v 2>&1`
    t="Version 12"
    t1="${cxx_compiler} version 12"
    testarg=${version#$t}
    testarg1=${version#$t1}

    if [ "y$testarg" != "y$version" -o "y$testarg1" != "y$version" ] ; then
        echo "Intel v12.x compilers found"
        fixmakeargs="$fixmakeargs INTEL12FIX"
    else
        echo "Intel v12.x compilers NOT found"
    fi
fi

if [ $ibmxlc = yes ] ; then
    if [ $openmp = yes ] ; then
        fixmakeargs="$fixmakeargs IBMXLC_OPENMP"
    fi
fi

if [ $machine = hitachi ] ; then
    if [ $openmp = yes ] ; then
        fixmakeargs="$fixmakeargs HITACHI_OPENMP"
    fi
fi

if [ $cxx_compiler = cxx ] ; then
    compaq=yes
    if [ $openmp = yes ] ; then
        fixmakeargs="$fixmakeargs COMPAQCXX_OPENMP"
    fi
    if [ $pthread = yes ] ; then
        fixmakeargs="$fixmakeargs COMPAQCXX_PTHREAD"
    fi
fi


if [ $fujitsu = yes ] ; then
    tauoptions="${tauoptions}-fujitsu"
fi


if [ $epilog = yes -o $vampirtrace = yes ] ; then
    if [ $forceshared = no -a $sicortex = no ] ; then
         disableshared=yes
    fi
fi

if [ $disableshared = yes ] ; then
    fixmakeargs="$fixmakeargs NOSHARED"
fi

if [ $disable_memory_manager = yes ] ; then
    fixmakeargs="$fixmakeargs DISABLE_MEM_MANAGER"
fi

if [ $pmi = yes ] ; then
    fixmakeargs="$fixmakeargs CRAYCNL_PMI"
fi

if [ $disableshared = yes -a $python = yes ] ; then
    echo "ERROR: Shared libraries are disabled, but Python is specified"
    exit 1
fi

if [ $noex = yes ] ; then
    tauoptions="${tauoptions}-noex"
    if [ $kai = yes ] ; then
        fixmakeargs="$fixmakeargs KAINOEX"
    else
        # OTHER COMPILERS NO EXCEPTIONS... SGICC needs this
        fixmakeargs="$fixmakeargs SGICCNOEX"
    fi
fi

# Disable plugins on Apple because rdynamic flag has no effect on OS X
# Removed. We use -dynamiclib and it works
#case "$machine" in
#    apple)
#     echo plugins="no"
#    ;;
#esac

if [ $machine = apple -o $machine = arm64_apple ]; then
#if [ $machine = apple ]; then
    if [ -e /System/Library/PrivateFrameworks/CoreSymbolication.framework ]; then
# Let us test if this works with the current compiler.
cat <<EOF > tau_apple_symbol.cpp
#include "CoreSymbolication.h"
EOF
        echo "Checking for Apple CoreSymbolication... ${nnl}"
        if $cxx_compiler -c tau_apple_symbol.cpp -Iinclude/Profile 1> /dev/null 2>&1 ; then
          echo "yes"
          fixmakeargs="$fixmakeargs CORESYMBOLICATION"
        else
          echo "no"
        fi
        /bin/rm -f tau_apple_symbol.o tau_apple_symbol.cpp
    fi
fi

if [ $plugins = no ] ; then
    fixmakeargs="$fixmakeargs NOPLUGINS"
fi


if [ $hpctoolkit = yes ] ; then
    tauoptions="${tauoptions}-hpctoolkit"


    # check to make sure it is the patched version
    if [ ! -r $hpctoolkit_src_dir/README.TAU ] ; then
        echo "Didn't find patched HPCToolkit source in $hpctoolkit_src_dir"
        exit 1
    fi

    hpctoolkit_include="-I${hpctoolkit_src_dir}/src/tool/hpcrun/unwind/common"
    hpctoolkit_link="-L${hpctoolkit_install_dir}/lib/hpctoolkit#-lcsprof"

    fixmakeargs="$fixmakeargs HPCTOOLKIT hpctoolkit_include=${hpctoolkit_include} hpctoolkit_link=${hpctoolkit_link}"
fi

# TAUmon MPI
if [ $mon_mpi = yes ] ; then
    if [ $mrnet = yes ] ; then
        echo "ERROR: MRNet and MPI cannot simultaneously be used as TAUmon transport"
        exit 1
    fi
    if [ $mpi = yes ] ; then
        fixmakeargs="$fixmakeargs MON_MPI"
    else
        echo "ERROR: MPI TAUmon transport requires MPI support"
        exit 1
    fi
fi

# TAUmon MRNet
if [ $mrnet = yes ] ; then
    if [ $mon_mpi = yes ] ; then
        echo "ERROR: MRNet and MPI cannot simultaneously be used as TAUmon transport"
        exit 1
    fi
    if [ $mpi = yes ] ; then
        fixmakeargs="$fixmakeargs MRNET"
        if [ $mrnet_use_lightweight = yes ] ; then
            fixmakeargs="$fixmakeargs mrnet_lw=-DMRNET_LIGHTWEIGHT"
        fi
        fixmakeargs="$fixmakeargs mrnet_root=${mrnet_root}"
        fixmakeargs="$fixmakeargs mrnet_inc=-I${mrnet_root}/include#-I${mrnet_root}/xplat/include#-I${mrnet_root}/lightweight/include"
        fixmakeargs="$fixmakeargs mrnet_lib=-L${mrnet_lib}#-lmrnet#-lxplat#-lmrnet_lightweight#-lxplat_lightweight#-ldl#-lm"
    else
        echo "ERROR: MRNet TAUmon transport requires MPI support"
        exit 1
    fi
fi

case "$machine" in
ia64|x86_64|i386_linux|bgp|bgq|mips|mips32|ibm64linux|ppc64)
    enable_export_dynamic="yes"
    ;;
apple)
    # Detect if -rdynamic is supported. gcc 4.6 does not support this.
    echo "Checking if -rdynamic is supported... ${nnl}"
    echo "int main(void) { return 0; }" > rdynamic_test.c
    if $c_compiler $orig_useropt rdynamic_test.c -rdynamic >/dev/null 2>&1 ; then
        echo "yes, passing -rdynamic to linker"
        extra_linker_args="${extralinker_args}#-rdynamic"
    else
        echo "no"
    fi
    rm -f rdynamic_test.c
    ;;
esac

if [ "$enable_export_dynamic" = "yes" -a "$pin" = "no" -a "$nvhpc" = "no" ] ; then
    extra_linker_args="${extralinker_args}#-Wl,--export-dynamic"
fi

if [ $cxx_compiler = x86_64-w64-mingw32-g++ ]; then
    extra_linker_args="${extralinker_args}#-Wl,--export-all-symbols,--enable-auto-image-base"
fi

fixmakeargs="$fixmakeargs extra_linker_args=$extra_linker_args"


if [ "${level_zero}" = "yes" -a "${level_zero_legacy}" = "yes" ] ; then
    echo "Forcing legacy Level Zero"
    fixmakeargs="$fixmakeargs LEVEL_ZERO_OLD"
    tauoptions="${tauoptions}-l0legacy"
    if [ "x${mdisc_dir}" = "x" ]; then
        if [ -r /usr/include/metrics_discovery_api.h ]; then
            fixmakeargs="$fixmakeargs mdisc_dir=$mdisc_dir LEVEL_ZERO_METRICS"
            tauoptions="${tauoptions}-l0metrics"
        else
            echo " /usr/include/metrics_discovery_api.h not found, L0 metrics not available."
            echo " If the header is available, use -level_metrics=<dir> and reconfigure TAU."
        fi
    fi

elif [ "${level_zero}" = "yes" -a "${level_zero_new}" = "yes" ] ; then
    echo "Forcing new Level Zero"
    fixmakeargs="$fixmakeargs LEVEL_ZERO"

    if [ "x${mdisc_dir}" = "x" ]; then
        if [ -r /usr/include/metrics_discovery_api.h ]; then
            fixmakeargs="$fixmakeargs mdisc_dir=$mdisc_dir LEVEL_ZERO_METRICS"
            tauoptions="${tauoptions}-l0metrics"
        else
            echo " /usr/include/metrics_discovery_api.h not found, L0 metrics not available."
            echo " If the header is available, use -level_metrics=<dir> and reconfigure TAU."
        fi
    fi

elif [ "${level_zero}" = "yes" ] ; then

cat <<EOF > test_l0.c
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include "level_zero/ze_api.h"
#include "level_zero/zes_api.h"
#include "level_zero/zet_api.h"
#include <vector>

#define PTI_ASSERT(x) assert(x)

inline ze_api_version_t GetDriverVersion(ze_driver_handle_t driver) {
  PTI_ASSERT(driver != nullptr);
  ze_api_version_t version = ZE_API_VERSION_FORCE_UINT32;
  ze_result_t status = zeDriverGetApiVersion(driver, &version);
  PTI_ASSERT(status == ZE_RESULT_SUCCESS);
  return version;
}

inline std::vector<ze_driver_handle_t> GetDriverList() {
  ze_result_t status = ZE_RESULT_SUCCESS;
  uint32_t driver_count = 0;
  status = zeDriverGet(&driver_count, nullptr);
  PTI_ASSERT(status == ZE_RESULT_SUCCESS);
  if (driver_count == 0)
    return std::vector<ze_driver_handle_t>();
  std::vector<ze_driver_handle_t> driver_list(driver_count);
  status = zeDriverGet(&driver_count, driver_list.data());
  PTI_ASSERT(status == ZE_RESULT_SUCCESS);
  return driver_list;
}

inline ze_api_version_t GetZeVersion() {
  auto driver_list = GetDriverList();
  if (driver_list.empty())
    return ZE_API_VERSION_FORCE_UINT32;
  return GetDriverVersion(driver_list.front());
}

int main()
{
        ze_result_t status = ZE_RESULT_SUCCESS;
        status = zeInit(ZE_INIT_FLAG_GPU_ONLY);
        assert(status == ZE_RESULT_SUCCESS);
        ze_api_version_t version = GetZeVersion();
        if ((ZE_MAJOR_VERSION(version)==1)&&(ZE_MINOR_VERSION(version)<6))
                return 1;
        return 0;
}
EOF



    if $cxx_compiler test_l0.c -lze_loader -I$l0dir/include -o test_l0 1> /dev/null 2>&1 ; then
        #echo "$cxx_compiler test_l0.c -lze_loader -I$l0dir/include -o test_l0"
        ./test_l0 1> /dev/null 2>&1
            result=$?
        rm -f ./openacc_test openacc_test.c

        if [ $result = "0" ]; then
            fixmakeargs="$fixmakeargs LEVEL_ZERO"
        else
            fixmakeargs="$fixmakeargs LEVEL_ZERO_OLD"
        fi

    else
        echo "Error compiling L0 version check"
        exit 1
    fi
    
    #fixmakeargs="$fixmakeargs LEVEL_ZERO"
    #fixmakeargs="$fixmakeargs LEVEL_ZERO_OLD"

    if [ "x${mdisc_dir}" = "x" ]; then
        if [ -r /usr/include/metrics_discovery_api.h ]; then
            fixmakeargs="$fixmakeargs mdisc_dir=$mdisc_dir LEVEL_ZERO_METRICS"
            tauoptions="${tauoptions}-l0metrics"
        else
            echo " /usr/include/metrics_discovery_api.h not found, L0 metrics not available."
            echo " If the header is available, use -level_metrics=<dir> and reconfigure TAU."
        fi
    fi

    rm test_l0.c test_l0
fi


#### END OF TAU OPTIONS. Make changes above this line
fixmakeargs="$fixmakeargs tauoptions=$tauoptions"


if [ $use_ansic = yes ] ; then
    fixmakeargs="$fixmakeargs ANSIC"
fi


if [ $sicortex = yes ] ; then
    fixmakeargs="$fixmakeargs SICORTEX extrashlibopts=${sicortexlink}"
fi


#selection of cube reader, external installation or provided .jar file
select_cube_reader_jar
correction_perfdmf_and_paraprof


echo "***********************************************************************"
echo "Installing utilities..."

configure_java

if [ ${tauprefix} = unknown ] ; then
    targetdir=${tauroot}
else
    targetdir=${tauprefix}
    if [ ! -d ${tauprefix} ] ; then
        echo "making directory $tauprefix "
        mkdir -p ${tauprefix}
        cp README LICENSE CREDITS ${tauprefix}
    fi
    if [ ! -d ${tauprefix}/${architecture} ] ; then
        echo "making directory $tauprefix/$architecture lib bin"
        mkdir ${tauprefix}/${architecture}
        mkdir ${tauprefix}/${architecture}/lib
        mkdir ${tauprefix}/${architecture}/bin
    else
        if [ "x$architecture" = "x" ] ; then
            echo "making directory $tauprefix lib bin"
            if [ ! -d ${tauprefix}/bin ] ; then
                mkdir ${tauprefix}/bin
            fi
            if [ ! -d ${tauprefix}/lib ] ; then
                mkdir ${tauprefix}/lib
            fi
        fi
    fi
fi


BINTARGET=${targetdir}/${architecture}/bin
JARTARGET=${targetdir}/${architecture}/lib


# Install tau_compiler.sh
if [ ! -f /bin/bash ] ; then
    bashpath=`which bash`
    if [ ! -f $bashpath ] ; then
        echo "ERROR: You need a working bash shell in your path to use tau_compiler.sh"
        echo "Please add bash to your path and re-run configure."
        echo "**************************************************"
    else
        cat ${tauroot}/tools/src/tau_compiler.sh | sed -e 's#/bin/bash#'$bashpath'#'  > $BINTARGET/tau_compiler.sh
    fi
else
    cp ${tauroot}/tools/src/tau_compiler.sh $BINTARGET
fi

cp ${tauroot}/tools/src/tau_treemerge.pl $BINTARGET
cp ${tauroot}/tools/src/tau_coalesce $BINTARGET
cp ${tauroot}/tools/src/tau_javamax.sh $BINTARGET
cp ${tauroot}/tools/src/tau_header_replace.pl $BINTARGET
cp ${tauroot}/tools/src/tau_exec $BINTARGET
cp ${tauroot}/tools/src/tauex $BINTARGET
cp ${tauroot}/tools/src/taucc $BINTARGET
cp ${tauroot}/tools/src/taucxx $BINTARGET
cp ${tauroot}/tools/src/tauupc $BINTARGET
cp ${tauroot}/tools/src/taucaf $BINTARGET
cp ${tauroot}/tools/src/tauf90 $BINTARGET
cp ${tauroot}/tools/src/tauex.multiple $BINTARGET
cp ${tauroot}/tools/src/tau_cxx.sh $BINTARGET
cp ${tauroot}/tools/src/tau_cc.sh $BINTARGET
cp ${tauroot}/tools/src/tau_show_libs $BINTARGET
cp ${tauroot}/tools/src/tau_rewrite $BINTARGET
cp ${tauroot}/tools/src/tau_pebil_rewrite $BINTARGET
cp ${tauroot}/tools/src/tau_f90.sh $BINTARGET
cp ${tauroot}/tools/src/tau_f77.sh $BINTARGET
cp ${tauroot}/tools/src/tau_upc.sh $BINTARGET
cp ${tauroot}/tools/src/tau_caf.sh $BINTARGET
cp ${tauroot}/tools/src/tau_prof2json.py $BINTARGET
cp ${tauroot}/tools/src/contrib/tcollect10.ini $BINTARGET
# tau_exec works differently when it's named "tau_python"
cp ${tauroot}/tools/src/tau_exec $BINTARGET/tau_python
cp ${tauroot}/tools/src/tau_spark_python $BINTARGET/tau_spark_python
cp ${tauroot}/tools/src/tau_spark-submit $BINTARGET/tau_spark-submit
cp ${tauroot}/tools/src/tau_spark-submit $BINTARGET/tau_pyspark
cp ${tauroot}/tools/src/tau_diff.py $BINTARGET/tau_diff.py

if [ $julia = yes ] ; then
    cp ${tauroot}/tools/src/tau_exec $BINTARGET/tau_julia
fi

if [ $machine = nec-sx-aurora ]; then
# NEC needs VE_LD_LIBRARY_PATH and VE_LD_PRELOAD
   sed -e 's/LD_PRELOAD/VE_LD_PRELOAD/g' -e 's/LD_LIBRARY_PATH/VE_LD_LIBRARY_PATH/g' ${tauroot}/tools/src/tau_exec > $BINTARGET/tau_exec
   chmod +x $BINTARGET/tau_exec
   cp $BINTARGET/tau_exec $BINTARGET/tau_python
fi

if [ $machine = ibm64 ]; then
# IBM AIX needs gmake instead of make in TAU compiler scripts. Which scripts need to be changed?
  for i in `grep "^make -s " ibm64/bin/tau* |  awk '{ print $1;}' | sed -e s@:make@@g`;
  do
    sed -e "s@^make -s@gmake -s@g" ${i} > ${i}~
    mv ${i}~ ${i}
    chmod +x ${i}
  done
fi



if [ $pdtarchdir != unknown ]
then
    pdtarchcompdir=${pdtarchdir}/${pdtcompdir}
else
    pdtarchcompdir=${architecture}
fi
#copy over tau_header_inst.sh
if [ $pdt = yes ] ; then
    if [ $opari1 = yes ] ; then
        cat ${tauroot}/tools/src/tau_header_inst_opari1.sh | sed -e 's,@TAUROOTDIR@,'$targetdir',' -e 's,@ARCH@,'$architecture',' \
            -e 's,@PDTROOTDIR@,'$pdtdir',' -e 's,@PDTARCH@, '$pdtarch',' > ${targetdir}/${architecture}/bin/tau_header_inst.sh
        chmod a+rx ${targetdir}/${architecture}/bin/tau_header_inst.sh
    fi
    if [ $opari = yes ] ; then
        cat ${tauroot}/tools/src/tau_header_inst.sh | sed -e 's,@TAUROOTDIR@,'$targetdir',' -e 's,@ARCH@,'$architecture',' \
            -e 's,@PDTROOTDIR@,'$pdtdir',' -e 's,@PDTARCH@, '$pdtarch',' > ${targetdir}/${architecture}/bin/tau_header_inst.sh
        chmod a+rx ${targetdir}/${architecture}/bin/tau_header_inst.sh
    fi
    if [ -x ${pdtdir}/${pdtarchcompdir}/bin/smaqao ]
    then
        echo "TAU: PDT: Using binary rewriting capabilities from MAQAO in tau_rewrite"
        rm -f ${targetdir}/${architecture}/bin/tau_maqao
        ln -s ${pdtdir}/${pdtarchcompdir}/bin/smaqao ${targetdir}/${architecture}/bin/tau_maqao
        if [ -r ${pdtdir}/${pdtarchcompdir}/bin/tau_rewrite -a ${pdtdir}/${pdtarchcompdir}/bin/tau_rewrite -nt ${tauroot}/tools/src/tau_rewrite ]; then
            # if Maqao has a newer tau_rewrite, use that instead!
            echo "NOTE: Found a newer tau_rewrite in PDT. Using it instead."
            cp ${pdtdir}/${pdtarchcompdir}/bin/tau_rewrite  $BINTARGET
        fi
    fi
    if [ -x ${pdtdir}/${pdtarchcompdir}/bin/pebil.static ]
    then
        echo "TAU: PDT: Using binary rewriting capabilities from PEBIL in tau_pebil_rewrite"
        rm -f ${targetdir}/${architecture}/bin/pebil.static
        ln -s ${pdtdir}/${pdtarchcompdir}/bin/pebil.static ${targetdir}/${architecture}/bin/pebil.static
    fi
fi

# Copy over tau_load.sh
if [ $mpi = yes ] ; then
    cat ${tauroot}/tools/src/tau_load.sh | sed -e 's,@TAUROOTDIR@,'$targetdir',' -e 's,@ARCH@,'$architecture',' > ${targetdir}/${architecture}/bin/tau_load.sh
    chmod a+rx ${targetdir}/${architecture}/bin/tau_load.sh
fi

if [ $ibmmpi = yes ] ; then
    cat ${tauroot}/tools/src/tau_poe | sed -e 's,@TAUROOTDIR@,'$targetdir',' -e 's,@ARCH@,'$architecture',' > ${targetdir}/${architecture}/bin/tau_poe
    chmod a+rx ${targetdir}/${architecture}/bin/tau_poe
fi

cp ${tauroot}/tools/src/jtau_tf/bin/TAU_tf.jar ${targetdir}/${architecture}/lib

cp ${tauroot}/tools/src/tau_multimerge/bin/tau_multimerge.jar ${targetdir}/${architecture}/lib

cat ${tauroot}/tools/src/contrib/slog2sdk/bin/jumpshot.skel | sed -e 's,@TAUROOTDIR@,'$targetdir','  -e 's,@SHELL@,'$taushell',' -e 's,@ARCH@,'$architecture',' > ${targetdir}/${architecture}/bin/jumpshot
chmod a+rx ${targetdir}/${architecture}/bin/jumpshot

cat ${tauroot}/tools/src/contrib/slog2sdk/bin/slog2print.skel |
sed -e 's,@TAUROOTDIR@,'$targetdir','  -e 's,@SHELL@,'$taushell',' -e 's,@ARCH@,'$architecture',' > ${targetdir}/${architecture}/bin/slog2print
chmod a+rx ${targetdir}/${architecture}/bin/slog2print

cat ${tauroot}/tools/src/tau2slog2/bin/tau2slog2.skel |
sed -e 's,@TAUROOTDIR@,'$targetdir','  -e 's,@SHELL@,'$taushell',' -e 's,@ARCH@,'$architecture',' > ${targetdir}/${architecture}/bin/tau2slog2
chmod a+rx ${targetdir}/${architecture}/bin/tau2slog2

cat ${tauroot}/tools/src/tau_multimerge/bin/tau_multimerge.skel |
sed -e 's,@TAUROOTDIR@,'$targetdir','  -e 's,@SHELL@,'$taushell',' -e 's,@ARCH@,'$architecture',' > ${targetdir}/${architecture}/bin/tau_multimerge
chmod a+rx ${targetdir}/${architecture}/bin/tau_multimerge

cp ${tauroot}/tools/src/contrib/slog2sdk/lib/jumpshot.jar ${targetdir}/${architecture}/lib
cp ${tauroot}/tools/src/contrib/slog2sdk/lib/traceTOslog2.jar ${targetdir}/${architecture}/lib
cp ${tauroot}/tools/src/contrib/slog2sdk/lib/slog2printserial.jar ${targetdir}/${architecture}/lib
cp ${tauroot}/tools/src/tau2slog2/bin/tau2slog2.jar ${targetdir}/${architecture}/lib



cat ${tauroot}/tools/src/tau-config.in |
sed -e 's,@TAUROOTDIR@,'$targetdir',' -e 's,@SHELL@,'$taushell',' -e 's,@ARCH@,'$architecture',' -e 's,@PYTHONINTV@,'$pythonintv',' \
    > ${targetdir}/${architecture}/bin/tau-config
chmod a+rx ${targetdir}/${architecture}/bin/tau-config

cp ${tauroot}/tools/src/tauinc.pl ${targetdir}/${architecture}/bin
chmod a+rx  ${targetdir}/${architecture}/bin/tauinc.pl

cp ${tauroot}/tools/src/tau_join.sh ${targetdir}/${architecture}/bin
chmod a+rx  ${targetdir}/${architecture}/bin/tau_join.sh

configure_perfexplorer

cat ${tauroot}/tools/src/TauIL/bin/tau_analyze.skel |
sed -e 's,@TAUROOTDIR@,'$targetdir','  -e 's,@SHELL@,'$taushell',' -e 's,@ARCH@,'$architecture',' \
    > ${targetdir}/${architecture}/bin/tau_analyze
chmod a+rx ${targetdir}/${architecture}/bin/tau_analyze

cat ${tauroot}/tools/src/TauIL/Makefile.inc.skel |
sed -e 's,@TAUROOTDIR@,'$tauroot',' \
    > ${tauroot}/tools/src/TauIL/Makefile.inc

cp ${tauroot}/tools/src/TauIL/bin/*.jar ${targetdir}/${architecture}/lib
cp ${tauroot}/tools/src/tau_throttle.sh ${targetdir}/${architecture}/bin

echo "... done"
echo "***********************************************************************"

# Install the Shared object library for LLVM-OMPT
if [ $ompt = yes -a $install_ompt = yes -a -r ${libomp_ossdir_shared}/lib${libomp_oss_name}.so -a -d ${targetdir}/${architecture}/lib ]; then
  if [ $machine = bgq -o $machine = bgp -o $machine = bgl ] ; then
    echo "BGQ ompt"
  else
    echo "TAU: Copying lib${libomp_oss_name}.so to ${targetdir}/${architecture}/lib/shared${tauoptions} ..."
    ompt_instdir=${targetdir}/${architecture}/lib
    # We also need to put the OpenMP runtime in the tauoptions directory, otherwise tau_exec doesn't find it when the application is not compiled using the TAU wrapper
    ompt_instdir_=${targetdir}/${architecture}/lib/shared${tauoptions}
    mkdir -p ${ompt_instdir}
    mkdir -p ${ompt_instdir_}
    #cp ${libomp_ossdir_shared}/lib${libomp_oss_name}.so ${ompt_instdir}/lib${libomp_oss_name}.so ;
    #cp ${libomp_ossdir_shared}/libomp.so ${ompt_instdir}/libomp.so ;

    # Copy the OpenMP runtime in the tauoptions directory as well
    #cp ${libomp_ossdir_shared}/lib${libomp_oss_name}.so ${ompt_instdir_}/lib${libomp_oss_name}.so ;
    #cp ${libomp_ossdir_shared}/libomp.so ${ompt_instdir_}/libomp.so ;
    cp ${libomp_ossdir_shared}/*.so ${ompt_instdir_}/.

    echo "ompt_dir=$ompt_dir"
    omptlinking="-Wl,-rpath=${ompt_instdir_}#-L${ompt_instdir_}#$omptlibrary"
    fixmakeargs="$fixmakeargs TAU_OMPT_5_0 omptlib=$omptlinking"
    # also put them in the "disable" directory for later builds - but only do it once
    ompt_instdir=${targetdir}/${architecture}/lib/shared-disable

    # apex also needs the headers...
    mkdir -p ${targetdir}/${architecture}/include
    cp ${START_DIR}/include/omp*.h ${targetdir}/${architecture}/include
  fi
elif [ $ompt = yes -a $ompt_compiler_support = yes ] ; then
    # Remove previously copied shared openmp libraries when using compiler libraries
    ompt_instdir=${targetdir}/${architecture}/lib
    ompt_instdir_=${targetdir}/${architecture}/lib/shared${tauoptions}

    rm -f ${ompt_instdir}/lib${libomp_oss_name}.so
    rm -f ${ompt_instdir}/libomp.so
    rm -f ${ompt_instdir_}/lib${libomp_oss_name}.so
    rm -f ${ompt_instdir_}/libomp.so

    ompt_instdir=${targetdir}/${architecture}/lib/shared-disable

    rm -f ${ompt_instdir}/libomp.so
    rm -f ${ompt_instdir}/libgomp.so
    rm -f ${ompt_instdir}/libiomp5.so
fi
# Regardless, remove the libomp.so from ${targetdir}/${architecture}/lib
if [ $ompt = yes ] ; then
    rm -f ${targetdir}/${architecture}/lib/libomp.so
    rm -f ${targetdir}/${architecture}/lib/libgomp.so
    rm -f ${targetdir}/${architecture}/lib/libiomp5.so
fi

# Install the Static object library for LLVM-OMPT
if [ $ompt = yes -a $install_ompt = yes -a -r ${libomp_ossdir_static}/lib${libomp_oss_name}.a -a -d ${targetdir}/${architecture}/lib ]; then
  if [ $machine = bgq -o $machine = bgp -o $machine = bgl ] ; then
    echo "BGQ ompt"
  else
    echo "TAU: Copying lib${libomp_oss_name}.a to ${targetdir}/${architecture}/lib/static${tauoptions} ..."
    ompt_instdir=${targetdir}/${architecture}/lib/static${tauoptions}
    mkdir -p ${ompt_instdir}
    cp ${libomp_ossdir_static}/lib${libomp_oss_name}.a ${ompt_instdir}/lib${libomp_oss_name}.a ;
    cp ${libomp_ossdir_static}/libomp.a ${ompt_instdir}/libomp.a ;
    echo "ompt_dir=$ompt_dir"

    # also put them in the "disable" directory for later builds - but only do it once
    ompt_instdir=${targetdir}/${architecture}/lib/static-disable

    if [ ! ${libomp_ossdir_static} = ${ompt_instdir} ] ; then
        mkdir -p ${ompt_instdir}
        cp ${libomp_ossdir_static}/libomp.a ${ompt_instdir}/libomp.a ;
        cp ${libomp_ossdir_static}/libgomp.a ${ompt_instdir}/libgomp.a ;
        cp ${libomp_ossdir_static}/libiomp5.a ${ompt_instdir}/libiomp5.a ;
    fi
        # apex also needs the headers...
        mkdir -p ${targetdir}/${architecture}/include
    cp ${START_DIR}/include/omp*.h ${targetdir}/${architecture}/include
  fi
fi

# doing this now, so that tauoptions is defined.
if [ $apex = yes ] ; then
    build_apex
fi
if [ $zerosum = yes ] ; then
    build_zerosum
fi
if [ $python3 = yes -o $python = yes ] ; then
    python_version=`${pythonintv} --version | awk '{print $NF}'`
    version_array=( ${python_version//./ } )
    if [ "${version_array[0]}" == 3 ] ; then
        if [ "${version_array[1]}" -gt 11 ] ; then
            Python3_VERSION_MINOR=${version_array[1]}
            build_perfstubs
        fi
    fi
fi

# Save the FixMakefile args
# Clear any old data
grep "^#" ./utils/FixMakefile.info > ./utils/FixMakefile.info~~0
/bin/mv ./utils/FixMakefile.info~~0 ./utils/FixMakefile.info
echo $fixmakeargs >> ./utils/FixMakefile.info

# Now FINALLY FixMakefile with the string I have built
utils/FixMakefile $fixmakeargs
retval=$?
if [ $retval -ne 0 ] ; then
  echo "ERROR: One or more TAU components could not be configured. Please check above for error messages"
  exit $retval
fi

# Use default setups, forget everything else (PHB)
if [ $machine = default ] ; then
    /bin/cp ./utils/FixMakefile.sed.default  ./utils/FixMakefile.sed
    /bin/cp ./utils/FixMakefile.info.default ./utils/FixMakefile.info
fi

sedout="./utils/FixMakefile.sed"
echo "Applying script to Makefile, please wait, this may take a while..."
# Change back to $START_DIR (defined at the top) to ensure we are in the tau2 directory
# before we execute this fairly dangerous 'find' command
cd $START_DIR
sed -f $sedout < Makefile.skel > Makefile
sed -f $sedout < include/Makefile.skel > include/Makefile
sed -f $sedout < src/Profile/Makefile.skel > src/Profile/Makefile
sed -f $sedout < src/TraceInput/Makefile.skel > src/TraceInput/Makefile
sed -f $sedout < src/wrappers/Makefile.skel > src/wrappers/Makefile
sed -f $sedout < utils/include/Makefile.skel > utils/include/Makefile
sed -f $sedout < plugins/examples/Makefile.skel > plugins/examples/Makefile

if [ $armci = yes ]; then
  sed -f $sedout < src/wrappers/armci/Makefile.skel > src/wrappers/armci/Makefile
fi


#############################################################################

echo " "
echo "***********************************************************************"
echo "Configuring TAU Build scripts..."


# Modify the Makefiles so include/Makefile is replaced by the TAU options
# for specific examples
#echo "s@include \(.*\)TAUROOTDIR\(.*\)@include $\(TAUROOTDIR\)/$architecture/lib/Makefile.tau$tauoptions@g" > utils/sedexamples.out
#echo "s@include/Makefile@$machine/lib/Makefile.tau$tauoptions@g" >utils/sedexamples.out
sedex="./utils/sedexamples.out"
echo "" > $sedex
echo "Modifying Makefiles in the examples subdirectory..."

# Prefix start from examples directory
pr=examples
exampleslist="instrument/Makefile mapping/embedded/Makefile \
  mapping/external/Makefile fork/Makefile selectiveAccess/Makefile"

if [ "x$fortran_compiler" != "xno" ] ; then
    exampleslist="$exampleslist fortran/Makefile f90/Makefile"
    echo "s@.*TAU_MAKEFILE_FORTRAN=\(.*\)@TAU_MAKEFILE_FORTRAN=$\(TAUROOT\)/$architecture/lib/Makefile.tau$tauoptions@" >> $sedex
fi

if [ $mpi = yes ] ; then
    echo "s@.*TAU_MAKEFILE_MPI=\(.*\)@TAU_MAKEFILE_MPI=$\(TAUROOT\)/$architecture/lib/Makefile.tau$tauoptions@" >> $sedex
    if [ "$fortran_compiler" != "no" ] ; then
        echo "s@.*TAU_MAKEFILE_MPI_FORTRAN=\(.*\)@TAU_MAKEFILE_MPI_FORTRAN=$\(TAUROOT\)/$architecture/lib/Makefile.tau$tauoptions@" >> $sedex
    fi
    if [ $pdt = yes ] ; then
        echo "s@.*TAU_MAKEFILE_MPI_OPENMP=\(.*\)@TAU_MAKEFILE_MPI_OPENMP=$\(TAUROOT\)/$architecture/lib/Makefile.tau$tauoptions@" >> $sedex
    fi
fi

if [ $opari1 = yes -o $opari = yes ] ; then
    echo "s@.*TAU_MAKEFILE_OPARI=\(.*\)@TAU_MAKEFILE_OPARI=$\(TAUROOT\)/$architecture/lib/Makefile.tau$tauoptions@" >> $sedex

fi

if [ $pthread = yes ] ; then
    echo "s@.*TAU_MAKEFILE_PTHREAD=\(.*\)@TAU_MAKEFILE_PTHREAD=$\(TAUROOT\)/$architecture/lib/Makefile.tau$tauoptions@" >> $sedex

fi

if [ $papi = yes ] ; then
    echo "s@.*TAU_MAKEFILE_PAPI=\(.*\)@TAU_MAKEFILE_PAPI=$\(TAUROOT\)/$architecture/lib/Makefile.tau$tauoptions@" >> $sedex
    if [ $pthread = yes ] ; then
        echo "s@.*TAU_MAKEFILE_PAPI_PTHREAD=\(.*\)@TAU_MAKEFILE_PAPI_PTHREAD=$\(TAUROOT\)/$architecture/lib/Makefile.tau$tauoptions@" >> $sedex
    fi

fi

if [ $sproc = yes ] ; then
    echo "s@.*TAU_MAKEFILE_SPROC=\(.*\)@TAU_MAKEFILE_SPROC=$\(TAUROOT\)/$architecture/lib/Makefile.tau$tauoptions@" >> $sedex

fi


if [ $pdt = yes ] ; then
    echo "s@.*TAU_MAKEFILE_PDT=\(.*\)@TAU_MAKEFILE_PDT=$\(TAUROOT\)/$architecture/lib/Makefile.tau$tauoptions@" >> $sedex

    if [ $mpi = yes ] ; then
        echo "s@.*TAU_MAKEFILE_PDT_MPI=\(.*\)@TAU_MAKEFILE_PDT_MPI=$\(TAUROOT\)/$architecture/lib/Makefile.tau$tauoptions@" >> $sedex
    fi
    if [ $pthread = yes ] ; then
        echo "s@.*TAU_MAKEFILE_PDT_PTHREAD=\(.*\)@TAU_MAKEFILE_PDT_PTHREAD=$\(TAUROOT\)/$architecture/lib/Makefile.tau$tauoptions@" >> $sedex
    fi
    if [ $openmp = yes ] ; then
        echo "s@.*TAU_MAKEFILE_PDT_OPENMP=\(.*\)@TAU_MAKEFILE_PDT_OPENMP=$\(TAUROOT\)/$architecture/lib/Makefile.tau$tauoptions@" >> $sedex
    fi
    if [ $opari1 = yes -o $opari = yes ] ; then
        echo "s@.*TAU_MAKEFILE_PDT_OPARI=\(.*\)@TAU_MAKEFILE_PDT_OPARI=$\(TAUROOT\)/$architecture/lib/Makefile.tau$tauoptions@" >> $sedex
    fi
    if [ "$fortran_compiler" != "no" ] ; then
        echo "s@.*TAU_MAKEFILE_PDT_FORTRAN=\(.*\)@TAU_MAKEFILE_PDT_FORTRAN=$\(TAUROOT\)/$architecture/lib/Makefile.tau$tauoptions@" >> $sedex
    fi
fi

echo "Setting enduring example Makefiles."
sed  -f $sedex examples/Makefile.skel > examples/Makefile



# Modify the Makefiles only if prefix is not specified.
if [ $tauprefix = unknown ] ; then
    cp include/Makefile $machine/lib/Makefile.tau$tauoptions
fi

# If it is default, restore to original state
if [ "y$1"  = "ydefault" ] ; then
    #delete files created by configure
    rm -f Makefile
    rm -f include/Makefile
    rm -f src/Profile/Makefile
    rm -f src/TraceInput/Makefile
    rm -f src/wrappers/Makefile
    rm -f utils/include/Makefile
    rm -f examples/Makefile
    rm -f utils/FixMakefile.info
    rm -f include/TAU.h
    rm -f include/tauarch.h
    rm -f include/tau_config.h
    rm -f include/tauroot.h

    STUB="default"
else
    STUB="$tautoplevel/$architecture/lib/Makefile.tau$tauoptions"
fi

if [ $default = false ] ; then
    # replace TAU_MAKEFILE in TAU.h
    cat ./include/TAU.h.default | sed -e "s;^#define TAU_MAKEFILE.*\$;#define TAU_MAKEFILE \"$STUB\";" > include/TAU.h
fi

if [ $tauprefix != unknown ] ; then
    echo "TAU: Copying include directory to ${tauprefix}"
    cp -r include ${tauprefix}
    echo "TAU: Copying man directory to ${tauprefix}"
    cp -r man ${tauprefix}
fi

if [ $default = false ] ; then
    rm -f .active_stub
fi

if [ $default = false ] ; then
    #    echo "configured with:"
    #    echo ""
    #    echo "$modargs"
    #    echo ""

    if [ "$STUB" != "$testlaststu" ] ; then

        echo "$STUB" >> .active_stubs

    fi
    echo "$STUB" > .active_stub
fi

if [ $perf = yes ] ; then
    cp include/Makefile $tautoplevel/$architecture/lib/Makefile.tau$tauoptions
fi

if [ $tauprefix != unknown ] ; then
    echo "TAU: Copying examples directory to ${tauprefix}"
    cp -r examples ${tauprefix}
    cp .all_configs .last_config .active_stubs .active_stub ${tauprefix}
fi



# bye bye
echo
echo "Configuration complete!"
if [ $dyninst = yes ]; then
       echo "TAU: Please use the following to use DyninstAPI: "
       echo "export LD_LIBRARY_PATH=${dyninstdir}/lib:$targetdir/$architecture/lib:\$LD_LIBRARY_PATH"
       echo "export DYNINSTAPI_RT_LIB=${dyninstdir}/lib/libdyninstAPI_RT.so"
fi
if [ $tauprefix != unknown ] ; then
    echo "   Please add " $tauprefix/$architecture/bin " to your path"
else
    echo "   Please add " $tauroot/$architecture/bin " to your path"
fi
echo '   Type "make install" to begin compilation'

if [ -d /usr/linux-k1om-*  -a $machine = x86_64 ]
then
   echo "NOTE: This system supports Intel MIC (KNC). You may configure with -arch=mic_linux for the older KNC to enable this option."
fi

if [ "x$PE_ENV" != "x" -a $machine = x86_64 ]
then
   echo "NOTE: This system supports the Cray CNL platform. You may configure with -arch=craycnl to enable this option."
fi

if [ "x$issue_intel_warning" = "xyes" ] ; then
    echo "NOTE: Intel compilers found in your path, but C++ compiler not specified.  Whatever was detected in 'mpicxx -show' will be used as the compiler.  To force Intel compilers, please pass -c++=icpc, -c++=icpx, or -c++=mpiicpc"
fi

if [ "x$PE_ENV" = "xCRAY" -a "x`uname -m`" == "xaarch64" -a "x$download_unwind" = "xyes" ];
then
   echo  "NOTE: TAU does not support currently support unwinding using TAU_EBS_UNWIND=1 on "
   echo  " the Cray aarch64 platforms as the libmpichcxx.so is linked with a different libunwind.so.1 library."
   echo  " Please do not use this option for now. For questions, please contact tau-bugs@cs.uoregon.edu."
   echo  "****************************************************************************************************"
fi

