forked from graalvm/mx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmx_compdb.py
167 lines (142 loc) · 5.13 KB
/
mx_compdb.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
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
# ----------------------------------------------------------------------------------------------------
#
# Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# This code is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 only, as
# published by the Free Software Foundation.
#
# This code is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# version 2 for more details (a copy is included in the LICENSE file that
# accompanied this code).
#
# You should have received a copy of the GNU General Public License version
# 2 along with this work; if not, write to the Free Software Foundation,
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
# or visit www.oracle.com if you need additional information or have any
# questions.
#
# ----------------------------------------------------------------------------------------------------
import json
import os
import re
import subprocess
from multiprocessing import Lock
import mx
_bear_version_regex = re.compile(r"bear ([0-9]+).([0-9]+).([0-9]+)", re.IGNORECASE)
_bear_version = '<uninitialized>'
def _get_bear_version():
global _bear_version
if _bear_version == '<uninitialized>':
try:
output = mx._check_output_str(['bear', '--version'], stderr=subprocess.STDOUT)
except OSError:
output = ''
m = _bear_version_regex.search(output)
if m:
_bear_version = int(m.group(1))
else:
mx.warn("Could not find bear, will not produce compilation database for make projects.")
_bear_version = None
return _bear_version
def gmake_with_bear(out=None, append=False, context=None):
v = _get_bear_version()
if v is None:
return [mx.gmake_cmd(context=context)]
else:
ret = ['bear']
if append:
ret.append('--append' if v >= 3 else '-a')
if out is not None:
ret.append('--output' if v >= 3 else '-o')
ret.append(out)
if v >= 3:
ret.append('--')
ret.append(mx.gmake_cmd(context=context))
return ret
_compdb_path = None
_compdb_lock = None
def _default_compdb_path():
suite = mx.primary_suite()
if suite is None:
# no primary suite, don't try to enable compdb
return None
if suite.vc_dir:
return os.path.join(os.path.dirname(suite.vc_dir), 'compile_commands.json')
else:
return os.path.join(suite.dir, 'compile_commands.json')
def init():
global _compdb_path
global _compdb_lock
o = mx.get_opts().compdb
if o is None:
o = mx.get_env('MX_COMPDB')
if o is not None and o != 'none':
_compdb_lock = Lock()
if o == 'default':
_compdb_path = _default_compdb_path()
else:
_compdb_path = os.path.abspath(o)
def enabled():
return _compdb_path is not None
def gmake_with_compdb_cmd(context=None):
if enabled():
return gmake_with_bear(append=True, context=context)
else:
return [mx.gmake_cmd(context=context)]
class Compdb:
def __init__(self):
self.content = {}
def __enter__(self):
_compdb_lock.acquire()
if os.path.exists(_compdb_path):
self.mergeFile(_compdb_path)
return self
def __exit__(self, *args):
with open(_compdb_path, 'w') as f:
json.dump(list(self.content.values()), f, indent=4)
_compdb_lock.release()
def merge(self, data):
for item in data:
key = item['file']
if not os.path.isabs(key):
key = os.path.normpath(os.path.join(item['directory'], item['file']))
self.content[item['file']] = item
def mergeString(self, string):
try:
self.merge(json.loads(string))
except json.JSONDecodeError:
mx.warn("Error decoding JSON compilation database. Ignoring.")
def mergeFile(self, path):
with open(path, 'r') as f:
try:
self.merge(json.load(f))
except json.JSONDecodeError:
mx.warn("Error decoding JSON compilation database from '%s'. Ignoring." % path)
class CompdbCapture:
def __init__(self, suite):
self.data = ""
def __call__(self, data):
self.data += data
def __enter__(self):
if enabled():
return self
else:
return None
def __exit__(self, *args):
if enabled():
with Compdb() as db:
db.mergeString(self.data)
def merge_compdb(subject, path):
if enabled():
with Compdb() as db:
inFile = os.path.join(path, 'compile_commands.json')
if os.path.exists(inFile):
db.mergeFile(inFile)
else:
mx.warn("JSON compilation database for %s not found (expected at %s)." % (subject, inFile))