Skip to contentSkip to conversation

PATCH Milton Miller 2 participants 4 messages Jul 4, 2006 – Jul 5, 2006 Kbuild

lore
Share & tools
lore
#0 lore raw

Al wanted to see why Kbuild wanted to build files, to help debug Makefiles or dependency chains.

Here is a first draft. It always does if_changed_dep $^ processing,
and it has a few whitepace issues, but the paths other than not in targets passes a basic test.

export KBUILLD_PRINTDEPS=1 and you wil get lines beginning with DEPS:

scripts/Kbuild.include renamed from scripts/Kbuild.include.orig +15 -3
@@ -112,6 +112,18 @@ ifneq ($(KBUILD_NOCMDDEP),1)
 arg-check = $(strip $(filter-out $(1), $(2)) $(filter-out $(2), $(1)) )
 endif
 
+ifeq ($(KBUILD_PRINTDEPS),1)
+deps-cmd = $(if 1,echo 'DEPS: $@ built because' 			\
+	'$(call escsq,$(if $(wildcard $@),				\
+		$(if $(filter $@, $(targets)),				\
+			$(if $(call arg-check, $(cmd_$(1)), $(cmd_$@)),	\
+				command $(if $(cmd_$@),changed,was missing) \
+				,of $(sort $(filter-out $(PHONY),$?) 	\
+				$(filter-out FORCE $(wildcard $^),$^)))	\
+		,it was not a target)					\
+	,it was missing))';)
+endif
+
 # echo command. Short version is $(quiet) equals quiet, otherwise full command
 echo-cmd = $(if $($(quiet)cmd_$(1)), \
 	echo '  $(call escsq,$($(quiet)cmd_$(1)))';)
