Commit 48f1f0589dd09df6ea07d41c737db3218ad2cb79

Authored by Sam Ravnborg
Committed by Sam Ravnborg
1 parent d3660a8cbd

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>

Showing 3 changed files with 35 additions and 24 deletions Side-by-side Diff

scripts/Kbuild.include
... ... @@ -8,9 +8,13 @@
8 8 space := $(empty) $(empty)
9 9  
10 10 ###
  11 +# Name of target with a '.' as filename prefix. foo/bar.o => foo/.bar.o
  12 +dot-target = $(dir $@).$(notdir $@)
  13 +
  14 +###
11 15 # The temporary file to save gcc -MD generated dependencies must not
12 16 # contain a comma
13   -depfile = $(subst $(comma),_,$(@D)/.$(@F).d)
  17 +depfile = $(subst $(comma),_,$(dot-target).d)
14 18  
15 19 ###
16 20 # filename of target with directory and extension stripped
17 21  
18 22  
19 23  
20 24  
21 25  
... ... @@ -119,41 +123,44 @@
119 123 ifneq ($(KBUILD_NOCMDDEP),1)
120 124 # Check if both arguments has same arguments. Result in empty string if equal
121 125 # 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))) )
123 128 endif
124 129  
125 130 # echo command. Short version is $(quiet) equals quiet, otherwise full command
126 131 echo-cmd = $(if $($(quiet)cmd_$(1)), \
127 132 echo ' $(call escsq,$($(quiet)cmd_$(1)))';)
128 133  
  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
129 138 make-cmd = $(subst \#,\\\#,$(subst $$,$$$$,$(call escsq,$(cmd_$(1)))))
130 139  
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
134 145 #
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)
140 150  
141 151 # execute the command and also postprocess generated .d dependencies
142 152 # 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)
151 159  
152 160 # Usage: $(call if_changed_rule,foo)
153 161 # will check if $(cmd_foo) changed, or any of the prequisites changed,
154 162 # 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)))
scripts/Makefile.build
... ... @@ -191,9 +191,10 @@
191 191 $(call echo-cmd,checksrc) $(cmd_checksrc) \
192 192 $(call echo-cmd,cc_o_c) $(cmd_cc_o_c); \
193 193 $(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; \
195 196 rm -f $(depfile); \
196   - mv -f $(@D)/.$(@F).tmp $(@D)/.$(@F).cmd
  197 + mv -f $(dot-target).tmp $(dot-target).cmd
197 198 endef
198 199  
199 200 # Built-in and composite module parts
... ... @@ -3,6 +3,8 @@
3 3 #
4 4  
5 5 klibcdirs:;
  6 +PHONY += klibcdirs
  7 +
6 8  
7 9 # Generate builtin.o based on initramfs_data.o
8 10 obj-y := initramfs_data.o