Skip to content

Commit

Permalink
Aspects should contribute base rule toolchain make variables to Confi…
Browse files Browse the repository at this point in the history
…gurationMakeVariableContext.

PiperOrigin-RevId: 720262297
Change-Id: Idba7a29323d57c87c928a997997497f78ace95c5
  • Loading branch information
katre authored and copybara-github committed Jan 27, 2025
1 parent 97e125f commit 0a9ebcf
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

package com.google.devtools.build.lib.analysis;

import static com.google.common.base.Predicates.notNull;
import static com.google.common.collect.ImmutableList.toImmutableList;

import com.google.auto.value.AutoValue;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -63,16 +66,27 @@ public static AspectBaseTargetResolvedToolchainContext load(

if (toolchainTarget instanceof MergedConfiguredTarget mergedConfiguredTarget) {
// Only add the aspects providers from the toolchains that the aspects applied to.
TemplateVariableInfo templateVariableInfo =
(TemplateVariableInfo)
mergedConfiguredTarget
.getBaseConfiguredTarget()
.get(TemplateVariableInfo.PROVIDER.id());
toolchainsBuilder.put(
toolchainType,
new ToolchainAspectsProviders(
mergedConfiguredTarget.getAspectsProviders(), mergedConfiguredTarget.getLabel()));
mergedConfiguredTarget.getAspectsProviders(),
templateVariableInfo,
mergedConfiguredTarget.getLabel()));
} else {
// Add empty providers for the toolchains that the aspects did not apply to.
TemplateVariableInfo templateVariableInfo =
(TemplateVariableInfo) toolchainTarget.get(TemplateVariableInfo.PROVIDER.id());
toolchainsBuilder.put(
toolchainType,
new ToolchainAspectsProviders(
new TransitiveInfoProviderMapBuilder().build(), toolchainTarget.getLabel()));
new TransitiveInfoProviderMapBuilder().build(),
templateVariableInfo,
toolchainTarget.getLabel()));
}
}
ImmutableMap<ToolchainTypeInfo, ToolchainAspectsProviders> toolchains =
Expand Down Expand Up @@ -102,6 +116,13 @@ public ToolchainAspectsProviders forToolchainType(Label toolchainTypeLabel) {
return null;
}

public ImmutableList<TemplateVariableInfo> templateVariableProviders() {
return toolchains().values().stream()
.map(ToolchainAspectsProviders::templateVariableProvider)
.filter(notNull())
.collect(toImmutableList());
}

/**
* A Starlark-indexable wrapper used to represent the providers of the aspects applied on the base
* target toolchains.
Expand All @@ -110,10 +131,15 @@ public static class ToolchainAspectsProviders
implements StarlarkIndexable, Structure, ResolvedToolchainData {

private final TransitiveInfoProviderMap aspectsProviders;
@Nullable private final TemplateVariableInfo templateVariableInfo;
private final Label label;

private ToolchainAspectsProviders(TransitiveInfoProviderMap aspectsProviders, Label label) {
private ToolchainAspectsProviders(
TransitiveInfoProviderMap aspectsProviders,
@Nullable TemplateVariableInfo templateVariableInfo,
Label label) {
this.aspectsProviders = aspectsProviders;
this.templateVariableInfo = templateVariableInfo;
this.label = label;
}

Expand Down Expand Up @@ -178,5 +204,10 @@ public String getErrorMessageForUnknownField(String field) {
// Use the default error message.
return null;
}

@Nullable
public TemplateVariableInfo templateVariableProvider() {
return this.templateVariableInfo;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -287,4 +287,23 @@ public ImmutableList<? extends TransitiveInfoCollection> getAllPrerequisites() {
getRulePrerequisitesCollection().getAllPrerequisites().stream())
.collect(toImmutableList());
}

private ImmutableList<TemplateVariableInfo> getTemplateVariablesFromBaseRuleToolchains() {
if (this.getBaseTargetToolchainContexts() == null) {
return ImmutableList.of();
}

return this.getBaseTargetToolchainContexts().contextMap().values().stream()
.flatMap(context -> context.templateVariableProviders().stream())
.collect(toImmutableList());
}

@Override
public ImmutableList<TemplateVariableInfo> getDefaultTemplateVariableProviders() {
return new ImmutableList.Builder<TemplateVariableInfo>()
.addAll(super.getDefaultTemplateVariableProviders())
// Add base rule toolchain data in addition to aspect attribute and toolchain data.
.addAll(getTemplateVariablesFromBaseRuleToolchains())
.build();
}
}

0 comments on commit 0a9ebcf

Please sign in to comment.