Search in sources :

Example 6 with SkylarkList

use of com.google.devtools.build.lib.syntax.SkylarkList in project bazel by bazelbuild.

the class SkylarkAspectsTest method aspectOnAspectLinear.

/**
   * Simple straightforward linear aspects-on-aspects.
   */
@Test
public void aspectOnAspectLinear() throws Exception {
    scratch.file("test/aspect.bzl", "a1p = provider()", "def _a1_impl(target,ctx):", "  return struct(a1p = a1p(text = 'random'))", "a1 = aspect(_a1_impl, attr_aspects = ['dep'], provides = ['a1p'])", "a2p = provider()", "def _a2_impl(target,ctx):", "  value = []", "  if hasattr(ctx.rule.attr.dep, 'a2p'):", "     value += ctx.rule.attr.dep.a2p.value", "  if hasattr(target, 'a1p'):", "     value.append(str(target.label) + str(ctx.aspect_ids) + '=yes')", "  else:", "     value.append(str(target.label) + str(ctx.aspect_ids) + '=no')", "  return struct(a2p = a2p(value = value))", "a2 = aspect(_a2_impl, attr_aspects = ['dep'], required_aspect_providers = ['a1p'])", "def _r1_impl(ctx):", "  pass", "def _r2_impl(ctx):", "  return struct(result = ctx.attr.dep.a2p.value)", "r1 = rule(_r1_impl, attrs = { 'dep' : attr.label(aspects = [a1])})", "r2 = rule(_r2_impl, attrs = { 'dep' : attr.label(aspects = [a2])})");
    scratch.file("test/BUILD", "load(':aspect.bzl', 'r1', 'r2')", "r1(name = 'r0')", "r1(name = 'r1', dep = ':r0')", "r2(name = 'r2', dep = ':r1')");
    AnalysisResult analysisResult = update("//test:r2");
    ConfiguredTarget target = Iterables.getOnlyElement(analysisResult.getTargetsToBuild());
    SkylarkList result = (SkylarkList) target.get("result");
    // "yes" means that aspect a2 sees a1's providers.
    assertThat(result).containsExactly("//test:r0[\"//test:aspect.bzl%a1\", \"//test:aspect.bzl%a2\"]=yes", "//test:r1[\"//test:aspect.bzl%a2\"]=no");
}
Also used : SkylarkList(com.google.devtools.build.lib.syntax.SkylarkList) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) AnalysisResult(com.google.devtools.build.lib.analysis.BuildView.AnalysisResult) Test(org.junit.Test)

Example 7 with SkylarkList

use of com.google.devtools.build.lib.syntax.SkylarkList in project bazel by bazelbuild.

the class SkylarkAspectsTest method aspectDescriptions.

@Test
public void aspectDescriptions() throws Exception {
    scratch.file("test/aspect.bzl", "def _a_impl(target,ctx):", "  s = str(target.label) + str(ctx.aspect_ids) + '='", "  value = []", "  if ctx.rule.attr.dep:", "     d = ctx.rule.attr.dep", "     this_id = ctx.aspect_ids[len(ctx.aspect_ids) - 1]", "     s += str(d.label) + str(d.my_ids) + ',' + str(this_id in d.my_ids)", "     value += ctx.rule.attr.dep.ap", "  else:", "     s += 'None'", "  value.append(s)", "  return struct(ap = value, my_ids = ctx.aspect_ids)", "a = aspect(_a_impl, attr_aspects = ['dep'])", "def _r_impl(ctx):", "  if not ctx.attr.dep:", "     return struct(result = [])", "  return struct(result = ctx.attr.dep.ap)", "r = rule(_r_impl, attrs = { 'dep' : attr.label(aspects = [a])})");
    scratch.file("test/BUILD", "load(':aspect.bzl', 'r')", "r(name = 'r0')", "r(name = 'r1', dep = ':r0')", "r(name = 'r2', dep = ':r1')");
    AnalysisResult analysisResult = update("//test:r2");
    ConfiguredTarget target = Iterables.getOnlyElement(analysisResult.getTargetsToBuild());
    SkylarkList<?> result = (SkylarkList<?>) target.get("result");
    assertThat(result).containsExactly("//test:r0[\"//test:aspect.bzl%a\"]=None", "//test:r1[\"//test:aspect.bzl%a\"]=//test:r0[\"//test:aspect.bzl%a\"],True");
}
Also used : SkylarkList(com.google.devtools.build.lib.syntax.SkylarkList) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) AnalysisResult(com.google.devtools.build.lib.analysis.BuildView.AnalysisResult) Test(org.junit.Test)

