gen_compile_commands.py: fix path resolve with symlinks in it
Subsystems (2)
PATCH 1/1 gen_compile_commands.py: fix path resolve with symlinks in it
When symbolic links are involved in the path, os.path.abspath might not resolve the symlinks and instead return the absolute path with the symlinks intact.
use pathlib.Path resolve() instead of os.path.abspath()
--- scripts/clang-tools/gen_compile_commands.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
scripts/clang-tools/gen_compile_commands.py +3 -2
@@ -11,6 +11,7 @@ import argparse import json import logging import os +from pathlib import Path import re import subprocess import sys
@@ -172,8 +173,8 @@ def process_line(root_directory, command_prefix, file_path): # by Make, so this code replaces the escaped version with '#'. prefix = command_prefix.replace('\#', '#').replace('$(pound)', '#') - # Use os.path.abspath() to normalize the path resolving '.' and '..' . - abs_path = os.path.abspath(os.path.join(root_directory, file_path)) + # Make the path absolute, resolving all symlinks on the way and also normalizing it. + abs_path = str(Path(os.path.join(root_directory, file_path)).resolve()) if not os.path.exists(abs_path): raise ValueError('File %s not found' % abs_path) return {
Signature · 1 line
2.39.2
Nathan Chancellor
maintainerQuoted 3 lines · Jialu Xu
When symbolic links are involved in the path, os.path.abspath might not resolve the symlinks and instead return the absolute path with the symlinks intact.
Can you provide an example or more detailed description of how this behavior is currently broken? I can't really understand how having symlinks in the path after normalization would break anything but I am potentially missing something :)
Quoted 20 lines
use pathlib.Path
resolve()instead ofos.path.abspath()Signed-off-by Jialu Xu <[email protected]>--- scripts/clang-tools/gen_compile_commands.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)scripts/clang-tools/gen_compile_commands.py +1
-0@@ -11,6 +11,7 @@ import argparse import json import logging import os +from pathlib import Path import re import subprocess import sys
scripts/clang-tools/gen_compile_commands.py def process_line(root_directory, command_prefix, file_path): · line 177
Quoted 7 lines
scripts/clang-tools/gen_compile_commands.py +2
-2@@ -172,8 +173,8 @@ def process_line(root_directory, command_prefix, file_path): # by Make, so this code replaces the escaped version with '#'. prefix = command_prefix.replace('\#', '#').replace('$(pound)', '#') - # Use os.path.abspath() to normalize the path resolving '.' and '..' . - abs_path = os.path.abspath(os.path.join(root_directory, file_path)) + # Make the path absolute, resolving all symlinks on the way and also normalizing it. + abs_path = str(Path(os.path.join(root_directory, file_path)).resolve())
I think this can be more simply:
abs_path = str(Path(root_directory, file_path).resolve())
I think there should be a comment around why we are creating a Path object then creating a string from it, rather than using the Path object directly, namely that PosixPath is not JSON serializable.
Quoted 6 lines
scripts/clang-tools/gen_compile_commands.py +0
-0if not os.path.exists(abs_path): raise ValueError('File %s not found' % abs_path) return {Signature · 1 line
2.39.2
Quoted 9 lines · Jialu Xu
Quoted 3 lines
When symbolic links are involved in the path, os.path.abspath might not resolve the symlinks and instead return the absolute path with the symlinks intact.
Can you provide an example or more detailed description of how this behavior is currently broken? I can't really understand how having symlinks in the path after normalization would break anything but I am potentially missing something :)
Glad to show my situation:
Say "drivers/hdf/" has some symlinks:
# ls -l drivers/hdf/
total 364
drwxrwxr-x 2 ... 4096 ... evdev
lrwxrwxrwx 1 ... 44 ... framework -> ../../../../../../drivers/hdf_core/framework
-rw-rw-r-- 1 ... 359010 ... hdf_macro_test.h
lrwxrwxrwx 1 ... 55 ... inner_api -> ../../../../../../drivers/hdf_core/interfaces/inner_api
lrwxrwxrwx 1 ... 53 ... khdf -> ../../../../../../drivers/hdf_core/adapter/khdf/linux
-rw-r--r-- 1 ... 74 ... Makefile
drwxrwxr-x 3 ... 4096 ... wifiOne .cmd file records that:
# head -1 ./framework/core/manager/src/.devmgr_service.o.cmd
cmd_drivers/hdf/khdf/manager/../../../../framework/core/manager/src/devmgr_service.o := ... \
/path/to/out/drivers/hdf/khdf/manager/../../../../framework/core/manager/src/devmgr_service.cos.path.abspath returns "/path/to/out/framework/core/manager/src/devmgr_service.c", not correct:
# ./scripts/clang-tools/gen_compile_commands.py
INFO: Could not add line from ./framework/core/manager/src/.devmgr_service.o.cmd: File \
/path/to/out/framework/core/manager/src/devmgr_service.c not foundUse pathlib.Path resolve() instead, got the correct path
# cat compile_commands.json
...
{
"command": ...
"directory": ...
"file": "/path/to/blabla/drivers/hdf_core/framework/core/manager/src/devmgr_service.c"
},
...Quoted 5 lines
scripts/clang-tools/gen_compile_commands.py def process_line(root_directory, command_prefix, file_path): · line 177
Quoted 4 lines
scripts/clang-tools/gen_compile_commands.py +2
-2- # Use os.path.abspath() to normalize the path resolving '.' and '..' . - abs_path = os.path.abspath(os.path.join(root_directory, file_path)) + # Make the path absolute, resolving all symlinks on the way and also normalizing it. + abs_path = str(Path(os.path.join(root_directory, file_path)).resolve())
Quoted 7 lines
I think this can be more simply:
abs_path=str(Path(root_directory, file_path).resolve())I think there should be a comment around why we are creating a Path object then creating a string from it, rather than using the Path object directly, namely that PosixPath is not JSON serializable.
Nice, update as yours, thanks.
Quoted 6 lines
Quoted 6 lines
if not
os.path.exists(abs_path):
raiseValueError('File %s not found' % abs_path)
return {Signature · 1 line
2.39.2
PATCH v2 1/1 gen_compile_commands.py: fix path resolve with symlinks in it
In reply to Nathan ChancellorWhen symbolic links are involved in the path, os.path.abspath might not resolve the symlinks and instead return the absolute path with the symlinks intact.
Use pathlib.Path resolve() instead of os.path.abspath()
--- scripts/clang-tools/gen_compile_commands.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
scripts/clang-tools/gen_compile_commands.py +4 -2
@@ -11,6 +11,7 @@ import argparse import json import logging import os +from pathlib import Path import re import subprocess import sys
@@ -172,8 +173,9 @@ def process_line(root_directory, command_prefix, file_path): # by Make, so this code replaces the escaped version with '#'. prefix = command_prefix.replace('\#', '#').replace('$(pound)', '#') - # Use os.path.abspath() to normalize the path resolving '.' and '..' . - abs_path = os.path.abspath(os.path.join(root_directory, file_path)) + # Make the path absolute, resolving all symlinks on the way and also normalizing it. + # Convert Path object to a string because 'PosixPath' is not JSON serializable. + abs_path = str(Path(root_directory, file_path).resolve()) if not os.path.exists(abs_path): raise ValueError('File %s not found' % abs_path) return {
Signature · 1 line
2.39.2
Hi Jialu,
Quoted 7 lines · Jialu Xu
When symbolic links are involved in the path, os.path.abspath might not resolve the symlinks and instead return the absolute path with the symlinks intact.
Use pathlib.Path
resolve()instead ofos.path.abspath()Signed-off-by Jialu Xu <[email protected]>
Thanks for the clarification in your previous message [1], I suppose that makes sense as to why nobody has reported this to us because that is a rather odd situation that the upstream kernel would not experience.
I think that some of those details should be in the commit message, along with a short example like you provided, so that we know exactly what the situation was and how this patch resolves it.
Perhaps something like (please feel free to correct or reword as you feel necessary):
"When a path contains relative symbolic links, os.path.abspath() might not follow the symlinks and instead return the absolute path with just the relative paths resolved, resulting in an incorrect path.
<broken example>
Use pathlib.Path.resolve(), which resolves the symlinks and normalizes the paths correctly.
<working example>"
The actual fix seems fine to me. Feel free to add
to the subsequent submission and please include both
Masahiro Yamada <[email protected]>
[email protected]
on it in addition to the people you have here, as he is the one who actually applies gen_compile_commands.py changes (I am going to send a MAINTAINERS change for this).
[1]: https://lore.kernel.org/[email protected]/
Cheers,
Nathan
Quoted 32 lines
--- scripts/clang-tools/gen_compile_commands.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)scripts/clang-tools/gen_compile_commands.py +4
-2@@ -11,6 +11,7 @@ import argparse import json import logging import os +from pathlib import Path import re import subprocess import sys@@ -172,8 +173,9 @@ def process_line(root_directory, command_prefix, file_path): # by Make, so this code replaces the escaped version with '#'. prefix = command_prefix.replace('\#', '#').replace('$(pound)', '#') - # Use os.path.abspath() to normalize the path resolving '.' and '..' . - abs_path = os.path.abspath(os.path.join(root_directory, file_path)) + # Make the path absolute, resolving all symlinks on the way and also normalizing it. + # Convert Path object to a string because 'PosixPath' is not JSON serializable. + abs_path = str(Path(root_directory, file_path).resolve()) if not os.path.exists(abs_path): raise ValueError('File %s not found' % abs_path) return {Signature · 1 line
2.39.2
Hi Nathan,
Quoted 37 lines · Jialu Xu
Quoted 7 lines
When symbolic links are involved in the path, os.path.abspath might not resolve the symlinks and instead return the absolute path with the symlinks intact.
Use pathlib.Path
resolve()instead ofos.path.abspath()Signed-off-by Jialu Xu <[email protected]>Thanks for the clarification in your previous message [1], I suppose that makes sense as to why nobody has reported this to us because that is a rather odd situation that the upstream kernel would not experience.
I think that some of those details should be in the commit message, along with a short example like you provided, so that we know exactly what the situation was and how this patch resolves it.
Perhaps something like (please feel free to correct or reword as you feel necessary):
"When a path contains relative symbolic links,
os.path.abspath()might not follow the symlinks and instead return the absolute path with just the relative paths resolved, resulting in an incorrect path.<broken example>
Use
pathlib.Path.resolve(), which resolves the symlinks and normalizes the paths correctly.<working example>"
The actual fix seems fine to me. Feel free to add
Reviewed-by Nathan Chancellor <[email protected]>to the subsequent submission and please include both
Quoted 9 lines
Masahiro Yamada <[email protected]>
[email protected]on it in addition to the people you have here, as he is the one who actually applies
gen_compile_commands.pychanges (I am going to send aMAINTAINERSchange for this).
Thanks for the very detailed help!
Patch update as v3.
Cheers,
Jialu
PATCH v3 1/1 gen_compile_commands.py: fix path resolve with symlinks in it
In reply to Nathan ChancellorWhen a path contains relative symbolic links, os.path.abspath() might not follow the symlinks and instead return the absolute path with just the relative paths resolved, resulting in an incorrect path.
1. Say "drivers/hdf/" has some symlinks:
# ls -l drivers/hdf/
total 364
drwxrwxr-x 2 ... 4096 ... evdev
lrwxrwxrwx 1 ... 44 ... framework -> ../../../../../../drivers/hdf_core/framework
-rw-rw-r-- 1 ... 359010 ... hdf_macro_test.h
lrwxrwxrwx 1 ... 55 ... inner_api -> ../../../../../../drivers/hdf_core/interfaces/inner_api
lrwxrwxrwx 1 ... 53 ... khdf -> ../../../../../../drivers/hdf_core/adapter/khdf/linux
-rw-r--r-- 1 ... 74 ... Makefile
drwxrwxr-x 3 ... 4096 ... wifi2. One .cmd file records that:
# head -1 ./framework/core/manager/src/.devmgr_service.o.cmd
cmd_drivers/hdf/khdf/manager/../../../../framework/core/manager/src/devmgr_service.o := ... \
/path/to/out/drivers/hdf/khdf/manager/../../../../framework/core/manager/src/devmgr_service.c3. os.path.abspath returns "/path/to/out/framework/core/manager/src/devmgr_service.c", not correct:
# ./scripts/clang-tools/gen_compile_commands.py
INFO: Could not add line from ./framework/core/manager/src/.devmgr_service.o.cmd: File \
/path/to/out/framework/core/manager/src/devmgr_service.c not foundUse pathlib.Path.resolve(), which resolves the symlinks and normalizes the paths correctly.
# cat compile_commands.json
...
{
"command": ...
"directory": ...
"file": "/path/to/blabla/drivers/hdf_core/framework/core/manager/src/devmgr_service.c"
},
...--- scripts/clang-tools/gen_compile_commands.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
scripts/clang-tools/gen_compile_commands.py +4 -2
@@ -11,6 +11,7 @@ import argparse import json import logging import os +from pathlib import Path import re import subprocess import sys
@@ -172,8 +173,9 @@ def process_line(root_directory, command_prefix, file_path): # by Make, so this code replaces the escaped version with '#'. prefix = command_prefix.replace('\#', '#').replace('$(pound)', '#') - # Use os.path.abspath() to normalize the path resolving '.' and '..' . - abs_path = os.path.abspath(os.path.join(root_directory, file_path)) + # Make the path absolute, resolving all symlinks on the way and also normalizing it. + # Convert Path object to a string because 'PosixPath' is not JSON serializable. + abs_path = str(Path(root_directory, file_path).resolve()) if not os.path.exists(abs_path): raise ValueError('File %s not found' % abs_path) return {
Signature · 1 line
2.39.2
PATCH v4 1/1 gen_compile_commands.py: fix path resolve with symlinks in it
In reply to Nathan ChancellorWhen a path contains relative symbolic links, os.path.abspath() might not follow the symlinks and instead return the absolute path with just the relative paths resolved, resulting in an incorrect path.
1. Say "drivers/hdf/" has some symlinks:
# ls -l drivers/hdf/
total 364
drwxrwxr-x 2 ... 4096 ... evdev
lrwxrwxrwx 1 ... 44 ... framework -> ../../../../../../drivers/hdf_core/framework
-rw-rw-r-- 1 ... 359010 ... hdf_macro_test.h
lrwxrwxrwx 1 ... 55 ... inner_api -> ../../../../../../drivers/hdf_core/interfaces/inner_api
lrwxrwxrwx 1 ... 53 ... khdf -> ../../../../../../drivers/hdf_core/adapter/khdf/linux
-rw-r--r-- 1 ... 74 ... Makefile
drwxrwxr-x 3 ... 4096 ... wifi2. One .cmd file records that:
# head -1 ./framework/core/manager/src/.devmgr_service.o.cmd
cmd_drivers/hdf/khdf/manager/../../../../framework/core/manager/src/devmgr_service.o := ... \
/path/to/out/drivers/hdf/khdf/manager/../../../../framework/core/manager/src/devmgr_service.c3. os.path.abspath returns "/path/to/out/framework/core/manager/src/devmgr_service.c", not correct:
# ./scripts/clang-tools/gen_compile_commands.py
INFO: Could not add line from ./framework/core/manager/src/.devmgr_service.o.cmd: File \
/path/to/out/framework/core/manager/src/devmgr_service.c not foundUse pathlib.Path.resolve(), which resolves the symlinks and normalizes the paths correctly.
# cat compile_commands.json
...
{
"command": ...
"directory": ...
"file": "/path/to/blabla/drivers/hdf_core/framework/core/manager/src/devmgr_service.c"
},
...--- scripts/clang-tools/gen_compile_commands.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
scripts/clang-tools/gen_compile_commands.py +4 -2
@@ -11,6 +11,7 @@ import argparse import json import logging import os +from pathlib import Path import re import subprocess import sys
@@ -172,8 +173,9 @@ def process_line(root_directory, command_prefix, file_path): # by Make, so this code replaces the escaped version with '#'. prefix = command_prefix.replace('\#', '#').replace('$(pound)', '#') - # Use os.path.abspath() to normalize the path resolving '.' and '..' . - abs_path = os.path.abspath(os.path.join(root_directory, file_path)) + # Make the path absolute, resolving all symlinks on the way and also normalizing it. + # Convert Path object to a string because 'PosixPath' is not JSON serializable. + abs_path = str(Path(root_directory, file_path).resolve()) if not os.path.exists(abs_path): raise ValueError('File %s not found' % abs_path) return {
Signature · 1 line
2.39.2
Hi,
Quoted 40 lines · Jialu Xu
When a path contains relative symbolic links,
os.path.abspath()might not follow the symlinks and instead return the absolute path with just the relative paths resolved, resulting in an incorrect path.1. Say "
drivers/hdf/" has some symlinks:# ls -l drivers/hdf/ total 364 drwxrwxr-x 2 ... 4096 ... evdev lrwxrwxrwx 1 ... 44 ... framework -> ../../../../../../drivers/hdf_core/framework -rw-rw-r-- 1 ... 359010 ... hdf_macro_test.h lrwxrwxrwx 1 ... 55 ... inner_api -> ../../../../../../drivers/hdf_core/interfaces/inner_api lrwxrwxrwx 1 ... 53 ... khdf -> ../../../../../../drivers/hdf_core/adapter/khdf/linux -rw-r--r-- 1 ... 74 ... Makefile drwxrwxr-x 3 ... 4096 ... wifi2. One .cmd file records that:
# head -1 ./framework/core/manager/src/.devmgr_service.o.cmd cmd_drivers/hdf/khdf/manager/../../../../framework/core/manager/src/devmgr_service.o := ... \ /path/to/out/drivers/hdf/khdf/manager/../../../../framework/core/manager/src/devmgr_service.c3. os.path.abspath returns "/path/to/out/framework/core/manager/src/devmgr_service.c", not correct:
# ./scripts/clang-tools/gen_compile_commands.py INFO: Could not add line from ./framework/core/manager/src/.devmgr_service.o.cmd: File \ /path/to/out/framework/core/manager/src/devmgr_service.c not foundUse
pathlib.Path.resolve(), which resolves the symlinks and normalizes the paths correctly.# cat compile_commands.json ... { "command": ... "directory": ... "file": "/path/to/blabla/drivers/hdf_core/framework/core/manager/src/devmgr_service.c" }, ...
Quoted 3 lines
Reviewed-by Nathan Chancellor <[email protected]>Signed-off-by Jialu Xu <[email protected]>---
This looks good to me. I prefer using pathlib over anything in os module, even if the behavior was the same. In this case, the pathlib behavior is better -- which is a bonus.
Quoted 30 lines
scripts/clang-tools/gen_compile_commands.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)scripts/clang-tools/gen_compile_commands.py +4
-3@@ -11,6 +11,7 @@ import argparse import json import logging import os +from pathlib import Path import re import subprocess import sys@@ -172,8 +173,9 @@ def process_line(root_directory, command_prefix, file_path): # by Make, so this code replaces the escaped version with '#'. prefix = command_prefix.replace('\#', '#').replace('$(pound)', '#') - # Use os.path.abspath() to normalize the path resolving '.' and '..' . - abs_path = os.path.abspath(os.path.join(root_directory, file_path)) + # Make the path absolute, resolving all symlinks on the way and also normalizing it. + # Convert Path object to a string because 'PosixPath' is not JSON serializable. + abs_path = str(Path(root_directory, file_path).resolve()) if not os.path.exists(abs_path): raise ValueError('File %s not found' % abs_path) return { --2.39.2
Quoted 61 lines · Jialu Xu
When a path contains relative symbolic links,
os.path.abspath()might not follow the symlinks and instead return the absolute path with just the relative paths resolved, resulting in an incorrect path.1. Say "
drivers/hdf/" has some symlinks:# ls -l drivers/hdf/ total 364 drwxrwxr-x 2 ... 4096 ... evdev lrwxrwxrwx 1 ... 44 ... framework -> ../../../../../../drivers/hdf_core/framework -rw-rw-r-- 1 ... 359010 ... hdf_macro_test.h lrwxrwxrwx 1 ... 55 ... inner_api -> ../../../../../../drivers/hdf_core/interfaces/inner_api lrwxrwxrwx 1 ... 53 ... khdf -> ../../../../../../drivers/hdf_core/adapter/khdf/linux -rw-r--r-- 1 ... 74 ... Makefile drwxrwxr-x 3 ... 4096 ... wifi2. One .cmd file records that:
# head -1 ./framework/core/manager/src/.devmgr_service.o.cmd cmd_drivers/hdf/khdf/manager/../../../../framework/core/manager/src/devmgr_service.o := ... \ /path/to/out/drivers/hdf/khdf/manager/../../../../framework/core/manager/src/devmgr_service.c3. os.path.abspath returns "/path/to/out/framework/core/manager/src/devmgr_service.c", not correct:
# ./scripts/clang-tools/gen_compile_commands.py INFO: Could not add line from ./framework/core/manager/src/.devmgr_service.o.cmd: File \ /path/to/out/framework/core/manager/src/devmgr_service.c not foundUse
pathlib.Path.resolve(), which resolves the symlinks and normalizes the paths correctly.# cat compile_commands.json ... { "command": ... "directory": ... "file": "/path/to/blabla/drivers/hdf_core/framework/core/manager/src/devmgr_service.c" }, ...Reviewed-by Nathan Chancellor <[email protected]>Signed-off-by Jialu Xu <[email protected]>--- scripts/clang-tools/gen_compile_commands.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)scripts/clang-tools/gen_compile_commands.py +1
-0@@ -11,6 +11,7 @@ import argparse import json import logging import os +from pathlib import Path import re import subprocess import sys@@ -172,8 +173,9 @@ def process_line(root_directory, command_prefix, file_path): # by Make, so this code replaces the escaped version with '#'.
scripts/clang-tools/gen_compile_commands.py def process_line(root_directory, command_prefix, file_path): · line 181
Quoted 10 lines
scripts/clang-tools/gen_compile_commands.py +3
-2prefix = command_prefix.replace('\#', '#').replace('$(pound)', '#') - # Use os.path.abspath() to normalize the path resolving '.' and '..' . - abs_path = os.path.abspath(os.path.join(root_directory, file_path)) + # Make the path absolute, resolving all symlinks on the way and also normalizing it. + # Convert Path object to a string because 'PosixPath' is not JSON serializable. + abs_path = str(Path(root_directory, file_path).resolve()) if not os.path.exists(abs_path): raise ValueError('File %s not found' % abs_path) return {
Is there any reason why you didn't simply replace os.path.abspath() with os.path.realpath() ?
This patch uses pathlib.Path() just in one place, leaving many call-sites of os.path.*() functions.
If it is just a matter of your preference,
you need to convert os.path.*() for consistency (as a follow-up patch).
I see one more os.path.abspath()
return (args.log_level,
os.path.abspath(args.directory),
args.output,
args.ar,
args.paths if len(args.paths) > 0 else [args.directory])
Does it cause a similar issue for the 'directory' field with symbolic link jungles?
--
Best Regards
Masahiro Yamada
Quoted 2 lines
Is there any reason why you didn't simply replace
os.path.abspath()withos.path.realpath()?
I have tried it before, but obviously, I made a mistake.
Quoted 6 lines
This patch uses
pathlib.Path()just in one place, leaving many call-sites of os.path.*() functions.If it is just a matter of your preference,
you need to convert os.path.*() for consistency (as a follow-up patch).
Keep os.path.* as os.path.realpath() works.
Quoted 10 lines
I see one more
os.path.abspath()return (args.log_level,
os.path.abspath(args.directory),
args.output,
args.ar,
args.paths if len(args.paths) > 0 else [args.directory])Does it cause a similar issue for the 'directory' field with symbolic link jungles?
Yes, also fixed.
--
Best Regards
Jialu Xu
PATCH v5 1/1 gen_compile_commands.py: fix path resolve with symlinks in it
When a path contains relative symbolic links, os.path.abspath() might not follow the symlinks and instead return the absolute path with just the relative paths resolved, resulting in an incorrect path.
1. Say "drivers/hdf/" has some symlinks:
# ls -l drivers/hdf/
total 364
drwxrwxr-x 2 ... 4096 ... evdev
lrwxrwxrwx 1 ... 44 ... framework -> ../../../../../../drivers/hdf_core/framework
-rw-rw-r-- 1 ... 359010 ... hdf_macro_test.h
lrwxrwxrwx 1 ... 55 ... inner_api -> ../../../../../../drivers/hdf_core/interfaces/inner_api
lrwxrwxrwx 1 ... 53 ... khdf -> ../../../../../../drivers/hdf_core/adapter/khdf/linux
-rw-r--r-- 1 ... 74 ... Makefile
drwxrwxr-x 3 ... 4096 ... wifi2. One .cmd file records that:
# head -1 ./framework/core/manager/src/.devmgr_service.o.cmd
cmd_drivers/hdf/khdf/manager/../../../../framework/core/manager/src/devmgr_service.o := ... \
/path/to/src/drivers/hdf/khdf/manager/../../../../framework/core/manager/src/devmgr_service.c3. os.path.abspath returns "/path/to/src/framework/core/manager/src/devmgr_service.c", not correct:
# ./scripts/clang-tools/gen_compile_commands.py
INFO: Could not add line from ./framework/core/manager/src/.devmgr_service.o.cmd: File \
/path/to/src/framework/core/manager/src/devmgr_service.c not foundUse os.path.realpath(), which resolves the symlinks and normalizes the paths correctly.
# cat compile_commands.json
...
{
"command": ...
"directory": ...
"file": "/path/to/bla/drivers/hdf_core/framework/core/manager/src/devmgr_service.c"
},
...Also fix it in parse_arguments().
--- scripts/clang-tools/gen_compile_commands.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
scripts/clang-tools/gen_compile_commands.py +3 -3
@@ -64,7 +64,7 @@ def parse_arguments(): args = parser.parse_args() return (args.log_level, - os.path.abspath(args.directory), + os.path.realpath(args.directory), args.output, args.ar, args.paths if len(args.paths) > 0 else [args.directory])
@@ -172,8 +172,8 @@ def process_line(root_directory, command_prefix, file_path): # by Make, so this code replaces the escaped version with '#'. prefix = command_prefix.replace('\#', '#').replace('$(pound)', '#') - # Use os.path.abspath() to normalize the path resolving '.' and '..' . - abs_path = os.path.abspath(os.path.join(root_directory, file_path)) + # Return the canonical path, eliminating any symbolic links encountered in the path. + abs_path = os.path.realpath(os.path.join(root_directory, file_path)) if not os.path.exists(abs_path): raise ValueError('File %s not found' % abs_path) return {
Signature · 1 line
2.39.2
Quoted 40 lines · Jialu Xu
When a path contains relative symbolic links,
os.path.abspath()might not follow the symlinks and instead return the absolute path with just the relative paths resolved, resulting in an incorrect path.1. Say "
drivers/hdf/" has some symlinks:# ls -l drivers/hdf/ total 364 drwxrwxr-x 2 ... 4096 ... evdev lrwxrwxrwx 1 ... 44 ... framework -> ../../../../../../drivers/hdf_core/framework -rw-rw-r-- 1 ... 359010 ... hdf_macro_test.h lrwxrwxrwx 1 ... 55 ... inner_api -> ../../../../../../drivers/hdf_core/interfaces/inner_api lrwxrwxrwx 1 ... 53 ... khdf -> ../../../../../../drivers/hdf_core/adapter/khdf/linux -rw-r--r-- 1 ... 74 ... Makefile drwxrwxr-x 3 ... 4096 ... wifi2. One .cmd file records that:
# head -1 ./framework/core/manager/src/.devmgr_service.o.cmd cmd_drivers/hdf/khdf/manager/../../../../framework/core/manager/src/devmgr_service.o := ... \ /path/to/src/drivers/hdf/khdf/manager/../../../../framework/core/manager/src/devmgr_service.c3. os.path.abspath returns "/path/to/src/framework/core/manager/src/devmgr_service.c", not correct:
# ./scripts/clang-tools/gen_compile_commands.py INFO: Could not add line from ./framework/core/manager/src/.devmgr_service.o.cmd: File \ /path/to/src/framework/core/manager/src/devmgr_service.c not foundUse
os.path.realpath(), which resolves the symlinks and normalizes the paths correctly.# cat compile_commands.json ... { "command": ... "directory": ... "file": "/path/to/bla/drivers/hdf_core/framework/core/manager/src/devmgr_service.c" }, ...
Quoted 3 lines
Also fix it in
parse_arguments().Signed-off-by Jialu Xu <[email protected]>
Applied to linux-kbuild. Thanks.
Signature · 2 lines
Best Regards Masahiro Yamada