#! /bin/sh
#  /*@@
#    @file      irix
#    @date      Thu Feb 25 12:20:53 1999
#    @author    Tom Goodale
#    @desc
#               Known architectures file for SGI Irix systems
#    @enddesc
#    @version   $Header: /CCT/Projects/XiRel/SPEC2006/CactusBSSN/lib/make/known-architectures/irix,v 1.1.1.1 2009/02/03 01:02:30 jtao Exp $
#  @@*/

if test "$CCTK_CONFIG_STAGE" = 'preferred-compilers' ; then

  # extract the CPU type from hinv output
  # (assumes all processors are of equal type :-)
  IRIX_CPU="`hinv -t cpu | tail -1 | sed 's/^[^R]*R\([01-9]*\).*$/\1/'`"

  if test -z "$IRIX_BITS" ; then
    if test -n "`uname | grep 64`" ; then
       IRIX_BITS=64
    else
       IRIX_BITS=32
    fi
  fi

  # use the Cactus preprocessor for Fortran
  if test -z "$FPP"; then
    FPP='$(PERL) $(CCTK_HOME)/lib/sbin/cpp.pl'
    echo Setting FPP to $FPP
  fi

  # use the native SGI compilers by default
  if test -z "$CC"; then
    CC='cc'
    echo Setting C compiler to $CC
  fi

  if test -z "$CXX"; then
    CXX='CC'
    echo Setting C++ compiler to $CXX
  fi

  if test -z "$LD"; then
    LD='CC'
    echo Setting linker to $LD
  fi


