#!/bin/sh 
COMPILER_OPTS=" -D_GNU_SOURCE "

BINDIR=/usr/local/pdtoolkit/x86_64/gcc/bin
PDTDIR=/usr/local/pdtoolkit
STDINC=
VERBOSE=off
TMP="/tmp"

# if -t is specified, template text strings are not included in pdb files
PDTOPT="-t"
ORIGARGS=$*

# default compiler options
#COMPOPT="$STDINC"
#COMPOPT="$COMPILER_OPTS $STDINC"

SYSINCLUDE=/usr/include
#CRAYXMT#CRAYXMTOPTS="--sys_include ${PDTDIR}/include/kai/fix/xmtheaders" #ENDIF#
#MIPSCROSS#SYSINCLUDE=`scgcc -print-file-name=libc.so | sed -n 's%\(/lib[^\/]*\)/libc.so%\1/../include%p'` #EOC#
COMPOPT="--sys_include ${PDTDIR}/include/kai/fix --sys_include ${SYSINCLUDE} --sys_include ${PDTDIR}/include --sys_include ${PDTDIR}/include/kai -D__noinline=\"\" -D__restrict=\"\" -D_TYPENAME -D_EXPLICIT ${CRAYXMTOPTS} "
ASMOPT1=-D__volatile__\\\(a,...\\\)=volatile\\\(\\\"nop\\\"\\\)
ASMOPT2=-Dvolatile\\\(a,...\\\)=__volatile__\\\(\\\"nop\\\"\\\)
ASMOPT3=-D__volatile\\\(a,...\\\)=__volatile__\\\(\\\"nop\\\"\\\)


# For KCC headers

# For parsing <Python.h>
# Please comment it if it causes problems with parsing the rest of the code
PYTHONOPTS="-DSIZEOF_LONG=4 -DPy_PYCONFIG_H -DHAVE_LIMITS_H  -DPY_LONG_LONG=long -DPy_USING_UNICODE  -DPy_UNICODE_SIZE=4"


# EDG options
# can add --remove_unneeded_entities
NEWGNUOPTS="--vla --g++"
#IRIX64#NEWGNUOPTS=" " #EOC#
#IRIXN32#NEWGNUOPTS=" " #EOC#
#IRIXO32#NEWGNUOPTS=" " #EOC#
#HPUX#NEWGNUOPTS=" " #EOC#
#CRAY#NEWGNUOPTS=" " #EOC#
#CRAYX1#NEWGNUOPTS=" " #EOC#
EDGOPT="--exceptions --late_tiebreaker \
	--no_class_name_injection --no_warnings --restrict ${NEWGNUOPTS} ${PYTHONOPTS} -DPDT_PARSER -DPDT_CXX_LANG"

# Architecture specific options for KAI headers
# ---------------------------------------------
#SOL2#ARCHOPT=" -Dsun -Dsparc -Dunix -D__CPLUSPLUS -D__STDC__=0 -D_EXCEPTIONS -D__KCC -D__sun__ -D__sparc__ -D__unix__ -D__sun -D__sparc -D__unix -D__SVR4 -D_NO_LONGLONG -Dsun -Dsparc -Dunix -D__CPLUSPLUS -D__STDC__=0 -D_EXCEPTIONS  " #EOC#

#SUN386I#ARCHOPT=" -Dsun -Dsparc -Dunix -D__CPLUSPLUS -D__STDC__=0 -D_EXCEPTIONS -D__KCC -D__sun__ -D__sparc__ -D__unix__ -D__sun -D__sparc -D__unix -D__SVR4 -D_NO_LONGLONG -Dsun -Dsparc -Dunix -D__CPLUSPLUS -D__STDC__=0 -D_EXCEPTIONS  " #EOC#

#SUNX86_64#ARCHOPT=" -Dsun -Dsparc -Dunix -D__CPLUSPLUS -D__STDC__=0 -D_EXCEPTIONS -D__KCC -D__sun__ -D__sparc__ -D__unix__ -D__sun -D__sparc -D__unix -D__SVR4 -D_NO_LONGLONG -Dsun -Dsparc -Dunix -D__CPLUSPLUS -D__STDC__=0 -D_EXCEPTIONS " #EOC#

#IRIXO32#ARCHOPT=" -Dsgi -Dunix -Dmips -Dhost_mips -DMIPSEB -D_SVR4_SOURCE -D_SGI_SOURCE -DSYSTYPE_SVR4 -D_LONGLONG -D__INLINE_INTRINSICS=1 -D__STDC__ -D__ANSI_CPP__ -D_MODERN_C -D_LANGUAGE_C_PLUS_PLUS=1 -D__mips=2 -D_MIPS_ISA=2 -D_EXCEPTIONS -D_MIPS_FPSET=16 -D_ABIO32=1 -D_MIPS_SIM=_ABIO32 -D_MIPS_SZINT=32 -D_MIPS_SZLONG=32 -D_MIPS_SZPTR=32 -D__KCC -D__EDG -D__EXTENSIONS -D__sgi -D__unix -D__host_mips -D_MIPSEB -D_SYSTYPE_SYSV -D_SYSTYPE_SVR4 -D_MODERN_C -D__DSO__ -D__EXTENSIONS__=1 -D_CFE -Dsgi -Dunix -Dmips -Dhost_mips -DMIPSEB -D_SVR4_SOURCE -D_SGI_SOURCE -DSYSTYPE_SVR4 -D_LONGLONG -D__INLINE_INTRINSICS=1 -D__STDC__ -D__ANSI_CPP__ -D_MODERN_C -D_LANGUAGE_C_PLUS_PLUS=1 -D__mips=2 -D_MIPS_ISA=2 -D_EXCEPTIONS -D__builtin_va_alist=va_alist  " #EOC#

#IRIXN32#ARCHOPT=" -D_COMPILER_VERSION=721 -Dsgi -Dunix -Dmips -Dhost_mips -DMIPSEB -D_SVR4_SOURCE -D_SGI_SOURCE -DSYSTYPE_SVR4 -D_LONGLONG -D__INLINE_INTRINSICS=1 -D__STDC__ -D__ANSI_CPP__ -D_MODERN_C -D_LANGUAGE_C_PLUS_PLUS=1 -D__mips=3 -D_MIPS_ISA=3 -D_EXCEPTIONS -D_MIPS_FPSET=32 -D_ABIN32=2 -D_MIPS_SIM=_ABIN32 -D_MIPS_SZINT=32 -D_MIPS_SZLONG=32 -D_MIPS_SZPTR=32 -D__KCC -D__EDG -D__sgi -D__unix -D__host_mips -D_MIPSEB -D_SYSTYPE_SYSV -D_MODERN_C -D__DSO__ -D_SYSTYPE_SVR4 -D__EXTENSIONS -D__EXTENSIONS__=1 -D_COMPILER_VERSION=721 -Dsgi -Dunix -Dmips -Dhost_mips -DMIPSEB -D_SVR4_SOURCE -D_SGI_SOURCE -DSYSTYPE_SVR4 -D_LONGLONG -D__INLINE_INTRINSICS=1 -D__STDC__ -D__ANSI_CPP__ -D_MODERN_C -D_LANGUAGE_C_PLUS_PLUS=1 -D__mips=3 -D_MIPS_ISA=3 -D_EXCEPTIONS -D__builtin_va_alist=va_alist  " #EOC#

#IRIX64#ARCHOPT=" -D_COMPILER_VERSION=721 -Dsgi -Dunix -Dmips -Dhost_mips -DMIPSEB -D_SVR4_SOURCE -D_SGI_SOURCE -DSYSTYPE_SVR4 -D_LONGLONG -D__INLINE_INTRINSICS=1 -D__STDC__ -D__ANSI_CPP__ -D_MODERN_C -D_LANGUAGE_C_PLUS_PLUS=1 -D__mips=3 -D_MIPS_ISA=3 -D_EXCEPTIONS -D_MIPS_FPSET=32 -D_ABI64=3 -D_MIPS_SIM=_ABI64 -D_MIPS_SZINT=32 -D_MIPS_SZLONG=64 -D_MIPS_SZPTR=64 -D__KCC -D__EDG -D__sgi -D__unix -D__host_mips -D_MIPSEB -D_SYSTYPE_SYSV -D_MODERN_C -D__DSO__ -D_SYSTYPE_SVR4 -D__EXTENSIONS -D__EXTENSIONS__=1 -D_COMPILER_VERSION=721 -Dsgi -Dunix -Dmips -Dhost_mips -DMIPSEB -D_SVR4_SOURCE -D_SGI_SOURCE -DSYSTYPE_SVR4 -D_LONGLONG -D__INLINE_INTRINSICS=1 -D__STDC__ -D__ANSI_CPP__ -D_MODERN_C -D_LANGUAGE_C_PLUS_PLUS=1 -D__mips=3 -D_MIPS_ISA=3 -D_EXCEPTIONS  " #EOC#

#LINUX#ARCHOPT=" -Dlinux -Dunix -D__CPLUSPLUS -D__inline__=inline -D__signed__=signed -D_EXCEPTIONS -D__KCC -D__unix -D__linux -D__unix__ -D__linux__ -D__i386 -D__i386__ -D__STDC__ -Dlinux -Dunix -D__CPLUSPLUS -D__inline__=inline -D__signed__=signed -D_EXCEPTIONS  -D__THROW=   " #EOC#

#PPCLINUX#ARCHOPT=" -Dlinux -Dunix -D__CPLUSPLUS -D__inline__=inline -D__signed__=signed -D_EXCEPTIONS -D__KCC -D__unix -D__linux -D__unix__ -D__linux__ -D__i386 -D__i386__ -D__STDC__ -Dlinux -Dunix -D__CPLUSPLUS -D__inline__=inline -D__signed__=signed -D_EXCEPTIONS  -D__THROW=   -D__BIG_ENDIAN__ " #EOC#

#IBM64LINUX#ARCHOPT=" -Dlinux -Dunix -D__CPLUSPLUS -D__inline__=inline -D__signed__=signed -D_EXCEPTIONS -D__KCC -D__unix -D__linux -D__unix__ -D__linux__ -D__i386 -D__i386__ -D__STDC__ -Dlinux -Dunix -D__CPLUSPLUS -D__inline__=inline -D__signed__=signed -D_EXCEPTIONS  -D__THROW=   -D__BIG_ENDIAN__ " #EOC#

#MIPS64LINUX#ARCHOPT=" -Dlinux -Dunix -D__CPLUSPLUS -D__inline__=inline -D__signed__=signed -D_EXCEPTIONS -D__KCC -D__unix -D__linux -D__unix__ -D__linux__ -D__i386 -D__i386__ -D__STDC__ -Dlinux -Dunix -D__CPLUSPLUS -D__inline__=inline -D__signed__=signed -D_EXCEPTIONS  -D__THROW=   -D__BIG_ENDIAN__ -D_ABI64=3 -D_MIPS_SIM=_ABI64 -D_MIPS_SZINT=32 -D_MIPS_SZLONG=64 -D_MIPS_SZPTR=64 -DPDT_MIPS64LINUX " #EOC#

#MIPS32LINUX#ARCHOPT=" -Dlinux -Dunix -D__CPLUSPLUS -D__inline__=inline -D__signed__=signed -D_EXCEPTIONS -D__KCC -D__unix -D__linux -D__unix__ -D__linux__ -D__i386 -D__i386__ -D__STDC__ -Dlinux -Dunix -D__CPLUSPLUS -D__inline__=inline -D__signed__=signed -D_EXCEPTIONS  -D__THROW=   -D__BIG_ENDIAN__ -D_ABI64=3 -D_MIPS_SIM=_ABI64 -D_MIPS_SZINT=32 -D_MIPS_SZLONG=64 -D_MIPS_SZPTR=64 -DPDT_MIPS64LINUX " #EOC#

#MIPSCROSS#ARCHOPT=" -Dlinux -Dunix -D__CPLUSPLUS -D__inline__=inline -D__signed__=signed -D_EXCEPTIONS -D__KCC -D__unix -D__linux -D__unix__ -D__linux__ -D__i386 -D__i386__ -D__STDC__ -Dlinux -Dunix -D__CPLUSPLUS -D__inline__=inline -D__signed__=signed -D_EXCEPTIONS  -D__BIG_ENDIAN__ -D_ABI64=3 -D_MIPS_SIM=_ABI64 -D_MIPS_SZINT=32 -D_MIPS_SZLONG=64 -D_MIPS_SZPTR=64 -DPDT_MIPS64LINUX -D__GNUC__=1 " #EOC#

ARCHOPT=" -Dlinux -Dunix -D__CPLUSPLUS -D__inline__=inline -D__signed__=signed -D_EXCEPTIONS -D__KCC -D__unix -D__linux -D__unix__ -D__linux__ -D__i386 -D__i386__ -D__STDC__ -Dlinux -Dunix -D__CPLUSPLUS -D__inline__=inline -D__signed__=signed -D_EXCEPTIONS  -D__THROW=   " #EOC##X86_64#
# You may need to add -D__int64_t=long to the x86_64 line above.

#CRAYXMT#ARCHOPT=" -Dlinux -Dunix -D__CPLUSPLUS -D__inline__=inline -D__signed__=signed -D_EXCEPTIONS -D__KCC -D__unix -D__linux -D__unix__ -D__linux__ -D__i386 -D__i386__ -D__STDC__ -Dlinux -Dunix -D__CPLUSPLUS -D__inline__=inline -D__signed__=signed -D_EXCEPTIONS  -D__THROW=   -D__TERA__ -D__MTA__ -D__ELDORADO__  -D__sync= -D__short32=int  -Dassert\(a\)=  -Dreadfe\(a\)=0 -Dwriteef\(a,b\)= -Dint_fetch_add\(a,b\)=0 -Dreadff\(a\)=0 -Dwriteff\(a,b\)= " #EOC#

#HPUX#ARCHOPT=" -Dhp9000s800 -Dhppa -Dhpux -Dunix -D_XOPEN_SOURCE -D_XPG4 -D__CPLUSPLUS -D_EXCEPTIONS -D__KCC -D__hp9000s700 -D__hp9000s800 -D__hppa -D__hpux -D__unix -D_PA_RISC1_1 -D_INCLUDE__STDC__ -D_HPUX_SOURCE -Dhp9000s800 -Dhppa -Dhpux -Dunix -D_XOPEN_SOURCE -D_XPG4 -D__CPLUSPLUS -D_EXCEPTIONS -D__cplusplus" #EOC#

#CRAY#ARCHOPT=" -Dunix -DCRAY -D__CPLUSPLUS -D__WCHAR_T__ -D__STDC__=2 -D__KCC -D__unix -D__unix__ -Dcray -D__cray -D_CRAYT3E -D_CRAYMPP -D_ADDR64 -D_CRAYIEEE -D_LD64=1 -D_EXCEPTIONS -Dunix -DCRAY -D__CPLUSPLUS -D__WCHAR_T__ -D__STDC__=2 -D__KCC -D__unix -D__unix__ -Dcray -D__cray -D_CRAYT3E -D_CRAYMPP -D_ADDR64 -D_CRAYIEEE -D_LD64=1 -D_EXCEPTIONS -D__builtin_va_alist=va_alist " #EOC#

#CRAYX1#ARCHOPT=" -Dunix -DCRAY -D__CPLUSPLUS -D__WCHAR_T__ -D__STDC__=2 -D__KCC -D__unix -D__unix__ -Dcray -D__cray -D_CRAYMPP -D_ADDR64 -D_CRAYIEEE -D_LD64=1 -D_EXCEPTIONS -Dunix -DCRAY -D__CPLUSPLUS -D__WCHAR_T__ -D__STDC__=2 -D__KCC -D__unix -D__unix__ -Dcray -D__cray -D_CRAYMPP -D_ADDR64 -D_CRAYIEEE -D_LD64=1 -D_EXCEPTIONS -D__builtin_va_alist=va_alist -D_LANGUAGE_C_PLUS_PLUS -D_crayx1" #EOC#

#OSF1#ARCHOPT="-Dunix -DSYSTYPE_BSD -D_LANGUAGE_C_PLUS_PLUS -D_CFE -D_EXCEPTIONS -D_LONGLONG -D_SYSTYPE_BSD -D__DSO__ -D__KCC -D__alpha -D__osf__ -D__unix__ -Dunix -DSYSTYPE_BSD -D_LANGUAGE_C_PLUS_PLUS -D_CFE -D_EXCEPTIONS -D_XOPEN_SOURCE=500 -D_OSF_SOURCE" #EOC#

#AIX#ARCHOPT=" -Dunix -D_LONG_LONG -D__CPLUSPLUS -D__STDC__=0 -D_ANSI_C_SOURCE -D_AIX=42 -D_EXCEPTIONS -D__KCC -D__EDG -D__aix -D_POWER -D__rs6000__ -D__unix -D__unix__ -D_ALL_SOURCE -Dunix -D_LONG_LONG -D__CPLUSPLUS -D__STDC__=0 -D_ANSI_C_SOURCE -D_AIX=42 -D_EXCEPTIONS  -D_XOPEN_SOURCE=500 " #EOC#

#IBM64#ARCHOPT=" -Dunix -D_LONG_LONG -D__CPLUSPLUS -D__STDC__=0 -D_ANSI_C_SOURCE -D_AIX=42 -D_EXCEPTIONS -D__KCC -D__EDG -D__aix -D_POWER -D__rs6000__ -D__unix -D__unix__ -D_ALL_SOURCE -Dunix -D_LONG_LONG -D__CPLUSPLUS -D__STDC__=0 -D_ANSI_C_SOURCE -D_AIX=42 -D_EXCEPTIONS  -D_XOPEN_SOURCE=500 " #EOC#

#DARWIN#ARCHOPT="  -D__builtin_va_list=int -D_BSD_RUNE_T_DEFINED_=1 -Drune_t=_BSD_RUNE_T_ -D_BSD_WCHAR_T_DEFINED_ -D__cplusplus -D__private_extern__=extern -D__ppc__ -D__NATURAL_ALIGNMENT__ -D__MACH__ -D__BIG_ENDIAN__ -D__APPLE__ -D__ppc__ -D__NATURAL_ALIGNMENT__ -D__MACH__ -D__BIG_ENDIAN__ -D__APPLE__ -D__STDC__ -D__APPLE_CC__=926 -D__DYNAMIC__ -D_OS_OSBYTEORDERPPC_H -DOS_INLINE=inline " #EOC#

#INTELMACOSX#ARCHOPT="  -D__builtin_va_list=int -D_BSD_RUNE_T_DEFINED_=1 -Drune_t=_BSD_RUNE_T_ -D_BSD_WCHAR_T_DEFINED_ -D__cplusplus -D__private_extern__=extern -D__NATURAL_ALIGNMENT__ -D__MACH__ -D__BIG_ENDIAN__ -D__APPLE__ -D__i386__ -D__NATURAL_ALIGNMENT__ -D__MACH__ -D__BIG_ENDIAN__ -D__APPLE__ -D__STDC__ -D__APPLE_CC__=926 -D__DYNAMIC__ " #EOC#

#HITACHI#ARCHOPT="  -D_AES_SOURCE -D__CPLUSPLUS -D_EXCEPTIONS -D__KCC -D__SR8000 -D__hiuxmpp -D__unix -D_OSF_SOURCE -D_AES_SOURCE -D__CPLUSPLUS -D_EXCEPTIONS " #EOC#

# local options, if any
LOCOPT=

# Remove all options other than -I and -D from the command line.
INF=$1
SPAWNARGS=
shift

# PtDB
# Added to support a "-o" option for specifiying an
# output file.
getnextarg=0
gotpdbfile=0
# End - PtDB

for arg in "$@"
do
# Allow -I -D arguments to be passed to edgcpfe
  # PtDB
  if [ $getnextarg -eq 1 ]
  then
    NEWPDB=$arg
    getnextarg=0
    gotpdbfile=1
  fi

  if [ "y$arg" = "y-o" ]
  then
    getnextarg=1
  fi
  # End - PtDB

  if [ "y$arg" = "y-v" ]
  then
    VERBOSE=on
  fi
  for t in -I -D 
  do
    testarg=${arg#$t}
    if [ "y$testarg" != "y$arg" -a "y$arg" != "y-I/usr/include" ]
    then
# /usr/include is already taken care of. It should not be passed by the user
      mod_arg=`echo $arg | sed -e 's/"/\\\"/g'`
# For PETSc, to take care of -DARG='"STR"' to generate -DARG=\"STR\"
      SPAWNARGS="$SPAWNARGS \"$mod_arg\""
    fi
  done
# For mpi.h to be classified as a system header we use:
# % cxxparse foo.cpp --sys_include/usr/local/packages/mpich-1.2/include

  for t in --sys_include
  do
    testarg=${arg#$t}
    if [ "y$testarg" != "y$arg" -a "y$arg" != "y--sys_include/usr/include" ]
    then
      mod_arg=`echo $arg | sed -e 's/--sys_include/--sys_include /g'`
      SPAWNARGS="$SPAWNARGS $mod_arg"
    fi
  done

# Processed an individual argument

done
#echo "SPAWNARGS=$SPAWNARGS"


ILF=${TMP}/pdt.$USER.$$.il
case ${INF} in
*.cc)	PDB=`basename ${INF} .cc`.pdb
	;;
*.cpp)	PDB=`basename ${INF} .cpp`.pdb
	;;
*.cxx)	PDB=`basename ${INF} .cxx`.pdb
	;;
*.C)	PDB=`basename ${INF} .C`.pdb
	;;
*)	PDB=${INF}.pdb
	;;
