#
#   Makefile.fpc for Free Pascal Source Tree
#

[package]
name=fpc
version=1.0.8

[target]
dirs=compiler rtl utils fcl fv packages ide installer

[install]
fpcpackage=y

[default]
fpcdir=.
rule=help

[prerules]
# make versions < 3.77 (OS2 version) are buggy
ifndef inOS2
override FPCDIR:=$(BASEDIR)
export FPCDIR
endif

# New ppc386 (or ppc68k if on m68k machine !)
ifndef PPNEW
ifeq ($(CPU_TARGET),m68k)
PPSUF=68k
endif
ifeq ($(CPU_TARGET),i386)
PPSUF=386
endif
ifeq ($(CPU_TARGET),powerpc)
PPSUF=ppc
endif
ifeq ($(CPU_TARGET),alpha)
PPSUF=axp
endif

ifneq ($(OS_TARGET),$(OS_SOURCE))
PPPRE=ppcross
else
ifneq ($(CPU_TARGET),$(CPU_SOURCE))
PPPRE=ppcross
else
PPPRE=ppc
endif
endif

PPNEW=$(BASEDIR)/compiler/$(PPPRE)$(PPSUF)$(SRCEXEEXT)
endif

# Check if install/ subdir is available
ifneq ($(wildcard install),)
CVSINSTALL=install
else
CVSINSTALL=.
endif

# Install target
ifdef SNAPSHOT
INSTALLTARGET=install
else
INSTALLTARGET=distinstall
endif

# All target
ifdef SNAPSHOT
ALLTARGET=all
else
ifndef ALLTARGET
ifeq ($(OS_TARGET),win32)
ALLTARGET=smart
else
ifeq ($(OS_TARGET),go32v2)
ALLTARGET=smart
else
ifeq ($(OS_TARGET),linux)
ALLTARGET=smart
else
ifeq ($(OS_TARGET),freebsd)
ALLTARGET=smart
else
ifeq ($(OS_TARGET),netbsd)
ALLTARGET=smart
else
ifeq ($(OS_TARGET),openbsd)
ALLTARGET=smart
else
ALLTARGET=all
endif
endif
endif
endif
endif
endif
endif
endif

# Stop with an error?
ifdef SNAPSHOT
NOSTOP=-
else
NOSTOP=
endif

# Prefix for units
ifeq ($(OS_TARGET),linux)
PKGPRE=units
else
ifeq ($(OS_TARGET),freebsd)
PKGPRE=units
else
ifeq ($(OS_TARGET),netbsd)
PKGPRE=units
else
ifeq ($(OS_TARGET),openbsd)
PKGPRE=units
else
PKGPRE=u
endif
endif
endif
endif

# Always compile for release
override RELEASE=1
export RELEASE

# We want to have the resulting .zips in the current dir
ifndef DIST_DESTDIR
export DIST_DESTDIR:=$(BASEDIR)
endif

# Temporary path to pack a file
BASEPACKDIR=$(BASEDIR)/basepack

# Newly created fpcmake
FPCMAKENEW=$(BASEDIR)/utils/fpcm/fpcmake$(EXEEXT)

# Build/install options
CLEANOPTS=FPC=$(PPNEW)
BUILDOPTS=FPC=$(PPNEW) RELEASE=1
INSTALLOPTS=FPC=$(PPNEW) ZIPDESTDIR=$(BASEDIR) FPCMAKE=$(FPCMAKENEW)

# Compile also IDE (check for ide and fv dir)
ifneq ($(wildcard ide),)
ifneq ($(wildcard fv),)
ifeq ($(OS_TARGET),go32v2)
IDE=1
endif
ifeq ($(OS_TARGET),win32)
IDE=1
endif
ifeq ($(OS_TARGET),linux)
IDE=1
endif
# is now doable too.
ifeq ($(OS_TARGET),freebsd)
IDE=1
endif
ifeq ($(OS_TARGET),os2)
IDE=1
endif
ifeq ($(OS_TARGET),emx)
IDE=1
endif
endif
endif


[rules]
# These values can change
unexport FPC_VERSION OS_SOURCE

# Only process directories that really exists
override TARGET_DIRS:=$(wildcard $(TARGET_DIRS))

#####################################################################
# Main targets
#####################################################################

.PHONY: help checkfpcdir

