xref: /aosp_15_r20/external/bazelbuild-rules_python/python/py_runtime_pair.bzl (revision 60517a1edbc8ecf509223e9af94a7adec7d736b8)
1# Copyright 2023 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"""Public entry point for py_runtime_pair."""
16
17load("@bazel_tools//tools/python:toolchain.bzl", _bazel_tools_impl = "py_runtime_pair")
18load("//python/private:py_runtime_pair_macro.bzl", _starlark_impl = "py_runtime_pair")
19load("//python/private:util.bzl", "IS_BAZEL_6_OR_HIGHER")
20
21_py_runtime_pair = _starlark_impl if IS_BAZEL_6_OR_HIGHER else _bazel_tools_impl
22
23# NOTE: This doc is copy/pasted from the builtin py_runtime_pair rule so our
24# doc generator gives useful API docs.
25def py_runtime_pair(name, py2_runtime = None, py3_runtime = None, **attrs):
26    """A toolchain rule for Python.
27
28    This is a macro around the underlying {rule}`py_runtime_pair` rule.
29
30    This used to wrap up to two Python runtimes, one for Python 2 and one for Python 3.
31    However, Python 2 is no longer supported, so it now only wraps a single Python 3
32    runtime.
33
34    Usually the wrapped runtimes are declared using the `py_runtime` rule, but any
35    rule returning a `PyRuntimeInfo` provider may be used.
36
37    This rule returns a `platform_common.ToolchainInfo` provider with the following
38    schema:
39
40    ```python
41    platform_common.ToolchainInfo(
42        py2_runtime = None,
43        py3_runtime = <PyRuntimeInfo or None>,
44    )
45    ```
46
47    Example usage:
48
49    ```python
50    # In your BUILD file...
51
52    load("@rules_python//python:py_runtime.bzl", "py_runtime")
53    load("@rules_python//python:py_runtime_pair.bzl", "py_runtime_pair")
54
55    py_runtime(
56        name = "my_py3_runtime",
57        interpreter_path = "/system/python3",
58        python_version = "PY3",
59    )
60
61    py_runtime_pair(
62        name = "my_py_runtime_pair",
63        py3_runtime = ":my_py3_runtime",
64    )
65
66    toolchain(
67        name = "my_toolchain",
68        target_compatible_with = <...>,
69        toolchain = ":my_py_runtime_pair",
70        toolchain_type = "@rules_python//python:toolchain_type",
71    )
72    ```
73
74    ```python
75    # In your WORKSPACE...
76
77    register_toolchains("//my_pkg:my_toolchain")
78    ```
79
80    Args:
81        name: str, the name of the target
82        py2_runtime: optional Label; must be unset or None; an error is raised
83            otherwise.
84        py3_runtime: Label; a target with `PyRuntimeInfo` for Python 3.
85        **attrs: Extra attrs passed onto the native rule
86    """
87    if attrs.get("py2_runtime"):
88        fail("PYthon 2 is no longer supported: see https://github.com/bazelbuild/rules_python/issues/886")
89    _py_runtime_pair(
90        name = name,
91        py2_runtime = py2_runtime,
92        py3_runtime = py3_runtime,
93        **attrs
94    )
95