#
# Makefile.fpc for Free Pascal Tests directory
#

[install]
fpcpackage=y

[default]
fpcdir=..
rule=allexectests


[rules]
# Subdirs available in the test subdir
TESTSUBDIRS=cg 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

units : units/$(FPCMADE)
units/$(FPCMADE):
        $(MAKE) -C units

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

testcheck: units allpreps $(DOTEST)

#
# Dotest options
#

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

ifdef GRAPH
override DOTESTOPT+=-g
endif

ifdef INTERACTIVE
override DOTESTOPT+=-i
endif

#
# Test run targets
#

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

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

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

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

.PHONY: allpreps

allpreps: allpreps-stamp.$(OS_TARGET)
allpreps-stamp.$(OS_TARGET):
        $(COPY) test/cg/obj/$(OS_TARGET)/$(CPU_TARGET)/ctest.o test/cg
        $(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))
ifdef INUNIX
        -rm -f $(wildcard $(patsubst %.pp,%$(EXEEXT),$(wildcard $(addsuffix /t*.pp,$(DIRS)))))
else
        -rm -f $(addsuffix /*$(EXEEXT),$(DIRS))
endif
        -rm -f *.tmp core
        -rm -f $(LOG) $(LONGLOG) $(FAILLIST)
        -rm -f ppas.sh ppas.bat gmon.out allpreps-stamp.$(OS_TARGET)
        $(MAKE) -C units clean

distclean: clean
        -rm -f dotest$(EXEEXT) dotest$(OEXT) redir$(PPUEXT) redir$(OEXT)

#
# Main rules
#

.PHONY: all full

all : allexectests

full : clean allexectests

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
