Skip to content

Commit

Permalink
Enables compiling against direct dependencies only
Browse files Browse the repository at this point in the history
  • Loading branch information
nkoroste committed Oct 7, 2022
1 parent 5d0e5a7 commit 8c39cd8
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,9 @@ private void addArgsAndJarsToAttributes(
attributes.addDirectJars(directJars);
}

attributes.merge(args);
boolean pruneTransitiveDeps = !ruleContext.getFragment(JavaConfiguration.class)
.experimentalPruneTransitiveDeps();
attributes.merge(args, pruneTransitiveDeps);
}

private void addLibrariesToAttributesInternal(Iterable<? extends TransitiveInfoCollection> deps) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public enum ImportDepsCheckingLevel {
private final boolean multiReleaseDeployJars;
private final boolean disallowJavaImportExports;
private final boolean disallowJavaImportEmptyJars;
private final boolean experimentalPruneTransitiveDeps;

// TODO(dmarting): remove once we have a proper solution for #2539
private final boolean useLegacyBazelJavaTest;
Expand Down Expand Up @@ -156,6 +157,7 @@ public JavaConfiguration(BuildOptions buildOptions) throws InvalidConfigurationE
this.multiReleaseDeployJars = javaOptions.multiReleaseDeployJars;
this.disallowJavaImportExports = javaOptions.disallowJavaImportExports;
this.disallowJavaImportEmptyJars = javaOptions.disallowJavaImportEmptyJars;
this.experimentalPruneTransitiveDeps = javaOptions.experimentalPruneTransitiveDeps;

Map<String, Label> optimizers = javaOptions.bytecodeOptimizers;
if (optimizers.size() > 1) {
Expand Down Expand Up @@ -466,4 +468,8 @@ public boolean experimentalEnableJspecify() {
public boolean requireJavaPluginInfo() {
return requireJavaPluginInfo;
}

public boolean experimentalPruneTransitiveDeps() {
return experimentalPruneTransitiveDeps;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,11 @@ public ConfiguredTarget create(RuleContext ruleContext)
ruleContext.getUniqueDirectoryArtifact(
"_java_import", "jdeps.proto", ruleContext.getBinOrGenfilesDirectory());
JavaCompilationArgsProvider provider = JavaCompilationArgsProvider.legacyFromTargets(targets);
boolean pruneTransitiveDeps = !ruleContext.getFragment(JavaConfiguration.class)
.experimentalPruneTransitiveDeps();
JavaTargetAttributes attributes =
new JavaTargetAttributes.Builder(semantics)
.merge(provider)
.merge(provider, pruneTransitiveDeps)
.addDirectJars(provider.getDirectCompileTimeJars())
.build();
ImportDepsCheckActionBuilder.newBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.analysis.RuleContext;
import com.google.devtools.build.lib.analysis.config.CoreOptionConverters.StrictDepsMode;
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.rules.java.JavaConfiguration.JavaClasspathMode;
Expand Down Expand Up @@ -404,7 +405,14 @@ private void addDepsToAttributes(JavaTargetAttributes.Builder attributes) {
attributes.addDirectJars(mergedDeps.getDirectCompileTimeJars());
}

attributes.addCompileTimeClassPathEntries(mergedDeps.getTransitiveCompileTimeJars());
boolean compileWithTransitiveDeps = !ruleContext.getFragment(JavaConfiguration.class)
.experimentalPruneTransitiveDeps();

NestedSet<Artifact> localCompileTimeDeps = compileWithTransitiveDeps
? mergedDeps.getTransitiveCompileTimeJars()
: mergedDeps.getDirectCompileTimeJars();

attributes.addCompileTimeClassPathEntries(localCompileTimeDeps);
attributes.addRuntimeClassPathEntries(mergedRuntimeDeps.getRuntimeJars());
attributes.addRuntimeClassPathEntries(mergedDeps.getRuntimeJars());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,21 @@ public ImportDepsCheckingLevelConverter() {
effectTags = {OptionEffectTag.UNKNOWN},
help = "Enable experimental jspecify integration.")
public boolean experimentalEnableJspecify;

@Option(
name = "experimental_prune_transitive_deps",
defaultValue = "false",
documentationCategory = OptionDocumentationCategory.BUILD_TIME_OPTIMIZATION,
effectTags = {
OptionEffectTag.LOADING_AND_ANALYSIS,
OptionEffectTag.EXECUTION,
OptionEffectTag.AFFECTS_OUTPUTS},
metadataTags = {OptionMetadataTag.EXPERIMENTAL},
help =
"If enabled, compilation is performed against only direct dependencies. Transitive deps "
+ "required for compilation must be explicitly added")
public boolean experimentalPruneTransitiveDeps;

@Override
public FragmentOptions getHost() {
// Note validation actions don't run in host config, so no need copying flags related to that.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,11 @@ public Builder addSourceFiles(Iterable<Artifact> sourceFiles) {
}

@CanIgnoreReturnValue
public Builder merge(JavaCompilationArgsProvider context) {
Preconditions.checkArgument(!built);
public Builder merge(JavaCompilationArgsProvider context, boolean pruneTransitiveDeps) {
addCompileTimeClassPathEntries(context.getTransitiveCompileTimeJars());
addCompileTimeClassPathEntries(
pruneTransitiveDeps ? context.getTransitiveCompileTimeJars()
: context.getDirectCompileTimeJars());
addRuntimeClassPathEntries(context.getRuntimeJars());
return this;
}
Expand Down

0 comments on commit 8c39cd8

Please sign in to comment.