# "Make.test-vpi"
#	vi: ft=automake
#	$Id: Make.test-vpi,v 1.9 2009/08/28 20:45:40 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.  

# known VCS issues:
# C-2009.06's vcs-generated Makefile does not properly
#	declare SUFFIXES and relies on implicit rules (fails with make -r)

HACKT_VPI_SUFFIXES = .v .vx .vxi .vx-out .vxi-out .vx-out-filter .vx-diff \
	.v-wrap

HACKT_VPI_TEST_SUFFIXES = .vpitest 
SUFFIXES += $(HACKT_VPI_SUFFIXES) $(HACKT_VPI_TEST_SUFFIXES)
BOGUS_SUFFIX_TARGETS += $(HACKT_VPI_SUFFIXES)

HACKT_VPI_CLEAN_IGNORE_PATTERNS = \
	"*.vx" \
	"*.v-wrap" \
	"*.vxi" \
	"*.vx-out" \
	"*.vxi-out" \
	"*.vx-out-filter" \
	"*.vx-diff" \
	"*.vpitest" \
	"*.csrc" \
	"*.daidir"

VPI_PRSIM_MODULE = $(top_builddir)/src/vpihacprsim.la
AUTO_WRAP_VERILOG_AWK = $(AWK) -f $(top_srcdir)/lib/verilog/wrap_verilog_modules_to_hacprsim.awk

AUTO_IGNORE += $(HACKT_VPI_CLEAN_IGNORE_PATTERNS)
AUTO_IGNORE += .vcsmx_rebuild
DISTCLEANFILES += .vcsmx_rebuild

VCS_OUTPUT_FILTER = \
	$(SED) -n '/^[$$]start of simulation/,/^[$$]finish at simulation/p' | \
	$(GREP) -v "\$$finish called from file"
# format may change slightly between versions
# initial version tested was Y-2006.06
# added grep for version C-2009-06

# where vpihacprim.la is built
VPI_LIB_PATH = $(top_builddir)/src
# where vpihacprsim.{la,so} are installed
INSTALLCHECK_VPI_LIB_PATH = $(pkglibdir)

COMMON_VCS_FLAGS = +cli+3 -Mupdate +v2k +vcsace+diff+ok
# wait for license if none available, damn commercial tools
COMMON_VCS_FLAGS += +vcs+lic+wait
# requires env. LM_LICENSE_FILE
if ABI_ILP64
# COMMON_VCS_FLAGS += -comp64
COMMON_VCS_FLAGS += -full64
endif

AM_VCS_VPI_FLAGS = +vpi \
	-use_vpiobj $(VPI_LIB_PATH)/$(LTDL_OBJDIR)/vpihacprsim.so \
	-LDFLAGS="-L$(VPI_LIB_PATH)/$(LTDL_OBJDIR)" 
AM_VCS_FLAGS = +incdir+$(srcdir) $(COMMON_VCS_FLAGS) -Mdir=$@.csrc
AM_PLI_FLAGS =

# for install-time tests, use installed library, not the built one
INSTALLCHECK_VCS_VPI_FLAGS = +vpi \
	-use_vpiobj $(INSTALLCHECK_VPI_LIB_PATH)/vpihacprsim.so \
	-LDFLAGS="-L$(INSTALLCHECK_VPI_LIB_PATH)" 
# lazy: for now, use the .. dir instead of +incdir+$(examplesdir)/...
# because compilation is done in a temporary subdirectory
# the generated Makefiles have a variable named $(VSRC) that could be used...
INSTALLCHECK_VCS_FLAGS = +incdir+$(srcdir) $(COMMON_VCS_FLAGS) -Mdir=$$@.csrc
INSTALLCHECK_PLI_FLAGS =

EXTRACT_VCS_FLAGS_PIPE = $(GREP) "@vcs-flags@" | \
        $(SED) 's/^.*@vcs-flags@[ ]*//g'

# create simulation executable
# '+' connects child make (vcs) to parent's jobserver
# touch b/c incremental compilation doesn't always updated timestamp
.v.vx:
if HAVE_VPI
	+vflags=`cat $< | $(EXTRACT_VCS_FLAGS_PIPE)` && \
	$(VCS_ENV) $(VCS) $(AM_VCS_FLAGS) $(AM_VCS_VPI_FLAGS) $(AM_PLI_FLAGS) \
		$$vflags -o $@ $< && \
	$(TOUCH) $@
else
	@$(ECHO) "VPI not explicitly enabled during configure."
endif

.v.v-wrap:
	{ $(ECHO) "\`include \"$<\"" ; $(ECHO) "" ; \
		$(AUTO_WRAP_VERILOG_AWK) $< ;} > $@

# post-install tests!
.v.vxi:
if HAVE_VPI
	+$(VCS_ENV) $(VCS) $(INSTALLCHECK_VCS_FLAGS) \
		$(INSTALLCHECK_VCS_VPI_FLAGS) $(INSTALLCHECK_PLI_FLAGS) -o $@ $<
else
	@$(ECHO) "VPI not explicitly enabled during configure."
endif

# setup run-time link path for build testing
VPI_TEST_ENV = \
	$(LTDL_SHLIBPATH_VAR)=$(VPI_LIB_PATH)/$(LTDL_OBJDIR)$(PATH_SEPARATOR)$$$(LTDL_SHLIBPATH_VAR)

# installcheck environment
VPI_INSTALLCHECK_ENV = \
	$(LTDL_SHLIBPATH_VAR)=$(INSTALLCHECK_VPI_LIB_PATH)$(PATH_SEPARATOR)$$$(LTDL_SHLIBPATH_VAR)

