xref: /aosp_15_r20/external/bazelbuild-rules_python/python/py_runtime_pair.bzl (revision 60517a1edbc8ecf509223e9af94a7adec7d736b8)
1*60517a1eSAndroid Build Coastguard Worker# Copyright 2023 The Bazel Authors. All rights reserved.
2*60517a1eSAndroid Build Coastguard Worker#
3*60517a1eSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
4*60517a1eSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
5*60517a1eSAndroid Build Coastguard Worker# You may obtain a copy of the License at
6*60517a1eSAndroid Build Coastguard Worker#
7*60517a1eSAndroid Build Coastguard Worker#     http://www.apache.org/licenses/LICENSE-2.0
8*60517a1eSAndroid Build Coastguard Worker#
9*60517a1eSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
10*60517a1eSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
11*60517a1eSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*60517a1eSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
13*60517a1eSAndroid Build Coastguard Worker# limitations under the License.
14*60517a1eSAndroid Build Coastguard Worker
15*60517a1eSAndroid Build Coastguard Worker"""Public entry point for py_runtime_pair."""
16*60517a1eSAndroid Build Coastguard Worker
17*60517a1eSAndroid Build Coastguard Workerload("@bazel_tools//tools/python:toolchain.bzl", _bazel_tools_impl = "py_runtime_pair")
18*60517a1eSAndroid Build Coastguard Workerload("//python/private:py_runtime_pair_macro.bzl", _starlark_impl = "py_runtime_pair")
19*60517a1eSAndroid Build Coastguard Workerload("//python/private:util.bzl", "IS_BAZEL_6_OR_HIGHER")
20*60517a1eSAndroid Build Coastguard Worker
21*60517a1eSAndroid Build Coastguard Worker_py_runtime_pair = _starlark_impl if IS_BAZEL_6_OR_HIGHER else _bazel_tools_impl
22*60517a1eSAndroid Build Coastguard Worker
23*60517a1eSAndroid Build Coastguard Worker# NOTE: This doc is copy/pasted from the builtin py_runtime_pair rule so our
24*60517a1eSAndroid Build Coastguard Worker# doc generator gives useful API docs.
25*60517a1eSAndroid Build Coastguard Workerdef py_runtime_pair(name, py2_runtime = None, py3_runtime = None, **attrs):
26*60517a1eSAndroid Build Coastguard Worker    """A toolchain rule for Python.
27*60517a1eSAndroid Build Coastguard Worker
28*60517a1eSAndroid Build Coastguard Worker    This is a macro around the underlying {rule}`py_runtime_pair` rule.
29*60517a1eSAndroid Build Coastguard Worker
30*60517a1eSAndroid Build Coastguard Worker    This used to wrap up to two Python runtimes, one for Python 2 and one for Python 3.
31*60517a1eSAndroid Build Coastguard Worker    However, Python 2 is no longer supported, so it now only wraps a single Python 3
32*60517a1eSAndroid Build Coastguard Worker    runtime.
33*60517a1eSAndroid Build Coastguard Worker
34*60517a1eSAndroid Build Coastguard Worker    Usually the wrapped runtimes are declared using the `py_runtime` rule, but any
35*60517a1eSAndroid Build Coastguard Worker    rule returning a `PyRuntimeInfo` provider may be used.
36*60517a1eSAndroid Build Coastguard Worker
37*60517a1eSAndroid Build Coastguard Worker    This rule returns a `platform_common.ToolchainInfo` provider with the following
38*60517a1eSAndroid Build Coastguard Worker    schema:
39*60517a1eSAndroid Build Coastguard Worker
40*60517a1eSAndroid Build Coastguard Worker    ```python
41*60517a1eSAndroid Build Coastguard Worker    platform_common.ToolchainInfo(
42*60517a1eSAndroid Build Coastguard Worker        py2_runtime = None,
43*60517a1eSAndroid Build Coastguard Worker        py3_runtime = <PyRuntimeInfo or None>,
44*60517a1eSAndroid Build Coastguard Worker    )
45*60517a1eSAndroid Build Coastguard Worker    ```
46*60517a1eSAndroid Build Coastguard Worker
47*60517a1eSAndroid Build Coastguard Worker    Example usage:
48*60517a1eSAndroid Build Coastguard Worker
49*60517a1eSAndroid Build Coastguard Worker    ```python
50*60517a1eSAndroid Build Coastguard Worker    # In your BUILD file...
51*60517a1eSAndroid Build Coastguard Worker
52*60517a1eSAndroid Build Coastguard Worker    load("@rules_python//python:py_runtime.bzl", "py_runtime")
53*60517a1eSAndroid Build Coastguard Worker    load("@rules_python//python:py_runtime_pair.bzl", "py_runtime_pair")
54*60517a1eSAndroid Build Coastguard Worker
55*60517a1eSAndroid Build Coastguard Worker    py_runtime(
56*60517a1eSAndroid Build Coastguard Worker        name = "my_py3_runtime",
57*60517a1eSAndroid Build Coastguard Worker        interpreter_path = "/system/python3",
58*60517a1eSAndroid Build Coastguard Worker        python_version = "PY3",
59*60517a1eSAndroid Build Coastguard Worker    )
60*60517a1eSAndroid Build Coastguard Worker
61*60517a1eSAndroid Build Coastguard Worker    py_runtime_pair(
62*60517a1eSAndroid Build Coastguard Worker        name = "my_py_runtime_pair",
63*60517a1eSAndroid Build Coastguard Worker        py3_runtime = ":my_py3_runtime",
64*60517a1eSAndroid Build Coastguard Worker    )
65*60517a1eSAndroid Build Coastguard Worker
66*60517a1eSAndroid Build Coastguard Worker    toolchain(
67*60517a1eSAndroid Build Coastguard Worker        name = "my_toolchain",
68*60517a1eSAndroid Build Coastguard Worker        target_compatible_with = <...>,
69*60517a1eSAndroid Build Coastguard Worker        toolchain = ":my_py_runtime_pair",
70*60517a1eSAndroid Build Coastguard Worker        toolchain_type = "@rules_python//python:toolchain_type",
71*60517a1eSAndroid Build Coastguard Worker    )
72*60517a1eSAndroid Build Coastguard Worker    ```
73*60517a1eSAndroid Build Coastguard Worker
74*60517a1eSAndroid Build Coastguard Worker    ```python
75*60517a1eSAndroid Build Coastguard Worker    # In your WORKSPACE...
76*60517a1eSAndroid Build Coastguard Worker
77*60517a1eSAndroid Build Coastguard Worker    register_toolchains("//my_pkg:my_toolchain")
78*60517a1eSAndroid Build Coastguard Worker    ```
79*60517a1eSAndroid Build Coastguard Worker
80*60517a1eSAndroid Build Coastguard Worker    Args:
81*60517a1eSAndroid Build Coastguard Worker        name: str, the name of the target
82*60517a1eSAndroid Build Coastguard Worker        py2_runtime: optional Label; must be unset or None; an error is raised
83*60517a1eSAndroid Build Coastguard Worker            otherwise.
84*60517a1eSAndroid Build Coastguard Worker        py3_runtime: Label; a target with `PyRuntimeInfo` for Python 3.
85*60517a1eSAndroid Build Coastguard Worker        **attrs: Extra attrs passed onto the native rule
86*60517a1eSAndroid Build Coastguard Worker    """
87*60517a1eSAndroid Build Coastguard Worker    if attrs.get("py2_runtime"):
88*60517a1eSAndroid Build Coastguard Worker        fail("PYthon 2 is no longer supported: see https://github.com/bazelbuild/rules_python/issues/886")
89*60517a1eSAndroid Build Coastguard Worker    _py_runtime_pair(
90*60517a1eSAndroid Build Coastguard Worker        name = name,
91*60517a1eSAndroid Build Coastguard Worker        py2_runtime = py2_runtime,
92*60517a1eSAndroid Build Coastguard Worker        py3_runtime = py3_runtime,
93*60517a1eSAndroid Build Coastguard Worker        **attrs
94*60517a1eSAndroid Build Coastguard Worker    )
95