# "Make.test-flatten"
#	vi: ft=automake
#	$Id: Make.test-flatten,v 1.12 2008/07/30 22:55:01 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_FLATTEN_ERROR_FILTER = $(AWK) \
		-f $(TEST_SRCDIR)/hackt-flatten-expect-filter.awk \
		-f $(TEST_SRCDIR)/vpath_file_filter.awk

HACKT_FLATTEN_SUFFIXES = \
	.hacf .hacf-filter .hacf-diff \
	.hacf-fail .hacf-fail-filter .hacf-fail-diff \
        .hacfo .compileflatdump .compileflatdump-filter \
        .compileflatdump-diff \
        .findump .findump-filter .hacfobjdiff
HACKT_FLATTEN_TEST_SUFFIXES = \
        .hacflattest \
        .hacflatfailtest \
	.hacfobjdifftest \
	.compileflatdump-test
SUFFIXES += $(HACKT_FLATTEN_SUFFIXES) $(HACKT_FLATTEN_TEST_SUFFIXES)
BOGUS_SUFFIX_TARGETS += $(HACKT_FLATTEN_SUFFIXES)

HACKT_FLATTEN_CLEAN_IGNORE_PATTERNS = \
	"*.hacf" \
	"*.hacf-filter" \
	"*.hacf-diff" \
	"*.hacf-fail" \
	"*.hacf-fail-filter" \
	"*.hacf-fail-diff" \
	"*.hacflattest" \
	"*.hacflatfailtest" \
	"*.hacfo" \
	"*.compiledump-filter-tmp" \
	"*.compileflatdump" \
	"*.compileflatdump-filter" \
	"*.compileflatdump-diff" \
	"*.compileflatdump-test" \
	"*.findump" \
	"*.findump-filter" \
	"*.hacfobjdiff" \
	"*.hacfobjdifftest"

AUTO_IGNORE += $(HACKT_FLATTEN_CLEAN_IGNORE_PATTERNS)

###############################################################################
# TODO: enable dependency tracking by default (see compile script)

HACKT_FLATTEN_TEST_DEFAULT_SCRIPT = \
	flags=`$(CAT) $< | $(EXTRACT_HACPP_FLAGS_PIPE)` && \
	cmd="$(HACKT_FLATTEN_EXE) $$flags $(HACO_FLAGS) $<" && \
	$(ECHO) "$$cmd > $@" && \
	$$cmd > $@ && \
	$(TOUCH) $@
#	@depbase=`$(ECHO) $@ | $(SED) 's/\.haco$$//g'` ; \
#	log="$$depbase.compiledump" ; \
#	$(ECHO) "$(HACKT_COMPILE_EXE) $(HACO_FLAGS) -M $$depbase.depend $< $@" ; \
#	if $(HACKT_COMPILE_EXE) $(HACO_FLAGS) -M "$$depbase.tmpd" $< $@ > "$$log" 2>&1 ; \
#	then $(CAT) "$$depbase.tmpd" | \
#		$(HAC_DEPEND_FILTER) > "$$depbase.depend" ; \
#		$(RM) "$$depbase.tmpd" ; \
#	else $(RM) "$$depbase.tmpd" ; exit 1 ; \
#	fi

# use this variant when piping stdin
HACKT_FLATTEN_TEST_STDIN_SCRIPT = \
	flags=`$(CAT) $< | $(EXTRACT_HACPP_FLAGS_PIPE)` && \
	cmd="$(HACKT_FLATTEN_EXE) $$flags $(HACO_FLAGS)" && \
	$(ECHO) "$$cmd < $< > $@" && \
	$$cmd < $< > $@ && \
	$(TOUCH) $@

.hac.hacf:
	@$(HACKT_FLATTEN_TEST_DEFAULT_SCRIPT)

.hacf.hacf-filter:
	@$(CAT) $< | $(HACKT_FLATTEN_ERROR_FILTER) > $@

if VERBOSE_CHECK
ECHO_FLATTEN_DIFF_COMMAND = $(ECHO) "$(DIFF) -bu $$expect $<"
else
ECHO_FLATTEN_DIFF_COMMAND = :
endif

