use of com.google.devtools.build.lib.analysis.ConfiguredTarget in project bazel by bazelbuild.
the class SkylarkAspectsTest method aspectOnAspectDiamond.
/**
* Diamond case.
* rule r1 depends or r0 with aspect a1.
* rule r2 depends or r0 with aspect a2.
* rule rcollect depends on r1, r2 with aspect a3.
*
* Aspect a3 should be applied twice to target r0: once in [a1, a3] sequence
* and once in [a2, a3] sequence.
*/
@Test
public void aspectOnAspectDiamond() throws Exception {
scratch.file("test/aspect.bzl", "def _a1_impl(target,ctx):", " return struct(a1p = 'text from a1')", "a1 = aspect(_a1_impl, attr_aspects = ['deps'], provides = ['a1p'])", "", "def _a2_impl(target,ctx):", " return struct(a2p = 'text from a2')", "a2 = aspect(_a2_impl, attr_aspects = ['deps'], provides = ['a2p'])", "", "def _a3_impl(target,ctx):", " value = []", " f = ctx.new_file('a3.out')", " ctx.file_action(f, 'text')", " for dep in ctx.rule.attr.deps:", " if hasattr(dep, 'a3p'):", " value += dep.a3p", " s = str(target.label) + str(ctx.aspect_ids) + '='", " if hasattr(target, 'a1p'):", " s += 'a1p'", " if hasattr(target, 'a2p'):", " s += 'a2p'", " value.append(s)", " return struct(a3p = value)", "a3 = aspect(_a3_impl, attr_aspects = ['deps'],", " required_aspect_providers = [['a1p'], ['a2p']])", "def _r1_impl(ctx):", " pass", "def _rcollect_impl(ctx):", " value = []", " for dep in ctx.attr.deps:", " if hasattr(dep, 'a3p'):", " value += dep.a3p", " return struct(result = value)", "r1 = rule(_r1_impl, attrs = { 'deps' : attr.label_list(aspects = [a1])})", "r2 = rule(_r1_impl, attrs = { 'deps' : attr.label_list(aspects = [a2])})", "rcollect = rule(_rcollect_impl, attrs = { 'deps' : attr.label_list(aspects = [a3])})");
scratch.file("test/BUILD", "load(':aspect.bzl', 'r1', 'r2', 'rcollect')", "r1(name = 'r0')", "r1(name = 'r1', deps = [':r0'])", "r2(name = 'r2', deps = [':r0'])", "rcollect(name = 'rcollect', deps = [':r1', ':r2'])");
AnalysisResult analysisResult = update("//test:rcollect");
ConfiguredTarget target = Iterables.getOnlyElement(analysisResult.getTargetsToBuild());
SkylarkList result = (SkylarkList) target.get("result");
assertThat(result).containsExactly("//test:r0[\"//test:aspect.bzl%a1\", \"//test:aspect.bzl%a3\"]=a1p", "//test:r1[\"//test:aspect.bzl%a3\"]=", "//test:r0[\"//test:aspect.bzl%a2\", \"//test:aspect.bzl%a3\"]=a2p", "//test:r2[\"//test:aspect.bzl%a3\"]=");
}
use of com.google.devtools.build.lib.analysis.ConfiguredTarget 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");
}
use of com.google.devtools.build.lib.analysis.ConfiguredTarget in project bazel by bazelbuild.
the class SkylarkIntegrationTest method testFilesToBuild.
@Test
public void testFilesToBuild() throws Exception {
scratch.file("test/skylark/extension.bzl", "def custom_rule_impl(ctx):", " attr1 = ctx.files.attr1", " ftb = depset(attr1)", " return struct(runfiles = ctx.runfiles(), files = ftb)", "", "custom_rule = rule(implementation = custom_rule_impl,", " attrs = {'attr1': attr.label_list(mandatory=True, allow_files=True)})");
scratch.file("test/skylark/BUILD", "load('/test/skylark/extension', 'custom_rule')", "", "custom_rule(name = 'cr', attr1 = [':a.txt'])");
ConfiguredTarget target = getConfiguredTarget("//test/skylark:cr");
assertEquals("//test/skylark:cr", target.getLabel().toString());
assertThat(ActionsTestUtil.baseArtifactNames(target.getProvider(FileProvider.class).getFilesToBuild())).containsExactly("a.txt");
}
use of com.google.devtools.build.lib.analysis.ConfiguredTarget in project bazel by bazelbuild.
the class SkylarkIntegrationTest method testOutputGroups.
@Test
public void testOutputGroups() throws Exception {
scratch.file("test/skylark/extension.bzl", "def _impl(ctx):", " f = ctx.attr.dep.output_group('_hidden_top_level" + INTERNAL_SUFFIX + "')", " return struct(result = f, ", " 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");
SkylarkNestedSet result = (SkylarkNestedSet) myTarget.getProvider(SkylarkProviders.class).getValue("result");
assertThat(result.getSet(Artifact.class)).containsExactlyElementsIn(hiddenTopLevelArtifacts);
assertThat(myTarget.getProvider(OutputGroupProvider.class).getOutputGroup("my_group")).containsExactlyElementsIn(hiddenTopLevelArtifacts);
}
use of com.google.devtools.build.lib.analysis.ConfiguredTarget in project bazel by bazelbuild.
the class SkylarkIntegrationTest method testRuleClassImplicitOutputFunctionAndDefaultValue.
@Test
public void testRuleClassImplicitOutputFunctionAndDefaultValue() throws Exception {
scratch.file("test/skylark/extension.bzl", "def custom_rule_impl(ctx):", " ctx.action(", " outputs = [ctx.outputs.o],", " command = 'echo')", " return struct(runfiles = ctx.runfiles())", "", "def output_func(attr1):", " return {'o': attr1 + '.txt'}", "", "custom_rule = rule(implementation = custom_rule_impl,", " attrs = {'attr1': attr.string(default='bar')},", " outputs = output_func)");
scratch.file("test/skylark/BUILD", "load('/test/skylark/extension', 'custom_rule')", "", "custom_rule(name = 'cr', attr1 = None)");
ConfiguredTarget target = getConfiguredTarget("//test/skylark:cr");
assertThat(ActionsTestUtil.baseArtifactNames(target.getProvider(FileProvider.class).getFilesToBuild())).containsExactly("bar.txt");
}
Aggregations