#
#  Tests of ulmBLAS.  Usually invoked from the top-level directory:
#
#      make check           all tests
#      make check-numeric   only the C++ tests (no Fortran compiler needed)
#      make check-netlib    only the Netlib BLAS test suites (FC, CC needed)
#      make check-reference-lapack
#                           test programs of Reference-LAPACK 3.12.1
#                           (downloaded, not part of make check)
#      make check-reference-lapack-lin
#                           LU and Cholesky (paths GE, PO) with the test
#                           programs of Reference-LAPACK 3.12.1 for linear
#                           equations (downloaded, not part of make check)
#      make check-conformance
#                           differential test against the reference BLAS
#                           and CBLAS of LAPACK 3.12.1 (downloaded, not
#                           part of make check)
#
#  All tests are built and run for each test configuration in config/
#  (different block sizes, see config/make.testcase*.inc) in
#  build/<configuration>.  The tests are run and evaluated by run-tests.sh.
#
#  make check runs the Netlib test suites only if a usable Fortran compiler,
#  C compiler and Fortran runtime are found; otherwise they are reported as
#  SKIP with the reason.  With STRICT=1 skipped tests make 'make check' fail.
#
#  Variables:
#
#      CONFIGS        test configurations   (default: all in config/)
#      NETLIB_SUITES  Netlib test suites    (default: C F77)
#      TESTS          only run tests whose ID or name contains one of these
#                     strings
#      VERBOSE=1      print every test result, not only problems
#      STRICT=1       skipped tests are errors
#
TOPDIR = ..

include $(TOPDIR)/make.inc

CONFIGS       ?= $(patsubst config/make.%.inc,%,\
                            $(wildcard config/make.testcase*.inc))
NETLIB_SUITES ?= C F77

LIB_TARGETS     = $(addprefix lib-,$(CONFIGS))
LIBF77_TARGETS  = $(addprefix lib-f77-,$(CONFIGS))
NUMERIC_TARGETS = $(addprefix numeric-,$(CONFIGS))
NETLIB_TARGETS  = $(foreach s,$(NETLIB_SUITES),$(addprefix netlib-$(s)-,$(CONFIGS)))

BUILD          = build
PROBE_LOG      = $(BUILD)/netlib-tools.log

RUN_TESTS      = TEST_BUILD='$(BUILD)' CONFIGS='$(CONFIGS)' \
                 TESTS='$(TESTS)' VERBOSE='$(VERBOSE)' STRICT='$(STRICT)' \
                 CC=$(call shell_quote,$(CC) $(CPPFLAGS) $(ULMBLAS_CFLAGS) $(CFLAGS)) \
                 CXX=$(call shell_quote,$(CXX) $(CXXSTD) $(CPPFLAGS)) \
                 $(SHELL) run-tests.sh

.PHONY : all check check-numeric check-netlib check-reference-lapack \
         check-reference-lapack-lin \
         check-conformance \
         build-numeric build-netlib \
         clean $(LIB_TARGETS) $(LIBF77_TARGETS) $(NUMERIC_TARGETS) \
         $(NETLIB_TARGETS)

all : check

include $(TOPDIR)/mk/tools.mk

#
#  Build targets (one per configuration and kind of test, so that the
#  configurations can be built in parallel with make -j)
#
build-numeric : $(NUMERIC_TARGETS)

build-netlib : $(NETLIB_TARGETS)

$(LIB_TARGETS) : lib-% : tool-ar tool-cxx-link
	@$(MAKE) -f Makefile.config CONFIG=$* libs

$(LIBF77_TARGETS) : lib-f77-% : tool-ar
	@$(MAKE) -f Makefile.config CONFIG=$* libs-f77

$(NUMERIC_TARGETS) : numeric-% : lib-%
	@$(MAKE) -f Makefile.config CONFIG=$* numeric

$(filter netlib-C-%,$(NETLIB_TARGETS)) : netlib-C-% : lib-%
	@$(MAKE) -f Makefile.config CONFIG=$* netlib-C

$(filter netlib-F77-%,$(NETLIB_TARGETS)) : netlib-F77-% : lib-f77-%
	@$(MAKE) -f Makefile.config CONFIG=$* netlib-F77

#
#  check: the Netlib suites are included if the tools they need work.
#
check : tool-ar
	@mkdir -p $(BUILD); \
	if  $(PROBE) fc > $(PROBE_LOG) 2>&1 \
	 && $(PROBE) cc >> $(PROBE_LOG) 2>&1 \
	 && $(PROBE) fc-link >> $(PROBE_LOG) 2>&1; \
	then \
	    suites='$(NETLIB_SUITES)'; rm -f $(PROBE_LOG); \
	    targets='build-numeric build-netlib'; \
	else \
	    suites=; targets=build-numeric; \
	    echo "note: Netlib test suites will be skipped, see below"; \
	fi; \
	$(MAKE) $$targets \
	&& NUMERIC=yes NETLIB_SUITES="$$suites" NETLIB_SKIP_LOG=$(PROBE_LOG) \
	   $(RUN_TESTS)

