# "Make.test-cflat"
#	vi: ft=automake
#	$Id: Make.test-cflat,v 1.15 2008/03/17 23:03:26 fang Exp $
# This is just a portion of the Make.test-tail template included by
# Makefile.am's in the test/ directory subtree.  
# This depends on variables defined in Make.test-tail, and thus, 
# should not be included by itself.  


HACKT_CFLAT_SUFFIXES = \
	.prs .prsdiff .prs-sort .expect-prs .expect-prs-sort .prsdiff-sort \
	.sprs .sprs-sort .sprs-diff .sprsdiff-sort \
	.prsseu .prsseu-sort .prsseu-diff prsseudiff-sort \
	.lvsprs .expect-lvsprs .lvsprsdiff .lvsprs-sort \
	.expect-lvsprs-sort .lvsprsdiff-sort \
	.lvsfail .lvsfaildiff \
	.lvssprs
HACKT_CFLAT_TEST_SUFFIXES = \
	.hacktcflattest \
	.hacktcflatlvstest \
	.hacktcflatlvsfailtest \
	.hacktcflatseutest \
	.hacktsprstest
SUFFIXES += $(HACKT_CFLAT_SUFFIXES) $(HACKT_CFLAT_TEST_SUFFIXES)
BOGUS_SUFFIX_TARGETS += $(HACKT_CFLAT_SUFFIXES)

# CVS ignore and clean these patterns
HACKT_CFLAT_CLEAN_IGNORE_PATTERNS = \
	"*.hacktcflattest" \
	"*.hacktcflatlvstest" \
	"*.hacktcflatlvsfailtest" \
	"*.hacktsprstest" \
	"*.hacktcflatseutest" \
	"*.hacktprsimexprtest" \
	"*.hacktprsdottest" \
	"*.prs" \
	"*.prs-sort" \
	"*.prsdiff" \
	"*.prsdiff-sort" \
	"*.lvssprs" \
	"*.lvsprs" \
	"*.lvsprsdiff" \
	"*.lvsprsdiff-sort" \
	"*.lvsfail" \
	"*.lvsfaildiff" \
	"*.prsseu" \
	"*.prsseudiff" \
	"*.prsseu-diff" \
	"*.prsimexpr" \
	"*.prsimexprdiff" \
	"*.prs-dot" \
	"*.prs-dot-O1" \
	"*.prs-*-ps" \
	"*.prs-*-fig" \
	"*.prs-*-pdf" \
	"*.sprs" \
	"*.sprsdiff" \
	"*.sprsdiff-sort"

AUTO_IGNORE += $(HACKT_CFLAT_CLEAN_IGNORE_PATTERNS)

# CFLAT tests

# TODO: unify all prs tests into one set of Makefile suffix rules and
# embed the flags variations in the source files with @hflat-flags@.  
# Rationale: remove clutter of suffixes for some trivial variations.  
# Not all variations, because that would result in excessive 
# duplicate object files (can that be avoided?).  
# TODO: test configuration files that dictate exact dependencies and options,
# e.g. source (or .) <script that sets variables>

# compute flags, -t <type> is always last for now (doesn't matter)
DEDUCE_HFLAT_VARIANT = \
	srcfile=$(srcdir)/$*.hac ; \
	flags=`$(CAT) $$srcfile | $(EXTRACT_HFLAT_FLAGS_PIPE)` ; \
	type=`$(CAT) $$srcfile | $(GREP) "@hflat-type@" | $(HEAD) -n1 | \
		$(SED) -e 's/^.*-type@[ \t]*//g' -e 's/\"//g'` ; \
	if test -n "$$type" ; then \
		flags="$$flags -t $$type" ; \
	fi


# the touch is only necessary if the output is empty, in which case
# we need to force a timestamp update
.haco-a.prs:
	@$(DEDUCE_HFLAT_VARIANT) ; \
	cmd="$(HACKT_CFLAT_PRSIM_EXE) $$flags $<" ; \
	$(ECHO) "$$cmd > $@" ; \
	$$cmd > $@
	@$(TOUCH) $@

