#!/bin/sh
#=============================================================================
#
#  Program:   GCC-XML
#  Module:    $RCSfile: gccxml_find_flags,v $
#  Language:  C++
#  Date:      $Date: 2004/01/22 21:45:45 $
#  Version:   $Revision: 1.2 $
#
#  Copyright (c) 2002 Kitware, Inc., Insight Consortium.  All rights reserved.
#  See Copyright.txt for details.
#
#     This software is distributed WITHOUT ANY WARRANTY; without even 
#     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
#     PURPOSE.  See the above copyright notices for more information.
#
#=============================================================================

# Find the compiler executable name.
if test "x$1" = "x" ; then
  if test "x$GCCXML_COMPILER" = "x"; then
    if test "x${CXX}" != "x" ; then
      GCCXML_COMPILER="${CXX}"
    else
      echo "Need to specify compiler name or set GCCXML_COMPILER or CXX."
      exit 1
    fi
  fi
  if test "x$GCCXML_CXXFLAGS" = "x"; then
    if test "x${CXXFLAGS}" != "x" ; then
      GCCXML_CXXFLAGS="${CXXFLAGS}"
    fi
  fi
else 
  GCCXML_COMPILER="$1"
  shift
  GCCXML_CXXFLAGS="$@"
fi

# Use the compiler's preprocessor to identify the compiler.
GCCXML_PID="$$"
cat > "/tmp/gccxml_identify_compiler$GCCXML_PID.cc" <<!
#if defined(__GNUC__)
GCCXML_SUPPORT="GCC"
#elif defined(__sgi) && defined(_COMPILER_VERSION)
GCCXML_SUPPORT="MIPSpro"
#elif defined(__INTEL_COMPILER) && (__INTEL_COMPILER >= 700)
GCCXML_SUPPORT="Intel"
#else
GCCXML_SUPPORT=""
#endif
!

# Run the compiler's preprocessor on the above code to identify it.
if (${GCCXML_COMPILER} ${GCCXML_CXXFLAGS} \
                         -E "/tmp/gccxml_identify_compiler$GCCXML_PID.cc" \
                          > "/tmp/gccxml_identify_compiler$GCCXML_PID.pp";
      rm -f "/tmp/gccxml_identify_compiler$GCCXML_PID.cc") ; then : ; else
  echo "Error running \"${GCCXML_COMPILER} -E\""
  exit 1
fi

# Source the result to get the settings found.
. "/tmp/gccxml_identify_compiler$GCCXML_PID.pp"
rm -f "/tmp/gccxml_identify_compiler$GCCXML_PID.pp"

# Check if a supported compiler was identified.
if test "x$GCCXML_SUPPORT" = "x" ; then
  echo "Compiler \"${GCCXML_COMPILER}\" is not supported by GCC-XML."
  exit 1
fi

# The support directories are located near this script.
SELFPATH=`cd "\`echo $0 | sed -n '/\//{s/\/[^\/]*$//;p;}'\`";pwd`

# Find the compiler-specific support directory.
if test -d "${SELFPATH}/${GCCXML_SUPPORT}" ; then
  GCCXML_SUPPORT_DIR="${SELFPATH}/${GCCXML_SUPPORT}"
else
  echo "Cannot find GCC_XML compiler support directory ${GCCXML_SUPPORT}."
  exit 1
fi

# Run the compiler-specific flag finding script.
GCCXML_FLAGS=`"${GCCXML_SUPPORT_DIR}/find_flags" ${GCCXML_COMPILER} ${GCCXML_CXXFLAGS}`

# Display the output.
echo ${GCCXML_FLAGS}
