forked from monkey666-cr/bazel-vscode-java-bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBazelBridge.java
More file actions
406 lines (365 loc) · 15.6 KB
/
Copy pathBazelBridge.java
File metadata and controls
406 lines (365 loc) · 15.6 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
package com.bazel.jdt;
import java.util.Objects;
import java.util.concurrent.*;
import java.util.concurrent.locks.ReentrantReadWriteLock;
public final class BazelBridge {
private static final BazelBridge INSTANCE = new BazelBridge();
private static final long JNI_TIMEOUT_SECONDS = 330;
private static final long DISCOVER_TIMEOUT_SECONDS = 1800;
private long handle = -1;
private final ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock();
private volatile ExecutorService jniExecutor = createExecutor();
private String lastWorkspacePath;
private String lastBazelPath;
private String lastCacheDir;
private volatile String dependencyResolutionMode = "transitive";
private volatile String dependencySourceLoadingMode = "full-project";
private volatile String syncMode = "fast";
private volatile String[] cachedDependencyPackages = new String[0];
private volatile BazelProjectView projectView;
private static ExecutorService createExecutor() {
return Executors.newSingleThreadExecutor(r -> {
Thread t = new Thread(r, "bazel-jdt-native");
t.setDaemon(true);
return t;
});
}
static {
NativeLoader.load();
}
private BazelBridge() {}
public static BazelBridge getInstance() {
return INSTANCE;
}
public void initialize(String workspacePath, String bazelPath, String cacheDir) {
rwLock.writeLock().lock();
try {
if (handle != -1
&& Objects.equals(workspacePath, lastWorkspacePath)
&& Objects.equals(bazelPath, lastBazelPath)
&& Objects.equals(cacheDir, lastCacheDir)) {
return;
}
if (handle != -1) {
nativeShutdown(handle);
handle = -1;
}
if (jniExecutor.isShutdown() || jniExecutor.isTerminated()) {
jniExecutor = createExecutor();
}
handle = nativeInitialize(workspacePath, bazelPath, cacheDir);
lastWorkspacePath = workspacePath;
lastBazelPath = bazelPath;
lastCacheDir = cacheDir;
projectView = null;
} finally {
rwLock.writeLock().unlock();
}
}
public void shutdown() {
rwLock.writeLock().lock();
try {
jniExecutor.shutdownNow();
try {
jniExecutor.awaitTermination(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
if (handle != -1) {
nativeShutdown(handle);
handle = -1;
}
lastWorkspacePath = null;
lastBazelPath = null;
lastCacheDir = null;
} finally {
rwLock.writeLock().unlock();
}
}
public String getWorkspacePath() {
rwLock.readLock().lock();
try {
return lastWorkspacePath;
} finally {
rwLock.readLock().unlock();
}
}
public String[] discoverTargets(String[] scopePatterns) {
return discoverTargets(scopePatterns, null);
}
public String[] discoverTargets(String[] scopePatterns, String[] buildFlags) {
String[] targets = queryTargets(scopePatterns);
if (targets == null || targets.length == 0) return targets;
populateGraph();
return runAspectBuild(targets, buildFlags);
}
public String[] queryTargets(String[] scopePatterns) {
long h = snapshotHandle();
try {
return jniExecutor.submit(() -> nativeQueryTargets(h, scopePatterns))
.get(DISCOVER_TIMEOUT_SECONDS, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("Interrupted during queryTargets", e);
} catch (ExecutionException e) {
Throwable cause = e.getCause();
if (cause instanceof RuntimeException) throw (RuntimeException) cause;
throw new RuntimeException("queryTargets failed", cause);
} catch (TimeoutException e) {
throw new RuntimeException("queryTargets timed out", e);
}
}
public void populateGraph() {
long h = snapshotHandle();
try {
jniExecutor.submit(() -> { nativePopulateGraph(h); return null; })
.get(DISCOVER_TIMEOUT_SECONDS, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("Interrupted during populateGraph", e);
} catch (ExecutionException e) {
Throwable cause = e.getCause();
if (cause instanceof RuntimeException) throw (RuntimeException) cause;
throw new RuntimeException("populateGraph failed", cause);
} catch (TimeoutException e) {
throw new RuntimeException("populateGraph timed out", e);
}
}
public String[] runAspectBuild(String[] targets, String[] buildFlags) {
long h = snapshotHandle();
String mode = this.syncMode;
try {
return jniExecutor.submit(() -> nativeRunAspectBuild(h, targets, buildFlags, mode))
.get(DISCOVER_TIMEOUT_SECONDS, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("Interrupted during runAspectBuild", e);
} catch (ExecutionException e) {
Throwable cause = e.getCause();
if (cause instanceof RuntimeException) throw (RuntimeException) cause;
throw new RuntimeException("runAspectBuild failed", cause);
} catch (TimeoutException e) {
throw new RuntimeException("runAspectBuild timed out", e);
}
}
public boolean buildTargets(String[] targets, String[] buildFlags) {
long h = snapshotHandle();
try {
return jniExecutor.submit(() -> nativeBuildTargets(h, targets, buildFlags))
.get(DISCOVER_TIMEOUT_SECONDS, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("Interrupted during buildTargets", e);
} catch (ExecutionException e) {
Throwable cause = e.getCause();
if (cause instanceof RuntimeException) throw (RuntimeException) cause;
throw new RuntimeException("buildTargets failed", cause);
} catch (TimeoutException e) {
throw new RuntimeException("buildTargets timed out", e);
}
}
public String[] computeClasspath(String targetLabel) {
long h = snapshotHandle();
try {
return jniExecutor.submit(() -> nativeComputeClasspath(h, targetLabel, null))
.get(JNI_TIMEOUT_SECONDS, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("Interrupted during computeClasspath", e);
} catch (ExecutionException e) {
Throwable cause = e.getCause();
if (cause instanceof RuntimeException) throw (RuntimeException) cause;
throw new RuntimeException("computeClasspath failed for " + targetLabel, cause);
} catch (TimeoutException e) {
throw new RuntimeException("computeClasspath timed out for " + targetLabel, e);
}
}
public String[] computeClasspathMerged(String[] labels) {
if (labels == null || labels.length == 0) {
return new String[0];
}
long h = snapshotHandle();
try {
return jniExecutor.submit(() -> nativeComputeClasspathMerged(h, labels))
.get(JNI_TIMEOUT_SECONDS, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("Interrupted during computeClasspathMerged", e);
} catch (ExecutionException e) {
Throwable cause = e.getCause();
if (cause instanceof RuntimeException) throw (RuntimeException) cause;
throw new RuntimeException("computeClasspathMerged failed", cause);
} catch (TimeoutException e) {
throw new RuntimeException("computeClasspathMerged timed out", e);
}
}
private static final int SYNC_STATE_DEAD = 3;
public int getSyncState() {
// Safe to bypass executor: nativeGetSyncState performs a single atomic read
// of the sync state field in BazelJdtState — no locks, no I/O, no blocking.
long h = snapshotHandleNullable();
if (h == -1) return SYNC_STATE_DEAD;
return nativeGetSyncState(h);
}
public boolean isInitialized() {
rwLock.readLock().lock();
try {
return handle != -1;
} finally {
rwLock.readLock().unlock();
}
}
public void setDependencyResolutionMode(String mode) {
this.dependencyResolutionMode = mode;
}
public String getDependencyResolutionMode() {
return this.dependencyResolutionMode;
}
public void setDependencySourceLoadingMode(String mode) {
this.dependencySourceLoadingMode = mode;
}
public String getDependencySourceLoadingMode() {
return this.dependencySourceLoadingMode;
}
public void setSyncMode(String mode) {
this.syncMode = mode;
}
public String getSyncMode() {
return this.syncMode;
}
public void setCachedDependencyPackages(String[] packages) {
this.cachedDependencyPackages = packages != null ? packages : new String[0];
}
public String[] getCachedDependencyPackages() {
return this.cachedDependencyPackages;
}
public void setProjectView(BazelProjectView view) {
this.projectView = view;
}
public BazelProjectView getProjectView() {
return this.projectView;
}
public String[] getBuildFlags() {
BazelProjectView view = this.projectView;
if (view == null || view.getBuildFlags().isEmpty()) {
return null;
}
return view.getBuildFlags().toArray(new String[0]);
}
public void cleanCache() {
long h = snapshotHandle();
try {
jniExecutor.submit(() -> { nativeCleanCache(h); return null; })
.get(JNI_TIMEOUT_SECONDS, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("Interrupted during cleanCache", e);
} catch (ExecutionException e) {
Throwable cause = e.getCause();
if (cause instanceof RuntimeException) throw (RuntimeException) cause;
throw new RuntimeException("cleanCache failed", cause);
} catch (TimeoutException e) {
throw new RuntimeException("cleanCache timed out", e);
}
}
public String[] getTransitiveWorkspaceDeps(String[] targetLabels) {
long h = snapshotHandle();
try {
return jniExecutor.submit(() -> nativeGetTransitiveWorkspaceDeps(h, targetLabels))
.get(JNI_TIMEOUT_SECONDS, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("Interrupted during getTransitiveWorkspaceDeps", e);
} catch (ExecutionException e) {
Throwable cause = e.getCause();
if (cause instanceof RuntimeException) throw (RuntimeException) cause;
throw new RuntimeException("getTransitiveWorkspaceDeps failed", cause);
} catch (TimeoutException e) {
throw new RuntimeException("getTransitiveWorkspaceDeps timed out", e);
}
}
public String[] syncIncremental(String[] changedFilePaths) {
long h = snapshotHandle();
try {
return jniExecutor.submit(() -> nativeSyncIncremental(h, changedFilePaths))
.get(JNI_TIMEOUT_SECONDS, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("Interrupted during syncIncremental", e);
} catch (ExecutionException e) {
Throwable cause = e.getCause();
if (cause instanceof RuntimeException) throw (RuntimeException) cause;
throw new RuntimeException("syncIncremental failed", cause);
} catch (TimeoutException e) {
throw new RuntimeException("syncIncremental timed out", e);
}
}
public void updateWatchPaths(String[] watchPaths) {
long h = snapshotHandle();
try {
jniExecutor.submit(() -> {
nativeUpdateWatchPaths(h, watchPaths != null ? watchPaths : new String[0]);
return null;
}).get(JNI_TIMEOUT_SECONDS, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("Interrupted during updateWatchPaths", e);
} catch (ExecutionException e) {
Throwable cause = e.getCause();
if (cause instanceof RuntimeException) throw (RuntimeException) cause;
throw new RuntimeException("updateWatchPaths failed", cause);
} catch (TimeoutException e) {
throw new RuntimeException("updateWatchPaths timed out", e);
}
}
public String[] getPendingChanges() {
long h = snapshotHandleNullable();
if (h == -1) return new String[0];
return nativeGetPendingChanges(h);
}
private long snapshotHandle() {
rwLock.readLock().lock();
try {
if (handle == -1) {
throw new IllegalStateException("BazelBridge not initialized");
}
return handle;
} finally {
rwLock.readLock().unlock();
}
}
private long snapshotHandleNullable() {
rwLock.readLock().lock();
try {
return handle;
} finally {
rwLock.readLock().unlock();
}
}
private native long nativeInitialize(String workspacePath, String bazelPath, String cacheDir);
private native void nativeShutdown(long handle);
private native void nativeUpdateWatchPaths(long handle, String[] watchPaths);
private native String[] nativeQueryTargets(long handle, String[] scopePatterns);
private native void nativePopulateGraph(long handle);
private native String[] nativeRunAspectBuild(long handle, String[] targets, String[] buildFlags, String syncMode);
private native boolean nativeBuildTargets(long handle, String[] targets, String[] buildFlags);
private native String[] nativeComputeClasspath(long handle, String targetLabel, String[] buildFlags);
private native String[] nativeComputeClasspathMerged(long handle, String[] labels);
private native int nativeGetSyncState(long handle);
private native void nativeCleanCache(long handle);
private native String[] nativeGetPendingChanges(long handle);
private native String[] nativeGetTransitiveWorkspaceDeps(long handle, String[] targetLabels);
private native String[] nativeSyncIncremental(long handle, String[] changedFilePaths);
private native String nativeGetAspectBuildStats(long handle);
private native boolean nativeIsTestTarget(long handle, String targetLabel);
public String getAspectBuildStats() {
long h = snapshotHandleNullable();
if (h == -1) return null;
return nativeGetAspectBuildStats(h);
}
public boolean isTestTarget(String targetLabel) {
long h = snapshotHandleNullable();
if (h == -1) return false;
return nativeIsTestTarget(h, targetLabel);
}
}