OLD_PRSIM_TEST_SCRIPT =
if HAVE_PRSIM
OLD_PRSIM_TEST_SCRIPT += \
	else \
	case $* in \
	echo* ) break ;; \
	macro* ) break ;; \
	spec* ) break ;; \
	weak* ) break ;; \
	*) @PRSIM@ "$$out" < /dev/null > /dev/null 2>&1 || \
	  $(ECHO) "Error: @PRSIM@ rejected $$out." ;; \
	esac
else
OLD_PRSIM_TEST_SCRIPT += :
endif

DEFAULT_SORT_SCRIPT = $(SORT) $< > $@

.prs.prs-sort:
	$(DEFAULT_SORT_SCRIPT)

.expect-prs.expect-prs-sort:
	$(DEFAULT_SORT_SCRIPT)

CFLATPRS_DIFF_COMMAND = $(DIFF) -bu $< $$out
if VERBOSE_CHECK
ECHO_CFLATPRS_DIFF_COMMAND = $(ECHO) "$(CFLATPRS_DIFF_COMMAND)"
else
ECHO_CFLATPRS_DIFF_COMMAND = :
endif

.expect-prs-sort.prsdiff-sort:
	@out=`$(ECHO) $@ | $(SED) 's/\.prsdiff-sort$$/.prs-sort/g'` ; \
	if test -f "$$out" ; then \
	  $(ECHO_CFLATPRS_DIFF_COMMAND) ; \
	  $(CFLATPRS_DIFF_COMMAND) > $@ ; \
	  if test ! -s $@ ; then \
	    $(ECHO) "Only re-ordering differences found.  (OK)" ; \
	  fi ; \
	else $(ECHO) "Missing $$out." | $(TEE) $@ ; \
	fi ; \
	$(TOUCH) $@

.expect-prs.prsdiff:
	@out=`$(ECHO) $@ | $(SED) 's/\.prsdiff$$/.prs/g'` ; \
	$(RM) $@ ; \
	if test -f "$$out" ; then \
	  $(ECHO_CFLATPRS_DIFF_COMMAND) ; \
	  $(CFLATPRS_DIFF_COMMAND) > $@ ; \
	  if test -s $@ ; then \
	    $(ECHO) "$@ is non-empty!" ; \
	    $(MAKE) $@-sort ; \
	    $(OLD_PRSIM_TEST_SCRIPT) ; \
	  fi ; \
	else $(ECHO) "Missing $$out." | $(TEE) $@ ; \
	fi ; \
	$(TOUCH) $@

.prsdiff.hacktcflattest:
	@$(DEFAULT_DIFF_TEST_SCRIPT)

.hacktcflattest:
	@$(DUMMY_TEST_SCRIPT)

cflatprstest.autodepend: Makefile
	@$(ECHO) "Generating dependencies $@" ; \
	$(ECHO) "$(HACKT_CFLAT_TEST_SUBJECTS)" | \
	  $(SPACES_TO_NEWLINE) | $(SORT) -u | \
	  $(AWK) '/.+/ {print $$1 ".prsdiff: " $$1 ".prs"; print $$1 ".prsdiff-sort: " $$1 ".prs-sort"; }' > $@ ; \
	$(TOUCH) $@


-include cflatprstest.autodepend

if VERBOSE_CHECK
ECHO_CFLAT_LVS_COMMAND = $(ECHO) "$$cmd > $@"
else
ECHO_CFLAT_LVS_COMMAND = :
endif

HACKT_CFLAT_LVS_SCRIPT = \
	$(DEDUCE_HFLAT_VARIANT) ; \
	cmd="$(HACKT_CFLAT_LVS_EXE) $$flags $<" ; \
	$(ECHO_CFLAT_LVS_COMMAND) ; \
	$$cmd > $@ 2>&1


# if the source file has a magic line @hflat-type@, then extract it
# as the type to process for cflatting.
# The hflat-type string must not contain any spaces!
.haco-a.lvsprs:
	@$(HACKT_CFLAT_LVS_SCRIPT)

