Skip to content

Commit

Permalink
Added flag_values for free_threading argument to point to correct pat…
Browse files Browse the repository at this point in the history
…hs of the headers and the library.
  • Loading branch information
vfdev-5 committed Oct 7, 2024
1 parent 30fc3f9 commit e005b94
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 12 deletions.
10 changes: 10 additions & 0 deletions python/config_settings/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,13 @@ string_flag(
define_pypi_internal_flags(
name = "define_pypi_internal_flags",
)

string_flag(
name = "free_threading",
build_setting_default = "no",
values = [
"yes",
"no",
],
visibility = ["//visibility:public"],
)
27 changes: 16 additions & 11 deletions python/private/hermetic_runtime_repo_setup.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ def define_hermetic_runtime_toolchain_impl(
extra_files_glob_exclude,
python_version,
python_bin,
coverage_tool):
coverage_tool,
free_threading = False):
"""Define a toolchain implementation for a python-build-standalone repo.
It expected this macro is called in the top-level package of an extracted
Expand All @@ -44,13 +45,17 @@ def define_hermetic_runtime_toolchain_impl(
python_version: {type}`str` The Python version, in `major.minor.micro`
format.
python_bin: {type}`str` The path to the Python binary within the
repositoroy.
repository.
coverage_tool: {type}`str` optional target to the coverage tool to
use.
free_threading: {type}`bool` optional free-threading support.
Default, False.
"""
_ = name # @unused
version_info = semver(python_version)
version_dict = version_info.to_dict()
version_dict["ft_postfix"] = "t" if free_threading else ""

native.filegroup(
name = "files",
srcs = native.glob(
Expand All @@ -67,19 +72,19 @@ def define_hermetic_runtime_toolchain_impl(
"**/* *", # Bazel does not support spaces in file names.
# Unused shared libraries. `python` executable and the `:libpython` target
# depend on `libpython{python_version}.so.1.0`.
"lib/libpython{major}.{minor}.so".format(**version_dict),
"lib/libpython{major}.{minor}{ft_postfix}.so".format(**version_dict),
# static libraries
"lib/**/*.a",
# tests for the standard libraries.
"lib/python{major}.{minor}/**/test/**".format(**version_dict),
"lib/python{major}.{minor}/**/tests/**".format(**version_dict),
"lib/python{major}.{minor}{ft_postfix}/**/test/**".format(**version_dict),
"lib/python{major}.{minor}{ft_postfix}/**/tests/**".format(**version_dict),
"**/__pycache__/*.pyc.*", # During pyc creation, temp files named *.pyc.NNN are created
] + extra_files_glob_exclude,
),
)
cc_import(
name = "interface",
interface_library = "libs/python{major}{minor}.lib".format(**version_dict),
interface_library = "libs/python{major}{minor}{ft_postfix}.lib".format(**version_dict),
system_provided = True,
)

Expand All @@ -96,7 +101,7 @@ def define_hermetic_runtime_toolchain_impl(
hdrs = [":includes"],
includes = [
"include",
"include/python{major}.{minor}".format(**version_dict),
"include/python{major}.{minor}{ft_postfix}".format(**version_dict),
"include/python{major}.{minor}m".format(**version_dict),
],
)
Expand All @@ -105,11 +110,11 @@ def define_hermetic_runtime_toolchain_impl(
hdrs = [":includes"],
srcs = select({
"@platforms//os:linux": [
"lib/libpython{major}.{minor}.so".format(**version_dict),
"lib/libpython{major}.{minor}.so.1.0".format(**version_dict),
"lib/libpython{major}.{minor}{ft_postfix}.so".format(**version_dict),
"lib/libpython{major}.{minor}{ft_postfix}.so.1.0".format(**version_dict),
],
"@platforms//os:macos": ["lib/libpython{major}.{minor}.dylib".format(**version_dict)],
"@platforms//os:windows": ["python3.dll", "libs/python{major}{minor}.lib".format(**version_dict)],
"@platforms//os:macos": ["lib/libpython{major}.{minor}{ft_postfix}.dylib".format(**version_dict)],
"@platforms//os:windows": ["python3.dll", "libs/python{major}{minor}{ft_postfix}.lib".format(**version_dict)],
}),
)

