-
-
Notifications
You must be signed in to change notification settings - Fork 517
/
file_collector.bzl
63 lines (51 loc) · 1.79 KB
/
file_collector.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
63
"""Example of using an aspect to collect information from dependencies.
For more information about aspects, see the documentation:
https://bazel.build/extending/aspects
"""
CollectedFileInfo = provider(
doc = "",
fields = {"files": "collected files"},
)
def _file_collector_aspect_impl(_, ctx):
# This function is executed for each dependency the aspect visits.
# Collect files from the srcs
direct = [
f
for f in ctx.rule.files.srcs
if ctx.attr.extension == "*" or ctx.attr.extension == f.extension
]
# Combine direct files with the files from the dependencies.
files = depset(
direct = direct,
transitive = [dep[CollectedFileInfo].files for dep in ctx.rule.attr.deps],
)
return [CollectedFileInfo(files = files)]
file_collector_aspect = aspect(
implementation = _file_collector_aspect_impl,
attr_aspects = ["deps"],
attrs = {
"extension": attr.string(values = ["*", "h", "cc"]),
},
)
def _file_collector_rule_impl(ctx):
# This function is executed once per `file_collector`.
# Write the collected information to the output file.
content = []
for dep in ctx.attr.deps:
files = [f.short_path for f in dep[CollectedFileInfo].files.to_list()]
content.append("files from {}: {}".format(dep.label, files))
content.append("") # trailing newline
ctx.actions.write(
output = ctx.outputs.out,
content = "\n".join(content),
)
_file_collector = rule(
implementation = _file_collector_rule_impl,
attrs = {
"deps": attr.label_list(aspects = [file_collector_aspect]),
"extension": attr.string(default = "*"),
"out": attr.output(),
},
)
def file_collector(**kwargs):
_file_collector(out = "{name}.files".format(**kwargs), **kwargs)