#!/usr/bin/make -f

# debian/rules for kernel-patch-powerpc

# written by Jens Schmalzing <jensen@debian.org>

# debhelper compatibility version
export DH_COMPAT=4

# the flavours we're building for
flavours := $(shell grep -v ^\# debian/flavours)

# cut the upstream version and the Debian revision from the changelog
kernel	:= $(shell head -1 debian/changelog | sed 's/^.*(\(.*\)-.*).*$$/\1/')
debian	:= $(shell head -1 debian/changelog | sed 's/^.*(.*-\(.*\)).*$$/\1/')
version	:= $(shell head -1 debian/changelog | sed 's/^.*(\(.*\)).*$$/\1/')

# how to call ourselves recursively
RECURSION = $(MAKE) -f debian/rules

#some files in debian/ get version numbers and stuff updated on the fly
M4 = m4 -DM4VERSION="$(version)" -DM4KERNEL="$(kernel)" -DM4FLAVOUR="$(flavour)"
debian/%: debian/%.m4
	$(M4) $< > $@

# installation of files and programs
INSTALL_DATA	= install -m 644 -o root -g root
INSTALL_EXEC	= install -m 755 -o root -g root

# build locations - point TMP to a tmpfs for best performance
TMP ?= $(CURDIR)/tmp
KSOURCE	 = $(TMP)/kernel-source-$(kernel)
KFLAVOUR = $(TMP)/kernel-source-$(kernel)-$(flavour)
KDUMMY   = $(TMP)/kernel-image-$(kernel)-$(flavour)_$(version)_powerpc.deb
KHEADERS = $(TMP)/kernel-headers-$(kernel)_$(version)_powerpc.deb

# install locations
KPATCH    = $(CURDIR)/debian/kernel-patch-powerpc-$(kernel)/usr/src/kernel-patches/powerpc/$(kernel)
KBUILD    = $(CURDIR)/debian/kernel-build-$(kernel)-$(flavour)
KBUILDDOC = $(KBUILD)/usr/share/doc/kernel-build-$(kernel)-$(flavour)
KSRC      = $(KBUILD)/usr/src/kernel-build-$(kernel)-$(flavour)
KIMAGE    = $(CURDIR)/debian/kernel-image-$(kernel)-$(flavour)
KIMAGEDOC = $(KIMAGE)/usr/share/doc/kernel-image-$(kernel)-$(flavour)
KLIB      = $(KIMAGE)/usr/lib/kernel-image-$(kernel)-$(flavour)

# environment for make-kpkg
export APPEND_TO_VERSION=-$(flavour)
export CONCURRENCY_LEVEL=3
export INITRD=Yes
export INITRD_OK=WeKnowWhatWeAreDoing
export KPKG_SUBARCH=pmac

# the configure target is responsible for setting up the build arena,
# i.e. unpacking the Debian kernel source, applying the PowerPC patch,
# and preparing a separate build tree for each flavour.

configure: stamp-configure

# the following is the first of a number of loop implementations using
# recursive make.  This provides much easier access to the loop
# variable than extracting it from the target.  In order to safeguard
# against infinite loops, the recursion level is checked for the
# critical targets.  This particular example calls a common target,
# then runs the loop, and also includes some common commands to
# process after the loop.

stamp-configure: \
stamp-configure-prepare \
$(foreach flavour,$(flavours),stamp-configure-flavour-$(flavour))

	cd $(KSOURCE); APPEND_TO_VERSION='' make-kpkg configure

	touch $@

# unpack the Debian kernel source, apply the PowerPC patch, add a
# default .config file and the necessary Debian files (changelog,
# control, copyright, and official)
stamp-configure-prepare:

	test -d $(dir $(KSOURCE)) || mkdir -p $(dir $(KSOURCE))
	tar jxCf $(dir $(KSOURCE)) /usr/src/kernel-source-$(kernel).tar.bz2
	cd $(KSOURCE); /usr/src/kernel-patches/all/$(kernel)/apply/debian
	for file in $(shell debian/list patches); do patch -d $(KSOURCE) -p1 < patches/$$file; done

	$(MAKE) -s -C config default > $(KSOURCE)/.config

	test -d $(KSOURCE)/debian || mkdir $(KSOURCE)/debian
	cd debian; cp -p changelog control copyright $(KSOURCE)/debian
	> $(KSOURCE)/debian/official

	touch $@

ifeq (0,$(MAKELEVEL))

stamp-configure-flavour-%:
	$(RECURSION) stamp-configure-flavour-$* flavour=$*

else
ifeq (1,$(MAKELEVEL))

# create a separate build tree for a given flavour, add the flavour's
# .config and control files
stamp-configure-flavour-$(flavour): stamp-configure-prepare

	test -d $(dir $(KFLAVOUR)) || mkdir -p $(dir $(KFLAVOUR))

	cp -al $(KSOURCE) $(KFLAVOUR)

	rm -f $(KFLAVOUR)/.config $(KFLAVOUR)/debian/control
	$(MAKE) -s -C config $(flavour) > $(KFLAVOUR)/.config
	$(M4) debian/control-dummy.m4 > $(KFLAVOUR)/debian/control

	touch $@

endif
endif

# the build target is responsible for, well, building the kernels and
# modules for each flavour.  To that end, it uses the build target of
# make-kpkg.

ifeq ($(DEB_HOST_ARCH),powerpc)
build: stamp-build
else
build:
endif

stamp-build: \
stamp-build-prepare \
$(foreach flavour,$(flavours),stamp-build-flavour-$(flavour))
	touch $@

stamp-build-prepare: stamp-configure
	dh_testdir
	touch $@

ifeq (0,$(MAKELEVEL))

stamp-build-flavour-%:
	$(RECURSION) stamp-build-flavour-$* flavour=$*

else
ifeq (1,$(MAKELEVEL))

# build the pmac kernel and the modules for a given flavour
stamp-build-flavour-$(flavour):
	cd $(KFLAVOUR); make-kpkg build
	touch $@

endif
endif

# the install target is responsible for assigning the built kernels
# and modules to the right kernel-image and kernel-modules packages.
# The main work is done through the kernel-image target of make-kpkg.
# The resulting intermediate package is immediately unpacked again and
# its contents re-arranged slightly.  The whole process results in one
# kernel-image directory per flavour.  Also, the install target puts
# together the patch package itself and the build infrastructure for
# modules.

install: stamp-install

stamp-install: \
stamp-build \
stamp-install-prepare \
$(foreach flavour,$(flavours),stamp-install-flavour-$(flavour))
	touch $@

# do the usual checks, install the patch itself and the accompanying scripts
stamp-install-prepare: debian/dirs debian/apply
	dh_testdir
	dh_testroot
	dh_clean
	dh_installdirs

	$(INSTALL_EXEC) debian/apply $(KPATCH)/apply/powerpc
	$(INSTALL_EXEC) debian/unpatch $(KPATCH)/unpatch/powerpc
	$(INSTALL_EXEC) debian/list $(KPATCH)/patches
	$(INSTALL_DATA) patches/* $(KPATCH)/patches
	bzip2 $(KPATCH)/patches/*.diff

	touch $@

ifeq (0,$(MAKELEVEL))

stamp-install-flavour-%:
	$(RECURSION) stamp-install-flavour-$* flavour=$*

else
ifeq (1,$(MAKELEVEL))

# create an intermediate kernel-image package from the build tree,
# immediately unpack it into the install directory, and re-arrange a
# few things.  Also, install the build infrastructure for modules from
# the kernel source.

stamp-install-flavour-$(flavour):

# create and unpack the intermediate kernel image package
	cd $(KFLAVOUR); make-kpkg kernel-image
	dpkg -x $(KDUMMY) $(KIMAGE)

# remove the coff image
	rm -f $(KIMAGE)/boot/vmlinux.coff-$(kernel)-$(flavour)

# add objects, libraries, and utilities from the kernel tree
	mkdir -p $(addprefix $(KLIB)/,obj/openfirmware obj/simple lib boot utils)
	$(INSTALL_DATA) $(KFLAVOUR)/arch/ppc/boot/openfirmware/*.o $(KLIB)/obj/openfirmware
	$(INSTALL_DATA) $(KFLAVOUR)/arch/ppc/boot/simple/*.o $(KLIB)/obj/simple
	rm -f $(KLIB)/obj/openfirmware/image*.o $(KLIB)/obj/simple/image*.o
	$(INSTALL_DATA) $(KFLAVOUR)/lib/lib.a $(KLIB)/lib
	$(INSTALL_DATA) $(KFLAVOUR)/arch/ppc/boot/lib/lib.a $(KLIB)/lib/ppc.a
	$(INSTALL_DATA) $(KFLAVOUR)/arch/ppc/boot/common/lib.a $(KLIB)/lib/common.a
	$(INSTALL_DATA) $(KFLAVOUR)/arch/ppc/boot/of1275/lib.a $(KLIB)/lib/of.a
	$(INSTALL_DATA) $(KFLAVOUR)/arch/ppc/boot/ld.script $(KLIB)/boot
	$(INSTALL_DATA) $(KFLAVOUR)/arch/ppc/boot/openfirmware/note $(KLIB)/boot
	$(INSTALL_EXEC) $(KFLAVOUR)/arch/ppc/boot/utils/addnote $(KLIB)/utils
	$(INSTALL_EXEC) $(KFLAVOUR)/arch/ppc/boot/utils/hack-coff $(KLIB)/utils
	$(INSTALL_EXEC) $(KFLAVOUR)/arch/ppc/boot/utils/mknote $(KLIB)/utils
	$(INSTALL_EXEC) $(KFLAVOUR)/arch/ppc/boot/utils/mkprep $(KLIB)/utils
	$(INSTALL_EXEC) $(KFLAVOUR)/arch/ppc/boot/utils/mkbugboot $(KLIB)/utils
	$(INSTALL_EXEC) $(KFLAVOUR)/arch/ppc/boot/utils/mktree $(KLIB)/utils

# add a README file
	$(INSTALL_DATA) debian/README.kernel-image $(KIMAGEDOC)/NEWS.Debian

# copy build infrastructure for kernel modules
	cd $(KFLAVOUR); find scripts -type f ! -name '*.o' | cpio -pd $(KSRC)
	cat debian/build-files | ( cd $(KFLAVOUR); cpio -pd $(KSRC) )

# add a README file
	mkdir -p $(KBUILDDOC)
	$(INSTALL_DATA) debian/README.kernel-build $(KBUILDDOC)/README.Debian

	touch $@

endif
endif

# the binary target is responsible for creating Debian packages from
# the various package directories created by the install target.
# Also, it creates additional packages such as kernel-headers directly
# from the build directories.

ifeq ($(DEB_HOST_ARCH),powerpc)
binary: binary-arch binary-indep
binary-indep: \
binary-kernel-patch-powerpc-$(kernel)
binary-arch: \
binary-kernel-headers-$(kernel) \
install \
$(foreach flavour,$(flavours),binary-flavour-$(flavour))
else
binary: binary-arch binary-indep
binary-arch:
binary-indep: \
binary-kernel-patch-powerpc-$(kernel)
endif

ifeq (0,$(MAKELEVEL))

binary-flavour-%:
	$(RECURSION) binary-flavour-$* flavour=$*

else
ifeq (1,$(MAKELEVEL))

binary-flavour-$(flavour): \
binary-kernel-image-$(flavour) \
binary-kernel-image-$(kernel)-$(flavour) \
binary-kernel-build-$(kernel)-$(flavour)

endif
endif

# the following targets do the actual work of creating the packages.

# The kernel-headers package is special since it can be created with a
# simple call to make-kpkg.

binary-kernel-headers-$(kernel):
	cd $(KSOURCE); APPEND_TO_VERSION='' make-kpkg kernel-headers
	mv $(KHEADERS) ..
	dpkg-distaddfile $(notdir $(KHEADERS)) devel optional

# For all packages that are actually built here, almost all tasks are
# identical and are therefore collected in a common target.  The
# recursion takes care of passing the package name to the debhelper
# scripts.

# The kernel-image packages branch from the common packaging procedure
# just before the creation of the control file, because they need
# install scripts.  Right now, they simply uses the ones from the
# intermediate package.  In the future, it may come handy to modify
# them slightly, so the following code snippet is kept for reference.
# It erases $loader and changes $kimage from vmlinux to vmlinuz.
#
#	for file in {pre,post}{inst,rm}; do \
#	< $(KIMAGE)/DEBIAN/$$file sed \
#	-e's/^\(my..loader.*"\)[a-z]*"/\1"/' \
#	-e's/^\(my..kimage.*"vmlinu\)x/\1z/' > $(KIMAGE)/DEBIAN/$$file; \
#	chmod 755 $(KIMAGE)/DEBIAN/$$file; done

binary-kernel-image-$(kernel)-%: stamp-install
	$(RECURSION) binary-image-package DH_OPTIONS=-pkernel-image-$(kernel)-$*

binary-image-package: binary-common-package
	dpkg -e $(KDUMMY) $(KIMAGE)/DEBIAN
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary-kernel-patch-%: stamp-install-prepare
	$(RECURSION) binary-package DH_OPTIONS=-pkernel-patch-$*
binary-kernel-%: stamp-install
	$(RECURSION) binary-package DH_OPTIONS=-pkernel-$*
binary-package: binary-common-package
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary-common-package:
	dh_testdir
	dh_testroot
	dh_installdocs
	dh_installexamples
	dh_installmodules
	dh_installchangelogs
	dh_compress -X.stub
	dh_link
	dh_strip
	dh_fixperms
	dh_installdeb

# the clean target flushes the whole shebang down the drain, generated
# files in debian/, build directories, stamp files, everything.  The
# install-clean target reverts only the install stage, which is nice
# for changing the packaging without having to rebuild in all the
# kernel source trees.

install-clean:
	for file in debian/*.m4; do rm -f $${file%.m4}; done
	for dir in debian/kernel-*; do if test -d $$dir; then rm -rf $$dir; fi; done
	rm -f stamp-install-*

clean:	install-clean
	dh_testdir

	rm -rf $(TMP)
	rm -f stamp-*

	dh_clean

.PHONY:	configure build install binary binary-arch clean

# this target is intended for easy upgrade of the control file, in
# case the kernel version, the Debian revision, or the flavours
# change.

# slightly different m4 call, using a shell variable for the flavour
M4CONTROL = m4 -DM4KERNEL=$(kernel) -DM4FLAVOUR=$$flavour

debian/control: \
debian/changelog debian/flavours \
$(foreach flavour,$(flavours), debian/flavour-$(flavour)) \
debian/control.stub debian/control-image.m4

	$(M4CONTROL) debian/control.stub > $@

	for flavour in $(flavours); do \
	$(M4CONTROL) debian/control-image.m4 >> $@; \
	cat debian/flavour-$$flavour >> $@; \
	done

