Skip to content

Commit 107a878

Browse files
aignasrickeylev
andauthored
fix: use platform_info.target_settings in toolchain aliases (bazel-contrib#3001)
During the refactor we forgot one more place where the `flag_values` on the platform information was used. They were no longer populated and broke. The solution is to use `selects.config_setting_group` to maintain behaviour and in order to smoke test I have added a target to verify that the aliases work. Related to bazel-contrib#2875 Fixes bazel-contrib#2993 Co-authored-by: Richard Levasseur <[email protected]>
1 parent be86f4a commit 107a878

File tree

6 files changed

+47
-28
lines changed

6 files changed

+47
-28
lines changed

python/config_settings/BUILD.bazel

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,19 @@ string_flag(
125125
visibility = ["//visibility:public"],
126126
)
127127

128-
config_setting(
128+
alias(
129129
name = "is_py_freethreaded",
130-
flag_values = {":py_freethreaded": FreeThreadedFlag.YES},
130+
actual = ":_is_py_freethreaded_yes",
131+
deprecation = "not actually public, please create your own config_setting using the flag that rules_python exposes",
132+
tags = ["manual"],
131133
visibility = ["//visibility:public"],
132134
)
133135

134-
config_setting(
136+
alias(
135137
name = "is_py_non_freethreaded",
136-
flag_values = {":py_freethreaded": FreeThreadedFlag.NO},
138+
actual = ":_is_py_freethreaded_no",
139+
deprecation = "not actually public, please create your own config_setting using the flag that rules_python exposes",
140+
tags = ["manual"],
137141
visibility = ["//visibility:public"],
138142
)
139143

python/private/config_settings.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def construct_config_settings(*, name, default_version, versions, minor_mapping,
143143
)
144144
native.config_setting(
145145
name = "_is_py_linux_libc_musl",
146-
flag_values = {libc: "glibc"},
146+
flag_values = {libc: "musl"},
147147
visibility = _NOT_ACTUALLY_PUBLIC,
148148
)
149149
freethreaded = Label("//python/config_settings:py_freethreaded")

python/private/hermetic_runtime_repo_setup.bzl

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ load(":glob_excludes.bzl", "glob_excludes")
2222
load(":py_exec_tools_toolchain.bzl", "py_exec_tools_toolchain")
2323
load(":version.bzl", "version")
2424

25-
_IS_FREETHREADED = Label("//python/config_settings:is_py_freethreaded")
25+
_IS_FREETHREADED_YES = Label("//python/config_settings:_is_py_freethreaded_yes")
26+
_IS_FREETHREADED_NO = Label("//python/config_settings:_is_py_freethreaded_no")
2627

2728
def define_hermetic_runtime_toolchain_impl(
2829
*,
@@ -87,16 +88,16 @@ def define_hermetic_runtime_toolchain_impl(
8788
cc_import(
8889
name = "interface",
8990
interface_library = select({
90-
_IS_FREETHREADED: "libs/python{major}{minor}t.lib".format(**version_dict),
91-
"//conditions:default": "libs/python{major}{minor}.lib".format(**version_dict),
91+
_IS_FREETHREADED_YES: "libs/python{major}{minor}t.lib".format(**version_dict),
92+
_IS_FREETHREADED_NO: "libs/python{major}{minor}.lib".format(**version_dict),
9293
}),
9394
system_provided = True,
9495
)
9596
cc_import(
9697
name = "abi3_interface",
9798
interface_library = select({
98-
_IS_FREETHREADED: "libs/python3t.lib",
99-
"//conditions:default": "libs/python3.lib",
99+
_IS_FREETHREADED_YES: "libs/python3t.lib",
100+
_IS_FREETHREADED_NO: "libs/python3.lib",
100101
}),
101102
system_provided = True,
102103
)
@@ -115,10 +116,10 @@ def define_hermetic_runtime_toolchain_impl(
115116
includes = [
116117
"include",
117118
] + select({
118-
_IS_FREETHREADED: [
119+
_IS_FREETHREADED_YES: [
119120
"include/python{major}.{minor}t".format(**version_dict),
120121
],
121-
"//conditions:default": [
122+
_IS_FREETHREADED_NO: [
122123
"include/python{major}.{minor}".format(**version_dict),
123124
"include/python{major}.{minor}m".format(**version_dict),
124125
],
@@ -224,8 +225,8 @@ def define_hermetic_runtime_toolchain_impl(
224225
implementation_name = "cpython",
225226
# See https://peps.python.org/pep-3147/ for pyc tag infix format
226227
pyc_tag = select({
227-
_IS_FREETHREADED: "cpython-{major}{minor}t".format(**version_dict),
228-
"//conditions:default": "cpython-{major}{minor}".format(**version_dict),
228+
_IS_FREETHREADED_YES: "cpython-{major}{minor}t".format(**version_dict),
229+
_IS_FREETHREADED_NO: "cpython-{major}{minor}".format(**version_dict),
229230
}),
230231
)
231232

python/private/pypi/config_settings.bzl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ FLAGS = struct(
8080
"is_pip_whl_auto",
8181
"is_pip_whl_no",
8282
"is_pip_whl_only",
83-
"is_py_freethreaded",
84-
"is_py_non_freethreaded",
83+
"_is_py_freethreaded_yes",
84+
"_is_py_freethreaded_no",
8585
"pip_whl_glibc_version",
8686
"pip_whl_muslc_version",
8787
"pip_whl_osx_arch",
@@ -205,12 +205,12 @@ def _dist_config_settings(*, suffix, plat_flag_values, python_version, **kwargs)
205205
for name, f, compatible_with in [
206206
("py_none", _flags.whl, None),
207207
("py3_none", _flags.whl_py3, None),
208-
("py3_abi3", _flags.whl_py3_abi3, (FLAGS.is_py_non_freethreaded,)),
208+
("py3_abi3", _flags.whl_py3_abi3, (FLAGS._is_py_freethreaded_no,)),
209209
("none", _flags.whl_pycp3x, None),
210-
("abi3", _flags.whl_pycp3x_abi3, (FLAGS.is_py_non_freethreaded,)),
210+
("abi3", _flags.whl_pycp3x_abi3, (FLAGS._is_py_freethreaded_no,)),
211211
# The below are not specializations of one another, they are variants
212-
(cpv, _flags.whl_pycp3x_abicp, (FLAGS.is_py_non_freethreaded,)),
213-
(cpv + "t", _flags.whl_pycp3x_abicp, (FLAGS.is_py_freethreaded,)),
212+
(cpv, _flags.whl_pycp3x_abicp, (FLAGS._is_py_freethreaded_no,)),
213+
(cpv + "t", _flags.whl_pycp3x_abicp, (FLAGS._is_py_freethreaded_yes,)),
214214
]:
215215
if (f, compatible_with) in used_flags:
216216
# This should never happen as all of the different whls should have
@@ -237,12 +237,12 @@ def _dist_config_settings(*, suffix, plat_flag_values, python_version, **kwargs)
237237
for name, f, compatible_with in [
238238
("py_none", _flags.whl_plat, None),
239239
("py3_none", _flags.whl_plat_py3, None),
240-
("py3_abi3", _flags.whl_plat_py3_abi3, (FLAGS.is_py_non_freethreaded,)),
240+
("py3_abi3", _flags.whl_plat_py3_abi3, (FLAGS._is_py_freethreaded_no,)),
241241
("none", _flags.whl_plat_pycp3x, None),
242-
("abi3", _flags.whl_plat_pycp3x_abi3, (FLAGS.is_py_non_freethreaded,)),
242+
("abi3", _flags.whl_plat_pycp3x_abi3, (FLAGS._is_py_freethreaded_no,)),
243243
# The below are not specializations of one another, they are variants
244-
(cpv, _flags.whl_plat_pycp3x_abicp, (FLAGS.is_py_non_freethreaded,)),
245-
(cpv + "t", _flags.whl_plat_pycp3x_abicp, (FLAGS.is_py_freethreaded,)),
244+
(cpv, _flags.whl_plat_pycp3x_abicp, (FLAGS._is_py_freethreaded_no,)),
245+
(cpv + "t", _flags.whl_plat_pycp3x_abicp, (FLAGS._is_py_freethreaded_yes,)),
246246
]:
247247
if (f, compatible_with) in used_flags:
248248
# This should never happen as all of the different whls should have
@@ -329,7 +329,7 @@ def _dist_config_setting(*, name, compatible_with = None, native = native, **kwa
329329
compatible_with: {type}`tuple[Label]` A collection of config settings that are
330330
compatible with the given dist config setting. For example, if only
331331
non-freethreaded python builds are allowed, add
332-
FLAGS.is_py_non_freethreaded here.
332+
FLAGS._is_py_freethreaded_no here.
333333
native (struct): The struct containing alias and config_setting rules
334334
to use for creating the objects. Can be overridden for unit tests
335335
reasons.

python/private/toolchain_aliases.bzl

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414

1515
"""Create toolchain alias targets."""
1616

17-
load("@rules_python//python:versions.bzl", "PLATFORMS")
17+
load("@bazel_skylib//lib:selects.bzl", "selects")
18+
load("//python:versions.bzl", "PLATFORMS")
1819

1920
def toolchain_aliases(*, name, platforms, visibility = None, native = native):
2021
"""Create toolchain aliases for the python toolchains.
@@ -30,12 +31,17 @@ def toolchain_aliases(*, name, platforms, visibility = None, native = native):
3031
if platform not in platforms:
3132
continue
3233

34+
_platform = "_" + platform
3335
native.config_setting(
34-
name = platform,
35-
flag_values = PLATFORMS[platform].flag_values,
36+
name = _platform,
3637
constraint_values = PLATFORMS[platform].compatible_with,
3738
visibility = ["//visibility:private"],
3839
)
40+
selects.config_setting_group(
41+
name = platform,
42+
match_all = PLATFORMS[platform].target_settings + [_platform],
43+
visibility = ["//visibility:private"],
44+
)
3945

4046
prefix = name
4147
for name in [

tests/toolchains/BUILD.bazel

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
load("@bazel_skylib//rules:build_test.bzl", "build_test")
1516
load("//python/private:bzlmod_enabled.bzl", "BZLMOD_ENABLED") # buildifier: disable=bzl-visibility
1617
load("//tests/support:sh_py_run_test.bzl", "py_reconfig_test")
1718
load(":defs.bzl", "define_toolchain_tests")
@@ -30,3 +31,10 @@ py_reconfig_test(
3031
"@platforms//cpu:x86_64",
3132
] if BZLMOD_ENABLED else ["@platforms//:incompatible"],
3233
)
34+
35+
build_test(
36+
name = "build_test",
37+
targets = [
38+
"@python_3_11//:python_headers",
39+
],
40+
)

0 commit comments

Comments
 (0)