Skip to content

Commit 0ee0678

Browse files
committed
cleanup and fix
1 parent 7ff127d commit 0ee0678

File tree

5 files changed

+27
-49
lines changed

5 files changed

+27
-49
lines changed

docs/toolchains.md

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -418,8 +418,8 @@ The two settings to change are
418418
{obj}`local_runtime_toolchains_repo.target_compatible_with` and
419419
{obj}`local_runtime_toolchains_repo.target_settings`, which control how Bazel
420420
decides if a toolchain should match. By default, they point to targets *within*
421-
the local runtime repository. We have to override them to *not* reference the
422-
local runtime repository at all.
421+
the local runtime repository (trigger repo initialization). We have to override
422+
them to *not* reference the local runtime repository at all.
423423

424424
In the example below, we reconfigure the local toolchains so they are only
425425
activated if the custom flag `--//:py=local` is set and the target platform
@@ -438,35 +438,27 @@ local_runtime_toolchains_repo(
438438
"local_python3": ["HOST_CONSTRAINTS"],
439439
},
440440
target_settings = {
441-
"local_python3": ["@//:is_local_py_enabled"]
441+
"local_python3": ["@//:is_py_local"]
442442
}
443443
)
444444
445445
# File: BUILD.bazel
446446
load("@bazel_skylib//rules:common_settings.bzl", "string_flag")
447447
448-
alias(
449-
name = "is_local_py_enabled",
450-
actual = select({
451-
":_is_py_local_set": "@local_python3//:is_matching_python_version",
452-
"//conditions:default": ":_does_not_match",
453-
}),
454-
)
455-
456448
config_setting(
457-
name = "_is_py_local_set",
449+
name = "is_py_local",
458450
flag_values = {":py": "local"},
459451
)
460452
461-
config_setting(
462-
name = "_does_not_match",
463-
flag_values = {":py": "<DOES NOT MATCH>"},
453+
string_flag(
454+
name = "py",
455+
build_setting_default = "",
464456
)
465-
466457
```
467458

468459
:::{tip}
469-
With some minor changes, different values for the `--//:py` flag can be used
460+
Easily switching between *multiple* local toolchains can be accomplished by
461+
adding additional `:is_py_X` targets and setting `--//:py` to match.
470462
to easily switch between different local toolchains.
471463
:::
472464

@@ -498,7 +490,7 @@ locally installed Python.
498490
### Autodetecting toolchain
499491

500492
The autodetecting toolchain is a deprecated toolchain that is built into Bazel.
501-
It's name is a bit misleading: it doesn't autodetect anything. All it does is
493+
**It's name is a bit misleading: it doesn't autodetect anything**. All it does is
502494
use `python3` from the environment a binary runs within. This provides extremely
503495
limited functionality to the rules (at build time, nothing is knowable about
504496
the Python runtime).

python/private/local_runtime_toolchains_repo.bzl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,10 @@ needed because the strings are evaluated in a different context than where
167167
they originate.
168168
:::
169169
170-
The list of settings are **becomes the** {obj}`toolchain.target_settings` value
171-
for each respective repo; i.e. they replace the auto-detected values the
172-
local runtime itself computes.
170+
The list of settings will be applied atop of any of the local runtime's
171+
settings that are used for {obj}`toolchain.target_settings`. i.e. they are
172+
evaluated first and guard the checking of the local runtime's auto-detected
173+
conditions.
173174
174175
This allows a local toolchain to only be used if certain flags or
175176
config setting conditions are met. Such conditions can include user-defined

python/private/py_toolchain_suite.bzl

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -184,14 +184,22 @@ def define_local_toolchain_suites(
184184

185185
i = 0
186186
for i, repo in enumerate(version_aware_repo_names, start = i):
187+
target_settings = ["@{}//:is_matching_python_version".format(repo)]
188+
189+
if repo_target_settings.get(repo):
190+
selects.config_setting_group(
191+
name = "_{}_user_guard".format(repo),
192+
match_all = repo_target_settings.get(repo, []) + target_settings,
193+
)
194+
target_settings = ["_{}_user_guard".format(repo)]
187195
_internal_toolchain_suite(
188196
prefix = render.left_pad_zero(i, 4),
189197
runtime_repo_name = repo,
190198
target_compatible_with = _get_local_toolchain_target_compatible_with(
191199
repo,
192200
repo_target_compatible_with,
193201
),
194-
target_settings = _get_local_toolchain_target_settings(repo, repo_target_settings),
202+
target_settings = target_settings,
195203
exec_compatible_with = repo_exec_compatible_with.get(repo, []),
196204
)
197205

@@ -211,13 +219,6 @@ def define_local_toolchain_suites(
211219
exec_compatible_with = repo_exec_compatible_with.get(repo, []),
212220
)
213221

214-
def _get_local_toolchain_target_settings(repo, repo_target_settings):
215-
if repo in repo_target_settings:
216-
target_settings = repo_target_settings[repo]
217-
else:
218-
target_settings = ["@{}//:is_matching_python_version".format(repo)]
219-
return target_settings
220-
221222
def _get_local_toolchain_target_compatible_with(repo, repo_target_compatible_with):
222223
if repo in repo_target_compatible_with:
223224
target_compatible_with = repo_target_compatible_with[repo]

tests/integration/local_toolchains/BUILD.bazel

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,38 +15,22 @@
1515
load("@bazel_skylib//rules:common_settings.bzl", "string_flag")
1616
load("@rules_python//python:py_test.bzl", "py_test")
1717

18-
package(default_visibility = ["//visibility:public"])
19-
2018
py_test(
2119
name = "test",
2220
srcs = ["test.py"],
2321
# Make this test better respect pyenv
2422
env_inherit = ["PYENV_VERSION"],
2523
)
2624

27-
alias(
28-
name = "is_local_py_enabled",
29-
actual = select({
30-
":_is_py_local_set": "@local_python3//:is_matching_python_version",
31-
"//conditions:default": ":_does_not_match",
32-
}),
33-
)
34-
3525
config_setting(
36-
name = "_is_py_local_set",
26+
name = "is_py_local",
3727
flag_values = {
3828
":py": "local",
3929
},
4030
)
4131

42-
config_setting(
43-
name = "_does_not_match",
44-
flag_values = {
45-
":py": "<DOES NOT MATCH>",
46-
},
47-
)
48-
49-
# set to "local" to use the local toolchain
32+
# Set `--//:py=local` to use the local toolchain
33+
# (This is set in this example's .bazelrc)
5034
string_flag(
5135
name = "py",
5236
build_setting_default = "",

tests/integration/local_toolchains/MODULE.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ local_runtime_toolchains_repo(
4242
},
4343
target_settings = {
4444
"local_python3": [
45-
"@//:is_local_py_enabled",
45+
"@//:is_py_local",
4646
],
4747
},
4848
)

0 commit comments

Comments
 (0)