# here's where we actually run the driver, bench.

.PHONY: run

run:
	../../bin/bench "$(CATEGORY)" "$(TEST)" "$(TITLE)" "$(MAKE)" "$(PUBLISH_URL)" "$(WORDS)" $(COMPILERS)


# ---------------------------------------

.PHONY: clean clean_objs

# cleaning up a test
clean:
	rm -f $(TEST) Main.hbc $(EXTRA_CLEANS) *.actual *.status *~ *.compile *.db
clean_objs:
	rm -f *.hi *.o

# ---------------------------------------
# run just a particular few tests
# usage:
#    env ONLY="ghc jhc" make just
#

.PHONY: just

just:
	@if [ -z "$(ONLY)" ] ; then echo "usage: env ONLY="ghc jhc" make just" ; exit 1 ; fi
	( category=`pwd | xargs dirname | xargs basename` ; ../../bin/bench $$category "$(TEST)" "$(TITLE)" "$(MAKE)" "$(PUBLISH_URL)" "../../$(WORDS)" $(ONLY) ; )

# ------------------------------------------------------------
# here's where the actual rules for building each system are defined:
# we assume we're in the current test directory.

# building ghc-based tests
.PHONY: ghc compile-ghc run-ghc

ghc: compile-ghc run-ghc

compile-ghc:
	$(GHC) -no-recomp $(GHC_OPTS) $(GHC_EXTRA_OPTS) --make -o $(TEST) $(TEST).$(TEST_SUFFIX) > ghc.compile 2>&1

run-ghc:
	../../bin/timeout $(TIMEOUT) "./$(TEST) $(TEST_ARGS) $(GHC_RUN_OPTS) < $(TEST_INPUT) > $(TEST).ghc.actual 2>&1" || v=$$? ; echo $$v > ghc.status ; exit $$v

# building ghc-old based tests
.PHONY: ghc-old compile-ghc-old run-ghc-old

ghc-old: compile-ghc-old run-ghc-old

compile-ghc-old:
	$(GHC_OLD) -no-recomp $(GHC_OLD_OPTS) $(GHC_OLD_EXTRA_OPTS) --make -o $(TEST) $(TEST).$(TEST_SUFFIX) > ghc-old.compile 2>&1

run-ghc-old:
	../../bin/timeout $(TIMEOUT) "./$(TEST) $(TEST_ARGS) $(GHC_OLD_RUN_OPTS) < $(TEST_INPUT) > $(TEST).ghc-old.actual 2>&1" || v=$$? ; echo $$v > ghc-old.status ; exit $$v

# ------------------------------------------------------------
# building hugs tests
.PHONY: hugs compile-hugs run-hugs

hugs: compile-hugs run-hugs

compile-hugs:

run-hugs:
	../../bin/timeout $(TIMEOUT) "$(HUGS) $(HUGS_OPTS) $(HUGS_RUN_OPTS) $(HUGS_EXTRA_OPTS) $(TEST).$(TEST_SUFFIX) $(TEST_ARGS) < $(TEST_INPUT) > $(TEST).hugs.actual 2>&1" || v=$$? ; echo $$v > hugs.status ; exit $$v


# ------------------------------------------------------------
# building ghci tests
.PHONY: ghci compile-ghci run-ghci

ghci: compile-ghci run-ghci

compile-ghci:

run-ghci: clean_objs
	../../bin/timeout $(TIMEOUT) "$(GHCI) $(GHCI_OPTS) $(GHCI_EXTRA_OPTS) $(GHCI_RUN_OPTS) $(TEST).$(TEST_SUFFIX) $(TEST_ARGS) < $(TEST_INPUT) > $(TEST).ghci.actual 2>&1" || v=$$? ; echo $$v > ghci.status ; exit $$v


# ------------------------------------------------------------
# building nhc98 tests
.PHONY: nhc98 compile-nhc98 run-nhc98

nhc98: compile-nhc98 run-nhc98

compile-nhc98:
	$(NHC) $(NHC_OPTS) $(NHC_EXTRA_OPTS) -o $(TEST) $(TEST).$(TEST_SUFFIX) > nhc98.compile 2>&1

run-nhc98: clean_objs
	../../bin/timeout $(TIMEOUT) "./$(TEST) $(TEST_ARGS) $(NHC_RUN_OPTS) < $(TEST_INPUT) > $(TEST).nhc98.actual 2>&1" || v=$$? ; echo $$v > nhc98.status ; exit $$v

# ------------------------------------------------------------
# building yhc tests
.PHONY: yhc compile-yhc run-yhc

yhc: compile-yhc run-yhc

compile-yhc:
	$(YHC) $(YHC_OPTS) $(YHC_EXTRA_OPTS) $(TEST).$(TEST_SUFFIX) > yhc.compile 2>&1

run-yhc: clean_objs
	../../bin/timeout $(TIMEOUT) "yhi $(YHC_RUN_OPTS) Main.hbc $(TEST_ARGS) < $(TEST_INPUT) > $(TEST).yhc.actual 2>&1" || v=$$? ; echo $$v > yhc.status ; exit $$v


# ------------------------------------------------------------
# building ghc-based tests
.PHONY: hbc compile-hbc run-hbc

hbc: compile-hbc run-hbc

# doesn't spot $? == 0 when hbcmake fails
compile-hbc:
	hmake -realclean $(HBC_OPTS) $(HBC_EXTRA_OPTS) $(TEST) > /dev/null  2>&1
	$(HBC) $(HBC_OPTS) $(HBC_EXTRA_OPTS) $(TEST) > hbc.compile 2>&1

run-hbc:
	../../bin/timeout $(TIMEOUT) "./$(TEST) $(TEST_ARGS) $(HBC_RUN_OPTS) < $(TEST_INPUT) > $(TEST).hbc.actual 2>&1" || v=$$? ; echo $$v > hbc.status ; exit $$v
