#!/usr/bin/make -f
# debian/rules for Free Pascal

DEB_PACKAGE_NAME = fpc
export DEB_BUILD_MAINT_OPTIONS := hardening=+bindnow
export DH_ALWAYS_EXCLUDE := COPYING:LICENSE
export LANG:=C

# Documentation type to use pdf/html
ifeq (${DEBDOCTYPE},)
DOCTYPE=html
# HTML Converter l2h/4ht/hevea or html for (default)
CONVERTER=hevea
else
DOCTYPE=${DEBDOCTYPE}
CONVERTER=${DOCTYPE}
endif
# Include standard dpkg-buildpackage variables
include /usr/share/dpkg/architecture.mk
# Undefine FPC if it was set
unexport FPC
# Build new compiler name
CPU_SOURCE=${DEB_BUILD_ARCH_CPU}
CPU_TARGET=${DEB_HOST_ARCH_CPU}
# Take care that Debian uses amd64 while FPC perfers x86_64 instead
CPU_SOURCE := $(subst amd64,x86_64,${CPU_SOURCE})
CPU_TARGET := $(subst amd64,x86_64,${CPU_TARGET})
# Take care that Debian uses arm64 while FPC perfers aarch64 instead
CPU_SOURCE := $(subst arm64,aarch64,${CPU_SOURCE})
CPU_TARGET := $(subst arm64,aarch64,${CPU_TARGET})
# Take care that debian uses ppc64/-el while FPC prefers powerpc64 instead
ifeq ($(DEB_HOST_ARCH_CPU),ppc64el)
CPU_SOURCE := $(subst ppc64el,powerpc64,${CPU_SOURCE})
CPU_TARGET := $(subst ppc64el,powerpc64,${CPU_TARGET})
else
CPU_SOURCE := $(subst ppc64,powerpc64,${CPU_SOURCE})
CPU_TARGET := $(subst ppc64,powerpc64,${CPU_TARGET})
endif
# Shuld get rid of this one day, as it is done automatically  by FPC make files
ifeq ($(CPU_TARGET),m68k)
PPSUF=68k
endif
ifeq ($(CPU_TARGET),i386)
PPSUF=386
endif
ifeq ($(CPU_TARGET),x86_64)
PPSUF=x64
endif
ifeq ($(CPU_TARGET),powerpc)
PPSUF=ppc
endif
ifeq ($(CPU_TARGET),powerpc64)
PPSUF=ppc64
endif
ifeq ($(CPU_TARGET),alpha)
PPSUF=axp
endif
ifeq ($(CPU_TARGET),arm)
PPSUF=arm
endif
ifeq ($(CPU_TARGET),aarch64)
PPSUF=a64
endif
ifeq ($(CPU_TARGET),sparc)
PPSUF=sparc
endif
ifeq ($(CPU_TARGET),sparc64)
PPSUF=sparc64
endif
ifeq ($(CPU_TARGET),mips)
PPSUF=mips
endif
ifeq ($(CPU_TARGET),mipsel)
PPSUF=mipsel
endif

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

PPNEW=$(PPPRE)$(PPSUF)
# Take care that Debian uses kfreebsd while FPC prefers freebsd
OS_SOURCE=${DEB_BUILD_ARCH_OS}
OS_TARGET=win64
# FPC uses FPCTARGET to identify the target for which the binaries are
# produced. This is generally a couple of target CPU and target OS names
# separated by a dash.
FPCTARGET=$(CPU_TARGET)-$(OS_TARGET)
# Get version information from changelog file
DEB_VERSION:=$(shell dpkg-parsechangelog --show-field Version)
DEB_DATE:=$(shell date --utc --date="`dpkg-parsechangelog --show-field Date`" +%Y/%m/%d)
DEB_UPSTREAM_VERSION:=$(shell echo $(DEB_VERSION) | cut -f 1 -d -)
export DEB_UPSTREAM_MAIN_VERSION:=$(shell echo ${DEB_UPSTREAM_VERSION} | sed -e 's/^\([0-9\.]*\).*/\1/')
DEB_BUILD=$(lastword $(subst -, ,${DEB_VERSION}))
ifndef PACKAGESUFFIX
export PACKAGESUFFIX=-${DEB_UPSTREAM_MAIN_VERSION}
endif
# Get directories
INSTALL_DIR=${CURDIR}/debian/tmp
BIN_DIR=${INSTALL_DIR}/usr/bin
# Debian policy is to install libraries that are multi-arch coinstallable in
# /usr/lib/${DEB_HOST_MULTIARCH}
LIB_DIR=/usr/lib/${DEB_HOST_MULTIARCH}/${DEB_PACKAGE_NAME}/${DEB_UPSTREAM_MAIN_VERSION}
DOC_DIR=${INSTALL_DIR}/usr/share/doc
SRC_DIR=/usr/share/fpcsrc/${DEB_UPSTREAM_MAIN_VERSION}
EXAMPLE_TEMP=${INSTALL_DIR}/usr/share/doc/fpc-${DEB_UPSTREAM_MAIN_VERSION}
STY_DIR=${INSTALL_DIR}/usr/share/texmf/tex/latex/fpc-${DEB_UPSTREAM_MAIN_VERSION}
# Override standard GNUE make variables
ifndef MKDIR
MKDIR=mkdir -p
endif
ifndef CP
CP=cp -Rfpl
endif
ifndef MV
MV=mv
endif
RM:=rm -rf
# Undefine FPCDIR if it was set
unexport FPCDIR
NEWPP=$(CURDIR)/fpcsrc/compiler/$(PPNEW)
NEWFPDOC=$(CURDIR)/fpcsrc/utils/fpdoc/fpdoc
# Set default compilation options
DEB_BUILD_FPC_OPT=debian/deb-build-fpc.cfg
DEB_HOST_FPC_OPT=debian/deb-host-fpc.cfg
ifeq ($(DEB_HOST_ARCH_CPU),ppc64el)
BUILDOPTS=PP=$(NEWPP) OPT='@${CURDIR}/${DEB_HOST_FPC_OPT} -Cb- -Caelfv2'
else
BUILDOPTS=OPT='@${CURDIR}/${DEB_HOST_FPC_OPT}'
endif
ifdef CROSSOPT
BUILDOPTS+= CROSSOPT=${CROSSOPT}
endif
INSTALLOPTS=INSTALL_PREFIX=$(INSTALL_DIR)/usr PP=$(NEWPP)

