#
#  Makefile.fpc for Free Pascal units, which participates in the tests
#

[target]

[install]
fpcpackage=y

[clean]
units = erroru win32err ptest

[default]
fpcdir=../..

[rules]

# Only 1.1 has Classes in the RTL
ifneq ($(findstring 1.0.,$(FPC_VERSION)),)
FCL=fcl
endif

.PHONY: rtl $(FCL) extra all clean cleanrtlfcl clean_ctest

#############################
# RTL and FCL
#

DUMMYINSTALLDIR=$(BASEDIR)/tmp
INSTALLOPT=INSTALL_PREFIX=$(DUMMYINSTALLDIR) INSTALL_UNITDIR=$(BASEDIR)

ifndef USEUNITDIR

rtl-stamp:
        $(MAKE) -C ../../rtl all "OPT=$(OPT) -n"
        $(MAKE) -C ../../rtl install $(INSTALLOPT)
        $(ECHO) Compiled > rtl-stamp

ifdef FCL
fcl-stamp:
        $(MAKE) -C ../../packages/base all "OPT=$(OPT) -n"
        $(MAKE) -C ../../fcl all "OPT=$(OPT) -n"
        $(MAKE) -C ../../fcl install $(INSTALLOPT)
        $(ECHO) Compiled > fcl-stamp
endif

cleanrtlfcl : cleanall
#        $(MAKE) -C ../../rtl clean
#        $(MAKE) -C ../../fcl clean
        $(DELTREE) $(DUMMYINSTALLDIR)
        -$(DEL) rtl-stamp  fcl-stamp

else

rtl-stamp:
#       Just copying everything doesn't work, because then the Makefile will be overwritten
        -$(COPY) $(USEUNITDIR)/*$(PPUEXT) .
        -$(COPY) $(USEUNITDIR)/*$(OEXT) .
        -$(COPY) $(USEUNITDIR)/*$(ASMEXT) .
        -$(COPY) $(USEUNITDIR)/*_ppas$(BATCHEXT) .
        $(ECHO) Copied > rtl-stamp

ifdef FCL
fcl-stamp: rtl-stamp
        $(ECHO) Copied > fcl-stamp
endif

cleanrtlfcl :
        -$(DEL) *$(PPUEXT)
        -$(DEL) *$(OEXT)
        -$(DEL) *$(ASMEXT)
        -$(DEL) *_ppas$(BATCHEXT)
        -$(DEL) rtl-stamp fcl-stamp

endif


rtl : rtl-stamp

ifdef FCL
fcl : fcl-stamp
endif

#############################
# Extra units
#


TESTOPT=-n -FE. -Fu. -T$(OS_TARGET) $(OPT)
ifneq ($(BINUTILSPREFIX),)
override TESTOPT+=-XP$(BINUTILSPREFIX) -Xc
endif
TESTCOMPILER=$(FPC) $(TESTOPT)

erroru$(PPUEXT) : erroru.pp
        $(TESTCOMPILER) erroru.pp

ptest$(PPUEXT) : ../test/cg/ptest.pp
        $(TESTCOMPILER) ../test/cg/ptest.pp

ifeq ($(OS_TARGET),win32)
win32err$(PPUEXT) : win32err.pp
        $(TESTCOMPILER) win32err.pp
endif

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


extra : erroru$(PPUEXT) ptest$(PPUEXT) ../test/cg/obj/$(OS_TARGET)/$(CPU_TARGET)/ctest.o

ifeq ($(OS_TARGET),win32)
extra : win32err$(PPUEXT)
endif


#############################
# Main rules
#

all : rtl $(FCL) extra

clean : cleanrtlfcl fpc_clean

clean_ctest :
        -$(DEL) ../test/cg/obj/$(OS_TARGET)/$(CPU_TARGET)/ctest.o
