#
#   Makefile.fpc for Free Pascal Compiler
#

[package]
name=fpcompiler
version=1.1

[target]
dirs=utils

[compiler]
targetdir=.
unitdir=$(PPC_TARGET) targets
includedir=$(PPC_TARGET)

[require]
packages=rtl
tools=diff cmp

[install]
fpcpackage=y

[default]
fpcdir=..


[prerules]
# Don't export version it can change after the first compile
unexport FPC_VERSION

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

# Default is to generate a compiler for the same
# platform as CPU_TARGET (a native compiler)
ifndef PPC_TARGET
PPC_TARGET=$(CPU_TARGET)
endif
export PPC_TARGET

# Default is to generate a compiler for the same
# target as OS_TARGET (a native compiler)
ifndef PPC_OS
PPC_OS=$(OS_TARGET)
endif
export PPC_OS

# 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 msg/error*.msg)

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

# Do not define the default -d$(CPU_TARGET) because that
# will conflict with our -d$(PPC_TARGET)
NOCPUDEF=1

# 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

# Default message file
MSGFILE=msg/error$(FPCLANG).msg

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

# i386 specific
ifeq ($(PPC_TARGET),i386)
override LOCALDEF+=-dSUPPORT_MMX
endif

# m68k specific
ifeq ($(PPC_TARGET),m68k)
override LOCALDEF+=-dNoAg68kMot -dNoAg68kMpw
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
#####################################################################

ifndef EXENAME
EXENAME=ppc$(CPUSUF)$(EXEEXT)
endif
PPEXENAME=pp$(EXEEXT)
TEMPNAME=ppc$(SRCEXEEXT)
PPCROSSNAME=ppcross$(CPUSUF)$(SRCEXEEXT)
TEMPNAME1=ppc1$(EXEEXT)
TEMPNAME2=ppc2$(EXEEXT)
TEMPNAME3=ppc3$(EXEEXT)
MAKEDEP=ppdep$(EXEEXT)
MSG2INC=./msg2inc$(EXEEXT)


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

.PHONY: alpha i386 m68k powerpc

alpha:
        $(MAKE) ALPHA=1 all

i386:
        $(MAKE) I386=1 all

m68k:
        $(MAKE) M68K=1 all

powerpc:
        $(MAKE) POWERPC=1 all


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

.PHONY: all compiler echotime ppuclean execlean clean distclean

all: $(EXENAME) $(addsuffix _all,$(TARGET_DIRS))

compiler: $(EXENAME)

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)

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

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

clean : execlean fpc_cleanall $(addsuffix _clean,$(TARGET_DIRS))

