Skip to content

Commit 48f1f05

Browse files
Sam RavnborgSam Ravnborg
authored andcommitted
kbuild: consistently decide when to rebuild a target
Consistently decide when to rebuild a target across all of if_changed, if_changed_dep, if_changed_rule. PHONY targets are now treated alike (ignored) for all targets While add it make Kbuild.include almost readable by factoring out a few bits to some common variables and reuse this in Makefile.build. Signed-off-by: Sam Ravnborg <sam@ravnborg.org>
1 parent d3660a8 commit 48f1f05

3 files changed

Lines changed: 35 additions & 24 deletions

File tree

‎scripts/Kbuild.include‎

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@ squote := '
77
empty :=
88
space := $(empty) $(empty)
99

10+
###
11+
# Name of target with a '.' as filename prefix. foo/bar.o => foo/.bar.o
12+
dot-target = $(dir $@).$(notdir $@)
13+
1014
###
1115
# The temporary file to save gcc -MD generated dependencies must not
1216
# contain a comma
13-
depfile = $(subst $(comma),_,$(@D)/.$(@F).d)
17+
depfile = $(subst $(comma),_,$(dot-target).d)
1418

1519
###
1620
# filename of target with directory and extension stripped
@@ -119,40 +123,44 @@ objectify = $(foreach o,$(1),$(if $(filter /%,$(o)),$(o),$(obj)/$(o)))
119123
ifneq ($(KBUILD_NOCMDDEP),1)
120124
# Check if both arguments has same arguments. Result in empty string if equal
121125
# User may override this check using make KBUILD_NOCMDDEP=1
122-
arg-check = $(strip $(filter-out $(1), $(2)) $(filter-out $(2), $(1)) )
126+
arg-check = $(strip $(filter-out $(cmd_$(1)), $(cmd_$@)) \
127+
$(filter-out $(cmd_$@), $(cmd_$(1))) )
123128
endif
124129

125130
# echo command. Short version is $(quiet) equals quiet, otherwise full command
126131
echo-cmd = $(if $($(quiet)cmd_$(1)), \
127132
echo ' $(call escsq,$($(quiet)cmd_$(1)))';)
128133

134+
# >'< substitution is for echo to work,
135+
# >$< substitution to preserve $ when reloading .cmd file
136+
# note: when using inline perl scripts [perl -e '...$$t=1;...']
137+
# in $(cmd_xxx) double $$ your perl vars
129138
make-cmd = $(subst \#,\\\#,$(subst $$,$$$$,$(call escsq,$(cmd_$(1)))))
130139

131-
# function to only execute the passed command if necessary
132-
# >'< substitution is for echo to work, >$< substitution to preserve $ when reloading .cmd file
133-
# note: when using inline perl scripts [perl -e '...$$t=1;...'] in $(cmd_xxx) double $$ your perl vars
140+
# Find any prerequisites that is newer than target or that does not exist.
141+
# PHONY targets skipped in both cases.
142+
any-prereq = $(filter-out $(PHONY),$?) $(filter-out $(PHONY) $(wildcard $^),$^)
143+
144+
# Execute command if command has changed or prerequisitei(s) are updated
134145
#
135-
if_changed = $(if $(strip $(filter-out $(PHONY),$?) \
136-
$(call arg-check, $(cmd_$(1)), $(cmd_$@)) ), \
137-
@set -e; \
138-
$(echo-cmd) $(cmd_$(1)); \
139-
echo 'cmd_$@ := $(make-cmd)' > $(@D)/.$(@F).cmd)
146+
if_changed = $(if $(strip $(any-prereq) $(arg-check)), \
147+
@set -e; \
148+
$(echo-cmd) $(cmd_$(1)); \
149+
echo 'cmd_$@ := $(make-cmd)' > $(dot-target).cmd)
140150

141151
# execute the command and also postprocess generated .d dependencies
142152
# file
143-
if_changed_dep = $(if $(strip $(filter-out $(PHONY),$?) \
144-
$(filter-out FORCE $(wildcard $^),$^) \
145-
$(call arg-check, $(cmd_$(1)), $(cmd_$@)) ), \
146-
@set -e; \
147-
$(echo-cmd) $(cmd_$(1)); \
148-
scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(@D)/.$(@F).tmp; \
149-
rm -f $(depfile); \
150-
mv -f $(@D)/.$(@F).tmp $(@D)/.$(@F).cmd)
153+
if_changed_dep = $(if $(strip $(any-prereq) $(arg-check) ), \
154+
@set -e; \
155+
$(echo-cmd) $(cmd_$(1)); \
156+
scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(dot-target).tmp;\
157+
rm -f $(depfile); \
158+
mv -f $(dot-target).tmp $(dot-target).cmd)
151159

152160
# Usage: $(call if_changed_rule,foo)
153161
# will check if $(cmd_foo) changed, or any of the prequisites changed,
154162
# and if so will execute $(rule_foo)
155-
if_changed_rule = $(if $(strip $(filter-out $(PHONY),$?) \
156-
$(call arg-check, $(cmd_$(1)), $(cmd_$@)) ),\
157-
@set -e; \
158-
$(rule_$(1)))
163+
if_changed_rule = $(if $(strip $(any-prereq) $(arg-check) ), \
164+
@set -e; \
165+
$(rule_$(1)))
166+

‎scripts/Makefile.build‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,10 @@ define rule_cc_o_c
191191
$(call echo-cmd,checksrc) $(cmd_checksrc) \
192192
$(call echo-cmd,cc_o_c) $(cmd_cc_o_c); \
193193
$(cmd_modversions) \
194-
scripts/basic/fixdep $(depfile) $@ '$(call make-cmd,cc_o_c)' > $(@D)/.$(@F).tmp; \
194+
scripts/basic/fixdep $(depfile) $@ '$(call make-cmd,cc_o_c)' > \
195+
$(dot-target).tmp; \
195196
rm -f $(depfile); \
196-
mv -f $(@D)/.$(@F).tmp $(@D)/.$(@F).cmd
197+
mv -f $(dot-target).tmp $(dot-target).cmd
197198
endef
198199

199200
# Built-in and composite module parts

‎usr/Makefile‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#
44

55
klibcdirs:;
6+
PHONY += klibcdirs
7+
68

79
# Generate builtin.o based on initramfs_data.o
810
obj-y := initramfs_data.o

0 commit comments

Comments
 (0)