|
| 1 | +# Copyright 2025 The Bazel Authors. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Implementation of the rules to access the underlying Python interpreter.""" |
| 16 | + |
| 17 | +load("@bazel_skylib//lib:paths.bzl", "paths") |
| 18 | +load("//python:py_runtime_info.bzl", "PyRuntimeInfo") |
| 19 | +load(":common.bzl", "runfiles_root_path") |
| 20 | +load(":sentinel.bzl", "SentinelInfo") |
| 21 | +load(":toolchain_types.bzl", "TARGET_TOOLCHAIN_TYPE") |
| 22 | + |
| 23 | +def _interpreter_binary_impl(ctx): |
| 24 | + if SentinelInfo in ctx.attr.binary: |
| 25 | + toolchain = ctx.toolchains[TARGET_TOOLCHAIN_TYPE] |
| 26 | + runtime = toolchain.py3_runtime |
| 27 | + else: |
| 28 | + runtime = ctx.attr.binary[PyRuntimeInfo] |
| 29 | + |
| 30 | + # NOTE: We name the output filename after the underlying file name |
| 31 | + # because of things like pyenv: they use $0 to determine what to |
| 32 | + # re-exec. If it's not a recognized name, then they fail. |
| 33 | + if runtime.interpreter: |
| 34 | + # In order for this to work both locally and remotely, we create a |
| 35 | + # shell script here that re-exec's into the real interpreter. Ideally, |
| 36 | + # we'd just use a symlink, but that breaks under certain conditions. If |
| 37 | + # we use a ctx.actions.symlink(target=...) then it fails under remote |
| 38 | + # execution. If we use ctx.actions.symlink(target_path=...) then it |
| 39 | + # behaves differently inside the runfiles tree and outside the runfiles |
| 40 | + # tree. |
| 41 | + # |
| 42 | + # This currently does not work on Windows. Need to find a way to enable |
| 43 | + # that. |
| 44 | + executable = ctx.actions.declare_file(runtime.interpreter.basename) |
| 45 | + ctx.actions.expand_template( |
| 46 | + template = ctx.file._template, |
| 47 | + output = executable, |
| 48 | + substitutions = { |
| 49 | + "%target_file%": runfiles_root_path(ctx, runtime.interpreter.short_path), |
| 50 | + }, |
| 51 | + is_executable = True, |
| 52 | + ) |
| 53 | + else: |
| 54 | + executable = ctx.actions.declare_symlink(paths.basename(runtime.interpreter_path)) |
| 55 | + ctx.actions.symlink(output = executable, target_path = runtime.interpreter_path) |
| 56 | + |
| 57 | + return [ |
| 58 | + DefaultInfo( |
| 59 | + executable = executable, |
| 60 | + runfiles = ctx.runfiles([executable], transitive_files = runtime.files).merge_all([ |
| 61 | + ctx.attr._bash_runfiles[DefaultInfo].default_runfiles, |
| 62 | + ]), |
| 63 | + ), |
| 64 | + ] |
| 65 | + |
| 66 | +interpreter_binary = rule( |
| 67 | + implementation = _interpreter_binary_impl, |
| 68 | + toolchains = [TARGET_TOOLCHAIN_TYPE], |
| 69 | + executable = True, |
| 70 | + attrs = { |
| 71 | + "binary": attr.label( |
| 72 | + mandatory = True, |
| 73 | + ), |
| 74 | + "_bash_runfiles": attr.label( |
| 75 | + default = "@bazel_tools//tools/bash/runfiles", |
| 76 | + ), |
| 77 | + "_template": attr.label( |
| 78 | + default = "//python/private:interpreter_tmpl.sh", |
| 79 | + allow_single_file = True, |
| 80 | + ), |
| 81 | + }, |
| 82 | +) |
0 commit comments