Skip to content

Commit 038227a

Browse files
committed
2
1 parent 3495c90 commit 038227a

2 files changed

Lines changed: 30 additions & 9 deletions

File tree

tools/tweak-compile-commands.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@ gcc searches both `/opt/x` *and* `/a/b/opt/x` for headers.
3838
`compile_commands.json` file so this implicit behaviour is spelled out
3939
explicitly: for every command that has a `--sysroot` argument, every
4040
existing `-isystem PATH` argument gets a matching, explicit
41-
`-isystem SYSROOT/PATH` argument added right after it. Commands without a
42-
`--sysroot` argument are left unchanged.
41+
`-isystem SYSROOT/PATH` argument added right after it, and the `--sysroot`
42+
argument is then removed (it is no longer needed since the sysroot-relative
43+
paths are now spelled out explicitly). Commands without a `--sysroot`
44+
argument are left unchanged.
4345

4446
### ISYSTEM
4547

@@ -127,7 +129,7 @@ the last command above produces:
127129

128130
```json
129131
{
130-
"command": "gcc --sysroot /a/b -I /opt/x -I /a/b/opt/x -isystem /path/lib1/include -isystem /a/b/path/lib1/include -c foo.c -o foo.o"
132+
"command": "gcc -I /opt/x -I /a/b/opt/x -isystem /path/lib1/include -isystem /a/b/path/lib1/include -c foo.c -o foo.o"
131133
}
132134
```
133135

tools/tweak-compile-commands.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@
22
#
33
# Tweaks a compile_commands.json file: for every build command that has a
44
# --sysroot argument, each existing -isystem argument gets a matching extra
5-
# -isystem argument pointing into the sysroot. This is useful when a
6-
# compiler resolves -isystem paths relative to --sysroot internally (as
7-
# part of its built-in search path handling) but a tool consuming
8-
# compile_commands.json (such as Cppcheck) does not, so the sysroot-relative
9-
# path needs to be spelled out explicitly.
5+
# -isystem argument pointing into the sysroot, and the --sysroot argument
6+
# itself is then removed. This is useful when a compiler resolves -isystem
7+
# paths relative to --sysroot internally (as part of its built-in search
8+
# path handling) but a tool consuming compile_commands.json (such as
9+
# Cppcheck) does not, so the sysroot-relative path needs to be spelled out
10+
# explicitly instead.
1011
#
1112
# Example:
1213
# --sysroot /a/b -isystem /opt/x
1314
# =>
14-
# --sysroot /a/b -isystem /opt/x -isystem /a/b/opt/x
15+
# -isystem /opt/x -isystem /a/b/opt/x
1516
#
1617
# Optionally, --isystem-to-i converts -isystem arguments to -I, which can be
1718
# useful since Cppcheck otherwise treats -isystem headers as "system"
@@ -55,6 +56,23 @@ def join_sysroot(sysroot, path):
5556
return sysroot.rstrip('/') + '/' + path.lstrip('/')
5657

5758

59+
def remove_sysroot_arg(tokens):
60+
result = []
61+
i = 0
62+
n = len(tokens)
63+
while i < n:
64+
tok = tokens[i]
65+
if tok == '--sysroot' and i + 1 < n:
66+
i += 2
67+
continue
68+
if tok.startswith('--sysroot='):
69+
i += 1
70+
continue
71+
result.append(tok)
72+
i += 1
73+
return result
74+
75+
5876
def add_isystem_sysroot(tokens, sysroot):
5977
result = []
6078
i = 0
@@ -153,6 +171,7 @@ def tweak_entry(entry, isystem_to_i, exclude_folders, remove_include_path):
153171
sysroot = find_sysroot(tokens)
154172
if sysroot is not None:
155173
tokens = add_isystem_sysroot(tokens, sysroot)
174+
tokens = remove_sysroot_arg(tokens)
156175
changed = True
157176

158177
if isystem_to_i:

0 commit comments

Comments
 (0)