-
Notifications
You must be signed in to change notification settings - Fork 1
/
fix_compile_commands.py
72 lines (61 loc) · 2.39 KB
/
fix_compile_commands.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
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
import os
import sys
import time
import json
import subprocess
from typing import TypedDict
class CompileCommand(TypedDict):
directory: str
command: str
file: str
output: str
if __name__ == "__main__":
timeout = 5.0
compile_commands_json = "compile_commands.json"
extra_flags = " " + os.environ["EXTRA_FLAGS"]
if len(sys.argv) > 1 and sys.argv[1] == "--watcher":
file = open("fix_compile_commands.log", "w", buffering=1)
sys.stdout = file
sys.stderr = file
started = time.time()
print(f"Waiting for {compile_commands_json} to be created...")
while not os.path.exists(compile_commands_json):
time.sleep(0.1)
if time.time() - started > timeout:
print(f"File not created within {timeout} seconds: {compile_commands_json}")
sys.exit(1)
while True:
json_contents = ""
try:
with open(compile_commands_json) as fp:
json_contents = fp.read().strip()
except Exception as x:
print(f"Could not read {compile_commands_json}: {x}")
elapsed = time.time() - started
if json_contents.endswith("]"):
elapsed = time.time() - started
print(f"File finished in {elapsed:.2f} seconds: {compile_commands_json}")
break
if elapsed > timeout:
print(f"File not finished within {timeout} seconds: {compile_commands_json}")
sys.exit(1)
time.sleep(0.1)
# Append the extra flags to the command
os.remove(compile_commands_json)
compile_commands: list[CompileCommand] = json.loads(json_contents)
print(f"before: {json.dumps(compile_commands, indent=2)}")
for compile_command in compile_commands:
compile_command["command"] += extra_flags
print(f"after: {json.dumps(compile_commands, indent=2)}")
with open(compile_commands_json, "w") as fp:
json.dump(compile_commands, fp, indent=2)
else:
if os.path.exists(compile_commands_json):
os.remove(compile_commands_json)
compile_command = [sys.executable, __file__, "--watcher"]
subprocess.Popen(
compile_command,
start_new_session=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)