Skip to content

Commit e005b94

Browse files
committed
Added flag_values for free_threading argument to point to correct paths of the headers and the library.
1 parent 30fc3f9 commit e005b94

File tree

5 files changed

+64
-12
lines changed

5 files changed

+64
-12
lines changed

python/config_settings/BUILD.bazel

+10
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,13 @@ string_flag(
170170
define_pypi_internal_flags(
171171
name = "define_pypi_internal_flags",
172172
)
173+
174+
string_flag(
175+
name = "free_threading",
176+
build_setting_default = "no",
177+
values = [
178+
"yes",
179+
"no",
180+
],
181+
visibility = ["//visibility:public"],
182+
)

python/private/hermetic_runtime_repo_setup.bzl

+16-11
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ def define_hermetic_runtime_toolchain_impl(
2727
extra_files_glob_exclude,
2828
python_version,
2929
python_bin,
30-
coverage_tool):
30+
coverage_tool,
31+
free_threading = False):
3132
"""Define a toolchain implementation for a python-build-standalone repo.
3233
3334
It expected this macro is called in the top-level package of an extracted
@@ -44,13 +45,17 @@ def define_hermetic_runtime_toolchain_impl(
4445
python_version: {type}`str` The Python version, in `major.minor.micro`
4546
format.
4647
python_bin: {type}`str` The path to the Python binary within the
47-
repositoroy.
48+
repository.
4849
coverage_tool: {type}`str` optional target to the coverage tool to
4950
use.
51+
free_threading: {type}`bool` optional free-threading support.
52+
Default, False.
5053
"""
5154
_ = name # @unused
5255
version_info = semver(python_version)
5356
version_dict = version_info.to_dict()
57+
version_dict["ft_postfix"] = "t" if free_threading else ""
58+
5459
native.filegroup(
5560
name = "files",
5661
srcs = native.glob(
@@ -67,19 +72,19 @@ def define_hermetic_runtime_toolchain_impl(
6772
"**/* *", # Bazel does not support spaces in file names.
6873
# Unused shared libraries. `python` executable and the `:libpython` target
6974
# depend on `libpython{python_version}.so.1.0`.
70-
"lib/libpython{major}.{minor}.so".format(**version_dict),
75+
"lib/libpython{major}.{minor}{ft_postfix}.so".format(**version_dict),
7176
# static libraries
7277
"lib/**/*.a",
7378
# tests for the standard libraries.
74-
"lib/python{major}.{minor}/**/test/**".format(**version_dict),
75-
"lib/python{major}.{minor}/**/tests/**".format(**version_dict),
79+
"lib/python{major}.{minor}{ft_postfix}/**/test/**".format(**version_dict),
80+
"lib/python{major}.{minor}{ft_postfix}/**/tests/**".format(**version_dict),
7681
"**/__pycache__/*.pyc.*", # During pyc creation, temp files named *.pyc.NNN are created
7782
] + extra_files_glob_exclude,
7883
),
7984
)
8085
cc_import(
8186
name = "interface",
82-
interface_library = "libs/python{major}{minor}.lib".format(**version_dict),
87+
interface_library = "libs/python{major}{minor}{ft_postfix}.lib".format(**version_dict),
8388
system_provided = True,
8489
)
8590

@@ -96,7 +101,7 @@ def define_hermetic_runtime_toolchain_impl(
96101
hdrs = [":includes"],
97102
includes = [
98103
"include",
99-
"include/python{major}.{minor}".format(**version_dict),
104+
"include/python{major}.{minor}{ft_postfix}".format(**version_dict),
100105
"include/python{major}.{minor}m".format(**version_dict),
101106
],
102107
)
@@ -105,11 +110,11 @@ def define_hermetic_runtime_toolchain_impl(
105110
hdrs = [":includes"],
106111
srcs = select({
107112
"@platforms//os:linux": [
108-
"lib/libpython{major}.{minor}.so".format(**version_dict),
109-
"lib/libpython{major}.{minor}.so.1.0".format(**version_dict),
113+
"lib/libpython{major}.{minor}{ft_postfix}.so".format(**version_dict),
114+
"lib/libpython{major}.{minor}{ft_postfix}.so.1.0".format(**version_dict),
110115
],
111-
"@platforms//os:macos": ["lib/libpython{major}.{minor}.dylib".format(**version_dict)],
112-
"@platforms//os:windows": ["python3.dll", "libs/python{major}{minor}.lib".format(**version_dict)],
116+
"@platforms//os:macos": ["lib/libpython{major}.{minor}{ft_postfix}.dylib".format(**version_dict)],
117+
"@platforms//os:windows": ["python3.dll", "libs/python{major}{minor}{ft_postfix}.lib".format(**version_dict)],
113118
}),
114119
)
115120

