Skip to content

Commit 72e8f87

Browse files
committed
feat: ad PyRuntimeInfo.abi_flags
1 parent daed352 commit 72e8f87

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ A brief description of the categories of changes:
5151
* (toolchain) Support for freethreaded Python toolchains is now available. Use
5252
the config flag `//python/config_settings:py_freethreaded` to toggle the
5353
selection of the free-threaded toolchains.
54+
* (toolchain) {obj}`py_runtime.abi_flags` attribute and
55+
{obj}`PyRuntimeInfo.abi_flags` field added.
5456

5557
{#v0-0-0-removed}
5658
### Removed

python/private/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,7 @@ bzl_library(
475475
srcs = ["py_runtime_rule.bzl"],
476476
deps = [
477477
":attributes_bzl",
478+
":flags_bzl",
478479
":py_internal_bzl",
479480
":py_runtime_info_bzl",
480481
":reexports_bzl",

python/private/py_runtime_info.bzl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ def _PyRuntimeInfo_init(
6767
bootstrap_template = None,
6868
interpreter_version_info = None,
6969
stage2_bootstrap_template = None,
70-
zip_main_template = None):
70+
zip_main_template = None,
71+
abi_flags = ""):
7172
if (interpreter_path and interpreter) or (not interpreter_path and not interpreter):
7273
fail("exactly one of interpreter or interpreter_path must be specified")
7374

@@ -105,6 +106,7 @@ def _PyRuntimeInfo_init(
105106
stub_shebang = DEFAULT_STUB_SHEBANG
106107

107108
return {
109+
"abi_flags": abi_flags,
108110
"bootstrap_template": bootstrap_template,
109111
"coverage_files": coverage_files,
110112
"coverage_tool": coverage_tool,
@@ -133,6 +135,11 @@ the same conventions as the standard CPython interpreter.
133135
""",
134136
init = _PyRuntimeInfo_init,
135137
fields = {
138+
"abi_flags": """
139+
:type: str
140+
141+
The runtime's ABI flags, i.e. `sys.abiflags`.
142+
""",
136143
"bootstrap_template": """
137144
:type: File
138145

python/private/py_runtime_rule.bzl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ load("@bazel_skylib//lib:dicts.bzl", "dicts")
1717
load("@bazel_skylib//lib:paths.bzl", "paths")
1818
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
1919
load(":attributes.bzl", "NATIVE_RULES_ALLOWLIST_ATTRS")
20+
load(":flags.bzl", "FreeThreadedFlag")
2021
load(":py_internal.bzl", "py_internal")
2122
load(":py_runtime_info.bzl", "DEFAULT_BOOTSTRAP_TEMPLATE", "DEFAULT_STUB_SHEBANG", "PyRuntimeInfo")
2223
load(":reexports.bzl", "BuiltinPyRuntimeInfo")
@@ -101,6 +102,13 @@ def _py_runtime_impl(ctx):
101102
interpreter_version_info["minor"],
102103
)
103104

105+
abi_flags = ctx.attr.abi_flags
106+
if abi_flags == "<AUTO>":
107+
abi_flags = ""
108+
if ctx.attr._py_freethreaded_flag[BuildSettingInfo].value == FreeThreadedFlag.YES:
109+
abi_flags += "t"
110+
111+
# Args common to both BuiltinPyRuntimeInfo and PyRuntimeInfo
104112
py_runtime_info_kwargs = dict(
105113
interpreter_path = interpreter_path or None,
106114
interpreter = interpreter,
@@ -120,6 +128,7 @@ def _py_runtime_impl(ctx):
120128
pyc_tag = pyc_tag,
121129
stage2_bootstrap_template = ctx.file.stage2_bootstrap_template,
122130
zip_main_template = ctx.file.zip_main_template,
131+
abi_flags = abi_flags,
123132
))
124133

125134
if not IS_BAZEL_7_OR_HIGHER:
@@ -179,6 +188,14 @@ py_runtime(
179188
""",
180189
fragments = ["py"],
181190
attrs = dicts.add(NATIVE_RULES_ALLOWLIST_ATTRS, {
191+
"abi_flags": attr.string(
192+
default = "<AUTO>",
193+
doc = """
194+
The runtime's ABI flags, i.e. `sys.abiflags`.
195+
196+
If not set, then it will be set based on flags.
197+
""",
198+
),
182199
"bootstrap_template": attr.label(
183200
allow_single_file = True,
184201
default = DEFAULT_BOOTSTRAP_TEMPLATE,
@@ -335,6 +352,9 @@ The {obj}`PyRuntimeInfo.zip_main_template` field.
335352
:::
336353
""",
337354
),
355+
"_py_freethreaded_flag": attr.label(
356+
default = "//python/config_settings:py_freethreaded",
357+
),
338358
"_python_version_flag": attr.label(
339359
default = "//python/config_settings:python_version",
340360
),

0 commit comments

Comments
 (0)