8000 Fix: Skip aliases for unloaded toolchains (#1472) · kilian-funk/rules_python@ed59285 · GitHub
[go: up one dir, main page]

Skip to content

Commit ed59285

Browse files
Fix: Skip aliases for unloaded toolchains (bazel-contrib#1472)
Toolchains without a checksum are skipped in python_register_toolchain. This commit also skips the creation of aliases to those toolchains. Closes bazel-contrib#1472
1 parent e009502 commit ed59285

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

python/private/toolchains_repo.bzl

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -155,22 +155,23 @@ def _toolchain_aliases_impl(rctx):
155155
build_contents = """\
156156
# Generated by python/private/toolchains_repo.bzl
157157
package(default_visibility = ["//visibility:public"])
158-
load("@rules_python//python:versions.bzl", "PLATFORMS", "gen_python_config_settings")
158+
load("@rules_python//python:versions.bzl", "gen_python_config_settings")
159+
load(":defs.bzl", "PLATFORMS")
159160
gen_python_config_settings()
160161
exports_files(["defs.bzl"])
161-
alias(name = "files", actual = select({{":" + item: "@{py_repository}_" + item + "//:files" for item in PLATFORMS.keys()}}))
162-
alias(name = "includes", actual = select({{":" + item: "@{py_repository}_" + item + "//:includes" for item in PLATFORMS.keys()}}))
163-
alias(name = "libpython", actual = select({{":" + item: "@{py_repository}_" + item + "//:libpython" for item in PLATFORMS.keys()}}))
164-
alias(name = "py3_runtime", actual = select({{":" + item: "@{py_repository}_" + item + "//:py3_runtime" for item in PLATFORMS.keys()}}))
165-
alias(name = "python_headers", actual = select({{":" + item: "@{py_repository}_" + item + "//:python_headers" for item in PLATFORMS.keys()}}))
166-
alias(name = "python_runtimes", actual = select({{":" + item: "@{py_repository}_" + item + "//:python_runtimes" for item in PLATFORMS.keys()}}))
167-
alias(name = "python3", actual = select({{":" + item: "@{py_repository}_" + item + "//:" + ("python.exe" if "windows" in item else "bin/python3") for item in PLATFORMS.keys()}}))
162+
alias(name = "files", actual = select({{":" + item: "@{py_repository}_" + item + "//:files" for item in PLATFORMS}}))
163+
alias(name = "includes", actual = select({{":" + item: "@{py_repository}_" + item + "//:includes" for item in PLATFORMS}}))
164+
alias(name = "libpython", actual = select({{":" + item: "@{py_repository}_" + item + "//:libpython" for item in PLATFORMS}}))
165+
alias(name = "py3_runtime", actual = select({{":" + item: "@{py_repository}_" + item + "//:py3_runtime" for item in PLATFORMS}}))
166+
alias(name = "python_headers", actual = select({{":" + item: "@{py_repository}_" + item + "//:python_headers" for item in PLATFORMS}}))
167+
alias(name = "python_runtimes", actual = select({{":" + item: "@{py_repository}_" + item + "//:python_runtimes" for item in PLATFORMS}}))
168+
alias(name = "python3", actual = select({{":" + item: "@{py_repository}_" + item + "//:" + ("python.exe" if "windows" in item else "bin/python3") for item in PLATFORMS}}))
168169
""".format(
169170
py_repository = rctx.attr.user_repository_name,
170171
)
171172
if not is_windows:
172173
build_contents += """\
173-
alias(name = "pip", actual = select({{":" + item: "@{py_repository}_" + item + "//:python_runtimes" for item in PLATFORMS.keys() if "windows" not in item}}))
174+
alias(name = "pip", actual = select({{":" + item: "@{py_repository}_" + item + "//:python_runtimes" for item in PLATFORMS if "windows" not in item}}))
174175
""".format(
175176
py_repository = rctx.attr.user_repository_name,
176177
host_platform = host_platform,
@@ -195,6 +196,9 @@ load("{rules_python}//python:pip.bzl", _compile_pip_requirements = "compile_pip_
195196
196197
host_platform = "{host_platform}"
197198
interpreter = "@{py_repository}_{host_platform}//:{python3_binary_path}"
199+
PLATFORMS = [
200+
{loaded_platforms}
201+
]
198202
199203
def py_binary(name, **kwargs):
200204
return _py_binary(
@@ -231,6 +235,7 @@ def compile_pip_requirements(name, **kwargs):
231235
python_version = rctx.attr.python_version,
232236
python3_binary_path = python3_binary_path,
233237
rules_python = get_repository_name(rctx.attr._rules_python_workspace),
238+
loaded_platforms = "\n".join([" \"{}\",".format(p) for p in rctx.attr.platforms]),
234239
))
235240

236241
toolchain_aliases = repository_rule(
@@ -244,6 +249,9 @@ toolchain_aliases = repository_rule(
244249
mandatory = True,
245250
doc = "The base name for all created repositories, like 'python38'.",
246251
),
252+
"platforms": attr.string_list(
253+
doc = "List of platforms for which aliases shall be created",
254+
),
247255
"_rules_python_workspace": attr.label(default = Label("//:WORKSPACE")),
248256
},
249257
)

python/repositories.bzl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,11 +553,13 @@ def python_register_toolchains(
553553
))
554554
register_coverage_tool = False
555555

556+
loaded_platforms = []
556557
for platform in PLATFORMS.keys():
557558
sha256 = tool_versions[python_version]["sha256"].get(platform, None)
558559
if not sha256:
559560
continue
560561

562+
loaded_platforms.append(platform)
561563
(release_filename, urls, strip_prefix, patches) = get_release_info(platform, python_version, base_url, tool_versions)
562564

563565
# allow passing in a tool version
@@ -604,6 +606,7 @@ def python_register_toolchains(
604606
name = name,
605607
python_version = python_version,
606608
user_repository_name = name,
609+
platforms = loaded_platforms,
607610
)
608611

609612
# in bzlmod we write out our own toolchain repos

0 commit comments

Comments
 (0)
0