help:
        @$(ECHO)
        @$(ECHO) Directory targets:
        @$(ECHO)
        @$(ECHO) $(TARGET_DIRS)
        @$(ECHO)
        @$(ECHO) Packing targets are:
        @$(ECHO)
        @$(ECHO) go32v2zip,win32zip,linuxzip,freebsdzip,sourcezip,os2zip,emxzip,beoszip,sunoszip,qnxzip,amigazip
        @$(ECHO)
        @exit


#####################################################################
# Dependencies
#####################################################################

#######################################
# Compiler
#######################################

.PHONY: compiler_cycle

compiler_cycle:
        $(MAKE) -C compiler cycle


#######################################
# Packages
#######################################

.PHONY: packages_base_all packages_extra_all \
        packages_base_smart packages_extra_smart

packages_base_all:
        $(MAKE) -C packages base_all

packages_base_smart:
        $(MAKE) -C packages base_smart

packages_extra_all:
        $(MAKE) -C packages extra_all

packages_extra_smart:
        $(MAKE) -C packages extra_smart


#######################################
# Install targets
#######################################

demo_install:
        $(MAKE) -C $(CVSINSTALL)/demo sourceinstall

man_install:
        $(MAKE) -C $(CVSINSTALL)/man installman

doc_install:
        $(MAKE) -C $(CVSINSTALL)/doc installdoc


#######################################
# Packaging targets
#######################################

optzips: optcompinstall optcompzip

optcompinstall:
        $(MAKE) compiler_cycle OPT=-dNEWOPTIMIZATIONS
        $(MAKE) compiler_install
        $(MAKE) rtl_install

optcompzip:
        $(MAKE) fpc_zipinstall PACKAGENAME=optcomp ZIPTARGET=optcompinstall


##########################################################################
# Packaging
##########################################################################

.PHONY: all clean distclean build install installbase zipinstall zipinstallbase zipinstallfcl \
        zipinstallpackages

all: build

clean: $(addsuffix _distclean,$(TARGET_DIRS))
        $(DEL) build-stamp.$(OS_TARGET)

distclean: clean

build: build-stamp.$(OS_TARGET)
build-stamp.$(OS_TARGET):
# create new compiler
        $(MAKE) compiler_cycle RELEASE=1
# clean
        $(MAKE) rtl_clean $(CLEANOPTS)
        $(MAKE) packages_clean $(CLEANOPTS)
        $(MAKE) fcl_clean $(CLEANOPTS)
        $(MAKE) utils_clean $(CLEANOPTS)
ifdef IDE
        $(MAKE) fv_clean $(CLEANOPTS)
        $(MAKE) ide_clean $(CLEANOPTS)
endif
# build everything
        $(MAKE) rtl_$(ALLTARGET) $(BUILDOPTS)
        $(MAKE) packages_base_$(ALLTARGET) $(BUILDOPTS)
        $(MAKE) fcl_$(ALLTARGET) $(BUILDOPTS)
        $(MAKE) packages_extra_$(ALLTARGET) $(BUILDOPTS)
        $(MAKE) utils_all $(BUILDOPTS)
ifdef IDE
        $(MAKE) fv_all $(BUILDOPTS)
        $(MAKE) ide_all $(BUILDOPTS)
        $(MAKE) installer_all $(BUILDOPTS)
endif
        $(ECHOREDIR) Build > build-stamp.$(OS_TARGET)

installbase: build-stamp.$(OS_TARGET)
# create dirs
        $(MKDIR) $(INSTALL_BASEDIR)
        $(MKDIR) $(INSTALL_DOCDIR)
        $(MKDIR) $(INSTALL_BINDIR)
