#
#   $Id: Makefile,v 1.1.2.2 1998/09/16 16:12:43 peter Exp $
#   This file is part of the Free Pascal run time library.
#   Copyright (c) 1993-98 by the Free Pascal Development Team
#
#   Makefile for the Free Pascal Compiler
#
#   See the file COPYING.FPC, included in this distribution,
#   for details about the copyright.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#

#####################################################################
# Configuration section
#####################################################################

ifndef LANGUAGE
LANGUAGE=e
endif

ifndef CPU
CPU=i386
endif

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

# Local options for the compiler only
ifndef LOCALOPT
LOCALOPT=
endif

# Options for the RTL only when cycling
ifndef RTLOPTS
RTLOPTS=
endif


#####################################################################
# Defaults
#####################################################################

# Where is the makefile.cfg ?
CFG=../rtl/cfg

# At least set -Sg
LOCALOPT+=-Sg

# set correct defines (-d$(CPU) is automaticly added in makefile.cfg)
LOCALDEF+=-dGDB -dFPC

# for i386 also insert MMX support
ifdef i386
LOCALDEF+=-dSUPPORT_MMX
endif

LOCALOPT+=$(LOCALDEF)


#####################################################################
# Include default makefile
#####################################################################

include $(CFG)/makefile.cfg


#####################################################################
# Setup Targets
#####################################################################

MSGFILE=error$(LANGUAGE).msg

DIFFEXIST:=$(shell $(DIFF) --help)

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


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

PPEXENAME=pp$(EXEEXT)
EXENAME=ppc386$(EXEEXT)
M68KEXENAME=ppc68k$(EXEEXT)
TEMPNAME=ppc$(EXEEXT)
TEMPNAME1=ppc1$(EXEEXT)
TEMPNAME2=ppc2$(EXEEXT)
TEMPNAME3=ppc3$(EXEEXT)
MAKEDEP=mkdep$(EXEEXT)
MSG2INC=msg2inc$(EXEEXT)


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

.SUFFIXES: .pas $(EXEEXT) .ppu .dif .d3p .d3i .d3m .new

.PHONY : all clean info \
	 cycle remake remake3 \
	 install next \
	 diff diff3 patch replacediff3 restorediff3 \
	 test rtl rtlclean \

# also call ppas if with command option -s
ifeq (,$(findstring -s ,$(COMPILER)))
EXECPPAS=
else
EXECPPAS=@$(PPAS)
endif

.pas$(PPUEXT):
	$(COMPILER) $(LOCALOPT) $<
	$(EXECPPAS)

.pas$(EXEEXT):
	$(COMPILER) $(LOCALOPT) $<
	$(EXECPPAS)


all : $(EXENAME)
#	@echo Start $(STARTTIME) now $(ENDTIME)
# does not work because $(ENDTIME) is expanded
# before execution !!
	make echotime

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

export STARTTIME

ENDTIME=$(shell $(DATE) +%T)

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

ifndef DIFFRESULT
next :
	@echo $(OLDPP) and $(PP) are equal
	$(COPY) $(PP) $(EXENAME)
else
next :
	make execlean
	make -C $(UNITDIR) libsclean
	make -C $(UNITDIR) 'PP=$(COMPILERDIR)/$(PP)' 'OPT=$(RTLOPTS)' all
	make clean
	make all
endif


clean :
	-$(DEL) *$(OEXT) *$(PPUEXT) *$(ASMEXT) *$(STATICLIBEXT) $(EXENAME)
	-$(DELTREE) $(SMARTEXT)

execlean :
	-$(DEL) $(EXENAME)

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


#####################################################################
# Include depencies (linux only)
#####################################################################

ifdef inlinux
$(MAKEDEP) : $(RTLDIR)/utils/mkdep.pp
	$(COMPILER) $(RTLDIR)/utils/mkdep.pp
	$(COPY) $(RTLDIR)/utils/$(MAKEDEP) $(MAKEDEP)

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

include depend

endif


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

$(MSG2INC): $(RTLDIR)/utils/msg2inc.pp
	$(COMPILER) $(RTLDIR)/utils/msg2inc.pp
	$(COPY) $(RTLDIR)/utils/$(MSG2INC) $(MSG2INC)

msgtxt.inc: $(MSGFILE) $(MSG2INC)
	$(RTLDIR)/utils/$(MSG2INC) $(MSGFILE) msg msg

msg: msgtxt.inc

# Make only the compiler
ifdef inlinux
$(EXENAME) : $(PPEXENAME)
	$(EXECPPAS)
	$(MOVE) $(PPEXENAME) $(EXENAME)
else
$(EXENAME) : $(PASFILES) $(INCFILES) $(MSGFILES)
	$(COMPILER) $(LOCALOPT) pp.pas
	$(EXECPPAS)
	$(MOVE) $(PPEXENAME) $(EXENAME)
endif

# This target remakes the units with the currently made version
remake: $(EXENAME)
	$(MOVE) $(EXENAME) $(TEMPNAME)
	make execlean
	make -C $(UNITDIR) libsclean
	make -C $(UNITDIR) 'PP=$(COMPILERDIR)/$(TEMPNAME)' 'OPT=$(RTLOPTS)' all
	make clean
	make 'PP=./$(TEMPNAME)' all

remake3: $(TEMPNAME3)
	make 'PP=./$(TEMPNAME3)' 'OLDPP=./$(TEMPNAME2)' next
	$(DIFF) $(TEMPNAME3) $(EXENAME)

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

