#!/bin/sh

#
# 
#  Copyright (c) 2002 Microsoft Corporation.  All rights reserved.
# 
#  The use and distribution terms for this software are contained in the file
#  named license.txt, which can be found in the root of this distribution.
#  By using this software in any fashion, you are agreeing to be bound by the
#  terms of this license.
# 
#  You must not remove this notice, or any other, from this software.
# 
#
#     mdtool_gcc
#
#Abstract:
#  Generates dependencies for a makefile 
#  (for gcc compilers)
#

str=`grep -n '#mdtool output goes here>' makefile`
if [ "$str" = "" ] ; then
#didn't find our placeholder string : don't change file
    echo mdtool_gcc : makefile not setup for mdtool : ignoring
    exit 0
fi

#truncate makefile to remove old dependencies
sed /'#mdtool output goes here>'/q makefile > makefile.final

echo '#dependencies generated by mdtool_gcc :' >> makefile.final
echo >> makefile.final
# gcc -M generates make-friendly dependencies;
# -MM ignores system includes (#include <file.h>)
gcc -MM $@ >> makefile.depfromgcc
#Prepend object target directory name to the object's filename
#with some sed magic, since gcc doesn't do it for us.
sed "s|^.*\.o|obj$BUILD_ALT_DIR/&|g" makefile.depfromgcc > makefile.aftersed
cat makefile.aftersed >> makefile.final

if [ "$?" = "0" ] ; then
   echo mdtool_gcc : dependencies generated
   # replace old makefile by new one.
   mv -f makefile.final makefile
   rm -f makefile.aftersed
   rm -f makefile.depfromgcc
   exit 0
fi
exit 1

