xref: /aosp_15_r20/external/bazelbuild-rules_python/python/uv/private/current_toolchain.bzl (revision 60517a1edbc8ecf509223e9af94a7adec7d736b8)
1# Copyright 2024 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"""This module implements an alias rule to the resolved toolchain.
16"""
17
18load("//python/uv/private:toolchain_types.bzl", "UV_TOOLCHAIN_TYPE")
19
20_DOC = """\
21Exposes a concrete toolchain which is the result of Bazel resolving the
22toolchain for the execution or target platform.
23Workaround for https://github.com/bazelbuild/bazel/issues/14009
24"""
25
26# Forward all the providers
27def _current_toolchain_impl(ctx):
28    toolchain_info = ctx.toolchains[UV_TOOLCHAIN_TYPE]
29
30    # Bazel requires executable rules to create the executable themselves,
31    # so we create a symlink in this rule so that it appears this rule created its executable.
32    original_uv_executable = toolchain_info.uv_toolchain_info.uv[DefaultInfo].files_to_run.executable
33
34    # Use `uv` as the name of the binary to make the help message well formatted
35    symlink_uv_executable = ctx.actions.declare_file("current_toolchain/uv".format(original_uv_executable.basename))
36    ctx.actions.symlink(output = symlink_uv_executable, target_file = original_uv_executable)
37
38    new_default_info = DefaultInfo(
39        files = depset([symlink_uv_executable]),
40        runfiles = toolchain_info.default_info.default_runfiles,
41        executable = symlink_uv_executable,
42    )
43
44    template_variable_info = platform_common.TemplateVariableInfo({
45        "UV_BIN": symlink_uv_executable.path,
46    })
47
48    return [
49        toolchain_info,
50        new_default_info,
51        template_variable_info,
52        toolchain_info.uv_toolchain_info,
53    ]
54
55# Copied from java_toolchain_alias
56# https://cs.opensource.google/bazel/bazel/+/master:tools/jdk/java_toolchain_alias.bzl
57current_toolchain = rule(
58    implementation = _current_toolchain_impl,
59    toolchains = [UV_TOOLCHAIN_TYPE],
60    doc = _DOC,
61    executable = True,
62)
63