# -*- Makefile -*-
# $Id: checklog 6488 2011-08-12 17:28:43Z sander $

#
# check-log
#
# This file implements the build-rules needed by the automated tests.
# "make check-log" will build the whole package and run all tests. For
# each test a log file under $(topsrc_dir)/check-log is created which
# contains the name of the test and what kind of test it was (build,
# lib, run, ...). If a test fails this is logged, but all other tests
# are still run.
#
# make OPTIONS:
# LOG_TIMING - specific command to measure the time needed for each test
#              example: "make LOG_TIMING='time -p' check-log"
#
# IMPLEMENTATION: check-log depends on different targets for different
# kinds of tests. These can run idividually by calling check-log-XXX.
# Possible variants are (in order of dependence)
#   - libs    (build all libraries)
#   - build   (build all applications and tests)
#   - test    (run each test from $(TESTS))
#   - sources (run sourcescheck in each directory)
#   - headers (run headercheck for each header)
#   - dir     (create dir entries for database, no actual tests are run)
#

LOG_FILE = $(top_srcdir)/check-log.log
LOG_DIR = $(top_srcdir)/check-log
LOG_STORE = $(SHELL) $(DUNE_COMMON_ROOT)/bin/check-log-store $(LOG_FILE) $(LOG_DIR)
LOG_TIMING = 
LOG_SET_OPTIONS = \
	path=`test "$(subdir)" = "." && \
	echo "/@PACKAGE_NAME@" || echo "/@PACKAGE_NAME@/$(subdir)"`; \
	ppath=`dirname $$path`; \
	dir=`basename $$path`

$(DUNE_COMMON_ROOT)/bin/check-log-store: $(DUNE_COMMON_ROOT)/bin/check-log-store.in
	cd $(DUNE_COMMON_ROOT)/bin/ && $(MAKE) check-log-store

check-log: $(DUNE_COMMON_ROOT)/bin/check-log-store
check-log:
	rm -f $(LOG_FILE)

check-log: check-log-libs
check-log: check-log-build
check-log: check-log-test
check-log: check-log-sources
check-log: check-log-headers
check-log: check-log-dir

check-log-libs: check-log-libs-recursive
check-log-build: check-log-build-recursive
check-log-test: check-log-test-recursive
check-log-sources: check-log-sources-recursive
check-log-headers: check-log-headers-recursive
check-log-dir: check-log-dir-recursive

check-log-headers-am:
	$(LOG_SET_OPTIONS); \
	list=`echo $(headercheck_PATTERN)`; \
	test "$$list" != "$(headercheck_PATTERN)" || exit 0; \
	for f in $$list; do \
	  echo "$(headercheck_IGNORE)" | tr ' ' '\n' | grep -q "$$f"; \
	    if echo "$(headercheck_IGNORE)" | tr ' ' '\n' | grep -q "$$f"; then continue; fi; \
	  $(LOG_TIMING) ($(MAKE) headercheck-am SILENT=0 HEADER=$$f; \
	    $(MAKE) headercheck-am SILENT=0 HEADER=$$f NO_CONFIG_H=1) > $(LOG_FILE) 2>&1; \
	  $(LOG_STORE) "headercheck" "$$f" "$$path"; \
	done

check-log-sources-am:
	$(LOG_SET_OPTIONS); \
	$(LOG_TIMING) $(MAKE) sourcescheck-am > $(LOG_FILE) 2>&1; \
	$(LOG_STORE) "sources" "Makefile.am" "$$path"

check-log-libs-am:
	$(LOG_SET_OPTIONS); \
	for lib in $(LTLIBRARIES); do \
	  $(LOG_TIMING) ($(MAKE) $$lib) > $(LOG_FILE) 2>&1; \
	  $(LOG_STORE) "lib" "$$lib" "$$path"; \
	done;

check-log-build-am:
	$(LOG_SET_OPTIONS); \
	for target in $(check_PROGRAMS) $(PROGRAMS); do \
	  $(LOG_TIMING) ($(MAKE) $$target) > $(LOG_FILE) 2>&1; \
	  $(LOG_STORE) "build" "$$target" "$$path"; \
	done;

check-log-test-am:
	@LOG () { \
	  echo "$$1" > $(LOG_FILE).2; \
	  cat $(LOG_FILE) >> $(LOG_FILE).2; \
	  mv $(LOG_FILE).2 $(LOG_FILE); \
	}; \
	$(LOG_SET_OPTIONS); \
	for tst in $(TESTS); do \
	  if test -f ./$$tst; then dir=./; \
	  elif test -f $$tst; then dir=; \
	  else dir="$(srcdir)/"; fi; \
	  echo "TEST $${dir}$$tst"; \
	  if $(LOG_TIMING) $(TESTS_ENVIRONMENT) $${dir}$$tst > $(LOG_FILE) 2>&1; then \
	    case " $(XFAIL_TESTS) " in \
	    *" $$tst "*) \
	      LOG "ERROR: XPASS ($$tst)"; \
		  $(LOG_STORE) "run" "$$tst" "$$path" 1 0; \
	    ;; \
	    *) \
	      LOG "SUCCESS: PASS ($$tst)"; \
		  $(LOG_STORE) "run" "$$tst" "$$path" 0 0; \
	    ;; \
	    esac; \
	  elif test $$? -ne 77; then \
	    case " $(XFAIL_TESTS) " in \
	    *" $$tst "*) \
	      LOG "SUCCESS: XFAIL ($$tst)"; \
		  $(LOG_STORE) "run" "$$tst" "$$path" 0 0; \
	    ;; \
	    *) \
	      LOG "ERROR: FAIL ($$tst)"; \
		  $(LOG_STORE) "run" "$$tst" "$$path" 1 0; \
	    ;; \
	    esac; \
	  else \
	    LOG "WARNING: SKIP ($$tst)"; \
		$(LOG_STORE) "run" "$$tst" "$$path" 0 1; \
	  fi; \
	done;

check-log-dir-am:
	$(LOG_SET_OPTIONS); \
	$(LOG_STORE) "dir" "$$dir" "$$ppath"

check-log-libs-recursive \
check-log-build-recursive \
check-log-test-recursive \
check-log-sources-recursive \
check-log-headers-recursive \
check-log-dir-recursive:
	@set fnord $$MAKEFLAGS; amf=$$2; \
	dot_seen=no; \
	target=`echo $@ | sed s/-recursive//`; \
	list='$(DIST_SUBDIRS)'; for subdir in $$list; do \
	  echo "Making $$target in $$subdir"; \
	  if test "$$subdir" = "."; then \
	    dot_seen=yes; \
	    local_target="$$target-am"; \
	  else \
	    local_target="$$target"; \
	  fi; \
	  (cd $$subdir && $(MAKE) $(AM_MAKEFLAGS) $$local_target) \
	   || case "$$amf" in *=*) exit 1;; *k*) fail=yes;; *) exit 1;; esac; \
	done; \
	if test "$$dot_seen" = "no"; then \
	  $(MAKE) $(AM_MAKEFLAGS) "$$target-am" || exit 1; \
	fi; test -z "$$fail"

.PHONY: check-log \
	check-log-lib check-log-lib-am check-log-lib-recursive \
	check-log-build check-log-build-am check-log-build-recursive \
	check-log-test check-log-test-am check-log-test-recursive \
	check-log-sources check-log-sources-am check-log-sources-recursive \
	check-log-dir check-log-dir-am check-log-dir-recursive
