# MakefileTarget.dist: Makefile for Ballista Test Execution subsystem  
# Copyright (C) 1998-2001  Carnegie Mellon University
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
########################
#
# Entry Conditions: 
# 	The following environment variables are set:
#	 ${OBLIST} : a list of the datatype files to be linked into mut.out
#	 ${FUNCTION} : the name of the MuT
#	 ${LLIST} : library list
#
#	The utility programs callGen and replacer have been built
#
########################

# declare all the environment variables needed by the target compiler
CC = g++
CFLAGS = ${BFLAGS} -DPOSIX_NO_REALTIME -w -O -fPIC -DB_HANDLE_EXCEPTIONS
LDFLAGS = -shared ${BFLAGS} -lm

MUT_O = -o mut.o
MUNCH = mv mut.o mut.out
RECURSIVE_FLAGS = CC="${CC}" CFLAGS="${CFLAGS}"
TARGET_DEF    = -DB_SELFHOST

OBJS =	executeTestCase.o \
	unmarshal.o \
	templates/bTypes.o \
	${OBLIST} \
	${LLIST}

#  Define suffixes to be recognized by make
.SUFFIXES: .tpl .o .cpp

# any <datatype>.o file is dependant on a <datatype>.tpl file
# call make recursively 
.tpl.o :
	cd templates; ${MAKE} ${@F} ${RECURSIVE_FLAGS}

.cpp.o :
	${CC} ${CFLAGS} -c $<

# make mut.out by default
all: mut.out

# callInclude.cpp (the actual call the the MuT) is dynamicly generated
#  by callGen
# NOTE - we have a dependancy on the file mut.  makeBallista stores the name
#  of the mut being built in the mut file, and changes the file when the mut
#  name changes, thus this rule gets invoked when the mut we are building
#  against changes
callInclude.cpp : callTable dataTypes mut userCatches.cpp
	./callGen "${FUNCTION}"
	mv callInclude.cpp callInclude.old
	./replacer < callInclude.old > callInclude.cpp


# special rule for bTypes.o, as there is no bTypes.tpl
templates/bTypes.o : templates/bTypes.cpp
	cd templates; ${MAKE} ${@F} ${RECURSIVE_FLAGS}

# touch user created files to make sure they exist, if the user didn't
#  create them manually
userSetup.cpp :
	touch $@

userCatches.cpp :
	touch $@

userShutdown.cpp :
	touch $@

# the user-defined-includes file is automaticly generated from the
#  dataTypes file
executeTestCase.h : dataTypes
	/bin/echo "//executeTestCase.h: automatically generated header file - executes Ballista test" > executeTestCase.h
	/bin/echo "// Copyright (C) 1998-2001  Carnegie Mellon University" >> executeTestCase.h
	/bin/echo " " >> executeTestCase.h
	/bin/echo "// This program is free software; you can redistribute it and/or" >> executeTestCase.h
	/bin/echo "// modify it under the terms of the GNU General Public License" >> executeTestCase.h
	/bin/echo "// as published by the Free Software Foundation; either version 2" >> executeTestCase.h
	/bin/echo "// of the License, or (at your option) any later version." >> executeTestCase.h
	/bin/echo " " >> executeTestCase.h
	/bin/echo "// This program is distributed in the hope that it will be useful," >> executeTestCase.h
	/bin/echo "// but WITHOUT ANY WARRANTY; without even the implied warranty of" >> executeTestCase.h
	/bin/echo "// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the" >> executeTestCase.h
	/bin/echo "// GNU General Public License for more details." >> executeTestCase.h
	/bin/echo " " >> executeTestCase.h
	/bin/echo "// You should have received a copy of the GNU General Public License" >> executeTestCase.h
	/bin/echo "// along with this program; if not, write to the Free Software" >> executeTestCase.h
	/bin/echo "// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA." >> executeTestCase.h
	/bin/echo " " >> executeTestCase.h
	/bin/echo '#'include \"userIncludes.h\" >> executeTestCase.h
	for A in `echo ${OBLIST:.o=.h}`; do \
		/bin/echo '#'include \"$$A\" >> executeTestCase.h; \
	done

# the bulk of the executeTestCase code is built by replacer
executeTestCase.cpp : executeTestCase.fresh userSetup.cpp userCatches.cpp userShutdown.cpp callInclude.cpp
	./replacer < executeTestCase.fresh > executeTestCase.cpp

# once the executeTestCase code is generated, build the object file
# NOTE - ${OBLIST} is included as a dependancy to take advantage of its
#  side effect of generating .h files for the datatypes
executeTestCase.o : executeTestCase.cpp unmarshal.h executeTestCase.h ${OBLIST} userIncludes.h
	${CC} ${CFLAGS} -c executeTestCase.cpp

# mut.out contains all of the code to be loaded onto the target
# NOTE - we must to C++ munching in order for the C++ code to work on the
#  target machine
mut.out : ${OBJS}
	${CC} ${LDFLAGS} ${OBJS} ${MUT_O}
	${MUNCH}

# clean up everything that is generated
clean :
	rm -f mut.o executeTestCase.o unmarshal.o mut.out __mut.* mut \
		callInclude.cpp vardefs.cpp executeTestCase.cpp \
		executeTestCase.h
	cd templates; ${MAKE} clean	