.hacf-filter.hacf-diff:
	@stderr=`$(ECHO) $@ | $(SED) 's/\.hacf-diff$$/.hacf-expect/g'` ; \
	expect=$(srcdir)/$$stderr ; \
	$(RM) $@ ; \
	if test -f "$$expect" ; then \
	  $(ECHO_FLATTEN_DIFF_COMMAND) ; \
	  $(DIFF) -bu "$$expect" $< > $@ ; \
	  if test -s $@ ; then $(ECHO) "Differences in $@!" ; fi ; \
	else exit 1 ; \
	fi ; \
	$(TOUCH) $@

.hacf-diff.hacflattest:
	@$(DEFAULT_DIFF_TEST_SCRIPT)

hacflattest.autodepend: Makefile
	@$(ECHO) "Generating dependencies $@" ; \
	$(ECHO) "$(HACKT_FLATTEN_TEST_PASSES)" | \
		$(SPACES_TO_NEWLINE) | $(SORT) -u | \
		$(AWK) '/.+/ {print $$1 ".hacf-diff: $$(srcdir)/" $$1 ".hacf-expect"; }' > $@ ; \
	$(TOUCH) $@

-include hacflattest.autodepend

###############################################################################
# flattened files have no dependencies!
# really don't need include $(HACO_FLAGS) flags for this...
.hacf.hacfo:
	@log="$$depbase.compileflatdump" ; \
	cmd="$(HACKT_COMPILE_EXE) $< $@" ; \
	$(ECHO) "$$cmd" ; \
	$$cmd > "$$log" 2>&1

.hacfo.findump:
	$(DEFAULT_OBJDUMP_RULE_ACTION)

# additional sed filter fakes the file name as if it were the original source
.findump.findump-filter:
	@$(CAT) $< | $(HACKT_OBJ_DIFF_FILTER) | \
	$(SED) -e '/^In module/s|$*.hacf|$(srcdir)/$*.hac|' \
		-e '/^In module/s| \./| |' > $@ ; \
	$(ECHO_FILTERED)

.findump-filter.hacfobjdiff:
	@expect=`$(ECHO) $@ | $(SED) 's/\.hacfobjdiff$$/.indump-filter/g'` ; \
	$(RM) $@ ; \
	if test -f "$$expect" ; then \
	  $(ECHO_FLATTEN_DIFF_COMMAND) ; \
	  $(DIFF) -bu "$$expect" $< > $@ ; \
	  if test -s $@ ; then $(ECHO) "Differences in $@!" ; fi ; \
	else exit 1 ; \
	fi ; \
	$(TOUCH) $@

.hacfobjdiff.hacfobjdifftest:
	@$(DEFAULT_DIFF_TEST_SCRIPT)

hacfobjdifftest.autodepend: Makefile
	@$(ECHO) "Generating dependencies $@" ; \
	$(ECHO) "$(HACKT_FLATTEN_PARSE_TEST_PASSES)" | \
		$(SPACES_TO_NEWLINE) | $(SORT) -u | \
		$(AWK) '/.+/ {print $$1 ".hacfobjdiff: " $$1 ".indump-filter"; }' > $@ ; \
	$(TOUCH) $@

-include hacfobjdifftest.autodepend
AUTO_DEPENDS += hacfobjdifftest.autodepend

###############################################################################
# expect fail: dummy object target should fail to be produced
# really don't need include $(HACO_FLAGS) flags for this...
.hacf.compileflatdump:
	-@obj=`$(ECHO) $@ | $(SED) 's/\.compileflatdump$$/.haco/g'` ; \
	cmd="$(HACKT_COMPILE_EXE) $< $$obj" ; \
	$(ECHO) "$$cmd" ; \
	$$cmd > $@ 2>&1

COMPILEFLATDUMP_EXTRA_FILTER = $(SED) -e '/^At:/s/\.hacf/.hac/' \
	-e '/(imported-root.*)/s|\[.*\]|\[\#POS\#\]|' \
	-e '/^At:.*:0:/d'
# 1) trivially adjust file name as if it were non-flattened
# 2) expanded embdedded files take up larger file position
# 3) compensates for parse errors placement of import decl.

