Skip to content

Commit 0d12bd9

Browse files
stonierDaniel Stonier
authored and
Daniel Stonier
committed
feat: make variable substitution for py_wheel abi, python_tag args
1 parent 244c606 commit 0d12bd9

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

examples/wheel/BUILD.bazel

+24-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414

1515
load("@bazel_skylib//rules:build_test.bzl", "build_test")
16-
load("//examples/wheel/private:wheel_utils.bzl", "directory_writer")
16+
load("//examples/wheel/private:wheel_utils.bzl", "directory_writer", "make_variable_tags")
1717
load("//python:defs.bzl", "py_library", "py_test")
1818
load("//python:packaging.bzl", "py_package", "py_wheel")
1919
load("//python:versions.bzl", "gen_python_config_settings")
@@ -62,6 +62,29 @@ py_wheel(
6262
],
6363
)
6464

65+
# Populate a rule with "Make Variable" arguments for
66+
# abi, python_tag and version. You might want to do this
67+
# for the following use cases:
68+
# - abi, python_tag: introspect a toolchain to map to appropriate cpython tags
69+
# - version: populate given this or a dependent module's version
70+
make_variable_tags(
71+
name = "make_variable_tags",
72+
)
73+
74+
py_wheel(
75+
name = "minimal_with_py_library_with_make_variables",
76+
testonly = True,
77+
abi = "$(ABI)",
78+
distribution = "example_minimal_library",
79+
python_tag = "$(PYTHON_TAG)",
80+
toolchains = ["//examples/wheel:make_variable_tags"],
81+
version = "$(VERSION)",
82+
deps = [
83+
"//examples/wheel/lib:module_with_data",
84+
"//examples/wheel/lib:simple_module",
85+
],
86+
)
87+
6588
build_test(
6689
name = "dist_build_tests",
6790
targets = [":minimal_with_py_library.dist"],

examples/wheel/private/wheel_utils.bzl

+13
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,16 @@ directory_writer = rule(
5454
),
5555
},
5656
)
57+
58+
def _make_variable_tags_impl(ctx): # buildifier: disable=unused-variable
59+
vars = {}
60+
vars["ABI"] = "cp38"
61+
vars["PYTHON_TAG"] = "cp38"
62+
vars["VERSION"] = "0.99.0"
63+
return [platform_common.TemplateVariableInfo(vars)]
64+
65+
make_variable_tags = rule(
66+
attrs = {},
67+
doc = """Make variable tags to pass to a py_wheel rule.""",
68+
implementation = _make_variable_tags_impl,
69+
)

python/private/py_wheel.bzl

+7-4
Original file line numberDiff line numberDiff line change
@@ -207,12 +207,15 @@ def _input_file_to_arg(input_file):
207207
return "%s;%s" % (py_package_lib.path_inside_wheel(input_file), input_file.path)
208208

209209
def _py_wheel_impl(ctx):
210+
abi = _replace_make_variables(ctx.attr.abi, ctx)
211+
python_tag = _replace_make_variables(ctx.attr.python_tag, ctx)
210212
version = _replace_make_variables(ctx.attr.version, ctx)
213+
211214
outfile = ctx.actions.declare_file("-".join([
212215
_escape_filename_segment(ctx.attr.distribution),
213216
_escape_filename_segment(version),
214-
_escape_filename_segment(ctx.attr.python_tag),
215-
_escape_filename_segment(ctx.attr.abi),
217+
_escape_filename_segment(python_tag),
218+
_escape_filename_segment(abi),
216219
_escape_filename_segment(ctx.attr.platform),
217220
]) + ".whl")
218221

@@ -237,8 +240,8 @@ def _py_wheel_impl(ctx):
237240
args = ctx.actions.args()
238241
args.add("--name", ctx.attr.distribution)
239242
args.add("--version", version)
240-
args.add("--python_tag", ctx.attr.python_tag)
241-
args.add("--abi", ctx.attr.abi)
243+
args.add("--python_tag", python_tag)
244+
args.add("--abi", abi)
242245
args.add("--platform", ctx.attr.platform)
243246
args.add("--out", outfile)
244247
args.add("--name_file", name_file)

0 commit comments

Comments
 (0)