xref: /aosp_15_r20/external/bazelbuild-rules_python/python/uv/extensions.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"""
16EXPERIMENTAL: This is experimental and may be removed without notice
17
18A module extension for working with uv.
19"""
20
21load("//python/uv:repositories.bzl", "uv_register_toolchains")
22
23_DOC = """\
24A module extension for working with uv.
25"""
26
27uv_toolchain = tag_class(attrs = {
28    "uv_version": attr.string(doc = "Explicit version of uv.", mandatory = True),
29})
30
31def _uv_toolchain_extension(module_ctx):
32    for mod in module_ctx.modules:
33        for toolchain in mod.tags.toolchain:
34            if not mod.is_root:
35                fail(
36                    "Only the root module may configure the uv toolchain.",
37                    "This prevents conflicting registrations with any other modules.",
38                    "NOTE: We may wish to enforce a policy where toolchain configuration is only allowed in the root module, or in rules_python. See https://github.com/bazelbuild/bazel/discussions/22024",
39                )
40
41            uv_register_toolchains(
42                uv_version = toolchain.uv_version,
43                register_toolchains = False,
44            )
45
46uv = module_extension(
47    doc = _DOC,
48    implementation = _uv_toolchain_extension,
49    tag_classes = {"toolchain": uv_toolchain},
50)
51