Skip to contentSkip to conversation

gen_compile_commands: honor the build directory for modules

PATCH In review vngocvan 4 participants 8 messages Oct 15 – Oct 17 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore

vngocvan

author
#0 lore raw

PATCH 1/1 gen_compile_commands: honor the build directory for modules

When invoked from outside the build tree with -d and a modules.order argument, the script opens .mod files relative to the current directory instead of the specified build directory. This can fail with FileNotFoundError or collect commands from unrelated artifacts in the current directory.

Pass the build directory to cmdfiles_for_modorder() and resolve both the .mod files and their associated .cmd files relative to it. Absolute paths continue to work, and the output location is unchanged.

Fixes ecca4fea1ede ("gen_compile_commands: support *.o, *.a, modules.order in positional argument")
---
 scripts/clang-tools/gen_compile_commands.py | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
scripts/clang-tools/gen_compile_commands.py +5 -4
index 8d14b81efd73..149135f85f19 100755
@@ -124,7 +124,7 @@ def cmdfiles_for_a(archive, ar):
         yield to_cmdfile(obj)
 
 
-def cmdfiles_for_modorder(modorder):
+def cmdfiles_for_modorder(modorder, root_directory):
     """Generate the iterator of .cmd files associated with the modules.order.
 
     Parse the given modules.order, and yield every .cmd file used to build the
@@ -132,6 +132,7 @@ def cmdfiles_for_modorder(modorder):
 
     Args:
         modorder: The modules.order file to parse
+        root_directory: The directory where the objects were built
 
     Yields:
         The path to every .cmd file found
@@ -142,11 +143,11 @@ def cmdfiles_for_modorder(modorder):
             base, ext = os.path.splitext(obj)
             if ext != '.o':
                 sys.exit('{}: module path must end with .o'.format(obj))
-            mod = base + '.mod'
+            mod = os.path.join(root_directory, base + '.mod')
             # Read from *.mod, to get a list of objects that compose the module.
             with open(mod) as m:
                 for mod_line in m:
-                    yield to_cmdfile(mod_line.rstrip())
+                    yield os.path.join(root_directory, to_cmdfile(mod_line.rstrip()))
 
 
 def process_line(root_directory, command_prefix, file_path):
@@ -206,7 +207,7 @@ def main():
         elif path.endswith('.a'):
             cmdfiles = cmdfiles_for_a(path, ar)
         elif path.endswith('modules.order'):
-            cmdfiles = cmdfiles_for_modorder(path)
+            cmdfiles = cmdfiles_for_modorder(path, directory)
         else:
             sys.exit('{}: unknown file type'.format(path))
 

base-commit: 0fcb05b72344f40806cbe810de8872f1698bf053

Signature · 1 line
2.56.0
#1 lore raw
Quoted 39 lines · vngocvan

When invoked from outside the build tree with -d and a modules.order argument, the script opens .mod files relative to the current directory instead of the specified build directory. This can fail with FileNotFoundError or collect commands from unrelated artifacts in the current directory.

Pass the build directory to cmdfiles_for_modorder() and resolve both the .mod files and their associated .cmd files relative to it. Absolute paths continue to work, and the output location is unchanged.

Fixes ecca4fea1ede ("gen_compile_commands: support *.o, *.a, modules.order in positional argument")
---
 scripts/clang-tools/gen_compile_commands.py | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
scripts/clang-tools/gen_compile_commands.py +2 -1
index 8d14b81efd73..149135f85f19 100755
@@ -124,7 +124,7 @@ def cmdfiles_for_a(archive, ar):
         yield to_cmdfile(obj)
 
 
-def cmdfiles_for_modorder(modorder):
+def cmdfiles_for_modorder(modorder, root_directory):
     """Generate the iterator of .cmd files associated with the modules.order.
 
     Parse the given modules.order, and yield every .cmd file used to build the
@@ -132,6 +132,7 @@ def cmdfiles_for_modorder(modorder):
 
     Args:
         modorder: The modules.order file to parse
+        root_directory: The directory where the objects were built
 
     Yields:
         The path to every .cmd file found

scripts/clang-tools/gen_compile_commands.py def cmdfiles_for_modorder(modorder): · line 150

