Skip to content

Commit

Permalink
feat: add attrs/fields for the runtime's ABI flags (#2390)
Browse files Browse the repository at this point in the history
This adds attributes and fields for storing the runtimes ABI flags
value, i.e. `sys.abiflags`.

For freethreaded interpreters, the abi flags contain `t`, which is used
creating e.g.
virtualenvs.

The attribute can be directly specified, or if it's not, will be
computed based on the
`--py_freethreaded` flag.
  • Loading branch information
rickeylev authored Nov 11, 2024
1 parent daed352 commit 4e61031
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ A brief description of the categories of changes:
* (toolchain) Support for freethreaded Python toolchains is now available. Use
the config flag `//python/config_settings:py_freethreaded` to toggle the
selection of the free-threaded toolchains.
* (toolchain) {obj}`py_runtime.abi_flags` attribute and
{obj}`PyRuntimeInfo.abi_flags` field added.

{#v0-0-0-removed}
### Removed
Expand Down
1 change: 1 addition & 0 deletions python/private/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ bzl_library(
srcs = ["py_runtime_rule.bzl"],
deps = [
":attributes_bzl",
":flags_bzl",
":py_internal_bzl",
":py_runtime_info_bzl",
":reexports_bzl",
Expand Down
9 changes: 8 additions & 1 deletion python/private/py_runtime_info.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ def _PyRuntimeInfo_init(
bootstrap_template = None,
interpreter_version_info = None,
stage2_bootstrap_template = None,
zip_main_template = None):
zip_main_template = None,
abi_flags = ""):
if (interpreter_path and interpreter) or (not interpreter_path and not interpreter):
fail("exactly one of interpreter or interpreter_path must be specified")

Expand Down Expand Up @@ -105,6 +106,7 @@ def _PyRuntimeInfo_init(
stub_shebang = DEFAULT_STUB_SHEBANG

return {
"abi_flags": abi_flags,
"bootstrap_template": bootstrap_template,
"coverage_files": coverage_files,
"coverage_tool": coverage_tool,
Expand Down Expand Up @@ -133,6 +135,11 @@ the same conventions as the standard CPython interpreter.
""",
init = _PyRuntimeInfo_init,
fields = {
"abi_flags": """
:type: str
The runtime's ABI flags, i.e. `sys.abiflags`.
""",
"bootstrap_template": """
:type: File
Expand Down
20 changes: 20 additions & 0 deletions python/private/py_runtime_rule.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ load("@bazel_skylib//lib:dicts.bzl", "dicts")
load("@bazel_skylib//lib:paths.bzl", "paths")
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
load(":attributes.bzl", "NATIVE_RULES_ALLOWLIST_ATTRS")
load(":flags.bzl", "FreeThreadedFlag")
load(":py_internal.bzl", "py_internal")
load(":py_runtime_info.bzl", "DEFAULT_BOOTSTRAP_TEMPLATE", "DEFAULT_STUB_SHEBANG", "PyRuntimeInfo")
load(":reexports.bzl", "BuiltinPyRuntimeInfo")
Expand Down Expand Up @@ -101,6 +102,13 @@ def _py_runtime_impl(ctx):
interpreter_version_info["minor"],
)

abi_flags = ctx.attr.abi_flags
if abi_flags == "<AUTO>":
abi_flags = ""
if ctx.attr._py_freethreaded_flag[BuildSettingInfo].value == FreeThreadedFlag.YES:
abi_flags += "t"

# Args common to both BuiltinPyRuntimeInfo and PyRuntimeInfo
py_runtime_info_kwargs = dict(
interpreter_path = interpreter_path or None,
interpreter = interpreter,
Expand All @@ -120,6 +128,7 @@ def _py_runtime_impl(ctx):
pyc_tag = pyc_tag,
stage2_bootstrap_template = ctx.file.stage2_bootstrap_template,
zip_main_template = ctx.file.zip_main_template,
abi_flags = abi_flags,
))

if not IS_BAZEL_7_OR_HIGHER:
Expand Down Expand Up @@ -179,6 +188,14 @@ py_runtime(
""",
fragments = ["py"],
attrs = dicts.add(NATIVE_RULES_ALLOWLIST_ATTRS, {
"abi_flags": attr.string(
default = "<AUTO>",
doc = """
The runtime's ABI flags, i.e. `sys.abiflags`.
If not set, then it will be set based on flags.
""",
),
"bootstrap_template": attr.label(
allow_single_file = True,
default = DEFAULT_BOOTSTRAP_TEMPLATE,
Expand Down Expand Up @@ -335,6 +352,9 @@ The {obj}`PyRuntimeInfo.zip_main_template` field.
:::
""",
),
"_py_freethreaded_flag": attr.label(
default = "//python/config_settings:py_freethreaded",
),
"_python_version_flag": attr.label(
default = "//python/config_settings:python_version",
),
Expand Down

0 comments on commit 4e61031

Please sign in to comment.