|
| 1 | +load("@bazel_skylib//lib:paths.bzl", "paths") |
| 2 | +load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo") |
| 3 | +load("//python:py_runtime_info.bzl", "PyRuntimeInfo") |
| 4 | +load("//python/private:sentinel.bzl", "SentinelInfo") |
| 5 | +load("//python/private:toolchain_types.bzl", "TARGET_TOOLCHAIN_TYPE") |
| 6 | + |
| 7 | +def _interpreter_impl(ctx): |
| 8 | + if SentinelInfo in ctx.attr.binary: |
| 9 | + toolchain = ctx.toolchains[TARGET_TOOLCHAIN_TYPE] |
| 10 | + runtime = toolchain.py3_runtime |
| 11 | + else: |
| 12 | + runtime = ctx.attr.binary[PyRuntimeInfo] |
| 13 | + |
| 14 | + # NOTE: We name the output filename after the underlying file name |
| 15 | + # because of things like pyenv: they use $0 to determine what to |
| 16 | + # re-exec. If it's not a recognized name, then they fail. |
| 17 | + if runtime.interpreter: |
| 18 | + executable = ctx.actions.declare_file(runtime.interpreter.basename) |
| 19 | + ctx.actions.symlink(output = executable, target_file = runtime.interpreter, is_executable = True) |
| 20 | + else: |
| 21 | + executable = ctx.actions.declare_symlink(paths.basename(runtime.interpreter_path)) |
| 22 | + ctx.actions.symlink(output = executable, target_path = runtime.interpreter_path) |
| 23 | + |
| 24 | + return [ |
| 25 | + DefaultInfo( |
| 26 | + executable = executable, |
| 27 | + runfiles = ctx.runfiles([executable], transitive_files = runtime.files), |
| 28 | + ), |
| 29 | + ] |
| 30 | + |
| 31 | +interpreter = rule( |
| 32 | + implementation = _interpreter_impl, |
| 33 | + toolchains = [TARGET_TOOLCHAIN_TYPE], |
| 34 | + executable = True, |
| 35 | + attrs = { |
| 36 | + "binary": attr.label( |
| 37 | + default = "//tools/run:bin", |
| 38 | + ), |
| 39 | + }, |
| 40 | +) |
0 commit comments