#!/usr/bin/make -f

# Work around a bug in cdbs 0.4.14.
DEB_PATCHDIRS_READONLY = no

# This implements the .deb package creation using debhelper.
include /usr/share/cdbs/1/rules/debhelper.mk

# Simple patches in debian/patches.
include /usr/share/cdbs/1/rules/simple-patchsys.mk

DEB_HOST_ARCH_CPU := $(shell dpkg-architecture -qDEB_HOST_ARCH_CPU 2>/dev/null)

# Take account of old dpkg-architecture output.
ifeq ($(DEB_HOST_ARCH_CPU),)
  DEB_HOST_ARCH_CPU := $(shell dpkg-architecture -qDEB_HOST_GNU_CPU)
  ifeq ($(DEB_HOST_ARCH_CPU),x86_64)
    DEB_HOST_ARCH_CPU := amd64
  endif
endif

pkgdir = debian/linux-kernel-headers
includedir = $(pkgdir)/usr/include

kernel_arch := $(DEB_HOST_ARCH)
kernel_arch := $(patsubst armeb,arm,$(kernel_arch))
kernel_arch := $(patsubst armel,arm,$(kernel_arch))
kernel_arch := $(patsubst powerpc,ppc,$(kernel_arch))
kernel_arch := $(patsubst hppa,parisc,$(kernel_arch))
kernel_arch := $(patsubst mipsel,mips,$(kernel_arch))
kernel_arch := $(patsubst amd64,x86_64,$(kernel_arch))

other_headers := $(wildcard others/asm-$(kernel_arch)-*)
other_headers_inst := $(patsubst others/asm-$(kernel_arch)-%,%,$(other_headers))

# Install architecture-specific headers into debian/linux-kernel-headers.
# Common headers are handled by linux-kernel-headers.install.
common-install-arch:: lkh-install lkh-test

lkh-install: stamp-lkh-install
stamp-lkh-install: build
	rm -rf $(includedir)
	install -d $(includedir)
	install -d $(includedir)/linux

ifneq (,$(findstring $(DEB_HOST_ARCH), amd64 i386 powerpc ppc64 sparc))
	sh debian/generate-asm.sh $(DEB_HOST_ARCH) include $(includedir) autoconfs
else
	cp -a include/asm-$(kernel_arch) $(includedir)/asm
	cp autoconfs/autoconf-$(DEB_HOST_ARCH_CPU).h $(includedir)/linux/autoconf.h
endif

ifeq ($(kernel_arch),arm)
	# ARM needs some extra symlinks.
	rm -f $(includedir)/asm/proc
	rm -f $(includedir)/asm/arch
	ln -s proc-armv $(includedir)/asm/proc
	ln -s arch-ebsa285 $(includedir)/asm/arch
endif

	# Some architectures need to include generated headers.  We can't
	# generate them at build time, so maintain a static collection.
	# Note: not compatible with Sparc hack, above.
	for i in dummy $(other_headers_inst); do \
	  if test $$i != dummy; then \
	    cp others/asm-$(kernel_arch)-$$i \
	       $(includedir)/asm/$$i; \
	  fi; \
	done

	touch $@

lkh-test: lkh-install build
#	$(MAKE) -C testsuite all

clean::
	$(MAKE) -C testsuite clean
	rm -f stamp-lkh-install
