Skip to content

Commit

Permalink
Add example test for requiring native aspect from a starlark defined …
Browse files Browse the repository at this point in the history
…aspect.

Starlark native aspects need to override the newly added `getDefaultParametersExtractor` from StarlarkAspect to be able to extract their parameters values from the base rule of the attribute in which they are required.

PiperOrigin-RevId: 381826763
  • Loading branch information
mai93 authored and copybara-github committed Jun 28, 2021
1 parent af42653 commit ae81f95
Show file tree
Hide file tree
Showing 8 changed files with 228 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1191,7 +1191,7 @@ public Builder<TYPE> aspect(
}

public Builder<TYPE> aspect(
NativeAspectClass nativeAspect,
StarlarkNativeAspect nativeAspect,
String baseAspectName,
ImmutableList<ImmutableSet<StarlarkProviderIdentifier>> inheritedRequiredProviders,
ImmutableList<String> inheritedAttributeAspects)
Expand All @@ -1206,7 +1206,7 @@ public Builder<TYPE> aspect(
NativeRuleAspect nativeRuleAspect =
new NativeRuleAspect(
nativeAspect,
EMPTY_FUNCTION,
nativeAspect.getDefaultParametersExtractor(),
baseAspectName,
inheritedRequiredProviders,
inheritedAttributeAspects);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

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

import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.devtools.build.lib.starlarkbuildapi.StarlarkAspectApi;
Expand Down Expand Up @@ -56,4 +57,7 @@ void attachToAttribute(

/** Returns the name of this aspect. */
String getName();

/** Returns a function to extract the aspect parameters values from its base rule. */
Function<Rule, AspectParameters> getDefaultParametersExtractor();
}
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ public boolean isExported() {
return aspectClass != null;
}

@Override
public Function<Rule, AspectParameters> getDefaultParametersExtractor() {
return rule -> {
AttributeMap ruleAttrs = RawAttributeMapper.of(rule);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,18 @@

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

import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
import net.starlark.java.eval.EvalException;
import net.starlark.java.eval.Printer;

/** A natively-defined aspect that is may be referenced by Starlark attribute definitions. */
public abstract class StarlarkNativeAspect extends NativeAspectClass implements StarlarkAspect {
@AutoCodec @AutoCodec.VisibleForSerialization
static final Function<Rule, AspectParameters> EMPTY_FUNCTION = input -> AspectParameters.EMPTY;

@Override
public void repr(Printer printer) {
printer.append("<native aspect>");
Expand All @@ -45,4 +50,9 @@ public AspectClass getAspectClass() {
public ImmutableSet<String> getParamAttributes() {
return ImmutableSet.of();
}

@Override
public Function<Rule, AspectParameters> getDefaultParametersExtractor() {
return EMPTY_FUNCTION;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,12 @@ public SkyValue compute(SkyKey skyKey, Environment env)
if (key.getAspectClass() instanceof NativeAspectClass) {
NativeAspectClass nativeAspectClass = (NativeAspectClass) key.getAspectClass();
aspectFactory = (ConfiguredAspectFactory) nativeAspectClass;
aspect = Aspect.forNative(nativeAspectClass, key.getParameters());
aspect =
Aspect.forNative(
nativeAspectClass,
key.getParameters(),
key.getInheritedRequiredProviders(),
key.getInheritedAttributeAspects());
} else if (key.getAspectClass() instanceof StarlarkAspectClass) {
StarlarkAspectClass starlarkAspectClass = (StarlarkAspectClass) key.getAspectClass();
StarlarkDefinedAspect starlarkAspect;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,23 @@
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
import com.google.devtools.build.lib.packages.AspectDefinition;
import com.google.devtools.build.lib.packages.AspectParameters;
import com.google.devtools.build.lib.packages.Attribute.AllowedValueSet;
import com.google.devtools.build.lib.packages.Attribute.ComputedDefault;
import com.google.devtools.build.lib.packages.Attribute.LabelListLateBoundDefault;
import com.google.devtools.build.lib.packages.AttributeMap;
import com.google.devtools.build.lib.packages.BuildType;
import com.google.devtools.build.lib.packages.NativeAspectClass;
import com.google.devtools.build.lib.packages.RawAttributeMapper;
import com.google.devtools.build.lib.packages.Rule;
import com.google.devtools.build.lib.packages.StarlarkNativeAspect;
import com.google.devtools.build.lib.packages.StarlarkProviderIdentifier;
import com.google.devtools.build.lib.packages.Type;
import com.google.devtools.build.lib.rules.java.JavaConfiguration;
import com.google.devtools.build.lib.skyframe.ConfiguredTargetAndData;
import com.google.devtools.build.lib.util.FileTypeSet;
import java.io.Serializable;
import java.util.List;
import javax.annotation.Nullable;

/**
* Various rule and aspect classes that aid in testing the aspect machinery.
Expand Down Expand Up @@ -251,14 +256,38 @@ public ConfiguredAspect create(
public static final SimpleAspect SIMPLE_ASPECT = new SimpleAspect();
public static final FooProviderAspect FOO_PROVIDER_ASPECT = new FooProviderAspect();
public static final BarProviderAspect BAR_PROVIDER_ASPECT = new BarProviderAspect();
public static final SimpleStarlarkNativeAspect SIMPLE_STARLARK_NATIVE_ASPECT =
new SimpleStarlarkNativeAspect();
public static final ParametrizedAspectWithProvider
PARAMETRIZED_STARLARK_NATIVE_ASPECT_WITH_PROVIDER = new ParametrizedAspectWithProvider();

private static final AspectDefinition SIMPLE_ASPECT_DEFINITION =
new AspectDefinition.Builder(SIMPLE_ASPECT).build();

private static final AspectDefinition FOO_PROVIDER_ASPECT_DEFINITION =
new AspectDefinition.Builder(FOO_PROVIDER_ASPECT).build();
private static final AspectDefinition BAR_PROVIDER_ASPECT_DEFINITION =
new AspectDefinition.Builder(BAR_PROVIDER_ASPECT).build();
private static final AspectDefinition SIMPLE_STARLARK_NATIVE_ASPECT_DEFINITION =
new AspectDefinition.Builder(SIMPLE_STARLARK_NATIVE_ASPECT).build();

/** Simple StarlarkNativeAspect */
public static class SimpleStarlarkNativeAspect extends StarlarkNativeAspect
implements ConfiguredAspectFactory {
@Override
public AspectDefinition getDefinition(AspectParameters aspectParameters) {
return SIMPLE_STARLARK_NATIVE_ASPECT_DEFINITION;
}

@Override
public ConfiguredAspect create(
ConfiguredTargetAndData ctadBase,
RuleContext ruleContext,
AspectParameters parameters,
String toolsRepository)
throws ActionConflictException, InterruptedException {
return new ConfiguredAspect.Builder(ruleContext).addProvider(new FooProvider()).build();
}
}

/**
* A very simple aspect.
Expand Down Expand Up @@ -457,6 +486,49 @@ public AspectDefinition getDefinition(AspectParameters aspectParameters) {
}
}

/**
* An aspect that has a definition depending on parameters provided by originating rule and
* advertises a simple provider.
*/
public static class ParametrizedAspectWithProvider extends StarlarkNativeAspect
implements ConfiguredAspectFactory {

@Override
public AspectDefinition getDefinition(AspectParameters aspectParameters) {
AspectDefinition.Builder builder =
new AspectDefinition.Builder(PARAMETRIZED_STARLARK_NATIVE_ASPECT_WITH_PROVIDER);
ImmutableCollection<String> aspectAttr = aspectParameters.getAttribute("aspect_attr");
if (aspectAttr != null) {
builder.add(
attr("aspect_attr", Type.STRING)
.allowedValues(new AllowedValueSet("v1", "v2"))
.value(aspectAttr.iterator().next()));
}
return builder.build();
}

@Override
public ConfiguredAspect create(
ConfiguredTargetAndData ctadBase,
RuleContext ruleContext,
AspectParameters parameters,
String toolsRepository)
throws ActionConflictException, InterruptedException {
return new ConfiguredAspect.Builder(ruleContext).addProvider(new FooProvider()).build();
}

@Override
public Function<Rule, AspectParameters> getDefaultParametersExtractor() {
return (Function<Rule, AspectParameters> & Serializable)
(@Nullable Rule rule) -> {
AttributeMap attributes = RawAttributeMapper.of(rule);
return new AspectParameters.Builder()
.addAttribute("aspect_attr", attributes.get("aspect_attr", Type.STRING))
.build();
};
}
}

/**
* An aspect that has a definition depending on parameters provided by originating rule.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,13 +344,13 @@ public void testAttrRequiredAspects_inheritAttrAspects() throws Exception {
Attribute attr =
attr("x", LABEL)
.aspect(
TestAspects.SIMPLE_ASPECT,
TestAspects.SIMPLE_STARLARK_NATIVE_ASPECT,
"base_aspect_1",
/** inheritedRequiredProviders= */
ImmutableList.of(),
inheritedAttributeAspects1)
.aspect(
TestAspects.SIMPLE_ASPECT,
TestAspects.SIMPLE_STARLARK_NATIVE_ASPECT,
"base_aspect_2",
/** inheritedRequiredProviders= */
ImmutableList.of(),
Expand All @@ -375,13 +375,13 @@ public void testAttrRequiredAspects_inheritRequiredProviders() throws Exception
Attribute attr =
attr("x", LABEL)
.aspect(
TestAspects.SIMPLE_ASPECT,
TestAspects.SIMPLE_STARLARK_NATIVE_ASPECT,
"base_aspect_1",
inheritedRequiredProviders1,
/** inheritedAttributeAspects= */
ImmutableList.of())
.aspect(
TestAspects.SIMPLE_ASPECT,
TestAspects.SIMPLE_STARLARK_NATIVE_ASPECT,
"base_aspect_2",
inheritedRequiredProviders2,
/** inheritedAttributeAspects= */
Expand Down Expand Up @@ -417,9 +417,9 @@ public void testAttrRequiredAspects_aspectAlreadyExists_inheritAttrAspects() thr

Attribute attr =
attr("x", LABEL)
.aspect(TestAspects.SIMPLE_ASPECT)
.aspect(TestAspects.SIMPLE_STARLARK_NATIVE_ASPECT)
.aspect(
TestAspects.SIMPLE_ASPECT,
TestAspects.SIMPLE_STARLARK_NATIVE_ASPECT,
"base_aspect",
/** inheritedRequiredProviders = */
ImmutableList.of(),
Expand All @@ -441,9 +441,9 @@ public void testAttrRequiredAspects_aspectAlreadyExists_inheritRequiredProviders

Attribute attr =
attr("x", LABEL)
.aspect(TestAspects.SIMPLE_ASPECT)
.aspect(TestAspects.SIMPLE_STARLARK_NATIVE_ASPECT)
.aspect(
TestAspects.SIMPLE_ASPECT,
TestAspects.SIMPLE_STARLARK_NATIVE_ASPECT,
"base_aspect",
inheritedRequiredProviders,
/** inheritedAttributeAspects= */
Expand Down Expand Up @@ -480,15 +480,15 @@ public void testAttrRequiredAspects_inheritAllAttrAspects() throws Exception {

Attribute attr =
attr("x", LABEL)
.aspect(TestAspects.SIMPLE_ASPECT)
.aspect(TestAspects.SIMPLE_STARLARK_NATIVE_ASPECT)
.aspect(
TestAspects.SIMPLE_ASPECT,
TestAspects.SIMPLE_STARLARK_NATIVE_ASPECT,
"base_aspect_1",
/** inheritedRequiredProviders = */
ImmutableList.of(),
inheritedAttributeAspects1)
.aspect(
TestAspects.SIMPLE_ASPECT,
TestAspects.SIMPLE_STARLARK_NATIVE_ASPECT,
"base_aspect_2",
/** inheritedRequiredProviders = */
ImmutableList.of(),
Expand All @@ -511,15 +511,15 @@ public void testAttrRequiredAspects_inheritAllRequiredProviders() throws Excepti

Attribute attr =
attr("x", LABEL)
.aspect(TestAspects.SIMPLE_ASPECT)
.aspect(TestAspects.SIMPLE_STARLARK_NATIVE_ASPECT)
.aspect(
TestAspects.SIMPLE_ASPECT,
TestAspects.SIMPLE_STARLARK_NATIVE_ASPECT,
"base_aspect_1",
inheritedRequiredProviders1,
/** inheritedAttributeAspects= */
ImmutableList.of())
.aspect(
TestAspects.SIMPLE_ASPECT,
TestAspects.SIMPLE_STARLARK_NATIVE_ASPECT,
"base_aspect_2",
inheritedRequiredProviders2,
/** inheritedAttributeAspects= */
Expand Down
Loading

0 comments on commit ae81f95

Please sign in to comment.