Skip to content

Commit

Permalink
Pass the platform classpath entries through to strict deps
Browse files Browse the repository at this point in the history
from JavaBuilder's command line flags, instead of retrieving them
using a non-standard filemanager API.

PiperOrigin-RevId: 156226725
  • Loading branch information
cushon authored and dslomov committed May 17, 2017
1 parent 354bf17 commit 527d20e
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ public JavaLibraryBuildRequest(
depsBuilder.setOutputDepsProtoFile(optionsParser.getOutputDepsProtoFile());
}
depsBuilder.addDepsArtifacts(optionsParser.getDepsArtifacts());
depsBuilder.setPlatformJars(
ImmutableSet.copyOf(
Iterables.concat(optionsParser.getBootClassPath(), optionsParser.getExtClassPath())));
if (optionsParser.reduceClasspath()) {
depsBuilder.setReduceClasspath();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.common.base.Function;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Ordering;
import com.google.devtools.build.buildjar.JarOwner;
import com.google.devtools.build.buildjar.javac.plugins.BlazeJavaCompilerPlugin;
Expand Down Expand Up @@ -79,6 +80,7 @@ public boolean isEnabled() {
private final Set<String> usedClasspath;
private final Map<String, Deps.Dependency> explicitDependenciesMap;
private final Map<String, Deps.Dependency> implicitDependenciesMap;
private final ImmutableSet<String> platformJars;
Set<String> requiredClasspath;
private final FixMessage fixMessage;
private final Set<String> exemptGenerators;
Expand All @@ -90,6 +92,7 @@ public boolean isEnabled() {
Map<String, JarOwner> indirectJarsToTargets,
boolean strictClasspathMode,
Set<String> depsArtifacts,
ImmutableSet<String> platformJars,
String ruleKind,
String targetLabel,
String outputDepsProtoFile,
Expand All @@ -105,6 +108,7 @@ public boolean isEnabled() {
this.outputDepsProtoFile = outputDepsProtoFile;
this.explicitDependenciesMap = new HashMap<>();
this.implicitDependenciesMap = new HashMap<>();
this.platformJars = platformJars;
this.usedClasspath = new HashSet<>();
this.fixMessage = fixMessage;
this.exemptGenerators = exemptGenerators;
Expand Down Expand Up @@ -202,6 +206,11 @@ public Map<String, Deps.Dependency> getImplicitDependenciesMap() {
return implicitDependenciesMap;
}

/** Returns the jars in the platform classpath. */
public ImmutableSet<String> getPlatformJars() {
return platformJars;
}

/** Adds a package to the set of packages built by this target. */
public boolean addPackage(PackageSymbol packge) {
return packages.add(packge);
Expand Down Expand Up @@ -307,6 +316,7 @@ public static class Builder {
private final Map<String, JarOwner> directJarsToTargets = new HashMap<>();
private final Map<String, JarOwner> indirectJarsToTargets = new HashMap<>();
private final Set<String> depsArtifacts = new HashSet<>();
private ImmutableSet<String> platformJars = ImmutableSet.of();
private String ruleKind;
private String targetLabel;
private String outputDepsProtoFile;
Expand Down Expand Up @@ -345,6 +355,7 @@ public DependencyModule build() {
indirectJarsToTargets,
strictClasspathMode,
depsArtifacts,
platformJars,
ruleKind,
targetLabel,
outputDepsProtoFile,
Expand Down Expand Up @@ -444,6 +455,12 @@ public Builder addDepsArtifacts(Collection<String> depsArtifacts) {
return this;
}

/** Sets the platform classpath entries. */
public Builder setPlatformJars(ImmutableSet<String> platformJars) {
this.platformJars = platformJars;
return this;
}

/**
* Requests compile-time classpath reduction based on provided dependency artifacts.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,15 @@

package com.google.devtools.build.buildjar.javac.plugins.dependency;

import com.google.common.collect.ImmutableSet;
import com.google.devtools.build.lib.view.proto.Deps;
import com.sun.tools.javac.code.Symbol.ClassSymbol;
import com.sun.tools.javac.code.Symtab;
import com.sun.tools.javac.file.JavacFileManager;
import com.sun.tools.javac.util.Context;
import java.lang.reflect.Field;
import java.nio.file.Path;
import java.util.Map;
import java.util.Set;
import javax.lang.model.util.SimpleTypeVisitor7;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileObject;
import javax.tools.StandardLocation;

/**
* A lightweight mechanism for extracting compile-time dependencies from javac, by performing a scan
Expand All @@ -44,18 +39,18 @@ public class ImplicitDependencyExtractor {
private final Map<String, Deps.Dependency> depsMap;

private final TypeVisitor typeVisitor = new TypeVisitor();
private final JavaFileManager fileManager;
private final Set<String> platformJars;

/**
* ImplicitDependencyExtractor does not guarantee any ordering of the reported dependencies.
* Clients should preserve the original classpath ordering if trying to minimize their classpaths
* using this information.
*/
public ImplicitDependencyExtractor(
Set<String> depsSet, Map<String, Deps.Dependency> depsMap, JavaFileManager fileManager) {
Set<String> depsSet, Map<String, Deps.Dependency> depsMap, Set<String> platformJars) {
this.depsSet = depsSet;
this.depsMap = depsMap;
this.fileManager = fileManager;
this.platformJars = platformJars;
}

/**
Expand All @@ -77,8 +72,6 @@ public void accumulate(Context context, Set<ClassSymbol> roots) {
root.type.accept(typeVisitor, null);
}

Set<String> platformJars = getPlatformJars(fileManager);

// Collect all other partially resolved types
for (ClassSymbol cs : symtab.getAllClasses()) {
// When recording we want to differentiate between jar references through completed symbols
Expand All @@ -92,24 +85,6 @@ public void accumulate(Context context, Set<ClassSymbol> roots) {
}
}

/** Collect the set of jars on the compilation bootclasspath. */
public static Set<String> getPlatformJars(JavaFileManager fileManager) {

if (fileManager instanceof JavacFileManager) {
JavacFileManager jfm = (JavacFileManager) fileManager;
ImmutableSet.Builder<String> result = ImmutableSet.builder();
for (Path path : jfm.getLocationAsPaths(StandardLocation.PLATFORM_CLASS_PATH)) {
result.add(path.toString());
}
return result.build();
}

// TODO(cushon): Assuming JavacFileManager is brittle, but in practice it is the only
// implementation that matters.
throw new IllegalStateException(
"Unsupported file manager type: " + fileManager.getClass().getName());
}

/**
* Attempts to add the jar associated with the given JavaFileObject, if any, to the collection,
* filtering out jars on the compilation bootclasspath.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import static com.google.common.collect.Iterables.getOnlyElement;
import static com.google.devtools.build.buildjar.javac.plugins.dependency.DependencyModule.StrictJavaDeps.ERROR;
import static com.google.devtools.build.buildjar.javac.plugins.dependency.ImplicitDependencyExtractor.getPlatformJars;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Ordering;
Expand Down Expand Up @@ -110,10 +109,10 @@ public void init(Context context, Log log, JavaCompiler compiler) {
new ImplicitDependencyExtractor(
dependencyModule.getUsedClasspath(),
dependencyModule.getImplicitDependenciesMap(),
fileManager);
dependencyModule.getPlatformJars());
checkingTreeScanner = context.get(CheckingTreeScanner.class);
if (checkingTreeScanner == null) {
Set<String> platformJars = getPlatformJars(fileManager);
Set<String> platformJars = dependencyModule.getPlatformJars();
checkingTreeScanner =
new CheckingTreeScanner(dependencyModule, log, missingTargets, platformJars, fileManager);
context.put(CheckingTreeScanner.class, checkingTreeScanner);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ private static DependencyModule buildDependencyModule(
.setReduceClasspath()
.setTargetLabel(turbineOptions.targetLabel().orNull())
.addDepsArtifacts(turbineOptions.depsArtifacts())
.setPlatformJars(turbineOptions.bootClassPath())
.setStrictJavaDeps(strictDepsMode.toString())
.addDirectMappings(parseJarsToTargets(turbineOptions.directJarsToTargets()))
.addIndirectMappings(parseJarsToTargets(turbineOptions.indirectJarsToTargets()));
Expand Down

0 comments on commit 527d20e

Please sign in to comment.