check-numeric : build-numeric
	@NUMERIC=yes NETLIB_SUITES= $(RUN_TESTS)

check-netlib : tool-ar tool-cc tool-fc-link
	@$(MAKE) build-netlib
	@NUMERIC=no NETLIB_SUITES='$(NETLIB_SUITES)' $(RUN_TESTS)

#
#  check-reference-lapack: the BLAS and CBLAS test programs of
#  Reference-LAPACK 3.12.1, downloaded (network access needed) and run
#  against the libraries of configuration REFERENCE_CONFIG (default block
#  sizes and kernels), see reference-lapack.sh.  Not part of make check.
#
REFERENCE_CONFIG ?= testcase04

check-reference-lapack : tool-ar tool-cc tool-fc-link
	@$(MAKE) lib-$(REFERENCE_CONFIG) lib-f77-$(REFERENCE_CONFIG)
	@CC=$(call shell_quote,$(CC)) CFLAGS=$(call shell_quote,$(CPPFLAGS) $(ULMBLAS_CFLAGS) $(CFLAGS)) \
	 FC=$(call shell_quote,$(FC)) FFLAGS=$(call shell_quote,$(FOPTFLAGS) $(FFLAGS)) \
	 CXX=$(call shell_quote,$(CXX) $(ULMBLAS_CXXFLAGS) $(CXXFLAGS)) \
	 LDFLAGS=$(call shell_quote,$(LDFLAGS)) LDLIBS=$(call shell_quote,$(LDLIBS)) \
	 FLIBS=$(call shell_quote,$(FLIBS)) \
	 $(SHELL) reference-lapack.sh $(BUILD)/$(REFERENCE_CONFIG)/lib \
	          $(BUILD)/reference-lapack

#
#  check-reference-lapack-lin: ?getrf and ?potrf (Fortran 77 interface)
#  with the test programs xlintst[sdcz] of Reference-LAPACK 3.12.1 on the
#  paths GE and PO, see reference-lapack-lin.sh.  Not part of make check.
#
check-reference-lapack-lin : tool-ar tool-fc-link
	@$(MAKE) lib-f77-$(REFERENCE_CONFIG)
	@FC=$(call shell_quote,$(FC)) FFLAGS=$(call shell_quote,$(FOPTFLAGS)) \
	 $(if $(LIN_LDLIBS),LDLIBS=$(call shell_quote,$(LIN_LDLIBS))) \
	 $(if $(wildcard $(BUILD)/reference-lapack/lapack-3.12.1.tar.gz),LAPACK_TARBALL=$(BUILD)/reference-lapack/lapack-3.12.1.tar.gz) \
	 $(SHELL) reference-lapack-lin.sh $(BUILD)/$(REFERENCE_CONFIG)/lib \
	          $(BUILD)/reference-lapack-lin

#
#  check-conformance: every level 1, 2 and 3 routine of the Fortran 77 and C
#  interfaces of configuration REFERENCE_CONFIG (index type int) compared
#  call by call with the reference BLAS and CBLAS of LAPACK 3.12.1, built
#  from the downloaded sources (see conformance/conformance.sh and
#  blas_conformance.md).  Not part of make check.
#
check-conformance : tool-ar tool-cc tool-fc-link
	@$(MAKE) lib-$(REFERENCE_CONFIG) lib-f77-$(REFERENCE_CONFIG)
	@CC=$(call shell_quote,$(CC)) CFLAGS=$(call shell_quote,$(CPPFLAGS) $(ULMBLAS_CFLAGS) $(CFLAGS)) \
	 FC=$(call shell_quote,$(FC)) FFLAGS=$(call shell_quote,$(FOPTFLAGS) $(FFLAGS)) \
	 CXX=$(call shell_quote,$(CXX) $(ULMBLAS_CXXFLAGS) $(CXXFLAGS)) \
	 LDFLAGS=$(call shell_quote,$(LDFLAGS)) LDLIBS=$(call shell_quote,$(LDLIBS)) \
	 FLIBS=$(call shell_quote,$(FLIBS)) \
	 $(SHELL) conformance/conformance.sh $(BUILD)/$(REFERENCE_CONFIG)/lib \
	          $(BUILD)/reference-lapack

clean :
	rm -rf $(BUILD)
