Skip to content

Commit 90d0776

Browse files
committed
Added flag_values for free_threading argument to point to correct paths of the headers and the library.
1 parent 9c3d303 commit 90d0776

File tree

4 files changed

+41
-12
lines changed

4 files changed

+41
-12
lines changed

python/config_settings/BUILD.bazel

+7
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,10 @@ string_flag(
169169
define_pypi_internal_flags(
170170
name = "define_pypi_internal_flags",
171171
)
172+
173+
string_flag(
174+
name = "free_threading",
175+
build_setting_default = "",
176+
values = ["yes", "no"],
177+
visibility = ["//visibility:public"],
178+
)

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_register_toolchains.bzl

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

132+
flag_values = tool_versions[python_version].get("flag_values", None)
133+
free_threading_label = str(Label("//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+
132138
python_repository(
133139
name = "{name}_{platform}".format(
134140
name = name,
@@ -143,6 +149,7 @@ def python_register_toolchains(
143149
urls = urls,
144150
strip_prefix = strip_prefix,
145151
coverage_tool = coverage_tool,
152+
free_threading = free_threading,
146153
**kwargs
147154
)
148155
if register_toolchains:

python/private/python_repository.bzl

+11-1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ def _python_repository_impl(rctx):
6868
urls = rctx.attr.urls or [rctx.attr.url]
6969
auth = get_auth(rctx, urls)
7070

71+
# TODO: get free_threading from flag_values
72+
free_threading = rctx.attr.free_threading
73+
7174
if release_filename.endswith(".zst"):
7275
rctx.download(
7376
url = urls,
@@ -130,7 +133,8 @@ def _python_repository_impl(rctx):
130133
if "windows" in platform:
131134
distutils_path = "Lib/distutils/distutils.cfg"
132135
else:
133-
distutils_path = "lib/python{}/distutils/distutils.cfg".format(python_short_version)
136+
ft_postfix = "t" if free_threading else ""
137+
distutils_path = "lib/python{}{}/distutils/distutils.cfg".format(python_short_version, ft_postfix)
134138
if rctx.attr.distutils:
135139
rctx.file(distutils_path, rctx.read(rctx.attr.distutils))
136140
elif rctx.attr.distutils_content:
@@ -255,13 +259,15 @@ define_hermetic_runtime_toolchain_impl(
255259
python_version = {python_version},
256260
python_bin = {python_bin},
257261
coverage_tool = {coverage_tool},
262+
free_threading = {free_threading},
258263
)
259264
""".format(
260265
extra_files_glob_exclude = render.list(glob_exclude),
261266
extra_files_glob_include = render.list(glob_include),
262267
python_bin = render.str(python_bin),
263268
python_version = render.str(rctx.attr.python_version),
264269
coverage_tool = render.str(coverage_tool),
270+
free_threading = free_threading,
265271
)
266272
rctx.delete("python")
267273
rctx.symlink(python_bin, "python")
@@ -383,6 +389,10 @@ function defaults (e.g. `single_version_override` for `MODULE.bazel` files.
383389
"zstd_version": attr.string(
384390
default = "1.5.2",
385391
),
392+
"free_threading": attr.bool(
393+
doc = "TODO",
394+
default = False
395+
),
386396
"_rule_name": attr.string(default = "python_repository"),
387397
},
388398
environ = [REPO_DEBUG_ENV_VAR],

0 commit comments

Comments
 (0)