else


  # Find out which version of the C++ compiler is installed
  IRIX_CVERSION="`versions c++_dev | grep , | tail -1 | sed 's:.*, ::'`"

  IRIX_CMAJ="`echo $IRIX_CVERSION | sed 's:\([^. ]*\)\.\([^. ]*\).*:\1:'`"
  IRIX_CMIN="`echo $IRIX_CVERSION | sed 's:\([^. ]*\)\.\([^. ]*\).*:\2:'`"

  if test $IRIX_CMAJ -gt 7 -o \( $IRIX_CMAJ -eq 7 -a $IRIX_CMIN -gt 2 \) ; then
    IRIX_CCOMP='new'
  else
    IRIX_CCOMP='old'
  fi

  case "$IRIX_CPU" in
    4???)
       IRIX_FLAGS='-mips3' ;;
    8???)
       IRIX_FLAGS='-mips3' ;;
    10???)
       IRIX_FLAGS='-mips4 -r10000' ;;
    12???)
       if test $IRIX_CCOMP = 'old'; then
         IRIX_FLAGS='-mips4 -r10000'
       else
         IRIX_FLAGS='-mips4 -r12000'
       fi
       ;;
    *)
       IRIX_FLAGS='-mips4' ;;
  esac

  # Find out which version of the Fortran compilers is installed
  IRIX_FVERSION="`versions ftn_dev | grep , | tail -1 | sed 's:.*, ::'`"

  IRIX_FMAJ="`echo $IRIX_FVERSION | sed 's:\([^. ]*\)\.\([^. ]*\).*:\1:'`"
  IRIX_FMIN="`echo $IRIX_FVERSION | sed 's:\([^. ]*\)\.\([^. ]*\).*:\2:'`"

  if test $IRIX_FMAJ -gt 7 -o \( $IRIX_FMAJ -eq 7 -a $IRIX_FMIN -ge 2 \) ; then
    IRIX_FLIBS='fortran ftn ftn90'
  else
    IRIX_FLIBS='ftn ftn90'
  fi

  # now set the compiler flags
  if test $IRIX_BITS = 64; then
    IRIX_BITS_FLAG='-64'
    : ${F90FLAGS="-r10000 -TARG:proc=r10000 $IRIX_BITS_FLAG -mips4"}
    : ${LIBS="malloc perfex fpe $IRIX_FLIBS m"}

    # Reduce the optimization level for debug configurations down to 1.
    # This is necessary in order to prevent the compiler from generating
    # conditional load/store ops which may cause problems on array accesses
    # which have been passed in from C to Fortran as a NULL pointer.
    if test -n "$DEBUG" -a "x$DEBUG" != 'xno' ; then
      : ${F90_OPTIMISE_FLAGS='-O1 -OPT:roundoff=3:IEEE_arithmetic=3:const_copy_limit=100000'}
    else
      : ${F90_OPTIMISE_FLAGS='-O3 -OPT:roundoff=3:IEEE_arithmetic=3:const_copy_limit=100000'}
    fi
  else
    IRIX_BITS_FLAG='-n32'
    : ${F90FLAGS="$IRIX_BITS_FLAG -mips3"}
    : ${LIBS="malloc fpe $IRIX_FLIBS m"}

    # Reduce the optimization level for debug configurations down to 1.
    # This is necessary in order to prevent the compiler from generating
    # conditional load/store ops which may cause problems on array accesses
    # which have been passed in from C to Fortran as a NULL pointer.
    if test -n "$DEBUG" -a "x$DEBUG" != 'xno' ; then
      : ${F90_OPTIMISE_FLAGS='-O1 -OPT:roundoff=3:IEEE_arithmetic=3:fold_arith_limit=10000:const_copy_limit=100000'}
    else
      : ${F90_OPTIMISE_FLAGS='-O3 -OPT:roundoff=3:IEEE_arithmetic=3:fold_arith_limit=10000:const_copy_limit=100000'}
    fi
  fi

  if test "`basename $CC`" = 'cc'; then
    : ${CFLAGS="$IRIX_BITS_FLAG $IRIX_FLAGS"}
    : ${C_OPTIMISE_FLAGS='-O3 -INLINE -OPT:Olimit=100000'}
    : ${C_DEBUG_FLAGS='-g3'}
    : ${C_WARN_FLAGS='-fullwarn'}
  fi
  if test "`basename $CXX`" = 'CC'; then
    # switch off automatic template instantiation
    : ${CXXFLAGS="-no_auto_include -ptused $IRIX_BITS_FLAG $IRIX_FLAGS"}
    : ${CXX_OPTIMISE_FLAGS='-O3 -INLINE -OPT:Olimit=100000'}
    : ${CXX_DEBUG_FLAGS='-g3'}
    : ${CXX_WARN_FLAGS='-fullwarn'}
  fi
  if test "`basename $LD`" = 'CC'; then
    : ${LDFLAGS="$IRIX_BITS_FLAG -Wl,\"-woff 84\",\"-woff 85\""}
  fi

  : ${F77FLAGS="$F90FLAGS"}
  : ${F77_OPTIMISE_FLAGS="$F90_OPTIMISE_FLAGS"}
  : ${F90_DEBUG_FLAGS='-g3'}
  : ${F77_DEBUG_FLAGS='-g3'}
  : ${F90_WARN_FLAGS='-fullwarn'}
  : ${F77_WARN_FLAGS='-fullwarn'}

  # Cache stuff
  if test -z "$CACHELINE_BYTES" ; then
    CACHELINE_BYTES=128
    echo "Setting CACHELINE_BYTES to $CACHELINE_BYTES"
  fi

  if test -z "$CACHE_SIZE" ; then
    TEMP_CACHE_SIZE="`hinv | perl -ne 'if (m/Secondary.*cache/) { s/^.*: (.*)( on P.*)?$/$1/ ; s/ Mbyte.*/\*1024/ ; s/ Kbyte.*//; print}' | sort | head -n1`"
    if test -n "$TEMP_CACHE_SIZE" ; then
      CACHE_SIZE="$TEMP_CACHE_SIZE*1024"
      echo "Setting CACHE_SIZE to $CACHE_SIZE bytes"
    else
      echo 'Unable to determine Cache size on this machine.'
    fi
  fi

  # MPI stuff
  if test -n "$MPI" ; then
    if test -e "/usr/lib${IRIX_BITS}/libmpi++.so" ; then
      NATIVE_MPI_LIBS='mpi mpi++'
    else
      NATIVE_MPI_LIBS='mpi'
    fi
  fi

fi