Expand Down
16 changes: 16 additions & 0 deletions python/private/python.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,13 @@ def _process_single_version_overrides(*, tag, _fail = fail, default):
for platform in sha256
}

if tag.flag_values:
# Normalize flag keys to strings
flag_values = {str(k): v for k, v in tag.flag_values.items()}
override["flag_values"] = flag_values
if tag.suffix:
override["suffix"] = tag.suffix

available_versions[tag.python_version] = {k: v for k, v in override.items() if v}

if tag.distutils_content:
Expand Down Expand Up @@ -789,6 +796,15 @@ class.
mandatory = False,
doc = "The URL template to fetch releases for this Python version. See {attr}`python.single_version_platform_override.urls` for documentation.",
),
"flag_values": attr.string_dict(
mandatory = False,
doc = "TODO",
),
"suffix": attr.string(
mandatory = False,
doc = "TODO",
default = "",
)
},
)

Expand Down
13 changes: 13 additions & 0 deletions python/private/python_register_toolchains.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,18 @@ def python_register_toolchains(
)],
)

flag_values = tool_versions[python_version].get("flag_values", None)
free_threading_label = "@rules_python//python/config_settings:free_threading"
free_threading = False
if flag_values:
free_threading = flag_values.get(free_threading_label, False) == "yes"

# print("python_version:", python_version)
# print("tool_versions:", tool_versions[python_version])
# print("flag_values:", flag_values)
# print("free_threading:", free_threading)
suffix = tool_versions[python_version].get("suffix", "")

python_repository(
name = "{name}_{platform}".format(
name = name,
Expand All @@ -143,6 +155,7 @@ def python_register_toolchains(
urls = urls,
strip_prefix = strip_prefix,
coverage_tool = coverage_tool,
free_threading = free_threading,
**kwargs
)
if register_toolchains:
Expand Down
10 changes: 9 additions & 1 deletion python/private/python_repository.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def _python_repository_impl(rctx):
release_filename = rctx.attr.release_filename
urls = rctx.attr.urls or [rctx.attr.url]
auth = get_auth(rctx, urls)
free_threading = rctx.attr.free_threading

if release_filename.endswith(".zst"):
rctx.download(
Expand Down Expand Up @@ -130,7 +131,8 @@ def _python_repository_impl(rctx):
if "windows" in platform:
distutils_path = "Lib/distutils/distutils.cfg"
else:
distutils_path = "lib/python{}/distutils/distutils.cfg".format(python_short_version)
ft_postfix = "t" if free_threading else ""
distutils_path = "lib/python{}{}/distutils/distutils.cfg".format(python_short_version, ft_postfix)
if rctx.attr.distutils:
rctx.file(distutils_path, rctx.read(rctx.attr.distutils))
elif rctx.attr.distutils_content:
Expand Down Expand Up @@ -255,13 +257,15 @@ define_hermetic_runtime_toolchain_impl(
python_version = {python_version},
python_bin = {python_bin},
coverage_tool = {coverage_tool},
free_threading = {free_threading},
)
""".format(
extra_files_glob_exclude = render.list(glob_exclude),
extra_files_glob_include = render.list(glob_include),
python_bin = render.str(python_bin),
python_version = render.str(rctx.attr.python_version),
coverage_tool = render.str(coverage_tool),
free_threading = free_threading,
)
rctx.delete("python")
rctx.symlink(python_bin, "python")
Expand Down Expand Up @@ -321,6 +325,10 @@ For more information see {attr}`py_runtime.coverage_tool`.
"Either distutils or distutils_content can be specified, but not both.",
mandatory = False,
),
"free_threading": attr.bool(
doc = "Whether we use CPython interpreter in free-threading mode (disabled GIL).",
default = False,
),
"ignore_root_user_error": attr.bool(
default = False,
doc = "Whether the check for root should be ignored or not. This causes cache misses with .pyc files.",
Expand Down

0 comments on commit e005b94

Please sign in to comment.