From c16b137e490308f5578dae9e7896a8798493c475 Mon Sep 17 00:00:00 2001
From: Masahiro Yamada <yamada.masahiro@socionext.com>
Date: Fri, 11 Jan 2019 19:42:27 +0900
Subject: [PATCH] kbuild: add .SECONDARY special target to
 scripts/Kbuild.include

Based on the following Linux commits:

 - 54a702f70589 ("kbuild: mark $(targets) as .SECONDARY and remove
   .PRECIOUS markers")

 - 8e9b61b293d9 ("kbuild: move .SECONDARY special target to
   Kbuild.include")

GNU Make automatically deletes intermediate files that are updated
in a chain of pattern rules.

Example 1) %.dtb.o <- %.dtb.S <- %.dtb <- %.dts
Example 2) %.o <- %.c <- %.c_shipped

A couple of makefiles mark such targets as .PRECIOUS to prevent Make
from deleting them, but the correct way is to use .SECONDARY.

  .SECONDARY
    Prerequisites of this special target are treated as intermediate
    files but are never automatically deleted.

  .PRECIOUS
    When make is interrupted during execution, it may delete the target
    file it is updating if the file was modified since make started.
    If you mark the file as precious, make will never delete the file
    if interrupted.

Both can avoid deletion of intermediate files, but the difference is
the behavior when Make is interrupted; .SECONDARY deletes the target,
but .PRECIOUS does not.

The use of .PRECIOUS is relatively rare since we do not want to keep
partially constructed (possibly corrupted) targets.

.SECONDARY with no prerequisites causes all targets to be treated as
secondary. This agrees the policy of Kbuild.

scripts/Kbuild.include seems a suitable place to add it because it is
included from almost all sub-makes.

Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
---
 dts/Makefile                  | 3 ---
 examples/standalone/Makefile  | 1 -
 post/lib_powerpc/fpu/Makefile | 3 ---
 scripts/Kbuild.include        | 3 +++
 scripts/Makefile.build        | 2 --
 scripts/Makefile.lib          | 3 ---
 6 files changed, 3 insertions(+), 12 deletions(-)

diff --git a/dts/Makefile b/dts/Makefile
index cd6e9a968e..a7a604303c 100644
--- a/dts/Makefile
+++ b/dts/Makefile
@@ -43,9 +43,6 @@ endif
 arch-dtbs:
 	$(Q)$(MAKE) $(build)=$(ARCH_PATH) dtbs
 
-.SECONDARY: $(obj)/dt.dtb.S $(obj)/dt-spl.dtb.S
-
-
 ifeq ($(CONFIG_SPL_BUILD),y)
 obj-$(CONFIG_OF_EMBED) := dt-spl.dtb.o
 # support "out-of-tree" build for dtb-spl
diff --git a/examples/standalone/Makefile b/examples/standalone/Makefile
index f01816f24f..0b17a91804 100644
--- a/examples/standalone/Makefile
+++ b/examples/standalone/Makefile
@@ -26,7 +26,6 @@ LIB	= $(obj)/libstubs.o
 LIBOBJS-$(CONFIG_PPC) += ppc_longjmp.o ppc_setjmp.o
 LIBOBJS-y += stubs.o
 
-.SECONDARY: $(call objectify,$(COBJS))
 targets += $(patsubst $(obj)/%,%,$(LIB)) $(COBJS) $(LIBOBJS-y)
 
 LIBOBJS	:= $(addprefix $(obj)/,$(LIBOBJS-y))
diff --git a/post/lib_powerpc/fpu/Makefile b/post/lib_powerpc/fpu/Makefile
index 404eefcfd0..9b2c1fadb6 100644
--- a/post/lib_powerpc/fpu/Makefile
+++ b/post/lib_powerpc/fpu/Makefile
@@ -11,9 +11,6 @@ targets += $(objs-before-objcopy)
 $(foreach m, $(objs-before-objcopy), $(eval CFLAGS_REMOVE_$m := -msoft-float))
 ccflags-y := -mhard-float -fkeep-inline-functions
 
-# Do not delete intermidiate files (*.o)
-.SECONDARY: $(call objectify, $(objs-before-objcopy))
-
 obj-y := $(objs-before-objcopy:.o=_.o)
 
 OBJCOPYFLAGS := -R .gnu.attributes
diff --git a/scripts/Kbuild.include b/scripts/Kbuild.include
index 460acd61f9..b8969e2a75 100644
--- a/scripts/Kbuild.include
+++ b/scripts/Kbuild.include
@@ -318,6 +318,9 @@ endif
 # delete partially updated (i.e. corrupted) files on error
 .DELETE_ON_ERROR:
 
+# do not delete intermediate files automatically
+.SECONDARY:
+
 ifdef CONFIG_SPL_BUILD
 SPL_ := SPL_
 ifeq ($(CONFIG_TPL_BUILD),y)
diff --git a/scripts/Makefile.build b/scripts/Makefile.build
index 482ed0c8d8..f7a041296d 100644
--- a/scripts/Makefile.build
+++ b/scripts/Makefile.build
@@ -331,8 +331,6 @@ quiet_cmd_asn1_compiler = ASN.1   $@
       cmd_asn1_compiler = $(objtree)/scripts/asn1_compiler $< \
 				$(subst .h,.c,$@) $(subst .c,.h,$@)
 
-.PRECIOUS: $(objtree)/$(obj)/%-asn1.c $(objtree)/$(obj)/%-asn1.h
-
 $(obj)/%-asn1.c $(obj)/%-asn1.h: $(src)/%.asn1 $(objtree)/scripts/asn1_compiler
 	$(call cmd,asn1_compiler)
 
diff --git a/scripts/Makefile.lib b/scripts/Makefile.lib
index a4f16bb4bb..a5b57fc6b9 100644
--- a/scripts/Makefile.lib
+++ b/scripts/Makefile.lib
@@ -214,7 +214,6 @@ endef
 quiet_cmd_flex = LEX     $@
       cmd_flex = $(LEX) -o$@ -L $<
 
-.PRECIOUS: $(obj)/%.lex.c
 $(obj)/%.lex.c: $(src)/%.l FORCE
 	$(call if_changed,flex)
 
@@ -223,14 +222,12 @@ $(obj)/%.lex.c: $(src)/%.l FORCE
 quiet_cmd_bison = YACC    $@
       cmd_bison = $(YACC) -o$@ -t -l $<
 
-.PRECIOUS: $(obj)/%.tab.c
 $(obj)/%.tab.c: $(src)/%.y FORCE
 	$(call if_changed,bison)
 
 quiet_cmd_bison_h = YACC    $@
       cmd_bison_h = $(YACC) -o/dev/null --defines=$@ -t -l $<
 
-.PRECIOUS: $(obj)/%.tab.h
 $(obj)/%.tab.h: $(src)/%.y FORCE
 	$(call if_changed,bison_h)
 
-- 
2.39.5