Skip to content

Commit

Permalink
WIP adding support for cpython build_option
Browse files Browse the repository at this point in the history
  • Loading branch information
vfdev-5 committed Oct 10, 2024
1 parent 5a06d94 commit be242fa
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 20 deletions.
2 changes: 1 addition & 1 deletion python/private/py_exec_tools_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ py_exec_tools_toolchain = rule(
Provides a toolchain for build time tools.
This provides `ToolchainInfo` with the following attributes:
* `exec_tools`: {type}`PyExecToolsInfo`
* `exec_tools`: {type}`PyExecToolsInfo`
* `toolchain_label`: {type}`Label` _only present when `--visibile_for_testing=True`
for internal testing_. The rule's label; this allows identifying what toolchain
implmentation was selected for testing purposes.
Expand Down
28 changes: 15 additions & 13 deletions python/private/python_register_toolchains.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -109,35 +109,37 @@ def python_register_toolchains(
if not sha256:
continue

build_option = tool_versions[python_version].get("build_option", None)
loaded_platforms.append(platform)
(release_filename, urls, strip_prefix, patches, patch_strip) = get_release_info(platform, python_version, base_url, tool_versions)
(release_filename, urls, strip_prefix, patches, patch_strip, free_threading) = get_release_info(
platform, python_version, base_url, tool_versions, build_option=build_option
)

build_opt_str = ("_" + build_option.replace("+", "-")) if build_option else ""
name_with_build_opt = "{name}{build_opt}".format(
name = name,
build_opt = build_opt_str
)
# allow passing in a tool version
coverage_tool = None
coverage_tool = tool_versions[python_version].get("coverage_tool", {}).get(platform, None)
if register_coverage_tool and coverage_tool == None:
coverage_tool = coverage_dep(
name = "{name}_{platform}_coverage".format(
name = name,
name = name_with_build_opt,
platform = platform,
),
python_version = python_version,
platform = platform,
visibility = ["@{name}_{platform}//:__subpackages__".format(
name = name,
name = name_with_build_opt,
platform = platform,
)],
)

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"
suffix = tool_versions[python_version].get("suffix", "")
python_repository(
name = "{name}_{platform}".format(
name = name,
name = name_with_build_opt,
platform = platform,
),
sha256 = sha256,
Expand Down Expand Up @@ -166,12 +168,12 @@ def python_register_toolchains(
platform = platform,
))

host_toolchain(name = name + "_host")
host_toolchain(name = name_with_build_opt + "_host")

toolchain_aliases(
name = name,
python_version = python_version,
user_repository_name = name,
user_repository_name = name_with_build_opt,
platforms = loaded_platforms,
)

Expand All @@ -183,5 +185,5 @@ def python_register_toolchains(
name = toolchain_repo_name,
python_version = python_version,
set_python_version_constraint = set_python_version_constraint,
user_repository_name = name,
user_repository_name = name_with_build_opt,
)
4 changes: 2 additions & 2 deletions python/private/python_repository.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ def _python_repository_impl(rctx):
for patch in patches:
rctx.patch(patch, strip = rctx.attr.patch_strip)

ft_postfix = "t" if free_threading else ""
# Write distutils.cfg to the Python installation.
if "windows" in platform:
distutils_path = "Lib/distutils/distutils.cfg"
else:
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))
Expand All @@ -145,7 +145,7 @@ def _python_repository_impl(rctx):
# dyld lookup errors. To fix, set the full path to the dylib as
# it appears in the Bazel workspace as its LC_ID_DYLIB using
# the `install_name_tool` bundled with macOS.
dylib = "libpython{}.dylib".format(python_short_version)
dylib = "libpython{}{}.dylib".format(python_short_version, ft_postfix)
repo_utils.execute_checked(
rctx,
op = "python_repository.FixUpDyldIdPath",
Expand Down
16 changes: 12 additions & 4 deletions python/versions.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -703,20 +703,28 @@ PLATFORMS = {
),
}

def get_release_info(platform, python_version, base_url = DEFAULT_RELEASE_BASE_URL, tool_versions = TOOL_VERSIONS):
def get_release_info(
platform, python_version, base_url = DEFAULT_RELEASE_BASE_URL, tool_versions = TOOL_VERSIONS, build_option = None
):
"""Resolve the release URL for the requested interpreter version
Args:
platform: The platform string for the interpreter
python_version: The version of the interpreter to get
base_url: The URL to prepend to the 'url' attr in the tool_versions dict
tool_versions: A dict listing the interpreter versions, their SHAs and URL
build_option: Python build option, default: "install_only"
Returns:
A tuple of (filename, url, archive strip prefix, patches, patch_strip)
A tuple of (filename, url, archive strip prefix, patches, patch_strip, free_threading)
"""

url = tool_versions[python_version]["url"]
if not build_option:
build_option = "shared-install_only" if (WINDOWS_NAME in platform) else "install_only"
free_threading = False
else:
free_threading = True if build_option.startswith("freethreaded") else False

if type(url) == type({}):
url = url[platform]
Expand All @@ -734,7 +742,7 @@ def get_release_info(platform, python_version, base_url = DEFAULT_RELEASE_BASE_U
release_filename = u.format(
platform = platform,
python_version = python_version,
build = "shared-install_only" if (WINDOWS_NAME in platform) else "install_only",
build = build_option,
)
if "://" in release_filename: # is absolute url?
rendered_urls.append(release_filename)
Expand All @@ -757,7 +765,7 @@ def get_release_info(platform, python_version, base_url = DEFAULT_RELEASE_BASE_U
else:
patch_strip = None

return (release_filename, rendered_urls, strip_prefix, patches, patch_strip)
return (release_filename, rendered_urls, strip_prefix, patches, patch_strip, free_threading)

def print_toolchains_checksums(name):
native.genrule(
Expand Down

0 comments on commit be242fa

Please sign in to comment.