$(TEMPNAME2) : $(TEMPNAME1)
	make 'PP=./$(TEMPNAME1)' 'OLDPP=' next
	-$(DEL) $(TEMPNAME2)
	$(MOVE) $(EXENAME) $(TEMPNAME2)

$(TEMPNAME3) : $(TEMPNAME2)
	make 'PP=./$(TEMPNAME2)' 'OLDPP=./$(TEMPNAME1)' next
	-$(DEL) $(TEMPNAME3)
	$(MOVE) $(EXENAME) $(TEMPNAME3)

cycle:
	make clean
	make -C $(UNITDIR) libsclean
	make -C $(UNITDIR)
	make remake3
	make echotime


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

install:
ifdef inlinux
	umask 022
	strip ppc386
	$(MKDIR) $(LIBINSTALLDIR)
	$(INSTALLEXE) ppc386 $(LIBINSTALLDIR)
	ln -sf $(LIBINSTALLDIR)/ppc386 $(BININSTALLDIR)/ppc386
	-makecfg $(LIBINSTALLDIR)/samplecfg $(UNITINSTALLDIR) $(MSGINSTALLDIR) $(GCCLIBPATH)
else
	$(MKDIR) $(BININSTALLDIR)
	$(INSTALLEXE) ppc386$(EXEEXT) $(BININSTALLDIR)
endif
	$(MKDIR) $(MSGINSTALLDIR)
	$(INSTALL) errore.msg errorn.msg $(MSGINSTALLDIR)


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

rtl :
	make -C $(UNITDIR) all

rtlclean :
	make -C $(UNITDIR) libsclean

# just a quick way to get ppc68k
$(M68KEXENAME):
	make clean
	$(PP) -uI386 -uSUPPORT_MMX -dm68k -o$(M68KEXENAME) pp
	make clean

ansirtl:
	make -C $(UNITDIR) libsclean
	make -C $(UNITDIR) PP=$(COMPILERDIR)/ppc386 OPT='-g -dUSeAnsiStrings' all

test:
	@echo time is $(STARTTIME) date.exe is $(DATE)


#####################################################################
# Default targets
#####################################################################

include $(CFG)/makefile.def

#
# $Log: Makefile,v $
# Revision 1.1.2.2  1998/09/16 16:12:43  peter
#   * uses makefile.cfg
#
# Revision 1.1.2.1  1998/09/11 12:07:51  pierre
#   + added execlean to be able to keep last *.s files
#
# Revision 1.1	1998/09/10 13:53:54  peter
#   * msg2inc compile works
#
# Revision 1.31  1998/09/09 18:26:25  michael
# Spaces to tabs...
#
# Revision 1.30  1998/09/09 18:17:15  florian
#   * version number changed to 0.99.8
#
# Revision 1.29  1998/09/02 08:51:30  pierre
#   + added -s support must be defined as LOCALOPT
#     because it does not work for rtl building
#   + added some compilation time info
#
# Revision 1.28  1998/08/29 13:52:30  peter
#   + new messagefile
#   * merged optione.msg into errore.msg
#
# Revision 1.27  1998/08/21 15:16:56  peter
#   * win32 compiles a bit better, no growheap crash
#
# Revision 1.26  1998/08/21 14:08:46  pierre
#   + TEST_FUNCRET now default (old code removed)
#     works also for m68k (at least compiles)
#
# Revision 1.25  1998/08/18 09:24:41  pierre
#   * small warning position bug fixed
#   * support_mmx switches splitting was missing
#   * rhide error and warning output corrected
#
# Revision 1.24  1998/08/14 09:29:53  michael
# Updated compiler number
#
# Revision 1.23  1998/08/06 10:42:56  pierre
#   + faster remake3 :
#     stops when two executables are equal
#
# Revision 1.22  1998/07/23 18:17:58  michael
# + Added ansirtl target to make an ansistrings capable rtl
#
# Revision 1.21  1998/07/22 21:33:32  michael
# + COPY and Replace back to mv and cp
#
# Revision 1.20  1998/06/24 14:02:58  peter
#   * new depend for the new ra units
#
# Revision 1.19  1998/06/23 14:00:15  peter
#   * renamed RA* units
#
# Revision 1.18  1998/06/10 10:42:44  peter
#   * ifndef TARGET to allow target on commandline
#
# Revision 1.17  1998/06/08 09:22:02  michael
# fixed dependcies and rules. Make would not run any more after peters changes.
#
# Revision 1.16  1998/06/05 14:37:28  pierre
#   * fixes for inline for operators
#   * inline procedure more correctly restricted
#
# Revision 1.15  1998/06/03 09:33:39  michael
# added distclean target to remove ppc1-ppc3 too
#
# Revision 1.14  1998/06/03 09:27:51  michael
# Clean cannot remove ppc1-ppc3 : make cycle uses clean
#
# Revision 1.13  1998/06/02 11:29:36  peter
#   + make clean removes also ppc1 - ppc3
#
# Revision 1.12  1998/05/31 14:09:45  peter
#   * use mv -f instead of move /y whch is not dos6.2 compatible
#
# Revision 1.11  1998/05/06 14:03:27  michael
# + Make install doesn't stop if makecfg not found
#
# Revision 1.10  1998/04/23 13:21:46  michael
# Updated version number
#
# Revision 1.9	1998/04/15 14:16:48  michael
# + Removed CR characters
#
# Revision 1.8	1998/04/06 12:28:23  pierre
#   Test of makefile log !
#
# End of log
