diff mbox series

kbuild: revive "Entering directory" for Make >= 4.4.1

Message ID 20230610161711.1094231-1-masahiroy@kernel.org (mailing list archive)
State New, archived
Headers show
Series kbuild: revive "Entering directory" for Make >= 4.4.1 | expand

Commit Message

Masahiro Yamada June 10, 2023, 4:17 p.m. UTC
With commit 9da0763bdd82 ("kbuild: Use relative path when building in
a subdir of the source tree"), compiler messages in out-of-tree builds
include relative paths, which are relative to the build directory, not
the directory where make was started.

To help IDEs/editors find the source files, Kbuild lets GNU Make print
"Entering directory ..." when it changes the working directory. It has
been working fine for a long time, but David reported it is broken with
GNU Make 4.4.1.

The behavior was changed by GNU Make commit 8f9e7722ff0f ("[SV 63537]
Fix setting -w in makefiles"). Previously, setting --no-print-directory
to MAKEFLAGS only affected child makes, but it is now interpreted in
the current make as soon as it is set.

[test code]

  $ cat /tmp/Makefile
  MAKEFLAGS += --no-print-directory
  all: ; :

[before 8f9e7722ff0f]

  $ make -C /tmp
  make: Entering directory '/tmp'
  :
  make: Leaving directory '/tmp'

[after 8f9e7722ff0f]

  $ make -C /tmp
  :

This commit restores the previous behavior for GNU Make >= 4.4.1.

Reported-by: David Howells <dhowells@redhat.com>
Closes: https://lore.kernel.org/all/2427604.1686237298@warthog.procyon.org.uk/
Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
---

 Makefile | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

Comments

Nicolas Schier June 14, 2023, 8:33 p.m. UTC | #1
On Sun 11 Jun 2023 01:17:11 +0900, Masahiro Yamada wrote:
> With commit 9da0763bdd82 ("kbuild: Use relative path when building in
> a subdir of the source tree"), compiler messages in out-of-tree builds
> include relative paths, which are relative to the build directory, not
> the directory where make was started.
> 
> To help IDEs/editors find the source files, Kbuild lets GNU Make print
> "Entering directory ..." when it changes the working directory. It has
> been working fine for a long time, but David reported it is broken with
> GNU Make 4.4.1.
> 
> The behavior was changed by GNU Make commit 8f9e7722ff0f ("[SV 63537]
> Fix setting -w in makefiles"). Previously, setting --no-print-directory
> to MAKEFLAGS only affected child makes, but it is now interpreted in
> the current make as soon as it is set.
> 
> [test code]
> 
>   $ cat /tmp/Makefile
>   MAKEFLAGS += --no-print-directory
>   all: ; :
> 
> [before 8f9e7722ff0f]
> 
>   $ make -C /tmp
>   make: Entering directory '/tmp'
>   :
>   make: Leaving directory '/tmp'
> 
> [after 8f9e7722ff0f]
> 
>   $ make -C /tmp
>   :
> 
> This commit restores the previous behavior for GNU Make >= 4.4.1.
> 
> Reported-by: David Howells <dhowells@redhat.com>
> Closes: https://lore.kernel.org/all/2427604.1686237298@warthog.procyon.org.uk/
> Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
> ---
> 
>  Makefile | 16 ++++++++++++++--
>  1 file changed, 14 insertions(+), 2 deletions(-)
> 
> diff --git a/Makefile b/Makefile
> index cc3fe09c4dec..9868186deb66 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -191,7 +191,7 @@ endif # ifneq ($(KBUILD_OUTPUT),)
>  
>  ifeq ($(abs_objtree),$(CURDIR))
>  # Suppress "Entering directory ..." unless we are changing the work directory.
> -MAKEFLAGS += --no-print-directory
> +no-print-directory := --no-print-directory
>  else
>  need-sub-make := 1
>  endif
> @@ -203,6 +203,15 @@ ifneq ($(words $(subst :, ,$(abs_srctree))), 1)
>  $(error source directory cannot contain spaces or colons)
>  endif
>  
> +ifneq ($(filter jobserver-fifo,$(.FEATURES)),) # test $(MAKE_VERSION) >= 4.4

In scripts/Kbuild.include, we use $(intcmp) to check against Make 
version >= 4.4, cp. commit fccb3d3eda8d ("kbuild: add 
test-{ge,gt,le,lt} macros", 2022-12-11).  Might it make sense to use 
the same test here too?

> +ifeq ($(filter 4.4,$(MAKE_VERSION)),)
> +# With GNU Make >= 4.4.1, a change in MAKEFLAGS takes effect as soon as it is
> +# set. Run __sub-make all the time so that we can pass --no-print-directory
> +# via the command line.
> +need-sub-make := 1
> +endif
> +endif
> +
>  ifneq ($(filter 3.%,$(MAKE_VERSION)),)
>  # 'MAKEFLAGS += -rR' does not immediately become effective for GNU Make 3.x
>  # We need to invoke sub-make to avoid implicit rules in the top Makefile.
> @@ -223,7 +232,8 @@ $(filter-out $(this-makefile), $(MAKECMDGOALS)) __all: __sub-make
>  
>  # Invoke a second make in the output directory, passing relevant variables
>  __sub-make:
> -	$(Q)$(MAKE) -C $(abs_objtree) -f $(abs_srctree)/Makefile $(MAKECMDGOALS)
> +	$(Q)$(MAKE) $(no-print-directory) -C $(abs_objtree) \
> +	-f $(abs_srctree)/Makefile $(MAKECMDGOALS)

I like that solution, thanks!

Reviewed-by: Nicolas Schier <nicolas@fjasle.eu>

>  
>  endif # need-sub-make
>  endif # sub_make_done
> @@ -234,6 +244,8 @@ ifeq ($(need-sub-make),)
>  # Do not print "Entering directory ...",
>  # but we want to display it when entering to the output directory
>  # so that IDEs/editors are able to understand relative filenames.
> +# This line is needed to allow Make < 4.4.1 to skip __sub-make.
> +# The newer Make versions runs __sub-make before seeing this line.
>  MAKEFLAGS += --no-print-directory
>  
>  ifeq ($(abs_srctree),$(abs_objtree))
> -- 
> 2.39.2

-- Nicolas Schier
Masahiro Yamada June 15, 2023, 2:16 p.m. UTC | #2
On Thu, Jun 15, 2023 at 5:34 AM Nicolas Schier <nicolas@fjasle.eu> wrote:
>
> On Sun 11 Jun 2023 01:17:11 +0900, Masahiro Yamada wrote:
> > With commit 9da0763bdd82 ("kbuild: Use relative path when building in
> > a subdir of the source tree"), compiler messages in out-of-tree builds
> > include relative paths, which are relative to the build directory, not
> > the directory where make was started.
> >
> > To help IDEs/editors find the source files, Kbuild lets GNU Make print
> > "Entering directory ..." when it changes the working directory. It has
> > been working fine for a long time, but David reported it is broken with
> > GNU Make 4.4.1.
> >
> > The behavior was changed by GNU Make commit 8f9e7722ff0f ("[SV 63537]
> > Fix setting -w in makefiles"). Previously, setting --no-print-directory
> > to MAKEFLAGS only affected child makes, but it is now interpreted in
> > the current make as soon as it is set.
> >
> > [test code]
> >
> >   $ cat /tmp/Makefile
> >   MAKEFLAGS += --no-print-directory
> >   all: ; :
> >
> > [before 8f9e7722ff0f]
> >
> >   $ make -C /tmp
> >   make: Entering directory '/tmp'
> >   :
> >   make: Leaving directory '/tmp'
> >
> > [after 8f9e7722ff0f]
> >
> >   $ make -C /tmp
> >   :
> >
> > This commit restores the previous behavior for GNU Make >= 4.4.1.
> >
> > Reported-by: David Howells <dhowells@redhat.com>
> > Closes: https://lore.kernel.org/all/2427604.1686237298@warthog.procyon.org.uk/
> > Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
> > ---


Sorry, I retract this patch.
It does not work.
diff mbox series

Patch

diff --git a/Makefile b/Makefile
index cc3fe09c4dec..9868186deb66 100644
--- a/Makefile
+++ b/Makefile
@@ -191,7 +191,7 @@  endif # ifneq ($(KBUILD_OUTPUT),)
 
 ifeq ($(abs_objtree),$(CURDIR))
 # Suppress "Entering directory ..." unless we are changing the work directory.
-MAKEFLAGS += --no-print-directory
+no-print-directory := --no-print-directory
 else
 need-sub-make := 1
 endif
@@ -203,6 +203,15 @@  ifneq ($(words $(subst :, ,$(abs_srctree))), 1)
 $(error source directory cannot contain spaces or colons)
 endif
 
+ifneq ($(filter jobserver-fifo,$(.FEATURES)),) # test $(MAKE_VERSION) >= 4.4
+ifeq ($(filter 4.4,$(MAKE_VERSION)),)
+# With GNU Make >= 4.4.1, a change in MAKEFLAGS takes effect as soon as it is
+# set. Run __sub-make all the time so that we can pass --no-print-directory
+# via the command line.
+need-sub-make := 1
+endif
+endif
+
 ifneq ($(filter 3.%,$(MAKE_VERSION)),)
 # 'MAKEFLAGS += -rR' does not immediately become effective for GNU Make 3.x
 # We need to invoke sub-make to avoid implicit rules in the top Makefile.
@@ -223,7 +232,8 @@  $(filter-out $(this-makefile), $(MAKECMDGOALS)) __all: __sub-make
 
 # Invoke a second make in the output directory, passing relevant variables
 __sub-make:
-	$(Q)$(MAKE) -C $(abs_objtree) -f $(abs_srctree)/Makefile $(MAKECMDGOALS)
+	$(Q)$(MAKE) $(no-print-directory) -C $(abs_objtree) \
+	-f $(abs_srctree)/Makefile $(MAKECMDGOALS)
 
 endif # need-sub-make
 endif # sub_make_done
@@ -234,6 +244,8 @@  ifeq ($(need-sub-make),)
 # Do not print "Entering directory ...",
 # but we want to display it when entering to the output directory
 # so that IDEs/editors are able to understand relative filenames.
+# This line is needed to allow Make < 4.4.1 to skip __sub-make.
+# The newer Make versions runs __sub-make before seeing this line.
 MAKEFLAGS += --no-print-directory
 
 ifeq ($(abs_srctree),$(abs_objtree))