.haco-a.lvsfail:
	-@$(HACKT_CFLAT_LVS_SCRIPT)

if VERBOSE_CHECK
ECHO_CFLAT_LVS_SIZE_COMMAND = $(ECHO) "$$cmd > $@"
else
ECHO_CFLAT_LVS_SIZE_COMMAND = :
endif

.haco-a.lvssprs:
	@$(DEDUCE_HFLAT_VARIANT) ; \
	cmd="$(HACKT_CFLAT_LVS_EXE) -fsizes $$flags $<" ; \
	$(ECHO_CFLAT_LVS_SIZE_COMMAND) ; \
	$$cmd > $@

.lvsprs.lvsprs-sort:
	$(DEFAULT_SORT_SCRIPT)

.expect-lvsprs.expect-lvsprs-sort:
	$(DEFAULT_SORT_SCRIPT)

if VERBOSE_CHECK
ECHO_CFLATLVS_DIFF_COMMAND = $(ECHO) "$(DIFF) -bu $< $$out"
else
ECHO_CFLATLVS_DIFF_COMMAND = :
endif

.expect-lvsprs-sort.lvsprsdiff-sort:
	@out=`$(ECHO) $@ | $(SED) 's/\.lvsprsdiff-sort$$/.lvsprs-sort/g'` ; \
	if test -f "$$out" ; then \
	  $(ECHO_CFLATLVS_DIFF_COMMAND) ; \
	  $(DIFF) -bu $< "$$out" > $@ ; \
	  if test ! -s $@ ; then \
	    $(ECHO) "Only re-ordering differences found.  (OK)" ; \
	  fi ; \
	else $(ECHO) "Missing $$out." | $(TEE) $@ ; \
	fi ; \
	$(TOUCH) $@

.expect-lvsprs.lvsprsdiff:
	@out=`$(ECHO) $@ | $(SED) 's/\.lvsprsdiff$$/.lvsprs/g'` ; \
	$(RM) $@ ; \
	if test -f "$$out" ; then \
	  $(ECHO_CFLATLVS_DIFF_COMMAND) ; \
	  $(DIFF) -bu $< "$$out" > $@ ; \
	  if test -s $@ ; then \
	    $(ECHO) "$@ is non-empty!" ; \
	  fi ; \
	else $(ECHO) "Missing $$out." | $(TEE) $@ ; \
	fi ; \
	$(TOUCH) $@

.expect-lvsprs.lvsfaildiff:
	@out=`$(ECHO) $@ | $(SED) 's/\.lvsfaildiff$$/.lvsfail/g'` ; \
	$(RM) $@ ; \
	if test -f "$$out" ; then \
	  $(ECHO_CFLATLVS_DIFF_COMMAND) ; \
	  $(DIFF) -bu $< "$$out" > $@ ; \
	  if test -s $@ ; then \
	    $(ECHO) "$@ is non-empty!" ; \
	  fi ; \
	else $(ECHO) "Missing $$out." | $(TEE) $@ ; \
	fi ; \
	$(TOUCH) $@

.lvsprsdiff.hacktcflatlvstest:
	@$(DEFAULT_DIFF_TEST_SCRIPT)

.lvsfaildiff.hacktcflatlvsfailtest:
	@$(DEFAULT_DIFF_TEST_SCRIPT)

.hacktcflatlvstest:
	@$(DUMMY_TEST_SCRIPT)

.hacktcflatlvsfailtest:
	@$(DUMMY_TEST_SCRIPT)

cflatlvstest.autodepend: Makefile
	@$(ECHO) "Generating dependencies $@" ; \
	$(ECHO) "$(HACKT_CFLAT_LVS_TEST_PASSES)" | \
	  $(SPACES_TO_NEWLINE) | $(SORT) -u | \
	  $(AWK) '/.+/ {print $$1 ".lvsprsdiff: " $$1 ".lvsprs"; print $$1 ".lvsprsdiff-sort: " $$1 ".lvsprs-sort"; }' > $@ ; \
	$(TOUCH) $@

