From 8c39cd8d4cc5b7fcc4b1a3796cd0580ead7c90c6 Mon Sep 17 00:00:00 2001 From: Nick Korostelev Date: Fri, 7 Oct 2022 12:39:14 -0700 Subject: [PATCH] Enables compiling against direct dependencies only --- .../lib/rules/java/JavaCompilationHelper.java | 4 +++- .../build/lib/rules/java/JavaConfiguration.java | 6 ++++++ .../devtools/build/lib/rules/java/JavaImport.java | 4 +++- .../build/lib/rules/java/JavaLibraryHelper.java | 10 +++++++++- .../build/lib/rules/java/JavaOptions.java | 15 +++++++++++++++ .../lib/rules/java/JavaTargetAttributes.java | 6 ++++-- 6 files changed, 40 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaCompilationHelper.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaCompilationHelper.java index 8ae3fad9cf94f2..5c9f2a6623065f 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaCompilationHelper.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaCompilationHelper.java @@ -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 deps) { diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaConfiguration.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaConfiguration.java index 66eb125d51fac9..84c3e1eb01803c 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaConfiguration.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaConfiguration.java @@ -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; @@ -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 optimizers = javaOptions.bytecodeOptimizers; if (optimizers.size() > 1) { @@ -466,4 +468,8 @@ public boolean experimentalEnableJspecify() { public boolean requireJavaPluginInfo() { return requireJavaPluginInfo; } + + public boolean experimentalPruneTransitiveDeps() { + return experimentalPruneTransitiveDeps; + } } diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaImport.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaImport.java index 0bd8682a89bdd7..9ca2ea3010abb7 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaImport.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaImport.java @@ -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() diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaLibraryHelper.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaLibraryHelper.java index 41bba1b73f433d..3d2f54f80ce6ed 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaLibraryHelper.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaLibraryHelper.java @@ -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; @@ -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 localCompileTimeDeps = compileWithTransitiveDeps + ? mergedDeps.getTransitiveCompileTimeJars() + : mergedDeps.getDirectCompileTimeJars(); + + attributes.addCompileTimeClassPathEntries(localCompileTimeDeps); attributes.addRuntimeClassPathEntries(mergedRuntimeDeps.getRuntimeJars()); attributes.addRuntimeClassPathEntries(mergedDeps.getRuntimeJars()); } diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaOptions.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaOptions.java index 9b4dc16f56fc89..553b332be3adc8 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaOptions.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaOptions.java @@ -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. diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaTargetAttributes.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaTargetAttributes.java index fedee3356aa4ef..302f22d4991061 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaTargetAttributes.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaTargetAttributes.java @@ -129,9 +129,11 @@ public Builder addSourceFiles(Iterable 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; }