Skip to content

Commit 289d87d

Browse files
committed
start prototyping how the platforms could be passed
1 parent d6af2b7 commit 289d87d

File tree

3 files changed

+168
-9
lines changed

3 files changed

+168
-9
lines changed

MODULE.bazel

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,63 @@ use_repo(
5656
# This call registers the Python toolchains.
5757
register_toolchains("@pythons_hub//:all")
5858

59+
# Let's define default platforms for our users first
60+
pip = use_extension("//python/extensions:pip.bzl", "pip")
61+
62+
_DEFAULT_LINUX_PLATFORM_VERSION = "0"
63+
64+
_DEFAULT_OSX_PLATFORM_VERSION = "14.0"
65+
66+
_DEFAULT_WINDOWS_PLATFORM_VERSION = "0"
67+
68+
pip.default(
69+
arch_name = "x86_64",
70+
constraint_values = [
71+
"@platforms//os:linux",
72+
"@platforms//cpu:x86_64",
73+
],
74+
env_platform_version = _DEFAULT_LINUX_PLATFORM_VERSION,
75+
os_name = "linux",
76+
platform = "linux_x86_64",
77+
)
78+
pip.default(
79+
arch_name = "x86_64",
80+
constraint_values = [
81+
"@platforms//os:osx",
82+
"@platforms//cpu:x86_64",
83+
],
84+
# We choose the oldest non-EOL version at the time when we release `rules_python`.
85+
# See https://endoflife.date/macos
86+
env_platform_version = _DEFAULT_OSX_PLATFORM_VERSION,
87+
os_name = "osx",
88+
platform = "osx_x86_64",
89+
)
90+
pip.default(
91+
arch_name = "aarch64",
92+
constraint_values = [
93+
"@platforms//os:osx",
94+
"@platforms//cpu:aarch64",
95+
],
96+
# We choose the oldest non-EOL version at the time when we release `rules_python`.
97+
# See https://endoflife.date/macos
98+
env_platform_version = _DEFAULT_OSX_PLATFORM_VERSION,
99+
os_name = "osx",
100+
platform = "osx_aarch64",
101+
)
102+
pip.default(
103+
arch_name = "x86_64",
104+
constraint_values = [
105+
"@platforms//os:windows",
106+
"@platforms//cpu:x86_64",
107+
],
108+
env_platform_version = _DEFAULT_WINDOWS_PLATFORM_VERSION,
109+
os_name = "windows",
110+
platform = "windows_x86_64",
111+
)
112+
59113
#####################
60-
# Install twine for our own runfiles wheel publishing and allow bzlmod users to use it.
114+
# Then install twine for our own runfiles wheel publishing and allow bzlmod users to use it.
61115

62-
pip = use_extension("//python/extensions:pip.bzl", "pip")
63116
pip.parse(
64117
# NOTE @aignas 2024-10-26: We have an integration test that depends on us
65118
# being able to build sdists for this hub, so explicitly set this to False.

python/private/pypi/evaluate_markers.bzl

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
"""A simple function that evaluates markers using a python interpreter."""
1616

1717
load(":deps.bzl", "record_files")
18-
load(":pep508_env.bzl", "env")
1918
load(":pep508_evaluate.bzl", "evaluate")
20-
load(":pep508_platform.bzl", "platform_from_str")
2119
load(":pep508_requirement.bzl", "requirement")
2220
load(":pypi_repo_utils.bzl", "pypi_repo_utils")
2321

@@ -30,21 +28,25 @@ SRCS = [
3028
Label("//python/private/pypi/whl_installer:platform.py"),
3129
]
3230

33-
def evaluate_markers(requirements, python_version = None):
31+
def evaluate_markers(requirements, platforms = platforms):
3432
"""Return the list of supported platforms per requirements line.
3533
3634
Args:
3735
requirements: {type}`dict[str, list[str]]` of the requirement file lines to evaluate.
38-
python_version: {type}`str | None` the version that can be used when evaluating the markers.
36+
platforms: {type}`dict[str | struct]` TODO
3937
4038
Returns:
4139
dict of string lists with target platforms
4240
"""
4341
ret = {}
44-
for req_string, platforms in requirements.items():
42+
for req_string, platform_strings in requirements.items():
4543
req = requirement(req_string)
46-
for platform in platforms:
47-
if evaluate(req.marker, env = env(platform_from_str(platform, python_version))):
44+
for platform_str in platform_strings:
45+
platform = platforms.get(platform_str)
46+
if not platform:
47+
fail("Please define platform: TODO")
48+
49+
if evaluate(req.marker, env = platform.env):
4850
ret.setdefault(req_string, []).append(platform)
4951

5052
return ret

python/private/pypi/extension.bzl

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ def _create_whl_repos(
7070
*,
7171
pip_attr,
7272
whl_overrides,
73+
config,
7374
available_interpreters = INTERPRETER_LABELS,
7475
minor_mapping = MINOR_MAPPING,
7576
evaluate_markers = evaluate_markers_py,
@@ -81,6 +82,7 @@ def _create_whl_repos(
8182
module_ctx: {type}`module_ctx`.
8283
pip_attr: {type}`struct` - the struct that comes from the tag class iteration.
8384
whl_overrides: {type}`dict[str, struct]` - per-wheel overrides.
85+
config: TODO.
8486
get_index_urls: A function used to get the index URLs
8587
available_interpreters: {type}`dict[str, Label]` The dictionary of available
8688
interpreters that have been registered using the `python` bzlmod extension.
@@ -104,6 +106,7 @@ def _create_whl_repos(
104106
"""
105107
logger = repo_utils.logger(module_ctx, "pypi:create_whl_repos")
106108
python_interpreter_target = pip_attr.python_interpreter_target
109+
platforms = config["platforms"]
107110

108111
# containers to aggregate outputs from this function
109112
whl_map = {}
@@ -199,6 +202,7 @@ def _create_whl_repos(
199202
srcs = pip_attr._evaluate_markers_srcs,
200203
logger = logger,
201204
),
205+
platforms = platforms,
202206
logger = logger,
203207
)
204208

@@ -364,6 +368,34 @@ def _whl_repos(*, requirement, whl_library_args, download_only, netrc, auth_patt
364368

365369
return ret
366370

371+
def _configure(config, *, platform, constraint_values, target_settings, override = False, **values):
372+
"""Set the value in the config if the value is provided"""
373+
for key, value in values.items():
374+
if not value:
375+
continue
376+
377+
if not override and config.get(key):
378+
continue
379+
380+
config[key] = value
381+
382+
config.setdefault("platforms", {})
383+
if not platform:
384+
if constraint_values or target_settings:
385+
fail("`platform` name must be specified when specifying `constraint_values`, `target_settings` or `urls`")
386+
elif constraint_values or target_settings:
387+
if not override and config.get("platforms", {}).get(platform):
388+
return
389+
390+
config["platforms"][platform] = struct(
391+
name = platform.replace("-", "_").lower(),
392+
constraint_values = constraint_values,
393+
target_settings = target_settings,
394+
env = struct(), # ...
395+
)
396+
else:
397+
config["platforms"].pop(platform)
398+
367399
def parse_modules(
368400
module_ctx,
369401
_fail = fail,
@@ -380,6 +412,32 @@ def parse_modules(
380412
Returns:
381413
A struct with the following attributes:
382414
"""
415+
defaults = {
416+
"platforms": {},
417+
}
418+
for mod in module_ctx.modules:
419+
if not (mod.is_root or mod.name == "rules_python"):
420+
continue
421+
422+
for tag in mod.tags.default:
423+
_configure(
424+
# TODO @aignas 2025-05-18: actually use all of this stuff
425+
defaults,
426+
arch_name = tag.arch_name,
427+
constraint_values = tag.constraint_values,
428+
env_implementation_name = tag.env_implementation_name,
429+
env_os_name = tag.env_os_name,
430+
env_platform_machine = tag.env_platform_machine,
431+
env_platform_release = tag.env_platform_release,
432+
env_platform_system = tag.env_platform_system,
433+
env_platform_version = tag.env_platform_version,
434+
env_sys_platform = tag.env_sys_platform,
435+
os_name = tag.os_name,
436+
platform = tag.platform,
437+
target_settings = tag.target_settings,
438+
override = mod.is_root,
439+
)
440+
383441
whl_mods = {}
384442
for mod in module_ctx.modules:
385443
for whl_mod in mod.tags.whl_mods:
@@ -526,6 +584,7 @@ You cannot use both the additive_build_content and additive_build_content_file a
526584
pip_attr = pip_attr,
527585
get_index_urls = get_index_urls,
528586
whl_overrides = whl_overrides,
587+
config = defaults,
529588
**kwargs
530589
)
531590
hub_whl_map.setdefault(hub_name, {})
@@ -680,6 +739,35 @@ def _pip_impl(module_ctx):
680739
else:
681740
return None
682741

742+
_default_attrs = {
743+
"arch_name": attr.string(),
744+
"constraint_values": attr.label_list(),
745+
# The values for PEP508 env marker evaluation during the lock file parsing
746+
"env_implementation_name": attr.string(),
747+
"env_os_name": attr.string(doc = "default will be inferred from {obj}`os_name`"),
748+
"env_platform_machine": attr.string(doc = "default will be inferred from {obj}`arch_name`"),
749+
"env_platform_release": attr.string(),
750+
"env_platform_system": attr.string(doc = "default will be inferred from {obj}`os_name`"),
751+
"env_platform_version": attr.string(),
752+
"env_sys_platform": attr.string(),
753+
"extra_index_urls": attr.string_list(),
754+
"index_url": attr.string(),
755+
"os_name": attr.string(),
756+
"platform": attr.string(),
757+
"target_settings": attr.label_list(
758+
doc = """\
759+
A list of config_settings that must be satisfied by the target configuration in order for this
760+
platform to be matched during analysis phase.
761+
""",
762+
),
763+
}
764+
765+
_configure_attrs = _default_attrs | {
766+
"hub_name": attr.string(),
767+
"python_version": attr.string(),
768+
"requirements_txt": attr.label(),
769+
}
770+
683771
def _pip_parse_ext_attrs(**kwargs):
684772
"""Get the attributes for the pip extension.
685773
@@ -936,6 +1024,22 @@ the BUILD files for wheels.
9361024
""",
9371025
implementation = _pip_impl,
9381026
tag_classes = {
1027+
"configure": tag_class(
1028+
attrs = _configure_attrs,
1029+
doc = """\
1030+
This tag class allows for more customization of how the configuration for the hub repositories is built.
1031+
1032+
This is still experimental and may be changed or removed without any notice.
1033+
""",
1034+
),
1035+
"default": tag_class(
1036+
attrs = _default_attrs,
1037+
doc = """\
1038+
This tag class allows for more customization of how the configuration for the hub repositories is built.
1039+
1040+
This is still experimental and may be changed or removed without any notice.
1041+
""",
1042+
),
9391043
"override": _override_tag,
9401044
"parse": tag_class(
9411045
attrs = _pip_parse_ext_attrs(),

0 commit comments

Comments
 (0)