cflatlvsfailtest.autodepend: Makefile
	@$(ECHO) "Generating dependencies $@" ; \
	$(ECHO) "$(HACKT_CFLAT_LVS_TEST_FAILURES)" | \
	  $(SPACES_TO_NEWLINE) | $(SORT) -u | \
	  $(AWK) '/.+/ {print $$1 ".lvsfaildiff: " $$1 ".lvsfail"; }' > $@ ; \
	$(TOUCH) $@

-include cflatlvstest.autodepend
-include cflatlvsfailtest.autodepend

# not bothering to extract @hflat-flags@ for these tests
.haco-a.prsseu:
	$(HACKT_CFLAT_PRSIM_SEU_EXE) $< > $@

.prsseu.prsseu-sort:
	$(DEFAULT_SORT_SCRIPT)

.expect-prsseu.expect-prsseu-sort:
	$(DEFAULT_SORT_SCRIPT)

if VERBOSE_CHECK
ECHO_CFLATPRSSEU_DIFF_COMMAND = $(ECHO) "$(DIFF) -bu $< $$out"
else
ECHO_CFLATPRSSEU_DIFF_COMMAND = :
endif

.expect-prsseu-sort.prsseudiff-sort:
	@out=`$(ECHO) $@ | $(SED) 's/\.prsseudiff-sort$$/.prsseu-sort/g'` ; \
	if test -f "$$out" ; then \
	  $(ECHO_CFLATPRSSEU_DIFF_COMMAND) ; \
	  $(DIFF) -bu $< "$$out" > $@ ; \
	  if test ! -s $@ ; then \
	    $(ECHO) "Only re-ordering differences found.  (OK)" ; \
	  fi ; \
	else $(ECHO) "Missing $$out." | $(TEE) $@ ; \
	fi ; \
	$(TOUCH) $@

.expect-prsseu.prsseudiff:
	@out=`$(ECHO) $@ | $(SED) 's/\.prsseudiff$$/.prsseu/g'` ; \
	$(RM) $@ ; \
	if test -f "$$out" ; then \
	  $(ECHO_CFLATPRSSEU_DIFF_COMMAND) ; \
	  $(DIFF) -bu $< "$$out" > $@ ; \
	  if test -s $@ ; then \
	    $(ECHO) "$@ is non-empty!" ; \
	  fi ; \
	else $(ECHO) "Missing $$out." | $(TEE) $@ ; \
	fi ; \
	$(TOUCH) $@

.prsseudiff.hacktcflatseutest:
	@$(DEFAULT_DIFF_TEST_SCRIPT)

.hacktcflatseutest:
	@$(DUMMY_TEST_SCRIPT)

cflatprsseutest.autodepend: Makefile
	@$(ECHO) "Generating dependencies $@" ; \
	$(ECHO) "$(HACKT_CFLAT_SEU_TEST_SUBJECTS)" | \
	  $(SPACES_TO_NEWLINE) | $(SORT) -u | \
	  $(AWK) '/.+/ {print $$1 ".prsseudiff: " $$1 ".prsseu"; print $$1 ".prsseudiff-sort: " $$1 ".prsseu-sort"; }' > $@ ; \
	$(TOUCH) $@

-include cflatprsseutest.autodepend


.haco-a.sprs:
	@$(DEDUCE_HFLAT_VARIANT) ; \
	cmd="$(HACKT_CFLAT_PRSIM_EXE) -fsizes $$flags $<" ; \
	$(ECHO) "$$cmd > $@" ; \
	$$cmd > $@


.sprs.sprs-sort:
	$(DEFAULT_SORT_SCRIPT)

.expect-sprs.expect-sprs-sort:
	$(DEFAULT_SORT_SCRIPT)

SPRS_DIFF_COMMAND = $(DIFF) -bu $< $$out
if VERBOSE_CHECK
ECHO_SPRS_DIFF_COMMAND = $(ECHO) "$(SPRS_DIFF_COMMAND)"
else
ECHO_SPRS_DIFF_COMMAND = :
endif

