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"Creates a repository to hold toolchains" 16 17load("//python/private:text_util.bzl", "render") 18 19_TOOLCHAIN_TEMPLATE = """ 20toolchain( 21 name = "{name}", 22 target_compatible_with = {compatible_with}, 23 toolchain = "{toolchain_label}", 24 toolchain_type = "{toolchain_type}", 25) 26""" 27 28def _toolchains_repo_impl(repository_ctx): 29 build_content = "" 30 for toolchain_name in repository_ctx.attr.toolchain_names: 31 toolchain_label = repository_ctx.attr.toolchain_labels[toolchain_name] 32 toolchain_compatible_with = repository_ctx.attr.toolchain_compatible_with[toolchain_name] 33 34 build_content += _TOOLCHAIN_TEMPLATE.format( 35 name = toolchain_name, 36 toolchain_type = repository_ctx.attr.toolchain_type, 37 toolchain_label = toolchain_label, 38 compatible_with = render.list(toolchain_compatible_with), 39 ) 40 41 repository_ctx.file("BUILD.bazel", build_content) 42 43uv_toolchains_repo = repository_rule( 44 _toolchains_repo_impl, 45 doc = "Generates a toolchain hub repository", 46 attrs = { 47 "toolchain_compatible_with": attr.string_list_dict(doc = "A list of platform constraints for this toolchain, keyed by toolchain name.", mandatory = True), 48 "toolchain_labels": attr.string_dict(doc = "The name of the toolchain implementation target, keyed by toolchain name.", mandatory = True), 49 "toolchain_names": attr.string_list(doc = "List of toolchain names", mandatory = True), 50 "toolchain_type": attr.string(doc = "The toolchain type of the toolchains", mandatory = True), 51 }, 52) 53