#
#   Makefile.fpc for Free Pascal Compiler
#

[dirs]
fpcdir=..
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

# RTL
UTILSDIR=../utils

# Utils used by compiler development/installation
COMPILERUTILSDIR=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 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

# Define Unix also for Linux
ifeq ($(OS_TARGET),linux)
ifneq ($(findstring 1.0.,$(FPC_VERSION)),)
override LOCALDEF+=-dUNIX
endif
endif

ifeq ($(OS_TARGET),freebsd)
ifneq ($(findstring 1.0.,$(FPC_VERSION)),)
override LOCALDEF+=-dUNIX
endif
endif


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

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

# i386 specific
ifeq ($(CPU_TARGET),i386)
# also insert MMX support
override LOCALDEF+=-dSUPPORT_MMX
endif

override LOCALOPT+=$(LOCALDEF)

override FPCOPT:=$(LOCALOPT)


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

ifeq ($(OS_TARGET),win32)
ifdef CMP
override DIFF:=$(CMP) -i138
endif
# force try to smartlink for windows unit
override COMPILER+=-XX
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
#####################################################################

FPCEXE=fpc$(EXEEXT)
PPEXENAME=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)

fpcexe: $(FPCEXE)

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)'
        $(MAKE) clean
        $(MAKE) $(EXENAME)
        $(MAKE) echotime
endif

clean : execlean fpc_cleanall

ppuclean:
        -$(DEL) *$(OEXT) *$(PPUEXT) *$(RSTEXT) *$(ASMEXT) *$(STATICLIBEXT) *$(SHAREDLIBEXT) *$(PPLEXT)

execlean :
        -$(DEL) fpc$(EXEEXT) 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) $(PPEXENAME) $(EXENAME)
else
$(EXENAME) : $(wildcard *.pas) $(wildcard *.inc) msg
        $(COMPILER) pp.pas
        $(EXECPPAS)
        $(COMPILER) pp.pas
        $(EXECPPAS)
        $(COMPILER) pp.pas
        $(EXECPPAS)
        $(MOVE) $(PPEXENAME) $(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
override FPCEXEFILE:=$(wildcard $(FPCEXE))
override PPEXEFILE:=$(wildcard $(EXENAME))

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

install: quickinstall
ifdef UNIXINSTALLDIR
        $(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 UNIXINSTALLDIR
        $(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
