-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathtweak-compile-commands.py
More file actions
executable file
·237 lines (203 loc) · 7.52 KB
/
Copy pathtweak-compile-commands.py
File metadata and controls
executable file
·237 lines (203 loc) · 7.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/usr/bin/env python3
#
# Tweaks a compile_commands.json file: for every build command that has a
# --sysroot argument, each existing -isystem argument gets a matching extra
# -isystem argument pointing into the sysroot, and the --sysroot argument
# itself is then removed. This is useful when a compiler resolves -isystem
# paths relative to --sysroot internally (as part of its built-in search
# path handling) but a tool consuming compile_commands.json (such as
# Cppcheck) does not, so the sysroot-relative path needs to be spelled out
# explicitly instead.
#
# Example:
# --sysroot /a/b -isystem /opt/x
# =>
# -isystem /opt/x -isystem /a/b/opt/x
#
# Optionally, --isystem-to-i converts -isystem arguments to -I, which can be
# useful since Cppcheck otherwise treats -isystem headers as "system"
# headers and skips some checks in them. Paths whose folder name matches one
# of the --exclude-folder values are left as -isystem. For example, with
# --isystem-to-i --exclude-folder lib1:
# -isystem /opt/x -isystem /path/lib1/include
# =>
# -I /opt/x -isystem /path/lib1/include
#
# Optionally, --remove-include-path removes -I arguments whose path contains
# the given path. For example, with --remove-include-path /path/lib1:
# -I /opt/x -I /path/lib1/include
# =>
# -I /opt/x
#
# Usage:
# tools/tweak-compile-commands.py compile_commands.json -o out.json
# tools/tweak-compile-commands.py -i compile_commands.json
# tools/tweak-compile-commands.py -i compile_commands.json --isystem-to-i --exclude-folder lib1
# tools/tweak-compile-commands.py -i compile_commands.json --remove-include-path /path/lib1
#
# With neither -o nor -i, the result is written to stdout.
import argparse
import json
import shlex
import sys
def find_sysroot(tokens):
for i, tok in enumerate(tokens):
if tok == '--sysroot' and i + 1 < len(tokens):
return tokens[i + 1]
if tok.startswith('--sysroot='):
return tok[len('--sysroot='):]
return None
def join_sysroot(sysroot, path):
return sysroot.rstrip('/') + '/' + path.lstrip('/')
def remove_sysroot_arg(tokens):
result = []
i = 0
n = len(tokens)
while i < n:
tok = tokens[i]
if tok == '--sysroot' and i + 1 < n:
i += 2
continue
if tok.startswith('--sysroot='):
i += 1
continue
result.append(tok)
i += 1
return result
def add_isystem_sysroot(tokens, sysroot):
result = []
i = 0
n = len(tokens)
while i < n:
tok = tokens[i]
if tok == '-isystem' and i + 1 < n:
path = tokens[i + 1]
result.append(tok)
result.append(path)
result.append('-isystem')
result.append(join_sysroot(sysroot, path))
i += 2
continue
if tok.startswith('-isystem') and tok != '-isystem':
path = tok[len('-isystem'):]
result.append(tok)
result.append('-isystem' + join_sysroot(sysroot, path))
i += 1
continue
result.append(tok)
i += 1
return result
def is_excluded(path, exclude_folders):
parts = path.replace('\\', '/').split('/')
return any(part in exclude_folders for part in parts if part)
def convert_isystem_to_i(tokens, exclude_folders):
result = []
i = 0
n = len(tokens)
while i < n:
tok = tokens[i]
if tok == '-isystem' and i + 1 < n:
path = tokens[i + 1]
result.append(tok if is_excluded(path, exclude_folders) else '-I')
result.append(path)
i += 2
continue
if tok.startswith('-isystem') and tok != '-isystem':
path = tok[len('-isystem'):]
result.append(tok if is_excluded(path, exclude_folders) else '-I' + path)
i += 1
continue
result.append(tok)
i += 1
return result
def matches_remove_path(path, remove_paths):
normalized = path.replace('\\', '/')
return any(remove_path.replace('\\', '/') in normalized for remove_path in remove_paths)
def remove_include_paths(tokens, remove_paths):
result = []
i = 0
n = len(tokens)
while i < n:
tok = tokens[i]
if tok == '-I' and i + 1 < n:
path = tokens[i + 1]
if matches_remove_path(path, remove_paths):
i += 2
continue
result.append(tok)
result.append(path)
i += 2
continue
if tok.startswith('-I') and tok != '-I':
path = tok[len('-I'):]
if matches_remove_path(path, remove_paths):
i += 1
continue
result.append(tok)
i += 1
continue
result.append(tok)
i += 1
return result
def tweak_entry(entry, isystem_to_i, exclude_folders, remove_include_path):
if 'command' in entry:
tokens = shlex.split(entry['command'])
elif 'arguments' in entry:
tokens = entry['arguments']
else:
return False
changed = False
sysroot = find_sysroot(tokens)
if sysroot is not None:
tokens = add_isystem_sysroot(tokens, sysroot)
tokens = remove_sysroot_arg(tokens)
changed = True
if isystem_to_i:
new_tokens = convert_isystem_to_i(tokens, exclude_folders)
if new_tokens != tokens:
tokens = new_tokens
changed = True
if remove_include_path:
new_tokens = remove_include_paths(tokens, remove_include_path)
if new_tokens != tokens:
tokens = new_tokens
changed = True
if not changed:
return False
if 'command' in entry:
entry['command'] = shlex.join(tokens)
else:
entry['arguments'] = tokens
return True
def main():
parser = argparse.ArgumentParser(
description='Add sysroot-relative -isystem arguments to build commands in a compile_commands.json file.')
parser.add_argument('compile_commands', help='path to the compile_commands.json file to read')
group = parser.add_mutually_exclusive_group()
group.add_argument('-o', '--output', help='write the result to this file instead of stdout')
group.add_argument('-i', '--in-place', action='store_true', help='overwrite the input file with the result')
parser.add_argument('--isystem-to-i', action='store_true',
help='convert -isystem arguments to -I (except excluded folders)')
parser.add_argument('--exclude-folder', action='append', default=[], metavar='FOLDER',
help='folder name to keep as -isystem when using --isystem-to-i; can be given multiple times')
parser.add_argument('--remove-include-path', action='append', default=[], metavar='PATH',
help='remove -I arguments whose path contains PATH; can be given multiple times')
args = parser.parse_args()
with open(args.compile_commands, encoding='utf-8') as f:
entries = json.load(f)
changed = 0
for entry in entries:
if tweak_entry(entry, args.isystem_to_i, args.exclude_folder, args.remove_include_path):
changed += 1
out = json.dumps(entries, indent=2) + '\n'
if args.in_place:
with open(args.compile_commands, 'w', encoding='utf-8') as f:
f.write(out)
elif args.output:
with open(args.output, 'w', encoding='utf-8') as f:
f.write(out)
else:
sys.stdout.write(out)
print('tweaked {} of {} entries'.format(changed, len(entries)), file=sys.stderr)
if __name__ == '__main__':
main()