New deb rules for linux-imx

- Based on the upstream Debian rules instead of kernel's built-in
make-deb
- Contains upstream's scripts infrastructure to build module tools
- Upstream {pre,post}{inst,rm} scripts
- Custom rules file that distills down most of the build/install
process, along with a few of the extra things that we had previously
added to builddeb

Change-Id: I2af5c1774617a12cd0a2eceb367af94349c8aea5
diff --git a/debian/changelog b/debian/changelog
new file mode 100644
index 0000000..407d0d8
--- /dev/null
+++ b/debian/changelog
@@ -0,0 +1,5 @@
+linux-imx (1) UNRELEASED; urgency=medium
+
+  * Initial release.
+
+ -- AIY Projects <support-aiyprojects@google.com>  Mon, 04 Jun 2018 16:14:00 -0800
diff --git a/debian/compat b/debian/compat
new file mode 100644
index 0000000..f599e28
--- /dev/null
+++ b/debian/compat
@@ -0,0 +1 @@
+10
diff --git a/debian/control b/debian/control
new file mode 100644
index 0000000..1d5ee76
--- /dev/null
+++ b/debian/control
@@ -0,0 +1,19 @@
+Source: linux-imx
+Maintainer: AIY Projects <support-aiyprojects@google.com>
+Build-Depends: bc, cpio, debhelper, libssl-dev, python
+Section: misc
+Priority: optional
+
+Package: linux-image-4.9.51-aiy
+Section: misc
+Priority: required
+Architecture: arm64
+Depends: kmod, perl-base
+Description: Miscellaneous tools to support the AIY Board
+
+Package: linux-headers-4.9.51-aiy
+Section: misc
+Priority: required
+Architecture: arm64
+Depends: perl-base
+Description: Miscellaneous tools to support the AIY Board
diff --git a/debian/copyright b/debian/copyright
new file mode 100644
index 0000000..cb411e2
--- /dev/null
+++ b/debian/copyright
@@ -0,0 +1,7 @@
+Format: http://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
+Upstream-Name: linux-imx
+Source: https://aiyprojects.googlesource.com/linux-imx
+
+Files: *
+Copyright: Copyright 2018 Google, LLC <support-aiyprojects@google.com>
+License: Apache-2.0
diff --git a/debian/defconfig b/debian/defconfig
new file mode 100644
index 0000000..17c06e2
--- /dev/null
+++ b/debian/defconfig
@@ -0,0 +1,81 @@
+CONFIG_IKCONFIG=y
+CONFIG_ANDROID_PARANOID_NETWORK=n
+CONFIG_BRCMFMAC=n
+CONFIG_BRCMFMAC_SDIO=n
+CONFIG_BRCMUTIL=n
+CONFIG_BUILD_ARM_APPENDED_DTB_IMAGE=n
+CONFIG_KERNEL_GZIP=y
+CONFIG_KERNEL_LZO=n
+CONFIG_REGULATOR_BD71837=y
+CONFIG_MFD_BD71837=y
+CONFIG_LOG_BUF_SHIFT=17
+CONFIG_WLAN=y
+CONFIG_ATH10K=n
+
+# QCACLD-2.0
+CONFIG_CFG80211_INTERNAL_REGDB=y
+CONFIG_CFG80211_WEXT=y
+CONFIG_WIRELESS=y
+CONFIG_WIRELESS_EXT=y
+CONFIG_WEXT_CORE=y
+CONFIG_WEXT_PROC=y
+CONFIG_WEXT_PRIV=y
+CONFIG_WEXT_SPY=y
+CONFIG_QCA_CLD_WLAN=y
+# CONFIG_CLD_HL_SDIO_CORE is not set
+CONFIG_QCACLD_WLAN_LFR3=y
+CONFIG_PRIMA_WLAN_OKC=y
+CONFIG_PRIMA_WLAN_11AC_HIGH_TP=y
+CONFIG_WLAN_FEATURE_11W=y
+CONFIG_WLAN_FEATURE_LPSS=y
+CONFIG_QCOM_VOWIFI_11R=y
+CONFIG_WLAN_FEATURE_NAN=y
+CONFIG_WLAN_FEATURE_NAN_DATAPATH=y
+CONFIG_QCOM_TDLS=y
+CONFIG_QCOM_LTE_COEX=y
+CONFIG_WLAN_OFFLOAD_PACKETS=y
+CONFIG_WLAN_FEATURE_MEMDUMP=y
+CONFIG_WLAN_SYNC_TSF=y
+# CONFIG_QCA_WIFI_AUTOMOTIVE_CONC is not set
+# CONFIG_WLAN_UDP_RESPONSE_OFFLOAD is not set
+# CONFIG_WLAN_WOW_PULSE is not set
+# CONFIG_DPTRACE_ENABLE is not set
+CONFIG_RFKILL=y
+
+CONFIG_I2C=y
+CONFIG_I2C_CHARDEV=y
+CONFIG_SND_SOC_FSL_ASOC_CARD=n
+CONFIG_SND_GOOGLE_EDGETPU_CARD=y
+CONFIG_SND_SOC_RT5645=y
+CONFIG_SND_SOC_RT5631=y
+CONFIG_SND_SOC_RL6231=y
+CONFIG_MFD_BD71837=y
+CONFIG_REGULATOR_BD71837=y
+CONFIG_MXC_CAMERA_OV5645_MIPI_V2=y
+CONFIG_SPI_IMX=y
+CONFIG_PCIEASPM_POWERSAVE=n
+CONFIG_PCIEASPM_POWER_SUPERSAVE=y
+CONFIG_LOCALVERSION_AUTO=n
+CONFIG_IMX8MQ_PHANBELL_POWERSAVE=y
+CONFIG_DRM_PANEL_TIANMA_S6D6FP0A2=y
+CONFIG_STAGING_GASKET_FRAMEWORK=y
+CONFIG_STAGING_APEX_DRIVER=y
+CONFIG_USB_GADGET=y
+CONFIG_USB_GADGETFS=y
+CONFIG_USB_FUNCTIONFS=y
+CONFIG_USB_FUNCTIONFS_GENERIC=y
+CONFIG_USB_LIBCOMPOSITE=y
+CONFIG_CONFIGFS_FS=y
+CONFIG_CONFIGFS_F_FS=y
+CONFIG_CONFIGFS_F_MTP=y
+CONFIG_CONFIGFS_F_PTP=y
+CONFIG_CONFIGFS_F_UEVENT=y
+CONFIG_USB_F_FS=y
+CONFIG_USB_F_ACM=y
+CONFIG_USB_GADGETFS=y
+CONFIG_GOOGLE_APEX_POWER=y
+
+CONFIG_USB_G_HID=y
+CONFIG_USB_SERIAL=y
+CONFIG_USB_SERIAL_CP210X=y
+CONFIG_MXC_GPU_VIV=n
diff --git a/debian/linux-headers-4.9.51-aiy.postinst b/debian/linux-headers-4.9.51-aiy.postinst
new file mode 100644
index 0000000..7a6b572
--- /dev/null
+++ b/debian/linux-headers-4.9.51-aiy.postinst
@@ -0,0 +1,18 @@
+#!/usr/bin/perl
+# Author: Michael Gilbert <michael.s.gilbert@gmail.com>
+# Origin: Stripped down version of the linux-headers postinst from Ubuntu's
+#         2.6.32-14-generic kernel, which was itself derived from a
+#         Debian linux-image postinst script.
+
+$|=1;
+my $version  = "4.9.51-aiy";
+
+if (-d "/etc/kernel/header_postinst.d") {
+  system ("run-parts --report --exit-on-error --arg=$version " .
+          "/etc/kernel/header_postinst.d") &&
+            die "Failed to process /etc/kernel/header_postinst.d";
+}
+
+exit 0;
+
+__END__
diff --git a/debian/linux-image-4.9.51-aiy.postinst b/debian/linux-image-4.9.51-aiy.postinst
new file mode 100644
index 0000000..5d38737
--- /dev/null
+++ b/debian/linux-image-4.9.51-aiy.postinst
@@ -0,0 +1,25 @@
+#!/bin/sh -e
+
+version=4.9.51-aiy
+image_path=/boot/vmlinuz-$version
+
+if [ "$1" != configure ]; then
+    exit 0
+fi
+
+depmod $version
+
+if [ -f /lib/modules/$version/.fresh-install ]; then
+    change=install
+else
+    change=upgrade
+fi
+linux-update-symlinks $change $version $image_path
+rm -f /lib/modules/$version/.fresh-install
+
+if [ -d /etc/kernel/postinst.d ]; then
+    DEB_MAINT_PARAMS="$*" run-parts --report --exit-on-error --arg=$version \
+	      --arg=$image_path /etc/kernel/postinst.d
+fi
+
+exit 0
diff --git a/debian/linux-image-4.9.51-aiy.postrm b/debian/linux-image-4.9.51-aiy.postrm
new file mode 100644
index 0000000..d4ba845
--- /dev/null
+++ b/debian/linux-image-4.9.51-aiy.postrm
@@ -0,0 +1,31 @@
+#!/bin/sh -e
+
+version=4.9.51-aiy
+image_path=/boot/vmlinuz-$version
+
+rm -f /lib/modules/$version/.fresh-install
+
+if [ "$1" != upgrade ] && command -v linux-update-symlinks >/dev/null; then
+    linux-update-symlinks remove $version $image_path
+fi
+
+if [ -d /etc/kernel/postrm.d ]; then
+    DEB_MAINT_PARAMS="$*" run-parts --report --exit-on-error --arg=$version \
+	      --arg=$image_path /etc/kernel/postrm.d
+fi
+
+if [ "$1" = purge ]; then
+    for extra_file in modules.dep modules.isapnpmap modules.pcimap \
+                      modules.usbmap modules.parportmap \
+                      modules.generic_string modules.ieee1394map \
+                      modules.ieee1394map modules.pnpbiosmap \
+                      modules.alias modules.ccwmap modules.inputmap \
+                      modules.symbols modules.ofmap \
+                      modules.seriomap modules.\*.bin \
+		      modules.softdep modules.devname; do
+	eval rm -f /lib/modules/$version/$extra_file
+    done
+    rmdir /lib/modules/$version || true
+fi
+
+exit 0
diff --git a/debian/linux-image-4.9.51-aiy.preinst b/debian/linux-image-4.9.51-aiy.preinst
new file mode 100644
index 0000000..0e025ac
--- /dev/null
+++ b/debian/linux-image-4.9.51-aiy.preinst
@@ -0,0 +1,21 @@
+#!/bin/sh -e
+
+version=4.9.51-aiy
+image_path=/boot/vmlinuz-$version
+
+if [ "$1" = abort-upgrade ]; then
+    exit 0
+fi
+
+if [ "$1" = install ]; then
+    # Create a flag file for postinst
+    mkdir -p /lib/modules/$version
+    touch /lib/modules/$version/.fresh-install
+fi
+
+if [ -d /etc/kernel/preinst.d ]; then
+    DEB_MAINT_PARAMS="$*" run-parts --report --exit-on-error --arg=$version \
+	      --arg=$image_path /etc/kernel/preinst.d
+fi
+
+exit 0
diff --git a/debian/linux-image-4.9.51-aiy.prerm b/debian/linux-image-4.9.51-aiy.prerm
new file mode 100644
index 0000000..14a678a
--- /dev/null
+++ b/debian/linux-image-4.9.51-aiy.prerm
@@ -0,0 +1,17 @@
+#!/bin/sh -e
+
+version=4.9.51-aiy
+image_path=/boot/vmlinuz-$version
+
+if [ "$1" != remove ]; then
+    exit 0
+fi
+
+linux-check-removal $version
+
+if [ -d /etc/kernel/prerm.d ]; then
+    DEB_MAINT_PARAMS="$*" run-parts --report --exit-on-error --arg=$version \
+	      --arg=$image_path /etc/kernel/prerm.d
+fi
+
+exit 0
diff --git a/debian/rules b/debian/rules
new file mode 100755
index 0000000..ba7ba80
--- /dev/null
+++ b/debian/rules
@@ -0,0 +1,85 @@
+#!/usr/bin/make -f
+# -*- makefile -*-
+
+# Uncomment this to turn on verbose mode.
+# export DH_VERBOSE=1
+
+VERSION := 4.9.51-aiy
+
+ARCH := arm64
+
+DTB := freescale/fsl-imx8mq-phanbell.dtb
+
+KERNEL_OPTIONS := \
+	ARCH=arm64 \
+	CROSS_COMPILE=aarch64-linux-gnu- \
+	LOCALVERSION=-aiy
+
+LDFLAGS :=
+BUILD_DIR := $(CURDIR)/debian/build
+DESTDIR := $(CURDIR)/debian/linux-image-$(VERSION)
+DESTDIR_HEADERS := $(CURDIR)/debian/linux-headers-$(VERSION)
+
+%:
+	dh $@
+
+override_dh_auto_clean:
+	true
+
+override_dh_auto_build:
+	+make $(KERNEL_OPTIONS) defconfig
+	cat debian/defconfig | tee -a .config
+	+make $(KERNEL_OPTIONS) olddefconfig
+	+make $(KERNEL_OPTIONS) -j$(shell nproc) Image modules dtbs
+
+override_dh_auto_install:
+	+make $(KERNEL_OPTIONS) INSTALL_MOD_PATH=$(DESTDIR) modules_install
+	rm $(DESTDIR)/lib/modules/$(VERSION)/build
+	rm $(DESTDIR)/lib/modules/$(VERSION)/source
+
+	# Setup contents of /boot
+	install -d $(DESTDIR)/boot
+	cp arch/$(ARCH)/boot/Image $(DESTDIR)/boot/vmlinuz-$(VERSION)
+	ln -sf vmlinuz-$(VERSION) $(DESTDIR)/boot/Image
+	cp System.map $(DESTDIR)/boot/System.map-$(VERSION)
+	cp .config $(DESTDIR)/boot/config-$(VERSION)
+	cp arch/$(ARCH)/boot/dts/$(DTB) $(DESTDIR)/boot
+
+	# Build header directory structure
+	install -d $(DESTDIR_HEADERS)/usr/src/linux-headers-$(VERSION)
+	install -d $(DESTDIR_HEADERS)/usr/src/linux-headers-$(VERSION)/arch/arm
+	install -d $(DESTDIR_HEADERS)/usr/src/linux-headers-$(VERSION)/arch/$(ARCH)/kernel
+	install -d $(DESTDIR_HEADERS)/usr/src/linux-headers-$(VERSION)/drivers/staging/android
+
+	# Copy module information, include files, and important Makefiles into the header tree
+	cp -a .config $(DESTDIR_HEADERS)/usr/src/linux-headers-$(VERSION)
+	cp -a Module.symvers $(DESTDIR_HEADERS)/usr/src/linux-headers-$(VERSION)
+	cp -a include $(DESTDIR_HEADERS)/usr/src/linux-headers-$(VERSION)
+	cp -a drivers/staging/android/uapi $(DESTDIR_HEADERS)/usr/src/linux-headers-$(VERSION)/drivers/staging/android
+	cp -a arch/$(ARCH)/include $(DESTDIR_HEADERS)/usr/src/linux-headers-$(VERSION)/arch/$(ARCH)
+	# Sigh, arm64 headers also need arm headers...
+	cp -a arch/arm/include $(DESTDIR_HEADERS)/usr/src/linux-headers-$(VERSION)/arch/arm
+	cp -a Makefile $(DESTDIR_HEADERS)/usr/src/linux-headers-$(VERSION)
+
+	( \
+		find arch/$(ARCH) -maxdepth 1 -name 'Makefile*' -print; \
+		find arch/$(ARCH) \( -name 'module.lds' -o -name 'Kbuild.platforms' -o -name 'Platform' \) -print; \
+		find $$(find arch/$(ARCH) \( -name include -o -name scripts \) -type d -print) -print; \
+	) | cpio -pdm $(DESTDIR_HEADERS)/usr/src/linux-headers-$(VERSION)
+
+	# Clean up any leftover x86 artifacts from the kernel build, and then build scripts
+	find $(CURDIR)/scripts -type f | xargs file | grep 'x86-' | awk '{print $$1}' | tr -d ':' | xargs rm -f
+	mkdir -p $(BUILD_DIR)/build-tools/scripts
+	$(MAKE) $(KERNEL_OPTIONS) \
+		-C $(BUILD_DIR)/build-tools/scripts -f $(CURDIR)/debian/rules.d/scripts/Makefile \
+		top_rulesdir=$(CURDIR)/debian/rules.d \
+		top_srcdir=$(CURDIR) \
+		OUTDIR=scripts \
+		install \
+		DESTDIR=$(DESTDIR_HEADERS) \
+		prefix=/usr/src/linux-headers-$(VERSION)
+	find $(BUILD_DIR)/build-tools
+
+	# Setup the symlink from the modules directory to the headers
+	install -d $(DESTDIR_HEADERS)/lib/modules/$(VERSION)
+	ln -s /usr/src/linux-headers-$(VERSION) $(DESTDIR_HEADERS)/lib/modules/$(VERSION)/build
diff --git a/debian/rules.d/Makefile.inc b/debian/rules.d/Makefile.inc
new file mode 100644
index 0000000..48f8c2e
--- /dev/null
+++ b/debian/rules.d/Makefile.inc
@@ -0,0 +1,57 @@
+# Normalise OUTDIR to avoid triggering rebuilds
+override OUTDIR := $(patsubst ./%,%,$(OUTDIR))
+
+VPATH = $(top_rulesdir)/$(OUTDIR) $(top_srcdir)/$(OUTDIR)
+
+SHELL = /bin/sh -e
+
+CC = $(CROSS_COMPILE)gcc
+CXX = $(CROSS_COMPILE)g++
+CFLAGS := $(shell dpkg-buildflags --get CFLAGS) -Wall
+CPPFLAGS := $(shell dpkg-buildflags --get CPPFLAGS) \
+	-I$(top_srcdir)/$(OUTDIR) \
+	-I$(top_srcdir)/debian/build/build-tools/$(OUTDIR) \
+	-isystem $(top_srcdir)/debian/build/build-tools/include
+CXXFLAGS := $(shell dpkg-buildflags --get CXXFLAGS) -Wall
+LDFLAGS := $(shell dpkg-buildflags --get LDFLAGS)
+
+installdir ?= $(prefix)/$(OUTDIR)
+
+all: all-local all-recursive
+clean: clean-recursive
+install: install-local install-recursive
+
+%-recursive:
+	+@list='$(SUBDIRS)'; \
+	for subdir in $$list; do \
+	  echo "Making $* in $$subdir"; \
+	  mkdir -p $$subdir; \
+	  $(MAKE) -C $$subdir -f $(top_rulesdir)/$(OUTDIR)/$$subdir/Makefile OUTDIR=$(OUTDIR)/$$subdir $*; \
+	done
+
+all-local: $(PROGS)
+
+install-local: install-local-progs install-local-scripts install-local-data
+
+install-local-progs: $(PROGS)
+	@for p in $^; do \
+	  echo " install -m755 '$$p' '$(DESTDIR)/$(installdir)'"; \
+	  install -D -m755 "$$p" "$(DESTDIR)/$(installdir)/$$(basename $$p)"; \
+	done
+
+SCRIPTS_REAL = $(wildcard $(addprefix $(top_srcdir)/$(OUTDIR)/,$(SCRIPTS)))
+
+install-local-scripts: $(SCRIPTS_REAL)
+	@for p in $^; do \
+	  echo " install -m755 '$$p' '$(DESTDIR)/$(installdir)'"; \
+	  install -D -m755 "$$p" "$(DESTDIR)/$(installdir)/$$(basename $$p)"; \
+	done
+
+DATA_REAL = $(wildcard $(addprefix $(top_srcdir)/$(OUTDIR)/,$(DATA)))
+
+install-local-data: $(DATA_REAL)
+	@for p in $^; do \
+	  echo " install -m644 '$$p' '$(DESTDIR)/$(installdir)'"; \
+	  install -D -m644 "$$p" "$(DESTDIR)/$(installdir)/$$(basename $$p)"; \
+	done
+
diff --git a/debian/rules.d/scripts/Makefile b/debian/rules.d/scripts/Makefile
new file mode 100644
index 0000000..fc8c681
--- /dev/null
+++ b/debian/rules.d/scripts/Makefile
@@ -0,0 +1,44 @@
+PROGS = \
+	conmakehash \
+	extract-cert \
+	kallsyms \
+	pnmtologo \
+	recordmcount \
+	sign-file \
+	unifdef
+
+DATA = \
+	Kbuild.include \
+	Makefile.* \
+	mkversion \
+	module-common.lds
+
+SCRIPTS =  \
+	checkincludes.pl \
+	checkstack.pl \
+	checkversion.pl \
+	depmod.sh \
+	gcc-*.sh \
+	gen_initramfs_list.sh \
+	headers_install.sh \
+	kernel-doc \
+	Lindent \
+	makelst \
+	mksysmap \
+	mkuboot.sh \
+	namespace.pl \
+	patch-kernel \
+	recordmcount.pl \
+	setlocalversion \
+	ver_linux
+
+SUBDIRS = \
+	basic \
+	genksyms \
+	kconfig \
+	mod
+
+include $(top_rulesdir)/Makefile.inc
+
+CPPFLAGS += -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64
+extract-cert sign-file: LDLIBS += -lcrypto
diff --git a/debian/rules.d/scripts/basic/Makefile b/debian/rules.d/scripts/basic/Makefile
new file mode 100644
index 0000000..86b86a8
--- /dev/null
+++ b/debian/rules.d/scripts/basic/Makefile
@@ -0,0 +1,5 @@
+PROGS = \
+	bin2c \
+	fixdep
+
+include $(top_rulesdir)/Makefile.inc
diff --git a/debian/rules.d/scripts/genksyms/Makefile b/debian/rules.d/scripts/genksyms/Makefile
new file mode 100644
index 0000000..8e416da
--- /dev/null
+++ b/debian/rules.d/scripts/genksyms/Makefile
@@ -0,0 +1,13 @@
+PROGS = genksyms
+
+include $(top_rulesdir)/Makefile.inc
+
+genksyms: genksyms.o parse.tab.o lex.lex.o
+
+lex.lex.o: keywords.hash.c parse.tab.h
+
+%.c: %.c_shipped
+	ln -s $< $@
+
+%.h: %.h_shipped
+	ln -s $< $@
diff --git a/debian/rules.d/scripts/kconfig/Makefile b/debian/rules.d/scripts/kconfig/Makefile
new file mode 100644
index 0000000..b6e2521
--- /dev/null
+++ b/debian/rules.d/scripts/kconfig/Makefile
@@ -0,0 +1,13 @@
+PROGS = conf
+
+include $(top_rulesdir)/Makefile.inc
+
+conf: conf.o zconf.tab.o
+
+zconf.tab.c: zconf.hash.c zconf.lex.c
+
+%.c: %.c_shipped
+	ln -sf $< $@
+
+%.h: %.h_shipped
+	ln -sf $< $@
diff --git a/debian/rules.d/scripts/mod/Makefile b/debian/rules.d/scripts/mod/Makefile
new file mode 100644
index 0000000..e57e262
--- /dev/null
+++ b/debian/rules.d/scripts/mod/Makefile
@@ -0,0 +1,22 @@
+PROGS = \
+	modpost \
+	modpost.real-lsb-32 \
+	modpost.real-lsb-64 \
+	modpost.real-msb-32 \
+	modpost.real-msb-64
+
+include $(top_rulesdir)/Makefile.inc
+
+wrapperdir = $(top_rulesdir)/$(OUTDIR)
+
+modpost.real-%:
+	$(MAKE) -f $(wrapperdir)/Makefile.real TYPE=$* SOURCEDIR=$(top_srcdir)/scripts/mod
+
+%: %.o
+	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^
+
+modpost-opts.h: $(top_srcdir)/scripts/mod/modpost.c
+	python $(wrapperdir)/gendef.py $< > $@
+
+modpost.o: modpost.c modpost-opts.h
+	$(CC) $(CFLAGS) -I $(CURDIR) -c -o $@ $<
diff --git a/debian/rules.d/scripts/mod/Makefile.real b/debian/rules.d/scripts/mod/Makefile.real
new file mode 100644
index 0000000..489ea4f
--- /dev/null
+++ b/debian/rules.d/scripts/mod/Makefile.real
@@ -0,0 +1,23 @@
+PROGS = modpost.real-$(TYPE)
+
+include $(top_rulesdir)/Makefile.inc
+
+wrapperdir = $(top_rulesdir)/$(OUTDIR)
+CFLAGS += -I $(CURDIR)/real-$(TYPE) -I $(wrapperdir)/real-$(TYPE)
+
+modpost.real-$(TYPE): file2alias.real-$(TYPE).o modpost.real-$(TYPE).o sumversion.real-$(TYPE).o
+	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^
+
+%.real-$(TYPE).o: $(SOURCEDIR)/%.c real-$(TYPE)/devicetable-offsets.h
+	$(CC) $(CFLAGS) -c -o $@ $<
+
+real-$(TYPE)/devicetable-offsets.s: $(SOURCEDIR)/devicetable-offsets.c
+	mkdir -p real-$(TYPE)
+	$(CC) -include $(wrapperdir)/real-$(TYPE)/types.h $(CFLAGS) -nostdinc -I$(top_srcdir)/include -S -o $@ $<
+
+real-$(TYPE)/devicetable-offsets.h: real-$(TYPE)/devicetable-offsets.s
+	echo >$@ "#define __DEVICEVTABLE_OFFSETS_H__"
+	sed -ne "/^->/{s:->#\(.*\):/* \1 */:; \
+		s:^->\([^ ]*\) [\$$#]*\([-0-9]*\) \(.*\):#define \1 \2 /* \3 */:; \
+		s:^->\([^ ]*\) [\$$#]*\([^ ]*\) \(.*\):#define \1 \2 /* \3 */:; \
+		s:->::; p;}" $< >>$@
diff --git a/debian/rules.d/scripts/mod/elfconfig.h b/debian/rules.d/scripts/mod/elfconfig.h
new file mode 100644
index 0000000..8c90ea6
--- /dev/null
+++ b/debian/rules.d/scripts/mod/elfconfig.h
@@ -0,0 +1,7 @@
+#include <endian.h>
+
+#if __BYTE_ORDER == __LITTLE_ENDIAN
+#define HOST_ELFDATA ELFDATA2LSB
+#elif __BYTE_ORDER == __BIG_ENDIAN
+#define HOST_ELFDATA ELFDATA2MSB
+#endif
diff --git a/debian/rules.d/scripts/mod/gendef.py b/debian/rules.d/scripts/mod/gendef.py
new file mode 100755
index 0000000..6e1bdf4
--- /dev/null
+++ b/debian/rules.d/scripts/mod/gendef.py
@@ -0,0 +1,20 @@
+#!/usr/bin/python
+import re
+import sys
+
+for line in open(sys.argv[1]):
+    match = re.search('getopt\(argc, argv, "([\w:]*?)"\)', line)
+    if match:
+        options = match.group(1)
+        break
+else:
+    raise RuntimeError
+
+print '#define GETOPT_OPTIONS "%s"' % options
+
+print '#define GETOPT_CASE',
+for c in options:
+    if c == ':' or c == 'T':
+        continue
+    print "case '%c':" % c,
+print
diff --git a/debian/rules.d/scripts/mod/modpost.c b/debian/rules.d/scripts/mod/modpost.c
new file mode 100644
index 0000000..d574957
--- /dev/null
+++ b/debian/rules.d/scripts/mod/modpost.c
@@ -0,0 +1,136 @@
+#include <elf.h>
+#include <errno.h>
+#include <getopt.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "modpost-opts.h"
+
+int main (int argc, char *argv[])
+{
+  char const *data, *class;
+  char *list_name = NULL;
+  char *name = NULL;
+  char prog[1024];
+  unsigned char ei[EI_NIDENT];
+  int opt;
+  FILE *file;
+
+  while ((opt = getopt (argc, argv, GETOPT_OPTIONS)) != -1)
+  { 
+    switch(opt)
+    {
+      GETOPT_CASE
+        break;
+      case 'T':
+        list_name = optarg;
+        break;
+      default:
+        return EXIT_FAILURE;
+    }
+  }
+
+  if (optind != argc)
+  {
+    name = argv[optind];
+  }
+  else if (list_name)
+  {
+    size_t name_len;
+    int is_stdin = strcmp (list_name, "-") == 0;
+
+    /* Read first line of list file */
+    if (is_stdin)
+    {
+      file = stdin;
+      setvbuf(stdin, NULL, _IONBF, 0); /* don't over-read */
+    }
+    else
+    {
+      file = fopen (list_name, "r");
+      if (!file)
+      {
+        fprintf (stderr, "Can't open \"%s\"\n", list_name);
+        return EXIT_FAILURE;
+      }
+    }
+    if (getline (&name, &name_len, file) < 0)
+    {
+      if (errno)
+      {
+        fprintf (stderr, "Can't read \"%s\"\n", list_name);
+        return EXIT_FAILURE;
+      }
+      else
+      {
+        /* Empty list */
+        return EXIT_SUCCESS;
+      }
+    }
+    if (!is_stdin)
+      fclose(file);
+
+    /* Remove new-line */
+    name [strcspn (name, "\n")] = 0;
+
+    /* If this came from stdin, we need to add the first name to the
+     * arguments, because the upstream modpost can't read it again.
+     */
+    if (is_stdin)
+    {
+      char **new_argv = malloc (sizeof(*argv) * (argc + 2));
+      memcpy(new_argv, argv, sizeof(*argv) * argc);
+      new_argv [argc] = name;
+      new_argv [argc + 1] = NULL;
+      argv = new_argv;
+    }
+  }
+  else
+  {
+    /* Empty list */
+    return EXIT_SUCCESS;
+  }
+
+  if (!(file = fopen (name, "r")))
+  {
+    fprintf (stderr, "Can't open \"%s\"\n", name);
+    return EXIT_FAILURE;
+  }
+
+  if (fread (ei, 1, EI_NIDENT, file) != EI_NIDENT)
+  {
+    fprintf (stderr, "Error: input truncated\n");
+    return EXIT_FAILURE;
+  }
+
+  if (memcmp (ei, ELFMAG, SELFMAG) != 0)
+  {
+    fprintf (stderr, "Error: not ELF\n");
+    return EXIT_FAILURE;
+  }
+  switch (ei[EI_DATA]) {
+    case ELFDATA2LSB:
+      data = "lsb";
+      break;
+    case ELFDATA2MSB:
+      data = "msb";
+      break;
+    default:
+      return EXIT_FAILURE;
+  }
+  switch (ei[EI_CLASS]) {
+    case ELFCLASS32:
+      class = "32";
+      break;
+    case ELFCLASS64:
+      class = "64";
+      break;
+    default:
+      return EXIT_FAILURE;
+  }
+  snprintf (prog, sizeof prog, "%s.real-%s-%s", argv[0], data, class);
+
+  return execv (prog, argv);
+}
diff --git a/debian/rules.d/scripts/mod/real-lsb-32/elfconfig.h b/debian/rules.d/scripts/mod/real-lsb-32/elfconfig.h
new file mode 100644
index 0000000..1f7a732
--- /dev/null
+++ b/debian/rules.d/scripts/mod/real-lsb-32/elfconfig.h
@@ -0,0 +1,4 @@
+#define KERNEL_ELFCLASS ELFCLASS32
+#define KERNEL_ELFDATA ELFDATA2LSB
+#define MODULE_SYMBOL_PREFIX ""
+#include "../elfconfig.h"
diff --git a/debian/rules.d/scripts/mod/real-lsb-32/types.h b/debian/rules.d/scripts/mod/real-lsb-32/types.h
new file mode 100644
index 0000000..bad6dcc
--- /dev/null
+++ b/debian/rules.d/scripts/mod/real-lsb-32/types.h
@@ -0,0 +1,3 @@
+#include "../types.h"
+typedef __u32 kernel_ulong_t;
+#define BITS_PER_LONG 32
diff --git a/debian/rules.d/scripts/mod/real-lsb-64/elfconfig.h b/debian/rules.d/scripts/mod/real-lsb-64/elfconfig.h
new file mode 100644
index 0000000..e6f519f
--- /dev/null
+++ b/debian/rules.d/scripts/mod/real-lsb-64/elfconfig.h
@@ -0,0 +1,4 @@
+#define KERNEL_ELFCLASS ELFCLASS64
+#define KERNEL_ELFDATA ELFDATA2LSB
+#define MODULE_SYMBOL_PREFIX ""
+#include "../elfconfig.h"
diff --git a/debian/rules.d/scripts/mod/real-lsb-64/types.h b/debian/rules.d/scripts/mod/real-lsb-64/types.h
new file mode 100644
index 0000000..8d7b875
--- /dev/null
+++ b/debian/rules.d/scripts/mod/real-lsb-64/types.h
@@ -0,0 +1,3 @@
+#include "../types.h"
+typedef __u64 __attribute__((aligned(8))) kernel_ulong_t;
+#define BITS_PER_LONG 64
diff --git a/debian/rules.d/scripts/mod/real-msb-32/elfconfig.h b/debian/rules.d/scripts/mod/real-msb-32/elfconfig.h
new file mode 100644
index 0000000..a9ae561
--- /dev/null
+++ b/debian/rules.d/scripts/mod/real-msb-32/elfconfig.h
@@ -0,0 +1,4 @@
+#define KERNEL_ELFCLASS ELFCLASS32
+#define KERNEL_ELFDATA ELFDATA2MSB
+#define MODULE_SYMBOL_PREFIX ""
+#include "../elfconfig.h"
diff --git a/debian/rules.d/scripts/mod/real-msb-32/types.h b/debian/rules.d/scripts/mod/real-msb-32/types.h
new file mode 100644
index 0000000..bad6dcc
--- /dev/null
+++ b/debian/rules.d/scripts/mod/real-msb-32/types.h
@@ -0,0 +1,3 @@
+#include "../types.h"
+typedef __u32 kernel_ulong_t;
+#define BITS_PER_LONG 32
diff --git a/debian/rules.d/scripts/mod/real-msb-64/elfconfig.h b/debian/rules.d/scripts/mod/real-msb-64/elfconfig.h
new file mode 100644
index 0000000..844d22f
--- /dev/null
+++ b/debian/rules.d/scripts/mod/real-msb-64/elfconfig.h
@@ -0,0 +1,4 @@
+#define KERNEL_ELFCLASS ELFCLASS64
+#define KERNEL_ELFDATA ELFDATA2MSB
+#define MODULE_SYMBOL_PREFIX ""
+#include "../elfconfig.h"
diff --git a/debian/rules.d/scripts/mod/real-msb-64/types.h b/debian/rules.d/scripts/mod/real-msb-64/types.h
new file mode 100644
index 0000000..8d7b875
--- /dev/null
+++ b/debian/rules.d/scripts/mod/real-msb-64/types.h
@@ -0,0 +1,3 @@
+#include "../types.h"
+typedef __u64 __attribute__((aligned(8))) kernel_ulong_t;
+#define BITS_PER_LONG 64
diff --git a/debian/rules.d/scripts/mod/types.h b/debian/rules.d/scripts/mod/types.h
new file mode 100644
index 0000000..aba3827
--- /dev/null
+++ b/debian/rules.d/scripts/mod/types.h
@@ -0,0 +1,9 @@
+/* Minimal definitions for mod_devicetable.h and devicetable-offsets.c */
+typedef unsigned char __u8;
+typedef unsigned short __u16;
+typedef unsigned int __u32;
+typedef unsigned long long __u64;
+typedef struct {
+        __u8 b[16];
+} uuid_le;
+#define offsetof(a,b) __builtin_offsetof(a,b)