Skip to content

Commit dac4b6b

Browse files
committed
add more logic and docs to here
1 parent ddcb1e3 commit dac4b6b

File tree

4 files changed

+124
-50
lines changed

4 files changed

+124
-50
lines changed

MODULE.bazel

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,31 +59,34 @@ register_toolchains("@pythons_hub//:all")
5959
# Let's define default platforms for our users first
6060
pip = use_extension("//python/extensions:pip.bzl", "pip")
6161

62-
_DEFAULT_LINUX_PLATFORM_VERSION = "0"
63-
64-
_DEFAULT_OSX_PLATFORM_VERSION = "14.0"
65-
66-
_DEFAULT_WINDOWS_PLATFORM_VERSION = "0"
67-
6862
[
6963
pip.default(
7064
arch_name = cpu,
7165
constraint_values = [
7266
"@platforms//os:linux",
7367
"@platforms//cpu:{}".format(cpu),
7468
],
75-
env_platform_version = _DEFAULT_LINUX_PLATFORM_VERSION,
69+
env_platform_version = "0",
7670
os_name = "linux",
7771
platform = "linux_{}".format(cpu),
72+
whl_platforms = [
73+
# this is by the order of preference.
74+
# TODO @aignas 2025-05-23: how do we specify the default libc version?
75+
"manylinux_*_{}".format(cpu),
76+
"linux_*_{}".format(cpu),
77+
"musllinux_*_{}".format(cpu),
78+
],
7879
)
79-
# TODO @aignas 2025-05-19: only leave tier 0-1 cpus when stabilizing the
80-
# `pip.default` extension
8180
for cpu in [
81+
"x86_64",
8282
"aarch64",
83+
# TODO @aignas 2025-05-19: only leave tier 0-1 cpus when stabilizing the
84+
# `pip.default` extension. i.e. drop the below values - users will have to
85+
# define themselves if they need them.
8386
"arm",
8487
"ppc",
88+
"ppc64le",
8589
"s390x",
86-
"x86_64",
8790
]
8891
]
8992

@@ -96,14 +99,26 @@ _DEFAULT_WINDOWS_PLATFORM_VERSION = "0"
9699
],
97100
# We choose the oldest non-EOL version at the time when we release `rules_python`.
98101
# See https://endoflife.date/macos
99-
env_platform_version = _DEFAULT_OSX_PLATFORM_VERSION,
102+
env_platform_version = "14.0",
100103
os_name = "osx",
101104
platform = "osx_{}".format(cpu),
105+
whl_platforms = [
106+
"macosx_*_*_{}".format(wheel_cpu)
107+
for wheel_cpu in wheel_cpu_values
108+
],
102109
)
103-
for cpu in [
104-
"aarch64",
105-
"x86_64",
106-
]
110+
for cpu, wheel_cpu_values in {
111+
# this is by the order of preference.
112+
"aarch64": [
113+
"arm64",
114+
"universal2",
115+
],
116+
"x86_64": [
117+
"x86_64",
118+
"universal2",
119+
"intel",
120+
],
121+
}.items()
107122
]
108123

109124
pip.default(
@@ -112,9 +127,10 @@ pip.default(
112127
"@platforms//os:windows",
113128
"@platforms//cpu:x86_64",
114129
],
115-
env_platform_version = _DEFAULT_WINDOWS_PLATFORM_VERSION,
130+
env_platform_version = "0",
116131
os_name = "windows",
117132
platform = "windows_x86_64",
133+
whl_platforms = ["win_amd64"],
118134
)
119135

120136
#####################

python/private/pypi/extension.bzl

Lines changed: 81 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -758,45 +758,90 @@ def _pip_impl(module_ctx):
758758
return None
759759

