Skip to contentSkip to conversation

kbuild: resolve symlinks for O= properly

PATCH Superseded Masahiro Yamada 2 participants 3 messages Dec 14, 2023 – Dec 15, 2023 Kbuild

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kbuild: resolve symlinks for O= properly

Currently, Kbuild follows the logical chain of directories for the O= option, just like 'cd' (or 'realpath --logical') does.

Example:

    $ mkdir -p /tmp/a /tmp/x/y
    $ ln -s /tmp/x/y /tmp/a/b
    $ realpath /tmp/a/b/..
    /tmp/x
    $ realpath --logical /tmp/a/b/..
    /tmp/a
    $ make O=/tmp/a/b/.. defconfig
    make[1]: Entering directory '/tmp/a'
      [snip]
    make[1]: Leaving directory '/tmp/a'

'make O=/tmp/a/b/.. defconfig' creates the kernel configuration in /tmp/a instead of /tmp/x despite the directory path /tmp/a/b/.. resolves to the physical directory path /tmp/x.

This is because Kbuild internally uses the 'cd ... && pwd' for the path resolution, but this behavior is not predictable for users. Additionally, it is not consistent with how the Kbuild handles the M= option or GNU Make works with 'make -C /tmp/a/b/..'.

Using the physical directory structure for the O= option seems more reasonable.

The comment says "expand a shell special character '~'", but it has already been expanded to the home directory in the command line.

---

 Makefile | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)
Makefile +4 -7
index 24fac1889997..a05f0f7c99e0 100644
@@ -190,14 +190,11 @@ ifeq ("$(origin O)", "command line")
 endif
 
 ifneq ($(KBUILD_OUTPUT),)
-# Make's built-in functions such as $(abspath ...), $(realpath ...) cannot
-# expand a shell special character '~'. We use a somewhat tedious way here.
-abs_objtree := $(shell mkdir -p $(KBUILD_OUTPUT) && cd $(KBUILD_OUTPUT) && pwd)
-$(if $(abs_objtree),, \
-     $(error failed to create output directory "$(KBUILD_OUTPUT)"))
-
+# $(realpath ...) gets empty if the path does not exist. Run 'mkdir -p' first.
+$(shell mkdir -p $(KBUILD_OUTPUT))
 # $(realpath ...) resolves symlinks
-abs_objtree := $(realpath $(abs_objtree))
+abs_objtree := $(realpath $(KBUILD_OUTPUT))
+$(if $(abs_objtree),,$(error failed to create output directory "$(KBUILD_OUTPUT)"))
 endif # ifneq ($(KBUILD_OUTPUT),)
 
 ifneq ($(words $(subst :, ,$(abs_srctree))), 1)
Signature · 1 line
2.40.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 25 lines · Masahiro Yamada

Currently, Kbuild follows the logical chain of directories for the O= option, just like 'cd' (or 'realpath --logical') does.

Example:

    $ mkdir -p /tmp/a /tmp/x/y
    $ ln -s /tmp/x/y /tmp/a/b
    $ realpath /tmp/a/b/..
    /tmp/x
    $ realpath --logical /tmp/a/b/..
    /tmp/a
    $ make O=/tmp/a/b/.. defconfig
    make[1]: Entering directory '/tmp/a'
      [snip]
    make[1]: Leaving directory '/tmp/a'

'make O=/tmp/a/b/.. defconfig' creates the kernel configuration in /tmp/a instead of /tmp/x despite the directory path /tmp/a/b/.. resolves to the physical directory path /tmp/x.

This is because Kbuild internally uses the 'cd ... && pwd' for the path resolution, but this behavior is not predictable for users. Additionally, it is not consistent with how the Kbuild handles the M= option or GNU Make works with 'make -C /tmp/a/b/..'.

Quoted 5 lines

Using the physical directory structure for the O= option seems more reasonable.

The comment says "expand a shell special character '~'", but it has already been expanded to the home directory in the command line.

I minor change in behaviour is that 'make O="~/..."' (=quoted '~) will not work any more. But I think this actually the way it should be. Thanks!

Quoted 32 lines

---

 Makefile | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)
Makefile +4 -7
index 24fac1889997..a05f0f7c99e0 100644
@@ -190,14 +190,11 @@ ifeq ("$(origin O)", "command line")
 endif
 
 ifneq ($(KBUILD_OUTPUT),)
