Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add attrs/fields for the runtime's ABI flags #2390

Merged
merged 1 commit into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Collaborator

@aignas aignas Nov 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about doing

diff --git a/python/private/hermetic_runtime_repo_setup.bzl b/python/private/hermetic_runtime_repo_setup.bzl
index 3f7bb5d7..207d021b 100644
--- a/python/private/hermetic_runtime_repo_setup.bzl
+++ b/python/private/hermetic_runtime_repo_setup.bzl
@@ -198,6 +198,10 @@ def define_hermetic_runtime_toolchain_impl(
             _IS_FREETHREADED: "cpython-{major}{minor}t".format(**version_dict),
             "//conditions:default": "cpython-{major}{minor}".format(**version_dict),
         }),
+        abi_info = select({
+            _IS_FREETHREADED: "t",
+            "//conditions:default": "",
+        }),
     )

     py_runtime_pair(

Instead of depending on the _py_freethreaded_flag implementation?

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