Skip to content

Commit 2aa7528

Browse files
committed
fix(toolchain): do not force users to depend on optional toolchains
With this change we set the `target_settings` to something that is not going to be ever matched if the `precompile` flag is not enabled. Users wanting to use the `precompile` feature will need to set the config_setting value to `enabled` instead of `auto`. Fixes #1967
1 parent 7a437cc commit 2aa7528

File tree

6 files changed

+54
-4
lines changed

6 files changed

+54
-4
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ A brief description of the categories of changes:
2828
* `protobuf`/`com_google_protobuf` dependency bumped to `v24.4`
2929

3030
### Fixed
31-
* Nothing yet
31+
* The targets would depend on the `exec_tools_toolchain_type` toolchain if it
32+
was registered even though the pre-compilation was not enabled. This has been
33+
fixed to not be the case anymore. Fixes [#1967](https://github.com/bazelbuild/rules_python/issues/1967).
3234

3335
### Removed
3436
* Nothing yet

python/config_settings/BUILD.bazel

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
load("@bazel_skylib//lib:selects.bzl", "selects")
12
load("@bazel_skylib//rules:common_settings.bzl", "string_flag")
23
load(
34
"//python/private:flags.bzl",
@@ -128,3 +129,45 @@ string_flag(
128129
)
129130
for flag in INTERNAL_FLAGS
130131
]
132+
133+
config_setting(
134+
name = "_precompile_enabled",
135+
flag_values = {
136+
":precompile": PrecompileFlag.ENABLED,
137+
},
138+
# Only public because it is used in toolchain definition
139+
visibility = ["//visibility:public"],
140+
)
141+
142+
config_setting(
143+
name = "_precompile_enabled_generated_source",
144+
flag_values = {
145+
":precompile": PrecompileFlag.IF_GENERATED_SOURCE,
146+
},
147+
# Only public because it is used in toolchain definition
148+
visibility = ["//visibility:public"],
149+
)
150+
151+
config_setting(
152+
name = "_precompile_enabled_force",
153+
flag_values = {
154+
":precompile": PrecompileFlag.FORCE_ENABLED,
155+
},
156+
# Only public because it is used in toolchain definition
157+
visibility = ["//visibility:public"],
158+
)
159+
160+
# FIXME @aignas 2024-06-14: this currently does not handle the `auto` value of
161+
# the flag correctly. Right now the behaviour is to disable the pre-compilation
162+
# via the _precompile_flag_get_effective_value function default to disabled if
163+
# it the flag is "AUTO", however we may need to revisit this.
164+
selects.config_setting_group(
165+
name = "is_precompile_enabled",
166+
match_any = [
167+
":_precompile_enabled",
168+
":_precompile_enabled_force",
169+
":_precompile_enabled_generated_source",
170+
],
171+
# Only public because it is used in toolchain definition
172+
visibility = ["//visibility:public"],
173+
)

python/private/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ bzl_library(
5959
name = "autodetecting_toolchain_bzl",
6060
srcs = ["autodetecting_toolchain.bzl"],
6161
deps = [
62+
":toolchain_types_bzl",
6263
"//python:py_runtime_bzl",
6364
"//python:py_runtime_pair_bzl",
6465
],

python/private/autodetecting_toolchain.bzl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
load("//python:py_runtime.bzl", "py_runtime")
1818
load("//python:py_runtime_pair.bzl", "py_runtime_pair")
19+
load("//python/private:toolchain_types.bzl", "TARGET_TOOLCHAIN_TYPE")
1920

2021
def define_autodetecting_toolchain(name):
2122
"""Defines the autodetecting Python toolchain.
@@ -65,6 +66,6 @@ def define_autodetecting_toolchain(name):
6566
native.toolchain(
6667
name = name,
6768
toolchain = ":_autodetecting_py_runtime_pair",
68-
toolchain_type = ":toolchain_type",
69+
toolchain_type = TARGET_TOOLCHAIN_TYPE,
6970
visibility = ["//visibility:public"],
7071
)

python/private/flags.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ BootstrapImplFlag = enum(
3434
def _precompile_flag_get_effective_value(ctx):
3535
value = ctx.attr._precompile_flag[BuildSettingInfo].value
3636
if value == PrecompileFlag.AUTO:
37-
value = PrecompileFlag.DISABLED
37+
value = PrecompileFlag.IF_GENERATED_SOURCE
3838
return value
3939

4040
# Determines if Python source files should be compiled at build time.

python/private/py_toolchain_suite.bzl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,10 @@ def py_toolchain_suite(*, prefix, user_repository_name, python_version, set_pyth
107107
),
108108
toolchain_type = EXEC_TOOLS_TOOLCHAIN_TYPE,
109109
# The target settings capture the Python version
110-
target_settings = target_settings,
110+
target_settings = select({
111+
Label("//python/config_settings:is_precompile_enabled"): target_settings,
112+
"//conditions:default": [Label("//python/config_settings:is_precompile_enabled")],
113+
}),
111114
exec_compatible_with = kwargs.get("target_compatible_with"),
112115
)
113116

0 commit comments

Comments
 (0)