kbuild: Fix output of make kernelrelease (and others)

Commit 7ff525712acf "kbuild: fake the "Entering directory ..." message
more simply" changed the output of "make kernelrelease" such that the
kernel release version was not the last line printed. This broke various
tools that would find the kernel release with "make kernelrelease | tail -1".
One of those tools that broke was ktest.pl that resides in the kernel.

If the target of the make is "kernelrelease", "kernelversion" or even
"image_name", then do not print the "Leaving directory ..." message at the
end as that will break tools that expect the kernelrelease version to be
the last line outputted.

Fixes: 7ff525712acf "kbuild: fake the "Entering directory ..." message more simply"
Cc: Masahiro Yamada <yamada.m@jp.panasonic.com>
Cc: Peter Foley <pefoley2@pefoley.com>
Cc: Michal Marek <mmarek@suse.cz>
Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
diff --git a/Makefile b/Makefile
index 05d67af..25d51b3 100644
--- a/Makefile
+++ b/Makefile
@@ -14,6 +14,20 @@
 # (this increases performance and avoids hard-to-debug behaviour);
 MAKEFLAGS += -rR
 
+#
+# The targets "kernelrelease", "kernelversion" and "image_name" requires
+# printing just the result. No need to show "Entering/Leaving directory ..."
+# messages. Tools like ktest.pl require this.
+ifeq ("$(MAKECMDGOALS)", "kernelrelease")
+  MAKEFLAGS += --no-print-directory
+endif
+ifeq ("$(MAKECMDGOALS)", "kernelversion")
+  MAKEFLAGS += --no-print-directory
+endif
+ifeq ("$(MAKECMDGOALS)", "image_name")
+  MAKEFLAGS += --no-print-directory
+endif
+
 # Avoid funny character set dependencies
 unexport LC_ALL
 LC_COLLATE=C