|
15 | 15 |
|
16 | 16 | load("@bazel_skylib//lib:dicts.bzl", "dicts")
|
17 | 17 | load("@bazel_skylib//lib:paths.bzl", "paths")
|
| 18 | +load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo") |
18 | 19 | load("//python/private:reexports.bzl", "BuiltinPyRuntimeInfo")
|
19 | 20 | load("//python/private:util.bzl", "IS_BAZEL_7_OR_HIGHER")
|
20 | 21 | load(":attributes.bzl", "NATIVE_RULES_ALLOWLIST_ATTRS")
|
@@ -80,6 +81,10 @@ def _py_runtime_impl(ctx):
|
80 | 81 | python_version = ctx.attr.python_version
|
81 | 82 |
|
82 | 83 | interpreter_version_info = ctx.attr.interpreter_version_info
|
| 84 | + if not interpreter_version_info: |
| 85 | + python_version_flag = ctx.attr._python_version_flag[BuildSettingInfo].value |
| 86 | + if python_version_flag: |
| 87 | + interpreter_version_info = _interpreter_version_info_from_version_str(python_version_flag) |
83 | 88 |
|
84 | 89 | # TODO: Uncomment this after --incompatible_python_disable_py2 defaults to true
|
85 | 90 | # if ctx.fragments.py.disable_py2 and python_version == "PY2":
|
@@ -133,13 +138,6 @@ def _py_runtime_impl(ctx):
|
133 | 138 | ),
|
134 | 139 | ]
|
135 | 140 |
|
136 |
| -def _is_singleton_depset(files): |
137 |
| - # Bazel 6 doesn't have this helper to optimize detecting singleton depsets. |
138 |
| - if _py_builtins: |
139 |
| - return _py_builtins.is_singleton_depset(files) |
140 |
| - else: |
141 |
| - return len(files.to_list()) == 1 |
142 |
| - |
143 | 141 | # Bind to the name "py_runtime" to preserve the kind/rule_class it shows up
|
144 | 142 | # as elsewhere.
|
145 | 143 | py_runtime = rule(
|
@@ -260,15 +258,22 @@ the target platform. For an in-build runtime this attribute must not be set.
|
260 | 258 | """),
|
261 | 259 | "interpreter_version_info": attr.string_dict(
|
262 | 260 | doc = """
|
263 |
| -Version information about the interpreter this runtime provides. The |
264 |
| -supported keys match the names for `sys.version_info`. While the input |
| 261 | +Version information about the interpreter this runtime provides. |
| 262 | +
|
| 263 | +If not specified, uses {obj}`--python_version` |
| 264 | +
|
| 265 | +The supported keys match the names for `sys.version_info`. While the input |
265 | 266 | values are strings, most are converted to ints. The supported keys are:
|
266 | 267 | * major: int, the major version number
|
267 | 268 | * minor: int, the minor version number
|
268 | 269 | * micro: optional int, the micro version number
|
269 | 270 | * releaselevel: optional str, the release level
|
270 |
| - * serial: optional int, the serial number of the release" |
271 |
| - """, |
| 271 | + * serial: optional int, the serial number of the release |
| 272 | +
|
| 273 | +:::{versionchanged} 0.36.0 |
| 274 | +{obj}`--python_version` determines the default value. |
| 275 | +::: |
| 276 | +""", |
272 | 277 | mandatory = False,
|
273 | 278 | ),
|
274 | 279 | "pyc_tag": attr.string(
|
@@ -327,5 +332,25 @@ The {obj}`PyRuntimeInfo.zip_main_template` field.
|
327 | 332 | :::
|
328 | 333 | """,
|
329 | 334 | ),
|
| 335 | + "_python_version_flag": attr.label( |
| 336 | + default = "//python/config_settings:python_version", |
| 337 | + ), |
330 | 338 | }),
|
331 | 339 | )
|
| 340 | + |
| 341 | +def _is_singleton_depset(files): |
| 342 | + # Bazel 6 doesn't have this helper to optimize detecting singleton depsets. |
| 343 | + if _py_builtins: |
| 344 | + return _py_builtins.is_singleton_depset(files) |
| 345 | + else: |
| 346 | + return len(files.to_list()) == 1 |
| 347 | + |
| 348 | +def _interpreter_version_info_from_version_str(version_str): |
| 349 | + parts = version_str.split(".") |
| 350 | + version_info = {} |
| 351 | + for key in ("major", "minor", "micro"): |
| 352 | + if not parts: |
| 353 | + break |
| 354 | + version_info[key] = parts.pop(0) |
| 355 | + |
| 356 | + return version_info |
0 commit comments