export GDBLIBDIR=/usr/lib

ifeq ($(CPU_TARGET),m68k)
  # Disable optimization for the first runs as there may be bugs associated
  # with that, preventing proper building new versions
  CYCLEOPTS:=OPT='@${CURDIR}/${DEB_HOST_FPC_OPT}' OPTLEVEL1=-O-
else
  CYCLEOPTS:=OPT='@${CURDIR}/${DEB_HOST_FPC_OPT}'
endif

BUILDOPTS+=OS_TARGET=${OS_TARGET}
INSTALLOPTS+=OS_TARGET=${OS_TARGET}

LDFLAGS=$(strip $(shell DEB_BUILD_MAINT_OPTIONS=$(DEB_BUILD_MAINT_OPTIONS) \
	dpkg-buildflags --get LDFLAGS | \
	sed -e 's/-Wl,//g' -e 's/,/ /g' -e 's1-specs=/usr/share/dpkg/.*\.specs11' -e 's/-ffat-lto-objects//'))

# Until we switch to shorthand dh, we need to set this ourselves for
# reproducible builds
export SOURCE_DATE_EPOCH = $(shell date -d "$$(dpkg-parsechangelog -SDate)" +%s)

#export DH_VERBOSE=1

###################
# Clean
#

clean:
	dh_clean
	${RM} fpcsrc
	${RM} ${DEB_BUILD_FPC_OPT} ${DEB_HOST_FPC_OPT}

