#
# Makefile.fpc for Free Pascal Tests directory
#

[install]
fpcpackage=y

[default]
fpcdir=..
rule=allexectests

[require]
packages_win32=mysql
packages_linux=mysql

[rules]
# Subdirs available in the test subdir
TESTSUBDIRS=cg cg/cdecl units/system units/dos units/crt units/objects

# All full dirnames in the test/ dir including the subdir self
TESTDIRS:=test $(addprefix test/,$(TESTSUBDIRS))

# Unix like OS ?
ifeq ($(OS_TARGET),linux)
INUNIX=1
endif
ifeq ($(OS_TARGET),freebsd)
INUNIX=1
endif

# For linux by default no graph tests
ifdef INUNIX
NOGRAPH=1
endif


#
# Tools
#

ifndef FAILLIST
export FAILLIST:=faillist
endif

ifndef LONGLOG
export LONGLOG:=longlog
endif

ifndef LOG
export LOG:=log
endif

ifndef CCOMPILER
CCOMPILER:=$(strip $(wildcard $(addsuffix /gcc$(SRCEXEEXT),$(SEARCHPATH))))
ifneq ($(CCOMPILER),)
CCOMPILER:=$(firstword $(CCOMPILER))
endif
endif

units : units/$(FPCMADE)
units/$(FPCMADE):
        $(MAKE) -C units
ifeq ($(OS_TARGET),win32)
        $(FPC) -n -Fuunits -FEunits utils/win32err.pp
endif

DOTEST=./dotest$(EXEEXT)
$(DOTEST) : units utils/dotest.pp utils/redir.pp utils/teststr.pp
        $(FPC) -n -Fuunits -FE. utils/dotest.pp
        $(FPC) -n -Fuunits -FE. utils/fail.pp
        $(FPC) -n -Fuunits -FE. utils/testfail.pp

ifneq ($(wildcard ./testfail$(SRCEXEEXT)),)
TESTCOMSPECRES:=$(shell ./testfail$(SRCEXEEXT))
endif

ifneq ($(TESTCOMSPECRES),)
NOCOMSPEC=1
endif

#
# Digest program
#
ifeq ($(USESQL),YES)
DIGEST=./dbdigest$(EXEEXT)
else
DIGEST=./digest$(EXEEXT)
endif

./digest$(EXEEXT) : units utils/digest.pp utils/teststr.pp utils/testu.pp
        $(FPC) -n -Fuunits -FE. utils/digest.pp

./dbdigest$(EXEEXT) : units utils/dbdigest.pp utils/teststr.pp utils/testu.pp utils/dbtests.pp
        $(FPC) -n -Fuunits -Fu../packages/base/mysql -FE. utils/dbdigest.pp

testcheck: units allpreps $(DOTEST)

#
# Dotest options
#

ifneq ($(FPC),ppc386$(EXEEXT))
ifeq ($(findstring -c$(FPC),$(DOTESTOPT)),)
override DOTESTOPT+=-c$(FPC)
endif
endif

ifdef GRAPH
override DOTESTOPT+=-g
endif

ifdef INTERACTIVE
override DOTESTOPT+=-i
endif

ifdef DOALL
override DOTESTOPT+=-a
endif

ifdef NOCOMSPEC
override DOTESTOPT+=-x
endif


#
# Test run targets
#

DIRS=webtbs webtbf tbs tbf test test/cg test/units/system test/units/dos test/units/objects

%.log : %.pp $(DOTEST)
        $(DOTEST) $(DOTESTOPT) $<

%.elg : %.pp $(DOTEST)
        $(DOTEST) $(DOTESTOPT) -e $<

#
# Preparations for tests:
#
# 1. Copy target dependent ctest.o to test/cg
#

.PHONY: allpreps


ifdef CCOMPILER
test/cg/obj/$(OS_TARGET)/$(CPU_TARGET)/ctest.o : test/cg/obj/ctest.c
        -$(CCOMPILER) -c -o test/cg/obj/$(OS_TARGET)/$(CPU_TARGET)/ctest.o test/cg/obj/ctest.c
endif

allpreps: allpreps-stamp.$(OS_TARGET)
allpreps-stamp.$(OS_TARGET):
        $(FPC) -FE. -Fuunits test/cg/ptest.pp
        $(COPY) test/cg/obj/$(OS_TARGET)/$(CPU_TARGET)/ctest.o test/cg
        $(COPY) test/units/system/test*.txt .
        $(ECHO) $(DATE) > allpreps-stamp.$(OS_TARGET)

#
# Compile tests
#

.PHONY: alltbs alltbf allwebtbs allwebtbf alltest alltests

alltbs : testcheck $(patsubst %.pp,%.log,$(wildcard tbs/t*.pp))
alltbf : testcheck $(patsubst %.pp,%.log,$(wildcard tbf/t*.pp))

