Skip to content

Commit 8f95d1c

Browse files
committed
fix(whl_library): correctly parse wheel target platforms
The bazel-contrib#1693 PR incorrectly assumed that the platform tag will be os-arch specific if and only if the abi tag is of form cpxy. However, there are many wheels that are not like this (e.g. watchdog, tornado, libclang). This fixes the starlark code that is overriding the user platforms with something that only the wheel supports by also taking into account the ABI. Fixes bazel-contrib#1810.
1 parent bdb2aa2 commit 8f95d1c

File tree

4 files changed

+64
-17
lines changed

4 files changed

+64
-17
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ A brief description of the categories of changes:
2323

2424
### Fixed
2525

26+
* (whl_library): Fix the experimental_target_platforms overriding for platform
27+
specific wheels when the wheels are for any python interpreter version. Fixes
28+
[#1810](https://github.com/bazelbuild/rules_python/issues/1810).
29+
2630
### Added
2731

2832
* New Python versions available: `3.11.8`, `3.12.2` using

python/pip_install/pip_repository.bzl

+5-2
Original file line numberDiff line numberDiff line change
@@ -801,8 +801,11 @@ def _whl_library_impl(rctx):
801801
# NOTE @aignas 2023-12-04: if the wheel is a platform specific
802802
# wheel, we only include deps for that target platform
803803
target_platforms = [
804-
"{}_{}_{}".format(parsed_whl.abi_tag, p.os, p.cpu)
805-
for p in whl_target_platforms(parsed_whl.platform_tag)
804+
p.target_platform
805+
for p in whl_target_platforms(
806+
parsed_whl.platform_tag,
807+
abi = parsed_whl.abi_tag,
808+
)
806809
]
807810

808811
repo_utils.execute_checked(

python/private/whl_target_platforms.bzl

+20-7
Original file line numberDiff line numberDiff line change
@@ -40,28 +40,41 @@ _OS_PREFIXES = {
4040
"win": "windows",
4141
} # buildifier: disable=unsorted-dict-items
4242

43-
def whl_target_platforms(tag):
44-
"""Parse the wheel platform tag and return (os, cpu) tuples.
43+
def whl_target_platforms(platform_tag, abi_tag = ""):
44+
"""Parse the wheel abi and platform tags and return (os, cpu) tuples.
4545
4646
Args:
47-
tag (str): The platform_tag part of the wheel name. See
47+
platform_tag (str): The platform_tag part of the wheel name. See
4848
./parse_whl_name.bzl for more details.
49+
abi_tag (str): The abi tag that should be used for parsing.
4950
5051
Returns:
5152
A list of structs, with attributes:
5253
* os: str, one of the _OS_PREFIXES values
5354
* cpu: str, one of the _CPU_PREFIXES values
55+
* abi: str, the ABI that the interpreter should have if it is passed.
56+
* target_platform: str, the target_platform that can be given to the
57+
wheel_installer for parsing whl METADATA.
5458
"""
55-
cpus = _cpu_from_tag(tag)
59+
cpus = _cpu_from_tag(platform_tag)
60+
61+
abi = None
62+
if abi_tag not in ["", "none", "abi3"]:
63+
abi = abi_tag
5664

5765
for prefix, os in _OS_PREFIXES.items():
58-
if tag.startswith(prefix):
66+
if platform_tag.startswith(prefix):
5967
return [
60-
struct(os = os, cpu = cpu)
68+
struct(
69+
os = os,
70+
cpu = cpu,
71+
abi = abi,
72+
target_platform = "_".join([abi, os, cpu] if abi else [os, cpu]),
73+
)
6174
for cpu in cpus
6275
]
6376

64-
fail("unknown tag os: {}".format(tag))
77+
fail("unknown platform_tag os: {}".format(platform_tag))
6578

6679
def _cpu_from_tag(tag):
6780
candidate = [

tests/private/whl_target_platforms/whl_target_platforms_tests.bzl

+35-8
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,56 @@ _tests = []
2222
def _test_simple(env):
2323
tests = {
2424
"macosx_10_9_arm64": [
25-
struct(os = "osx", cpu = "aarch64"),
25+
struct(os = "osx", cpu = "aarch64", abi = None, target_platform = "osx_aarch64"),
2626
],
2727
"macosx_10_9_universal2": [
28-
struct(os = "osx", cpu = "x86_64"),
29-
struct(os = "osx", cpu = "aarch64"),
28+
struct(os = "osx", cpu = "x86_64", abi = None, target_platform = "osx_x86_64"),
29+
struct(os = "osx", cpu = "aarch64", abi = None, target_platform = "osx_aarch64"),
3030
],
3131
"manylinux1_i686.manylinux_2_17_i686": [
32-
struct(os = "linux", cpu = "x86_32"),
32+
struct(os = "linux", cpu = "x86_32", abi = None, target_platform = "linux_x86_32"),
3333
],
3434
"musllinux_1_1_ppc64le": [
35-
struct(os = "linux", cpu = "ppc"),
35+
struct(os = "linux", cpu = "ppc", abi = None, target_platform = "linux_ppc"),
3636
],
3737
"win_amd64": [
38-
struct(os = "windows", cpu = "x86_64"),
38+
struct(os = "windows", cpu = "x86_64", abi = None, target_platform = "windows_x86_64"),
3939
],
4040
}
4141

4242
for give, want in tests.items():
43-
got = whl_target_platforms(give)
44-
env.expect.that_collection(got).contains_exactly(want)
43+
for abi in ["", "abi3", "none"]:
44+
got = whl_target_platforms(give, abi)
45+
env.expect.that_collection(got).contains_exactly(want)
4546

4647
_tests.append(_test_simple)
4748

49+
def _test_with_abi(env):
50+
tests = {
51+
"macosx_10_9_arm64": [
52+
struct(os = "osx", cpu = "aarch64", abi = "cp39", target_platform = "cp39_osx_aarch64"),
53+
],
54+
"macosx_10_9_universal2": [
55+
struct(os = "osx", cpu = "x86_64", abi = "cp310", target_platform = "cp310_osx_x86_64"),
56+
struct(os = "osx", cpu = "aarch64", abi = "cp310", target_platform = "cp310_osx_aarch64"),
57+
],
58+
"manylinux1_i686.manylinux_2_17_i686": [
59+
struct(os = "linux", cpu = "x86_32", abi = "cp38", target_platform = "cp38_linux_x86_32"),
60+
],
61+
"musllinux_1_1_ppc64le": [
62+
struct(os = "linux", cpu = "ppc", abi = "cp311", target_platform = "cp311_linux_ppc"),
63+
],
64+
"win_amd64": [
65+
struct(os = "windows", cpu = "x86_64", abi = "cp311", target_platform = "cp311_windows_x86_64"),
66+
],
67+
}
68+
69+
for give, want in tests.items():
70+
got = whl_target_platforms(give, want[0].abi)
71+
env.expect.that_collection(got).contains_exactly(want)
72+
73+
_tests.append(_test_with_abi)
74+
4875
def whl_target_platforms_test_suite(name):
4976
"""Create the test suite.
5077

0 commit comments

Comments
 (0)