Quoted 10 lines
scripts/clang-tools/gen_compile_commands.py +2 -2
@@ -142,11 +143,11 @@ def cmdfiles_for_modorder(modorder):
             base, ext = os.path.splitext(obj)
             if ext != '.o':
                 sys.exit('{}: module path must end with .o'.format(obj))
-            mod = base + '.mod'
+            mod = os.path.join(root_directory, base + '.mod')
             # Read from *.mod, to get a list of objects that compose the module.
             with open(mod) as m:
                 for mod_line in m:
-                    yield to_cmdfile(mod_line.rstrip())
+                    yield os.path.join(root_directory, to_cmdfile(mod_line.rstrip()))

Thanks for the patch. Can we avoid os.path in favor of Path from Pathlib? Here and throughout.

Quoted 17 lines
scripts/clang-tools/gen_compile_commands.py +1 -1
 def process_line(root_directory, command_prefix, file_path):
@@ -206,7 +207,7 @@ def main():
         elif path.endswith('.a'):
             cmdfiles = cmdfiles_for_a(path, ar)
         elif path.endswith('modules.order'):
-            cmdfiles = cmdfiles_for_modorder(path)
+            cmdfiles = cmdfiles_for_modorder(path, directory)
         else:
             sys.exit('{}: unknown file type'.format(path))
 

base-commit: 0fcb05b72344f40806cbe810de8872f1698bf053 --
2.56.0

Signature · 2 lines
Thanks,
~Nick Desaulniers
#2 lore raw
Quoted 2 lines · Nick Desaulniers

Thanks for the patch. Can we avoid os.path in favor of Path from Pathlib? Here and throughout.

I would not mind seeing some modernization in this script such as os.path -> pathlib.Path but it should definitely be done as a separate patch.

Signature · 2 lines
Cheers,
Nathan
#3 lore raw
In reply to vngocvan

Hi,

Quoted 5 lines · vngocvan

When invoked from outside the build tree with -d and a modules.order argument, the script opens .mod files relative to the current directory instead of the specified build directory. This can fail with FileNotFoundError or collect commands from unrelated artifacts in the current directory.

How was this discovered? This cannot happen within Kbuild because this script is always invoked from within the build folder, so was it a manual invocation or something else?

Quoted 3 lines

Pass the build directory to cmdfiles_for_modorder() and resolve both the .mod files and their associated .cmd files relative to it. Absolute paths continue to work, and the output location is unchanged.

I think you can omit the final sentence here, it does not add much value in my opinion.

Quoted 2 lines
Fixes ecca4fea1ede ("gen_compile_commands: support *.o, *.a, modules.order in positional argument")

Please fix your name, this needs to be a known identity.

Quoted 53 lines
---
 scripts/clang-tools/gen_compile_commands.py | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
scripts/clang-tools/gen_compile_commands.py +5 -4
index 8d14b81efd73..149135f85f19 100755
@@ -124,7 +124,7 @@ def cmdfiles_for_a(archive, ar):
         yield to_cmdfile(obj)
 
 
