Skip to content

Commit be242fa

Browse files
committed
WIP adding support for cpython build_option
1 parent 5a06d94 commit be242fa

File tree

4 files changed

+30
-20
lines changed

4 files changed

+30
-20
lines changed

python/private/py_exec_tools_toolchain.bzl

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ py_exec_tools_toolchain = rule(
4343
Provides a toolchain for build time tools.
4444
4545
This provides `ToolchainInfo` with the following attributes:
46-
* `exec_tools`: {type}`PyExecToolsInfo`
46+
* `exec_tools`: {type}`PyExecToolsInfo`
4747
* `toolchain_label`: {type}`Label` _only present when `--visibile_for_testing=True`
4848
for internal testing_. The rule's label; this allows identifying what toolchain
4949
implmentation was selected for testing purposes.

python/private/python_register_toolchains.bzl

+15-13
Original file line numberDiff line numberDiff line change
@@ -109,35 +109,37 @@ def python_register_toolchains(
109109
if not sha256:
110110
continue
111111

112+
build_option = tool_versions[python_version].get("build_option", None)
112113
loaded_platforms.append(platform)
113-
(release_filename, urls, strip_prefix, patches, patch_strip) = get_release_info(platform, python_version, base_url, tool_versions)
114+
(release_filename, urls, strip_prefix, patches, patch_strip, free_threading) = get_release_info(
115+
platform, python_version, base_url, tool_versions, build_option=build_option
116+
)
114117

118+
build_opt_str = ("_" + build_option.replace("+", "-")) if build_option else ""
119+
name_with_build_opt = "{name}{build_opt}".format(
120+
name = name,
121+
build_opt = build_opt_str
122+
)
115123
# allow passing in a tool version
116124
coverage_tool = None
117125
coverage_tool = tool_versions[python_version].get("coverage_tool", {}).get(platform, None)
118126
if register_coverage_tool and coverage_tool == None:
119127
coverage_tool = coverage_dep(
120128
name = "{name}_{platform}_coverage".format(
121-
name = name,
129+
name = name_with_build_opt,
122130
platform = platform,
123131
),
124132
python_version = python_version,
125133
platform = platform,
126134
visibility = ["@{name}_{platform}//:__subpackages__".format(
127-
name = name,
135+
name = name_with_build_opt,
128136
platform = platform,
129137
)],
130138
)
131139

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-
suffix = tool_versions[python_version].get("suffix", "")
138140
python_repository(
139141
name = "{name}_{platform}".format(
140-
name = name,
142+
name = name_with_build_opt,
141143
platform = platform,
142144
),
143145
sha256 = sha256,
@@ -166,12 +168,12 @@ def python_register_toolchains(
166168
platform = platform,
167169
))
168170

169-
host_toolchain(name = name + "_host")
171+
host_toolchain(name = name_with_build_opt + "_host")
170172

171173
toolchain_aliases(
172174
name = name,
173175
python_version = python_version,
174-
user_repository_name = name,
176+
user_repository_name = name_with_build_opt,
175177
platforms = loaded_platforms,
176178
)
177179

@@ -183,5 +185,5 @@ def python_register_toolchains(
183185
name = toolchain_repo_name,
184186
python_version = python_version,
185187
set_python_version_constraint = set_python_version_constraint,
186-
user_repository_name = name,
188+
user_repository_name = name_with_build_opt,
187189
)

python/private/python_repository.bzl

+2-2
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,11 @@ def _python_repository_impl(rctx):
127127
for patch in patches:
128128
rctx.patch(patch, strip = rctx.attr.patch_strip)
129129

130+
ft_postfix = "t" if free_threading else ""
130131
# Write distutils.cfg to the Python installation.
131132
if "windows" in platform:
132133
distutils_path = "Lib/distutils/distutils.cfg"
133134
else:
134-
ft_postfix = "t" if free_threading else ""
135135
distutils_path = "lib/python{}{}/distutils/distutils.cfg".format(python_short_version, ft_postfix)
136136
if rctx.attr.distutils:
137137
rctx.file(distutils_path, rctx.read(rctx.attr.distutils))
@@ -145,7 +145,7 @@ def _python_repository_impl(rctx):
145145
# dyld lookup errors. To fix, set the full path to the dylib as
146146
# it appears in the Bazel workspace as its LC_ID_DYLIB using
147147
# the `install_name_tool` bundled with macOS.
148-
dylib = "libpython{}.dylib".format(python_short_version)
148+
dylib = "libpython{}{}.dylib".format(python_short_version, ft_postfix)
149149
repo_utils.execute_checked(
150150
rctx,
151151
op = "python_repository.FixUpDyldIdPath",

python/versions.bzl

+12-4
Original file line numberDiff line numberDiff line change
@@ -703,20 +703,28 @@ PLATFORMS = {
703703
),
704704
}
705705

706-
def get_release_info(platform, python_version, base_url = DEFAULT_RELEASE_BASE_URL, tool_versions = TOOL_VERSIONS):
706+
def get_release_info(
707+
platform, python_version, base_url = DEFAULT_RELEASE_BASE_URL, tool_versions = TOOL_VERSIONS, build_option = None
708+
):
707709
"""Resolve the release URL for the requested interpreter version
708710
709711
Args:
710712
platform: The platform string for the interpreter
711713
python_version: The version of the interpreter to get
712714
base_url: The URL to prepend to the 'url' attr in the tool_versions dict
713715
tool_versions: A dict listing the interpreter versions, their SHAs and URL
716+
build_option: Python build option, default: "install_only"
714717
715718
Returns:
716-
A tuple of (filename, url, archive strip prefix, patches, patch_strip)
719+
A tuple of (filename, url, archive strip prefix, patches, patch_strip, free_threading)
717720
"""
718721

719722
url = tool_versions[python_version]["url"]
723+
if not build_option:
724+
build_option = "shared-install_only" if (WINDOWS_NAME in platform) else "install_only"
725+
free_threading = False
726+
else:
727+
free_threading = True if build_option.startswith("freethreaded") else False
720728

721729
if type(url) == type({}):
722730
url = url[platform]
@@ -734,7 +742,7 @@ def get_release_info(platform, python_version, base_url = DEFAULT_RELEASE_BASE_U
734742
release_filename = u.format(
735743
platform = platform,
736744
python_version = python_version,
737-
build = "shared-install_only" if (WINDOWS_NAME in platform) else "install_only",
745+
build = build_option,
738746
)
739747
if "://" in release_filename: # is absolute url?
740748
rendered_urls.append(release_filename)
@@ -757,7 +765,7 @@ def get_release_info(platform, python_version, base_url = DEFAULT_RELEASE_BASE_U
757765
else:
758766
patch_strip = None
759767

760-
return (release_filename, rendered_urls, strip_prefix, patches, patch_strip)
768+
return (release_filename, rendered_urls, strip_prefix, patches, patch_strip, free_threading)
761769

762770
def print_toolchains_checksums(name):
763771
native.genrule(

0 commit comments

Comments
 (0)