.compileflatdump.compileflatdump-filter:
	@$(CAT) $< | $(HACKT_COMPILE_ERROR_FILTER) | \
		$(COMPILEFLATDUMP_EXTRA_FILTER) > $@

.compileflatdump-filter.compileflatdump-diff:
	@expect=`$(ECHO) $@ | $(SED) 's/\.compileflatdump-diff$$/.compiledump-filter/g'` ; \
	$(RM) $@ ; \
	if test -f "$$expect" ; then \
	  $(ECHO_FLATTEN_DIFF_COMMAND) ; \
	  $(DIFF) -bu "$$expect" $< > $@ ; \
	  if test -s $@ ; then \
	    $(CAT) "$$expect" | $(COMPILEFLATDUMP_EXTRA_FILTER) > "$$expect"-tmp ; \
	    $(DIFF) -bu "$$expect"-tmp $< > $@ ; \
	    if test -s $@ ; then \
	      $(ECHO) "Differences in $@!" ; \
	    fi ; \
	  fi ; \
	else exit 1 ; \
	fi ; \
	$(TOUCH) $@

.compileflatdump-diff.compileflatdump-test:
	@$(DEFAULT_DIFF_TEST_SCRIPT)

compileflattest.autodepend: Makefile
	@$(ECHO) "Generating dependencies $@" ; \
	$(ECHO) "$(HACKT_FLATTEN_PARSE_TEST_FAILURES)" | \
		$(SPACES_TO_NEWLINE) | $(SORT) -u | \
		$(AWK) '/.+/ {print $$1 ".compileflatdump-diff: " $$1 ".compiledump-filter"; }' > $@ ; \
	$(TOUCH) $@

-include compileflattest.autodepend
AUTO_DEPENDS += compileflattest.autodepend

###############################################################################
.hac.hacf-fail:
	-$(HACKT_FLATTEN_EXE) $(HACO_FLAGS) $< > $@ 2> $@-stderr ; \
	$(CAT) $@-stderr >> $@ ; \
	$(RM) $@-stderr


.hacf-fail.hacf-fail-filter:
	@$(CAT) $< | $(HACKT_FLATTEN_ERROR_FILTER) > $@

.hacf-fail-filter.hacf-fail-diff:
	@stderr=`$(ECHO) $@ | $(SED) 's/\.hacf-fail-diff$$/.hacf-expect/g'` ; \
	expect=$(srcdir)/$$stderr ; \
	$(RM) $@ ; \
	if test -f "$$expect" ; then \
	  $(ECHO_FLATTEN_DIFF_COMMAND) ; \
	  $(DIFF) -bu "$$expect" $< > $@ ; \
	  if test -s $@ ; then $(ECHO) "Differences in $@!" ; fi ; \
	else exit 1 ; \
	fi ; \
	$(TOUCH) $@

.hacf-fail-diff.hacflatfailtest:
	@$(DEFAULT_DIFF_TEST_SCRIPT)

hacflatfailtest.autodepend: Makefile
	@$(ECHO) "Generating dependencies $@" ; \
	$(ECHO) "$(HACKT_FLATTEN_TEST_FAILURES)" | \
		$(SPACES_TO_NEWLINE) | $(SORT) -u | \
		$(AWK) '/.+/ {print $$1 ".hacf-fail-diff: $$(srcdir)/" $$1 ".hacf-expect"; }' > $@ ; \
	$(TOUCH) $@

-include hacflatfailtest.autodepend
###############################################################################


# dummy test for when make variable is empty
.hacflattest:
	@$(DUMMY_TEST_SCRIPT)

.hacflatfailtest:
	@$(DUMMY_TEST_SCRIPT)

.hacfobjdifftest:
	@$(DUMMY_TEST_SCRIPT)

.compileflatdump-test:
	@$(DUMMY_TEST_SCRIPT)


BOGUS_TESTS += .hacflattest .hacflatfailtest \
	.hacfobjdifftest .compileflatdump-test

BOGUS_TARGETS += .hac .hacf .hacf-fail .hacf-filter .hacf-fail-filter

