Skip to content

Commit 392885e

Browse files
committed
kbuild: let fixdep directly write to .*.cmd files
Currently, fixdep writes dependencies to .*.tmp, which is renamed to .*.cmd after everything succeeds. This is a very safe way to avoid corrupted .*.cmd files. The if_changed_dep has carried this safety mechanism since it was added in 2002. If fixdep fails for some reasons or a user terminates the build while fixdep is running, the incomplete output from the fixdep could be troublesome. This is my insight about some bad scenarios: [1] If the compiler succeeds to generate *.o file, but fixdep fails to write necessary dependencies to .*.cmd file, Make will miss to rebuild the object when headers or CONFIG options are changed. In this case, fixdep should not generate .*.cmd file at all so that 'arg-check' will surely trigger the rebuild of the object. [2] A partially constructed .*.cmd file may not be a syntactically correct makefile. The next time Make runs, it would include it, then fail to parse it. Once this happens, 'make clean' is be the only way to fix it. In fact, [1] is no longer a problem since commit 9c2af1c ("kbuild: add .DELETE_ON_ERROR special target"). Make deletes a target file on any failure in its recipe. Because fixdep is a part of the recipe of *.o target, if it fails, the *.o is deleted anyway. However, I am a bit worried about the slight possibility of [2]. So, here is a solution. Let fixdep directly write to a .*.cmd file, but allow makefiles to include it only when its corresponding target exists. This effectively reverts commit 2982c95 ("kbuild: remove redundant $(wildcard ...) for cmd_files calculation"), and commit 00d78ab ("kbuild: remove dead code in cmd_files calculation in top Makefile") because now we must check the presence of targets. Signed-off-by: Masahiro Yamada <[email protected]>
1 parent ce2fd53 commit 392885e

File tree

3 files changed

+16
-19
lines changed

3 files changed

+16
-19
lines changed

Makefile

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,6 +1043,8 @@ ifdef CONFIG_GDB_SCRIPTS
10431043
endif
10441044
+$(call if_changed,link-vmlinux)
10451045

1046+
targets := vmlinux
1047+
10461048
# Build samples along the rest of the kernel. This needs headers_install.
10471049
ifdef CONFIG_SAMPLES
10481050
vmlinux-dirs += samples
@@ -1758,13 +1760,12 @@ quiet_cmd_depmod = DEPMOD $(KERNELRELEASE)
17581760
cmd_crmodverdir = $(Q)mkdir -p $(MODVERDIR) \
17591761
$(if $(KBUILD_MODULES),; rm -f $(MODVERDIR)/*)
17601762

1761-
# read all saved command lines
1762-
cmd_files := $(wildcard .*.cmd)
1763+
# read saved command lines for existing targets
1764+
existing-targets := $(wildcard $(sort $(targets)))
17631765

1764-
ifneq ($(cmd_files),)
1765-
$(cmd_files): ; # Do not try to update included dependency files
1766-
include $(cmd_files)
1767-
endif
1766+
cmd_files := $(foreach f,$(existing-targets),$(dir $(f)).$(notdir $(f)).cmd)
1767+
$(cmd_files): ; # Do not try to update included dependency files
1768+
-include $(cmd_files)
17681769

17691770
endif # ifeq ($(config-targets),1)
17701771
endif # ifeq ($(mixed-targets),1)

scripts/Kbuild.include

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -264,9 +264,8 @@ ifndef CONFIG_TRIM_UNUSED_KSYMS
264264

265265
cmd_and_fixdep = \
266266
$(echo-cmd) $(cmd_$(1)); \
267-
scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(dot-target).tmp;\
268-
rm -f $(depfile); \
269-
mv -f $(dot-target).tmp $(dot-target).cmd;
267+
scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(dot-target).cmd;\
268+
rm -f $(depfile);
270269

271270
else
272271

@@ -289,9 +288,8 @@ cmd_and_fixdep = \
289288
$(echo-cmd) $(cmd_$(1)); \
290289
$(ksym_dep_filter) | \
291290
scripts/basic/fixdep -e $(depfile) $@ '$(make-cmd)' \
292-
> $(dot-target).tmp; \
293-
rm -f $(depfile); \
294-
mv -f $(dot-target).tmp $(dot-target).cmd;
291+
> $(dot-target).cmd; \
292+
rm -f $(depfile);
295293

296294
endif
297295

scripts/Makefile.build

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -529,18 +529,16 @@ FORCE:
529529
# optimization, we don't need to read them if the target does not
530530
# exist, we will rebuild anyway in that case.
531531

532-
cmd_files := $(wildcard $(foreach f,$(sort $(targets)),$(dir $(f)).$(notdir $(f)).cmd))
532+
existing-targets := $(wildcard $(sort $(targets)))
533533

534-
ifneq ($(cmd_files),)
535-
include $(cmd_files)
536-
endif
534+
-include $(foreach f,$(existing-targets),$(dir $(f)).$(notdir $(f)).cmd)
537535

538536
ifneq ($(KBUILD_SRC),)
539537
# Create directories for object files if they do not exist
540538
obj-dirs := $(sort $(obj) $(patsubst %/,%, $(dir $(targets))))
541-
# If cmd_files exist, their directories apparently exist. Skip mkdir.
542-
exist-dirs := $(sort $(patsubst %/,%, $(dir $(cmd_files))))
543-
obj-dirs := $(strip $(filter-out $(exist-dirs), $(obj-dirs)))
539+
# If targets exist, their directories apparently exist. Skip mkdir.
540+
existing-dirs := $(sort $(patsubst %/,%, $(dir $(existing-targets))))
541+
obj-dirs := $(strip $(filter-out $(existing-dirs), $(obj-dirs)))
544542
ifneq ($(obj-dirs),)
545543
$(shell mkdir -p $(obj-dirs))
546544
endif

0 commit comments

Comments
 (0)