-
Notifications
You must be signed in to change notification settings - Fork 4
/
expose_all_files.bzl
97 lines (89 loc) · 2.82 KB
/
expose_all_files.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
def _recursive_filegroup_impl(ctx):
files = depset(transitive = [d.data_runfiles.files for d in ctx.attr.data])
return [DefaultInfo(
files = files,
data_runfiles = ctx.runfiles(
files = files.to_list(),
),
)]
"""
Provides all files (including `data` dependencies) at one level such that they
are expandable via `$(locations ...)`.
@param data
Upstream data targets. This will consume both the `srcs` and `data`
portions of an existing `filegroup`.
"""
recursive_filegroup = rule(
attrs = {
"data": attr.label_list(
allow_files = True,
mandatory = True,
),
},
implementation = _recursive_filegroup_impl,
)
# Patterns to be exposed.
_patterns_map = dict(
all_files = [
"*",
],
# To minimize the number of dependencies need to consume `external_data`,
# this list is copied (not imported) from `//tools/lint:bazel_lint.bzl`.
bazel_lint_files = [
"*.bzl",
"*.BUILD",
"*.BUILD.bazel",
"BUILD",
"BUILD.bazel",
"WORKSPACE",
],
python_lint_files = [
"*.py",
],
)
def expose_all_files(
sub_packages = [],
sub_dirs = [],
visibility = ["//visibility:public"]):
"""
Declares files to be consumed externally (for Bazel workspace tests,
linting, etc).
Creates rules "${type}_files" and "${type}_files_recursive", where `type`
will be all of {"all", "bazel_lint", "python_lint"}.
@param sub_packages
Child packages, only the first level.
@param sub_dirs
Any directories that are not packages.
"""
# @note It'd be nice if this could respect *ignore files, but meh.
# Also, it'd be **super** nice if Bazel did not let `**` globs leak into
# other packages and then error out.
package_name = native.package_name()
if package_name:
package_prefix = "//" + package_name + "/"
else:
package_prefix = "//" # Root case.
for name, patterns in _patterns_map.items():
srcs = native.glob(patterns)
for sub_dir in sub_dirs:
srcs += native.glob([
sub_dir + "/**/" + pattern
for pattern in patterns
])
native.filegroup(
name = name,
srcs = srcs,
# Trying to use `data = deps` here only exposes the files in
# runfiles, but not for expansion via `$(locations...)`.
visibility = visibility,
)
# Expose all files recursively (from one level).
deps = [
package_prefix + sub_package + ":" + name + "_recursive"
for sub_package in sub_packages
]
recursive_filegroup(
name = name + "_recursive",
data = [name] + deps,
visibility = visibility,
)