diff --git a/src/main/java/com/google/devtools/build/lib/analysis/AnalysisEnvironment.java b/src/main/java/com/google/devtools/build/lib/analysis/AnalysisEnvironment.java index 12bab8df8168a8..50c17df23f53e8 100644 --- a/src/main/java/com/google/devtools/build/lib/analysis/AnalysisEnvironment.java +++ b/src/main/java/com/google/devtools/build/lib/analysis/AnalysisEnvironment.java @@ -26,6 +26,7 @@ import com.google.devtools.build.lib.actions.MiddlemanFactory; import com.google.devtools.build.lib.analysis.buildinfo.BuildInfoKey; import com.google.devtools.build.lib.analysis.config.BuildConfigurationValue; +import com.google.devtools.build.lib.cmdline.RepositoryMapping; import com.google.devtools.build.lib.events.ExtendedEventHandler; import com.google.devtools.build.lib.vfs.PathFragment; import com.google.devtools.build.skyframe.SkyFunction; @@ -176,4 +177,10 @@ ImmutableList getBuildInfo( ImmutableSet getTreeArtifactsConflictingWithFiles(); ActionKeyContext getActionKeyContext(); + + /** + * The repository mapping applicable to the main repository. This is purely meant to support + * {@link com.google.devtools.build.lib.cmdline.Label#getDisplayForm()}. + */ + RepositoryMapping getMainRepoMapping(); } diff --git a/src/main/java/com/google/devtools/build/lib/analysis/CachingAnalysisEnvironment.java b/src/main/java/com/google/devtools/build/lib/analysis/CachingAnalysisEnvironment.java index d9c9d5d171ffbb..633ef39e220fc6 100644 --- a/src/main/java/com/google/devtools/build/lib/analysis/CachingAnalysisEnvironment.java +++ b/src/main/java/com/google/devtools/build/lib/analysis/CachingAnalysisEnvironment.java @@ -30,6 +30,7 @@ import com.google.devtools.build.lib.analysis.buildinfo.BuildInfoCollection; import com.google.devtools.build.lib.analysis.buildinfo.BuildInfoKey; import com.google.devtools.build.lib.analysis.config.BuildConfigurationValue; +import com.google.devtools.build.lib.cmdline.RepositoryMapping; import com.google.devtools.build.lib.events.ExtendedEventHandler; import com.google.devtools.build.lib.events.StoredEventHandler; import com.google.devtools.build.lib.packages.Target; @@ -88,6 +89,8 @@ public final class CachingAnalysisEnvironment implements AnalysisEnvironment { */ private final List actions = new ArrayList<>(); + private final RepositoryMapping mainRepoMapping; + public CachingAnalysisEnvironment( ArtifactFactory artifactFactory, ActionKeyContext actionKeyContext, @@ -96,7 +99,8 @@ public CachingAnalysisEnvironment( boolean allowAnalysisFailures, ExtendedEventHandler errorEventListener, SkyFunction.Environment env, - StarlarkBuiltinsValue starlarkBuiltinsValue) { + StarlarkBuiltinsValue starlarkBuiltinsValue, + RepositoryMapping mainRepoMapping) { this.artifactFactory = artifactFactory; this.actionKeyContext = actionKeyContext; this.owner = Preconditions.checkNotNull(owner); @@ -105,6 +109,7 @@ public CachingAnalysisEnvironment( this.errorEventListener = errorEventListener; this.skyframeEnv = env; this.starlarkBuiltinsValue = starlarkBuiltinsValue; + this.mainRepoMapping = mainRepoMapping; middlemanFactory = new MiddlemanFactory(artifactFactory, this); } @@ -231,6 +236,11 @@ public ActionKeyContext getActionKeyContext() { return actionKeyContext; } + @Override + public RepositoryMapping getMainRepoMapping() { + return mainRepoMapping; + } + @Override public boolean hasErrors() { Preconditions.checkState(enabled); diff --git a/src/main/java/com/google/devtools/build/lib/analysis/RuleContext.java b/src/main/java/com/google/devtools/build/lib/analysis/RuleContext.java index ecb5c28e176382..400ffc4a8557fe 100644 --- a/src/main/java/com/google/devtools/build/lib/analysis/RuleContext.java +++ b/src/main/java/com/google/devtools/build/lib/analysis/RuleContext.java @@ -57,6 +57,7 @@ import com.google.devtools.build.lib.analysis.starlark.StarlarkRuleContext; import com.google.devtools.build.lib.analysis.stringtemplate.TemplateContext; import com.google.devtools.build.lib.cmdline.Label; +import com.google.devtools.build.lib.cmdline.RepositoryMapping; import com.google.devtools.build.lib.cmdline.RepositoryName; import com.google.devtools.build.lib.collect.ImmutableSortedKeyListMultimap; import com.google.devtools.build.lib.collect.nestedset.NestedSet; @@ -327,6 +328,11 @@ public String getWorkspaceName() { return rule.getPackage().getWorkspaceName(); } + /** Returns the repository mapping of the main repository. */ + public RepositoryMapping getMainRepoMapping() { + return getAnalysisEnvironment().getMainRepoMapping(); + } + /** The configuration conditions that trigger this rule's configurable attributes. */ public ImmutableMap getConfigConditions() { return configConditions; 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 2ea7ca146d3649..303d170367c138 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 @@ -16,6 +16,8 @@ import static com.google.common.base.Preconditions.checkNotNull; import static com.google.devtools.build.lib.packages.ExecGroup.DEFAULT_EXEC_GROUP_NAME; +import com.github.benmanes.caffeine.cache.Caffeine; +import com.github.benmanes.caffeine.cache.LoadingCache; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; @@ -23,6 +25,8 @@ import com.google.common.collect.Interner; import com.google.common.collect.Iterables; import com.google.devtools.build.lib.actions.Artifact; +import com.google.devtools.build.lib.actions.CommandLineItem; +import com.google.devtools.build.lib.actions.CommandLineItem.ExceptionlessMapFn; import com.google.devtools.build.lib.actions.ExecutionRequirements; import com.google.devtools.build.lib.actions.ParamFileInfo; import com.google.devtools.build.lib.actions.ParameterFile; @@ -38,6 +42,7 @@ import com.google.devtools.build.lib.analysis.config.CoreOptionConverters.StrictDepsMode; import com.google.devtools.build.lib.analysis.test.InstrumentedFilesCollector; import com.google.devtools.build.lib.cmdline.Label; +import com.google.devtools.build.lib.cmdline.RepositoryMapping; 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; @@ -50,6 +55,7 @@ import com.google.devtools.build.lib.rules.java.JavaToolchainProvider.JspecifyInfo; import com.google.devtools.build.lib.vfs.FileSystemUtils; import com.google.devtools.build.lib.vfs.PathFragment; +import java.io.Serializable; import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -63,6 +69,33 @@ */ public final class JavaCompilationHelper { + /** + * Cache for the {@link com.google.devtools.build.lib.actions.CommandLineItem.MapFn} that turns a + * {@link Label} into its possibly @-prefixed display form, which requires the repository mapping + * of the main repo. + * + *

The capacity of this cache after weakly reachable object have been cleaned will always be 1 + * as there is only a single main repo mapping in a given build. + */ + static final LoadingCache> + TARGET_LABEL_MAP_FN_CACHE = + Caffeine.newBuilder() + .initialCapacity(1) + .weakKeys() + .build( + mainRepoMapping -> + (ExceptionlessMapFn