esac

# PtDB
if [ $gotpdbfile -eq 1 ]
then
  PDB=${NEWPDB}
  PDBOUTARG="-o ${PDB}"
fi
# End - PtDB

#
# run the EDG parser
#
if [ $VERBOSE = on ] 
then
  echo "	edgcpfe ${INF} --> ${ILF}"
fi

eval ${BINDIR}/edgcpfe ${SPAWNARGS} ${LOCOPT} ${COMPOPT} ${EDGOPT} ${ARCHOPT} ${ASMOPT1} ${ASMOPT2} ${ASMOPT3} -o ${ILF} ${INF} >/dev/null 2>/dev/null
if [ $? != 0 ] ; then
    eval ${BINDIR}/edgcpfe ${SPAWNARGS} ${LOCOPT} ${COMPOPT} ${EDGOPT} ${ARCHOPT} ${ASMOPT1} ${ASMOPT2} ${ASMOPT3} -o ${ILF} ${INF}
    if [ -x ${BINDIR}/roseparse ]
    then
      echo "PDT: Error: Couldn't create ${PDB}: Trying the alternate PDT Rose parser ${ORIGARGS}"
      ${BINDIR}/roseparse ${ORIGARGS}
      ret=$?
      if [ $ret != 0 ]; then 
        echo "PDT: Error parsing input file ${INF}"
      fi
      exit $ret
    else
      exit 1
    fi
fi

#
# convert output of parser to pdb file
#
if [ $VERBOSE = on ] 
then
  echo "	taucpdisp ${PDTOPT} ${ILF} --> ${PDB}"
fi
${BINDIR}/taucpdisp ${PDTOPT} ${ILF} > ${PDB}
rm -f ${ILF}

if [ ! -f ${PDB} -a -x ${BINDIR}/roseparse ]
then
  echo "PDT: Error: Couldn't create ${PDB}: Invoking the alternate PDT Rose parser"
  ${BINDIR}/roseparse ${ORIGARGS}
fi
