• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 current_py_toolchain rule."""
16
17def _current_py_toolchain_impl(ctx):
18    toolchain = ctx.toolchains[ctx.attr._toolchain]
19
20    direct = []
21    transitive = []
22    vars = {}
23
24    if toolchain.py3_runtime and toolchain.py3_runtime.interpreter:
25        direct.append(toolchain.py3_runtime.interpreter)
26        transitive.append(toolchain.py3_runtime.files)
27        vars["PYTHON3"] = toolchain.py3_runtime.interpreter.path
28
29    if toolchain.py2_runtime and toolchain.py2_runtime.interpreter:
30        direct.append(toolchain.py2_runtime.interpreter)
31        transitive.append(toolchain.py2_runtime.files)
32        vars["PYTHON2"] = toolchain.py2_runtime.interpreter.path
33
34    files = depset(direct, transitive = transitive)
35    return [
36        toolchain,
37        platform_common.TemplateVariableInfo(vars),
38        DefaultInfo(
39            runfiles = ctx.runfiles(transitive_files = files),
40            files = files,
41        ),
42    ]
43
44current_py_toolchain = rule(
45    doc = """
46    This rule exists so that the current python toolchain can be used in the `toolchains` attribute of
47    other rules, such as genrule. It allows exposing a python toolchain after toolchain resolution has
48    happened, to a rule which expects a concrete implementation of a toolchain, rather than a
49    toolchain_type which could be resolved to that toolchain.
50    """,
51    implementation = _current_py_toolchain_impl,
52    attrs = {
53        "_toolchain": attr.string(default = str(Label("@bazel_tools//tools/python:toolchain_type"))),
54    },
55    toolchains = [
56        str(Label("@bazel_tools//tools/python:toolchain_type")),
57    ],
58)
59