-def cmdfiles_for_modorder(modorder):
+def cmdfiles_for_modorder(modorder, root_directory):
     """Generate the iterator of .cmd files associated with the modules.order.
 
     Parse the given modules.order, and yield every .cmd file used to build the
@@ -132,6 +132,7 @@ def cmdfiles_for_modorder(modorder):
 
     Args:
         modorder: The modules.order file to parse
+        root_directory: The directory where the objects were built
 
     Yields:
         The path to every .cmd file found
@@ -142,11 +143,11 @@ def cmdfiles_for_modorder(modorder):
             base, ext = os.path.splitext(obj)
             if ext != '.o':
                 sys.exit('{}: module path must end with .o'.format(obj))
-            mod = base + '.mod'
+            mod = os.path.join(root_directory, base + '.mod')
             # Read from *.mod, to get a list of objects that compose the module.
             with open(mod) as m:
                 for mod_line in m:
-                    yield to_cmdfile(mod_line.rstrip())
+                    yield os.path.join(root_directory, to_cmdfile(mod_line.rstrip()))
 
 
 def process_line(root_directory, command_prefix, file_path):
@@ -206,7 +207,7 @@ def main():
         elif path.endswith('.a'):
             cmdfiles = cmdfiles_for_a(path, ar)
         elif path.endswith('modules.order'):
-            cmdfiles = cmdfiles_for_modorder(path)
+            cmdfiles = cmdfiles_for_modorder(path, directory)
         else:
             sys.exit('{}: unknown file type'.format(path))
 

base-commit: 0fcb05b72344f40806cbe810de8872f1698bf053

Signature · 1 line
2.56.0
Signature · 2 lines
Cheers,
Nathan

A message that is not in the archive (sent off-list or not yet received): CAJavoFk+zgySyNps5tuKmntmvuAyev=kKL640TFrfCc-njeWBw@mail.gmail.com

author
#5 lore raw

Hi Nathan,

Quoted 1 line

How was this discovered?

This came from code inspection of standalone use of -d with an explicit modules.order argument, followed initially by a constructed reproducer. It was not a failure encountered through Kbuild's compile_commands.json target.

I have now reproduced it using real Kbuild-generated artifacts from 0fcb05b72344f40806cbe810de8872f1698bf053 on Linux/aarch64, with Python 3.11.13 and GCC 12.2.0. This was a partial lib/ build with CONFIG_TEST_LKM=m.

The following uses a disposable source tree at that base. The guards prevent overwriting an existing output directory or result file:

    (
        set -eu
        test ! -e out
        test ! -e outside.json
        test ! -e inside.json
        make O=out -j1 allnoconfig
        bash scripts/config --file out/.config --enable MODULES \
            --enable RUNTIME_TESTING_MENU --module TEST_LKM
        make O=out -j1 olddefconfig
        make O=out -j1 V=1 lib/

        outside_exit=0
        python3 -B scripts/clang-tools/gen_compile_commands.py -a ar \
            -d out -o outside.json out/lib/modules.order ||
            outside_exit=$?
        printf 'outside_exit=%s\n' "$outside_exit"

        inside_exit=0
        (cd out &&
         python3 -B ../scripts/clang-tools/gen_compile_commands.py -a ar \
             -o ../inside.json lib/modules.order) ||
            inside_exit=$?
        printf 'inside_exit=%s\n' "$inside_exit"
        test "$outside_exit" -eq 1
        test "$inside_exit" -eq 0
        python3 -c \
            'import json; assert len(json.load(open("inside.json"))) == 1'
    )

Kbuild generated lib/test_module.o, lib/test_module.mod, lib/.test_module.o.cmd and lib/modules.order under out/. Both .mod and modules.order contain lib/test_module.o.

The old script's invocation from the source directory fails with:

FileNotFoundError: [Errno 2] No such file or directory:
'lib/test_module.mod'
outside_exit=1

The same artifacts work when the old script is invoked from out/:

inside_exit=0

The successful invocation produces one JSON entry for lib/test_module.c. The patched script succeeds from the source directory and produces an identical entry. The old script also fails with absolute paths for -d and modules.order; the patched script succeeds with those paths too.

Only the standalone generator invocation is outside the build directory; compilation itself runs under out/. This is not a full kernel build or a boot/runtime test.

Quoted 2 lines

I think you can omit the final sentence here, it does not add much value in my opinion.

I'll drop that sentence and clarify the standalone scope in v2.

Quoted 1 line

Please fix your name, this needs to be a known identity.

I'll use Võ Ngọc Văn for the author and sign-off in v2.

Quoted 2 lines

os.path -> pathlib.Path but it should definitely be done as a separate patch.

I'll keep the pathlib conversion as a separate follow-up patch.

Thanks,
Võ Ngọc Văn

Thanks & best regards,

V

Quoted 170 lines · Van Vo

Hi Nathan,

Quoted 1 line

How was this discovered?

This came from code inspection of standalone use of -d with an explicit modules.order argument, followed initially by a constructed reproducer. It was not a failure encountered through Kbuild's compile_commands.json target.

I have now reproduced it using real Kbuild-generated artifacts from 0fcb05b72344f40806cbe810de8872f1698bf053 on Linux/aarch64, with Python 3.11.13 and GCC 12.2.0. This was a partial lib/ build with CONFIG_TEST_LKM=m.

The following uses a disposable source tree at that base. The guards prevent overwriting an existing output directory or result file:

    (
        set -eu
        test ! -e out
        test ! -e outside.json
        test ! -e inside.json
        make O=out -j1 allnoconfig
        bash scripts/config --file out/.config --enable MODULES \
            --enable RUNTIME_TESTING_MENU --module TEST_LKM
        make O=out -j1 olddefconfig
        make O=out -j1 V=1 lib/

        outside_exit=0
        python3 -B scripts/clang-tools/gen_compile_commands.py -a ar \
            -d out -o outside.json out/lib/modules.order ||
            outside_exit=$?
        printf 'outside_exit=%s\n' "$outside_exit"

        inside_exit=0
        (cd out &&
         python3 -B ../scripts/clang-tools/gen_compile_commands.py -a ar \
             -o ../inside.json lib/modules.order) ||
            inside_exit=$?
        printf 'inside_exit=%s\n' "$inside_exit"
        test "$outside_exit" -eq 1
        test "$inside_exit" -eq 0
        python3 -c \
            'import json; assert len(json.load(open("inside.json"))) == 1'
    )

Kbuild generated lib/test_module.o, lib/test_module.mod, lib/.test_module.o.cmd and lib/modules.order under out/. Both .mod and modules.order contain lib/test_module.o.

The old script's invocation from the source directory fails with:

FileNotFoundError: [Errno 2] No such file or directory: 'lib/test_module.mod' outside_exit=1

The same artifacts work when the old script is invoked from out/:

inside_exit=0

The successful invocation produces one JSON entry for lib/test_module.c. The patched script succeeds from the source directory and produces an identical entry. The old script also fails with absolute paths for -d and modules.order; the patched script succeeds with those paths too.

Only the standalone generator invocation is outside the build directory; compilation itself runs under out/. This is not a full kernel build or a boot/runtime test.

Quoted 2 lines

I think you can omit the final sentence here, it does not add much value in my opinion.

I'll drop that sentence and clarify the standalone scope in v2.

Quoted 1 line

Please fix your name, this needs to be a known identity.

I'll use Võ Ngọc Văn for the author and sign-off in v2.

Quoted 2 lines

os.path -> pathlib.Path but it should definitely be done as a separate patch.

I'll keep the pathlib conversion as a separate follow-up patch.

Thanks,
Võ Ngọc Văn

Quoted 83 lines · Nathan Chancellor

Hi,

Quoted 5 lines · vngocvan

When invoked from outside the build tree with -d and a modules.order argument, the script opens .mod files relative to the current directory instead of the specified build directory. This can fail with FileNotFoundError or collect commands from unrelated artifacts in the current directory.

How was this discovered? This cannot happen within Kbuild because this script is always invoked from within the build folder, so was it a manual invocation or something else?

Quoted 3 lines

Pass the build directory to cmdfiles_for_modorder() and resolve both the .mod files and their associated .cmd files relative to it. Absolute paths continue to work, and the output location is unchanged.

I think you can omit the final sentence here, it does not add much value in my opinion.

Quoted 2 lines
Fixes ecca4fea1ede ("gen_compile_commands: support *.o, *.a, modules.order in positional argument")

Please fix your name, this needs to be a known identity.

Quoted 49 lines
---
 scripts/clang-tools/gen_compile_commands.py | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
scripts/clang-tools/gen_compile_commands.py +5 -4
index 8d14b81efd73..149135f85f19 100755
@@ -124,7 +124,7 @@ def cmdfiles_for_a(archive, ar):
         yield to_cmdfile(obj)
 
 
-def cmdfiles_for_modorder(modorder):
+def cmdfiles_for_modorder(modorder, root_directory):
     """Generate the iterator of .cmd files associated with the modules.order.
 
     Parse the given modules.order, and yield every .cmd file used to build the
