Search in sources :

Example 11 with SkylarkProviders

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

the class SkylarkAspectsTest method aspectsPropagatingForDefaultAndImplicit.

@Test
public void aspectsPropagatingForDefaultAndImplicit() throws Exception {
    scratch.file("test/aspect.bzl", "def _impl(target, ctx):", "   s = depset([target.label])", "   c = depset([ctx.rule.kind])", "   a = ctx.rule.attr", "   if hasattr(a, '_stl') and a._stl:", "       s += a._stl.target_labels", "       c += a._stl.rule_kinds", "   if hasattr(a, '_stl_default') and a._stl_default:", "       s += a._stl_default.target_labels", "       c += a._stl_default.rule_kinds", "   return struct(target_labels = s, rule_kinds = c)", "", "def _rule_impl(ctx):", "   pass", "", "my_rule = rule(implementation = _rule_impl,", "   attrs = { '_stl' : attr.label(default = Label('//test:xxx')) },", ")", "MyAspect = aspect(", "   implementation=_impl,", "   attr_aspects=['_stl', '_stl_default'],", ")");
    scratch.file("test/BUILD", "load('/test/aspect', 'my_rule')", "cc_library(", "     name = 'xxx',", ")", "my_rule(", "     name = 'yyy',", ")");
    AnalysisResult analysisResult = update(ImmutableList.of("test/aspect.bzl%MyAspect"), "//test:yyy");
    AspectValue aspectValue = analysisResult.getAspects().iterator().next();
    SkylarkProviders skylarkProviders = aspectValue.getConfiguredAspect().getProvider(SkylarkProviders.class);
    assertThat(skylarkProviders).isNotNull();
    Object names = skylarkProviders.getValue("target_labels");
    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 ((Label) o).getName();
        }
    })).containsExactly("stl", "xxx", "yyy");
}
Also used : SkylarkProviders(com.google.devtools.build.lib.analysis.SkylarkProviders) AspectValue(com.google.devtools.build.lib.skyframe.AspectValue) Label(com.google.devtools.build.lib.cmdline.Label) AnalysisResult(com.google.devtools.build.lib.analysis.BuildView.AnalysisResult) Nullable(javax.annotation.Nullable) Test(org.junit.Test)

Example 12 with SkylarkProviders

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

the class SkylarkAspectsTest method aspectsPropagatingToAllAttributes.

@Test
public void aspectsPropagatingToAllAttributes() throws Exception {
    scratch.file("test/aspect.bzl", "def _impl(target, ctx):", "   s = depset([target.label])", "   if hasattr(ctx.rule.attr, 'runtime_deps'):", "     for i in ctx.rule.attr.runtime_deps:", "       s += i.target_labels", "   return struct(target_labels = s)", "", "MyAspect = aspect(", "    implementation=_impl,", "    attrs = { '_tool' : attr.label(default = Label('//test:tool')) },", "    attr_aspects=['*'],", ")");
    scratch.file("test/BUILD", "java_library(", "    name = 'tool',", ")", "java_library(", "     name = 'bar',", "     runtime_deps = [':tool'],", ")", "java_library(", "     name = 'foo',", "     runtime_deps = [':bar'],", ")");
    AnalysisResult analysisResult = update(ImmutableList.of("test/aspect.bzl%MyAspect"), "//test:foo");
    AspectValue aspectValue = analysisResult.getAspects().iterator().next();
    SkylarkProviders skylarkProviders = aspectValue.getConfiguredAspect().getProvider(SkylarkProviders.class);
    assertThat(skylarkProviders).isNotNull();
    Object names = skylarkProviders.getValue("target_labels");
    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 ((Label) o).getName();
        }
    })).containsExactly("foo", "bar", "tool");
}
Also used : SkylarkProviders(com.google.devtools.build.lib.analysis.SkylarkProviders) AspectValue(com.google.devtools.build.lib.skyframe.AspectValue) Label(com.google.devtools.build.lib.cmdline.Label) AnalysisResult(com.google.devtools.build.lib.analysis.BuildView.AnalysisResult) Nullable(javax.annotation.Nullable) Test(org.junit.Test)

Example 13 with SkylarkProviders

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

the class SkylarkJavaLiteProtoLibraryTest method getProvider.

private static <P extends TransitiveInfoProvider> P getProvider(Class<P> providerClass, ConfiguredTarget target) {
    SkylarkProviders skylarkProviders = target.getProvider(SkylarkProviders.class);
    JavaProvider javaProvider = (JavaProvider) skylarkProviders.getDeclaredProvider(JavaProvider.JAVA_PROVIDER.getKey());
    return javaProvider.getProvider(providerClass);
}
Also used : SkylarkProviders(com.google.devtools.build.lib.analysis.SkylarkProviders) JavaProvider(com.google.devtools.build.lib.rules.java.JavaProvider)

Example 14 with SkylarkProviders

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

the class JavaSkylarkApiTest method testJavaPlugin.

