Skip to content

Commit 68d1b41

Browse files
aignasrickeylev
andauthored
refactor!(toolchain): remove uname dep in the repository_rule stage (#2406)
Before this PR we would shell out to `uname` on UNIX systems to get the `arch` of the toolchain - on Windows we would not need to do it because there used to be only a single Windows platform. With this change we can correctly support the resolution of the python interpreter on various platforms and I have also added an env variable to customize the selection, so that users can use `musl` or a `freethreaded` interpreter if they wish. As part of this change, I have restricted visibility of the config settings used in the toolchain alias repo so that we are creating fewer targets. This is a very good time to do this before `1.0.0`. Fixes #2145 Work towards #2276 Work towards #2386 Work towards #1211 to unblock #2402 Work towards #1361 --------- Co-authored-by: Richard Levasseur <[email protected]>
1 parent 79bd1f5 commit 68d1b41

File tree

10 files changed

+195
-109
lines changed

10 files changed

+195
-109
lines changed

CHANGELOG.md

+24-1
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,39 @@ Unreleased changes template.
5252

5353
{#v0-0-0-changed}
5454
### Changed
55+
56+
**Breaking**:
57+
* (toolchains) stop exposing config settings in python toolchain alias repos.
58+
Please consider depending on the flags defined in
59+
`//python/config_setting/...` and the `@platforms` package instead.
60+
* (toolchains) consumers who were depending on the `MACOS_NAME` and the `arch`
61+
attribute in the `PLATFORMS` list, please update your code to respect the new
62+
values. The values now correspond to the values available in the
63+
`@platforms//` package constraint values.
64+
* (toolchains) `host_platform` and `interpreter` constants are no longer created
65+
in the `toolchain` generated alias `.bzl` files. If you need to access the
66+
host interpreter during the `repository_rule` evaluation, please use the
67+
`@python_{version}_host//:python` targets created by
68+
{bzl:obj}`python_register_toolchains` and
69+
{bzl:obj}`python_register_multi_toolchains` macros or the {bzl:obj}`python`
70+
bzlmod extension.
71+
72+
Other changes:
5573
* (python_repository) Start honoring the `strip_prefix` field for `zstd` archives.
5674

5775
{#v0-0-0-fixed}
5876
### Fixed
59-
* Nothing fixed.
77+
* (toolchains) stop depending on `uname` to get the value of the host platform.
6078

6179
{#v0-0-0-added}
6280
### Added
6381
* (gazelle): Parser failures will now be logged to the terminal. Additional
6482
details can be logged by setting `GAZELLE_VERBOSE=1`.
83+
* (toolchains) allow users to select which variant of the support host toolchain
84+
they would like to use through
85+
`RULES_PYTHON_REPO_TOOLCHAIN_{VERSION}_{OS}_{ARCH}` env variable setting. For
86+
example, this allows one to use `freethreaded` python interpreter in the
87+
`repository_rule` to build a wheel from `sdist`.
6588

6689
{#v0-0-0-removed}
6790
### Removed

WORKSPACE

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ load("@rules_python_gazelle_plugin//:deps.bzl", _py_gazelle_deps = "gazelle_deps
8686
_py_gazelle_deps()
8787

8888
# This interpreter is used for various rules_python dev-time tools
89-
load("@python//3.11.9:defs.bzl", "interpreter")
89+
interpreter = "@python_3_11_9_host//:python"
9090

9191
#####################
9292
# Install twine for our own runfiles wheel publishing.

docs/environment-variables.md

+10
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ Determines the verbosity of logging output for repo rules. Valid values:
1515
* `TRACE`
1616
:::
1717

18+
:::{envvar} RULES_PYTHON_REPO_TOOLCHAIN_VERSION_OS_ARCH
19+
20+
Determines the python interpreter platform to be used for a particular
21+
interpreter `(version, os, arch)` triple to be used in repository rules.
22+
Replace the `VERSION_OS_ARCH` part with actual values when using, e.g.
23+
`3_13_0_linux_x86_64`. The version values must have `_` instead of `.` and the
24+
os, arch values are the same as the ones mentioned in the
25+
`//python:versions.bzl` file.
26+
:::
27+
1828
:::{envvar} RULES_PYTHON_PIP_ISOLATED
1929

2030
Determines if `--isolated` is used with pip.

examples/bzlmod/MODULE.bazel.lock

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

python/private/python_register_toolchains.bzl

+5-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,11 @@ def python_register_toolchains(
160160
platform = platform,
161161
))
162162

163-
host_toolchain(name = name + "_host")
163+
host_toolchain(
164+
name = name + "_host",
165+
platforms = loaded_platforms,
166+
python_version = python_version,
167+
)
164168

165169
toolchain_aliases(
166170
name = name,

python/private/repo_utils.bzl

+5-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ def _logger(mrctx, name = None):
4141
4242
Returns:
4343
A struct with attributes logging: trace, debug, info, warn, fail.
44+
Please use `return logger.fail` when using the `fail` method, because
45+
it makes `buildifier` happy and ensures that other implementation of
46+
the logger injected into the function work as expected by terminating
47+
on the given line.
4448
"""
4549
if _is_repo_debug_enabled(mrctx):
4650
verbosity_level = "DEBUG"
@@ -140,7 +144,7 @@ def _execute_internal(
140144
result = mrctx.execute(arguments, environment = environment, **kwargs)
141145

142146
if fail_on_error and result.return_code != 0:
143-
logger.fail((
147+
return logger.fail((
144148
"repo.execute: {op}: end: failure:\n" +
145149
" command: {cmd}\n" +
146150
" return code: {return_code}\n" +

python/private/toolchain_aliases.bzl

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Copyright 2024 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Create toolchain alias targets."""
16+
17+
load("@rules_python//python:versions.bzl", "PLATFORMS")
18+
19+
def toolchain_aliases(*, name, platforms, visibility = None, native = native):
20+
"""Create toolchain aliases for the python toolchains.
21+
22+
Args:
23+
name: {type}`str` The name of the current repository.
24+
platforms: {type}`platforms` The list of platforms that are supported
25+
for the current toolchain repository.
26+
visibility: {type}`list[Target] | None` The visibility of the aliases.
27+
native: The native struct used in the macro, useful for testing.
28+
"""
29+
for platform in PLATFORMS.keys():
30+
if platform not in platforms:
31+
continue
32+
33+
native.config_setting(
34+
name = platform,
35+
flag_values = PLATFORMS[platform].flag_values,
36+
constraint_values = PLATFORMS[platform].compatible_with,
37+
visibility = ["//visibility:private"],
38+
)
39+
40+
prefix = name
41+
for name in [
42+
"files",
43+
"includes",
44+
"libpython",
45+
"py3_runtime",
46+
"python_headers",
47+
"python_runtimes",
48+
]:
49+
native.alias(
50+
name = name,
51+
actual = select({
52+
":" + platform: "@{}_{}//:{}".format(prefix, platform, name)
53+
for platform in platforms
54+
}),
55+
visibility = visibility,
56+
)
57+
58+
native.alias(
59+
name = "python3",
60+
actual = select({
61+
":" + platform: "@{}_{}//:{}".format(prefix, platform, "python.exe" if "windows" in platform else "bin/python3")
62+
for platform in platforms
63+
}),
64+
visibility = visibility,
65+
)
66+
native.alias(
67+
name = "pip",
68+
actual = select({
69+
":" + platform: "@{}_{}//:python_runtimes".format(prefix, platform)
70+
for platform in platforms
71+
if "windows" not in platform
72+
}),
73+
visibility = visibility,
74+
)

0 commit comments

Comments
 (0)