Skip to content

Commit d983499

Browse files
committed
[Bazel] Make td_library usable as data
This patch makes it possible to list a td_library as a rule's data attribute and get its source files and all its transitive dependencies at runtime. This is useful for, e.g. shell tests running tblgen. Note that this is a bit different from how a "normal" (e.g. C++) library rule would work because those have actual library outputs and the td_library rule just bundles some source files and includes. If someone wanted to make use of the includes, they would have to access the TdInfo provider, but this keeps simple things simple. Reviewed By: jpienaar Differential Revision: https://reviews.llvm.org/D106922
1 parent f3a8a7b commit d983499

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

utils/bazel/llvm-project-overlay/mlir/tblgen.bzl

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,21 @@ def _td_library_impl(ctx):
100100
_resolve_includes(ctx, ctx.attr.includes),
101101
ctx.attr.deps,
102102
)
103+
104+
# Note that we include srcs in runfiles. A td_library doesn't compile to
105+
# produce an output: it's just a depset of source files and include
106+
# directories. So if it is needed for execution of some rule (likely
107+
# something running tblgen as a test action), the files needed are the same
108+
# as the source files.
109+
# Note: not using merge_all, as that is not available in Bazel 4.0
110+
runfiles = ctx.runfiles(ctx.files.srcs)
111+
for src in ctx.attr.srcs:
112+
runfiles = runfiles.merge(src[DefaultInfo].default_runfiles)
113+
for dep in ctx.attr.deps:
114+
runfiles = runfiles.merge(dep[DefaultInfo].default_runfiles)
115+
103116
return [
104-
DefaultInfo(files = trans_srcs),
117+
DefaultInfo(files = trans_srcs, runfiles = runfiles),
105118
TdInfo(
106119
transitive_sources = trans_srcs,
107120
transitive_includes = trans_includes,
@@ -224,11 +237,6 @@ gentbl_rule = rule(
224237
def _gentbl_test_impl(ctx):
225238
td_file = ctx.file.td_file
226239

227-
trans_srcs = _get_transitive_srcs(
228-
ctx.files.td_srcs + [td_file],
229-
ctx.attr.deps,
230-
)
231-
232240
# Note that we have two types of includes here. The deprecated ones expanded
233241
# only by "_prefix_roots" are already relative to the execution root, i.e.
234242
# may contain an `external/<workspace_name>` prefix if the current workspace
@@ -256,18 +264,26 @@ def _gentbl_test_impl(ctx):
256264
is_executable = True,
257265
)
258266

267+
# Note: not using merge_all, as that is not available in Bazel 4.0
268+
runfiles = ctx.runfiles(
269+
files = [ctx.executable.tblgen],
270+
transitive_files = _get_transitive_srcs(
271+
ctx.files.td_srcs + [td_file],
272+
ctx.attr.deps,
273+
),
274+
)
275+
for src in ctx.attr.td_srcs:
276+
runfiles = runfiles.merge(src[DefaultInfo].default_runfiles)
277+
for dep in ctx.attr.deps:
278+
runfiles = runfiles.merge(dep[DefaultInfo].default_runfiles)
279+
259280
return [
260281
coverage_common.instrumented_files_info(
261282
ctx,
262283
source_attributes = ["td_file", "td_srcs"],
263284
dependency_attributes = ["tblgen", "deps"],
264285
),
265-
DefaultInfo(
266-
runfiles = ctx.runfiles(
267-
[ctx.executable.tblgen],
268-
transitive_files = trans_srcs,
269-
),
270-
),
286+
DefaultInfo(runfiles = runfiles),
271287
]
272288

273289
gentbl_test = rule(

0 commit comments

Comments
 (0)