Skip to content

Commit 9a13cec

Browse files
committed
wip: spike the passing of constraint_values to the hub repo
1 parent cd4dd15 commit 9a13cec

File tree

5 files changed

+67
-16
lines changed

5 files changed

+67
-16
lines changed

python/private/pypi/config_settings.bzl

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ def config_settings(
113113
osx_versions = [],
114114
target_platforms = [],
115115
name = None,
116+
platform_constraint_values = {},
116117
**kwargs):
117118
"""Generate all of the pip config settings.
118119
@@ -128,29 +129,36 @@ def config_settings(
128129
config settings for.
129130
target_platforms (list[str]): The list of "{os}_{cpu}" for deriving
130131
constraint values for each condition.
132+
platform_constraint_values: {type}`dict[str, list[str]]` the constraint
133+
values to use instead of the default ones. This will become a mandatory
134+
parameter later.
131135
**kwargs: Other args passed to the underlying implementations, such as
132136
{obj}`native`.
133137
"""
134138

135139
glibc_versions = [""] + glibc_versions
136140
muslc_versions = [""] + muslc_versions
137141
osx_versions = [""] + osx_versions
138-
target_platforms = [("", ""), ("osx", "universal2")] + [
139-
t.split("_", 1)
142+
target_platforms = {
143+
tuple(t.split("_", 1)): t
140144
for t in target_platforms
141-
]
145+
}
146+
target_platforms = {
147+
("", ""): [],
148+
# TODO @aignas 2025-06-15: allowing universal2 and platform specific wheels in one
149+
# closure is making things maybe a little bit too complicated.
150+
("osx", "universal2"): ["@platforms//os:osx"],
151+
} | {
152+
(os, cpu): platform_constraint_values.get(platform_name, [
153+
"@platforms//os:{}".format(os),
154+
"@platforms//cpu:{}".format(cpu),
155+
])
156+
for (os, cpu), platform_name in target_platforms.items()
157+
}
142158

143159
for python_version in python_versions:
144-
for os, cpu in target_platforms:
145-
constraint_values = []
146-
suffix = ""
147-
if os:
148-
constraint_values.append("@platforms//os:" + os)
149-
suffix += "_" + os
150-
if cpu:
151-
suffix += "_" + cpu
152-
if cpu != "universal2":
153-
constraint_values.append("@platforms//cpu:" + cpu)
160+
for (os, cpu), constraint_values in target_platforms.items():
161+
suffix = "_{}_{}".format(os, cpu) if os and cpu else ""
154162

155163
_dist_config_settings(
156164
suffix = suffix,

python/private/pypi/extension.bzl

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ def _whl_repo(*, src, whl_library_args, is_multiple_versions, download_only, net
372372
),
373373
)
374374

375-
def _configure(config, *, platform, os_name, arch_name, override = False, **values):
375+
def _configure(config, *, platform, os_name, arch_name, constraint_values, override = False, **values):
376376
"""Set the value in the config if the value is provided"""
377377
config.setdefault("platforms", {})
378378
if platform:
@@ -383,6 +383,7 @@ def _configure(config, *, platform, os_name, arch_name, override = False, **valu
383383
name = platform.replace("-", "_").lower(),
384384
os_name = os_name,
385385
arch_name = arch_name,
386+
constraint_values = constraint_values,
386387
env = {
387388
k[4:]: v
388389
for k, v in values.items()
@@ -414,6 +415,10 @@ def _create_config(defaults):
414415
env_platform_version = "0",
415416
os_name = "linux",
416417
platform = "linux_{}".format(cpu),
418+
constraint_values = [
419+
"@platforms//os:linux",
420+
"@platforms//cpu:{}".format(cpu),
421+
],
417422
)
418423
for cpu in [
419424
"aarch64",
@@ -427,6 +432,10 @@ def _create_config(defaults):
427432
env_platform_version = "14.0",
428433
os_name = "osx",
429434
platform = "osx_{}".format(cpu),
435+
constraint_values = [
436+
"@platforms//os:osx",
437+
"@platforms//cpu:{}".format(cpu),
438+
],
430439
)
431440

432441
_configure(
@@ -435,6 +444,10 @@ def _create_config(defaults):
435444
env_platform_version = "0",
436445
os_name = "windows",
437446
platform = "windows_x86_64",
447+
constraint_values = [
448+
"@platforms//os:windows",
449+
"@platforms//cpu:x86_64",
450+
],
438451
)
439452
return struct(**defaults)
440453

@@ -500,6 +513,7 @@ You cannot use both the additive_build_content and additive_build_content_file a
500513
_configure(
501514
defaults,
502515
arch_name = tag.arch_name,
516+
constraint_values = tag.constraint_values,
503517
# The env_ values is only used if the `PIPSTAR` is enabled
504518
env_implementation_name = tag.env_implementation_name,
505519
env_os_name = tag.env_os_name,
@@ -684,6 +698,13 @@ You cannot use both the additive_build_content and additive_build_content_file a
684698
}
685699
for hub_name, extra_whl_aliases in extra_aliases.items()
686700
},
701+
platform_constraint_values = {
702+
hub_name: {
703+
platform_name: sorted([str(Label(cv)) for cv in p.constraint_values])
704+
for platform_name, p in config.platforms.items()
705+
}
706+
for hub_name in hub_whl_map
707+
},
687708
whl_libraries = {
688709
k: dict(sorted(args.items()))
689710
for k, args in sorted(whl_libraries.items())
@@ -774,6 +795,7 @@ def _pip_impl(module_ctx):
774795
for key, values in whl_map.items()
775796
},
776797
packages = mods.exposed_packages.get(hub_name, []),
798+
platform_constraint_values = mods.platform_constraint_values.get(hub_name, {}),
777799
groups = mods.hub_group_map.get(hub_name),
778800
)
779801

