#!/bin/sh 

######################################################################

# This is a simple wrapper to  the 'make' command.  It is catching all
# calls to compilers  and memorizing used options.  If  you clean your
# project and rebuild the  project with 'xref-recipe-make' (instead of
# 'make'), you will  obtain the list of compiled  files and options in
# the file  /tmp/recipe.  This  file is directly  usable by xref  as a
# recipe for your project.

######################################################################

export XREFACTORY_RECIPE_FILE="/tmp/recipe"

export XREFACTORY_FLAGS="-includesrelativetocwd -ignoreunknownopt \
  -ignoreopt2-o -ignoreopt2-I -ignoreopt2-Xlinker \
  -ignoreopt2-u -ignoreopt2-b -ignoreopt2-V"

######################################################################

# define current paths to compilers
export XREFACTORY_WHICH_CPP=`which c++`
export XREFACTORY_WHICH_GPP=`which g++`
export XREFACTORY_WHICH_GCC=`which gcc`
export XREFACTORY_WHICH_CC=`which cc`
export XREFACTORY_WHICH_JAVAC=`which javac`

# get absolute path to this file from $0 or $PATH
whichxrm=`which xref-recipe-make`
if test `expr substr $0 1 1` = '/'
then
   XREF_RECIPE_MAKE_ABS_PATH=$0
elif test `expr substr $0 1 1` = '.'
then
	XREF_RECIPE_MAKE_ABS_PATH=${PWD}/./$0
elif test `expr substr ${whichxrm} 1 1` = '/'
then
	XREF_RECIPE_MAKE_ABS_PATH=${whichxrm}
elif test `expr substr ${whichxrm} 1 1` = '.'
then
	XREF_RECIPE_MAKE_ABS_PATH=${PWD}/./${whichxrm}
else
   echo "Can not infer absolute path to the file xref-recipe-make."
   echo "Put its directory into PATH, or invoke it with absolute prefix."
   echo "Sorry."
   exit 1
fi

# remove old recipe
rm -f ${XREFACTORY_RECIPE_FILE}


# redefine PATH, so that xref wrappers are used instead of compilers
# and build the project

( export PATH=${XREF_RECIPE_MAKE_ABS_PATH}-compiler-wrappers:${PATH} ; make $* )


#All done.
echo Done.

