Skip to content

Commit

Permalink
Allow BootClassPathInfo.system to be a sequence of files
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 388718540
  • Loading branch information
cushon authored and copybara-github committed Aug 4, 2021
1 parent 3ea7559 commit 6db5f93
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
// limitations under the License.
package com.google.devtools.build.lib.rules.java;

import static com.google.common.collect.Iterables.getOnlyElement;

import com.google.common.collect.ImmutableList;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
Expand All @@ -24,7 +27,8 @@
import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec.VisibleForSerialization;
import com.google.devtools.build.lib.starlarkbuildapi.FileApi;
import com.google.devtools.build.lib.starlarkbuildapi.core.ProviderApi;
import javax.annotation.Nullable;
import com.google.devtools.build.lib.vfs.PathFragment;
import java.util.Optional;
import net.starlark.java.annot.Param;
import net.starlark.java.annot.ParamType;
import net.starlark.java.annot.StarlarkBuiltin;
Expand Down Expand Up @@ -66,8 +70,13 @@ private Provider() {
allowedTypes = {
@ParamType(type = FileApi.class),
@ParamType(type = NoneType.class),
@ParamType(type = Sequence.class),
},
defaultValue = "None"),
defaultValue = "None",
doc =
"The inputs to javac's --system flag, either a directory or a listing of files,"
+ " which must contain at least 'release', 'lib/modules', and"
+ " 'lib/jrt-fs.jar'"),
},
selfCall = true,
useStarlarkThread = true)
Expand All @@ -77,10 +86,13 @@ public BootClassPathInfo bootClassPathInfo(
Object systemOrNone,
StarlarkThread thread)
throws EvalException {
NestedSet<Artifact> systemInputs = getSystemInputs(systemOrNone);
Optional<PathFragment> systemPath = getSystemPath(systemInputs);
return new BootClassPathInfo(
getBootClassPath(bootClassPathList),
getAuxiliary(auxiliaryList),
getSystem(systemOrNone),
systemInputs,
systemPath,
thread.getCallerLocation());
}

Expand All @@ -96,32 +108,65 @@ private static NestedSet<Artifact> getAuxiliary(Sequence<?> auxiliaryList)
Order.STABLE_ORDER, Sequence.cast(auxiliaryList, Artifact.class, "auxiliary"));
}

private static Artifact getSystem(Object systemOrNone) throws EvalException {
private static NestedSet<Artifact> getSystemInputs(Object systemOrNone) throws EvalException {
if (systemOrNone == Starlark.NONE) {
return null;
return NestedSetBuilder.emptySet(Order.STABLE_ORDER);
}
if (systemOrNone instanceof Artifact) {
return (Artifact) systemOrNone;
return NestedSetBuilder.create(Order.STABLE_ORDER, (Artifact) systemOrNone);
}
if (systemOrNone instanceof Sequence<?>) {
return NestedSetBuilder.wrap(
Order.STABLE_ORDER, Sequence.cast(systemOrNone, Artifact.class, "system"));
}
throw Starlark.errorf(
"for system, got %s, want File, sequence, or None", Starlark.type(systemOrNone));
}

private static Optional<PathFragment> getSystemPath(NestedSet<Artifact> systemInputs)
throws EvalException {
ImmutableList<Artifact> inputs = systemInputs.toList();
if (inputs.isEmpty()) {
return Optional.empty();
}
if (inputs.size() == 1) {
Artifact input = getOnlyElement(inputs);
if (!input.isTreeArtifact()) {
throw Starlark.errorf("for system, %s is not a directory", input.getExecPathString());
}
return Optional.of(input.getExecPath());
}
throw Starlark.errorf("for system, got %s, want File or None", Starlark.type(systemOrNone));
Optional<PathFragment> input =
inputs.stream()
.map(Artifact::getExecPath)
.filter(p -> p.getBaseName().equals("release"))
.map(PathFragment::getParentDirectory)
.findAny();
if (!input.isPresent()) {
throw Starlark.errorf("for system, expected inputs to contain 'release'");
}
return input;
}
}

private final NestedSet<Artifact> bootclasspath;
private final NestedSet<Artifact> auxiliary;
@Nullable private final Artifact system;
private final NestedSet<Artifact> systemInputs;
private final Optional<PathFragment> systemPath;

@VisibleForSerialization
@AutoCodec.Instantiator
public BootClassPathInfo(
NestedSet<Artifact> bootclasspath,
NestedSet<Artifact> auxiliary,
Artifact system,
NestedSet<Artifact> systemInputs,
Optional<PathFragment> systemPath,
Location creationLocation) {
super(creationLocation);
this.bootclasspath = bootclasspath;
this.auxiliary = auxiliary;
this.system = system;
this.systemInputs = systemInputs;
this.systemPath = systemPath;
}