@@ -132,6 +132,7 @@ def cmdfiles_for_modorder(modorder):
 
     Args:
         modorder: The modules.order file to parse
+        root_directory: The directory where the objects were built
 
     Yields:
         The path to every .cmd file found
@@ -142,11 +143,11 @@ def cmdfiles_for_modorder(modorder):
             base, ext = os.path.splitext(obj)
             if ext != '.o':
                 sys.exit('{}: module path must end with .o'.format(obj))
-            mod = base + '.mod'
+            mod = os.path.join(root_directory, base + '.mod')
             # Read from *.mod, to get a list of objects that compose the module.
             with open(mod) as m:
                 for mod_line in m:
-                    yield to_cmdfile(mod_line.rstrip())
+                    yield os.path.join(root_directory, to_cmdfile(mod_line.rstrip()))
 
 
 def process_line(root_directory, command_prefix, file_path):
@@ -206,7 +207,7 @@ def main():
         elif path.endswith('.a'):
             cmdfiles = cmdfiles_for_a(path, ar)
         elif path.endswith('modules.order'):
-            cmdfiles = cmdfiles_for_modorder(path)
+            cmdfiles = cmdfiles_for_modorder(path, directory)
         else:
             sys.exit('{}: unknown file type'.format(path))
 
Quoted 4 lines

