Skip to content

Commit 31fae9e

Browse files
fmeumcopybara-github
authored andcommitted
Share classpath NestedSet between full and header compile actions
The Starlark Java rules already add the direct JARs to the transitive classpath depset and pass the same depset to the full and the header compile action. By allowing them to bypass the logic that prepends the direct jars to the compile-time classpath, both actions retain the same `NestedSet` instead of both retaining a new one with identical elements. Closes #21343. PiperOrigin-RevId: 607790056 Change-Id: Ia08c834ae3e5b1151607459408cdfea85d47314f
1 parent 014292b commit 31fae9e

2 files changed

Lines changed: 32 additions & 6 deletions

File tree

src/main/java/com/google/devtools/build/lib/rules/java/JavaStarlarkCommon.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ public void createHeaderCompilationAction(
141141
.addSourceJars(Sequence.cast(sourceJars, Artifact.class, "source_jars"))
142142
.addSourceFiles(sourceFiles.toList(Artifact.class))
143143
.addDirectJars(directJars.getSet(Artifact.class))
144-
.addCompileTimeClassPathEntries(compileTimeClasspath.getSet(Artifact.class))
144+
.setCompileTimeClassPathEntriesWithPrependedDirectJars(
145+
compileTimeClasspath.getSet(Artifact.class))
145146
.setStrictJavaDeps(getStrictDepsMode(Ascii.toUpperCase(strictDepsMode)))
146147
.setTargetLabel(targetLabel)
147148
.setInjectingRuleKind(
@@ -214,7 +215,8 @@ public void createCompilationAction(
214215
.addSourceJars(Sequence.cast(sourceJars, Artifact.class, "source_jars"))
215216
.addSourceFiles(Depset.noneableCast(sourceFiles, Artifact.class, "sources").toList())
216217
.addDirectJars(directJars.getSet(Artifact.class))
217-
.addCompileTimeClassPathEntries(compileTimeClasspath.getSet(Artifact.class))
218+
.setCompileTimeClassPathEntriesWithPrependedDirectJars(
219+
compileTimeClasspath.getSet(Artifact.class))
218220
.addClassPathResources(
219221
Sequence.cast(classpathResources, Artifact.class, "classpath_resources"))
220222
.setStrictJavaDeps(getStrictDepsMode(Ascii.toUpperCase(strictDepsMode)))

src/main/java/com/google/devtools/build/lib/rules/java/JavaTargetAttributes.java

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ public static class Builder {
8888

8989
private final NestedSetBuilder<Artifact> excludedArtifacts = NestedSetBuilder.naiveLinkOrder();
9090

91+
private boolean prependDirectJars = true;
92+
9193
private boolean built = false;
9294

9395
private final JavaSemantics semantics;
@@ -179,6 +181,24 @@ public Builder addCompileTimeClassPathEntries(NestedSet<Artifact> entries) {
179181
return this;
180182
}
181183

184+
/**
185+
* Avoids prepending the direct jars to the compile-time classpath when building the attributes,
186+
* assuming that they have already been prepended. This avoids creating a new {@link NestedSet}
187+
* instance.
188+
*
189+
* <p>After this method is called, {@link #addDirectJar(Artifact)} and {@link
190+
* #addDirectJars(NestedSet)} will throw an exception.
191+
*/
192+
@CanIgnoreReturnValue
193+
public Builder setCompileTimeClassPathEntriesWithPrependedDirectJars(
194+
NestedSet<Artifact> entries) {
195+
Preconditions.checkArgument(!built);
196+
Preconditions.checkArgument(compileTimeClassPathBuilder.isEmpty());
197+
prependDirectJars = false;
198+
compileTimeClassPathBuilder.addTransitive(entries);
199+
return this;
200+
}
201+
182202
@CanIgnoreReturnValue
183203
public Builder setTargetLabel(Label targetLabel) {
184204
Preconditions.checkArgument(!built);
@@ -251,13 +271,15 @@ public Builder setStrictJavaDeps(StrictDepsMode strictDeps) {
251271
@CanIgnoreReturnValue
252272
public Builder addDirectJars(NestedSet<Artifact> directJars) {
253273
Preconditions.checkArgument(!built);
274+
Preconditions.checkArgument(prependDirectJars);
254275
this.directJarsBuilder.addTransitive(directJars);
255276
return this;
256277
}
257278

258279
@CanIgnoreReturnValue
259280
public Builder addDirectJar(Artifact directJar) {
260281
Preconditions.checkArgument(!built);
282+
Preconditions.checkArgument(prependDirectJars);
261283
this.directJarsBuilder.add(directJar);
262284
return this;
263285
}
@@ -323,10 +345,12 @@ public JavaTargetAttributes build() {
323345
built = true;
324346
NestedSet<Artifact> directJars = directJarsBuilder.build();
325347
NestedSet<Artifact> compileTimeClassPath =
326-
NestedSetBuilder.<Artifact>naiveLinkOrder()
327-
.addTransitive(directJars)
328-
.addTransitive(compileTimeClassPathBuilder.build())
329-
.build();
348+
prependDirectJars
349+
? NestedSetBuilder.<Artifact>naiveLinkOrder()
350+
.addTransitive(directJars)
351+
.addTransitive(compileTimeClassPathBuilder.build())
352+
.build()
353+
: compileTimeClassPathBuilder.build();
330354
return new JavaTargetAttributes(
331355
ImmutableSet.copyOf(sourceFiles),
332356
runtimeClassPath.build(),

0 commit comments

Comments
 (0)