Skip to content

Commit

Permalink
Only set -Abazel.repository if consumed by the plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
fmeum committed Nov 9, 2022
1 parent 9716e25 commit c4ddafa
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableList.Builder;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
Expand Down Expand Up @@ -371,11 +372,14 @@ public final void initializeJavacOpts() {

/** Computes javacopts for the current rule. */
private ImmutableList<String> computeJavacOpts(Collection<String> extraRuleJavacOpts) {
return ImmutableList.<String>builder()
Builder<String> javacOpts = ImmutableList.<String>builder()
.addAll(javaToolchain.getJavacOptions(ruleContext))
.addAll(extraRuleJavacOpts)
// Consumed by com.google.devtools.build.runfiles.AutoBazelRepositoryProcessor.
.add("-Abazel.repository=" + ruleContext.getRepository().getName())
.addAll(extraRuleJavacOpts);
if (activePlugins.plugins().processorClasses().toList()
.contains("com.google.devtools.build.runfiles.AutoBazelRepositoryProcessor")) {
javacOpts.add("-Abazel.repository=" + ruleContext.getRepository().getName());
}
return javacOpts
.addAll(computePerPackageJavacOpts(ruleContext, javaToolchain))
.addAll(addModuleJavacopts(ruleContext))
.addAll(ruleContext.getExpander().withDataLocations().tokenized("javacopts"))
Expand Down Expand Up @@ -540,8 +544,8 @@ public JavaTargetAttributes.Builder initCommon() {
public JavaTargetAttributes.Builder initCommon(
Collection<Artifact> extraSrcs, Iterable<String> extraJavacOpts) {
Preconditions.checkState(javacOpts == null);
javacOpts = computeJavacOpts(ImmutableList.copyOf(extraJavacOpts));
activePlugins = collectPlugins();
javacOpts = computeJavacOpts(ImmutableList.copyOf(extraJavacOpts));

JavaTargetAttributes.Builder javaTargetAttributes = new JavaTargetAttributes.Builder(semantics);
javaCompilationHelper =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import com.google.common.base.Ascii;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableList.Builder;
import com.google.common.collect.Streams;
import com.google.devtools.build.lib.actions.ActionRegistry;
import com.google.devtools.build.lib.actions.Artifact;
Expand Down Expand Up @@ -284,6 +285,22 @@ public JavaInfo createJavaCompileAction(

JavaToolchainProvider toolchainProvider = javaToolchain;

JavaPluginInfo pluginInfo = mergeExportedJavaPluginInfo(plugins, deps);
Builder<String> allJavacOptsBuilder = ImmutableList.<String>builder()
.addAll(toolchainProvider.getJavacOptions(starlarkRuleContext.getRuleContext()))
.addAll(javaSemantics.getCompatibleJavacOptions(starlarkRuleContext.getRuleContext(),
toolchainProvider));
if (pluginInfo.plugins().processorClasses().toList()
.contains("com.google.devtools.build.runfiles.AutoBazelRepositoryProcessor")) {
allJavacOptsBuilder.add(
"-Abazel.repository=" + starlarkRuleContext.getRuleContext().getRepository().getName());
}
allJavacOptsBuilder.addAll(
JavaCommon.computePerPackageJavacOpts(starlarkRuleContext.getRuleContext(),
toolchainProvider))
.addAll(JavaModuleFlagsProvider.toFlags(addExports, addOpens))
.addAll(tokenize(javacOpts));

JavaLibraryHelper helper =
new JavaLibraryHelper(starlarkRuleContext.getRuleContext())
.setOutput(outputJar)
Expand All @@ -295,22 +312,7 @@ public JavaInfo createJavaCompileAction(
.setSourcePathEntries(sourcepathEntries)
.addAdditionalOutputs(annotationProcessorAdditionalOutputs)
.enableJspecify(enableJSpecify)
.setJavacOpts(
ImmutableList.<String>builder()
.addAll(toolchainProvider.getJavacOptions(starlarkRuleContext.getRuleContext()))
.addAll(
javaSemantics.getCompatibleJavacOptions(
starlarkRuleContext.getRuleContext(), toolchainProvider))
// Consumed by com.google.devtools.build.runfiles.AutoBazelRepositoryProcessor.
.add(
"-Abazel.repository=" + starlarkRuleContext.getRuleContext().getRepository()
.getName())
.addAll(
JavaCommon.computePerPackageJavacOpts(
starlarkRuleContext.getRuleContext(), toolchainProvider))
.addAll(JavaModuleFlagsProvider.toFlags(addExports, addOpens))
.addAll(tokenize(javacOpts))
.build());
.setJavacOpts(allJavacOptsBuilder.build());

if (injectingRuleKind != Starlark.NONE) {
helper.setInjectingRuleKind((String) injectingRuleKind);
Expand All @@ -320,7 +322,6 @@ public JavaInfo createJavaCompileAction(
streamProviders(deps, JavaCompilationArgsProvider.class).forEach(helper::addDep);
streamProviders(exports, JavaCompilationArgsProvider.class).forEach(helper::addExport);
helper.setCompilationStrictDepsMode(getStrictDepsMode(Ascii.toUpperCase(strictDepsMode)));
JavaPluginInfo pluginInfo = mergeExportedJavaPluginInfo(plugins, deps);
// Optimization: skip this if there are no annotation processors, to avoid unnecessarily
// disabling the direct classpath optimization if `enable_annotation_processor = False`
// but there aren't any annotation processors.
Expand Down

0 comments on commit c4ddafa

Please sign in to comment.