Skip to content

Commit d944d5b

Browse files
Add macros for unpacking wheels and using the wheels content in py_library.
PiperOrigin-RevId: 688169758
1 parent 747d2cf commit d944d5b

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

third_party/xla/third_party/tsl/opensource_only.files

+1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ third_party/py/python_init_repositories.bzl:
108108
third_party/py/python_init_rules.bzl:
109109
third_party/py/python_init_toolchains.bzl:
110110
third_party/py/python_repo.bzl:
111+
third_party/py/python_wheel_library.bzl:
111112
third_party/pybind11.BUILD:
112113
third_party/pybind11_bazel/BUILD:
113114
third_party/python_runtime/BUILD:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
""" Macros to unpack a wheel and use its content as a py_library. """
2+
3+
def _unpacked_wheel_impl(ctx):
4+
output_dir = ctx.actions.declare_directory(ctx.label.name)
5+
ctx.actions.run(
6+
inputs = [ctx.file.wheel],
7+
outputs = [output_dir],
8+
executable = ctx.executable.zipper,
9+
arguments = ["x", ctx.file.wheel.path, "-d", output_dir.path],
10+
)
11+
return [
12+
DefaultInfo(files = depset([output_dir])),
13+
]
14+
15+
_unpacked_wheel = rule(
16+
implementation = _unpacked_wheel_impl,
17+
attrs = {
18+
"wheel": attr.label(mandatory = True, allow_single_file = True),
19+
"zipper": attr.label(
20+
default = Label("@bazel_tools//tools/zip:zipper"),
21+
cfg = "exec",
22+
executable = True,
23+
),
24+
},
25+
)
26+
27+
def wheel_library(name, wheel, deps):
28+
unpacked_wheel_name = name + "_unpacked_wheel"
29+
_unpacked_wheel(
30+
name = unpacked_wheel_name,
31+
wheel = wheel,
32+
)
33+
native.py_library(
34+
name = name,
35+
data = [":" + unpacked_wheel_name],
36+
imports = [unpacked_wheel_name],
37+
deps = deps,
38+
visibility = ["//visibility:public"],
39+
)

0 commit comments

Comments
 (0)