Skip to content

Commit 4d15a73

Browse files
committed
explore using ctx.actions.declare_symlink
1 parent 83a774d commit 4d15a73

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

python/bin/BUILD.bazel

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ _interpreter_binary(
1010
name = "python",
1111
binary = ":python_src",
1212
visibility = ["//visibility:public"],
13+
target_compatible_with = select({
14+
"@platforms//os:windows": ["@platforms//:incompatible"],
15+
"//conditions:default": [],
16+
}),
1317
)
1418

1519
# The user can modify this flag to source different interpreters for the

python/private/interpreter.bzl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ load("@bazel_skylib//lib:paths.bzl", "paths")
1818
load("//python:py_runtime_info.bzl", "PyRuntimeInfo")
1919
load(":sentinel.bzl", "SentinelInfo")
2020
load(":toolchain_types.bzl", "TARGET_TOOLCHAIN_TYPE")
21+
load(":py_executable.bzl", "relative_path", "runfiles_root_path")
2122

2223
def _interpreter_binary_impl(ctx):
2324
if SentinelInfo in ctx.attr.binary:
@@ -30,6 +31,13 @@ def _interpreter_binary_impl(ctx):
3031
# because of things like pyenv: they use $0 to determine what to
3132
# re-exec. If it's not a recognized name, then they fail.
3233
if runtime.interpreter:
34+
# Option 1:
35+
# Works locally, but not remotely.
36+
#executable = ctx.actions.declare_file(runtime.interpreter.basename)
37+
#ctx.actions.symlink(output = executable, target_file = runtime.interpreter)
38+
39+
# Option 2:
40+
# Works locally, and remotely, but not on Windows.
3341
executable = ctx.actions.declare_file(runtime.interpreter.basename)
3442
ctx.actions.expand_template(
3543
template = ctx.file._template,
@@ -39,6 +47,14 @@ def _interpreter_binary_impl(ctx):
3947
},
4048
is_executable = True,
4149
)
50+
51+
# Option 3:
52+
# Works locally and remotely, but only in runfiles, doesn't work via "bazel run".
53+
#executable = ctx.actions.declare_symlink(runtime.interpreter.basename)
54+
#ctx.actions.symlink(output = executable, target_path = relative_path(
55+
# from_ = paths.dirname(runfiles_root_path(ctx, executable.short_path)),
56+
# to = runfiles_root_path(ctx, runtime.interpreter.short_path),
57+
#))
4258
else:
4359
executable = ctx.actions.declare_symlink(paths.basename(runtime.interpreter_path))
4460
ctx.actions.symlink(output = executable, target_path = runtime.interpreter_path)

python/private/py_executable.bzl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ def _create_executable(
447447
)
448448

449449
def _create_zip_main(ctx, *, stage2_bootstrap, runtime_details, venv):
450-
python_binary = _runfiles_root_path(ctx, venv.interpreter.short_path)
450+
python_binary = runfiles_root_path(ctx, venv.interpreter.short_path)
451451
python_binary_actual = venv.interpreter_actual_path
452452

453453
# The location of this file doesn't really matter. It's added to
@@ -522,7 +522,7 @@ def _create_venv(ctx, output_prefix, imports, runtime_details):
522522

523523
if not venvs_use_declare_symlink_enabled:
524524
if runtime.interpreter:
525-
interpreter_actual_path = _runfiles_root_path(ctx, runtime.interpreter.short_path)
525+
interpreter_actual_path = runfiles_root_path(ctx, runtime.interpreter.short_path)
526526
else:
527527
interpreter_actual_path = runtime.interpreter_path
528528

@@ -543,11 +543,11 @@ def _create_venv(ctx, output_prefix, imports, runtime_details):
543543
# may choose to write what symlink() points to instead.
544544
interpreter = ctx.actions.declare_symlink("{}/bin/{}".format(venv, py_exe_basename))
545545

546-
interpreter_actual_path = _runfiles_root_path(ctx, runtime.interpreter.short_path)
546+
interpreter_actual_path = runfiles_root_path(ctx, runtime.interpreter.short_path)
547547
rel_path = relative_path(
548548
# dirname is necessary because a relative symlink is relative to
549549
# the directory the symlink resides within.
550-
from_ = paths.dirname(_runfiles_root_path(ctx, interpreter.short_path)),
550+
from_ = paths.dirname(runfiles_root_path(ctx, interpreter.short_path)),
551551
to = interpreter_actual_path,
552552
)
553553

@@ -646,7 +646,7 @@ def _create_stage2_bootstrap(
646646
)
647647
return output
648648

649-
def _runfiles_root_path(ctx, short_path):
649+
def runfiles_root_path(ctx, short_path):
650650
"""Compute a runfiles-root relative path from `File.short_path`
651651
652652
Args:
@@ -676,7 +676,7 @@ def _create_stage1_bootstrap(
676676
runtime = runtime_details.effective_runtime
677677

678678
if venv:
679-
python_binary_path = _runfiles_root_path(ctx, venv.interpreter.short_path)
679+
python_binary_path = runfiles_root_path(ctx, venv.interpreter.short_path)
680680
else:
681681
python_binary_path = runtime_details.executable_interpreter_path
682682

0 commit comments

Comments
 (0)