distclean: execlean fpc_cleanall $(addsuffix _distclean,$(TARGET_DIRS))
        -$(DEL) $(PPCROSSNAME) $(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 \
             $(wildcard targets/*.pas) $(wilcard targets/*.inc) \
             $(wildcard $(PPC_TARGET)/*.pas) $(wildcard $(PPC_TARGET)/*.inc)
        $(COMPILER) pp.pas
        $(EXECPPAS)
        $(MOVE) $(PPEXENAME) $(EXENAME)
else
$(EXENAME) : $(wildcard *.pas) $(wildcard *.inc) msg \
             $(wildcard targets/*.pas) $(wilcard targets/*.inc) \
             $(wildcard $(PPC_TARGET)/*.pas) $(wildcard $(PPC_TARGET)/*.inc)
        $(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

#####################################################################
# Cycle targets
#
# 1. Source CPU = Target CPU  and  Source OS = Target OS
#    Normal cycle
# 2. Source CPU = Target CPU  and  Source OS <> Target OS
#    First source native compiler
#    Second target native compiler
#
# 3. Source CPU <> Target CPU
#    First source native compiler
#    Second cross compiler
#    Third target native compiler
#
#####################################################################

ifeq ($(CPU_SOURCE),$(PPC_TARGET))

ifeq ($(OS_SOURCE),$(OS_TARGET))

##########################
# Normal cycle
#

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

$(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)

# 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)


cycle:
        $(MAKE) clean
        $(MAKE) -C $(UNITDIR_RTL) clean
        $(MAKE) -C $(UNITDIR_RTL) 'OPT=$(RTLOPTS)' all
        $(MAKE) remake3
        $(MAKE) $(addsuffix _all,$(TARGET_DIRS)) 'FPC=$(BASEDIR)/$(EXENAME)'
        $(MAKE) echotime

else

##########################
# Cross Target cycle
#

cycle:
# ppc (source native)
        $(MAKE) OS_TARGET=$(OS_SOURCE) rtlclean rtl
        $(MAKE) OS_TARGET=$(OS_SOURCE) EXENAME=$(TEMPNAME) clean compiler
# ppcross<ARCH> (source native)
        $(MAKE) 'FPC=$(BASEDIR)/$(TEMPNAME)' OS_TARGET=$(OS_SOURCE) rtlclean rtl
        $(MAKE) 'FPC=$(BASEDIR)/$(TEMPNAME)' OS_TARGET=$(OS_SOURCE) EXENAME=$(PPCROSSNAME) clean compiler
# ppc<ARCH> (target native)
        $(MAKE) 'FPC=$(BASEDIR)/$(PPCROSSNAME)' rtlclean rtl
        $(MAKE) 'FPC=$(BASEDIR)/$(PPCROSSNAME)' clean compiler

endif

else

##########################
# Cross CPU cycle
#
# ppc1 = native
# ppc2 = cross running on this platform
# ppc3/ppcXXX = native
#

cycle:
# ppc (source native)
        $(MAKE) OS_TARGET=$(OS_SOURCE) CPU_TARGET=$(CPU_SOURCE) rtlclean rtl
        $(MAKE) OS_TARGET=$(OS_SOURCE) CPU_TARGET=$(CPU_SOURCE) EXENAME=$(TEMPNAME) clean compiler
# ppcross<ARCH> (source native)
        $(MAKE) 'FPC=$(BASEDIR)/$(TEMPNAME)' OS_TARGET=$(OS_SOURCE) CPU_TARGET=$(CPU_SOURCE) rtlclean rtl
        $(MAKE) 'FPC=$(BASEDIR)/$(TEMPNAME)' OS_TARGET=$(OS_SOURCE) CPU_TARGET=$(CPU_SOURCE) PPC_TARGET=$(CPU_TARGET) EXENAME=$(PPCROSSNAME) clean compiler
# ppc<ARCH> (target native)
        $(MAKE) 'FPC=$(BASEDIR)/$(PPCROSSNAME)' rtlclean rtl
        $(MAKE) 'FPC=$(BASEDIR)/$(PPCROSSNAME)' clean compiler

endif

cycledep:
        $(MAKE) cycle USEDEPEND=1

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


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

.PHONY: quickinstall install installsym

MSGINSTALLDIR=$(INSTALL_BASEDIR)/msg
override PPEXEFILE:=$(wildcard $(EXENAME))

# This will only install the ppc386.exe, not the message files etc.
quickinstall: $(addsuffix _install,$(TARGET_DIRS))
# Install ppc386.exe
ifneq ($(PPEXEFILE),)
ifdef UPXPROG
        -$(UPXPROG) $(EXENAME)
endif
ifdef UNIXINSTALLDIR
        $(MKDIR) $(INSTALL_BASEDIR)
        $(INSTALLEXE) $(EXENAME) $(INSTALL_BASEDIR)
else
        $(MKDIR) $(INSTALL_BINDIR)
        $(INSTALLEXE) $(EXENAME) $(INSTALL_BINDIR)
endif
endif

install: quickinstall
ifdef UNIXINSTALLDIR
        $(MKDIR) $(INSTALL_BASEDIR)
        $(INSTALLEXE) $(COMPILERUTILSDIR)/samplecfg $(INSTALL_BASEDIR)/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) $(INSTALL_BINDIR)
        ln -sf $(INSTALL_BASEDIR)/ppc386 $(INSTALL_BINDIR)/ppc386
endif


#####################################################################
# RTL
#####################################################################

.PHONY: rtl rtlclean rtlinstall

rtl:
        $(MAKE) -C $(UNITDIR_RTL) 'OPT=$(RTLOPTS)' 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