.expect-sprs-sort.sprsdiff-sort:
	@out=`$(ECHO) $@ | $(SED) 's/\.sprsdiff-sort$$/.sprs-sort/g'` ; \
	if test -f "$$out" ; then \
	    $(ECHO_SPRS_DIFF_COMMAND) ; \
	    $(SPRS_DIFF_COMMAND) > $@ ; \
	    if test ! -s $@ ; then \
	      $(ECHO) "Only re-ordering differences found.  (OK)" ; \
	    fi ; \
	else $(ECHO) "Missing $$out." | $(TEE) $@ ; \
	fi ; \
	$(TOUCH) $@

.expect-sprs.sprsdiff:
	@out=`$(ECHO) $@ | $(SED) 's/\.sprsdiff$$/.sprs/g'` ; \
	$(RM) $@ ; \
	if test -f "$$out" ; then \
	    $(ECHO_SPRS_DIFF_COMMAND) ; \
	    $(SPRS_DIFF_COMMAND) > $@ ; \
	    if test -s $@ ; then \
	      $(ECHO) "$@ is non-empty!" ; \
	    fi ; \
	else $(ECHO) "Missing $$out." | $(TEE) $@ ; \
	fi ; \
	$(TOUCH) $@

.sprsdiff.hacktsprstest:
	@$(DEFAULT_DIFF_TEST_SCRIPT)

.hacktsprstest:
	@$(DUMMY_TEST_SCRIPT)

cflatsprstest.autodepend: Makefile
	@$(ECHO) "Generating dependencies $@" ; \
	$(ECHO) "$(HACKT_CFLAT_SPRS_TEST_SUBJECTS)" | \
	  $(SPACES_TO_NEWLINE) | $(SORT) -u | \
	  $(AWK) '/.+/ {print $$1 ".sprsdiff: " $$1 ".sprs"; print $$1 ".sprsdiff-sort: " $$1 ".sprs-sort"; }' > $@ ; \
	$(TOUCH) $@

-include cflatsprstest.autodepend


if HAVE_CFLAT
.cast.castprs:
	$(CFLAT) -prsim $< > $@
else
.cast.castprs:
	@$(ECHO) "Sorry, no cflat found in path." ; exit 1
endif


AUTO_DEPENDS += cflatprstest.autodepend \
	cflatlvstest.autodepend \
	cflatlvsfailtest.autodepend \
	cflatprsseutest.autodepend \
	cflatsprstest.autodepend

BOGUS_TESTS += .hacktcflattest .hacktcflatlvstest .hacktcflatlvsfailtest \
	.hacktcflatseutest .hacktsprstest
BOGUS_TARGETS += .expect-prs

CHECK_SUMMARIES += prsdiffs prsdiffs.sort \
	lvsprsdiffs lvsfaildiffs \
	prsseudiffs prsseudiffs.sort sprsdiffs sprsdiffs.sort


prsdiffs: force
	-$(CAT) *.prsdiff > $@

lvsprsdiffs: force
	-$(CAT) *.lvsprsdiff > $@

lvsfaildiffs: force
	-$(CAT) *.lvsfaildiff > $@

prsseudiffs: force
	-$(CAT) *.prsseu-diff > $@

sprsdiffs: force
	-$(CAT) *.sprsdiff > $@

prsdiffs.sort: force
	-$(CAT) *.prsdiff-sort > $@

prsseudiffs.sort: force
	-$(CAT) *.prsseu-diff-sort > $@

sprsdiffs.sort: force
	-$(CAT) *.sprsdiff.sort > $@


HACKT_CFLAT_LVS_TEST_SUBJECTS = \
	$(HACKT_CFLAT_LVS_TEST_PASSES) \
	$(HACKT_CFLAT_LVS_TEST_FAILURES)

srcdir_EXTRA_DIST += \
	$(HACKT_CFLAT_LVS_TEST_SUBJECTS:=.hac) \
	$(HACKT_CFLAT_LVS_TEST_SUBJECTS:=.expect-lvsprs) \
	$(HACKT_CFLAT_SEU_TEST_SUBJECTS:=.expect-prsseu) \
	$(HACKT_CFLAT_SPRS_TEST_SUBJECTS:=.expect-sprs)

