Search in sources :

Example 1 with SkylarkProviders

use of com.google.devtools.build.lib.analysis.SkylarkProviders in project bazel by bazelbuild.

the class SkylarkIntegrationTest method rulesReturningDeclaredProviders.

@Test
public void rulesReturningDeclaredProviders() throws Exception {
    scratch.file("test/extension.bzl", "my_provider = provider()", "def _impl(ctx):", "   return [my_provider(x = 1)]", "my_rule = rule(_impl)");
    scratch.file("test/BUILD", "load(':extension.bzl', 'my_rule')", "my_rule(name = 'r')");
    ConfiguredTarget configuredTarget = getConfiguredTarget("//test:r");
    ClassObjectConstructor.Key key = new SkylarkClassObjectConstructor.SkylarkKey(Label.create(configuredTarget.getLabel().getPackageIdentifier(), "extension.bzl"), "my_provider");
    SkylarkProviders skylarkProviders = configuredTarget.getProvider(SkylarkProviders.class);
    assertThat(skylarkProviders).isNotNull();
    SkylarkClassObject declaredProvider = skylarkProviders.getDeclaredProvider(key);
    assertThat(declaredProvider).isNotNull();
    assertThat(declaredProvider.getConstructor().getKey()).isEqualTo(key);
    assertThat(declaredProvider.getValue("x")).isEqualTo(1);
}
Also used : SkylarkProviders(com.google.devtools.build.lib.analysis.SkylarkProviders) SkylarkClassObjectConstructor(com.google.devtools.build.lib.packages.SkylarkClassObjectConstructor) ClassObjectConstructor(com.google.devtools.build.lib.packages.ClassObjectConstructor) SkylarkClassObject(com.google.devtools.build.lib.packages.SkylarkClassObject) RuleConfiguredTarget(com.google.devtools.build.lib.analysis.RuleConfiguredTarget) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) FileConfiguredTarget(com.google.devtools.build.lib.analysis.FileConfiguredTarget) Test(org.junit.Test)

Example 2 with SkylarkProviders

use of com.google.devtools.build.lib.analysis.SkylarkProviders in project bazel by bazelbuild.

the class SkylarkAspectsTest method labelKeyedStringDictAllowsAspects.

@Test
public void labelKeyedStringDictAllowsAspects() throws Exception {
    scratch.file("test/aspect.bzl", "def _aspect_impl(target, ctx):", "   return struct(aspect_data=target.label.name)", "", "def _rule_impl(ctx):", "   return struct(", "       data=','.join(['{}:{}'.format(dep.aspect_data, val)", "                      for dep, val in ctx.attr.attr.items()]))", "", "MyAspect = aspect(", "   implementation=_aspect_impl,", ")", "my_rule = rule(", "   implementation=_rule_impl,", "   attrs = { 'attr' : ", "             attr.label_keyed_string_dict(aspects = [MyAspect]) },", ")");
    scratch.file("test/BUILD", "load('/test/aspect', 'my_rule')", "java_library(", "     name = 'yyy',", ")", "my_rule(", "     name = 'xxx',", "     attr = {':yyy': 'zzz'},", ")");
    AnalysisResult analysisResult = update("//test:xxx");
    ConfiguredTarget target = analysisResult.getTargetsToBuild().iterator().next();
    SkylarkProviders skylarkProviders = target.getProvider(SkylarkProviders.class);
    Object value = skylarkProviders.getValue("data");
    assertThat(value).isEqualTo("yyy:zzz");
}
Also used : SkylarkProviders(com.google.devtools.build.lib.analysis.SkylarkProviders) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) AnalysisResult(com.google.devtools.build.lib.analysis.BuildView.AnalysisResult) Test(org.junit.Test)

Example 3 with SkylarkProviders

use of com.google.devtools.build.lib.analysis.SkylarkProviders in project bazel by bazelbuild.

the class SkylarkAspectsTest method aspectOnLabelAttr.

@Test
public void aspectOnLabelAttr() throws Exception {
    scratch.file("test/aspect.bzl", "def _aspect_impl(target, ctx):", "   return struct(aspect_data='foo')", "", "def _rule_impl(ctx):", "   return struct(data=ctx.attr.attr.aspect_data)", "", "MyAspect = aspect(", "   implementation=_aspect_impl,", ")", "my_rule = rule(", "   implementation=_rule_impl,", "   attrs = { 'attr' : ", "             attr.label(aspects = [MyAspect]) },", ")");
    scratch.file("test/BUILD", "load('/test/aspect', 'my_rule')", "java_library(", "     name = 'yyy',", ")", "my_rule(", "     name = 'xxx',", "     attr = ':yyy',", ")");
    AnalysisResult analysisResult = update("//test:xxx");
    ConfiguredTarget target = analysisResult.getTargetsToBuild().iterator().next();
    SkylarkProviders skylarkProviders = target.getProvider(SkylarkProviders.class);
    Object value = skylarkProviders.getValue("data");
    assertThat(value).isEqualTo("foo");
}
Also used : SkylarkProviders(com.google.devtools.build.lib.analysis.SkylarkProviders) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) AnalysisResult(com.google.devtools.build.lib.analysis.BuildView.AnalysisResult) Test(org.junit.Test)

Example 4 with SkylarkProviders

use of com.google.devtools.build.lib.analysis.SkylarkProviders in project bazel by bazelbuild.