AUTO_DEPENDS += hacflattest.autodepend \
	hacflatfailtest.autodepend

hacflatdiffs: force
	-$(CAT) *.hacf-diff > $@

hacflatfaildiffs: force
	-$(CAT) *.hacf-fail-diff > $@

hacfobjdiffs: force
	-$(CAT) *.hacfobjdiff > $@

compileflatdiffs: force
	-$(CAT) *.compileflatdump-diff > $@

CHECK_SUMMARIES += hacflatdiffs hacflatfaildiffs \
	hacfobjdiffs compileflatdiffs


HACKT_FLATTEN_TEST_SUBJECTS = \
	$(HACKT_FLATTEN_TEST_PASSES) \
	$(HACKT_FLATTEN_TEST_WARNINGS) \
	$(HACKT_FLATTEN_TEST_FAILURES)

srcdir_EXTRA_DIST += $(HACKT_FLATTEN_TEST_SUBJECTS:=.hac) \
	$(HACKT_FLATTEN_TEST_SUBJECTS:=.hacf-expect)

if CHECK_TESTS
TESTS += $(HACKT_FLATTEN_TEST_PASSES:=.hacflattest) \
	$(HACKT_FLATTEN_TEST_FAILURES:=.hacflatfailtest) \
	$(HACKT_FLATTEN_PARSE_TEST_PASSES:=.hacfobjdifftest) \
	$(HACKT_FLATTEN_PARSE_TEST_FAILURES:=.compileflatdump-test)
endif

# cover all possible cases, whether they pass or fail
HACKT_FLATTEN_DUMPS = $(HACKT_FLATTEN_TEST_SUBJECTS:=.hacf) \
	$(HACKT_FLATTEN_TEST_SUBJECTS:=.hacf-fail)
$(HACKT_FLATTEN_DUMPS): $(HACKT_FLATTEN_EXE)
# $(HACKT_FLATTEN_PARSE_TEST_PASSES:=.hacfo): $(HACKT_COMPILE_EXE)
# $(HACKT_FLATTEN_PARSE_TEST_PASSES:=.findump): $(HACKT_OBJDUMP_EXE)
# $(HACKT_FLATTEN_PARSE_TEST_FAILURES:=.compileflatdump): $(HACKT_COMPILE_EXE)

build-check-local: \
	$(HACKT_FLATTEN_TEST_PASSES:=.hacf) \
	$(HACKT_FLATTEN_TEST_PASSES:=.hacf-filter) \
	$(HACKT_FLATTEN_TEST_PASSES:=.hacf-diff) \
	$(HACKT_FLATTEN_TEST_FAILURES:=.hacf-fail) \
	$(HACKT_FLATTEN_TEST_FAILURES:=.hacf-fail-filter) \
	$(HACKT_FLATTEN_TEST_FAILURES:=.hacf-fail-diff) \
	$(HACKT_FLATTEN_PARSE_TEST_PASSES:=.hacfo) \
	$(HACKT_FLATTEN_PARSE_TEST_PASSES:=.findump) \
	$(HACKT_FLATTEN_PARSE_TEST_PASSES:=.findump-filter) \
	$(HACKT_FLATTEN_PARSE_TEST_PASSES:=.hacfobjdiff) \
	$(HACKT_FLATTEN_PARSE_TEST_FAILURES:=.compileflatdump) \
	$(HACKT_FLATTEN_PARSE_TEST_FAILURES:=.compileflatdump-filter) \
	$(HACKT_FLATTEN_PARSE_TEST_FAILURES:=.compiledump) \
	$(HACKT_FLATTEN_PARSE_TEST_FAILURES:=.compiledump-filter) \
	$(HACKT_FLATTEN_PARSE_TEST_FAILURES:=.compileflatdump-diff)


echo-flatten-passes: force
	@$(ECHO) $(HACKT_FLATTEN_TEST_PASSES)

echo-flatten-failures: force
	@$(ECHO) $(HACKT_FLATTEN_TEST_FAILURES)

echo-flatten-tests: force
	@$(ECHO) $(HACKT_FLATTEN_TEST_SUBJECTS)


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

clean-local: clean-flattentests

.PHONY: clean-flattentests

