Skip to content

Commit 45de4ac

Browse files
committed
move all of the alias setting to the new macro
1 parent f373be6 commit 45de4ac

File tree

2 files changed

+40
-20
lines changed

2 files changed

+40
-20
lines changed

python/private/toolchain_aliases.bzl

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@
1616

1717
load("@rules_python//python:versions.bzl", "PLATFORMS")
1818

19-
def toolchain_aliases(*, name, platforms, native = native):
19+
def toolchain_aliases(*, name, platforms, visibility = None, native = native):
2020
"""Cretae toolchain aliases for the python toolchains.
2121
2222
Args:
2323
name: {type}`str` The name of the current repository.
2424
platforms: {type}`platforms` The list of platforms that are supported
2525
for the current toolchain repository.
26+
visibility: {type}`list[Target] | None` The visibility of the aliases.
2627
native: The native struct used in the macro, useful for testing.
2728
"""
2829
for platform in PLATFORMS.keys():
@@ -36,10 +37,38 @@ def toolchain_aliases(*, name, platforms, native = native):
3637
visibility = ["//visibility:private"],
3738
)
3839

39-
native.alias(name = "files", actual = select({{":" + item: "@" + name + "_" + item + "//:files" for item in platforms}}))
40-
native.alias(name = "includes", actual = select({{":" + item: "@" + name + "_" + item + "//:includes" for item in platforms}}))
41-
native.alias(name = "libpython", actual = select({{":" + item: "@" + name + "_" + item + "//:libpython" for item in platforms}}))
42-
native.alias(name = "py3_runtime", actual = select({{":" + item: "@" + name + "_" + item + "//:py3_runtime" for item in platforms}}))
43-
native.alias(name = "python_headers", actual = select({{":" + item: "@" + name + "_" + item + "//:python_headers" for item in platforms}}))
44-
native.alias(name = "python_runtimes", actual = select({{":" + item: "@" + name + "_" + item + "//:python_runtimes" for item in platforms}}))
45-
native.alias(name = "python3", actual = select({{":" + item: "@" + name + "_" + item + "//:" + ("python.exe" if "windows" in item else "bin/python3") for item in platforms}}))
40+
prefix = name
41+
for name in [
42+
"files",
43+
"includes",
44+
"libpython",
45+
"py3_runtime",
46+
"python_headers",
47+
"python_runtimes",
48+
]:
49+
native.alias(
50+
name = name,
51+
actual = select({
52+
":" + platform: "@{}_{}//:{}".format(prefix, platform, name)
53+
for platform in platforms
54+
}),
55+
visibility = visibility,
56+
)
57+
58+
native.alias(
59+
name = "python3",
60+
actual = select({
61+
":" + platform: "@{}_{}//:{}".format(prefix, platform, "python.exe" if "windows" in platform else "bin/python3")
62+
for platform in platforms
63+
}),
64+
visibility = visibility,
65+
)
66+
native.alias(
67+
name = "pip",
68+
actual = select({
69+
":" + platform: "@{}_{}//:python_runtimes".format(prefix, platform)
70+
for platform in platforms
71+
if "windows" not in platform
72+
}),
73+
visibility = visibility,
74+
)

python/private/toolchains_repo.bzl

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -126,16 +126,13 @@ toolchains_repo = repository_rule(
126126
)
127127

128128
def _toolchain_aliases_impl(rctx):
129-
logger = repo_utils.logger(rctx)
130-
(os_name, _) = _get_host_os_arch(rctx, logger)
131-
132-
is_windows = (os_name == WINDOWS_NAME)
133-
134129
# Base BUILD file for this repository.
135130
build_contents = """\
136131
# Generated by python/private/toolchains_repo.bzl
137-
package(default_visibility = ["//visibility:public"])
138132
load("@rules_python//python/private:toolchain_aliases.bzl", "toolchain_aliases")
133+
134+
package(default_visibility = ["//visibility:public"])
135+
139136
exports_files(["defs.bzl"])
140137
141138
PLATFORMS = [
@@ -149,12 +146,6 @@ toolchain_aliases(
149146
py_repository = rctx.attr.user_repository_name,
150147
loaded_platforms = "\n".join([" \"{}\",".format(p) for p in rctx.attr.platforms]),
151148
)
152-
if not is_windows:
153-
build_contents += """\
154-
alias(name = "pip", actual = select({{":" + item: "@{py_repository}_" + item + "//:python_runtimes" for item in PLATFORMS if "windows" not in item}}))
155-
""".format(
156-
py_repository = rctx.attr.user_repository_name,
157-
)
158149
rctx.file("BUILD.bazel", build_contents)
159150

160151
# Expose a Starlark file so rules can know what host platform we used and where to find an interpreter

0 commit comments

Comments
 (0)