+# That's our default target when none is given on the command line
+.PHONY: _all
+_all:
+
+ifneq ($(KBUILD_OUTPUT),)
+# Invoke a second make in the output directory, passing relevant variables
+# check that the output directory actually exists
+saved-output := $(KBUILD_OUTPUT)
+KBUILD_OUTPUT := $(shell cd $(KBUILD_OUTPUT) && /bin/pwd)
+$(if $(wildcard $(KBUILD_OUTPUT)),, \
+ $(error output directory "$(saved-output)" does not exist))
+
+.PHONY: $(MAKECMDGOALS)
+
+$(filter-out _all,$(MAKECMDGOALS)) _all: $(KBUILD_OUTPUT)/Rules.mak $(KBUILD_OUTPUT)/Makefile
+ $(MAKE) -C $(KBUILD_OUTPUT) \
+ top_srcdir=$(CURDIR) \
+ top_builddir=$(KBUILD_OUTPUT) \
+ TOPDIR=$(KBUILD_OUTPUT) \
+ KBUILD_SRC=$(CURDIR) \
+ -f $(CURDIR)/Makefile $@
+
+$(KBUILD_OUTPUT)/Rules.mak:
+ @echo > $@
+ @echo top_srcdir=$(CURDIR) >> $@
+ @echo top_builddir=$(KBUILD_OUTPUT) >> $@
+ @echo include $(top_srcdir)/Rules.mak >> $@
+
+$(KBUILD_OUTPUT)/Makefile:
+ @echo > $@
+ @echo top_srcdir=$(CURDIR) >> $@
+ @echo top_builddir=$(KBUILD_OUTPUT) >> $@
+ @echo KBUILD_SRC='$$(top_srcdir)' >> $@
+ @echo include '$$(KBUILD_SRC)'/Makefile >> $@