@Test
public void testJavaPlugin() throws Exception {
    scratch.file("java/test/extension.bzl", "result = provider()", "def impl(ctx):", "   depj = ctx.attr.dep.java", "   return [result(", "             processor_classpath = depj.annotation_processing.processor_classpath,", "             processor_classnames = depj.annotation_processing.processor_classnames,", "          )]", "my_rule = rule(impl, attrs = { 'dep' : attr.label() })");
    scratch.file("java/test/BUILD", "load(':extension.bzl', 'my_rule')", "java_library(name = 'plugin_dep',", "    srcs = [ 'ProcessorDep.java'])", "java_plugin(name = 'plugin',", "    srcs = ['AnnotationProcessor.java'],", "    processor_class = 'com.google.process.stuff',", "    deps = [ ':plugin_dep' ])", "java_library(name = 'to_be_processed',", "    plugins = [':plugin'],", "    srcs = ['ToBeProcessed.java'])", "my_rule(name = 'my', dep = ':to_be_processed')");
    ConfiguredTarget configuredTarget = getConfiguredTarget("//java/test:my");
    SkylarkProviders provider = configuredTarget.getProvider(SkylarkProviders.class);
    SkylarkClassObject skylarkClassObject = provider.getDeclaredProvider(new SkylarkKey(Label.parseAbsolute("//java/test:extension.bzl"), "result"));
    assertThat(Iterables.transform((List<?>) skylarkClassObject.getValue("processor_classpath"), new Function<Object, String>() {

        @Nullable
        @Override
        public String apply(@Nullable Object o) {
            return ((Artifact) o).getFilename();
        }
    })).containsExactly("libplugin.jar", "libplugin_dep.jar");
    assertThat((List<?>) skylarkClassObject.getValue("processor_classnames")).containsExactly("com.google.process.stuff");
}
Also used : SkylarkProviders(com.google.devtools.build.lib.analysis.SkylarkProviders) SkylarkClassObject(com.google.devtools.build.lib.packages.SkylarkClassObject) SkylarkKey(com.google.devtools.build.lib.packages.SkylarkClassObjectConstructor.SkylarkKey) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) SkylarkClassObject(com.google.devtools.build.lib.packages.SkylarkClassObject) List(java.util.List) Nullable(javax.annotation.Nullable) Test(org.junit.Test)

Example 15 with SkylarkProviders

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

the class SkylarkIntegrationTest method testOutputGroupsAsDictionary.

@Test
public void testOutputGroupsAsDictionary() throws Exception {
    scratch.file("test/skylark/extension.bzl", "def _impl(ctx):", "  f = ctx.attr.dep.output_groups['_hidden_top_level" + INTERNAL_SUFFIX + "']", "  has_key1 = '_hidden_top_level" + INTERNAL_SUFFIX + "' in ctx.attr.dep.output_groups", "  has_key2 = 'foobar' in ctx.attr.dep.output_groups", "  all_keys = [k for k in ctx.attr.dep.output_groups]", "  return struct(result = f, ", "                has_key1 = has_key1,", "                has_key2 = has_key2,", "                all_keys = all_keys,", "               output_groups = { 'my_group' : f })", "my_rule = rule(implementation = _impl,", "    attrs = { 'dep' : attr.label() })");
    scratch.file("test/skylark/BUILD", "load('/test/skylark/extension',  'my_rule')", "cc_binary(name = 'lib', data = ['a.txt'])", "my_rule(name='my', dep = ':lib')");
    NestedSet<Artifact> hiddenTopLevelArtifacts = getConfiguredTarget("//test/skylark:lib").getProvider(OutputGroupProvider.class).getOutputGroup(OutputGroupProvider.HIDDEN_TOP_LEVEL);
    ConfiguredTarget myTarget = getConfiguredTarget("//test/skylark:my");
    SkylarkProviders skylarkProviders = myTarget.getProvider(SkylarkProviders.class);
    SkylarkNestedSet result = (SkylarkNestedSet) skylarkProviders.getValue("result");
    assertThat(result.getSet(Artifact.class)).containsExactlyElementsIn(hiddenTopLevelArtifacts);
    assertThat(myTarget.getProvider(OutputGroupProvider.class).getOutputGroup("my_group")).containsExactlyElementsIn(hiddenTopLevelArtifacts);
    assertThat(skylarkProviders.getValue("has_key1")).isEqualTo(Boolean.TRUE);
    assertThat(skylarkProviders.getValue("has_key2")).isEqualTo(Boolean.FALSE);
    assertThat((SkylarkList) skylarkProviders.getValue("all_keys")).containsExactly("_hidden_top_level" + INTERNAL_SUFFIX, "compilation_prerequisites" + INTERNAL_SUFFIX, "files_to_compile" + INTERNAL_SUFFIX, "temp_files" + INTERNAL_SUFFIX);
}
Also used : SkylarkProviders(com.google.devtools.build.lib.analysis.SkylarkProviders) SkylarkList(com.google.devtools.build.lib.syntax.SkylarkList) OutputGroupProvider(com.google.devtools.build.lib.analysis.OutputGroupProvider) RuleConfiguredTarget(com.google.devtools.build.lib.analysis.RuleConfiguredTarget) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) FileConfiguredTarget(com.google.devtools.build.lib.analysis.FileConfiguredTarget) Artifact(com.google.devtools.build.lib.actions.Artifact) SkylarkNestedSet(com.google.devtools.build.lib.syntax.SkylarkNestedSet) Test(org.junit.Test)

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