Skip to content

Commit 9abd323

Browse files
authored
refactor: make bzlmod create host repos for toolchains (#2888)
This moves the creation of the host_toolchain repos into the bzlmod phase. This is to facilitate future work to allow for when a a particular version doesn't provide a host-compatible variant Work towards #2081
1 parent e2e9a43 commit 9abd323

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

python/private/python.bzl

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ load(":full_version.bzl", "full_version")
2121
load(":python_register_toolchains.bzl", "python_register_toolchains")
2222
load(":pythons_hub.bzl", "hub_repo")
2323
load(":repo_utils.bzl", "repo_utils")
24-
load(":toolchains_repo.bzl", "multi_toolchain_aliases")
24+
load(":toolchains_repo.bzl", "host_toolchain", "multi_toolchain_aliases")
2525
load(":util.bzl", "IS_BAZEL_6_4_OR_HIGHER")
2626
load(":version.bzl", "version")
2727

@@ -298,6 +298,7 @@ def _python_impl(module_ctx):
298298
_internal_bzlmod_toolchain_call = True,
299299
**kwargs
300300
)
301+
host_compatible = []
301302
for repo_name, (platform_name, platform_info) in register_result.impl_repos.items():
302303
toolchain_impls.append(struct(
303304
# str: The base name to use for the toolchain() target
@@ -315,6 +316,15 @@ def _python_impl(module_ctx):
315316
# The last toolchain is the default; it can't have version constraints
316317
set_python_version_constraint = is_last,
317318
))
319+
if _is_compatible_with_host(module_ctx, platform_info):
320+
host_compatible.append(platform_name)
321+
322+
host_toolchain(
323+
name = toolchain_info.name + "_host",
324+
# NOTE: Order matters. The first found to be compatible is (usually) used.
325+
platforms = host_compatible,
326+
python_version = full_python_version,
327+
)
318328

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

419+
def _is_compatible_with_host(mctx, platform_info):
420+
os_name = repo_utils.get_platforms_os_name(mctx)
421+
cpu_name = repo_utils.get_platforms_cpu_name(mctx)
422+
return platform_info.os_name == os_name and platform_info.arch == cpu_name
423+
409424
def _one_or_the_same(first, second, *, onerror = None):
410425
if not first:
411426
return second

python/private/python_register_toolchains.bzl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,26 +171,26 @@ def python_register_toolchains(
171171
platform = platform,
172172
))
173173

174-
host_toolchain(
175-
name = name + "_host",
176-
platforms = loaded_platforms,
177-
python_version = python_version,
178-
)
179-
180174
toolchain_aliases(
181175
name = name,
182176
python_version = python_version,
183177
user_repository_name = name,
184178
platforms = loaded_platforms,
185179
)
186180

187-
# in bzlmod we write out our own toolchain repos
181+
# in bzlmod we write out our own toolchain repos and host repos
188182
if bzlmod_toolchain_call:
189183
return struct(
190184
# dict[str name, tuple[str platform_name, platform_info]]
191185
impl_repos = impl_repos,
192186
)
193187

188+
host_toolchain(
189+
name = name + "_host",
190+
platforms = loaded_platforms,
191+
python_version = python_version,
192+
)
193+
194194
toolchains_repo(
195195
name = toolchain_repo_name,
196196
python_version = python_version,

python/private/toolchains_repo.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,8 @@ def _host_toolchain_impl(rctx):
375375
if not rctx.delete(python_tester):
376376
fail("Failed to delete the python tester")
377377

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

0 commit comments

Comments
 (0)