base-commit: 0fcb05b72344f40806cbe810de8872f1698bf053 --
2.56.0

--
Cheers,
Nathan

author
#6 lore raw
In reply to a message not in the archive

Hi Nathan,

Quoted 1 line

How was this discovered?

This came from code inspection of standalone use of -d with an explicit modules.order argument, followed initially by a constructed reproducer. It was not a failure encountered through Kbuild's compile_commands.json target.

I have now reproduced it using real Kbuild-generated artifacts from 0fcb05b72344f40806cbe810de8872f1698bf053 on Linux/aarch64, with Python 3.11.13 and GCC 12.2.0. This was a partial lib/ build with CONFIG_TEST_LKM=m.

The following uses a disposable source tree at that base. The guards prevent overwriting an existing output directory or result file:

    (
        set -eu
        test ! -e out
        test ! -e outside.json
        test ! -e inside.json
        make O=out -j1 allnoconfig
        bash scripts/config --file out/.config --enable MODULES \
            --enable RUNTIME_TESTING_MENU --module TEST_LKM
        make O=out -j1 olddefconfig
        make O=out -j1 V=1 lib/

        outside_exit=0
        python3 -B scripts/clang-tools/gen_compile_commands.py -a ar \
            -d out -o outside.json out/lib/modules.order ||
            outside_exit=$?
        printf 'outside_exit=%s\n' "$outside_exit"

        inside_exit=0
        (cd out &&
         python3 -B ../scripts/clang-tools/gen_compile_commands.py -a ar \
             -o ../inside.json lib/modules.order) ||
            inside_exit=$?
        printf 'inside_exit=%s\n' "$inside_exit"
        test "$outside_exit" -eq 1
        test "$inside_exit" -eq 0
        python3 -c \
            'import json; assert len(json.load(open("inside.json"))) == 1'
    )

Kbuild generated lib/test_module.o, lib/test_module.mod, lib/.test_module.o.cmd and lib/modules.order under out/. Both .mod and modules.order contain lib/test_module.o.

The old script's invocation from the source directory fails with:

FileNotFoundError: [Errno 2] No such file or directory:
'lib/test_module.mod'
outside_exit=1

The same artifacts work when the old script is invoked from out/:

inside_exit=0

The successful invocation produces one JSON entry for lib/test_module.c. The patched script succeeds from the source directory and produces an identical entry. The old script also fails with absolute paths for -d and modules.order; the patched script succeeds with those paths too.

Only the standalone generator invocation is outside the build directory; compilation itself runs under out/. This is not a full kernel build or a boot/runtime test.

Quoted 2 lines

I think you can omit the final sentence here, it does not add much value in my opinion.

I'll drop that sentence and clarify the standalone scope in v2.

Quoted 1 line

Please fix your name, this needs to be a known identity.

I'll use Võ Ngọc Văn for the author and sign-off in v2.

Quoted 2 lines

os.path -> pathlib.Path but it should definitely be done as a separate patch.

I'll keep the pathlib conversion as a separate follow-up patch.

Thanks,
Võ Ngọc Văn

Quoted 170 lines · Van Vo

Hi Nathan,

Quoted 1 line

How was this discovered?

