Skip to content

Commit c3dc7b0

Browse files
committed
feat(toolchain): use py_linux_libc flag value
With this we start using the `py_linux_libc` flag value in toolchains as well and ensure that no hermetic toolchain will be matched when we specify to pull the `musl` wheels. In that case the user will need to supply their own toolchain that is the expected behaviour. However, I am not sure how we could fail due to no available toolchain in that case because the autodetecting toolchain will just use the host interpreter.
1 parent 2d20670 commit c3dc7b0

File tree

4 files changed

+76
-14
lines changed

4 files changed

+76
-14
lines changed

python/private/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,8 @@ bzl_library(
252252
name = "py_toolchain_suite_bzl",
253253
srcs = ["py_toolchain_suite.bzl"],
254254
deps = [
255+
":config_settings_bzl",
256+
":text_util_bzl",
255257
":toolchain_types_bzl",
256258
"@bazel_skylib//lib:selects",
257259
],

python/private/py_toolchain_suite.bzl

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,23 @@
1515
"""Create the toolchain defs in a BUILD.bazel file."""
1616

1717
load("@bazel_skylib//lib:selects.bzl", "selects")
18+
load(":config_settings.bzl", "is_python_config_setting")
1819
load(
1920
":toolchain_types.bzl",
2021
"EXEC_TOOLS_TOOLCHAIN_TYPE",
2122
"PY_CC_TOOLCHAIN_TYPE",
2223
"TARGET_TOOLCHAIN_TYPE",
2324
)
2425

25-
def py_toolchain_suite(*, prefix, user_repository_name, python_version, set_python_version_constraint, **kwargs):
26+
def py_toolchain_suite(*, prefix, user_repository_name, python_version, set_python_version_constraint, flag_values, **kwargs):
2627
"""For internal use only.
2728
2829
Args:
2930
prefix: Prefix for toolchain target names.
3031
user_repository_name: The name of the user repository.
3132
python_version: The full (X.Y.Z) version of the interpreter.
3233
set_python_version_constraint: True or False as a string.
34+
flag_values: Extra flag values to match for this toolchain.
3335
**kwargs: extra args passed to the `toolchain` calls.
3436
3537
"""
@@ -38,23 +40,54 @@ def py_toolchain_suite(*, prefix, user_repository_name, python_version, set_pyth
3840
# string as we cannot have list of bools in build rule attribues.
3941
# This if statement does not appear to work unless it is in the
4042
# toolchain file.
41-
if set_python_version_constraint == "True":
43+
if set_python_version_constraint in ["True", "False"]:
4244
major_minor, _, _ = python_version.rpartition(".")
4345

46+
match_any = []
47+
for i, v in enumerate([major_minor, python_version]):
48+
name = "{prefix}_{python_version}_{i}".format(
49+
prefix = prefix,
50+
python_version = python_version,
51+
i = i,
52+
)
53+
match_any.append(name)
54+
if flag_values:
55+
is_python_config_setting(
56+
name = name,
57+
python_version = v,
58+
flag_values = flag_values,
59+
visibility = ["//visibility:private"],
60+
)
61+
else:
62+
native.alias(
63+
name = name,
64+
actual = Label("//python/config_settings:is_python_%s" % v),
65+
visibility = ["//visibility:private"],
66+
)
67+
68+
if set_python_version_constraint == "False":
69+
name = "{prefix}_version_setting_no_python_version".format(prefix = prefix)
70+
match_any.append(name)
71+
native.config_setting(
72+
name = name,
73+
flag_values = flag_values | {
74+
str(Label("//python/config_settings:python_version")): "",
75+
},
76+
visibility = ["//visibility:private"],
77+
)
78+
79+
name = "{prefix}_version_setting_{python_version}".format(
80+
prefix = prefix,
81+
python_version = python_version,
82+
visibility = ["//visibility:private"],
83+
)
4484
selects.config_setting_group(
45-
name = prefix + "_version_setting",
46-
match_any = [
47-
Label("//python/config_settings:is_python_%s" % v)
48-
for v in [
49-
major_minor,
50-
python_version,
51-
]
52-
],
85+
name = name,
86+
match_any = match_any,
5387
visibility = ["//visibility:private"],
5488
)
55-
target_settings = [prefix + "_version_setting"]
56-
elif set_python_version_constraint == "False":
57-
target_settings = []
89+
90+
target_settings = [name]
5891
else:
5992
fail(("Invalid set_python_version_constraint value: got {} {}, wanted " +
6093
"either the string 'True' or the string 'False'; " +

python/private/toolchains_repo.bzl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ load(
3131
"WINDOWS_NAME",
3232
)
3333
load("//python/private:repo_utils.bzl", "REPO_DEBUG_ENV_VAR", "repo_utils")
34+
load("//python/private:text_util.bzl", "render")
3435

3536
def get_repository_name(repository_workspace):
3637
dummy_label = "//:_"
@@ -64,10 +65,15 @@ py_toolchain_suite(
6465
user_repository_name = "{user_repository_name}_{platform}",
6566
prefix = "{prefix}{platform}",
6667
target_compatible_with = {compatible_with},
68+
flag_values = {flag_values},
6769
python_version = "{python_version}",
6870
set_python_version_constraint = "{set_python_version_constraint}",
6971
)""".format(
70-
compatible_with = meta.compatible_with,
72+
compatible_with = render.indent(render.list(meta.compatible_with)).lstrip(),
73+
flag_values = render.indent(render.dict(
74+
meta.flag_values,
75+
key_repr = lambda x: repr(str(x)), # this is to correctly display labels
76+
)).lstrip(),
7177
platform = platform,
7278
set_python_version_constraint = set_python_version_constraint,
7379
user_repository_name = user_repository_name,

python/versions.bzl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,7 @@ PLATFORMS = {
501501
"@platforms//os:macos",
502502
"@platforms//cpu:aarch64",
503503
],
504+
flag_values = {},
504505
os_name = MACOS_NAME,
505506
# Matches the value returned from:
506507
# repository_ctx.execute(["uname", "-m"]).stdout.strip()
@@ -511,6 +512,9 @@ PLATFORMS = {
511512
"@platforms//os:linux",
512513
"@platforms//cpu:aarch64",
513514
],
515+
flag_values = {
516+
Label("//python/config_settings:py_linux_libc"): "glibc",
517+
},
514518
os_name = LINUX_NAME,
515519
# Note: this string differs between OSX and Linux
516520
# Matches the value returned from:
@@ -522,6 +526,9 @@ PLATFORMS = {
522526
"@platforms//os:linux",
523527
"@platforms//cpu:armv7",
524528
],
529+
flag_values = {
530+
Label("//python/config_settings:py_linux_libc"): "glibc",
531+
},
525532
os_name = LINUX_NAME,
526533
arch = "armv7",
527534
),
@@ -530,6 +537,9 @@ PLATFORMS = {
530537
"@platforms//os:linux",
531538
"@platforms//cpu:ppc",
532539
],
540+
flag_values = {
541+
Label("//python/config_settings:py_linux_libc"): "glibc",
542+
},
533543
os_name = LINUX_NAME,
534544
# Note: this string differs between OSX and Linux
535545
# Matches the value returned from:
@@ -541,6 +551,9 @@ PLATFORMS = {
541551
"@platforms//os:linux",
542552
"@platforms//cpu:riscv64",
543553
],
554+
flag_values = {
555+
Label("//python/config_settings:py_linux_libc"): "glibc",
556+
},
544557
os_name = LINUX_NAME,
545558
arch = "riscv64",
546559
),
@@ -549,6 +562,9 @@ PLATFORMS = {
549562
"@platforms//os:linux",
550563
"@platforms//cpu:s390x",
551564
],
565+
flag_values = {
566+
Label("//python/config_settings:py_linux_libc"): "glibc",
567+
},
552568
os_name = LINUX_NAME,
553569
# Note: this string differs between OSX and Linux
554570
# Matches the value returned from:
@@ -560,6 +576,7 @@ PLATFORMS = {
560576
"@platforms//os:macos",
561577
"@platforms//cpu:x86_64",
562578
],
579+
flag_values = {},
563580
os_name = MACOS_NAME,
564581
arch = "x86_64",
565582
),
@@ -568,6 +585,7 @@ PLATFORMS = {
568585
"@platforms//os:windows",
569586
"@platforms//cpu:x86_64",
570587
],
588+
flag_values = {},
571589
os_name = WINDOWS_NAME,
572590
arch = "x86_64",
573591
),
@@ -576,6 +594,9 @@ PLATFORMS = {
576594
"@platforms//os:linux",
577595
"@platforms//cpu:x86_64",
578596
],
597+
flag_values = {
598+
Label("//python/config_settings:py_linux_libc"): "glibc",
599+
},
579600
os_name = LINUX_NAME,
580601
arch = "x86_64",
581602
),

0 commit comments

Comments
 (0)