Example 8 with SkylarkList

use of com.google.devtools.build.lib.syntax.SkylarkList in project bazel by bazelbuild.

the class SkylarkAspectsTest method aspectOnAspectLinearAlias.

/**
   * Linear aspects-on-aspects with alias rule.
   */
@Test
public void aspectOnAspectLinearAlias() throws Exception {
    scratch.file("test/aspect.bzl", "a1p = provider()", "def _a1_impl(target,ctx):", "  return struct(a1p = a1p(text = 'random'))", "a1 = aspect(_a1_impl, attr_aspects = ['dep'], provides = ['a1p'])", "a2p = provider()", "def _a2_impl(target,ctx):", "  value = []", "  if hasattr(ctx.rule.attr.dep, 'a2p'):", "     value += ctx.rule.attr.dep.a2p.value", "  if hasattr(target, 'a1p'):", "     value.append(str(target.label) + str(ctx.aspect_ids) + '=yes')", "  else:", "     value.append(str(target.label) + str(ctx.aspect_ids) + '=no')", "  return struct(a2p = a2p(value = value))", "a2 = aspect(_a2_impl, attr_aspects = ['dep'], required_aspect_providers = ['a1p'])", "def _r1_impl(ctx):", "  pass", "def _r2_impl(ctx):", "  return struct(result = ctx.attr.dep.a2p.value)", "r1 = rule(_r1_impl, attrs = { 'dep' : attr.label(aspects = [a1])})", "r2 = rule(_r2_impl, attrs = { 'dep' : attr.label(aspects = [a2])})");
    scratch.file("test/BUILD", "load(':aspect.bzl', 'r1', 'r2')", "r1(name = 'r0')", "alias(name = 'a0', actual = ':r0')", "r1(name = 'r1', dep = ':a0')", "r2(name = 'r2', dep = ':r1')");
    AnalysisResult analysisResult = update("//test:r2");
    ConfiguredTarget target = Iterables.getOnlyElement(analysisResult.getTargetsToBuild());
    SkylarkList<?> result = (SkylarkList<?>) target.get("result");
    // "yes" means that aspect a2 sees a1's providers.
    assertThat(result).containsExactly("//test:r0[\"//test:aspect.bzl%a1\", \"//test:aspect.bzl%a2\"]=yes", "//test:r1[\"//test:aspect.bzl%a2\"]=no");
}
Also used : SkylarkList(com.google.devtools.build.lib.syntax.SkylarkList) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) AnalysisResult(com.google.devtools.build.lib.analysis.BuildView.AnalysisResult) Test(org.junit.Test)

Example 9 with SkylarkList

use of com.google.devtools.build.lib.syntax.SkylarkList in project bazel by bazelbuild.

the class SkylarkAspectsTest method aspectOnAspectLinearDuplicates.

/**
   * Linear with duplicates.
   * r2_1 depends on r0 with aspect a2.
   * r1 depends on r2_1 with aspect a1.
   * r2 depends on r1 with aspect a2.
   *
   * a2 is not interested in a1.
   * There should be just one instance of aspect a2 on r0, and is should *not* see a1.
   */
