#!/bin/sh
# $Header: buildtools/port/oraxlc /stpl_db_11.2.0_aix.ppc32/2 2011/06/07 15:22:19 mpolivka Exp $

#   MODIFIED  (MM/DD/YY)
#   mpolivka   06/07/11 - update compiler to newer PTF to support profiling
#                         (PDF)
#   surikuma   06/25/09 - remove dependency on AIX_OSVER
#   thasandk   03/26/09 - aix61/vac9 support
#   mpolivka   02/10/09 - change ORACCENV default to use direct path not
#                         /usr/local/bin/xlc
#   mpolivka   02/10/09 - update to vac9 compiler
#   surikuma   11/20/08  - 
#   vsridhar   02/28/03  - accept -cpp flag and invoke xlC
#   sawadhwa   04/11/01 -  Add support for C++ compilation
#
# Optimize the use of the binder for Oracle on AIX 3.
#
# The optimizations done are multiple library removal and prebinding of
# libraries into objects.  Only the former is performed by default
#
# Options are specified by the environment ORACCENV.  See 'oracc -help'.
# 
# Updating makefiles:
#
# If you want your makefiles to use the oracc, add the line 'CC=oracc' to
# each troublesome makefile, normally named <product>.mk in the <product>/lib
# directory under your ORACLE_HOME directory.

# iFarm location of AIX compiler 
FARM_COMP_LOCATION=/usr/local/packages/vac_remote/vac_9_March2011

if test -d "${FARM_COMP_LOCATION}"
then
  COMP_DIR="${FARM_COMP_LOCATION}"
else
  COMP_DIR=""
fi

PATH=/bin:/usr/bin:/usr/ucb:${COMP_DIR}/usr/vac/bin:${COMP_DIR}/usr/vacpp/bin; export PATH

HOST_S=`hostname -s`
invoc=`basename $0`

if test $# = 0
then
  echo For help on $invoc, type \'$invoc -help\' 1>&2
  echo For help on cc or xlc, type \'cc\' or \'xlc\' 1>&2
  exit 1
fi

if test $# = 1 -a "$1" = -help
then
  case $invoc in
    *xlc) cclist=xlc,cc;;
    *)    cclist=cc,xlc;;
  esac
  cat << ENDMSG
	$invoc - Compile and link Oracle software

	$invoc options are specified in the environment ORACCENV separated by
	comma.  Possible options are (default mentioned first):

	multelim={true,false}	# eliminate multiple libraries
	prebind={false,true}	# prebind libraries into objects
	verbose={0,1,2,3}	# be verbose
	cc={$cclist,c89,bsdcc}	# use that cc invocation

	Do not put spaces around the = signs.

	Example:

	$ ORACCENV='verbose=3, prebind=true'; export ORACCENV;
	$ make -f oracle.mk oracle
ENDMSG
  exit 1
fi

if test -z "$ORACCENV"
then
    ORACCENV="multelim=true,prebind=false,verbose=0,cc=${COMP_DIR}/usr/vac/bin/xlc,oraccdbg=false"
fi

if [[ "$TMPDIR" = "" ]]; then
  TMPDIR="/tmp"
fi

tmparg=$TMPDIR/OCarg$HOST_S$$
rm -f $tmparg
echo $ORACCENV | tr ',' '\
' > $tmparg
. $tmparg
rm -f $tmparg

# put in the defaults
test -z "$oraccdbg" && oraccdbg=false
test -z "$prebind"  && prebind=false
test -z "$multelim" && multelim=true
test -z "$verbose"  && verbose=0
if test -z "$cc"
then
  case $invoc in
    *oracc)	cc=cc;;
    *oraxlc)	cc=xlc;;
    *)		cc=cc;;
  esac
fi

fail=0

case "$cc" in
  cc|xlc|c89|bsdcc|xlc_r|*xlc|*xlC) ;;
  *)	echo oracc: $cc is an illegal value for cc
	fail=1;;
esac

case "$multelim" in
  true|false) ;;
  *)	echo oracc: $multelim is an illegal value for multelim
	fail=1;;
esac

case "$prebind" in
  true|false) ;;
  *)	echo oracc: $prebind is an illegal value for prebind
	fail=1;;
esac

case "$oraccdbg" in
  true|false) ;;
  *)	echo oracc: $oraccdbg is an illegal value for oraccdbg
	fail=1;;
