#!/bin/sh
#=============================================================================
#
#  Program:   GCC-XML
#  Module:    $RCSfile: find_flags,v $
#  Language:  C++
#  Date:      $Date: 2004/01/26 18:59:35 $
#  Version:   $Revision: 1.7 $
#
#  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 GCC executable name.
if test "x$1" = "x" ; then
  if test "x${CXX}" = "x" ; then
    CXX=gcc
  fi
else 
  CXX="$1"
  shift
  CXXFLAGS="$@"
fi

# Find the macro definition options.
MACROS=`echo "" | ${CXX} -x c++ -E -dM ${CXXFLAGS} - 2>/dev/null |
  sed -n '
/^#define / {s/#define \([A-Za-z_][A-Za-z0-9_]*\) \(.*\)/-D\1=\2/;p;}
' |
  awk '
BEGIN { first=1 }
/./ { if(first) { printf("%s", $0); first=0 } else { printf(" %s", $0) } }
'`

# Find the include path options.
INCLUDES=`
  echo "" | ${CXX} -v -x c++ -E ${CXXFLAGS} - 2>&1 |
  awk '/^\#include <.*$/   {f=1}
       /^ \/.*$/           { if (f) { printf("-I%s ",$0) } }
       /^End of search.*$/ {printf("\n"); exit}' - |
  sed 's/^-I /-I/;s/ -I / -I/g'
`

# The support headers are located where this script is.
SELFPATH=`echo $0 | sed -n '/\//{s/\/find_flags//;p;}'`
if test "x$SELFPATH" = "x" ; then SELFPATH="." ; fi
SELFPATH=`cd "$SELFPATH" ; pwd`

# Determine the major version number.
MAJOR_VERSION=`
echo "__GNUC__" | ${CXX} -v -x c++ -E ${CXXFLAGS} - 2>/dev/null |
  sed -n '/^[0-9]/{s/[^0-9]//g;p;}'`

MINOR_VERSION=`
echo "__GNUC_MINOR__" | ${CXX} -v -x c++ -E ${CXXFLAGS} - 2>/dev/null |
  sed -n '/^[0-9]/{s/[^0-9]//g;p;}'`

# For GCC versions before 3, some special options are needed.
if [ "$MAJOR_VERSION" -lt 3 ]; then
  INCLUDES="-iwrapper$SELFPATH/2.95 $INCLUDES"
  if [ "$MINOR_VERSION" = 96 ]; then
  INCLUDES="-iwrapper$SELFPATH/2.96 $INCLUDES"
  fi
elif [ "$MAJOR_VERSION" = 3 -a "$MINOR_VERSION" -ge 3 ]; then
  SPECIAL="-include $SELFPATH/3.3/gccxml_builtins.h"
fi

# Format and print out the options.
OPTIONS="$MACROS -D__declspec(x)= -D__attribute__(x)= $INCLUDES $SPECIAL"
echo $OPTIONS
