xref: /aosp_15_r20/external/bazelbuild-rules_python/python/private/runtime_env_toolchain.bzl (revision 60517a1edbc8ecf509223e9af94a7adec7d736b8)
1# Copyright 2019 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"""Definitions related to the Python toolchain."""
15
16load("@rules_cc//cc:defs.bzl", "cc_library")
17load("//python:py_runtime.bzl", "py_runtime")
18load("//python:py_runtime_pair.bzl", "py_runtime_pair")
19load("//python/cc:py_cc_toolchain.bzl", "py_cc_toolchain")
20load(":py_exec_tools_toolchain.bzl", "py_exec_tools_toolchain")
21load(":toolchain_types.bzl", "EXEC_TOOLS_TOOLCHAIN_TYPE", "PY_CC_TOOLCHAIN_TYPE", "TARGET_TOOLCHAIN_TYPE")
22
23_IS_EXEC_TOOLCHAIN_ENABLED = Label("//python/config_settings:is_exec_tools_toolchain_enabled")
24
25def define_runtime_env_toolchain(name):
26    """Defines the runtime_env Python toolchain.
27
28    This is a minimal suite of toolchains that provided limited functionality.
29    They're mostly only useful to aid migration off the builtin
30    `@bazel_tools//tools/python:autodetecting_toolchain` toolchain.
31
32    NOTE: This was previously called the "autodetecting" toolchain, but was
33    renamed to better reflect its behavior, since it doesn't autodetect
34    anything.
35
36    Args:
37        name: The name of the toolchain to introduce.
38    """
39    base_name = name.replace("_toolchain", "")
40
41    py_runtime(
42        name = "_runtime_env_py3_runtime",
43        interpreter = "//python/private:runtime_env_toolchain_interpreter.sh",
44        python_version = "PY3",
45        stub_shebang = "#!/usr/bin/env python3",
46        visibility = ["//visibility:private"],
47        tags = ["manual"],
48    )
49
50    # This is a dummy runtime whose interpreter_path triggers the native rule
51    # logic to use the legacy behavior on Windows.
52    # TODO(#7844): Remove this target.
53    py_runtime(
54        name = "_magic_sentinel_runtime",
55        interpreter_path = "/_magic_pyruntime_sentinel_do_not_use",
56        python_version = "PY3",
57        visibility = ["//visibility:private"],
58        tags = ["manual"],
59    )
60
61    py_runtime_pair(
62        name = "_runtime_env_py_runtime_pair",
63        py3_runtime = select({
64            # If we're on windows, inject the sentinel to tell native rule logic
65            # that we attempted to use the runtime_env toolchain and need to
66            # switch back to legacy behavior.
67            # TODO(#7844): Remove this hack.
68            "@platforms//os:windows": ":_magic_sentinel_runtime",
69            "//conditions:default": ":_runtime_env_py3_runtime",
70        }),
71        visibility = ["//visibility:public"],
72        tags = ["manual"],
73    )
74
75    native.toolchain(
76        name = name,
77        toolchain = ":_runtime_env_py_runtime_pair",
78        toolchain_type = TARGET_TOOLCHAIN_TYPE,
79        visibility = ["//visibility:public"],
80    )
81
82    py_exec_tools_toolchain(
83        name = "_runtime_env_py_exec_tools_toolchain_impl",
84        precompiler = Label("//tools/precompiler:precompiler"),
85        visibility = ["//visibility:private"],
86        tags = ["manual"],
87    )
88    native.toolchain(
89        name = base_name + "_py_exec_tools_toolchain",
90        toolchain = "_runtime_env_py_exec_tools_toolchain_impl",
91        toolchain_type = EXEC_TOOLS_TOOLCHAIN_TYPE,
92        target_settings = [_IS_EXEC_TOOLCHAIN_ENABLED],
93        visibility = ["//visibility:public"],
94    )
95    cc_library(
96        name = "_empty_cc_lib",
97        visibility = ["//visibility:private"],
98        tags = ["manual"],
99    )
100    py_cc_toolchain(
101        name = "_runtime_env_py_cc_toolchain_impl",
102        headers = ":_empty_cc_lib",
103        libs = ":_empty_cc_lib",
104        python_version = "0.0",
105        tags = ["manual"],
106    )
107    native.toolchain(
108        name = base_name + "_py_cc_toolchain",
109        toolchain = ":_runtime_env_py_cc_toolchain_impl",
110        toolchain_type = PY_CC_TOOLCHAIN_TYPE,
111        visibility = ["//visibility:public"],
112    )
113