Skip to content

Commit

Permalink
Provide label of the toolchain in ToolchainAspectsProviders
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 690667496
Change-Id: Ice3cfd5f27ed0f94979cb642dffa56ef2a2dc4a9
  • Loading branch information
mai93 authored and copybara-github committed Oct 28, 2024
1 parent 66a1038 commit 419fdac
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import com.google.auto.value.AutoValue;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.Iterables;
Expand All @@ -32,6 +33,7 @@
import net.starlark.java.eval.Starlark;
import net.starlark.java.eval.StarlarkIndexable;
import net.starlark.java.eval.StarlarkSemantics;
import net.starlark.java.eval.Structure;

/**
* A toolchain context for the aspect's base target toolchains. It is used to represent the result
Expand Down Expand Up @@ -105,7 +107,7 @@ public ToolchainAspectsProviders forToolchainType(Label toolchainTypeLabel) {
* target toolchains.
*/
public static class ToolchainAspectsProviders
implements StarlarkIndexable, ResolvedToolchainData {
implements StarlarkIndexable, Structure, ResolvedToolchainData {

private final TransitiveInfoProviderMap aspectsProviders;
private final Label label;
Expand Down Expand Up @@ -155,5 +157,26 @@ private Provider selectExportedProvider(Object key, String operation) throws Eva
public void repr(Printer printer) {
printer.append("<ToolchainAspectsProviders for toolchain target: " + label + ">");
}

@Nullable
@Override
public Object getValue(String name) {
if (name.equals(MergedConfiguredTarget.LABEL_FIELD)) {
return label;
}
return null;
}

@Override
public ImmutableList<String> getFieldNames() {
return ImmutableList.of(MergedConfiguredTarget.LABEL_FIELD);
}

@Nullable
@Override
public String getErrorMessageForUnknownField(String field) {
// Use the default error message.
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2218,6 +2218,56 @@ def _rule_impl(ctx):
+ " exec_platform: //platforms:platform_2");
}

@Test
public void aspectPropagatesToToolchain_seesToolchainLabel(@TestParameter boolean autoExecGroups)
throws Exception {
scratch.file(
"test/defs.bzl",
"""
AspectProvider = provider()
def _impl(target, ctx):
if ctx.rule.toolchains and '//rule:toolchain_type_1' in ctx.rule.toolchains:
return [
AspectProvider(value = str(target.label) + ' has toolchain ' +
str(ctx.rule.toolchains['//rule:toolchain_type_1'].label))]
return []
toolchain_aspect = aspect(
implementation = _impl,
toolchains_aspects = ['//rule:toolchain_type_1'],
)
def _rule_impl(ctx):
pass
r1 = rule(
implementation = _rule_impl,
toolchains = ['//rule:toolchain_type_1'],
)
""");
scratch.file(
"test/BUILD",
"""
load('//test:defs.bzl', 'r1')
r1(name = 't1')
""");
useConfiguration(
"--extra_toolchains=//toolchain:foo_toolchain",
"--incompatible_auto_exec_groups=" + autoExecGroups);

var analysisResult = update(ImmutableList.of("//test:defs.bzl%toolchain_aspect"), "//test:t1");

ConfiguredAspect configuredAspect =
Iterables.getOnlyElement(analysisResult.getAspectsMap().values());

StarlarkProvider.Key providerKey =
new StarlarkProvider.Key(
keyForBuild(Label.parseCanonical("//test:defs.bzl")), "AspectProvider");

var value = ((StarlarkInfo) configuredAspect.get(providerKey)).getValue("value");
assertThat((String) value).isEqualTo("@@//test:t1 has toolchain @@//toolchain:foo");
}

private ImmutableList<ConfiguredTargetKey> getConfiguredTargetKey(String targetLabel) {
return skyframeExecutor.getEvaluator().getInMemoryGraph().getAllNodeEntries().stream()
.filter(n -> isConfiguredTarget(n.getKey(), targetLabel))
Expand Down

0 comments on commit 419fdac

Please sign in to comment.