forked from bazelbuild/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sum.bzl
31 lines (25 loc) · 950 Bytes
/
sum.bzl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
"""Rule with an optional provider.
In this example, rules have a number attribute. Each rule adds its number
with the numbers of its transitive dependencies, and write the result in a
file. This shows how to transfer information from a dependency to its
dependents. Dependencies are not required to provide a number.
"""
NumberInfo = provider(doc = "", fields = ["number"])
def _impl(ctx):
result = ctx.attr.number
for dep in ctx.attr.deps:
if NumberInfo in dep:
result += dep[NumberInfo].number
ctx.actions.write(output = ctx.outputs.out, content = str(result))
# Return the provider with result, visible to other rules.
return [NumberInfo(number = result)]
_sum = rule(
implementation = _impl,
attrs = {
"number": attr.int(default = 1),
"deps": attr.label_list(),
"out": attr.output(),
},
)
def sum(**kwargs):
_sum(out = "{name}.sum".format(**kwargs), **kwargs)