Skip to content

Commit e7c306d

Browse files
authored
feat(musl): add musl toolchain (#2402)
With this and the previously landed #2406 users can use this in `sdist` or `whl`-only setups alike. The PR itself is very simple and just adds `musl` toolchains. Whilst at it also move the `WhlLibc` flag out of the `pypi` namespace since it is also used for `toolchain` matching now. Fixes #1211
1 parent 0e9f97d commit e7c306d

File tree

7 files changed

+49
-19
lines changed

7 files changed

+49
-19
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ Other changes:
9090
`RULES_PYTHON_REPO_TOOLCHAIN_{VERSION}_{OS}_{ARCH}` env variable setting. For
9191
example, this allows one to use `freethreaded` python interpreter in the
9292
`repository_rule` to build a wheel from `sdist`.
93+
* (toolchain) The python interpreters targeting `muslc` libc have been added
94+
for the latest toolchain versions for each minor Python version. You can control
95+
the toolchain selection by using the
96+
{bzl:obj}`//python/config_settings:py_linux_libc` build flag.
9397

9498
{#v0-0-0-removed}
9599
### Removed

docs/toolchains.md

+7
Original file line numberDiff line numberDiff line change
@@ -451,3 +451,10 @@ The toolchain() calls should be in a separate BUILD file from everything else.
451451
This avoids Bazel having to perform unnecessary work when it discovers the list
452452
of available toolchains.
453453
:::
454+
455+
## Toolchain selection flags
456+
457+
Currently the following flags are used to influence toolchain selection:
458+
* {obj}`--@rules_python//python/config_settings:py_linux_libc` for selecting the Linux libc variant.
459+
* {obj}`--@rules_python//python/config_settings:py_freethreaded` for selecting
460+
the freethreaded experimental Python builds available from `3.13.0` onwards.

python/config_settings/BUILD.bazel

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ load(
66
"BootstrapImplFlag",
77
"ExecToolsToolchainFlag",
88
"FreeThreadedFlag",
9+
"LibcFlag",
910
"PrecompileFlag",
1011
"PrecompileSourceRetentionFlag",
1112
)
1213
load(
1314
"//python/private/pypi:flags.bzl",
1415
"UniversalWhlFlag",
1516
"UseWhlFlag",
16-
"WhlLibcFlag",
1717
"define_pypi_internal_flags",
1818
)
1919
load(":config_settings.bzl", "construct_config_settings")
@@ -87,8 +87,8 @@ string_flag(
8787
# This is used for pip and hermetic toolchain resolution.
8888
string_flag(
8989
name = "py_linux_libc",
90-
build_setting_default = WhlLibcFlag.GLIBC,
91-
values = sorted(WhlLibcFlag.__members__.values()),
90+
build_setting_default = LibcFlag.GLIBC,
91+
values = LibcFlag.flag_values(),
9292
# NOTE: Only public because it is used in pip hub and toolchain repos.
9393
visibility = ["//visibility:public"],
9494
)

python/private/flags.bzl

+11
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,14 @@ FreeThreadedFlag = enum(
132132
# Do not use freethreaded python toolchain and wheels.
133133
NO = "no",
134134
)
135+
136+
# Determines which libc flavor is preferred when selecting the toolchain and
137+
# linux whl distributions.
138+
#
139+
# buildifier: disable=name-conventions
140+
LibcFlag = FlagEnum(
141+
# Prefer glibc wheels (e.g. manylinux_2_17_x86_64 or linux_x86_64)
142+
GLIBC = "glibc",
143+
# Prefer musl wheels (e.g. musllinux_2_17_x86_64)
144+
MUSL = "musl",
145+
)

python/private/pypi/config_settings.bzl

+7-6
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ Note, that here the specialization of musl vs manylinux wheels is the same in
3939
order to ensure that the matching fails if the user requests for `musl` and we don't have it or vice versa.
4040
"""
4141

42-
load(":flags.bzl", "INTERNAL_FLAGS", "UniversalWhlFlag", "WhlLibcFlag")
42+
load("//python/private:flags.bzl", "LibcFlag")
43+
load(":flags.bzl", "INTERNAL_FLAGS", "UniversalWhlFlag")
4344

4445
FLAGS = struct(
4546
**{
@@ -251,14 +252,14 @@ def _plat_flag_values(os, cpu, osx_versions, glibc_versions, muslc_versions):
251252

252253
elif os == "linux":
253254
for os_prefix, linux_libc in {
254-
os: WhlLibcFlag.GLIBC,
255-
"many" + os: WhlLibcFlag.GLIBC,
256-
"musl" + os: WhlLibcFlag.MUSL,
255+
os: LibcFlag.GLIBC,
256+
"many" + os: LibcFlag.GLIBC,
257+
"musl" + os: LibcFlag.MUSL,
257258
}.items():
258-
if linux_libc == WhlLibcFlag.GLIBC:
259+
if linux_libc == LibcFlag.GLIBC:
259260
libc_versions = glibc_versions
260261
libc_flag = FLAGS.pip_whl_glibc_version
261-
elif linux_libc == WhlLibcFlag.MUSL:
262+
elif linux_libc == LibcFlag.MUSL:
262263
libc_versions = muslc_versions
263264
libc_flag = FLAGS.pip_whl_muslc_version
264265
else:

python/private/pypi/flags.bzl

-10
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,6 @@ UniversalWhlFlag = enum(
4444
UNIVERSAL = "universal",
4545
)
4646

47-
# Determines which libc flavor is preferred when selecting the linux whl distributions.
48-
#
49-
# buildifier: disable=name-conventions
50-
WhlLibcFlag = enum(
51-
# Prefer glibc wheels (e.g. manylinux_2_17_x86_64 or linux_x86_64)
52-
GLIBC = "glibc",
53-
# Prefer musl wheels (e.g. musllinux_2_17_x86_64)
54-
MUSL = "musl",
55-
)
56-
5747
INTERNAL_FLAGS = [
5848
"dist",
5949
"whl_plat",

python/versions.bzl

+17
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ TOOL_VERSIONS = {
248248
"x86_64-apple-darwin": "193dc7f0284e4917d52b17a077924474882ee172872f2257cfe3375d6d468ed9",
249249
"x86_64-pc-windows-msvc": "5069008a237b90f6f7a86956903f2a0221b90d471daa6e4a94831eaa399e3993",
250250
"x86_64-unknown-linux-gnu": "c20ee831f7f46c58fa57919b75a40eb2b6a31e03fd29aaa4e8dab4b9c4b60d5d",
251+
"x86_64-unknown-linux-musl": "5c1cc348e317fe7af1acd6a7f665b46eccb554b20d6533f0e76c53f44d4556cc",
251252
},
252253
"strip_prefix": "python",
253254
},
@@ -367,6 +368,7 @@ TOOL_VERSIONS = {
367368
"x86_64-apple-darwin": "90b46dfb1abd98d45663c7a2a8c45d3047a59391d8586d71b459cec7b75f662b",
368369
"x86_64-pc-windows-msvc": "e48952619796c66ec9719867b87be97edca791c2ef7fbf87d42c417c3331609e",
369370
"x86_64-unknown-linux-gnu": "3db2171e03c1a7acdc599fba583c1b92306d3788b375c9323077367af1e9d9de",
371+
"x86_64-unknown-linux-musl": "ed519c47d9620eb916a6f95ec2875396e7b1a9ab993ee40b2f31b837733f318c",
370372
},
371373
"strip_prefix": "python",
372374
},
@@ -481,6 +483,7 @@ TOOL_VERSIONS = {
481483
"x86_64-apple-darwin": "1e23ffe5bc473e1323ab8f51464da62d77399afb423babf67f8e13c82b69c674",
482484
"x86_64-pc-windows-msvc": "647b66ff4552e70aec3bf634dd470891b4a2b291e8e8715b3bdb162f577d4c55",
483485
"x86_64-unknown-linux-gnu": "8b50a442b04724a24c1eebb65a36a0c0e833d35374dbdf9c9470d8a97b164cd9",
486+
"x86_64-unknown-linux-musl": "d36fc77a8dd76155a7530f6235999a693b9e7c48aa11afeb5610a091cae5aa6f",
484487
},
485488
"strip_prefix": "python",
486489
},
@@ -559,6 +562,7 @@ TOOL_VERSIONS = {
559562
"x86_64-apple-darwin": "60c5271e7edc3c2ab47440b7abf4ed50fbc693880b474f74f05768f5b657045a",
560563
"x86_64-pc-windows-msvc": "f05531bff16fa77b53be0776587b97b466070e768e6d5920894de988bdcd547a",
561564
"x86_64-unknown-linux-gnu": "43576f7db1033dd57b900307f09c2e86f371152ac8a2607133afa51cbfc36064",
565+
"x86_64-unknown-linux-musl": "5ed4a4078db3cbac563af66403aaa156cd6e48831d90382a1820db2b120627b5",
562566
},
563567
"strip_prefix": "python",
564568
},
@@ -572,6 +576,7 @@ TOOL_VERSIONS = {
572576
"x86_64-apple-darwin": "cff1b7e7cd26f2d47acac1ad6590e27d29829776f77e8afa067e9419f2f6ce77",
573577
"x86_64-pc-windows-msvc": "b25926e8ce4164cf103bacc4f4d154894ea53e07dd3fdd5ebb16fb1a82a7b1a0",
574578
"x86_64-unknown-linux-gnu": "2c8cb15c6a2caadaa98af51df6fe78a8155b8471cb3dd7b9836038e0d3657fb4",
579+
"x86_64-unknown-linux-musl": "2f61ee3b628a56aceea63b46c7afe2df3e22a61da706606b0c8efda57f953cf4",
575580
"aarch64-apple-darwin-freethreaded": "efc2e71c0e05bc5bedb7a846e05f28dd26491b1744ded35ed82f8b49ccfa684b",
576581
"aarch64-unknown-linux-gnu-freethreaded": "59b50df9826475d24bb7eff781fa3949112b5e9c92adb29e96a09cdf1216d5bd",
577582
"ppc64le-unknown-linux-gnu-freethreaded": "1217efa5f4ce67fcc9f7eb64165b1bd0912b2a21bc25c1a7e2cb174a21a5df7e",
@@ -588,6 +593,7 @@ TOOL_VERSIONS = {
588593
"x86_64-apple-darwin": "python",
589594
"x86_64-pc-windows-msvc": "python",
590595
"x86_64-unknown-linux-gnu": "python",
596+
"x86_64-unknown-linux-musl": "python",
591597
"aarch64-apple-darwin-freethreaded": "python/install",
592598
"aarch64-unknown-linux-gnu-freethreaded": "python/install",
593599
"ppc64le-unknown-linux-gnu-freethreaded": "python/install",
@@ -727,6 +733,17 @@ def _generate_platforms():
727733
# Matches the value in @platforms//cpu package
728734
arch = "x86_64",
729735
),
736+
"x86_64-unknown-linux-musl": struct(
737+
compatible_with = [
738+
"@platforms//os:linux",
739+
"@platforms//cpu:x86_64",
740+
],
741+
flag_values = {
742+
libc: "musl",
743+
},
744+
os_name = LINUX_NAME,
745+
arch = "x86_64",
746+
),
730747
}
731748

732749
freethreaded = Label("//python/config_settings:py_freethreaded")

0 commit comments

Comments
 (0)