forked from PolyArch/gem-forge-gem5
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix_compile_command.py
More file actions
44 lines (39 loc) · 1.35 KB
/
fix_compile_command.py
File metadata and controls
44 lines (39 loc) · 1.35 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
import os
import sys
import subprocess
data = eval(''.join(open(sys.argv[1]).readlines()))
extra_inc_path = sys.argv[2] if len(sys.argv) >= 3 else None
print('[')
first = True
for entry in data:
if not first:
print(',')
f = '%s/%s' % (entry['directory'], entry['file'])
len_prefix = len(entry['directory'])
if os.path.islink(f):
raw = subprocess.check_output('ls -l %s' % f, shell=True).decode('utf-8')
assert '->' in raw
tokens = raw.split()
idx = tokens.index('->')
entry['file'] = tokens[idx + 1][len_prefix+1:]
entry['arguments'][-1] = tokens[idx + 1][len_prefix+1:]
print('{\n\t"directory": "%s",\n\t"file": "%s",' % (entry['directory'], tokens[idx + 1][len_prefix+1:]))
else:
print('{\n\t"directory": "%s",\n\t"file": "%s",' % (entry['directory'], entry['file']))
print('\t"arguments": [')
res = []
for elem in entry['arguments']:
if '"' in elem:
elem = elem.replace('"', chr(92) + chr(34))
s = '\t\t"%s"' % elem
res.append(s)
if extra_inc_path:
# Add extra include path here.
res.append(f'\t\t"-I{extra_inc_path}"')
# We always add the directory to the include path.
res.append(f'\t\t"-I{os.path.dirname(f)}"')
print(',\n'.join(res))
print('\t]')
print('}', end='')
first = False
print(']')