This came from code inspection of standalone use of -d with an explicit modules.order argument, followed initially by a constructed reproducer. It was not a failure encountered through Kbuild's compile_commands.json target.

I have now reproduced it using real Kbuild-generated artifacts from 0fcb05b72344f40806cbe810de8872f1698bf053 on Linux/aarch64, with Python 3.11.13 and GCC 12.2.0. This was a partial lib/ build with CONFIG_TEST_LKM=m.

The following uses a disposable source tree at that base. The guards prevent overwriting an existing output directory or result file:

    (
        set -eu
        test ! -e out
        test ! -e outside.json
        test ! -e inside.json
        make O=out -j1 allnoconfig
        bash scripts/config --file out/.config --enable MODULES \
            --enable RUNTIME_TESTING_MENU --module TEST_LKM
        make O=out -j1 olddefconfig
        make O=out -j1 V=1 lib/

        outside_exit=0
        python3 -B scripts/clang-tools/gen_compile_commands.py -a ar \
            -d out -o outside.json out/lib/modules.order ||
            outside_exit=$?
        printf 'outside_exit=%s\n' "$outside_exit"

        inside_exit=0
        (cd out &&
         python3 -B ../scripts/clang-tools/gen_compile_commands.py -a ar \
             -o ../inside.json lib/modules.order) ||
            inside_exit=$?
        printf 'inside_exit=%s\n' "$inside_exit"
        test "$outside_exit" -eq 1
        test "$inside_exit" -eq 0
        python3 -c \
            'import json; assert len(json.load(open("inside.json"))) == 1'
    )

Kbuild generated lib/test_module.o, lib/test_module.mod, lib/.test_module.o.cmd and lib/modules.order under out/. Both .mod and modules.order contain lib/test_module.o.

The old script's invocation from the source directory fails with:

FileNotFoundError: [Errno 2] No such file or directory: 'lib/test_module.mod' outside_exit=1

The same artifacts work when the old script is invoked from out/:

inside_exit=0

The successful invocation produces one JSON entry for lib/test_module.c. The patched script succeeds from the source directory and produces an identical entry. The old script also fails with absolute paths for -d and modules.order; the patched script succeeds with those paths too.

Only the standalone generator invocation is outside the build directory; compilation itself runs under out/. This is not a full kernel build or a boot/runtime test.

Quoted 2 lines

I think you can omit the final sentence here, it does not add much value in my opinion.

I'll drop that sentence and clarify the standalone scope in v2.

Quoted 1 line

Please fix your name, this needs to be a known identity.

I'll use Võ Ngọc Văn for the author and sign-off in v2.

Quoted 2 lines

os.path -> pathlib.Path but it should definitely be done as a separate patch.

I'll keep the pathlib conversion as a separate follow-up patch.

Thanks,
Võ Ngọc Văn

Quoted 83 lines · Nathan Chancellor

Hi,

Quoted 5 lines · vngocvan

When invoked from outside the build tree with -d and a modules.order argument, the script opens .mod files relative to the current directory instead of the specified build directory. This can fail with FileNotFoundError or collect commands from unrelated artifacts in the current directory.

How was this discovered? This cannot happen within Kbuild because this script is always invoked from within the build folder, so was it a manual invocation or something else?

Quoted 3 lines

Pass the build directory to cmdfiles_for_modorder() and resolve both the .mod files and their associated .cmd files relative to it. Absolute paths continue to work, and the output location is unchanged.

I think you can omit the final sentence here, it does not add much value in my opinion.

Quoted 2 lines
Fixes ecca4fea1ede ("gen_compile_commands: support *.o, *.a, modules.order in positional argument")

Please fix your name, this needs to be a known identity.

Quoted 49 lines
---
 scripts/clang-tools/gen_compile_commands.py | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
scripts/clang-tools/gen_compile_commands.py +5 -4
index 8d14b81efd73..149135f85f19 100755
@@ -124,7 +124,7 @@ def cmdfiles_for_a(archive, ar):
         yield to_cmdfile(obj)
 
 
