Skip to content

Commit 54a702f

Browse files
committed
kbuild: mark $(targets) as .SECONDARY and remove .PRECIOUS markers
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. Another difference is that .PRECIOUS works with pattern rules whereas .SECONDARY does not. .PRECIOUS: $(obj)/%.lex.c works, but .SECONDARY: $(obj)/%.lex.c has no effect. However, for the reason above, I do not want to use .PRECIOUS which could cause obscure build breakage. The targets specified as .SECONDARY must be explicit. $(targets) contains all targets that need to include .*.cmd files. So, the intermediates you want to keep are mostly in there. Therefore, mark $(targets) as .SECONDARY. It means primary targets are also marked as .SECONDARY, but I do not see any drawback for this. I replaced some .SECONDARY / .PRECIOUS markers with 'targets'. This will make Kbuild search for non-existing .*.cmd files, but this is not a noticeable performance issue. Signed-off-by: Masahiro Yamada <[email protected]> Acked-by: Frank Rowand <[email protected]> Acked-by: Ingo Molnar <[email protected]>
1 parent 4fa8bc9 commit 54a702f

File tree

8 files changed

+11
-20
lines changed

8 files changed

+11
-20
lines changed

arch/arc/boot/dts/Makefile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ endif
99
obj-y += $(builtindtb-y).dtb.o
1010
dtb-y := $(builtindtb-y).dtb
1111

12-
.SECONDARY: $(obj)/$(builtindtb-y).dtb.S
13-
1412
# for CONFIG_OF_ALL_DTBS test
1513
dtstree := $(srctree)/$(src)
1614
dtb- := $(patsubst $(dtstree)/%.dts,%.dtb, $(wildcard $(dtstree)/*.dts))

arch/arm/crypto/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,4 @@ $(src)/sha512-core.S_shipped: $(src)/sha512-armv4.pl
6767
$(call cmd,perl)
6868
endif
6969

70-
.PRECIOUS: $(obj)/sha256-core.S $(obj)/sha512-core.S
70+
targets += sha256-core.S sha512-core.S

arch/arm64/crypto/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,4 @@ $(src)/sha512-core.S_shipped: $(src)/sha512-armv8.pl
7878
$(call cmd,perlasm)
7979
endif
8080

81-
.PRECIOUS: $(obj)/sha256-core.S $(obj)/sha512-core.S
81+
targets += sha256-core.S sha512-core.S

arch/sparc/vdso/Makefile

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ vdso_img_cfiles := $(vdso_img-y:%=vdso-image-%.c)
2929
vdso_img_sodbg := $(vdso_img-y:%=vdso%.so.dbg)
3030
obj-y += $(vdso_img_objs)
3131
targets += $(vdso_img_cfiles)
32-
targets += $(vdso_img_sodbg)
33-
.SECONDARY: $(vdso_img-y:%=$(obj)/vdso-image-%.c) \
34-
$(vdso_img-y:%=$(obj)/vdso%.so)
32+
targets += $(vdso_img_sodbg) $(vdso_img-y:%=vdso%.so)
3533

3634
export CPPFLAGS_vdso.lds += -P -C
3735

arch/x86/entry/vdso/Makefile

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ vdso_img_cfiles := $(vdso_img-y:%=vdso-image-%.c)
4242
vdso_img_sodbg := $(vdso_img-y:%=vdso%.so.dbg)
4343
obj-y += $(vdso_img_objs)
4444
targets += $(vdso_img_cfiles)
45-
targets += $(vdso_img_sodbg)
46-
.SECONDARY: $(vdso_img-y:%=$(obj)/vdso-image-%.c) \
47-
$(vdso_img-y:%=$(obj)/vdso%.so)
45+
targets += $(vdso_img_sodbg) $(vdso_img-y:%=vdso%.so)
4846

4947
export CPPFLAGS_vdso.lds += -P -C
5048

drivers/of/unittest-data/Makefile

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,3 @@ DTC_FLAGS_testcases += -@
3030

3131
# suppress warnings about intentional errors
3232
DTC_FLAGS_testcases += -Wno-interrupts_property
33-
34-
.PRECIOUS: \
35-
$(obj)/%.dtb.S \
36-
$(obj)/%.dtb

scripts/Makefile.build

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -430,8 +430,6 @@ quiet_cmd_asn1_compiler = ASN.1 $@
430430
cmd_asn1_compiler = $(objtree)/scripts/asn1_compiler $< \
431431
$(subst .h,.c,$@) $(subst .c,.h,$@)
432432

433-
.PRECIOUS: $(objtree)/$(obj)/%.asn1.c $(objtree)/$(obj)/%.asn1.h
434-
435433
$(obj)/%.asn1.c $(obj)/%.asn1.h: $(src)/%.asn1 $(objtree)/scripts/asn1_compiler
436434
$(call cmd,asn1_compiler)
437435

@@ -544,10 +542,12 @@ targets := $(filter-out $(PHONY), $(targets))
544542
intermediate_targets = $(foreach sfx, $(2), \
545543
$(patsubst %$(strip $(1)),%$(sfx), \
546544
$(filter %$(strip $(1)), $(targets))))
545+
# %.asn1.o <- %.asn1.[ch] <- %.asn1
547546
# %.dtb.o <- %.dtb.S <- %.dtb <- %.dts
548547
# %.lex.o <- %.lex.c <- %.l
549548
# %.tab.o <- %.tab.[ch] <- %.y
550-
targets += $(call intermediate_targets, .dtb.o, .dtb.S .dtb) \
549+
targets += $(call intermediate_targets, .asn1.o, .asn1.c .asn1.h) \
550+
$(call intermediate_targets, .dtb.o, .dtb.S .dtb) \
551551
$(call intermediate_targets, .lex.o, .lex.c) \
552552
$(call intermediate_targets, .tab.o, .tab.c .tab.h)
553553

@@ -587,6 +587,10 @@ $(shell mkdir -p $(obj-dirs))
587587
endif
588588
endif
589589

590+
# Some files contained in $(targets) are intermediate artifacts.
591+
# We never want them to be removed automatically.
592+
.SECONDARY: $(targets)
593+
590594
# Declare the contents of the .PHONY variable as phony. We keep that
591595
# information in a variable se we can use it in if_changed and friends.
592596

scripts/Makefile.lib

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@ endef
184184
quiet_cmd_flex = LEX $@
185185
cmd_flex = $(LEX) -o$@ -L $<
186186

187-
.PRECIOUS: $(obj)/%.lex.c
188187
$(obj)/%.lex.c: $(src)/%.l FORCE
189188
$(call if_changed,flex)
190189

@@ -193,14 +192,12 @@ $(obj)/%.lex.c: $(src)/%.l FORCE
193192
quiet_cmd_bison = YACC $@
194193
cmd_bison = $(YACC) -o$@ -t -l $<
195194

196-
.PRECIOUS: $(obj)/%.tab.c
197195
$(obj)/%.tab.c: $(src)/%.y FORCE
198196
$(call if_changed,bison)
199197

200198
quiet_cmd_bison_h = YACC $@
201199
cmd_bison_h = bison -o/dev/null --defines=$@ -t -l $<
202200

203-
.PRECIOUS: $(obj)/%.tab.h
204201
$(obj)/%.tab.h: $(src)/%.y FORCE
205202
$(call if_changed,bison_h)
206203

0 commit comments

Comments
 (0)