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"Repo rule used by bzlmod extension to create a repo that has a map of Python interpreters and their labels" 16 17load( 18 "//python/private:toolchains_repo.bzl", 19 "python_toolchain_build_file_content", 20) 21 22def _have_same_length(*lists): 23 if not lists: 24 fail("expected at least one list") 25 return len({len(length): None for length in lists}) == 1 26 27_HUB_BUILD_FILE_TEMPLATE = """\ 28load("@bazel_skylib//:bzl_library.bzl", "bzl_library") 29load("@@{rules_python}//python/private:py_toolchain_suite.bzl", "py_toolchain_suite") 30 31bzl_library( 32 name = "interpreters_bzl", 33 srcs = ["interpreters.bzl"], 34 visibility = ["@rules_python//:__subpackages__"], 35) 36 37{toolchains} 38""" 39 40def _hub_build_file_content( 41 prefixes, 42 python_versions, 43 set_python_version_constraints, 44 user_repository_names, 45 workspace_location): 46 """This macro iterates over each of the lists and returns the toolchain content. 47 48 python_toolchain_build_file_content is called to generate each of the toolchain 49 definitions. 50 """ 51 52 if not _have_same_length(python_versions, set_python_version_constraints, user_repository_names): 53 fail("all lists must have the same length") 54 55 # Iterate over the length of python_versions and call 56 # build the toolchain content by calling python_toolchain_build_file_content 57 toolchains = "\n".join( 58 [ 59 python_toolchain_build_file_content( 60 prefix = prefixes[i], 61 python_version = python_versions[i], 62 set_python_version_constraint = set_python_version_constraints[i], 63 user_repository_name = user_repository_names[i], 64 ) 65 for i in range(len(python_versions)) 66 ], 67 ) 68 69 return _HUB_BUILD_FILE_TEMPLATE.format( 70 toolchains = toolchains, 71 rules_python = workspace_location.workspace_name, 72 ) 73 74_interpreters_bzl_template = """ 75INTERPRETER_LABELS = {{ 76{interpreter_labels} 77}} 78DEFAULT_PYTHON_VERSION = "{default_python_version}" 79""" 80 81_line_for_hub_template = """\ 82 "{name}_host": Label("@{name}_host//:python"), 83""" 84 85def _hub_repo_impl(rctx): 86 # Create the various toolchain definitions and 87 # write them to the BUILD file. 88 rctx.file( 89 "BUILD.bazel", 90 _hub_build_file_content( 91 rctx.attr.toolchain_prefixes, 92 rctx.attr.toolchain_python_versions, 93 rctx.attr.toolchain_set_python_version_constraints, 94 rctx.attr.toolchain_user_repository_names, 95 rctx.attr._rules_python_workspace, 96 ), 97 executable = False, 98 ) 99 100 # Create a dict that is later used to create 101 # a symlink to a interpreter. 102 interpreter_labels = "".join([ 103 _line_for_hub_template.format(name = name) 104 for name in rctx.attr.toolchain_user_repository_names 105 ]) 106 107 rctx.file( 108 "interpreters.bzl", 109 _interpreters_bzl_template.format( 110 interpreter_labels = interpreter_labels, 111 default_python_version = rctx.attr.default_python_version, 112 ), 113 executable = False, 114 ) 115 116hub_repo = repository_rule( 117 doc = """\ 118This private rule create a repo with a BUILD file that contains a map of interpreter names 119and the labels to said interpreters. This map is used to by the interpreter hub extension. 120This rule also writes out the various toolchains for the different Python versions. 121""", 122 implementation = _hub_repo_impl, 123 attrs = { 124 "default_python_version": attr.string( 125 doc = "Default Python version for the build in `X.Y` or `X.Y.Z` format.", 126 mandatory = True, 127 ), 128 "toolchain_prefixes": attr.string_list( 129 doc = "List prefixed for the toolchains", 130 mandatory = True, 131 ), 132 "toolchain_python_versions": attr.string_list( 133 doc = "List of Python versions for the toolchains. In `X.Y.Z` format.", 134 mandatory = True, 135 ), 136 "toolchain_set_python_version_constraints": attr.string_list( 137 doc = "List of version contraints for the toolchains", 138 mandatory = True, 139 ), 140 "toolchain_user_repository_names": attr.string_list( 141 doc = "List of the user repo names for the toolchains", 142 mandatory = True, 143 ), 144 "_rules_python_workspace": attr.label(default = Label("//:does_not_matter_what_this_name_is")), 145 }, 146) 147