aboutsummaryrefslogtreecommitdiff
path: root/python/py_runtime_pair.bzl
blob: 951c606f4a33fccd331b72015a31ce03c76d2f33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# Copyright 2023 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Public entry point for py_runtime_pair."""

load("@bazel_tools//tools/python:toolchain.bzl", _py_runtime_pair = "py_runtime_pair")

# NOTE: This doc is copy/pasted from the builtin py_runtime_pair rule so our
# doc generator gives useful API docs.
def py_runtime_pair(name, py2_runtime = None, py3_runtime = None, **attrs):
    """A toolchain rule for Python.

    This used to wrap up to two Python runtimes, one for Python 2 and one for Python 3.
    However, Python 2 is no longer supported, so it now only wraps a single Python 3
    runtime.

    Usually the wrapped runtimes are declared using the `py_runtime` rule, but any
    rule returning a `PyRuntimeInfo` provider may be used.

    This rule returns a `platform_common.ToolchainInfo` provider with the following
    schema:

    ```python
    platform_common.ToolchainInfo(
        py2_runtime = None,
        py3_runtime = <PyRuntimeInfo or None>,
    )
    ```

    Example usage:

    ```python
    # In your BUILD file...

    load("@rules_python//python:defs.bzl", "py_runtime_pair")

    py_runtime(
        name = "my_py3_runtime",
        interpreter_path = "/system/python3",
        python_version = "PY3",
    )

    py_runtime_pair(
        name = "my_py_runtime_pair",
        py3_runtime = ":my_py3_runtime",
    )

    toolchain(
        name = "my_toolchain",
        target_compatible_with = <...>,
        toolchain = ":my_py_runtime_pair",
        toolchain_type = "@rules_python//python:toolchain_type",
    )
    ```

    ```python
    # In your WORKSPACE...

    register_toolchains("//my_pkg:my_toolchain")
    ```

    Args:
        name: str, the name of the target
        py2_runtime: optional Label; must be unset or None; an error is raised
            otherwise.
        py3_runtime: Label; a target with `PyRuntimeInfo` for Python 3.
        **attrs: Extra attrs passed onto the native rule
    """
    if attrs.get("py2_runtime"):
        fail("PYthon 2 is no longer supported: see https://github.com/bazelbuild/rules_python/issues/886")
    _py_runtime_pair(
        name = name,
        py2_runtime = py2_runtime,
        py3_runtime = py3_runtime,
        **attrs
    )