-# Make's built-in functions such as $(abspath ...), $(realpath ...) cannot
-# expand a shell special character '~'. We use a somewhat tedious way here.
-abs_objtree := $(shell mkdir -p $(KBUILD_OUTPUT) && cd $(KBUILD_OUTPUT) && pwd)
-$(if $(abs_objtree),, \
-     $(error failed to create output directory "$(KBUILD_OUTPUT)"))
-
+# $(realpath ...) gets empty if the path does not exist. Run 'mkdir -p' first.
+$(shell mkdir -p $(KBUILD_OUTPUT))
 # $(realpath ...) resolves symlinks
-abs_objtree := $(realpath $(abs_objtree))
+abs_objtree := $(realpath $(KBUILD_OUTPUT))
+$(if $(abs_objtree),,$(error failed to create output directory "$(KBUILD_OUTPUT)"))
 endif # ifneq ($(KBUILD_OUTPUT),)
 
 ifneq ($(words $(subst :, ,$(abs_srctree))), 1)
Signature · 1 line
2.40.1
#2 lore raw
Quoted 30 lines · Nicolas Schier
Quoted 28 lines · Masahiro Yamada

Currently, Kbuild follows the logical chain of directories for the O= option, just like 'cd' (or 'realpath --logical') does.

Example:

    $ mkdir -p /tmp/a /tmp/x/y
    $ ln -s /tmp/x/y /tmp/a/b
    $ realpath /tmp/a/b/..
    /tmp/x
    $ realpath --logical /tmp/a/b/..
    /tmp/a
    $ make O=/tmp/a/b/.. defconfig
    make[1]: Entering directory '/tmp/a'
      [snip]
    make[1]: Leaving directory '/tmp/a'

'make O=/tmp/a/b/.. defconfig' creates the kernel configuration in /tmp/a instead of /tmp/x despite the directory path /tmp/a/b/.. resolves to the physical directory path /tmp/x.

This is because Kbuild internally uses the 'cd ... && pwd' for the path resolution, but this behavior is not predictable for users. Additionally, it is not consistent with how the Kbuild handles the M= option or GNU Make works with 'make -C /tmp/a/b/..'.

Using the physical directory structure for the O= option seems more reasonable.

Quoted 6 lines
Quoted 2 lines

The comment says "expand a shell special character '~'", but it has already been expanded to the home directory in the command line.

I minor change in behaviour is that 'make O="~/..."' (=quoted '~) will not work any more. But I think this actually the way it should be. Thanks!

A good catch. This bug should be fixed by quoting $(KBUILD_OUTPUT).

-$(shell mkdir -p $(KBUILD_OUTPUT))
+$(shell mkdir -p "$(KBUILD_OUTPUT)")

When ~ is quoted, it loses a special meaning.

masahiro@zoe:~$ echo ~
/home/masahiro
masahiro@zoe:~$ echo "~"
~

So, the right behaviour of 'make O="~/foo"' is
to create "~/foo" under the current directory.

If you try to test it, please be careful to
not delete your home directory.

(I did "rm -r ./~" just in case)

Quoted 35 lines
Quoted 32 lines

---

 Makefile | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)
Makefile +4 -8
index 24fac1889997..a05f0f7c99e0 100644
@@ -190,14 +190,11 @@ ifeq ("$(origin O)", "command line")
 endif
 
 ifneq ($(KBUILD_OUTPUT),)
-# Make's built-in functions such as $(abspath ...), $(realpath ...) cannot
-# expand a shell special character '~'. We use a somewhat tedious way here.
-abs_objtree := $(shell mkdir -p $(KBUILD_OUTPUT) && cd $(KBUILD_OUTPUT) && pwd)
-$(if $(abs_objtree),, \
-     $(error failed to create output directory "$(KBUILD_OUTPUT)"))
-
+# $(realpath ...) gets empty if the path does not exist. Run 'mkdir -p' first.
+$(shell mkdir -p $(KBUILD_OUTPUT))
 # $(realpath ...) resolves symlinks
-abs_objtree := $(realpath $(abs_objtree))
+abs_objtree := $(realpath $(KBUILD_OUTPUT))
+$(if $(abs_objtree),,$(error failed to create output directory "$(KBUILD_OUTPUT)"))
 endif # ifneq ($(KBUILD_OUTPUT),)
 
 ifneq ($(words $(subst :, ,$(abs_srctree))), 1)
--

2.40.1

Signature · 2 lines
Best Regards
Masahiro Yamada