ifndef SNAPSHOT
# readme & whatsnew and docs
        -$(COPY) $(CVSINSTALL)/doc/*.txt $(CVSINSTALL)/doc/copying* $(CVSINSTALL)/doc/faq.* $(INSTALL_DOCDIR)
# bingo32 (cwsdpmi,wmemu387.dxe)
ifeq ($(OS_TARGET),go32v2)
        $(COPY) $(CVSINSTALL)/bingo32/*.dxe $(INSTALL_BINDIR)
        $(COPY) $(CVSINSTALL)/bingo32/*.exe $(INSTALL_BINDIR)
endif
# binw32 (cygwin1.dll,*.ico)
ifeq ($(OS_TARGET),win32)
        $(COPY) $(CVSINSTALL)/binw32/*.ico $(INSTALL_BINDIR)
        $(COPY) $(CVSINSTALL)/binw32/*.dll $(INSTALL_BINDIR)
endif
# manpages for UNIX systems
ifeq ($(OS_TARGET),linux)
        $(MAKE) -C $(CVSINSTALL)/man installman
endif
ifeq ($(OS_TARGET),freebsd)
        $(MAKE) -C $(CVSINSTALL)/man installman
endif
ifeq ($(OS_TARGET),sunos)
        $(MAKE) -C $(CVSINSTALL)/man installman
endif
ifeq ($(OS_TARGET),qnx)
        $(MAKE) -C $(CVSINSTALL)/man installman
endif
endif
# install generated things
        $(MAKE) compiler_$(INSTALLTARGET) $(INSTALLOPTS)
        $(MAKE) rtl_$(INSTALLTARGET) $(INSTALLOPTS)

install: build-stamp.$(OS_TARGET)
        $(MAKE) installbase $(INSTALLOPTS)
        $(MAKE) packages_$(INSTALLTARGET) $(INSTALLOPTS)
        $(MAKE) fcl_$(INSTALLTARGET) $(INSTALLOPTS)
        $(MAKE) utils_$(INSTALLTARGET) $(INSTALLOPTS)
ifdef IDE
        $(NOSTOP)$(MAKE) ide_$(INSTALLTARGET) $(BUILDOPTS)
endif

zipinstall: build-stamp.$(OS_TARGET)
        $(MAKE) fpc_zipinstall ZIPTARGET=installbase ZIPNAME=base$(ZIPSUFFIX) $(INSTALLOPTS)
        $(MAKE) packages_zip$(INSTALLTARGET) $(INSTALLOPTS) ZIPPREFIX=$(PKGPRE)
        $(MAKE) fcl_zip$(INSTALLTARGET) $(INSTALLOPTS) ZIPPREFIX=$(PKGPRE)
        $(MAKE) utils_zip$(INSTALLTARGET) $(INSTALLOPTS) ZIPNAME=util$(ZIPSUFFIX)
ifdef IDE
        $(NOSTOP)$(MAKE) ide_zip$(INSTALLTARGET) $(INSTALLOPTS) ZIPNAME=ide$(ZIPSUFFIX)
endif


##########################################################################
# Docs
##########################################################################

.PHONY: docspdf docs docsrcinstall docsrc

# We don't want the package name fpc in the path
DOCSOURCEDIR=$(INSTALL_SOURCEDIR)/../docs

docspdf:
        $(MAKE) -C docs pdfinstall DOCINSTALLDIR=$(PACKDIR)

docs:
        $(MAKE) fpc_zipinstall ZIPTARGET=docspdf ZIPNAME=docs

docsrcinstall:
        $(MAKE) -C docs clean
        $(MKDIR) $(DOCSOURCEDIR)
        $(COPYTREE) docs/* $(DOCSOURCEDIR)

docsrc:
        $(MAKE) fpc_zipinstall ZIPTARGET=docsrcinstall ZIPNAME=docsrc


##########################################################################
# Demos
##########################################################################

.PHONY: demozip

# We don't want the package name fpc in the path
ifdef UNIXINSTALLDIR
DEMOSOURCEDIR=$(INSTALL_DOCDIR)/../demo
else
DEMOSOURCEDIR=$(INSTALL_PREFIX)/demo
endif

demosrcinstall:
        $(MAKE) -C demo clean
        $(MKDIR) $(DEMOSOURCEDIR)
        $(COPYTREE) demo/* $(DEMOSOURCEDIR)

demozip:
        $(MAKE) fpc_zipinstall ZIPTARGET=demosrcinstall ZIPNAME=demo


##########################################################################
# Source targets
##########################################################################

.PHONY: sourcebase sourcezip

# We don't want the fpc/ package prefix
INSTALL_BASESOURCEDIR=$(INSTALL_SOURCEDIR)/..

sourcebase:
# base Makefiles needed for sources
        $(MKDIR) $(INSTALL_BASESOURCEDIR)
        $(MKDIR) $(INSTALL_BASESOURCEDIR)/rtl
        $(MKDIR) $(INSTALL_BASESOURCEDIR)/packages
        $(MKDIR) $(INSTALL_BASESOURCEDIR)/packages/base
        $(MKDIR) $(INSTALL_BASESOURCEDIR)/packages/extra
        $(COPY) Makefile*  $(INSTALL_BASESOURCEDIR)
        $(COPY) rtl/Makefile* $(INSTALL_BASESOURCEDIR)/rtl
        $(COPY) packages/Makefile* $(INSTALL_BASESOURCEDIR)/packages
        $(COPY) packages/base/Makefile* $(INSTALL_BASESOURCEDIR)/packages/base
        $(COPY) packages/extra/Makefile* $(INSTALL_BASESOURCEDIR)/packages/extra

sourcezip:
        $(MAKE) fpc_zipinstall ZIPTARGET=sourcebase ZIPNAME=basesrc
        $(MAKE) compiler_zipsourceinstall
        $(MAKE) rtl_zipsourceinstall
        $(MAKE) packages_zipsourceinstall ZIPPREFIX=$(PKGPRE)
        $(MAKE) fcl_zipsourceinstall
        $(MAKE) utils_zipsourceinstall
        $(MAKE) ide_zipsourceinstall
        $(MAKE) installer_zipsourceinstall


##########################################################################
# OS targets
##########################################################################

.PHONY: go32v2 win32 linux freebsd os2 emx netbsd openbsd go32v2zip win32zip linuxzip freebsdzip os2zip emxzip netbsdzip openbsdzip

go32v2: checkfpcdir
        $(MAKE) install OS_TARGET=go32v2

win32: checkfpcdir
        $(MAKE) install OS_TARGET=win32

linux: checkfpcdir
        $(MAKE) install OS_TARGET=linux

freebsd: checkfpcdir
        $(MAKE) install OS_TARGET=freebsd

netbsd: checkfpcdir
        $(MAKE) install OS_TARGET=netbsd

openbsd: checkfpcdir
        $(MAKE) install OS_TARGET=openbsd

os2: checkfpcdir
        $(MAKE) install OS_TARGET=os2

emx: checkfpcdir
        $(MAKE) install OS_TARGET=emx

sunos: checkfpcdir
        $(MAKE) install OS_TARGET=sunos

go32v2zip: checkfpcdir
        $(MAKE) zipinstall OS_TARGET=go32v2

win32zip: checkfpcdir
        $(MAKE) zipinstall OS_TARGET=win32

linuxzip: checkfpcdir
        $(MAKE) zipinstall OS_TARGET=linux

freebsdzip : checkfpcdir
        $(MAKE) zipinstall OS_TARGET=freebsd

netbsdzip : checkfpcdir
        $(MAKE) zipinstall OS_TARGET=netbsd

openbsdzip : checkfpcdir
        $(MAKE) zipinstall OS_TARGET=openbsd

beoszip : checkfpcdir
        $(MAKE) zipinstall OS_TARGET=beos USEZIP=1

os2zip: checkfpcdir
        $(MAKE) zipinstall OS_TARGET=os2

emxzip: checkfpcdir
        $(MAKE) zipinstall OS_TARGET=emx

sunoszip: checkfpcdir
        $(MAKE) zipinstall OS_TARGET=sunos TARPROG=gtar

qnxzip: checkfpcdir
                $(MAKE) zipinstall OS_TARGET=qnx

amigazip: checkfpcdir
                $(MAKE) zipinstall OS_TARGET=amiga

##########################################################################
# Dos, OS/2 Short 8.3 Names
##########################################################################

.PHONY: renametoshort shortsrc shortbin shortnames

renametoshort:
# Compiler parts
        -mv compiler$(ZIPSUFFIX).zip comp$(ZIPSUFFIX).zip
        -mv utils$(ZIPSUFFIX).zip util$(ZIPSUFFIX).zip
        -mv installer$(ZIPSUFFIX).zip inst$(ZIPSUFFIX).zip
# Packages
        -mv ugdbint$(ZIPSUFFIX).zip ugdb$(ZIPSUFFIX).zip
        -mv uregexpr$(ZIPSUFFIX).zip ureg$(ZIPSUFFIX).zip
        -mv upaszlib$(ZIPSUFFIX).zip upzl$(ZIPSUFFIX).zip
        -mv uuncgi$(ZIPSUFFIX).zip ucgi$(ZIPSUFFIX).zip
        -mv uunzip$(ZIPSUFFIX).zip uzip$(ZIPSUFFIX).zip
        -mv uftpapi$(ZIPSUFFIX).zip uftp$(ZIPSUFFIX).zip
        -mv uhwvideo$(ZIPSUFFIX).zip uhwv$(ZIPSUFFIX).zip

shortsrc:
        $(MAKE) renametoshort ZIPSUFFIX=src

shortbin:
        $(MAKE) renametoshort

shortnames: shortsrc shortbin

##########################################################################
# Debian
##########################################################################

ifdef inUnix

ifneq ($(wildcard $(CVSINSTALL)/debian/changelog),)

.PHONY: debcopy deb

DEBPACKAGEVERSION:=$(shell head -n 1 $(CVSINSTALL)/debian/changelog | awk '{ print $$2 }' | tr -d '[()]')
DEBFPCVERSION:=$(shell echo $(DEBPACKAGEVERSION) | awk -F '-' '{ print $$1 }')
DEBSRCDIR:=/usr/src/fpc-$(DEBFPCVERSION)

debcopy: distclean
        rm -rf $(DEBSRCDIR)
        install -d $(DEBSRCDIR)
        $(COPYTREE) compiler $(DEBSRCDIR)
        $(COPYTREE) rtl $(DEBSRCDIR)
        $(COPYTREE) fcl $(DEBSRCDIR)
        $(COPYTREE) packages $(DEBSRCDIR)
        $(COPYTREE) utils $(DEBSRCDIR)
        $(COPYTREE) demo $(DEBSRCDIR)
        $(COPYTREE) logs $(DEBSRCDIR)
        $(COPYTREE) docs $(DEBSRCDIR)
        $(COPYTREE) Makefile* $(DEBSRCDIR)
        $(COPYTREE) $(CVSINSTALL)/debian $(DEBSRCDIR)
        $(COPYTREE) $(CVSINSTALL)/man $(DEBSRCDIR)
        $(COPYTREE) $(CVSINSTALL)/doc $(DEBSRCDIR)
        find $(DEBSRCDIR) -name 'CVS*' | xargs -n1 rm -rf
        chmod 755 $(DEBSRCDIR)/debian/rules

deb: checkfpcdir debcopy
        cd $(DEBSRCDIR) ; debian/rules binary

endif   # changelog found

endif


##########################################################################
# RPM
##########################################################################

ifdef inUnix

ifneq ($(wildcard $(CVSINSTALL)/fpc.spec),)

.PHONY: rpmcopy rpm

RPMFPCVERSION:=$(shell grep '^Version:' $(CVSINSTALL)/fpc.spec | awk '{ print $$2 }')

REDHATDIR=/usr/src/redhat
RPMSOURCESDIR:=$(REDHATDIR)/SOURCES
RPMSPECDIR:=$(REDHATDIR)/SPECS
RPMSRCDIR:=$(RPMSOURCESDIR)/fpc
rpmcopy: distclean
        install -d $(REDHATDIR)
        install -d $(RPMSPECDIR)
        install -d $(RPMSOURCESDIR)
# fpc.rpm
        rm -rf $(RPMSRCDIR)
        cp $(CVSINSTALL)/fpc.spec $(RPMSPECDIR)/fpc-$(RPMFPCVERSION).spec
        install -d $(RPMSRCDIR)
        $(COPYTREE) compiler $(RPMSRCDIR)
        $(COPYTREE) rtl $(RPMSRCDIR)
        $(COPYTREE) fcl $(RPMSRCDIR)
        $(COPYTREE) packages $(RPMSRCDIR)
        $(COPYTREE) utils $(RPMSRCDIR)
        $(COPYTREE) demo $(RPMSRCDIR)
        $(COPYTREE) logs $(RPMSRCDIR)
        $(COPYTREE) Makefile* $(RPMSRCDIR)
        $(COPYTREE) $(CVSINSTALL)/man $(RPMSRCDIR)
        $(COPYTREE) $(CVSINSTALL)/doc $(RPMSRCDIR)
        $(COPYTREE) docs $(RPMSRCDIR)
        find $(RPMSRCDIR) -name 'CVS*' | xargs -n1 rm -rf
        cd $(RPMSRCDIR) ; tar cvz * > $(RPMSOURCESDIR)/fpc-$(RPMFPCVERSION)-src.tar.gz

rpm: checkfpcdir rpmcopy
        cd $(RPMSPECDIR) ; rpm --nodeps -ba fpc-$(RPMFPCVERSION).spec

endif   # spec found

endif
