Skip to content

Commit d428325

Browse files
committed
checkpoint: sort of works, but repo backend name is off i think
1 parent a511e65 commit d428325

File tree

2 files changed

+61
-20
lines changed

2 files changed

+61
-20
lines changed

python/private/python.bzl

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -313,17 +313,17 @@ def _python_impl(module_ctx):
313313
platforms = platforms,
314314
**kwargs
315315
)
316-
print("{} registered platforms: {}".format(toolchain_info.name, tc_created_platforms))
317316
loaded_platforms[full_python_version] = tc_created_platforms
317+
print("{} registered platforms: {}".format(toolchain_info.name, tc_created_platforms))
318318
for loaded in tc_created_platforms:
319319
info = platforms[loaded]
320320
if info.os_name == host_os and info.arch == host_cpu:
321321
host_compatible.append(struct(
322-
##repo_prefix = toolchain_info.name,
322+
repo_prefix = toolchain_info.name,
323323
repo_name = toolchain_info.name + "_" + loaded,
324324
full_python_version = full_python_version,
325-
##base_version = toolchain_info.python_version,
326-
##platform_name = loaded,
325+
base_version = toolchain_info.python_version,
326+
platform_name = loaded,
327327
##platform_info = info,
328328
))
329329
print("create host for:", toolchain_info.name, toolchain_info.python_version)
@@ -368,6 +368,12 @@ def _python_impl(module_ctx):
368368
"best match" means:
369369
* for major.minor, the highest version <= the minor_mapping bound
370370
* for major.minor.patch: whatever is available, but maybe nothing.
371+
372+
Or, maybe just detect what needs more advanced matching.
373+
So if python_register_toolchains(X) is able to find a match for host,
374+
that's fine.
375+
If it can't, then we use more advanced matching.
376+
371377
"""
372378
minor_mapping = py.config.minor_mapping
373379

@@ -417,7 +423,20 @@ def _python_impl(module_ctx):
417423
for host_needed_base_name, host_needed in host_repos_to_create.items():
418424
print("find backend for:", host_needed_base_name, "v:", host_needed.version)
419425
needed_version_str = host_needed.version
420-
backing_repo_name = None
426+
backing_repo_names = None
427+
428+
def candidate_key(e):
429+
pref = 0
430+
if e.platform_name.endswith("-freethreaded"):
431+
pref = -1
432+
elif e.platform_name.endswith("-musl"):
433+
pref = -2
434+
435+
return (
436+
0 if e.repo_prefix == host_needed_base_name else -1,
437+
version_tuple(e.full_python_version),
438+
pref,
439+
)
421440

422441
# Major.Minor case: look for a minor <= the minor_mapping bound
423442
# that is compatible with our host
@@ -431,30 +450,43 @@ def _python_impl(module_ctx):
431450
#print("add candidate:", entry)
432451
candidates.append(entry)
433452

434-
def keyer(e):
435-
return (
436-
version_tuple(e.full_python_version),
437-
)
438-
439453
candidates = sorted(
440454
candidates,
441455
reverse = True,
442-
key = lambda e: (version_tuple(e.full_python_version), e.repo_name),
456+
key = candidate_key,
443457
)
444458
print("sorted candidates:")
445459
for x in candidates:
446460
print(" ", x)
447-
if candidates:
448-
backing_repo_name = candidates[0].repo_name
461+
backing_repo_names = [c.repo_name for c in candidates]
449462
else:
450-
fail("not implemented: fv", needed_version_str)
463+
candidates = []
464+
for entry in host_compatible:
465+
if entry.full_python_version == needed_version_str:
466+
candidates.append(entry)
467+
candidates = sorted(candidates, reverse = True, key = candidate_key)
468+
backing_repo_names = [c.repo_name for c in candidates]
469+
#fail("not implemented: fv", needed_version_str)
451470

452-
if not backing_repo_name:
471+
if not backing_repo_names and host_needed.version not in ("3.13.3",):
453472
fail("no host-compatible repo found", host_needed)
454-
print("{} using {}".format(host_needed_base_name, backing_repo_name))
473+
474+
# Argh. Old impl:
475+
# create python_3_10_linux
476+
# create python_3_10_host using python_3_10_linux
477+
# new impl:
478+
# create python_3_10_linux
479+
# create python_3_10_host using python_3_10_X_linux
480+
# Thing to do is give preference such that e.g.
481+
# python_3_10 prefers python_3_10_linux_x86 over python_3_10_9_linux_x86
482+
backing_repo_names = [
483+
x.removeprefix(host_needed_base_name + "_")
484+
for x in backing_repo_names
485+
]
486+
print("{} using {}".format(host_needed_base_name, backing_repo_names))
455487
host_toolchain(
456488
name = host_needed_base_name + "_host",
457-
backing_repo_name = backing_repo_name,
489+
backing_repo_names = backing_repo_names,
458490
)
459491

460492
# Now major_minor_lte maps major_minor to a list of descending full

python/private/toolchains_repo.bzl

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,8 @@ def _host_toolchain_impl(rctx):
234234
235235
exports_files(["python"], visibility = ["//visibility:public"])
236236
""")
237-
if not rctx.attr.backing_repo_name:
237+
os_name = repo_utils.get_platforms_os_name(rctx)
238+
if not rctx.attr.backing_repo_names:
238239
platforms = {
239240
p: struct(os_name = rctx.attr.os_names[p], arch = rctx.attr.archs[p])
240241
for p in rctx.attr.platforms
@@ -253,7 +254,15 @@ exports_files(["python"], visibility = ["//visibility:public"])
253254
host_platform = host_platform,
254255
)
255256
else:
256-
repo = rctx.attr.backing_repo_name
257+
print("host repo: candidates=", rctx.attr.backing_repo_names)
258+
host_platform = rctx.attr.backing_repo_names[0]
259+
260+
# todo: getenv(). If set, use it or error. Else take offset 0.
261+
##repo = "@" + rctx.attr.backing_repo_names[0]
262+
repo = "@@{py_repository}_{host_platform}".format(
263+
py_repository = rctx.attr.name[:-len("_host")],
264+
host_platform = host_platform,
265+
)
257266

258267
rctx.report_progress("Symlinking interpreter files to the target platform")
259268
host_python_repo = rctx.path(Label("{repo}//:BUILD.bazel".format(repo = repo)))
@@ -325,7 +334,7 @@ toolchain_aliases repo because referencing the `python` interpreter target from
325334
this repo causes an eager fetch of the toolchain for the host platform.
326335
""",
327336
attrs = {
328-
"backing_repo_name": attr.string(),
337+
"backing_repo_names": attr.string_list(),
329338
"platforms": attr.string_list(mandatory = False),
330339
"python_version": attr.string(mandatory = False),
331340
"_rule_name": attr.string(default = "host_toolchain"),

0 commit comments

Comments
 (0)