kbuild: generate u-boot.cfg as a byproduct of include/autoconf.mk

Our build system still parses ad-hoc CONFIG options in header files
and generates include/autoconf.mk so that Makefiles can reference
them.  This gimmick was introduced in the pre-Kconfig days and will
be kept until Kconfig migration is completed.

The include/autoconf.mk is generated like follows:

  [1] Preprocess include/common.h with -DDO_DEPS_ONLY and
      retrieve macros into include/autoconf.mk.tmp
  [2] Reformat include/autoconf.mk.dep into include/autoconf.mk
      with tools/scripts/define2mk.sed script
  [3] Remove include/autoconf.mk.tmp

Here, include/autoconf.mk.tmp is similar to u-boot.cfg, which is
also generated by preprocessing include/config.h with -DDO_DEPS_ONLY.
In other words, there is much overlap among include/autoconf.mk and
u-boot.cfg build rules.

So, the idea is to split the build rule of include/autoconf.mk
into two stages.  The first preprocesses headers into u-boot.cfg.
The second parses the u-boot.cfg into include/autoconf.mk.  The
build rules of u-boot.cfg in Makefile and spl/Makefile will be gone.

Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
diff --git a/scripts/Makefile.autoconf b/scripts/Makefile.autoconf
index ba674f8..2f85eb9 100644
--- a/scripts/Makefile.autoconf
+++ b/scripts/Makefile.autoconf
@@ -58,29 +58,44 @@
 # same CONFIG macros
 quiet_cmd_autoconf = GEN     $@
       cmd_autoconf = \
-	$(CPP) $(c_flags) $2 -DDO_DEPS_ONLY -dM $(srctree)/include/common.h > $@.tmp && { \
-		sed -n -f $(srctree)/tools/scripts/define2mk.sed $@.tmp |		\
+		sed -n -f $(srctree)/tools/scripts/define2mk.sed $< |			\
 		while read line; do							\
 			if [ -n "${KCONFIG_IGNORE_DUPLICATES}" ] ||			\
 			   ! grep -q "$${line%=*}=" include/config/auto.conf; then	\
 				echo "$$line";						\
 			fi								\
-		done > $@;								\
-		rm $@.tmp;								\
-	} || {										\
-		rm $@.tmp; false;							\
+		done > $@
+
+quiet_cmd_u_boot_cfg = CFG     $@
+      cmd_u_boot_cfg = \
+	$(CPP) $(c_flags) $2 -DDO_DEPS_ONLY -dM $(srctree)/include/common.h > $@.tmp && { \
+		grep 'define CONFIG_' $@.tmp > $@;			\
+		rm $@.tmp;						\
+	} || {								\
+		rm $@.tmp; false;					\
 	}
 
+u-boot.cfg: include/config.h FORCE
+	$(call cmd,u_boot_cfg)
+
+spl/u-boot.cfg: include/config.h FORCE
+	$(Q)mkdir -p $(dir $@)
+	$(call cmd,u_boot_cfg,-DCONFIG_SPL_BUILD)
+
+tpl/u-boot.cfg: include/config.h FORCE
+	$(Q)mkdir -p $(dir $@)
+	$(call cmd,u_boot_cfg,-DCONFIG_SPL_BUILD -DCONFIG_TPL_BUILD)
+
-include/autoconf.mk: include/config.h FORCE
+include/autoconf.mk: u-boot.cfg
 	$(call cmd,autoconf)
 
-spl/include/autoconf.mk: include/config.h FORCE
+spl/include/autoconf.mk: spl/u-boot.cfg
 	$(Q)mkdir -p $(dir $@)
-	$(call cmd,autoconf,-DCONFIG_SPL_BUILD)
+	$(call cmd,autoconf)
 
-tpl/include/autoconf.mk: include/config.h FORCE
+tpl/include/autoconf.mk: tpl/u-boot.cfg
 	$(Q)mkdir -p $(dir $@)
-	$(call cmd,autoconf,-DCONFIG_SPL_BUILD -DCONFIG_TPL_BUILD)
+	$(call cmd,autoconf)
 
 # include/config.h
 # Prior to Kconfig, it was generated by mkconfig. Now it is created here.