|  | 
|  | 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 file contains repository rules and macros to support toolchain registration. | 
|  | 16 | +""" | 
|  | 17 | + | 
|  | 18 | +load("//python:versions.bzl", "MINOR_MAPPING") | 
|  | 19 | +load(":python_register_toolchains.bzl", "python_register_toolchains") | 
|  | 20 | +load(":toolchains_repo.bzl", "multi_toolchain_aliases") | 
|  | 21 | + | 
|  | 22 | +def python_register_multi_toolchains( | 
|  | 23 | +        name, | 
|  | 24 | +        python_versions, | 
|  | 25 | +        default_version = None, | 
|  | 26 | +        minor_mapping = None, | 
|  | 27 | +        **kwargs): | 
|  | 28 | +    """Convenience macro for registering multiple Python toolchains. | 
|  | 29 | +
 | 
|  | 30 | +    Args: | 
|  | 31 | +        name: {type}`str` base name for each name in {obj}`python_register_toolchains` call. | 
|  | 32 | +        python_versions: {type}`list[str]` the Python versions. | 
|  | 33 | +        default_version: {type}`str` the default Python version. If not set, | 
|  | 34 | +            the first version in python_versions is used. | 
|  | 35 | +        minor_mapping: {type}`dict[str, str]` mapping between `X.Y` to `X.Y.Z` | 
|  | 36 | +            format. Defaults to the value in `//python:versions.bzl`. | 
|  | 37 | +        **kwargs: passed to each {obj}`python_register_toolchains` call. | 
|  | 38 | +    """ | 
|  | 39 | +    if len(python_versions) == 0: | 
|  | 40 | +        fail("python_versions must not be empty") | 
|  | 41 | + | 
|  | 42 | +    minor_mapping = minor_mapping or MINOR_MAPPING | 
|  | 43 | + | 
|  | 44 | +    if not default_version: | 
|  | 45 | +        default_version = python_versions.pop(0) | 
|  | 46 | +    for python_version in python_versions: | 
|  | 47 | +        if python_version == default_version: | 
|  | 48 | +            # We register the default version lastly so that it's not picked first when --platforms | 
|  | 49 | +            # is set with a constraint during toolchain resolution. This is due to the fact that | 
|  | 50 | +            # Bazel will match the unconstrained toolchain if we register it before the constrained | 
|  | 51 | +            # ones. | 
|  | 52 | +            continue | 
|  | 53 | +        python_register_toolchains( | 
|  | 54 | +            name = name + "_" + python_version.replace(".", "_"), | 
|  | 55 | +            python_version = python_version, | 
|  | 56 | +            set_python_version_constraint = True, | 
|  | 57 | +            minor_mapping = minor_mapping, | 
|  | 58 | +            **kwargs | 
|  | 59 | +        ) | 
|  | 60 | +    python_register_toolchains( | 
|  | 61 | +        name = name + "_" + default_version.replace(".", "_"), | 
|  | 62 | +        python_version = default_version, | 
|  | 63 | +        set_python_version_constraint = False, | 
|  | 64 | +        minor_mapping = minor_mapping, | 
|  | 65 | +        **kwargs | 
|  | 66 | +    ) | 
|  | 67 | + | 
|  | 68 | +    multi_toolchain_aliases( | 
|  | 69 | +        name = name, | 
|  | 70 | +        python_versions = { | 
|  | 71 | +            python_version: name + "_" + python_version.replace(".", "_") | 
|  | 72 | +            for python_version in (python_versions + [default_version]) | 
|  | 73 | +        }, | 
|  | 74 | +        minor_mapping = minor_mapping, | 
|  | 75 | +    ) | 
0 commit comments