esac

case "$verbose" in
  0|1|2|3) ;;
  *)	echo oracc: $verbose is an illegal value for verbose
	fail=1;;
esac

if test $fail -gt 0
then
  exit $fail
fi

tmpf=$TMPDIR/OCf$HOST_S$$	# list of all arguments
tmpm=$TMPDIR/OCm$HOST_S$$	# list of all arguments except multiple libraries
tmpa=$TMPDIR/OCa$HOST_S$$	# list of all library arguments
tmps=$TMPDIR/OCs$HOST_S$$ # sed script to change libriries by prebound objects

if test $oraccdbg = false
then
  trap "rm -f $tmpa $tmpf $tmpm $tmps; exit 1" 1 2 3 15
fi

rm -f $tmpf $tmpm $tmpa $tmps

dold=false	# see if there is a -o argument

test $verbose -ge 3 && echo $invoc: Original arguments are \'"$@"\'
# fill the temp files
for arg
do
  case $arg in
    *.a|-l*)  grep -e $arg\$ $tmpm > /dev/null || echo $arg >> $tmpm
	  echo $arg >> $tmpf
	  echo $arg >> $tmpa
	  ;;
    -o*)  echo XX$arg | sed 's/^XX//' >> $tmpm
    	  echo XX$arg | sed 's/^XX//' >> $tmpf
	  dold=true
	  ;;
    -cpp) cc=xlC
          ;;
    -*)	  echo XX$arg | sed 's/^XX//' >> $tmpm
    	  echo XX$arg | sed 's/^XX//' >> $tmpf
	  ;;
    *.cpp)  cc=xlC
          echo $arg >> $tmpm
          echo $arg >> $tmpf
          ;;
    *)    echo $arg >> $tmpm
	  echo $arg >> $tmpf
	  ;;
  esac
done

# add -qsaveopt to $tmpm and $tmpf
echo "-qsaveopt" >> $tmpm
echo "-qsaveopt" >> $tmpf


# let's see what to do
if test $dold = false
then
  # nothing really
  test $verbose -ge 3 && echo $invoc: calling $cc with arguments \'`cat $tmpf`\'
  $cc `cat $tmpf`
  xit=$?
  if test $oraccdbg = false
  then
    rm -f $tmpf $tmpm $tmpa $tmps
  fi
  exit $xit
fi

if test $prebind = true
then
  # loop thru all libraries
  for lib in `grep -v '^-l' $tmpa`
  do
    obj=`echo $lib | sed 's/\.a$/.o/'`
    if test ! -f $obj || \
      test `ls -t $lib $obj | sed 1q` = $lib -a \
      -w `dirname $obj`
    then
      # prebind necessary
      test $verbose -ge 1 && echo $invoc: prebinding $lib into $obj
      rm -f $obj
      if ld -r -o $obj $lib 
      then
	:
      else
	rm -f $obj
	echo $invoc: prebinding $lib FAILED 1>&2
      fi
    fi
    if test -f $obj
    then
      test -f $obj -a $verbose -ge 2 \
	&& echo $invoc: using prebound $obj in stead of $lib
      echo 's%^'$lib'$%'$obj'%' >> $tmps
    fi
  done
fi

if test $multelim = true
then
  test $verbose -ge 2 && echo $invoc: eliminating multiple libraries
  if test -s $tmps
  then
    test $verbose -ge 3 && echo $invoc: calling $cc with arguments \'`sed -f $tmps $tmpm`\'
    $cc `sed -f $tmps $tmpm`
    xit=$?
  else
    test $verbose -ge 3 && echo $invoc: calling $cc with arguments \'`cat $tmpm`\'
    $cc `cat $tmpm`
    xit=$?
  fi
else
  if test -s $tmps
  then
    test $verbose -ge 3 && echo $invoc: calling $cc with arguments \'`sed -f $tmps $tmpf`\'
    $cc `sed -f $tmps $tmpf`
    xit=$?
  else
    test $verbose -ge 3 && echo $invoc: calling $cc with arguments \'`cat $tmpf`\'
    $cc `cat $tmpf`
    xit=$?
  fi
fi

test $verbose -ge 3 && echo $invoc: $cc returned exit status $xit
if test $oraccdbg = false
then
  rm -f $tmpf $tmpm $tmpa $tmps
fi

exit $xit