Skip to content

Commit 29933c0

Browse files
committed
feat: make variable substitution for py_wheel abi, python_tag args
1 parent 244c606 commit 29933c0

File tree

3 files changed

+56
-9
lines changed

3 files changed

+56
-9
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+
version = "$(VERSION)",
81+
deps = [
82+
"//examples/wheel:make_variable_tags",
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

+19-8
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,10 @@ def _escape_filename_segment(segment):
195195
escaped += "_"
196196
return escaped
197197

198-
def _replace_make_variables(flag, ctx):
199-
"""Replace $(VERSION) etc make variables in flag"""
198+
def _replace_make_variables(flag, variable_index):
199+
"""Replace $(XYZ) etc make variables in flag"""
200200
if "$" in flag:
201-
for varname, varsub in ctx.var.items():
201+
for varname, varsub in variable_index.items():
202202
flag = flag.replace("$(%s)" % varname, varsub)
203203
return flag
204204

@@ -207,12 +207,23 @@ 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-
version = _replace_make_variables(ctx.attr.version, ctx)
210+
# Make Variable Substitutions
211+
variable_index = {}
212+
variable_index.update(ctx.var)
213+
for dep in ctx.attr.deps:
214+
if platform_common.TemplateVariableInfo in dep:
215+
variable_index.update(
216+
dep[platform_common.TemplateVariableInfo].variables,
217+
)
218+
abi = _replace_make_variables(ctx.attr.abi, variable_index)
219+
python_tag = _replace_make_variables(ctx.attr.python_tag, variable_index)
220+
version = _replace_make_variables(ctx.attr.version, variable_index)
221+
211222
outfile = ctx.actions.declare_file("-".join([
212223
_escape_filename_segment(ctx.attr.distribution),
213224
_escape_filename_segment(version),
214-
_escape_filename_segment(ctx.attr.python_tag),
215-
_escape_filename_segment(ctx.attr.abi),
225+
_escape_filename_segment(python_tag),
226+
_escape_filename_segment(abi),
216227
_escape_filename_segment(ctx.attr.platform),
217228
]) + ".whl")
218229

@@ -237,8 +248,8 @@ def _py_wheel_impl(ctx):
237248
args = ctx.actions.args()
238249
args.add("--name", ctx.attr.distribution)
239250
args.add("--version", version)
240-
args.add("--python_tag", ctx.attr.python_tag)
241-
args.add("--abi", ctx.attr.abi)
251+
args.add("--python_tag", python_tag)
252+
args.add("--abi", abi)
242253
args.add("--platform", ctx.attr.platform)
243254
args.add("--out", outfile)
244255
args.add("--name_file", name_file)

0 commit comments

Comments
 (0)