Basic implementation of 'make headers_install'

This adds a make target which exports a subset of headers which contain
definitions which are useful for system libraries and tools. It uses the
BSD 'unifdef' tool to remove instances of #ifdef __KERNEL__, and uses
sed to remove markers like __user.

Based on an original implementation by Arnd Bergmann <arnd@arndb.de>
Hacked about by David Woodhouse <dwmw2@infradead.org>
Reviewed and cleaned up by Sam Ravnborg <sam@ravnborg.org>

Signed-off-by: David Woodhouse <dwmw2@infradead.org>
diff --git a/scripts/Makefile.headersinst b/scripts/Makefile.headersinst
new file mode 100644
index 0000000..688f8cb
--- /dev/null
+++ b/scripts/Makefile.headersinst
@@ -0,0 +1,147 @@
+# ==========================================================================
+# Installing headers
+#
+# header-y files will be installed verbatim
+# unifdef-y are the files where unifdef will be run before installing files
+# objhdr-y are generated files that will be installed verbatim
+#
+# ==========================================================================
+
+UNIFDEF := unifdef -U__KERNEL__
+
+# Eliminate the contents of (and inclusions of) compiler.h
+HDRSED  := sed 	-e "s/ inline / __inline__ /g" \
+		-e "s/[[:space:]]__user[[:space:]]\+/ /g" \
+		-e "s/(__user[[:space:]]\+/ (/g" \
+		-e "s/[[:space:]]__force[[:space:]]\+/ /g" \
+		-e "s/(__force[[:space:]]\+/ (/g" \
+		-e "s/[[:space:]]__iomem[[:space:]]\+/ /g" \
+		-e "s/(__iomem[[:space:]]\+/ (/g" \
+		-e "s/[[:space:]]__attribute_const__[[:space:]]\+/\ /g" \
+		-e "s/[[:space:]]__attribute_const__$$//" \
+		-e "/^\#include <linux\/compiler.h>/d"
+
+_dst := $(if $(dst),$(dst),$(obj))
+
+.PHONY: __headersinst
+__headersinst:
+
+
+ifeq (,$(patsubst include/asm/%,,$(obj)/))
+# For producing the generated stuff in include/asm for biarch builds, include
+# both sets of Kbuild files; we'll generate anything which is mentioned in
+# _either_ arch, and recurse into subdirectories which are mentioned in either
+# arch. Since some directories may exist in one but not the other, we must
+# use '-include'.
+GENASM := 1
+archasm	   := $(subst include/asm,asm-$(ARCH),$(obj))
+altarchasm := $(subst include/asm,asm-$(ALTARCH),$(obj))
+-include $(srctree)/include/$(archasm)/Kbuild
+-include $(srctree)/include/$(altarchasm)/Kbuild
+else
+include $(srctree)/$(obj)/Kbuild
+endif
+
+include scripts/Kbuild.include
+
+# If this is include/asm-$(ARCH) and there's no $(ALTARCH), then
+# override $(_dst) so that we install to include/asm directly.
+ifeq ($(obj)$(ALTARCH),include/asm-$(ARCH))
+     _dst := include/asm
+endif
+
+header-y	:= $(sort $(header-y))
+unifdef-y	:= $(sort $(unifdef-y))
+subdir-y	:= $(patsubst %/,%,$(filter %/, $(header-y)))
+header-y	:= $(filter-out %/, $(header-y))
+header-y	:= $(filter-out $(unifdef-y),$(header-y))
+
+ifdef ALTARCH
+ifeq ($(obj),include/asm-$(ARCH))
+altarch-y	:= altarch-dir
+endif
+endif
+
+# Make the definitions visible for recursive make invocations
+export ALTARCH
+export ARCHDEF
+export ALTARCHDEF
+
+quiet_cmd_o_hdr_install   = INSTALL $(_dst)/$@
+      cmd_o_hdr_install   = cp $(objtree)/$(obj)/$@ $(INSTALL_HDR_PATH)/$(_dst)
+
+quiet_cmd_headers_install = INSTALL $(_dst)/$@
+      cmd_headers_install = $(HDRSED) $(srctree)/$(obj)/$@		\
+			    > $(INSTALL_HDR_PATH)/$(_dst)/$@
+
+quiet_cmd_unifdef	  = UNIFDEF $(_dst)/$@
+      cmd_unifdef	  = $(UNIFDEF) $(srctree)/$(obj)/$@ | $(HDRSED)	\
+                            > $(INSTALL_HDR_PATH)/$(_dst)/$@ || :
+
+quiet_cmd_mkdir		  = MKDIR   $@
+      cmd_mkdir		  = mkdir -p $(INSTALL_HDR_PATH)/$@
+
+quiet_cmd_gen		  = GEN     $(_dst)/$@
+      cmd_gen		  = \
+STUBDEF=__ASM_STUB_`echo $@ | tr a-z. A-Z_`;				\
+(echo "/* File autogenerated by 'make headers_install' */" ;		\
+echo "\#ifndef $$STUBDEF" ;						\
+echo "\#define $$STUBDEF" ;						\
+echo "\# if $(ARCHDEF)" ;						\
+if [ -r $(srctree)/include/$(archasm)/$@ ]; then			\
+	echo "\#  include <$(archasm)/$@>" ;				\
+else									\
+	echo "\#  error $(archasm)/$@ does not exist in"		\
+			"the $(ARCH) architecture" ;			\
+fi ;									\
+echo "\# elif $(ALTARCHDEF)" ;						\
+if [ -r $(srctree)/include/$(altarchasm)/$@ ]; then			\
+	echo "\#  include <$(altarchasm)/$@>" ;				\
+else									\
+	echo "\#  error $(altarchasm)/$@ does not exist in"		\
+			"the $(ALTARCH) architecture" ;			\
+fi ;									\
+echo "\# else" ;							\
+echo "\#  warning This machine appears to be"				\
+		 "neither $(ARCH) nor $(ALTARCH)." ;			\
+echo "\# endif" ;							\
+echo "\#endif /* $$STUBDEF */" ;					\
+) > $(INSTALL_HDR_PATH)/$(_dst)/$@
+
+__headersinst: $(subdir-y) $(header-y) $(unifdef-y) $(altarch-y) $(objhdr-y)
+
+.PHONY: $(header-y) $(unifdef-y) $(subdir-y)
+
+# Rules for installing headers
+
+$(objhdr-y) $(subdir-y) $(header-y) $(unifdef-y): $(_dst)
+
+.PHONY: $(_dst)
+$(_dst):
+	$(call cmd,mkdir)
+
+ifdef GENASM
+$(objhdr-y) $(header-y) $(unifdef-y):
+	$(call cmd,gen)
+
+else
+$(objhdr-y):
+	$(call cmd,o_hdr_install)
+
+$(header-y):
+	$(call cmd,headers_install)
+
+$(unifdef-y):
+	$(call cmd,unifdef)
+endif
+
+hdrinst := -rR -f $(srctree)/scripts/Makefile.headersinst obj
+
+.PHONY: altarch-dir
+altarch-dir:
+	$(Q)$(MAKE) $(hdrinst)=include/asm-$(ALTARCH) dst=include/asm-$(ALTARCH)
+	$(Q)$(MAKE) $(hdrinst)=include/asm dst=include/asm
+
+# Recursion
+$(subdir-y):
+	$(Q)$(MAKE) $(hdrinst)=$(obj)/$@ dst=$(_dst)/$@ rel=../$(rel)