# this contains empty files to be removed, listed in EMPTY_CFLAT_PRS
EXTRA_DIST += $(HACKT_CFLAT_TEST_SUBJECTS:=.expect-prs)

if CHECK_TESTS
TESTS += \
	$(HACKT_CFLAT_TEST_SUBJECTS:=.hacktcflattest) \
	$(HACKT_CFLAT_LVS_TEST_PASSES:=.hacktcflatlvstest) \
	$(HACKT_CFLAT_LVS_TEST_FAILURES:=.hacktcflatlvsfailtest) \
	$(HACKT_CFLAT_SEU_TEST_SUBJECTS:=.hacktcflatseutest) \
	$(HACKT_CFLAT_SPRS_TEST_SUBJECTS:=.hacktsprstest)
endif

CFLAT_BUILD_CHECK_LOCAL = \
	$(HACKT_CFLAT_TEST_SUBJECTS:=.haco-a) \
	$(HACKT_CFLAT_TEST_SUBJECTS:=.prs) \
	$(HACKT_CFLAT_TEST_SUBJECTS:=.prsdiff) \
	$(HACKT_CFLAT_SEU_TEST_SUBJECTS:=.haco-a) \
	$(HACKT_CFLAT_SEU_TEST_SUBJECTS:=.prsseu) \
	$(HACKT_CFLAT_SEU_TEST_SUBJECTS:=.prsseudiff) \
	$(HACKT_CFLAT_LVS_TEST_SUBJECTS:=.haco) \
	$(HACKT_CFLAT_LVS_TEST_SUBJECTS:=.haco-c) \
	$(HACKT_CFLAT_LVS_TEST_SUBJECTS:=.haco-a) \
	$(HACKT_CFLAT_LVS_TEST_PASSES:=.lvsprs) \
	$(HACKT_CFLAT_LVS_TEST_PASSES:=.lvsprsdiff) \
	$(HACKT_CFLAT_LVS_TEST_FAILURES:=.lvsfail) \
	$(HACKT_CFLAT_LVS_TEST_FAILURES:=.lvsfaildiff)

build-check-local: $(CFLAT_BUILD_CHECK_LOCAL)

EMPTY_CFLAT_PRS = $(EMPTY_CFLAT_PRS_TESTS:=.expect-prs)

clean-empty-cflat-prs:
	-test -z "$(EMPTY_CFLAT_PRS)" || $(RM) $(EMPTY_CFLAT_PRS)

# need a bogus dependency to make it work... ?
$(EMPTY_CFLAT_PRS): Makefile
	@$(TOUCH) $@

CLEANFILES += $(EMPTY_CFLAT_PRS)
AUTO_IGNORE += $(EMPTY_CFLAT_PRS)

all-local: $(EMPTY_CFLAT_PRS)

echo-empty-cflat-prs: force
	@$(ECHO) $(EMPTY_CFLAT_PRS)

echo-cflat-tests: force
	@$(ECHO) $(HACKT_CFLAT_TEST_SUBJECTS)

echo-cflat-lvs-passes: force
	@$(ECHO) $(HACKT_CFLAT_LVS_TEST_PASSES)

echo-cflat-lvs-failures: force
	@$(ECHO) $(HACKT_CFLAT_LVS_TEST_FAILURES)

echo-cflat-lvs-tests: force
	@$(ECHO) $(HACKT_CFLAT_LVS_TEST_SUBJECTS)

echo-cflat-sprs-tests: force
	@$(ECHO) $(HACKT_CFLAT_SPRS_TEST_SUBJECTS)


clean-cflattests:
	patternlist='$(HACKT_CFLAT_CLEAN_IGNORE_PATTERNS)' ; \
	for p in $$patternlist ; do \
	  pp=`$(ECHO) $$p | $(SED) 's/"//g'` ; \
	  $(RM_PATTERN) "$$pp" ; \
	done

clean-local: clean-cflattests

.PHONY: clean-cflattests