760760
_default_attrs = {
761-
"arch_name": attr.string(),
762-
"constraint_values": attr.label_list(),
763-
"os_name": attr.string(),
764-
"platform": attr.string(),
761+
"arch_name": attr.string(
762+
doc = """\
763+
The CPU architecture name to be used.
764+
765+
:::{note}
766+
Either this or {attr}`env_platform_machine` should be specified.
767+
:::
768+
""",
769+
),
770+
"constraint_values": attr.label_list(
771+
doc = """\
772+
The list of labels to {obj}`constraint_value` that will be used in the `hub repository` when
773+
including python packages that are compatible with this platform.
774+
""",
775+
),
776+
"os_name": attr.string(
777+
doc = """\
778+
The OS name to be used.
779+
780+
:::{note}
781+
Either this or the appropriate `env_*` attributes should be specified.
782+
:::
783+
""",
784+
),
785+
"platform": attr.string(
786+
doc = """\
787+
A platform identifier which will be used as the unique identifier within the extension evaluation.
788+
If you are defining custom platforms in your project and don't want things to clash, use extension
789+
[isolation] feature.
790+
791+
[isolation]: https://bazel.build/rules/lib/globals/module#use_extension.isolate
792+
""",
793+
),
765794
# TODO @aignas 2025-05-19: use the following
766795
"target_settings": attr.label_list(
767796
doc = """\
768-
A list of config_settings that must be satisfied by the target configuration in order for this
769-
platform to be matched during analysis phase.
797+
A list of labels to {obj}`config_setting` targets that must be satisfied by the target
798+
configuration in order for this platform to be matched during analysis phase.
799+
""",
800+
),
801+
"whl_limit": attr.int(
802+
default = 1,
803+
doc = """\
804+
The limit of wheels that we are going to include per platform.
805+
""",
806+
),
807+
"whl_platforms": attr.string_list(
808+
doc = """\
809+
The platform_tag values to consider as supported for the target platforms.
810+
811+
For `manylinux` and `musllinux` platform tags, please use the `manylinux_x_y_<arch>`
812+
or `musllinux_x_y_<arch>` syntax.
770813
""",
771814
),
772815
} | {
773816
# The values for PEP508 env marker evaluation during the lock file parsing
774-
"env_implementation_name": attr.string(),
775-
"env_os_name": attr.string(doc = "default will be inferred from {obj}`os_name`"),
776-
"env_platform_machine": attr.string(doc = "default will be inferred from {obj}`arch_name`"),
777-
"env_platform_release": attr.string(),
778-
"env_platform_system": attr.string(doc = "default will be inferred from {obj}`os_name`"),
779-
"env_platform_version": attr.string(),
780-
"env_sys_platform": attr.string(),
817+
"env_implementation_name": attr.string(
818+
doc = "Value for `implementation_name` to evaluate environment markers. Defaults to `cpython`.",
819+
),
820+
"env_os_name": attr.string(
821+
doc = "Value for `os_name` to evaluate environment markers. Defaults to a value inferred from the {attr}`os_name`.",
822+
),
823+
"env_platform_machine": attr.string(
824+
doc = "Value for `platform_machine` to evaluate environment markers. Defaults to a value inferred from the {attr}`arch_name`.",
825+
),
826+
"env_platform_release": attr.string(
827+
doc = "Value for `platform_machine` to evaluate environment markers. Defaults to an empty value.",
828+
),
829+
"env_platform_system": attr.string(
830+
doc = "Value for `platform_system` to evaluate environment markers. Defaults to a value inferred from the {attr}`os_name`.",
831+
),
832+
"env_platform_version": attr.string(
833+
doc = "Value for `platform_machine` to evaluate environment markers. Defaults to `0`.",
834+
),
835+
"env_sys_platform": attr.string(
836+
doc = "Value for `sys_platform` to evaluate environment markers. Defaults to a value inferred from the {attr}`os_name`.",
837+
),
781838
# TODO @aignas 2025-05-19: add wiring for the following
782839
} | AUTH_ATTRS | {
783840
# TODO @aignas 2025-05-19: add wiring for the following
784-
"extra_index_urls": attr.string_list(),
785-
"index_url": attr.string(),
786-
"index_url_overrides": attr.string_dict(),
787-
"simpleapi_skip": attr.string_list(
788-
doc = """\
789-
The list of packages to skip fetching metadata for from SimpleAPI index. You should
790-
normally not need this attribute, but in case you do, please report this as a bug
791-
to `rules_python` and use this attribute until the bug is fixed.
792-
793-
EXPERIMENTAL: this may be removed without notice.
794-
795-
:::{versionadded} 1.4.0
796-
:::
797-
""",
798-
),
799-
"whls_limit": attr.int(default = -1),
841+
# "extra_index_urls": attr.string_list(),
842+
# "index_url": attr.string(),
843+
# "index_url_overrides": attr.string_dict(),
844+
# "simpleapi_skip": attr.string_list(),
800845
}
801846

802847
# _configure_attrs = _default_attrs | {
@@ -1067,6 +1112,13 @@ the BUILD files for wheels.
10671112
This tag class allows for more customization of how the configuration for the hub repositories is built.
10681113
10691114
This is still experimental and may be changed or removed without any notice.
1115+
1116+
:::{seealso}
1117+
The [environment markers][environment_markers] specification for the explanation of the
1118+
terms used in this extension.
1119+
1120+
[environment_markers]: https://packaging.python.org/en/latest/specifications/dependency-specifiers/#environment-markers
1121+
:::
10701122
""",
10711123
),
10721124
"override": _override_tag,

python/private/pypi/parse_requirements.bzl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,10 @@ def _add_dists(*, requirement, index_urls, logger = None):
350350
]))
351351

352352
# Filter out the wheels that are incompatible with the target_platforms.
353-
whls = select_whls(whls = whls, want_platforms = requirement.target_platforms, logger = logger)
353+
whls = select_whls(
354+
whls = whls,
355+
want_platforms = requirement.target_platforms,
356+
logger = logger,
357+
)
354358

355359
return whls, sdist

python/private/pypi/whl_target_platforms.bzl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,15 @@ _OS_PREFIXES = {
4646
"win": "windows",
4747
} # buildifier: disable=unsorted-dict-items
4848

49-
def select_whls(*, whls, want_platforms = [], logger = None):
49+
def select_whls(*, whls, want_platforms = {}, logger = None):
5050
"""Select a subset of wheels suitable for target platforms from a list.
5151
5252
Args:
53-
whls(list[struct]): A list of candidates which have a `filename`
54-
attribute containing the `whl` filename.
55-
want_platforms(str): The platforms in "{abi}_{os}_{cpu}" or "{os}_{cpu}" format.
53+
whls: {type}`list[struct]` candidates which have a `filename` attribute containing
54+
the `whl` filename.
55+
want_platforms: {type}`dict[str, struct]` The platforms in "{abi}_{os}_{cpu}" or
56+
"{os}_{cpu}" format for the keys and the values are options for further fine
57+
tuning the selection.
5658
logger: A logger for printing diagnostic messages.
5759
5860
Returns:

0 commit comments

Comments
 (0)