allwebtbs : testcheck $(patsubst %.pp,%.log,$(wildcard webtbs/t*.pp))
allwebtbf : testcheck $(patsubst %.pp,%.log,$(wildcard webtbf/t*.pp))

alltest : testcheck $(patsubst %.pp,%.log,$(wildcard $(addsuffix /t*.pp,$(TESTDIRS))))

alltests: alltest alltbs alltbf allwebtbs allwebtbf

#
# Compile and Run tests
#

.PHONY: allexectbs allexectbf allexecwebtbs allexecwebtbf allexectest allexectests

allexectbs : testcheck $(patsubst %.pp,%.elg,$(wildcard tbs/t*.pp))
allexectbf : testcheck $(patsubst %.pp,%.elg,$(wildcard tbf/t*.pp))

allexecwebtbs : testcheck $(patsubst %.pp,%.elg,$(wildcard webtbs/t*.pp))
allexecwebtbf : testcheck $(patsubst %.pp,%.elg,$(wildcard webtbf/t*.pp))

allexectest : testcheck $(patsubst %.pp,%.elg,$(wildcard $(addsuffix /t*.pp,$(TESTDIRS))))

allexectests: allexectest allexectbs allexectbf allexecwebtbs allexecwebtbf

#
# Clean
#

.PHONY: clean distclean

clean:
        -rm -f $(addsuffix /*$(PPUEXT),$(DIRS))
        -rm -f $(addsuffix /*$(OEXT),$(DIRS))
        -rm -f $(addsuffix /*.rst,$(DIRS))
        -rm -f $(addsuffix /*$(SHAREDLIBEXT),$(DIRS))
        -rm -f $(addsuffix /*.log,$(DIRS))
        -rm -f $(addsuffix /*.elg,$(DIRS))
ifeq ($(EXEEXT),)
        -rm -f $(wildcard $(patsubst %.pp,%$(EXEEXT),$(wildcard $(addsuffix /t*.pp,$(DIRS)))))
else
        -rm -f $(addsuffix /*$(EXEEXT),$(DIRS))
endif
        -rm -f test*.txt *.tmp *$(SHAREDLIBEXT) *$(OEXT) *$(PPUEXT) core
        -rm -f $(LOG) $(LONGLOG) $(FAILLIST)
        -rm -f ptest.* ppas.sh ppas.bat gmon.out allpreps-stamp.$(OS_TARGET)
        $(MAKE) -C units clean

distclean: clean
        -rm -f dotest$(EXEEXT) digest$(EXEEXT)

#
# Main rules
#

.PHONY: all full rundigest dailytest onlyknown onlygraph onlyinteractive

rundigest : $(DIGEST)
        -$(DIGEST)

all : allexectests

full : clean allexectests rundigest

onlyknown :
        $(MAKE) full "DOTESTOPT= $(DOTESTOPT) -k-"

onlygraph :
        $(MAKE) full "DOTESTOPT= $(DOTESTOPT) -g-"

onlyinteractive :
        $(MAKE) "DOTESTOPT= $(DOTESTOPT) -i-"

info :
        @echo This Makefile allows to test the compiler
        @echo
        @echo Targets:
        @echo "  all   - continue all tests"
        @echo "  full  - clean and run all tests"
        @echo "  dailytest - run full and save results"
        @echo "              in files having the date as extension"
        @echo "  onlyknown - run only known bugs"
        @echo "  onlygraph - run only graph tests"
        @echo "  onlyinteractive - run only interactive tests"
	@echo Done using FPC=$(FPC)
ifdef COMSPEC
	@echo And using COMSPEC=$(COMSPEC)
endif

override DATESUFFIX:=$(shell $(DATE) +%Y.%m.%d)

ifneq ($(wildcard log.$(DATESUFFIX)),)
override DATESUFFIX=$(shell $(DATE) +%Y.%m.%d.%H.%M)
endif

ifneq ($(wildcard lastdate.txt),)
LASTDATESUFFIX:=$(shell cat lastdate.txt)
endif

dailytest : full
        $(COPY) faillist faillist.$(DATESUFFIX)
        $(COPY) log log.$(DATESUFFIX)
        $(COPY) longlog longlog.$(DATESUFFIX)
        -$(DIGEST) > digest.$(DATESUFFIX)
ifdef LASTDATESUFFIX
        -diff -u log.$(LASTDATESUFFIX) log.$(DATESUFFIX) > difflog.$(DATESUFFIX)
        -diff -u digest.$(LASTDATESUFFIX) digest.$(DATESUFFIX) > diffdigest.$(DATESUFFIX)
        -diff -u faillist.$(LASTDATESUFFIX) faillist.$(DATESUFFIX) > difflist.$(DATESUFFIX)
endif
        @echo $(DATESUFFIX) > lastdate.txt

