Skip to content

Commit 1c5b92b

Browse files
stonierrickeylev
andauthored
feat: make variable substitution for py_wheel abi, python_tag args (#1113)
Expands make variables in to `abi` and `python_tag` attributes --------- Co-authored-by: Richard Levasseur <richardlev@gmail.com>
1 parent 5ff514a commit 1c5b92b

File tree

3 files changed

+48
-5
lines changed

3 files changed

+48
-5
lines changed

examples/wheel/BUILD.bazel

Lines changed: 24 additions & 1 deletion
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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,20 @@ directory_writer = rule(
5454
),
5555
},
5656
)
57+
58+
def _make_variable_tags_impl(ctx): # buildifier: disable=unused-variable
59+
# This example is contrived. In a real usage, this rule would
60+
# look at flags or dependencies to determine what values to use.
61+
# If all you're doing is setting constant values, then you can simply
62+
# set them in the py_wheel() call.
63+
vars = {}
64+
vars["ABI"] = "cp38"
65+
vars["PYTHON_TAG"] = "cp38"
66+
vars["VERSION"] = "0.99.0"
67+
return [platform_common.TemplateVariableInfo(vars)]
68+
69+
make_variable_tags = rule(
70+
attrs = {},
71+
doc = """Make variable tags to pass to a py_wheel rule.""",
72+
implementation = _make_variable_tags_impl,
73+
)

python/private/py_wheel.bzl

Lines changed: 7 additions & 4 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy