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