#
#   Makefile.fpc for Free Pascal Compiler NEWCG
#

[dirs]
fpcdir=../..
unitdir=$(CPU_TARGET) . $(OLDDIR)
incdir=$(OLDDIR)
targetdir=.

[install]
packagename=compiler
sourcesubdirs=0

[require]
rtl=1
options=-Sg

[tools]
tooldiff=1
toolcmp=1
tooldate=1
toolupx=1


[presettings]
# Don't export OS_SOURCE because it can change after the first compile
unexport OS_SOURCE FPC_VERSION

# Allow ALPHA, POWERPC, M68K, I386 defines for target cpu
ifdef ALPHA
CPU_TARGET=alpha
endif
ifdef POWERPC
CPU_TARGET=powerpc
endif
ifdef M68K
CPU_TARGET=m68k
endif
ifdef I386
CPU_TARGET=i386
endif

# Where is the 'old' compiler located
OLDDIR=..

# RTL
UTILSDIR=$(OLDDIR)/../utils

# Utils used by compiler development/installation
COMPILERUTILSDIR=$(OLDDIR)/utils

# Default language for the compiler
ifndef FPCLANG
FPCLANG=e
endif

# Local defines for the compiler only
ifndef LOCALDEF
LOCALDEF=
endif

# Local options for the compiler only
ifndef LOCALOPT
LOCALOPT=$(OPT)
endif

# Options for the RTL only when cycling
ifndef RTLOPTS
RTLOPTS=$(OPT)
endif

# Message files
MSGFILES=$(wildcard $(OLDDIR)/error*.msg)

# ppcSUFFIX
ifeq ($(CPU_TARGET),i386)
CPUSUF=386
endif
ifeq ($(CPU_TARGET),alpha)
CPUSUF=axp
endif
ifeq ($(CPU_TARGET),m68k)
CPUSUF=68k
endif
ifeq ($(CPU_TARGET),powerpc)
CPUSUF=ppc
endif


[postsettings]
# Default message file
MSGFILE=$(OLDDIR)/error$(FPCLANG).msg

# set correct defines (-d$(CPU_TARGET) is automaticly added in makefile.fpc)
override LOCALDEF+=-dGDB -dBROWSERLOG -dNEWCG

# i386 specific
ifeq ($(CPU_TARGET),i386)
# also insert MMX support
override LOCALDEF+=-dSUPPORT_MMX
# We don't need the intel and binary writer on linux...
ifdef inlinux
override LOCALDEF+=-dNOAG386INT -dNOAG386BIN
endif
endif

override LOCALOPT+=$(LOCALDEF)

override FPCOPT+=$(LOCALOPT)


[rules]
#####################################################################
# Setup Targets
#####################################################################

ifeq ($(OS_TARGET),win32)
ifdef CMP
override DIFF:=$(CMP) -i138
endif
endif

# Used to avoid unnecessary steps in remake3
ifdef DIFF
ifdef OLDFPC
DIFFRESULT:=$(shell $(DIFF) $(OLDFPC) $(FPC))
else
DIFFRESULT=Not equal
endif
else
DIFFRESULT=No diff program
endif


#####################################################################
# Setup os-independent filenames
#####################################################################

FPCEXENAME=pp$(EXEEXT)
EXENAME=ppc$(CPUSUF)$(EXEEXT)
TEMPNAME=ppc$(EXEEXT)
TEMPNAME1=ppc1$(EXEEXT)
TEMPNAME2=ppc2$(EXEEXT)
TEMPNAME3=ppc3$(EXEEXT)
MAKEDEP=ppdep$(EXEEXT)
MSG2INC=msg2inc$(EXEEXT)


#####################################################################
# CPU targets
#####################################################################

alpha:
        $(MAKE) ALPHA=1 all

i386:
        $(MAKE) I386=1 all

m68k:
        $(MAKE) M68K=1 all

powerpc:
        $(MAKE) POWERPC=1 all


#####################################################################
# Default makefile
#####################################################################

all: $(EXENAME)
        $(MAKE) echotime

ifeq ($(MAKELEVEL),0)
ifndef STARTTIME
ifdef DATE
STARTTIME:=$(shell $(DATE) +%T)
else
STARTTIME:=unknown
endif
endif
endif

export STARTTIME

ifdef DATE
ENDTIME=$(shell $(DATE) +%T)
else
ENDTIME:=unknown
endif

echotime:
        @echo Start $(STARTTIME) now $(ENDTIME)

ifndef DIFFRESULT
next :
        @echo $(OLDFPC) and $(FPC) are equal
        $(COPY) $(FPC) $(EXENAME)
else
next :
        $(MAKE) execlean
        $(MAKE) -C $(UNITDIR_RTL) clean
        $(MAKE) -C $(UNITDIR_RTL) 'FPC=$(FPC)' 'OPT=$(RTLOPTS)' all
        $(MAKE) clean
        $(MAKE) all
endif

clean : execlean fpc_cleanall

ppuclean: fpc_cleanall

execlean :
        -$(DEL) ppc386$(EXEEXT) ppcaxp$(EXEEXT) ppc68k$(EXEEXT) ppcppc$(EXEEXT)

distclean: clean
        -$(DEL) $(TEMPNAME) $(TEMPNAME1) $(TEMPNAME2) $(TEMPNAME3) $(MSG2INC)


#####################################################################
# Include depencies
#####################################################################

