Skip to content

Commit 3a2429e

Browse files
committed
kbuild: change if_changed_rule for multi-line recipe
The 'define' ... 'endef' directive is useful to confine a series of shell commands into a single macro: define foo [action1] [action2] [action3] endif Each action is executed in a separate subshell. However, rule_cc_o_c and rule_as_o_S in scripts/Makefile.build are written as follows (with a trailing semicolon in each cmd_*): define rule_cc_o_c [action1] ; \ [action2] ; \ [action3] ; endef All shell commands are concatenated with '; \' so that it looks like a single command from the Makefile point of view. This does not exploit the benefits of 'define' ... 'endef' form because a single shell command can be more simply written, like this: rule_cc_o_c = \ [action1] ; \ [action2] ; \ [action3] ; I guess the intention for the command concatenation was to let the '@set -e' in if_changed_rule cover all the commands. We can improve the readability by moving '@set -e' to the 'cmd' macro. The combo of $(call echo-cmd,*) $(cmd_*) in rule_cc_o_c and rule_as_o_S have been replaced with $(call cmd,*). The trailing back-slashes have been removed. Here is a note about the performance: the commands in rule_cc_o_c and rule_as_o_S were previously executed all together in a single subshell, but now each line in a separate subshell. This means Make will spawn extra subshells [1]. I measured the build performance for x86_64_defconfig + CONFIG_MODVERSIONS + CONFIG_TRIM_UNUSED_KSYMS and I saw slight performance regression, but I believe code readability and maintainability wins. [1] Precisely, GNU Make may optimize this by executing the command directly instead of forking a subshell, if no shell special characters are found in the command line and omitting the subshell will not change the behavior. Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
1 parent bbda5ec commit 3a2429e

File tree

2 files changed

+15
-19
lines changed

2 files changed

+15
-19
lines changed

scripts/Kbuild.include

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ echo-cmd = $(if $($(quiet)cmd_$(1)),\
215215
echo ' $(call escsq,$($(quiet)cmd_$(1)))$(echo-why)';)
216216

217217
# printing commands
218-
cmd = @$(echo-cmd) $(cmd_$(1))
218+
cmd = @set -e; $(echo-cmd) $(cmd_$(1))
219219

220220
# Add $(obj)/ for paths that are not absolute
221221
objectify = $(foreach o,$(1),$(if $(filter /%,$(o)),$(o),$(obj)/$(o)))
@@ -256,21 +256,17 @@ if_changed = $(if $(strip $(any-prereq) $(arg-check)), \
256256
printf '%s\n' 'cmd_$@ := $(make-cmd)' > $(dot-target).cmd, @:)
257257

258258
# Execute the command and also postprocess generated .d dependencies file.
259-
if_changed_dep = $(if $(strip $(any-prereq) $(arg-check) ), \
260-
@set -e; \
261-
$(cmd_and_fixdep), @:)
259+
if_changed_dep = $(if $(strip $(any-prereq) $(arg-check)),$(cmd_and_fixdep),@:)
262260

263261
cmd_and_fixdep = \
264-
$(echo-cmd) $(cmd_$(1)); \
262+
$(cmd); \
265263
scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(dot-target).cmd;\
266264
rm -f $(depfile);
267265

268266
# Usage: $(call if_changed_rule,foo)
269267
# Will check if $(cmd_foo) or any of the prerequisites changed,
270268
# and if so will execute $(rule_foo).
271-
if_changed_rule = $(if $(strip $(any-prereq) $(arg-check) ), \
272-
@set -e; \
273-
$(rule_$(1)), @:)
269+
if_changed_rule = $(if $(strip $(any-prereq) $(arg-check)),$(rule_$(1)),@:)
274270

275271
###
276272
# why - tell why a target got built

scripts/Makefile.build

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -260,20 +260,20 @@ cmd_gen_ksymdeps = \
260260
endif
261261

262262
define rule_cc_o_c
263-
$(call echo-cmd,checksrc) $(cmd_checksrc) \
264-
$(call cmd_and_fixdep,cc_o_c) \
265-
$(cmd_gen_ksymdeps) \
266-
$(cmd_checkdoc) \
267-
$(call echo-cmd,objtool) $(cmd_objtool) \
268-
$(cmd_modversions_c) \
269-
$(call echo-cmd,record_mcount) $(cmd_record_mcount)
263+
$(call cmd,checksrc)
264+
$(call cmd_and_fixdep,cc_o_c)
265+
$(call cmd,gen_ksymdeps)
266+
$(call cmd,checkdoc)
267+
$(call cmd,objtool)
268+
$(call cmd,modversions_c)
269+
$(call cmd,record_mcount)
270270
endef
271271

272272
define rule_as_o_S
273-
$(call cmd_and_fixdep,as_o_S) \
274-
$(cmd_gen_ksymdeps) \
275-
$(call echo-cmd,objtool) $(cmd_objtool) \
276-
$(cmd_modversions_S)
273+
$(call cmd_and_fixdep,as_o_S)
274+
$(call cmd,gen_ksymdeps)
275+
$(call cmd,objtool)
276+
$(call cmd,modversions_S)
277277
endef
278278

279279
# List module undefined symbols (or empty line if not enabled)

0 commit comments

Comments
 (0)