@Override
Expand All @@ -131,14 +176,19 @@ public Provider getProvider() {

public static BootClassPathInfo create(NestedSet<Artifact> bootclasspath) {
return new BootClassPathInfo(
bootclasspath, NestedSetBuilder.emptySet(Order.NAIVE_LINK_ORDER), null, null);
bootclasspath,
NestedSetBuilder.emptySet(Order.NAIVE_LINK_ORDER),
NestedSetBuilder.emptySet(Order.NAIVE_LINK_ORDER),
Optional.empty(),
null);
}

public static BootClassPathInfo empty() {
return new BootClassPathInfo(
NestedSetBuilder.emptySet(Order.NAIVE_LINK_ORDER),
NestedSetBuilder.emptySet(Order.NAIVE_LINK_ORDER),
null,
NestedSetBuilder.emptySet(Order.NAIVE_LINK_ORDER),
Optional.empty(),
null);
}

Expand All @@ -156,9 +206,13 @@ public NestedSet<Artifact> auxiliary() {
}

/** An argument to the javac >= 9 {@code --system} flag. */
@Nullable
public Artifact system() {
return system;
public Optional<PathFragment> systemPath() {
return systemPath;
}

/** Contents of the directory that is passed to the javac >= 9 {@code --system} flag. */
public NestedSet<Artifact> systemInputs() {
return systemInputs;
}

public boolean isEmpty() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@
import com.google.devtools.build.lib.rules.java.JavaPluginInfo.JavaPluginData;
import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
import com.google.devtools.build.lib.util.StringCanonicalizer;
import com.google.devtools.build.lib.vfs.PathFragment;
import java.util.Collections;
import java.util.Optional;
import java.util.stream.Stream;
import javax.annotation.Nullable;

Expand Down Expand Up @@ -80,7 +82,7 @@ static class JavaCompileExtraActionInfoSupplier
private final NestedSet<Artifact> bootclasspathEntries;

/** An argument to the javac >= 9 {@code --system} flag. */
@Nullable private final Artifact system;
@Nullable private final Optional<PathFragment> system;

/** The list of classpath entries to search for annotation processors. */
private final NestedSet<Artifact> processorPath;
Expand All @@ -101,7 +103,7 @@ static class JavaCompileExtraActionInfoSupplier
Artifact outputJar,
NestedSet<Artifact> classpathEntries,
NestedSet<Artifact> bootclasspathEntries,
@Nullable Artifact system,
Optional<PathFragment> system,
NestedSet<Artifact> processorPath,
NestedSet<String> processorNames,
ImmutableList<Artifact> sourceJars,
Expand Down Expand Up @@ -130,8 +132,8 @@ public void extend(ExtraActionInfo.Builder builder, ImmutableList<String> argume
.addAllProcessor(processorNames.toList())
.addAllProcessorpath(Artifact.toExecPaths(processorPath.toList()))
.setOutputjar(outputJar.getExecPathString());
if (system != null) {
info.setSystem(system.getExecPathString());
if (system.isPresent()) {
info.setSystem(system.get().toString());
}
info.addAllArgument(arguments);
builder.setExtension(JavaCompileInfo.javaCompileInfo, info.build());
Expand Down Expand Up @@ -215,17 +217,18 @@ public JavaCompileAction build() {
.addTransitive(toolchain.getJavaRuntime().javaBaseInputs())
.addTransitive(bootClassPath.bootclasspath())
.addAll(sourcePathEntries)
.addAll(additionalInputs);
Stream.of(coverageArtifact, bootClassPath.system())
.filter(x -> x != null)
.forEachOrdered(mandatoryInputs::add);
.addAll(additionalInputs)
.addTransitive(bootClassPath.systemInputs());
if (coverageArtifact != null) {
mandatoryInputs.add(coverageArtifact);
}

JavaCompileExtraActionInfoSupplier extraActionInfoSupplier =
new JavaCompileExtraActionInfoSupplier(
outputs.output(),
classpathEntries,
bootClassPath.bootclasspath(),
bootClassPath.system(),
bootClassPath.systemPath(),
plugins.processorClasspath(),
plugins.processorClasses(),
sourceJars,
Expand Down Expand Up @@ -303,7 +306,9 @@ private CustomCommandLine buildParamFileContents(ImmutableList<String> javacOpts
}
result.addExecPath("--output_deps_proto", outputs.depsProto());
result.addExecPaths("--bootclasspath", bootClassPath.bootclasspath());
result.addExecPath("--system", bootClassPath.system());
if (bootClassPath.systemPath().isPresent()) {
result.addPath("--system", bootClassPath.systemPath().get());
}
result.addExecPaths("--sourcepath", sourcePathEntries);
result.addExecPaths("--processorpath", plugins.processorClasspath());
result.addAll("--processors", plugins.processorClasses());
Expand Down

0 comments on commit 6db5f93

Please sign in to comment.