python/private/python.bzl

+16
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,13 @@ def _process_single_version_overrides(*, tag, _fail = fail, default):
357357
for platform in sha256
358358
}
359359

360+
if tag.flag_values:
361+
# Normalize flag keys to strings
362+
flag_values = {str(k): v for k, v in tag.flag_values.items()}
363+
override["flag_values"] = flag_values
364+
if tag.suffix:
365+
override["suffix"] = tag.suffix
366+
360367
available_versions[tag.python_version] = {k: v for k, v in override.items() if v}
361368

362369
if tag.distutils_content:
@@ -789,6 +796,15 @@ class.
789796
mandatory = False,
790797
doc = "The URL template to fetch releases for this Python version. See {attr}`python.single_version_platform_override.urls` for documentation.",
791798
),
799+
"flag_values": attr.string_dict(
800+
mandatory = False,
801+
doc = "TODO",
802+
),
803+
"suffix": attr.string(
804+
mandatory = False,
805+
doc = "TODO",
806+
default = "",
807+
)
792808
},
793809
)
794810

python/private/python_register_toolchains.bzl

+13
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,18 @@ def python_register_toolchains(
129129
)],
130130
)
131131

132+
flag_values = tool_versions[python_version].get("flag_values", None)
133+
free_threading_label = "@rules_python//python/config_settings:free_threading"
134+
free_threading = False
135+
if flag_values:
136+
free_threading = flag_values.get(free_threading_label, False) == "yes"
137+
138+
# print("python_version:", python_version)
139+
# print("tool_versions:", tool_versions[python_version])
140+
# print("flag_values:", flag_values)
141+
# print("free_threading:", free_threading)
142+
suffix = tool_versions[python_version].get("suffix", "")
143+
132144
python_repository(
133145
name = "{name}_{platform}".format(
134146
name = name,
@@ -143,6 +155,7 @@ def python_register_toolchains(
143155
urls = urls,
144156
strip_prefix = strip_prefix,
145157
coverage_tool = coverage_tool,
158+
free_threading = free_threading,
146159
**kwargs
147160
)
148161
if register_toolchains:

python/private/python_repository.bzl

+9-1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ def _python_repository_impl(rctx):
6767
release_filename = rctx.attr.release_filename
6868
urls = rctx.attr.urls or [rctx.attr.url]
6969
auth = get_auth(rctx, urls)
70+
free_threading = rctx.attr.free_threading
7071

7172
if release_filename.endswith(".zst"):
7273
rctx.download(
@@ -130,7 +131,8 @@ def _python_repository_impl(rctx):
130131
if "windows" in platform:
131132
distutils_path = "Lib/distutils/distutils.cfg"
132133
else:
133-
distutils_path = "lib/python{}/distutils/distutils.cfg".format(python_short_version)
134+
ft_postfix = "t" if free_threading else ""
135+
distutils_path = "lib/python{}{}/distutils/distutils.cfg".format(python_short_version, ft_postfix)
134136
if rctx.attr.distutils:
135137
rctx.file(distutils_path, rctx.read(rctx.attr.distutils))
136138
elif rctx.attr.distutils_content:
@@ -255,13 +257,15 @@ define_hermetic_runtime_toolchain_impl(
255257
python_version = {python_version},
256258
python_bin = {python_bin},
257259
coverage_tool = {coverage_tool},
260+
free_threading = {free_threading},
258261
)
259262
""".format(
260263
extra_files_glob_exclude = render.list(glob_exclude),
261264
extra_files_glob_include = render.list(glob_include),
262265
python_bin = render.str(python_bin),
263266
python_version = render.str(rctx.attr.python_version),
264267
coverage_tool = render.str(coverage_tool),
268+
free_threading = free_threading,
265269
)
266270
rctx.delete("python")
267271
rctx.symlink(python_bin, "python")
@@ -321,6 +325,10 @@ For more information see {attr}`py_runtime.coverage_tool`.
321325
"Either distutils or distutils_content can be specified, but not both.",
322326
mandatory = False,
323327
),
328+
"free_threading": attr.bool(
329+
doc = "Whether we use CPython interpreter in free-threading mode (disabled GIL).",
330+
default = False,
331+
),
324332
"ignore_root_user_error": attr.bool(
325333
default = False,
326334
doc = "Whether the check for root should be ignored or not. This causes cache misses with .pyc files.",

0 commit comments

Comments
 (0)