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 authored and raviagarwal7 committed Jul 25, 2023
1 parent 0f231ac commit 68bde1b
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 6 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 @@ -112,6 +112,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 @@ -154,6 +155,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 @@ -459,4 +461,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 pruneTransitiveDeps = ruleContext.getFragment(JavaConfiguration.class)
.experimentalPruneTransitiveDeps();

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

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 @@ -610,6 +610,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,10 @@ public Builder addSourceFiles(Iterable<Artifact> sourceFiles) {
}

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

0 comments on commit 68bde1b

Please sign in to comment.