@Test
public void aspectOnAspectLinearDuplicates() throws Exception {
    scratch.file("test/aspect.bzl", "a1p = provider()", "def _a1_impl(target,ctx):", "  return struct(a1p = 'a1p')", "a1 = aspect(_a1_impl, attr_aspects = ['dep'], provides = ['a1p'])", "a2p = provider()", "def _a2_impl(target,ctx):", "  value = []", "  if hasattr(ctx.rule.attr.dep, 'a2p'):", "     value += ctx.rule.attr.dep.a2p.value", "  if hasattr(target, 'a1p'):", "     value.append(str(target.label) + str(ctx.aspect_ids) + '=yes')", "  else:", "     value.append(str(target.label) + str(ctx.aspect_ids) + '=no')", "  return struct(a2p = a2p(value = value))", "a2 = aspect(_a2_impl, attr_aspects = ['dep'], required_aspect_providers = [])", "def _r1_impl(ctx):", "  pass", "def _r2_impl(ctx):", "  return struct(result = ctx.attr.dep.a2p.value)", "r1 = rule(_r1_impl, attrs = { 'dep' : attr.label(aspects = [a1])})", "r2 = rule(_r2_impl, attrs = { 'dep' : attr.label(aspects = [a2])})");
    scratch.file("test/BUILD", "load(':aspect.bzl', 'r1', 'r2')", "r1(name = 'r0')", "r2(name = 'r2_1', dep = ':r0')", "r1(name = 'r1', dep = ':r2_1')", "r2(name = 'r2', dep = ':r1')");
    AnalysisResult analysisResult = update("//test:r2");
    ConfiguredTarget target = Iterables.getOnlyElement(analysisResult.getTargetsToBuild());
    SkylarkList result = (SkylarkList) target.get("result");
    // "yes" means that aspect a2 sees a1's providers.
    assertThat(result).containsExactly("//test:r0[\"//test:aspect.bzl%a2\"]=no", "//test:r1[\"//test:aspect.bzl%a2\"]=no", "//test:r2_1[\"//test:aspect.bzl%a2\"]=no");
}
Also used : SkylarkList(com.google.devtools.build.lib.syntax.SkylarkList) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) AnalysisResult(com.google.devtools.build.lib.analysis.BuildView.AnalysisResult) Test(org.junit.Test)

Example 10 with SkylarkList

use of com.google.devtools.build.lib.syntax.SkylarkList 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

SkylarkList (com.google.devtools.build.lib.syntax.SkylarkList)12 Test (org.junit.Test)8 ConfiguredTarget (com.google.devtools.build.lib.analysis.ConfiguredTarget)6 AnalysisResult (com.google.devtools.build.lib.analysis.BuildView.AnalysisResult)5 SkylarkClassObject (com.google.devtools.build.lib.packages.SkylarkClassObject)3 Artifact (com.google.devtools.build.lib.actions.Artifact)2 SkylarkRuleContext (com.google.devtools.build.lib.rules.SkylarkRuleContext)2 ClassObject (com.google.devtools.build.lib.syntax.ClassObject)2 EvalException (com.google.devtools.build.lib.syntax.EvalException)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 FileConfiguredTarget (com.google.devtools.build.lib.analysis.FileConfiguredTarget)1 OutputGroupProvider (com.google.devtools.build.lib.analysis.OutputGroupProvider)1 RuleConfiguredTarget (com.google.devtools.build.lib.analysis.RuleConfiguredTarget)1 SkylarkProviders (com.google.devtools.build.lib.analysis.SkylarkProviders)1 Label (com.google.devtools.build.lib.cmdline.Label)1 Attribute (com.google.devtools.build.lib.packages.Attribute)1 AllowedValueSet (com.google.devtools.build.lib.packages.Attribute.AllowedValueSet)1 SkylarkComputedDefaultTemplate (com.google.devtools.build.lib.packages.Attribute.SkylarkComputedDefaultTemplate)1