@@ -125,7 +137,7 @@ make-cmd = $(subst \#,\\\#,$(subst $$,$$
 if_changed = $(if $(strip $(filter-out $(PHONY),$?)          \
 		$(call arg-check, $(cmd_$(1)), $(cmd_$@)) ), \
 	@set -e; \
-	$(echo-cmd) $(cmd_$(1)); \
+	$(deps-cmd) $(echo-cmd) $(cmd_$(1)); \
 	echo 'cmd_$@ := $(make-cmd)' > $(@D)/.$(@F).cmd)
 
 # execute the command and also postprocess generated .d dependencies
@@ -134,7 +146,7 @@ if_changed_dep = $(if $(strip $(filter-o
 		$(filter-out FORCE $(wildcard $^),$^)    \
 	$(call arg-check, $(cmd_$(1)), $(cmd_$@)) ),     \
 	@set -e; \
-	$(echo-cmd) $(cmd_$(1)); \
+	$(deps-cmd) $(echo-cmd) $(cmd_$(1)); \
 	scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(@D)/.$(@F).tmp; \
 	rm -f $(depfile); \
 	mv -f $(@D)/.$(@F).tmp $(@D)/.$(@F).cmd)
@@ -145,4 +157,4 @@ if_changed_dep = $(if $(strip $(filter-o
 if_changed_rule = $(if $(strip $(filter-out $(PHONY),$?)            \
 			$(call arg-check, $(cmd_$(1)), $(cmd_$@)) ),\
 			@set -e; \
-			$(rule_$(1)))
+			$(deps-cmd) $(rule_$(1)))
#1 lore raw

[KBUILD] optionally print cause of rebuild (#2)

Al wanted to see why Kbuild wanted to build files, to help debug Makefiles or dependency chains.

Here is my second draft, with with whitespace handled by strip. It also now considers cmd_$@ before the targets variable because its both what the rules do and because the toplevel doesn't use targets. It still always prints the non-existing $^ stuff that only if_changed_dep uses.

Set KBUILLD_PRINTDEPS=1 and you will get lines like
DEPS: building bar/foo.o because command changed
DEPS: building bar/foo.o because command was missing
DEPS: building bar/foo.o because it was not a target
DEPS: building bar/foo.o because it was missing
DEPS: building bar/foo.o because of bar/foo.c include/linux/baz.h

scripts/Kbuild.include renamed from scripts/Kbuild.include.orig +16 -3
@@ -112,6 +112,19 @@ ifneq ($(KBUILD_NOCMDDEP),1)
 arg-check = $(strip $(filter-out $(1), $(2)) $(filter-out $(2), $(1)) )
 endif
 
+ifeq ($(KBUILD_PRINTDEPS),1)
+deps-cmd = $(if 1,echo 'DEPS: building $@ because' 			\
+	'$(call escsq,$(strip $(if $(wildcard $@),			\
+		$(if $(call arg-check, $(cmd_$(1)), $(cmd_$@)),		\
+			$(if $(cmd_$@),command changed			\
+				,$(if $(filter $@, $(targets)),		\
+					cmd was missing			\
+					,it was not a target))		\
+			,of $(sort $(filter-out $(PHONY),$?) 		\
+				$(filter-out FORCE $(wildcard $^),$^)))	\
+	,it was missing)))';)
+endif
+
 # echo command. Short version is $(quiet) equals quiet, otherwise full command
 echo-cmd = $(if $($(quiet)cmd_$(1)), \
 	echo '  $(call escsq,$($(quiet)cmd_$(1)))';)
@@ -125,7 +138,7 @@ make-cmd = $(subst \#,\\\#,$(subst $$,$$
 if_changed = $(if $(strip $(filter-out $(PHONY),$?)          \
 		$(call arg-check, $(cmd_$(1)), $(cmd_$@)) ), \
 	@set -e; \
-	$(echo-cmd) $(cmd_$(1)); \
+	$(deps-cmd) $(echo-cmd) $(cmd_$(1)); \
 	echo 'cmd_$@ := $(make-cmd)' > $(@D)/.$(@F).cmd)
 
 # execute the command and also postprocess generated .d dependencies
@@ -134,7 +147,7 @@ if_changed_dep = $(if $(strip $(filter-o
 		$(filter-out FORCE $(wildcard $^),$^)    \
 	$(call arg-check, $(cmd_$(1)), $(cmd_$@)) ),     \
 	@set -e; \
-	$(echo-cmd) $(cmd_$(1)); \
+	$(deps-cmd) $(echo-cmd) $(cmd_$(1)); \
 	scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(@D)/.$(@F).tmp; \
 	rm -f $(depfile); \
 	mv -f $(@D)/.$(@F).tmp $(@D)/.$(@F).cmd)
@@ -145,4 +158,4 @@ if_changed_dep = $(if $(strip $(filter-o
 if_changed_rule = $(if $(strip $(filter-out $(PHONY),$?)            \
 			$(call arg-check, $(cmd_$(1)), $(cmd_$@)) ),\
 			@set -e; \
-			$(rule_$(1)))
+			$(deps-cmd) $(rule_$(1)))
#2 lore raw

Re: [KBUILD] optionally print cause of rebuild (#2)

Hi Milton.

Quoted 8 lines · Milton Miller

Al wanted to see why Kbuild wanted to build files, to help debug Makefiles or dependency chains.

Here is my second draft, with with whitespace handled by strip. It also now considers cmd_$@ before the targets variable because its both what the rules do and because the toplevel doesn't use targets. It still always prints the non-existing $^ stuff that only if_changed_dep uses.

I like the idea and I have played around with it a bit before. To keep noise level down the "why got it rebuild" info should be on same line as the CC command and to trigger it increasing verbose level to 2 seems more natural.

Following patch does this.
The patch contains a few clean-ups to make the "why" part slimmer.

Comments?

Sam

scripts/Kbuild.include +46 -9
index 2180c88..4e5f0aa 100644
@@ -113,30 +113,33 @@ # See Documentation/kbuild/makefiles.txt
 ifneq ($(KBUILD_NOCMDDEP),1)
 # Check if both arguments has same arguments. Result in empty string if equal
 # User may override this check using make KBUILD_NOCMDDEP=1
-arg-check = $(strip $(filter-out $(1), $(2)) $(filter-out $(2), $(1)) )
+arg-check = $(strip $(filter-out $(cmd_$(1)), $(cmd_$@)) \
+                    $(filter-out $(cmd_$@),   $(cmd_$(1))) )
 endif
 
 # echo command. Short version is $(quiet) equals quiet, otherwise full command
 echo-cmd = $(if $($(quiet)cmd_$(1)), \
-	echo '  $(call escsq,$($(quiet)cmd_$(1)))';)
+	echo '  $(call escsq,$($(quiet)cmd_$(1)))$(echo-why)';)
 
 make-cmd = $(subst \#,\\\#,$(subst $$,$$$$,$(call escsq,$(cmd_$(1)))))
 
+# Any prerequisites that require target to be built - phony prereq's skipped
+get-prereq = $(filter-out $(PHONY),$?)
+
 # function to only execute the passed command if necessary
 # >'< substitution is for echo to work, >$< substitution to preserve $ when reloading .cmd file
 # note: when using inline perl scripts [perl -e '...$$t=1;...'] in $(cmd_xxx) double $$ your perl vars
 #
-if_changed = $(if $(strip $(filter-out $(PHONY),$?)          \
-		$(call arg-check, $(cmd_$(1)), $(cmd_$@)) ), \
+if_changed = $(if $(strip $(get-prereq) $(arg-check) ), \
 	@set -e; \
 	$(echo-cmd) $(cmd_$(1)); \
 	echo 'cmd_$@ := $(make-cmd)' > $(@D)/.$(@F).cmd)
 
 # execute the command and also postprocess generated .d dependencies
 # file
-if_changed_dep = $(if $(strip $(filter-out $(PHONY),$?)  \
+if_changed_dep = $(if $(strip $(get-prereq)              \
 		$(filter-out FORCE $(wildcard $^),$^)    \
-	$(call arg-check, $(cmd_$(1)), $(cmd_$@)) ),     \
+	$(arg-check) ), \
 	@set -e; \
 	$(echo-cmd) $(cmd_$(1)); \
 	scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(@D)/.$(@F).tmp; \
@@ -146,7 +149,41 @@ if_changed_dep = $(if $(strip $(filter-o
 # Usage: $(call if_changed_rule,foo)
 # will check if $(cmd_foo) changed, or any of the prequisites changed,
 # and if so will execute $(rule_foo)
-if_changed_rule = $(if $(strip $(filter-out $(PHONY),$?)            \
-			$(call arg-check, $(cmd_$(1)), $(cmd_$@)) ),\
-			@set -e; \
+if_changed_rule = $(if $(strip $(filter-out $(PHONY),$?)        \
+			$(arg-check) ), @set -e; \
 			$(rule_$(1)))
+
+###
+# why - tell why a a target got build
+#       enabled by make V=2
+#       Output:
+#          (1) due to: command line change
+#          (2) due to: file1.h file2.h
+#          (3) due to: no .cmd file
+#          (4) due to: target not in $(targets)
+#          (5) due to: target missing
+# (1) The command line stored in the file named dir/.target.cmd
+#     differed from actual command line. This happens when compiler
+#     options changes
+# (2) Prerequisite is newer than target
+# (3) No dir/.target.cmd file (used to store command line)
+# (4) No dir/.target.cmd file and target not listed in $(targets)
+#     This is a good hint that there is a bug in the kbuild file
+# (5) No target, so we better build it
+ifeq ($(KBUILD_VERBOSE),2)
+why =                                                            \
+    $(if $(wildcard $@),                                         \
+        $(if $(strip $(get-prereq)),- due to: $(get-prereq),     \
+            $(if $(arg-check),                                   \
+	        $(if $(cmd_$@),- due to: command line change,    \
+		    $(if $(filter $@, $(targets)),               \
+		        - due to: no .cmd file,                  \
+			-due to: $(notdir $@) not in $$(targets) \
+                     )                                           \
+                 )                                               \
+	     )                                                   \
+	 ),                                                      \
+    - due to: target missing                                     \
+     )
+echo-why = $(call escsq, $(strip $(why)))
+endif
#3 lore raw

Re: [KBUILD] optionally print cause of rebuild (#2)

Quoted 10 lines · Sam Ravnborg

Hi Milton.

Quoted 2 lines · Milton Miller

Al wanted to see why Kbuild wanted to build files, to help debug Makefiles or dependency chains.

I like the idea and I have played around with it a bit before. To keep noise level down the "why got it rebuild" info should be on same line as the CC command and to trigger it increasing verbose level to 2 seems more natural.

Quoted 4 lines

Following patch does this.
The patch contains a few clean-ups to make the "why" part slimmer.

Comments?

You gave priority of dependent files while I gave priority to command
change. This is a judgment call, both are actually correct, but I
chose command change because changing the config will always cause the built-in.o to be rebuilt even if you turned something back on that you had off for a while.

You removed the subtle marker ('of') that said "here is a list of files" vs some descriptive text (cmd change, etc); that means machine parsing scripts have to recognize all the strings in why.

Printing it with the command like this is more compact, as long as we
enforce the rule that quiet_xxx_cmd will be of the format TAG file to
allow machine parsing. However, it does mean that for things built with a rule, one has to figure out why the command was invoked.

For example, mine printed
DEPS: building .tmp_vmlinux1 because of drivers/built-in.o

  GEN     .version
  CHK     include/linux/compile.h
  UPD     include/linux/compile.h

DEPS: building init/version.o because of include/linux/compile.h
CC init/version.o
DEPS: building init/built-in.o because of init/version.o
LD init/built-in.o
LD .tmp_vmlinux1

while yours does

  GEN     .version - due to: drivers/built-in.o
  CHK     include/linux/compile.h
  UPD     include/linux/compile.h
  CC      init/version.o - due to: include/linux/compile.h
  LD      init/built-in.o - due to: init/version.o
  LD      .tmp_vmlinux1 - due to: drivers/built-in.o

One might wonder how .version depends on drivers/built-in.o, and why tmp_vmlinux1 wasn't built for init/built-in.o.

We should add this to make help.

milton