Skip to content

Commit 2fa2f40

Browse files
committed
add nice log messages
1 parent 6269dc3 commit 2fa2f40

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

python/private/python_register_toolchains.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ def python_register_toolchains(
163163
host_toolchain(
164164
name = name + "_host",
165165
platforms = loaded_platforms,
166+
python_version = python_version,
166167
)
167168

168169
toolchain_aliases(

python/private/toolchains_repo.bzl

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,14 @@ exports_files(["python"], visibility = ["//visibility:public"])
225225
""")
226226

227227
os_name = repo_utils.get_platforms_os_name(rctx)
228-
cpu_name = repo_utils.get_platforms_cpu_name(rctx)
229-
host_platform = _get_host_platform(rctx, os_name, cpu_name, rctx.attr.platforms)
228+
host_platform = _get_host_platform(
229+
rctx = rctx,
230+
logger = repo_utils.logger(rctx),
231+
python_version = rctx.attr.python_version,
232+
os_name = os_name,
233+
cpu_name = repo_utils.get_platforms_cpu_name(rctx),
234+
platforms = rctx.attr.platforms,
235+
)
230236
repo = "@@{py_repository}_{host_platform}".format(
231237
py_repository = rctx.attr.name[:-len("_host")],
232238
host_platform = host_platform,
@@ -296,6 +302,7 @@ this repo causes an eager fetch of the toolchain for the host platform.
296302
""",
297303
attrs = {
298304
"platforms": attr.string_list(mandatory = True),
305+
"python_version": attr.string(mandatory = True),
299306
"_rule_name": attr.string(default = "host_toolchain"),
300307
"_rules_python_workspace": attr.label(default = Label("//:WORKSPACE")),
301308
},
@@ -364,11 +371,13 @@ multi_toolchain_aliases = repository_rule(
364371
def sanitize_platform_name(platform):
365372
return platform.replace("-", "_")
366373

367-
def _get_host_platform(rctx, os_name, cpu_name, platforms):
374+
def _get_host_platform(*, rctx, logger, python_version, os_name, cpu_name, platforms):
368375
"""Gets the host platform.
369376
370377
Args:
371378
rctx: {type}`repository_ctx`.
379+
logger: {type}`struct`.
380+
python_version: {type}`string`.
372381
os_name: {type}`str` the host OS name.
373382
cpu_name: {type}`str` the host CPU name.
374383
platforms: {type}`list[str]` the list of loaded platforms.
@@ -386,25 +395,30 @@ def _get_host_platform(rctx, os_name, cpu_name, platforms):
386395
return candidates[0]
387396

388397
if candidates:
389-
preference = repo_utils.getenv(
390-
rctx,
391-
"RULES_PYTHON_{}_{}_USE_HOST_PYTHON".format(
392-
os_name.upper(),
393-
cpu_name.upper(),
394-
),
398+
env_var = "RULES_PYTHON_HERMETIC_PYTHON_{}_{}_{}".format(
399+
python_version.replace(".", "_"),
400+
os_name.upper(),
401+
cpu_name.upper(),
395402
)
403+
logger.warn("Please use '{}' to select one of the candidates: {}".format(
404+
env_var,
405+
candidates,
406+
))
407+
preference = repo_utils.getenv(rctx, env_var)
396408
if preference == None:
397409
candidates = sorted(candidates, lambda k: ("freethreaded" in k, k))
398410
elif preference not in candidates:
399-
fail("Please choose a prefrered interpreter out of the following platforms: {}".format(candidates))
411+
logger.fail("Please choose a prefrered interpreter out of the following platforms: {}".format(candidates))
412+
return None
400413
else:
401414
candidates = [preference]
402415

403416
if candidates:
404417
return candidates[0]
405418

406-
fail("Could not find a compatible 'host' python for '{os_name}', '{cpu_name}' from the loaded platforms: {platforms}".format(
419+
logger.fail("Could not find a compatible 'host' python for '{os_name}', '{cpu_name}' from the loaded platforms: {platforms}".format(
407420
os_name = os_name,
408421
cpu_name = cpu_name,
409422
platforms = platforms,
410423
))
424+
return None

0 commit comments

Comments
 (0)