# SHOW_VPI_TEST_ENV = $(LTDL_SHLIBPATH_VAR)=$(VPI_LIB_PATH)/$(LTDL_OBJDIR)$(PATH_SEPARATOR)$$"$(LTDL_SHLIBPATH_VAR)"

.vx.vx-out:
if HAVE_VPI
	$(VPI_TEST_ENV) ./$< > $@ 2>&1
else
	@$(ECHO) "VCS/VPI was not explicitly configured, so skipping." > $@
endif

.vxi.vxi-out:
	$(VPI_INSTALLCHECK_ENV) ./$< > $@ 2>&1

.vx-out.vx-out-filter:
	$(CAT) $< | $(VCS_OUTPUT_FILTER) > $@ ; \
	$(TOUCH) $@
#	$(GREP) "observed" $< > $@ || $(TOUCH) $@
#	$(CP) $< $@

if VERBOSE_CHECK
ECHO_VPI_DIFF_COMMAND = $(ECHO) "$(DIFF) -bu $$""(srcdir)/$$stderr $<"
else
ECHO_VPI_DIFF_COMMAND = :
endif

.vx-out-filter.vx-diff:
if HAVE_VPI
	@stderr=`$(ECHO) $@ | $(SED) 's/\.vx-diff$$/.expect-vpi/g'` ; \
	expect=$(srcdir)/$$stderr ; \
	$(RM) $@ ; \
	if test -f "$$expect" ; then \
	  $(ECHO_VPI_DIFF_COMMAND) ; \
	  $(DIFF) -bu "$$expect" $< > $@ 2>&1 ; \
	  if test -s $@ ; then \
	    $(ECHO) "$@ is non-empty!" ; \
	  fi ; \
	else $(ECHO) "Missing $$expect." | $(TEE) $@ ; \
	fi ; \
	$(TOUCH) $@
else
	@$(CP) $< $@
endif

.vx-diff.vpitest:
if HAVE_VPI
	@$(DEFAULT_DIFF_TEST_SCRIPT)
else
	@$(SKIP_TEST_SCRIPT)
endif

vpitest.autodepend: Makefile
	@$(ECHO) "Generating dependencies $@" ; \
	$(RM) $@ ; \
	{ list='$(HACKT_VPI_TEST_SCRIPTS_PASSES)' ; \
	exts='vx-out' ; \
	test -z "$$list" || \
	for d in $$list ; do \
	  src=$(srcdir)/$$d.v ; \
	  obj=`$(CAT) "$$src" | $(EXTRACT_HACO_PIPE)` ; \
	  deps=`$(CAT) "$$src" | $(EXTRACT_DEPS_PIPE)` ; \
	  for e in $$exts ; do \
	    target=$$d.$$e ; \
	    $(ECHO) -n "$$target " ; \
	  done ; \
	  $(ECHO) ": $$obj $$deps" ; \
	done ; \
	$(ECHO) "$(HACKT_VPI_TEST_SCRIPTS_BASES)" | \
	  $(SPACES_TO_NEWLINE) | $(SORT) -u | \
	  $(AWK) '/.+/ {print $$1 ".vx-diff: $$(srcdir)/" $$1 ".expect-vpi"; }'; \
	} > $@ ; \
	$(TOUCH) $@

-include vpitest.autodepend

AUTO_DEPENDS += vpitest.autodepend

BOGUS_TESTS += .vpitest

CHECK_SUMMARIES += vpidiffs


vpidiffs: force
	-$(CAT) *.vx-diff > $@


HACKT_VPI_TEST_SCRIPTS_BASES = \
	$(HACKT_VPI_TEST_SCRIPTS_PASSES) \
	$(HACKT_VPI_TEST_SCRIPTS_FAILURES)

srcdir_EXTRA_DIST += \
	$(HACKT_VPI_TEST_SCRIPTS_BASES:=.v) \
	$(HACKT_VPI_TEST_SCRIPTS_BASES:=.expect-vpi)

if CHECK_TESTS
TESTS += $(HACKT_VPI_TEST_SCRIPTS_PASSES:=.vpitest)
endif

VPI_SIM_EXECUTABLES = $(HACKT_VPI_TEST_SCRIPTS_PASSES:=.vx)
if HAVE_VPI
# exeuctables depend on the plugin module
$(VPI_SIM_EXECUTABLES): $(VPI_PRSIM_MODULE)
endif

VPI_TARGETS =
if HAVE_VPI
VPI_TARGETS += \
	$(HACKT_VPI_TEST_SCRIPTS_PASSES:=.vx-out) \
	$(HACKT_VPI_TEST_SCRIPTS_PASSES:=.vx-out-filter) \
	$(HACKT_VPI_TEST_SCRIPTS_PASSES:=.vx-diff)
endif

build-check-local: $(VPI_TARGETS)

echo-vpi-tests: force
	@$(ECHO) $(HACKT_VPI_TEST_SCRIPTS_BASES)

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

# 'csrc' is the temporary directory used in VCS compilation
CLEANDIRS += csrc .csrc \
	$(HACKT_VPI_TEST_SCRIPTS_BASES:=.csrc) \
	$(HACKT_VPI_TEST_SCRIPTS_BASES:=.vx.csrc) \
	$(HACKT_VPI_TEST_SCRIPTS_BASES:=.vxi.csrc) \
	$(HACKT_VPI_TEST_SCRIPTS_BASES:=.vx.daidir) \
	$(HACKT_VPI_TEST_SCRIPTS_BASES:=.vxi.daidir)

clean-local: clean-vpitests

.PHONY: clean-vpitests

