# Makefile.in for test of yash
# (C) 2007-2016 magicant
#
# 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, see <http://www.gnu.org/licenses/>.

.POSIX:
.SUFFIXES: .c .h .d .o .tst .trs

@MAKE_SHELL@
topdir = ..
subdir = tests

CC = @CC@
CFLAGS = @CFLAGS@
CPPFLAGS = @CPPFLAGS@
LDFLAGS = @LDFLAGS@
LDLIBS = @LDLIBS@
SOURCES = checkfg.c resetsig.c
POSIX_TEST_SOURCES = alias-p.tst andor-p.tst arith-p.tst async-p.tst bg-p.tst break-p.tst builtins-p.tst case-p.tst cd-p.tst cmdsub-p.tst command-p.tst comment-p.tst continue-p.tst dot-p.tst error-p.tst eval-p.tst exec-p.tst exit-p.tst export-p.tst fg-p.tst fnmatch-p.tst for-p.tst fsplit-p.tst function-p.tst getopts-p.tst grouping-p.tst if-p.tst input-p.tst job-p.tst kill-p.tst lineno-p.tst nop-p.tst option-p.tst param-p.tst path-p.tst pipeline-p.tst ppid-p.tst quote-p.tst read-p.tst readonly-p.tst redir-p.tst return-p.tst set-p.tst shift-p.tst signal-p.tst simple-p.tst test-p.tst testtty-p.tst tilde-p.tst trap-p.tst umask-p.tst unset-p.tst until-p.tst wait-p.tst while-p.tst
YASH_TEST_SOURCES = alias-y.tst andor-y.tst arith-y.tst array-y.tst async-y.tst bg-y.tst bindkey-y.tst brace-y.tst break-y.tst builtins-y.tst case-y.tst cd-y.tst cmdsub-y.tst command-y.tst complete-y.tst continue-y.tst dirstack-y.tst dot-y.tst echo-y.tst error-y.tst eval-y.tst exec-y.tst exit-y.tst export-y.tst fc-y.tst fg-y.tst for-y.tst fsplit-y.tst function-y.tst getopts-y.tst grouping-y.tst hash-y.tst help-y.tst history-y.tst historyx-y.tst if-y.tst job-y.tst jobs-y.tst kill-y.tst lineno-y.tst option-y.tst param-y.tst pipeline-y.tst printf-y.tst prompt-y.tst pwd-y.tst quote-y.tst random-y.tst read-y.tst readonly-y.tst redir-y.tst return-y.tst set-y.tst settty-y.tst shift-y.tst signal-y.tst simple-y.tst startup-y.tst suspend-y.tst test-y.tst tilde-y.tst times-y.tst trap-y.tst typeset-y.tst ulimit-y.tst umask-y.tst unset-y.tst until-y.tst wait-y.tst while-y.tst
TEST_SOURCES = $(POSIX_TEST_SOURCES) $(YASH_TEST_SOURCES)
TEST_RESULTS = $(TEST_SOURCES:.tst=.trs)
RECHECK_LOGS = $(TEST_RESULTS)
TARGET = @TARGET@
YASH = $(topdir)/$(TARGET)
TESTERS = checkfg resetsig
TESTEE = $(YASH)
RUN_TEST = ./resetsig $(YASH) ./run-test.sh
SUMMARY = summary.log
BYPRODUCTS = $(SOURCES:.c=.o) $(TESTERS) $(TEST_RESULTS) $(SUMMARY) *.dSYM

test:
	rm -rf $(RECHECK_LOGS)
	@$(MAKE) $(SUMMARY)
	@tail -n 6 $(SUMMARY)
	@tail -n 6 $(SUMMARY) | grep ^FAILED | \
		(read _ fail_count && \
		if [ "$$fail_count" -ne 0 ]; then \
			echo See $(SUMMARY) for details; \
			exit 1; \
		fi)
test-posix:
	@$(MAKE) TEST_SOURCES='$$(POSIX_TEST_SOURCES)' test
test-yash:
	@$(MAKE) TEST_SOURCES='$$(YASH_TEST_SOURCES)' test
test-valgrind:
	@$(MAKE) RUN_TEST='$(RUN_TEST) -v' test

$(SUMMARY): $(TEST_RESULTS)
	$(SHELL) ./summarize.sh $(TEST_RESULTS) >| $@
$(TEST_RESULTS): $(TESTERS) $(YASH)
.tst.trs:
	@set $(RUN_TEST) $(TESTEE) $<; \
	if grep -q %SEQUENTIAL% $<; then \
		set $(SHELL) ./enqueue.sh "$$@"; \
	fi; \
	echo "$$*"; \
	"$$@"

tester: $(TESTERS)
$(TESTERS):
	$(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) -o $@ $@.c $(LDLIBS)
$(YASH):
	@echo Make $(TARGET) in $(topdir) first >&2; false

.c.o:
	@rm -f $@
	$(CC) $(CFLAGS) $(CPPFLAGS) -c $<

DISTFILES = $(SOURCES) $(SOURCES:.c=.d) Makefile.in POSIX2013 README enqueue.sh run-test.sh summarize.sh
distfiles: makedeps $(DISTFILES)
copy-distfiles: distfiles
	mkdir -p $(topdir)/$(DISTTARGETDIR)
	cp $(DISTFILES) $(TEST_SOURCES) $(topdir)/$(DISTTARGETDIR)
makedeps: _PHONY
	@(cd $(topdir) && $(MAKE) $(TARGET))
	$(topdir)/$(TARGET) $(topdir)/makedeps.yash $(SOURCES)

mostlyclean:
	rm -fr $(BYPRODUCTS)
clean: mostlyclean
distclean: clean
	rm -fr Makefile
maintainer-clean: distclean
	rm -fr $(SOURCES:.c=.d)

Makefile: Makefile.in $(topdir)/config.status
	@+(cd $(topdir) && $(MAKE) config.status)
	@(cd $(topdir) && $(SHELL) config.status $(subdir)/$@)

.PHONY: test test-posix test-yash test-valgrind tester distfiles copy-distfiles makedeps mostlyclean clean distclean maintainer-clean
_PHONY:

@MAKE_INCLUDE@ checkfg.d
@MAKE_INCLUDE@ resetsig.d
