# The list of test is stored in a separate file, so that it can be shared with
# the Windows makefile, win.mak.

origdir = @srcdir@

vpath %.c $(origdir)
vpath %.sh $(origdir)

include $(origdir)/tests

# The included file defined variable TESTBINS, which is a list of program
# names in format exe/<testname>-test. Extract the base test names from it.
TESTNAMES = $(patsubst exe/%-test,%, $(TESTBINS))

# Set by autoconf
LDFLAGS = @LDFLAGS@
CFLAGS = @CFLAGS@
CPPFLAGS = @CPPFLAGS@
ODBC_CONFIG = @ODBC_CONFIG@
PROVE = @PROVE@

LD_LIBRARY_PATH := $(origdir)/../../mppdb_temp_install/lib:$(LD_LIBRARY_PATH)

LIBODBC := $(shell $(ODBC_CONFIG) --libs)

all: $(TESTBINS) runsuite reset-db

odbc.ini:
	$(origdir)/odbcini-gen.sh $(odbc_ini_extras)

installcheck: all reset-db runsuite
	$(origdir)/odbcini-gen.sh $(odbc_ini_extras)
	rm -f regression.diffs
	@if test ! -d results; then mkdir -p results; fi
	ODBCSYSINI=. ODBCINSTINI=./odbcinst.ini ODBCINI=./odbc.ini ./reset-db < $(origdir)/sampletables.sql
# If the perl 'prove' test running utility is available, use it. It produces
# nice output. But otherwise just run the plain suite, it's usable as is too.
ifdef PROVE
	prove -e ./runsuite $(TESTNAMES) :: --inputdir=$(origdir)
else
	./runsuite $(TESTNAMES) --inputdir=$(origdir)
endif

override CFLAGS += -Wno-pointer-sign $(CFLAGS_ADD)

runsuite: runsuite.c

reset-db: reset-db.c exe/common.o
	$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) $^ -o $@ $(LIBODBC)

exe/common.o: src/common.c
	@if test ! -d exe; then mkdir -p exe; fi
	$(COMPILE.c) -c $< -o $@

$(origdir)/src/wchar-char-test.c: $(wildcard $(origdir)/src/wchar-char-test-*.c)
	@touch -c $@

# For each test, compile the .c file.
exe/%-test: src/%-test.c exe/common.o
	$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) $^ -o exe/$*-test $(LIBODBC)

# This target runs the regression tests with all combinations of
# UseDeclareFetch, UseServerSidePrepare and Protocol options.
installcheck-all:
	rm -f odbc.ini odbcinst.ini
	$(MAKE) installcheck odbc_ini_extras="UseDeclareFetch=0 UseServerSidePrepare=1 Protocol=7.4-2"
	rm -f odbc.ini odbcinst.ini
	$(MAKE) installcheck odbc_ini_extras="UseDeclareFetch=0 UseServerSidePrepare=1 Protocol=7.4-1"
	rm -f odbc.ini odbcinst.ini
	$(MAKE) installcheck odbc_ini_extras="UseDeclareFetch=0 UseServerSidePrepare=1 Protocol=7.4-0"

	rm -f odbc.ini odbcinst.ini
	$(MAKE) installcheck odbc_ini_extras="UseDeclareFetch=0 UseServerSidePrepare=0 Protocol=7.4-2"
	rm -f odbc.ini odbcinst.ini
	$(MAKE) installcheck odbc_ini_extras="UseDeclareFetch=0 UseServerSidePrepare=0 Protocol=7.4-1"
	rm -f odbc.ini odbcinst.ini
	$(MAKE) installcheck odbc_ini_extras="UseDeclareFetch=0 UseServerSidePrepare=0 Protocol=7.4-0"

	rm -f odbc.ini odbcinst.ini
	$(MAKE) installcheck odbc_ini_extras="UseDeclareFetch=1 UseServerSidePrepare=1 Protocol=7.4-2"
	rm -f odbc.ini odbcinst.ini
	$(MAKE) installcheck odbc_ini_extras="UseDeclareFetch=1 UseServerSidePrepare=1 Protocol=7.4-1"
	rm -f odbc.ini odbcinst.ini
	$(MAKE) installcheck odbc_ini_extras="UseDeclareFetch=1 UseServerSidePrepare=1 Protocol=7.4-0"

	rm -f odbc.ini odbcinst.ini
	$(MAKE) installcheck odbc_ini_extras="UseDeclareFetch=1 UseServerSidePrepare=0 Protocol=7.4-2"
	rm -f odbc.ini odbcinst.ini
	$(MAKE) installcheck odbc_ini_extras="UseDeclareFetch=1 UseServerSidePrepare=0 Protocol=7.4-1"
	rm -f odbc.ini odbcinst.ini
	$(MAKE) installcheck odbc_ini_extras="UseDeclareFetch=1 UseServerSidePrepare=0 Protocol=7.4-0"

clean:
	rm -f $(TESTBINS) exe/*.o runsuite reset-db
	rm -f results/*
