Skip to content

Commit fe35320

Browse files
committed
Pass around the list of loaded platforms for each toolchain
1 parent 503acab commit fe35320

File tree

5 files changed

+25
-16
lines changed

5 files changed

+25
-16
lines changed

examples/bzlmod/MODULE.bazel.lock

Lines changed: 2 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

python/private/python.bzl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ def parse_modules(*, module_ctx, _fail = fail):
213213
def _python_impl(module_ctx):
214214
py = parse_modules(module_ctx = module_ctx)
215215

216+
loaded_platforms = {}
216217
for toolchain_info in py.toolchains:
217218
# Ensure that we pass the full version here.
218219
full_python_version = full_version(
@@ -228,7 +229,7 @@ def _python_impl(module_ctx):
228229
kwargs.update(py.config.kwargs.get(toolchain_info.python_version, {}))
229230
kwargs.update(py.config.kwargs.get(full_python_version, {}))
230231
kwargs.update(py.config.default)
231-
python_register_toolchains(
232+
loaded_platforms[full_python_version] = python_register_toolchains(
232233
name = toolchain_info.name,
233234
_internal_bzlmod_toolchain_call = True,
234235
**kwargs
@@ -257,6 +258,7 @@ def _python_impl(module_ctx):
257258
for i in range(len(py.toolchains))
258259
],
259260
toolchain_user_repository_names = [t.name for t in py.toolchains],
261+
loaded_platforms = loaded_platforms,
260262
)
261263

262264
# This is require in order to support multiple version py_test

python/private/python_register_toolchains.bzl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ def python_register_toolchains(
7373
minor_mapping: {type}`dict[str, str]` contains a mapping from `X.Y` to `X.Y.Z`
7474
version.
7575
**kwargs: passed to each {obj}`python_repository` call.
76+
77+
Returns:
78+
on bzlmod this returns the loaded platform labels.
7679
"""
7780
bzlmod_toolchain_call = kwargs.pop("_internal_bzlmod_toolchain_call", False)
7881
if bzlmod_toolchain_call:
@@ -168,11 +171,13 @@ def python_register_toolchains(
168171

169172
# in bzlmod we write out our own toolchain repos
170173
if bzlmod_toolchain_call:
171-
return
174+
return loaded_platforms
172175

173176
toolchains_repo(
174177
name = toolchain_repo_name,
175178
python_version = python_version,
176179
set_python_version_constraint = set_python_version_constraint,
177180
user_repository_name = name,
181+
platforms = loaded_platforms,
178182
)
183+
return None

python/private/pythons_hub.bzl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ def _hub_build_file_content(
4646
python_versions,
4747
set_python_version_constraints,
4848
user_repository_names,
49-
workspace_location):
49+
workspace_location,
50+
loaded_platforms):
5051
"""This macro iterates over each of the lists and returns the toolchain content.
5152
5253
python_toolchain_build_file_content is called to generate each of the toolchain
@@ -65,6 +66,7 @@ def _hub_build_file_content(
6566
python_version = python_versions[i],
6667
set_python_version_constraint = set_python_version_constraints[i],
6768
user_repository_name = user_repository_names[i],
69+
loaded_platforms = loaded_platforms[python_versions[i]],
6870
)
6971
for i in range(len(python_versions))
7072
],
@@ -103,6 +105,7 @@ def _hub_repo_impl(rctx):
103105
rctx.attr.toolchain_set_python_version_constraints,
104106
rctx.attr.toolchain_user_repository_names,
105107
rctx.attr._rules_python_workspace,
108+
rctx.attr.loaded_platforms,
106109
),
107110
executable = False,
108111
)
@@ -149,6 +152,9 @@ This rule also writes out the various toolchains for the different Python versio
149152
doc = "Default Python version for the build in `X.Y` or `X.Y.Z` format.",
150153
mandatory = True,
151154
),
155+
"loaded_platforms": attr.string_list_dict(
156+
doc = "The mapping from platform to version",
157+
),
152158
"minor_mapping": attr.string_dict(
153159
doc = "The minor mapping of the `X.Y` to `X.Y.Z` format that is used in config settings.",
154160
mandatory = True,

python/private/toolchains_repo.bzl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ def python_toolchain_build_file_content(
4141
prefix,
4242
python_version,
4343
set_python_version_constraint,
44-
user_repository_name):
44+
user_repository_name,
45+
loaded_platforms):
4546
"""Creates the content for toolchain definitions for a build file.
4647
4748
Args:
@@ -51,6 +52,8 @@ def python_toolchain_build_file_content(
5152
have the Python version constraint added as a requirement for
5253
matching the toolchain, "False" if not.
5354
user_repository_name: names for the user repos
55+
loaded_platforms: the list of platform identifiers for which to generate
56+
the toolchain targets.
5457
5558
Returns:
5659
build_content: Text containing toolchain definitions
@@ -78,6 +81,7 @@ py_toolchain_suite(
7881
python_version = python_version,
7982
)
8083
for platform, meta in PLATFORMS.items()
84+
if platform in loaded_platforms
8185
])
8286

8387
def _toolchains_repo_impl(rctx):
@@ -100,6 +104,7 @@ load("@{rules_python}//python/private:py_toolchain_suite.bzl", "py_toolchain_sui
100104
python_version = rctx.attr.python_version,
101105
set_python_version_constraint = str(rctx.attr.set_python_version_constraint),
102106
user_repository_name = rctx.attr.user_repository_name,
107+
loaded_platforms = rctx.attr.platforms,
103108
)
104109

105110
rctx.file("BUILD.bazel", build_content + toolchains)
@@ -109,6 +114,7 @@ toolchains_repo = repository_rule(
109114
doc = "Creates a repository with toolchain definitions for all known platforms " +
110115
"which can be registered or selected.",
111116
attrs = {
117+
"platforms": attr.string_list(doc = "List of platforms for which the toolchain definitions shall be created"),
112118
"python_version": attr.string(doc = "The Python version."),
113119
"set_python_version_constraint": attr.bool(doc = "if target_compatible_with for the toolchain should set the version constraint"),
114120
"user_repository_name": attr.string(doc = "what the user chose for the base name"),

0 commit comments

Comments
 (0)