forked from bazelbuild/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfoo.bzl
62 lines (55 loc) · 1.7 KB
/
foo.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# buildifier: disable=module-docstring
FooFilesInfo = provider(doc = "", fields = ["transitive_sources"])
def get_transitive_srcs(srcs, deps):
"""Obtain the source files for a target and its transitive dependencies.
Args:
srcs: a list of source files
deps: a list of targets that are direct dependencies
Returns:
a collection of the transitive sources
"""
return depset(
srcs,
transitive = [dep[FooFilesInfo].transitive_sources for dep in deps],
)
def _foo_library_impl(ctx):
trans_srcs = get_transitive_srcs(ctx.files.srcs, ctx.attr.deps)
return [
FooFilesInfo(transitive_sources = trans_srcs),
DefaultInfo(files = trans_srcs),
]
foo_library = rule(
implementation = _foo_library_impl,
attrs = {
"srcs": attr.label_list(allow_files = True),
"deps": attr.label_list(),
},
)
def _foo_binary_impl(ctx):
foocc = ctx.executable._foocc
out = ctx.outputs.out
trans_srcs = get_transitive_srcs(ctx.files.srcs, ctx.attr.deps)
srcs_list = trans_srcs.to_list()
ctx.actions.run(
executable = foocc,
arguments = [out.path] + [src.path for src in srcs_list],
inputs = srcs_list,
tools = [foocc],
outputs = [out],
)
_foo_binary = rule(
implementation = _foo_binary_impl,
attrs = {
"srcs": attr.label_list(allow_files = True),
"deps": attr.label_list(),
"_foocc": attr.label(
default = ":foocc",
allow_files = True,
executable = True,
cfg = "exec",
),
"out": attr.output(),
},
)
def foo_binary(**kwargs):
_foo_binary(out = "{name}.out".format(**kwargs), **kwargs)