@@ -793,6 +815,12 @@ The CPU architecture name to be used.
793815
:::{note}
794816
Either this or {attr}`env_platform_machine` should be specified.
795817
:::
818+
""",
819+
),
820+
"constraint_values": attr.label_list(
821+
mandatory = True,
822+
doc = """\
823+
The constraint_values to use in select statements.
796824
""",
797825
),
798826
"os_name": attr.string(

python/private/pypi/hub_repository.bzl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def _impl(rctx):
3434
},
3535
extra_hub_aliases = rctx.attr.extra_hub_aliases,
3636
requirement_cycles = rctx.attr.groups,
37+
platform_constraint_values = rctx.attr.platform_constraint_values,
3738
)
3839
for path, contents in aliases.items():
3940
rctx.file(path, contents)
@@ -83,6 +84,10 @@ hub_repository = repository_rule(
8384
The list of packages that will be exposed via all_*requirements macros. Defaults to whl_map keys.
8485
""",
8586
),
87+
"platform_constraint_values": attr.string_list_dict(
88+
doc = "The constraint values for each platform name. The values are string canonical string Label representations",
89+
mandatory = False,
90+
),
8691
"repo_name": attr.string(
8792
mandatory = True,
8893
doc = "The apparent name of the repo. This is needed because in bzlmod, the name attribute becomes the canonical name.",

python/private/pypi/render_pkg_aliases.bzl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,13 @@ def _major_minor_versions(python_versions):
155155
# Use a dict as a simple set
156156
return sorted({_major_minor(v): None for v in python_versions})
157157

158-
def render_multiplatform_pkg_aliases(*, aliases, **kwargs):
158+
def render_multiplatform_pkg_aliases(*, aliases, platform_constraint_values = {}, **kwargs):
159159
"""Render the multi-platform pkg aliases.
160160
161161
Args:
162162
aliases: dict[str, list(whl_config_setting)] A list of aliases that will be
163163
transformed from ones having `filename` to ones having `config_setting`.
164+
platform_constraint_values: {type}`dict[str, list[str]]` TODO.
164165
**kwargs: extra arguments passed to render_pkg_aliases.
165166
166167
Returns:
@@ -188,17 +189,22 @@ def render_multiplatform_pkg_aliases(*, aliases, **kwargs):
188189
osx_versions = flag_versions.get("osx_versions", []),
189190
python_versions = _major_minor_versions(flag_versions.get("python_versions", [])),
190191
target_platforms = flag_versions.get("target_platforms", []),
192+
platform_constraint_values = platform_constraint_values,
191193
visibility = ["//:__subpackages__"],
192194
)
193195
return contents
194196

195-
def _render_config_settings(**kwargs):
197+
def _render_config_settings(platform_constraint_values, **kwargs):
196198
return """\
197199
load("@rules_python//python/private/pypi:config_settings.bzl", "config_settings")
198200
199201
{}""".format(render.call(
200202
"config_settings",
201203
name = repr("config_settings"),
204+
platform_constraint_values = render.dict(
205+
platform_constraint_values,
206+
value_repr = render.list,
207+
),
202208
**_repr_dict(value_repr = render.list, **kwargs)
203209
))
204210

tests/pypi/extension/extension_tests.bzl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,6 +1063,10 @@ def _test_pipstar_platforms(env):
10631063
default = [
10641064
_default(
10651065
platform = "{}_{}".format(os, cpu),
1066+
constraint_values = [
1067+
"@platforms//os:{}".format(os),
1068+
"@platforms//cpu:{}".format(cpu),
1069+
],
10661070
)
10671071
for os, cpu in [
10681072
("linux", "x86_64"),

0 commit comments

Comments
 (0)