$(MAKEDEP): $(UTILSDIR)/ppdep.pp
        $(COMPILER) $(UTILSDIR)/ppdep.pp
        $(COPY) $(UTILSDIR)/$(MAKEDEP) $(MAKEDEP)

dependencies : $(MAKEDEP)
        $(MAKEDEP) pp.pas $(FPCOPTDEF) $(LOCALDEF) '-F$$(COMPILER) $$(LOCALOPT)' > depend

ifdef USEDEPEND

include depend

endif


#####################################################################
# Make targets
#####################################################################

$(MSG2INC): $(COMPILERUTILSDIR)/msg2inc.pp
        $(COMPILER) -FE. $(COMPILERUTILSDIR)/msg2inc.pp

# The msgtxt.inc only depends on the error?.msg file, not on msg2inc,
# because that one will be new almost everytime
msgtxt.inc: $(MSGFILE)
        $(MAKE) $(MSG2INC)
        $(MSG2INC) $(MSGFILE) msg msg

msg: msgtxt.inc

# Make only the compiler
ifndef COMPLETE
$(EXENAME) : $(wildcard *.pas) $(wildcard *.inc) msg
        $(COMPILER) pp.pas
        $(EXECPPAS)
        $(MOVE) $(FPCEXENAME) $(EXENAME)
else
$(EXENAME) : $(wildcard *.pas) $(wildcard *.inc) msg
        $(COMPILER) pp.pas
        $(EXECPPAS)
        $(COMPILER) pp.pas
        $(EXECPPAS)
        $(COMPILER) pp.pas
        $(EXECPPAS)
        $(MOVE) $(FPCEXENAME) $(EXENAME)
endif

tokens.dat : $(wildcard *.pas) $(wildcard *.inc)
        $(COMPILER) tokendat.pas
        ./tokendat

# This target remakes the units with the currently made version
remake: $(EXENAME)
        $(MOVE) $(EXENAME) $(TEMPNAME)
        $(MAKE) execlean
        $(MAKE) -C $(UNITDIR_RTL) clean
        $(MAKE) clean
        $(MAKE) 'FPC=$(BASEDIR)/$(TEMPNAME)' all

remake3: $(TEMPNAME3)
        $(MAKE) 'FPC=$(BASEDIR)/$(TEMPNAME3)' 'OLDFPC=$(BASEDIR)/$(TEMPNAME2)' next
        $(DIFF) $(TEMPNAME3) $(EXENAME)

$(TEMPNAME1) : $(EXENAME)
        -$(DEL) $(TEMPNAME1)
        $(MOVE) $(EXENAME) $(TEMPNAME1)

$(TEMPNAME2) : $(TEMPNAME1)
        $(MAKE) 'FPC=$(BASEDIR)/$(TEMPNAME1)' 'OLDFPC=' next
        -$(DEL) $(TEMPNAME2)
        $(MOVE) $(EXENAME) $(TEMPNAME2)

$(TEMPNAME3) : $(TEMPNAME2)
        $(MAKE) 'FPC=$(BASEDIR)/$(TEMPNAME2)' 'OLDFPC=$(BASEDIR)/$(TEMPNAME1)' next
        -$(DEL) $(TEMPNAME3)
        $(MOVE) $(EXENAME) $(TEMPNAME3)

cycle:
        $(MAKE) clean
        $(MAKE) -C $(UNITDIR_RTL) clean
        $(MAKE) -C $(UNITDIR_RTL) 'OPT=$(RTLOPTS)' all
        $(MAKE) remake3
        $(MAKE) echotime

cycledep:
        $(MAKE) cycle USEDEPEND=1

cvstest:
        $(MAKE) cycle 'LOCALOPT=-n -Se' 'RTLOPTS=-n -Se'


#####################################################################
# Installation
#####################################################################

.PHONY: quickinstall install installsym

MSGINSTALLDIR=$(BASEINSTALLDIR)/msg

# This will only install the ppc386.exe, not the message files etc.
quickinstall:
ifdef inlinux
        $(MKDIR) $(BASEINSTALLDIR)
        $(INSTALLEXE) $(EXENAME) $(BASEINSTALLDIR)
else
        $(MKDIR) $(BININSTALLDIR)
ifdef UPXPROG
        -$(UPXPROG) $(EXENAME)
endif
        $(INSTALLEXE) $(EXENAME) $(BININSTALLDIR)
endif

install: quickinstall
ifdef inlinux
        $(INSTALLEXE) $(COMPILERUTILSDIR)/samplecfg $(BASEINSTALLDIR)/samplecfg
endif
        $(MKDIR) $(MSGINSTALLDIR)
        $(INSTALL) $(MSGFILES) $(MSGINSTALLDIR)

# this also installs the link /usr/bin/ppc386. The .deb does that later
installsymlink: install
ifdef inlinux
        $(MKDIR) $(BININSTALLDIR)
        ln -sf $(BASEINSTALLDIR)/ppc386 $(BININSTALLDIR)/ppc386
endif


#####################################################################
# Misc
#####################################################################

.PHONY: rtl rtlclean rtlinstall

rtl:
        $(MAKE) -C $(UNITDIR_RTL) all

rtlclean:
        $(MAKE) -C $(UNITDIR_RTL) clean

rtlinstall:
        $(MAKE) -C $(UNITDIR_RTL) install

#####################################################################
# local user configurable file
# in makefile.loc you can add any desired target
#####################################################################

localmake:=$(strip $(wildcard makefile.loc))

ifdef localmake
include ./$(localmake)
endif