FPCMAKEFILES=
makefiles: makefiles-stamp
makefiles-stamp:
	# Regenerate make files using fpcmake.
	for ff in fpcsrc/*/Makefile.fpc ; do fpcmake -r -Tall -q $${ff} ; done
	# Make files are now generated.
	touch $@

###################
# Debian files
#

debian-files: debian-files-stamp
debian-files-stamp:debian/fixdeb debian/*.in
	@echo "--- Creating/fixing *.install files"
	DEB_SUBST_PACKAGESUFFIX=${PACKAGESUFFIX} \
	DEB_SUBST_PPCBIN=${PPNEW} \
	DEB_SUBST_TARGET=${FPCTARGET} \
	DEB_SUBST_DEB_HOST_MULTIARCH=${DEB_HOST_MULTIARCH} \
	$< debian/*.in
	# Because we use dh-exec to track units that don't need to install
	# everywhere, we need the <package>.install files to be executable
	chmod a+x debian/*.install
	touch debian-files-stamp


###################
# Source
#

prepare-source: prepare-source-stamp
prepare-source-stamp:
	dh_testdir
	dh_prep
	# Crete sources directory
	${MKDIR} fpcsrc
	# Copy sources into it
	cp -at fpcsrc/ ${SRC_DIR}/*
	cp -at fpcsrc/compiler/ ${LIB_DIR}/msg
	tar -vaxf missing-sources.tar.xz

	# Checksum in fastcgi/fpunits.cfg & fcl-web/fpunits.cfg
	# Note: revert in clean target
	sed --in-place "s/Now/EncodeDate($(shell date --utc --date=${DEB_DATE} +'%Y, %m, %d'))/" fpcsrc/packages/fpmkunit/src/fpmkunit.pp
	touch prepare-source-stamp


###################
# Arch packages
#

build-arch: debian-files prepare-source makefiles build-arch-stamp
build-arch-stamp: ${DEB_BUILD_FPC_OPT} ${DEB_HOST_FPC_OPT}
	@echo "--- Building"
	dh_testdir
	# First make a new Compiler using a make cycle
	$(MAKE) -C fpcsrc/compiler $(CYCLEOPTS)
	# Then build RTL using new compiler
	$(MAKE) -C fpcsrc/rtl clean all $(BUILDOPTS)
	# Finally build packages and tools using new compiler and new RTL
	$(MAKE) -C fpcsrc/packages all ${BUILDOPTS}
	# *.fpm files contain the build directory, which results in unreproducible
	# builds. As this is useless anyways, let's canonize the text.
	find . -name "*.fpm" -exec sed --in-place "s#${CURDIR}/##" {} \;
	touch build-arch-stamp

install-arch: build-arch install-arch-stamp
install-arch-stamp:
	@echo "--- Installing"
	dh_testdir
	dh_testroot
	dh_installdirs
	# create all necessary directories
	${MKDIR} ${BIN_DIR}
	${MKDIR} ${STY_DIR}
	# Specify the compiler to use so installing will use the correct versioned dir
	#$(MAKE) -C fpcsrc compiler_distinstall $(INSTALLOPTS)
	$(MAKE) -C fpcsrc/rtl distinstall $(INSTALLOPTS)
	$(MAKE) -C fpcsrc/packages distinstall $(INSTALLOPTS)
	${CP} -t  ${BIN_DIR} debian/fpc-depends
	${RM} ${EXAMPLE_TEMP}/ide
	# Copy examples to the correct doc dir
	/bin/bash debian/moveexamples ${EXAMPLE_TEMP} ${DOC_DIR} ${DEB_UPSTREAM_MAIN_VERSION}
	touch install-arch-stamp

arrange-arch: install-arch arrange-arch-stamp
arrange-arch-stamp:
	dh_testdir
	dh_testroot
	# Expected here:
	# usr/lib/fpc/lexyacc/yyparse.cod
	# usr/lib/fpc/lexyacc/yylex.cod
	# usr/bin/unihelper-<version>
	# usr/bin/ptop.rsj-<version>
	# usr/bin/mkx86ins-<version>
	# usr/bin/mkinsad-<version>
	# usr/bin/mkarmins-<version>
	# usr/bin/mka64ins-<version>
	# usr/bin/makeskel.rsj-<version>
	# usr/bin/cldrparser-<version>
	dh_install -a --list-missing
	touch arrange-arch-stamp

# This is a policy required target
build-indep: debian-files

###################
# Generic
#

build: build-arch
install: install-arch
binary: binary-arch


###################
# Deb building
#

binary-indep: install-indep
	@echo "--- Building: arch-indep packages"
	dh_testdir
	dh_testroot
	dh_installdocs -i -X.in
	dh_installchangelogs -i
	dh_install -i --list-missing
	dh_lintian -i
	dh_compress -i -X.pdf
	dh_fixperms
	dh_installdebconf -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i $(DEB_DH_BUILDDEB_ARGS)

binary-arch: arrange-arch
	@echo "--- Building: arch packages"
	dh_testdir
	dh_testroot
	dh_link
	dh_installdocs -a -X.in
	dh_installchangelogs -a
	dh_installexamples -a
	dh_strip -a
	dh_lintian -a
	dh_compress -a
	dh_fixperms
	dh_installdebconf -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a $(DEB_DH_BUILDDEB_ARGS)

.PHONY: arrange-arch binary binary-arch build build-arch \
	build-indep check-makefiles clean \
	debian-files install install-arch \
	makefiles prepare-source gen-control run-tests \

# Generate debian.control file
gen-control: debian/control.in

# Run upstream test suite
run-tests:
	dpkg-source --before-build ${CURDIR}
	debian/tests/run-upstream-testsuite

debian/%:debian/fixdeb debian/changelog debian/%.in
	DEB_SUBST_PACKAGESUFFIX=${PACKAGESUFFIX} \
	DEB_SUBST_PPCBIN=${PPNEW} \
	DEB_SUBST_TARGET=${FPCTARGET} \
	DEB_SUBST_DEB_HOST_MULTIARCH=${DEB_HOST_MULTIARCH} \
	$< --gen-control $@.in

${DEB_BUILD_FPC_OPT}:
	echo '# FPC configuration file for build system tools' > $@
	echo '-k${LDFLAGS}' >> $@
	echo '-Fl/usr/lib/${DEB_BUILD_MULTIARCH}' >> $@

${DEB_HOST_FPC_OPT}:
	echo '# FPC configuration file for host system applications' > $@
	echo '-k${LDFLAGS}' >> $@
	echo '-Fl/usr/lib/${DEB_HOST_MULTIARCH}' >> $@

lintian:
	lintian -I -i ../fpc_${DEB_VERSION}_${DEB_HOST_ARCH}.changes