-def cmdfiles_for_modorder(modorder):
+def cmdfiles_for_modorder(modorder, root_directory):
     """Generate the iterator of .cmd files associated with the modules.order.
 
     Parse the given modules.order, and yield every .cmd file used to build the
@@ -132,6 +132,7 @@ def cmdfiles_for_modorder(modorder):
 
     Args:
         modorder: The modules.order file to parse
+        root_directory: The directory where the objects were built
 
     Yields:
         The path to every .cmd file found
@@ -142,11 +143,11 @@ def cmdfiles_for_modorder(modorder):
             base, ext = os.path.splitext(obj)
             if ext != '.o':
                 sys.exit('{}: module path must end with .o'.format(obj))
-            mod = base + '.mod'
+            mod = os.path.join(root_directory, base + '.mod')
             # Read from *.mod, to get a list of objects that compose the module.
             with open(mod) as m:
                 for mod_line in m:
-                    yield to_cmdfile(mod_line.rstrip())
+                    yield os.path.join(root_directory, to_cmdfile(mod_line.rstrip()))
 
 
 def process_line(root_directory, command_prefix, file_path):
@@ -206,7 +207,7 @@ def main():
         elif path.endswith('.a'):
             cmdfiles = cmdfiles_for_a(path, ar)
         elif path.endswith('modules.order'):
-            cmdfiles = cmdfiles_for_modorder(path)
+            cmdfiles = cmdfiles_for_modorder(path, directory)
         else:
             sys.exit('{}: unknown file type'.format(path))
 
Quoted 4 lines

base-commit: 0fcb05b72344f40806cbe810de8872f1698bf053 --
2.56.0

--
Cheers,
Nathan

#7 lore raw
Quoted 59 lines · Van Vo

Hi Nathan,

Quoted 1 line

How was this discovered?

This came from code inspection of standalone use of -d with an explicit modules.order argument, followed initially by a constructed reproducer. It was not a failure encountered through Kbuild's compile_commands.json target.

I have now reproduced it using real Kbuild-generated artifacts from 0fcb05b72344f40806cbe810de8872f1698bf053 on Linux/aarch64, with Python 3.11.13 and GCC 12.2.0. This was a partial lib/ build with CONFIG_TEST_LKM=m.

The following uses a disposable source tree at that base. The guards prevent overwriting an existing output directory or result file:

    (
        set -eu
        test ! -e out
        test ! -e outside.json
        test ! -e inside.json
        make O=out -j1 allnoconfig
        bash scripts/config --file out/.config --enable MODULES \
            --enable RUNTIME_TESTING_MENU --module TEST_LKM
        make O=out -j1 olddefconfig
        make O=out -j1 V=1 lib/

        outside_exit=0
        python3 -B scripts/clang-tools/gen_compile_commands.py -a ar \
            -d out -o outside.json out/lib/modules.order ||
            outside_exit=$?
        printf 'outside_exit=%s\n' "$outside_exit"

        inside_exit=0
        (cd out &&
         python3 -B ../scripts/clang-tools/gen_compile_commands.py -a ar \
             -o ../inside.json lib/modules.order) ||
            inside_exit=$?
        printf 'inside_exit=%s\n' "$inside_exit"
        test "$outside_exit" -eq 1
        test "$inside_exit" -eq 0
        python3 -c \
            'import json; assert len(json.load(open("inside.json"))) == 1'
    )

Kbuild generated lib/test_module.o, lib/test_module.mod, lib/.test_module.o.cmd and lib/modules.order under out/. Both .mod and modules.order contain lib/test_module.o.

The old script's invocation from the source directory fails with:

FileNotFoundError: [Errno 2] No such file or directory:
'lib/test_module.mod'
outside_exit=1

The same artifacts work when the old script is invoked from out/:

inside_exit=0

Quoted 8 lines

The successful invocation produces one JSON entry for lib/test_module.c. The patched script succeeds from the source directory and produces an identical entry. The old script also fails with absolute paths for -d and modules.order; the patched script succeeds with those paths too.

Only the standalone generator invocation is outside the build directory; compilation itself runs under out/. This is not a full kernel build or a boot/runtime test.

Thanks for the detailed reproducer.

Quoting from Nathan:

| How was this discovered? This cannot happen within Kbuild because this
| script is always invoked from within the build folder, so was it a
| manual invocation or something else?

So, if I get youre reply correctly, the issue this was not discovered "in the wild" but can only be reproduced by manual use of gen_compile_commands.py.

Signature · 1 line
Nicolas
author
#8 lore raw

Hi Nicolas,

Quoted 3 lines

So, if I get youre reply correctly, the issue this was not discovered "in the wild" but can only be reproduced by manual use of gen_compile_commands.py.

Yes, that is correct. I found this through code inspection and reproduced it with manual invocations, not from a failure in an existing workflow. My later test with Kbuild-generated artifacts was to validate the path handling; it was not a failure through Kbuild's compile_commands.json target.

While looking for earlier reports after your reply, I found Omer Zalman's RFC dated 28 September 2026 [1]. It describes the same failure with -d, an absolute modules.order path, and .mod paths resolved from the caller's directory. The author also used a fixture and explicitly had not tested that invocation against a real kernel build. I only found this RFC during this follow-up search, after submitting my patch.

There is also a real project wrapper in DRBD that invokes the generator directly with -d [2]. It scans the directory without passing modules.order, so it is not evidence of this particular failure.

I do not currently have a report of this failure in a real-world workflow.

[1] https://marc.info/?l=linux-kbuild&m=179063910090741&w=1
[2] https://github.com/LINBIT/drbd/blob/6d3787572ac95313afe7d139df7b24d589a00a6b/misc/gen-compile-commands.sh

Thanks,
Võ Ngọc Văn

Quoted 83 lines · Nicolas Schier
Quoted 59 lines · Van Vo

Hi Nathan,

Quoted 1 line

How was this discovered?

This came from code inspection of standalone use of -d with an explicit modules.order argument, followed initially by a constructed reproducer. It was not a failure encountered through Kbuild's compile_commands.json target.

I have now reproduced it using real Kbuild-generated artifacts from 0fcb05b72344f40806cbe810de8872f1698bf053 on Linux/aarch64, with Python 3.11.13 and GCC 12.2.0. This was a partial lib/ build with CONFIG_TEST_LKM=m.

The following uses a disposable source tree at that base. The guards prevent overwriting an existing output directory or result file:

    (
        set -eu
        test ! -e out
        test ! -e outside.json
        test ! -e inside.json
        make O=out -j1 allnoconfig
        bash scripts/config --file out/.config --enable MODULES \
            --enable RUNTIME_TESTING_MENU --module TEST_LKM
        make O=out -j1 olddefconfig
        make O=out -j1 V=1 lib/

        outside_exit=0
        python3 -B scripts/clang-tools/gen_compile_commands.py -a ar \
            -d out -o outside.json out/lib/modules.order ||
            outside_exit=$?
        printf 'outside_exit=%s\n' "$outside_exit"

        inside_exit=0
        (cd out &&
         python3 -B ../scripts/clang-tools/gen_compile_commands.py -a ar \
             -o ../inside.json lib/modules.order) ||
            inside_exit=$?
        printf 'inside_exit=%s\n' "$inside_exit"
        test "$outside_exit" -eq 1
        test "$inside_exit" -eq 0
        python3 -c \
            'import json; assert len(json.load(open("inside.json"))) == 1'
    )

Kbuild generated lib/test_module.o, lib/test_module.mod, lib/.test_module.o.cmd and lib/modules.order under out/. Both .mod and modules.order contain lib/test_module.o.

The old script's invocation from the source directory fails with:

FileNotFoundError: [Errno 2] No such file or directory:
'lib/test_module.mod'
outside_exit=1

The same artifacts work when the old script is invoked from out/:

inside_exit=0

Quoted 8 lines

The successful invocation produces one JSON entry for lib/test_module.c. The patched script succeeds from the source directory and produces an identical entry. The old script also fails with absolute paths for -d and modules.order; the patched script succeeds with those paths too.

Only the standalone generator invocation is outside the build directory; compilation itself runs under out/. This is not a full kernel build or a boot/runtime test.

Thanks for the detailed reproducer.

Quoting from Nathan:

| How was this discovered? This cannot happen within Kbuild because this
| script is always invoked from within the build folder, so was it a
| manual invocation or something else?

So, if I get youre reply correctly, the issue this was not discovered "in the wild" but can only be reproduced by manual use of gen_compile_commands.py.

--
Nicolas