-
Notifications
You must be signed in to change notification settings - Fork 283
/
fix-compilation-database.py
executable file
·37 lines (32 loc) · 1.21 KB
/
fix-compilation-database.py
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
#!/usr/bin/env python3
"""
Fixes compilation database used by run-clang-tidy.py on Windows.
"""
import json
import os
import re
import shlex
compile_db = "build/compile_commands.json"
starts_with_drive_letter = re.compile("^/(\w)/(.*)$")
data = None
with open(compile_db, 'r', encoding="utf-8") as fs:
data = json.load( fs )
for j in range(len(data)):
directory = data[j]["directory"]
command = shlex.split(data[j]["command"])
i = 0
while i < len(command):
if command[i].startswith("@"):
rsp_path = os.path.join(directory, command[i][1:])
with open(rsp_path, 'r', encoding="utf-8") as rsp:
newflags = shlex.split(rsp.read())
command = command[:i] + newflags + command[i+1:]
i = i + len(newflags)
else:
match_result = starts_with_drive_letter.match(command[i])
if match_result:
command[i] = f"{match_result.group(1)}:/{match_result.group(2)}"
i = i + 1
data[j]["command"] = " ".join([shlex.quote(s) for s in command])
with open(compile_db, 'w', encoding="utf-8") as fs:
json.dump(data, fs, indent=2)