the class SkylarkAspectsTest method aspectsFromSkylarkRules.

@Test
public void aspectsFromSkylarkRules() throws Exception {
    scratch.file("test/aspect.bzl", "def _aspect_impl(target, ctx):", "   s = depset([target.label])", "   for i in ctx.rule.attr.deps:", "       s += i.target_labels", "   return struct(target_labels = s)", "", "def _rule_impl(ctx):", "   s = depset([])", "   for i in ctx.attr.attr:", "       s += i.target_labels", "   return struct(rule_deps = s)", "", "MyAspect = aspect(", "   implementation=_aspect_impl,", "   attr_aspects=['deps'],", ")", "my_rule = rule(", "   implementation=_rule_impl,", "   attrs = { 'attr' : ", "             attr.label_list(mandatory=True, allow_files=True, aspects = [MyAspect]) },", ")");
    scratch.file("test/BUILD", "load('/test/aspect', 'my_rule')", "java_library(", "     name = 'yyy',", ")", "my_rule(", "     name = 'xxx',", "     attr = [':yyy'],", ")");
    AnalysisResult analysisResult = update("//test:xxx");
    assertThat(getLabelsToBuild(analysisResult)).containsExactly("//test:xxx");
    ConfiguredTarget target = analysisResult.getTargetsToBuild().iterator().next();
    SkylarkProviders skylarkProviders = target.getProvider(SkylarkProviders.class);
    assertThat(skylarkProviders).isNotNull();
    Object names = skylarkProviders.getValue("rule_deps");
    assertThat(names).isInstanceOf(SkylarkNestedSet.class);
    assertThat(transform(((SkylarkNestedSet) names).toCollection(), new Function<Object, String>() {

        @Nullable
        @Override
        public String apply(Object o) {
            assertThat(o).isInstanceOf(Label.class);
            return o.toString();
        }
    })).containsExactly("//test:yyy");
}
Also used : SkylarkProviders(com.google.devtools.build.lib.analysis.SkylarkProviders) Label(com.google.devtools.build.lib.cmdline.Label) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) AnalysisResult(com.google.devtools.build.lib.analysis.BuildView.AnalysisResult) Nullable(javax.annotation.Nullable) Test(org.junit.Test)

Example 5 with SkylarkProviders

use of com.google.devtools.build.lib.analysis.SkylarkProviders in project bazel by bazelbuild.

the class JavaProvider method getProvider.

/**
   * Returns a provider of the specified class, fetched from the specified target or, if not found,
   * from the JavaProvider of the given target. JavaProvider can be found as a declared provider
   * in SkylarkProviders.
   * Returns null if no such provider exists.
   *
   * <p>A target can either have both the specified provider and JavaProvider that encapsulates the
   * same information, or just one of them.</p>
   */
@Nullable
public static <T extends TransitiveInfoProvider> T getProvider(Class<T> providerClass, TransitiveInfoCollection target) {
    T provider = target.getProvider(providerClass);
    if (provider != null) {
        return provider;
    }
    SkylarkProviders skylarkProviders = target.getProvider(SkylarkProviders.class);
    if (skylarkProviders == null) {
        return null;
    }
    JavaProvider javaProvider = (JavaProvider) skylarkProviders.getDeclaredProvider(JavaProvider.JAVA_PROVIDER.getKey());
    if (javaProvider == null) {
        return null;
    }
    return javaProvider.getProvider(providerClass);
}
Also used : SkylarkProviders(com.google.devtools.build.lib.analysis.SkylarkProviders) Nullable(javax.annotation.Nullable)

Aggregations

SkylarkProviders (com.google.devtools.build.lib.analysis.SkylarkProviders)17 Test (org.junit.Test)12 ConfiguredTarget (com.google.devtools.build.lib.analysis.ConfiguredTarget)9 AnalysisResult (com.google.devtools.build.lib.analysis.BuildView.AnalysisResult)6 Nullable (javax.annotation.Nullable)6 SkylarkClassObject (com.google.devtools.build.lib.packages.SkylarkClassObject)5 Artifact (com.google.devtools.build.lib.actions.Artifact)4 FileConfiguredTarget (com.google.devtools.build.lib.analysis.FileConfiguredTarget)4 RuleConfiguredTarget (com.google.devtools.build.lib.analysis.RuleConfiguredTarget)4 Label (com.google.devtools.build.lib.cmdline.Label)4 SkylarkNestedSet (com.google.devtools.build.lib.syntax.SkylarkNestedSet)4 AspectValue (com.google.devtools.build.lib.skyframe.AspectValue)3 OutputGroupProvider (com.google.devtools.build.lib.analysis.OutputGroupProvider)2 ClassObjectConstructor (com.google.devtools.build.lib.packages.ClassObjectConstructor)2 SkylarkClassObjectConstructor (com.google.devtools.build.lib.packages.SkylarkClassObjectConstructor)2 FileProvider (com.google.devtools.build.lib.analysis.FileProvider)1 TransitiveInfoCollection (com.google.devtools.build.lib.analysis.TransitiveInfoCollection)1 SkylarkKey (com.google.devtools.build.lib.packages.SkylarkClassObjectConstructor.SkylarkKey)1 JavaProvider (com.google.devtools.build.lib.rules.java.JavaProvider)1 EvalException (com.google.devtools.build.lib.syntax.EvalException)1