Skip to content

refactor: make bzlmod create host repos for toolchains #2888

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion python/private/python.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ load(":full_version.bzl", "full_version")
load(":python_register_toolchains.bzl", "python_register_toolchains")
load(":pythons_hub.bzl", "hub_repo")
load(":repo_utils.bzl", "repo_utils")
load(":toolchains_repo.bzl", "multi_toolchain_aliases")
load(":toolchains_repo.bzl", "host_toolchain", "multi_toolchain_aliases")
load(":util.bzl", "IS_BAZEL_6_4_OR_HIGHER")
load(":version.bzl", "version")

Expand Down Expand Up @@ -298,6 +298,7 @@ def _python_impl(module_ctx):
_internal_bzlmod_toolchain_call = True,
**kwargs
)
host_compatible = []
for repo_name, (platform_name, platform_info) in register_result.impl_repos.items():
toolchain_impls.append(struct(
# str: The base name to use for the toolchain() target
Expand All @@ -315,6 +316,15 @@ def _python_impl(module_ctx):
# The last toolchain is the default; it can't have version constraints
set_python_version_constraint = is_last,
))
if _is_compatible_with_host(module_ctx, platform_info):
host_compatible.append(platform_name)

host_toolchain(
name = toolchain_info.name + "_host",
# NOTE: Order matters. The first found to be compatible is (usually) used.
platforms = host_compatible,
python_version = full_python_version,
)

# List of the base names ("python_3_10") for the toolchain repos
base_toolchain_repo_names = []
Expand Down Expand Up @@ -406,6 +416,11 @@ def _python_impl(module_ctx):
else:
return None

def _is_compatible_with_host(mctx, platform_info):
os_name = repo_utils.get_platforms_os_name(mctx)
cpu_name = repo_utils.get_platforms_cpu_name(mctx)
return platform_info.os_name == os_name and platform_info.arch == cpu_name

def _one_or_the_same(first, second, *, onerror = None):
if not first:
return second
Expand Down
14 changes: 7 additions & 7 deletions python/private/python_register_toolchains.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -170,26 +170,26 @@ def python_register_toolchains(
platform = platform,
))

host_toolchain(
name = name + "_host",
platforms = loaded_platforms,
python_version = python_version,
)

toolchain_aliases(
name = name,
python_version = python_version,
user_repository_name = name,
platforms = loaded_platforms,
)

# in bzlmod we write out our own toolchain repos
# in bzlmod we write out our own toolchain repos and host repos
if bzlmod_toolchain_call:
return struct(
# dict[str name, tuple[str platform_name, platform_info]]
impl_repos = impl_repos,
)

host_toolchain(
name = name + "_host",
platforms = loaded_platforms,
python_version = python_version,
)

toolchains_repo(
name = toolchain_repo_name,
python_version = python_version,
Expand Down
2 changes: 2 additions & 0 deletions python/private/toolchains_repo.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,8 @@ def _host_toolchain_impl(rctx):
if not rctx.delete(python_tester):
fail("Failed to delete the python tester")

# NOTE: The term "toolchain" is a misnomer for this rule. This doesn't define
# a repo with toolchains or toolchain implementations.
host_toolchain = repository_rule(
_host_toolchain_impl,
doc = """\
Expand Down