-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
JavaCompileAction.java
873 lines (806 loc) · 36.8 KB
/
JavaCompileAction.java
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
// Copyright 2019 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.devtools.build.lib.rules.java;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static com.google.devtools.build.lib.actions.ActionAnalysisMetadata.mergeMaps;
import static java.nio.charset.StandardCharsets.ISO_8859_1;
import static java.util.concurrent.TimeUnit.SECONDS;
import static java.util.stream.Collectors.joining;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Joiner;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import com.google.common.flogger.GoogleLogger;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.devtools.build.lib.actions.AbstractAction;
import com.google.devtools.build.lib.actions.ActionContinuationOrResult;
import com.google.devtools.build.lib.actions.ActionEnvironment;
import com.google.devtools.build.lib.actions.ActionExecutionContext;
import com.google.devtools.build.lib.actions.ActionExecutionException;
import com.google.devtools.build.lib.actions.ActionInput;
import com.google.devtools.build.lib.actions.ActionKeyContext;
import com.google.devtools.build.lib.actions.ActionOwner;
import com.google.devtools.build.lib.actions.ActionResult;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.actions.BaseSpawn;
import com.google.devtools.build.lib.actions.CommandAction;
import com.google.devtools.build.lib.actions.CommandLine;
import com.google.devtools.build.lib.actions.CommandLineExpansionException;
import com.google.devtools.build.lib.actions.CommandLines;
import com.google.devtools.build.lib.actions.CommandLines.CommandLineAndParamFileInfo;
import com.google.devtools.build.lib.actions.EmptyRunfilesSupplier;
import com.google.devtools.build.lib.actions.EnvironmentalExecException;
import com.google.devtools.build.lib.actions.ExecException;
import com.google.devtools.build.lib.actions.ParamFileInfo;
import com.google.devtools.build.lib.actions.ParameterFile;
import com.google.devtools.build.lib.actions.PathStripper;
import com.google.devtools.build.lib.actions.ResourceSet;
import com.google.devtools.build.lib.actions.RunfilesSupplier;
import com.google.devtools.build.lib.actions.Spawn;
import com.google.devtools.build.lib.actions.SpawnContinuation;
import com.google.devtools.build.lib.actions.SpawnResult;
import com.google.devtools.build.lib.actions.extra.ExtraActionInfo;
import com.google.devtools.build.lib.analysis.actions.CustomCommandLine;
import com.google.devtools.build.lib.analysis.config.BuildConfigurationValue;
import com.google.devtools.build.lib.analysis.starlark.Args;
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
import com.google.devtools.build.lib.collect.nestedset.Order;
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadCompatible;
import com.google.devtools.build.lib.exec.SpawnStrategyResolver;
import com.google.devtools.build.lib.rules.java.JavaConfiguration.JavaClasspathMode;
import com.google.devtools.build.lib.rules.java.JavaPluginInfo.JavaPluginData;
import com.google.devtools.build.lib.server.FailureDetails.FailureDetail;
import com.google.devtools.build.lib.server.FailureDetails.JavaCompile;
import com.google.devtools.build.lib.server.FailureDetails.JavaCompile.Code;
import com.google.devtools.build.lib.starlarkbuildapi.CommandLineArgsApi;
import com.google.devtools.build.lib.util.DetailedExitCode;
import com.google.devtools.build.lib.util.Fingerprint;
import com.google.devtools.build.lib.util.OnDemandString;
import com.google.devtools.build.lib.vfs.FileSystemUtils;
import com.google.devtools.build.lib.vfs.Path;
import com.google.devtools.build.lib.vfs.PathFragment;
import com.google.devtools.build.lib.view.proto.Deps;
import com.google.protobuf.ExtensionRegistry;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.function.Function;
import javax.annotation.Nullable;
import net.starlark.java.eval.EvalException;
import net.starlark.java.eval.Sequence;
import net.starlark.java.eval.StarlarkList;
/** Action that represents a Java compilation. */
@ThreadCompatible
@Immutable
public final class JavaCompileAction extends AbstractAction implements CommandAction {
private static final GoogleLogger logger = GoogleLogger.forEnclosingClass();
private static final ResourceSet LOCAL_RESOURCES =
ResourceSet.createWithRamCpu(/* memoryMb= */ 750, /* cpuUsage= */ 1);
private static final UUID GUID = UUID.fromString("e423747c-2827-49e6-b961-f6c08c10bb51");
private static final ParamFileInfo PARAM_FILE_INFO =
ParamFileInfo.builder(ParameterFile.ParameterFileType.UNQUOTED)
.setCharset(ISO_8859_1)
.setUseAlways(true)
.build();
enum CompilationType {
JAVAC("Javac"),
// TODO(cushon): rename the mnemonic to 'Turbine' after javac-turbine is turned down (and after
// collecting data on the perform impact of the turndown)
TURBINE("JavacTurbine");
final String mnemonic;
CompilationType(String mnemonic) {
this.mnemonic = mnemonic;
}
}
private final CompilationType compilationType;
private final ImmutableMap<String, String> executionInfo;
private final CommandLine executableLine;
private final CommandLine flagLine;
private final BuildConfigurationValue configuration;
private final OnDemandString progressMessage;
private final NestedSet<Artifact> directJars;
private final NestedSet<Artifact> mandatoryInputs;
private final NestedSet<Artifact> transitiveInputs;
private final NestedSet<Artifact> dependencyArtifacts;
@Nullable private final Artifact outputDepsProto;
private final JavaClasspathMode classpathMode;
@Nullable private final ExtraActionInfoSupplier extraActionInfoSupplier;
public JavaCompileAction(
CompilationType compilationType,
ActionOwner owner,
ActionEnvironment env,
NestedSet<Artifact> tools,
RunfilesSupplier runfilesSupplier,
OnDemandString progressMessage,
NestedSet<Artifact> mandatoryInputs,
NestedSet<Artifact> transitiveInputs,
NestedSet<Artifact> directJars,
ImmutableSet<Artifact> outputs,
ImmutableMap<String, String> executionInfo,
ExtraActionInfoSupplier extraActionInfoSupplier,
CommandLine executableLine,
CommandLine flagLine,
BuildConfigurationValue configuration,
NestedSet<Artifact> dependencyArtifacts,
Artifact outputDepsProto,
JavaClasspathMode classpathMode) {
super(
owner,
tools,
allInputs(mandatoryInputs, transitiveInputs, dependencyArtifacts),
runfilesSupplier,
outputs,
env);
if (outputs.stream().anyMatch(Artifact::isTreeArtifact)) {
throw new IllegalArgumentException(
String.format(
"Unexpected tree artifact output(s): [%s] in JavaCompileAction: %s",
outputs.stream()
.filter(Artifact::isTreeArtifact)
.map(Artifact::getExecPathString)
.collect(joining(",")),
this));
}
this.compilationType = compilationType;
// TODO(djasper): The only thing that is conveyed through the executionInfo is whether worker
// mode is enabled or not. Investigate whether we can store just that.
this.executionInfo =
configuration.modifiedExecutionInfo(executionInfo, compilationType.mnemonic);
this.executableLine = executableLine;
this.flagLine = flagLine;
this.configuration = configuration;
this.progressMessage = progressMessage;
this.extraActionInfoSupplier = extraActionInfoSupplier;
this.directJars = directJars;
this.mandatoryInputs = mandatoryInputs;
this.transitiveInputs = transitiveInputs;
this.dependencyArtifacts = dependencyArtifacts;
this.outputDepsProto = outputDepsProto;
this.classpathMode = classpathMode;
checkState(
outputDepsProto != null || classpathMode != JavaClasspathMode.BAZEL,
"Cannot have null outputDepsProto with reduced class path mode BAZEL %s",
describe());
}
/** Computes all of a {@link JavaCompileAction}'s inputs. */
static NestedSet<Artifact> allInputs(
NestedSet<Artifact> mandatoryInputs,
NestedSet<Artifact> transitiveInputs,
NestedSet<Artifact> dependencyArtifacts) {
return NestedSetBuilder.<Artifact>stableOrder()
.addTransitive(mandatoryInputs)
.addTransitive(transitiveInputs)
.addTransitive(dependencyArtifacts)
.build();
}
@Override
public String getMnemonic() {
return compilationType.mnemonic;
}
@Override
protected void computeKey(
ActionKeyContext actionKeyContext,
@Nullable Artifact.ArtifactExpander artifactExpander,
Fingerprint fp)
throws CommandLineExpansionException, InterruptedException {
fp.addUUID(GUID);
fp.addInt(classpathMode.ordinal());
executableLine.addToFingerprint(actionKeyContext, artifactExpander, fp);
flagLine.addToFingerprint(actionKeyContext, artifactExpander, fp);
// As the classpath is no longer part of commandLines implicitly, we need to explicitly add
// the transitive inputs to the key here.
actionKeyContext.addNestedSetToFingerprint(fp, transitiveInputs);
// We don't need the toolManifests here, because they are a subset of the inputManifests by
// definition and the output of an action shouldn't change whether something is considered a
// tool or not.
fp.addPaths(getRunfilesSupplier().getRunfilesDirs());
ImmutableList<Artifact> runfilesManifests = getRunfilesSupplier().getManifests();
fp.addInt(runfilesManifests.size());
for (Artifact runfilesManifest : runfilesManifests) {
fp.addPath(runfilesManifest.getExecPath());
}
env.addTo(fp);
fp.addStringMap(executionInfo);
}
/**
* Compute a reduced classpath that is comprised of the header jars of all the direct dependencies
* and the jars needed to build those (read from the produced .jdeps file). This duplicates the
* logic from {@link
* com.google.devtools.build.buildjar.javac.plugins.dependency.DependencyModule#computeStrictClasspath}.
*/
@VisibleForTesting
ReducedClasspath getReducedClasspath(
ActionExecutionContext actionExecutionContext, JavaCompileActionContext context)
throws IOException {
HashSet<String> direct = new HashSet<>();
for (Artifact directJar : directJars.toList()) {
direct.add(directJar.getExecPathString());
}
for (Artifact depArtifact : dependencyArtifacts.toList()) {
for (Deps.Dependency dep :
context.getDependencies(depArtifact, actionExecutionContext).getDependencyList()) {
direct.add(dep.getPath());
}
}
ImmutableList<Artifact> transitiveCollection = transitiveInputs.toList();
ImmutableList<Artifact> reducedJars =
ImmutableList.copyOf(
Iterables.filter(
transitiveCollection, input -> direct.contains(input.getExecPathString())));
return new ReducedClasspath(reducedJars, transitiveCollection.size());
}
/**
* Simpliar to {@link
* com.google.devtools.build.lib.analysis.actions.SpawnAction.ExtraActionInfoSupplier} but
* additionally includes the spawn arguments, which change between direct and fallback
* invocations.
*/
interface ExtraActionInfoSupplier {
void extend(ExtraActionInfo.Builder builder, ImmutableList<String> arguments);
}
static class ReducedClasspath {
final NestedSet<Artifact> reducedJars;
final int reducedLength;
final int fullLength;
ReducedClasspath(ImmutableList<Artifact> reducedJars, int fullLength) {
this.reducedJars = NestedSetBuilder.wrap(Order.STABLE_ORDER, reducedJars);
this.reducedLength = reducedJars.size();
this.fullLength = fullLength;
}
}
/** Should we strip config prefixes from executor output paths for this compilation? */
private boolean stripOutputPaths() {
return JavaCompilationHelper.stripOutputPaths(
allInputs(mandatoryInputs, transitiveInputs, dependencyArtifacts), configuration);
}
@VisibleForTesting
JavaSpawn getReducedSpawn(
ActionExecutionContext actionExecutionContext,
ReducedClasspath reducedClasspath,
boolean fallback)
throws CommandLineExpansionException, InterruptedException {
CustomCommandLine.Builder classpathLine = CustomCommandLine.builder();
boolean stripOutputPaths = stripOutputPaths();
if (stripOutputPaths) {
classpathLine.stripOutputPaths(JavaCompilationHelper.outputBase(getPrimaryOutput()));
}
if (fallback) {
classpathLine.addExecPaths("--classpath", transitiveInputs);
} else {
classpathLine.addExecPaths("--classpath", reducedClasspath.reducedJars);
}
// These flags instruct JavaBuilder that this is a compilation with a reduced classpath and
// that it should report a special value back if a compilation error occurs that suggests
// retrying with the full classpath.
classpathLine.add("--reduce_classpath_mode", fallback ? "BAZEL_FALLBACK" : "BAZEL_REDUCED");
classpathLine.add("--full_classpath_length", Integer.toString(reducedClasspath.fullLength));
classpathLine.add(
"--reduced_classpath_length", Integer.toString(reducedClasspath.reducedLength));
CommandLines reducedCommandLine =
CommandLines.builder()
.addCommandLine(executableLine)
.addCommandLine(flagLine, PARAM_FILE_INFO)
.addCommandLine(classpathLine.build(), PARAM_FILE_INFO)
.build();
CommandLines.ExpandedCommandLines expandedCommandLines =
reducedCommandLine.expand(
actionExecutionContext.getArtifactExpander(),
getPrimaryOutput().getExecPath(),
stripOutputPaths ? PathStripper::strip : Function.identity(),
configuration.getCommandLineLimits());
NestedSet<Artifact> inputs =
NestedSetBuilder.<Artifact>stableOrder()
.addTransitive(mandatoryInputs)
.addTransitive(fallback ? transitiveInputs : reducedClasspath.reducedJars)
.build();
return new JavaSpawn(
expandedCommandLines,
getEffectiveEnvironment(actionExecutionContext.getClientEnv()),
getExecutionInfo(),
inputs,
/*onlyMandatoryOutput=*/ fallback ? null : outputDepsProto,
stripOutputPaths);
}
private JavaSpawn getFullSpawn(ActionExecutionContext actionExecutionContext)
throws CommandLineExpansionException, InterruptedException {
boolean stripOutputPaths = stripOutputPaths();
CommandLines.ExpandedCommandLines expandedCommandLines =
getCommandLines()
.expand(
actionExecutionContext.getArtifactExpander(),
getPrimaryOutput().getExecPath(),
stripOutputPaths ? PathStripper::strip : Function.identity(),
configuration.getCommandLineLimits());
return new JavaSpawn(
expandedCommandLines,
getEffectiveEnvironment(actionExecutionContext.getClientEnv()),
getExecutionInfo(),
NestedSetBuilder.<Artifact>stableOrder()
.addTransitive(mandatoryInputs)
.addTransitive(transitiveInputs)
// Full spawn mode means classPathMode != JavaClasspathMode.BAZEL, which means
// JavaBuilder may read .jdeps files to perform classpath reduction on the executor. So
// make sure these files are staged as inputs to the executor action.
//
// Contrast this with getReducedSpawn, which reduces the classpath in the Blaze process
// *before* sending actions to the executor. In those cases we want to avoid staging
// .jdeps files, which have config prefixes in output paths, which compromise caching
// possible by stripping prefixes on the executor.
.addTransitive(dependencyArtifacts)
.build(),
/*onlyMandatoryOutput=*/ null,
stripOutputPaths);
}
@Override
public ImmutableMap<String, String> getEffectiveEnvironment(Map<String, String> clientEnv) {
LinkedHashMap<String, String> effectiveEnvironment =
Maps.newLinkedHashMapWithExpectedSize(env.size());
env.resolve(effectiveEnvironment, clientEnv);
return ImmutableMap.copyOf(effectiveEnvironment);
}
@Override
public ActionContinuationOrResult beginExecution(ActionExecutionContext actionExecutionContext)
throws ActionExecutionException, InterruptedException {
ReducedClasspath reducedClasspath;
Spawn spawn;
try {
if (classpathMode == JavaClasspathMode.BAZEL) {
JavaCompileActionContext context =
actionExecutionContext.getContext(JavaCompileActionContext.class);
try {
reducedClasspath = getReducedClasspath(actionExecutionContext, context);
} catch (IOException e) {
throw createActionExecutionException(e, Code.REDUCED_CLASSPATH_FAILURE);
}
spawn = getReducedSpawn(actionExecutionContext, reducedClasspath, /* fallback= */ false);
} else {
reducedClasspath = null;
spawn = getFullSpawn(actionExecutionContext);
}
} catch (CommandLineExpansionException e) {
throw createActionExecutionException(e, Code.COMMAND_LINE_EXPANSION_FAILURE);
}
SpawnContinuation spawnContinuation =
actionExecutionContext
.getContext(SpawnStrategyResolver.class)
.beginExecution(spawn, actionExecutionContext);
return new JavaActionContinuation(actionExecutionContext, reducedClasspath, spawnContinuation);
}
@Override
protected String getRawProgressMessage() {
return progressMessage.toString();
}
static class ProgressMessage extends OnDemandString {
private final String prefix;
private final Artifact output;
private final ImmutableSet<Artifact> sourceFiles;
private final ImmutableList<Artifact> sourceJars;
private final JavaPluginData plugins;
ProgressMessage(
String prefix,
Artifact output,
ImmutableSet<Artifact> sourceFiles,
ImmutableList<Artifact> sourceJars,
JavaPluginData plugins) {
this.prefix = prefix;
this.output = output;
this.sourceFiles = sourceFiles;
this.sourceJars = sourceJars;
this.plugins = plugins;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder(prefix);
sb.append(' ');
sb.append(output.prettyPrint());
sb.append(" (");
boolean first = true;
first = appendCount(sb, first, sourceFiles.size(), "source file");
appendCount(sb, first, sourceJars.size(), "source jar");
sb.append(")");
appendProcessorNames(sb, plugins.processorClasses());
return sb.toString();
}
private static void appendProcessorNames(StringBuilder sb, NestedSet<String> processorClasses) {
if (processorClasses.isEmpty()) {
return;
}
List<String> shortNames = new ArrayList<>();
for (String name : processorClasses.toList()) {
// Annotation processor names are qualified class names. Omit the package part for the
// progress message, e.g. `com.google.Foo` -> `Foo`.
int idx = name.lastIndexOf('.');
String shortName = idx != -1 ? name.substring(idx + 1) : name;
shortNames.add(shortName);
}
sb.append(" and running annotation processors (");
Joiner.on(", ").appendTo(sb, shortNames);
sb.append(")");
}
/**
* Append an input count to the progress message, e.g. "2 source jars". If an input count has
* already been appended, prefix with ", ".
*/
private static boolean appendCount(StringBuilder sb, boolean first, int count, String name) {
if (count > 0) {
if (!first) {
sb.append(", ");
} else {
first = false;
}
sb.append(count).append(' ').append(name);
if (count > 1) {
sb.append('s');
}
}
return first;
}
}
@Override
public ExtraActionInfo.Builder getExtraActionInfo(ActionKeyContext actionKeyContext)
throws CommandLineExpansionException, InterruptedException {
ExtraActionInfo.Builder builder = super.getExtraActionInfo(actionKeyContext);
CommandLines commandLinesWithoutExecutable =
CommandLines.builder()
.addCommandLine(flagLine)
.addCommandLine(getFullClasspathLine())
.build();
if (extraActionInfoSupplier != null) {
extraActionInfoSupplier.extend(builder, commandLinesWithoutExecutable.allArguments());
}
return builder;
}
private final class JavaSpawn extends BaseSpawn {
private final NestedSet<ActionInput> inputs;
private final Artifact onlyMandatoryOutput;
private final boolean stripOutputPaths;
JavaSpawn(
CommandLines.ExpandedCommandLines expandedCommandLines,
Map<String, String> environment,
Map<String, String> executionInfo,
NestedSet<Artifact> inputs,
@Nullable Artifact onlyMandatoryOutput,
boolean stripOutputPaths) {
super(
ImmutableList.copyOf(expandedCommandLines.arguments()),
environment,
executionInfo,
EmptyRunfilesSupplier.INSTANCE,
JavaCompileAction.this,
LOCAL_RESOURCES);
this.onlyMandatoryOutput = onlyMandatoryOutput;
this.inputs =
NestedSetBuilder.<ActionInput>fromNestedSet(inputs)
.addAll(expandedCommandLines.getParamFiles())
.build();
this.stripOutputPaths = stripOutputPaths;
}
@Override
public NestedSet<? extends ActionInput> getInputFiles() {
return inputs;
}
@Override
public boolean isMandatoryOutput(ActionInput output) {
return onlyMandatoryOutput == null || onlyMandatoryOutput.equals(output);
}
@Override
public boolean stripOutputPaths() {
return stripOutputPaths;
}
}
@VisibleForTesting
public CommandLines getCommandLines() {
return CommandLines.builder()
.addCommandLine(executableLine)
.addCommandLine(flagLine, PARAM_FILE_INFO)
.addCommandLine(getFullClasspathLine(), PARAM_FILE_INFO)
.build();
}
private CommandLine getFullClasspathLine() {
CustomCommandLine.Builder classpathLine =
CustomCommandLine.builder().addExecPaths("--classpath", transitiveInputs);
if (stripOutputPaths()) {
classpathLine.stripOutputPaths(JavaCompilationHelper.outputBase(getPrimaryOutput()));
}
if (classpathMode == JavaClasspathMode.JAVABUILDER) {
classpathLine.add("--reduce_classpath_mode", "JAVABUILDER_REDUCED");
if (!dependencyArtifacts.isEmpty()) {
classpathLine.addExecPaths("--deps_artifacts", dependencyArtifacts);
}
}
return classpathLine.build();
}
@Override
public Sequence<String> getStarlarkArgv() throws EvalException, InterruptedException {
try {
return StarlarkList.immutableCopyOf(getArguments());
} catch (CommandLineExpansionException ex) {
throw new EvalException(ex);
}
}
/** Returns the out-of-band execution data for this action. */
@Override
public ImmutableMap<String, String> getExecutionInfo() {
return mergeMaps(super.getExecutionInfo(), executionInfo);
}
@Override
public List<String> getArguments() throws CommandLineExpansionException, InterruptedException {
return ImmutableList.copyOf(getCommandLines().allArguments());
}
@Override
public Sequence<CommandLineArgsApi> getStarlarkArgs() throws EvalException {
ImmutableList.Builder<CommandLineArgsApi> result = ImmutableList.builder();
ImmutableSet<Artifact> directoryInputs =
getInputs().toList().stream().filter(Artifact::isDirectory).collect(toImmutableSet());
for (CommandLineAndParamFileInfo commandLine : getCommandLines().getCommandLines()) {
result.add(Args.forRegisteredAction(commandLine, directoryInputs));
}
return StarlarkList.immutableCopyOf(result.build());
}
@Override
@VisibleForTesting
public final ImmutableMap<String, String> getIncompleteEnvironmentForTesting() {
// TODO(ulfjack): AbstractAction should declare getEnvironment with a return value of type
// ActionEnvironment to avoid developers misunderstanding the purpose of this method. That
// requires first updating all subclasses and callers to actually handle environments correctly,
// so it's not a small change.
return env.getFixedEnv();
}
@Override
public NestedSet<Artifact> getPossibleInputsForTesting() {
return null;
}
/**
* Locally rewrites a .jdeps file to replace missing config prefixes.
*
* <p>For example: {@code bazel-out/bin/foo/foo.jar -> bazel-out/x86-fastbuild/bin/foo/foo.jar}.
*
* <p>The executor may strip config prefixes from actions (i.e. remove {@code /x86-fastbuild/} or
* equivalent from all input and output paths, command lines, and input file contents). This
* provides better caching for actions that don't vary by --cpu or --compilation_mode. The full
* paths must be re-created in Bazel's output tree to keep correct builds. For example, if an
* otherwise cacheable action's input file *contents* differ across CPUs (like a CPU-dependent
* generated source file), Bazel needs to maintain distinct paths for each instance. These paths
* are chosen in Bazel's analysis phase, before it's possible to input contents. So all actions in
* the output tree must conservatively keep full paths.
*
* <p>So this method's ultimate purpose is to translate the executor-optimized version of a .jdeps
* to the original Bazel-safe version.
*
* <p>If the executor doesn't strip config prefixes (i.e. config stripping isn't turned on as a
* feature), this is a trivial copy.
*
* <p>If config stripping is on, this method won't work with {@link
* JavaConfiguration.JavaClasspathMode#JAVABUILDER}. That mode causes downstream Java compilations
* to read this .jdeps on the executor. Since this method replaces config prefixes, the .jdeps
* entries won't match the executor's stripped paths. This works best with {@link
* JavaConfiguration.JavaClasspathMode#BAZEL}, where Bazel directly processes the .jdeps on the
* local filesystem. Those paths match.
*
* @param spawnResult the executor action that created the possibly stripped .jdeps output
* @param outputDepsProto path to the .jdeps output
* @param actionInputs all inputs to the current action
* @param actionExecutionContext the action execution context
* @return the full deps proto (also written to disk to satisfy the action's declared output)
*/
static Deps.Dependencies createFullOutputDeps(
SpawnResult spawnResult,
Artifact outputDepsProto,
NestedSet<Artifact> actionInputs,
ActionExecutionContext actionExecutionContext)
throws IOException {
// Get the executor-produce jdeps.
InputStream inMemoryOutput = spawnResult.getInMemoryOutput(outputDepsProto);
InputStream inputStream =
inMemoryOutput == null
? actionExecutionContext.getInputPath(outputDepsProto).getInputStream()
: inMemoryOutput;
Deps.Dependencies executorJdeps =
Deps.Dependencies.parseFrom(inputStream, ExtensionRegistry.getEmptyRegistry());
inputStream.close();
// For each of the action's generated inputs, map its stripped path to its full path.
PathFragment outputRoot = outputDepsProto.getExecPath().subFragment(0, 1);
Map<PathFragment, PathFragment> strippedToFullPaths = new HashMap<>();
for (Artifact actionInput : actionInputs.toList()) {
if (actionInput.isSourceArtifact()) {
continue;
}
// Turns "bazel-out/x86-fastbuild/bin/foo" to "bazel-out/bin/foo".
PathFragment strippedPath = outputRoot.getRelative(actionInput.getExecPath().subFragment(2));
if (strippedToFullPaths.put(strippedPath, actionInput.getExecPath()) != null) {
// If an entry already exists, that means different inputs reduce to the same stripped path.
// That also means PathStripper would exempt this action from path stripping, so the
// executor-produced .jdeps already includes full paths. No need to update it.
return executorJdeps;
}
}
// Rewrite the .jdeps proto with full paths.
Deps.Dependencies.Builder fullDepsBuilder = Deps.Dependencies.newBuilder(executorJdeps);
for (Deps.Dependency.Builder dep : fullDepsBuilder.getDependencyBuilderList()) {
PathFragment pathOnExecutor = PathFragment.create(dep.getPath());
PathFragment fullPath = strippedToFullPaths.get(pathOnExecutor);
if (fullPath == null && pathOnExecutor.subFragment(0, 1).equals(outputRoot)) {
// The stripped path -> full path map failed, which means the paths weren't stripped. Fast-
// return the original jdeps to save unnecessary CPU time.
return executorJdeps;
}
dep.setPath(fullPath == null ? pathOnExecutor.getPathString() : fullPath.getPathString());
}
Deps.Dependencies fullOutputDeps = fullDepsBuilder.build();
// Write the updated proto back to the filesystem. If the executor produced in-memory-only
// outputs (see getInMemoryOutput above), the filesystem version doesn't exist and we can skip
// this. Note that in-memory and filesystem outputs aren't necessarily mutually exclusive.
Path fsPath = actionExecutionContext.getInputPath(outputDepsProto);
if (fsPath.exists()) {
fsPath.setWritable(true);
FileSystemUtils.writeContent(fsPath, fullOutputDeps.toByteArray());
}
return fullOutputDeps;
}
/** Reads the full {@code .jdeps} output from the given spawn results. */
private Deps.Dependencies readFullOutputDeps(
List<SpawnResult> results, ActionExecutionContext actionExecutionContext)
throws ActionExecutionException {
try {
return createFullOutputDeps(
Iterables.getOnlyElement(results), outputDepsProto, getInputs(), actionExecutionContext);
} catch (IOException e) {
throw ActionExecutionException.fromExecException(
new EnvironmentalExecException(
e, createFailureDetail(".jdeps read IOException", Code.JDEPS_READ_IO_EXCEPTION)),
this);
}
}
private ActionExecutionException createActionExecutionException(Exception e, Code detailedCode) {
DetailedExitCode detailedExitCode =
DetailedExitCode.of(createFailureDetail(Strings.nullToEmpty(e.getMessage()), detailedCode));
return new ActionExecutionException(e, this, /*catastrophe=*/ false, detailedExitCode);
}
private static FailureDetail createFailureDetail(String message, Code detailedCode) {
return FailureDetail.newBuilder()
.setMessage(message)
.setJavaCompile(JavaCompile.newBuilder().setCode(detailedCode))
.build();
}
private final class JavaActionContinuation extends ActionContinuationOrResult {
private final ActionExecutionContext actionExecutionContext;
@Nullable private final ReducedClasspath reducedClasspath;
private final SpawnContinuation spawnContinuation;
public JavaActionContinuation(
ActionExecutionContext actionExecutionContext,
@Nullable ReducedClasspath reducedClasspath,
SpawnContinuation spawnContinuation) {
this.actionExecutionContext = actionExecutionContext;
this.reducedClasspath = reducedClasspath;
this.spawnContinuation = spawnContinuation;
}
@Override
public ListenableFuture<?> getFuture() {
return spawnContinuation.getFuture();
}
@Override
public ActionContinuationOrResult execute()
throws ActionExecutionException, InterruptedException {
try {
SpawnContinuation nextContinuation = spawnContinuation.execute();
if (!nextContinuation.isDone()) {
return new JavaActionContinuation(
actionExecutionContext, reducedClasspath, nextContinuation);
}
List<SpawnResult> results = nextContinuation.get();
if (reducedClasspath == null) {
return ActionContinuationOrResult.of(ActionResult.create(results));
}
Deps.Dependencies dependencies = readFullOutputDeps(results, actionExecutionContext);
if (compilationType == CompilationType.TURBINE) {
actionExecutionContext
.getContext(JavaCompileActionContext.class)
.insertDependencies(outputDepsProto, dependencies);
}
if (!dependencies.getRequiresReducedClasspathFallback()) {
return ActionContinuationOrResult.of(ActionResult.create(results));
}
logger.atInfo().atMostEvery(1, SECONDS).log(
"Failed reduced classpath compilation for %s", JavaCompileAction.this.prettyPrint());
// Fall back to running with the full classpath. This requires first deleting potential
// artifacts generated by the reduced action and clearing the metadata caches.
try {
deleteOutputs(
actionExecutionContext.getExecRoot(),
actionExecutionContext.getPathResolver(),
/*bulkDeleter=*/ null,
// We don't create any tree artifacts anyway.
/*cleanupArchivedArtifacts=*/ false);
} catch (IOException e) {
throw ActionExecutionException.fromExecException(
new EnvironmentalExecException(
e,
createFailureDetail(
"Failed to delete reduced action outputs",
Code.REDUCED_CLASSPATH_FALLBACK_CLEANUP_FAILURE)),
JavaCompileAction.this);
}
actionExecutionContext.getMetadataHandler().resetOutputs(getOutputs());
Spawn spawn;
try {
spawn = getReducedSpawn(actionExecutionContext, reducedClasspath, /* fallback=*/ true);
} catch (CommandLineExpansionException e) {
Code detailedCode = Code.COMMAND_LINE_EXPANSION_FAILURE;
throw createActionExecutionException(e, detailedCode);
}
SpawnContinuation fallbackContinuation =
actionExecutionContext
.getContext(SpawnStrategyResolver.class)
.beginExecution(spawn, actionExecutionContext);
return new JavaFallbackActionContinuation(
actionExecutionContext, results, fallbackContinuation);
} catch (ExecException e) {
throw ActionExecutionException.fromExecException(e, JavaCompileAction.this);
}
}
}
private final class JavaFallbackActionContinuation extends ActionContinuationOrResult {
private final ActionExecutionContext actionExecutionContext;
private final List<SpawnResult> primaryResults;
private final SpawnContinuation spawnContinuation;
public JavaFallbackActionContinuation(
ActionExecutionContext actionExecutionContext,
List<SpawnResult> primaryResults,
SpawnContinuation spawnContinuation) {
this.actionExecutionContext = actionExecutionContext;
this.primaryResults = primaryResults;
this.spawnContinuation = spawnContinuation;
}
@Override
public ListenableFuture<?> getFuture() {
return spawnContinuation.getFuture();
}
@Override
public ActionContinuationOrResult execute()
throws ActionExecutionException, InterruptedException {
try {
SpawnContinuation nextContinuation = spawnContinuation.execute();
if (!nextContinuation.isDone()) {
return new JavaFallbackActionContinuation(
actionExecutionContext, primaryResults, nextContinuation);
}
List<SpawnResult> fallbackResults = nextContinuation.get();
if (compilationType == CompilationType.TURBINE) {
actionExecutionContext
.getContext(JavaCompileActionContext.class)
.insertDependencies(
outputDepsProto, readFullOutputDeps(fallbackResults, actionExecutionContext));
}
return ActionContinuationOrResult.of(
ActionResult.create(
ImmutableList.copyOf(Iterables.concat(primaryResults, fallbackResults))));
} catch (ExecException e) {
throw ActionExecutionException.fromExecException(e, JavaCompileAction.this);
}
}
}
}