Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Track runfiles library users in RunfilesProvider #16126

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,8 @@ private static ConfiguredTarget erroredConfiguredTargetWithFailures(
throws ActionConflictException, InterruptedException, AnalysisFailurePropagationException {
RuleConfiguredTargetBuilder builder = new RuleConfiguredTargetBuilder(ruleContext);
builder.addNativeDeclaredProvider(AnalysisFailureInfo.forAnalysisFailureSets(analysisFailures));
builder.addProvider(RunfilesProvider.class, RunfilesProvider.simple(Runfiles.EMPTY));
builder.addProvider(RunfilesProvider.class,
RunfilesProvider.simple(ruleContext, Runfiles.EMPTY));
ConfiguredTarget configuredTarget = builder.build();
if (configuredTarget == null) {
// A failure here is a failure in analysis failure testing machinery, not a "normal" analysis
Expand Down Expand Up @@ -437,7 +438,8 @@ private static ConfiguredTarget erroredConfiguredTarget(RuleContext ruleContext)
RuleConfiguredTargetBuilder builder = new RuleConfiguredTargetBuilder(ruleContext);
builder.addNativeDeclaredProvider(
AnalysisFailureInfo.forAnalysisFailures(analysisFailures.build()));
builder.addProvider(RunfilesProvider.class, RunfilesProvider.simple(Runfiles.EMPTY));
builder.addProvider(RunfilesProvider.class,
RunfilesProvider.simple(ruleContext, Runfiles.EMPTY));
ConfiguredTarget configuredTarget = builder.build();
if (configuredTarget == null) {
// See comment in erroredConfiguredTargetWithFailures.
Expand Down Expand Up @@ -692,7 +694,8 @@ private static ConfiguredTarget createFailConfiguredTargetForMissingFragmentClas
"Missing fragment class: " + missingFragmentClass.getName(),
Code.FRAGMENT_CLASS_MISSING));
}
builder.addProvider(RunfilesProvider.class, RunfilesProvider.simple(Runfiles.EMPTY));
builder.addProvider(RunfilesProvider.class,
RunfilesProvider.simple(ruleContext, Runfiles.EMPTY));
try {
return builder.build();
} catch (ActionConflictException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,19 @@
package com.google.devtools.build.lib.analysis;

import com.google.common.base.Function;
import com.google.common.base.Objects;
import com.google.devtools.build.lib.cmdline.RepositoryMapping;
import com.google.devtools.build.lib.cmdline.RepositoryName;
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.concurrent.ThreadSafety.Immutable;
import com.google.devtools.build.lib.packages.Attribute;
import com.google.devtools.build.lib.packages.Type.LabelClass;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* Runfiles a target contributes to targets that depend on it.
Expand All @@ -25,12 +37,60 @@
*/
@Immutable
public final class RunfilesProvider implements TransitiveInfoProvider {

/**
* Holds a {@link RepositoryName} together with the corresponding {@link RepositoryMapping}.
*
* <p>Instances of this class compare equal iff the <code>repositoryName</code> members are
* equal. As a result, a {@link Set<RepositoryNameAndMapping>} will behave like an
* {@link Map#entrySet} of a {@link Map Map<RepositoryName, RepositoryMapping>}.
*/
public static final class RepositoryNameAndMapping {

private final RepositoryName repositoryName;
private final RepositoryMapping repositoryMapping;

public RepositoryNameAndMapping(RepositoryName repositoryName,
RepositoryMapping repositoryMapping) {
this.repositoryName = repositoryName;
this.repositoryMapping = repositoryMapping;
}

public RepositoryName getRepositoryName() {
return repositoryName;
}

public RepositoryMapping getRepositoryMapping() {
return repositoryMapping;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
RepositoryNameAndMapping that = (RepositoryNameAndMapping) o;
return Objects.equal(repositoryName, that.repositoryName);
}

@Override
public int hashCode() {
return Objects.hashCode(repositoryName);
}
}

private final Runfiles defaultRunfiles;
private final Runfiles dataRunfiles;
private final NestedSet<RepositoryNameAndMapping> runfilesLibraryUsers;

private RunfilesProvider(Runfiles defaultRunfiles, Runfiles dataRunfiles) {
private RunfilesProvider(Runfiles defaultRunfiles, Runfiles dataRunfiles,
NestedSet<RepositoryNameAndMapping> runfilesLibraryUsers) {
this.defaultRunfiles = defaultRunfiles;
this.dataRunfiles = dataRunfiles;
this.runfilesLibraryUsers = runfilesLibraryUsers;
}

public Runfiles getDefaultRunfiles() {
Expand All @@ -41,6 +101,10 @@ public Runfiles getDataRunfiles() {
return dataRunfiles;
}

public NestedSet<RepositoryNameAndMapping> getRunfilesLibraryUsers() {
return runfilesLibraryUsers;
}

/**
* Returns a function that gets the default runfiles from a {@link TransitiveInfoCollection} or
* the empty runfiles instance if it does not contain that provider.
Expand Down Expand Up @@ -77,15 +141,51 @@ public Runfiles apply(TransitiveInfoCollection input) {
}
};

public static RunfilesProvider simple(Runfiles defaultRunfiles) {
return new RunfilesProvider(defaultRunfiles, defaultRunfiles);
public static RunfilesProvider simple(RuleContext ruleContext, Runfiles defaultRunfiles) {
return new RunfilesProvider(defaultRunfiles, defaultRunfiles,
collectRunfilesLibraryUsers(ruleContext));
}

public static RunfilesProvider withData(
Runfiles defaultRunfiles, Runfiles dataRunfiles) {
return new RunfilesProvider(defaultRunfiles, dataRunfiles);
RuleContext ruleContext, Runfiles defaultRunfiles, Runfiles dataRunfiles) {
return new RunfilesProvider(defaultRunfiles, dataRunfiles,
collectRunfilesLibraryUsers(ruleContext));
}

public static final RunfilesProvider EMPTY = new RunfilesProvider(
Runfiles.EMPTY, Runfiles.EMPTY);
Runfiles.EMPTY, Runfiles.EMPTY, NestedSetBuilder.emptySet(Order.COMPILE_ORDER));

/**
* Collects the runfiles library users of all non-tool dependencies and adds the current
* repository if a runfiles library (marked with {@link RunfilesLibraryInfo}) is among these
* dependencies.
*/
private static NestedSet<RepositoryNameAndMapping> collectRunfilesLibraryUsers(
RuleContext ruleContext) {
NestedSetBuilder<RepositoryNameAndMapping> users = NestedSetBuilder.compileOrder();
for (TransitiveInfoCollection dep : getAllNonToolPrerequisites(ruleContext)) {
RunfilesProvider provider = dep.getProvider(RunfilesProvider.class);
if (provider != null) {
users.addTransitive(provider.getRunfilesLibraryUsers());
}
if (dep.get(RunfilesLibraryInfo.PROVIDER) != null) {
users.add(new RepositoryNameAndMapping(ruleContext.getRepository(),
ruleContext.getRule().getPackage().getRepositoryMapping()));
}
}
return users.build();
}

private static Iterable<TransitiveInfoCollection> getAllNonToolPrerequisites(
RuleContext ruleContext) {
List<TransitiveInfoCollection> prerequisites = new ArrayList<>();
for (Attribute attribute : ruleContext.getRule().getAttributes()) {
if (attribute.getType().getLabelClass() != LabelClass.DEPENDENCY
|| attribute.isToolDependency()) {
continue;
}
prerequisites.addAll(ruleContext.getPrerequisites(attribute.getName()));
}
return prerequisites;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ private static RuleConfiguredTargetValue createIncompatibleRuleConfiguredTarget(
TransitiveInfoProviderMapBuilder providerBuilder =
new TransitiveInfoProviderMapBuilder()
.put(incompatiblePlatformProvider)
.add(RunfilesProvider.simple(Runfiles.EMPTY))
.add(RunfilesProvider.EMPTY)
.add(fileProvider)
.add(filesToRunProvider);
if (configuration.hasFragment(TestConfiguration.class)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -625,11 +625,12 @@ private static void addSimpleProviders(

RunfilesProvider runfilesProvider =
statelessRunfiles != null
? RunfilesProvider.simple(mergeFiles(statelessRunfiles, executable, ruleContext))
? RunfilesProvider.simple(ruleContext,
mergeFiles(statelessRunfiles, executable, ruleContext))
: RunfilesProvider.withData(
// The executable doesn't get into the default runfiles if we have runfiles states.
// This is to keep Starlark genrule consistent with the original genrule.
defaultRunfiles != null ? defaultRunfiles : Runfiles.EMPTY,
ruleContext, defaultRunfiles != null ? defaultRunfiles : Runfiles.EMPTY,
dataRunfiles != null ? dataRunfiles : Runfiles.EMPTY);
builder.addProvider(RunfilesProvider.class, runfilesProvider);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public ConfiguredTarget create(RuleContext ruleContext)
return new RuleConfiguredTargetBuilder(ruleContext)
.setFilesToBuild(filesToBuild)
.setRunfilesSupport(runfilesSupport, mainExecutable)
.addProvider(RunfilesProvider.class, RunfilesProvider.simple(runfiles))
.addProvider(RunfilesProvider.class, RunfilesProvider.simple(ruleContext, runfiles))
.addNativeDeclaredProvider(
InstrumentedFilesCollector.collect(ruleContext, ShCoverage.INSTRUMENTATION_SPEC))
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public ConfiguredTarget create(RuleContext ruleContext)
.build();
return new RuleConfiguredTargetBuilder(ruleContext)
.setFilesToBuild(filesToBuild)
.addProvider(RunfilesProvider.class, RunfilesProvider.simple(runfiles))
.addProvider(RunfilesProvider.class, RunfilesProvider.simple(ruleContext, runfiles))
.addNativeDeclaredProvider(
InstrumentedFilesCollector.collect(ruleContext, ShCoverage.INSTRUMENTATION_SPEC))
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public ConfiguredTarget create(RuleContext ruleContext)
private static ConfiguredTarget createEmptyConfiguredTarget(RuleContext ruleContext)
throws ActionConflictException, InterruptedException {
return new RuleConfiguredTargetBuilder(ruleContext)
.addProvider(RunfilesProvider.class, RunfilesProvider.simple(Runfiles.EMPTY))
.addProvider(RunfilesProvider.class, RunfilesProvider.simple(ruleContext, Runfiles.EMPTY))
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public ConfiguredTarget create(RuleContext ruleContext)
ToolchainTypeInfo toolchainTypeInfo = ToolchainTypeInfo.create(ruleContext.getLabel());

return new RuleConfiguredTargetBuilder(ruleContext)
.addProvider(RunfilesProvider.simple(Runfiles.EMPTY))
.addProvider(RunfilesProvider.simple(ruleContext, Runfiles.EMPTY))
.addNativeDeclaredProvider(toolchainTypeInfo)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -854,9 +854,9 @@ public static RuleConfiguredTargetBuilder createAndroidBinary(
.addProvider(
RunfilesProvider.class,
RunfilesProvider.simple(
new Runfiles.Builder(
ruleContext.getWorkspaceName(),
ruleContext.getConfiguration().legacyExternalRunfiles())
ruleContext, new Runfiles.Builder(
ruleContext.getWorkspaceName(),
ruleContext.getConfiguration().legacyExternalRunfiles())
.addRunfiles(ruleContext, RunfilesProvider.DEFAULT_RUNFILES)
.addTransitiveArtifacts(filesToBuild)
.build()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,7 @@ public RuleConfiguredTargetBuilder addTransitiveInfoProviders(
return builder
.setFilesToBuild(filesToBuild)
.addNativeDeclaredProvider(javaInfo)
.addProvider(RunfilesProvider.class, RunfilesProvider.simple(getRunfiles()))
.addProvider(RunfilesProvider.class, RunfilesProvider.simple(ruleContext, getRunfiles()))
.addNativeDeclaredProvider(
createAndroidIdeInfoProvider(
ruleContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public ConfiguredTarget create(RuleContext ruleContext)
ruleContext.attributes().get("pregenerate_oat_files_for_tests", Type.BOOLEAN);
return new RuleConfiguredTargetBuilder(ruleContext)
.setFilesToBuild(filesToBuild)
.addProvider(RunfilesProvider.class, RunfilesProvider.simple(runfiles))
.addProvider(RunfilesProvider.class, RunfilesProvider.simple(ruleContext, runfiles))
.setRunfilesSupport(runfilesSupport, executable)
.addFilesToRun(extraFilesToRun)
.addNativeDeclaredProvider(new ExecutionInfo(executionInfo))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public ConfiguredTarget create(RuleContext ruleContext)
.addProvider(
RunfilesProvider.class,
RunfilesProvider.simple(
new Runfiles.Builder(ruleContext.getWorkspaceName())
ruleContext, new Runfiles.Builder(ruleContext.getWorkspaceName())
.addArtifact(fixtureScript)
.build()))
.addNativeDeclaredProvider(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public ConfiguredTarget create(RuleContext ruleContext)
.build();
return ruleBuilder
.setFilesToBuild(filesToBuild)
.addProvider(RunfilesProvider.class, RunfilesProvider.simple(runfiles))
.addProvider(RunfilesProvider.class, RunfilesProvider.simple(ruleContext, runfiles))
.addNativeDeclaredProvider(
new AndroidHostServiceFixtureInfoProvider(
executable.getExecutable(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public ConfiguredTarget create(RuleContext ruleContext)

return new RuleConfiguredTargetBuilder(ruleContext)
.setFilesToBuild(NestedSetBuilder.<Artifact>stableOrder().add(testExecutable).build())
.addProvider(RunfilesProvider.class, RunfilesProvider.simple(runfiles))
.addProvider(RunfilesProvider.class, RunfilesProvider.simple(ruleContext, runfiles))
.setRunfilesSupport(
RunfilesSupport.withExecutable(ruleContext, runfiles, testExecutable), testExecutable)
.addNativeDeclaredProvider(getExecutionInfoProvider(ruleContext))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ public ConfiguredTarget create(RuleContext ruleContext)
.addProvider(
RunfilesProvider.class,
RunfilesProvider.withData(
defaultRunfiles,
ruleContext, defaultRunfiles,
new Runfiles.Builder(ruleContext.getWorkspaceName())
.merge(runfilesSupport)
.build()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,8 @@ public static void init(

CcStarlarkApiProvider.maybeAdd(ruleContext, ruleBuilder);
ruleBuilder
.addProvider(RunfilesProvider.class, RunfilesProvider.simple(runtimeFiles.runfiles()))
.addProvider(RunfilesProvider.class,
RunfilesProvider.simple(ruleContext, runtimeFiles.runfiles()))
.addNativeDeclaredProvider(
new DebugPackageProvider(ruleContext.getLabel(), strippedFile, binary, explicitDwpFile))
.setRunfilesSupport(runfilesSupport, binary)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public ConfiguredTarget create(RuleContext ruleContext)
ToolchainInfo toolchain = ccToolchainAlias.get(ToolchainInfo.PROVIDER);

return new RuleConfiguredTargetBuilder(ruleContext)
.addProvider(RunfilesProvider.simple(Runfiles.EMPTY))
.addProvider(RunfilesProvider.simple(ruleContext, Runfiles.EMPTY))
.addNativeDeclaredProvider(ccToolchainProvider)
.addNativeDeclaredProvider(toolchain)
.addNativeDeclaredProvider(templateVariableInfo)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ public static void init(
CcCommon.mergeOutputGroups(
ImmutableList.of(currentOutputGroups, outputGroups.buildOrThrow())))
.addNativeDeclaredProvider(instrumentedFilesProvider)
.addProvider(RunfilesProvider.withData(defaultRunfiles.build(), dataRunfiles.build()));
.addProvider(RunfilesProvider.withData(ruleContext, defaultRunfiles.build(), dataRunfiles.build()));
}

private static void warnAboutEmptyLibraries(RuleContext ruleContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public ConfiguredTarget create(RuleContext ruleContext)
RuleConfiguredTargetBuilder ruleConfiguredTargetBuilder =
new RuleConfiguredTargetBuilder(ruleContext)
.addNativeDeclaredProvider(attributes)
.addProvider(RunfilesProvider.simple(Runfiles.EMPTY));
.addProvider(RunfilesProvider.simple(ruleContext, Runfiles.EMPTY));

if (attributes.getLicensesProvider() != null) {
ruleConfiguredTargetBuilder.add(LicensesProvider.class, attributes.getLicensesProvider());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public ConfiguredTarget create(RuleContext ruleContext)
.build());

return new RuleConfiguredTargetBuilder(ruleContext)
.addProvider(RunfilesProvider.simple(Runfiles.EMPTY))
.addProvider(RunfilesProvider.simple(ruleContext, Runfiles.EMPTY))
.addNativeDeclaredProvider(ccToolchainProvider)
.addNativeDeclaredProvider(toolchain)
.addNativeDeclaredProvider(templateVariableInfo)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public ConfiguredTarget create(RuleContext ruleContext)
.addNativeDeclaredProvider(ccToolchainProvider)
.addNativeDeclaredProvider(templateVariableInfo)
.setFilesToBuild(ccToolchainProvider.getAllFilesIncludingLibc())
.addProvider(RunfilesProvider.simple(Runfiles.EMPTY));
.addProvider(RunfilesProvider.simple(ruleContext, Runfiles.EMPTY));

if (ccToolchainProvider.getLicensesProvider() != null) {
builder.add(LicensesProvider.class, ccToolchainProvider.getLicensesProvider());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public ConfiguredTarget create(RuleContext ruleContext)

return new RuleConfiguredTargetBuilder(ruleContext)
.addNativeDeclaredProvider(new FdoPrefetchHintsProvider(inputFile))
.addProvider(RunfilesProvider.simple(Runfiles.EMPTY))
.addProvider(RunfilesProvider.simple(ruleContext, Runfiles.EMPTY))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public ConfiguredTarget create(RuleContext ruleContext)

return new RuleConfiguredTargetBuilder(ruleContext)
.addNativeDeclaredProvider(new FdoProfileProvider(inputFile, protoProfileArtifact))
.addProvider(RunfilesProvider.simple(Runfiles.EMPTY))
.addProvider(RunfilesProvider.simple(ruleContext, Runfiles.EMPTY))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public ConfiguredTarget create(RuleContext ruleContext)

return new RuleConfiguredTargetBuilder(ruleContext)
.addNativeDeclaredProvider(new PropellerOptimizeProvider(inputFile))
.addProvider(RunfilesProvider.simple(Runfiles.EMPTY))
.addProvider(RunfilesProvider.simple(ruleContext, Runfiles.EMPTY))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public ConfiguredTarget create(RuleContext ruleContext)
ruleContext)
.setFilesToBuild(depProviders.filesBuilder)
.addProvider(
RunfilesProvider.class, RunfilesProvider.withData(Runfiles.EMPTY, Runfiles.EMPTY))
RunfilesProvider.class, RunfilesProvider.withData(ruleContext, Runfiles.EMPTY, Runfiles.EMPTY))
.addProviders(depProviders.providerMap);

for (String groupName : depProviders.outputGroupInfo) {
Expand Down
Loading