|
| 1 | +# Copyright 2025 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 | +"""A macro used from the uv_toolchain hub repo.""" |
| 16 | + |
| 17 | +load(":toolchain_types.bzl", "UV_TOOLCHAIN_TYPE") |
| 18 | + |
| 19 | +def toolchains_hub( |
| 20 | + *, |
| 21 | + name, |
| 22 | + toolchains, |
| 23 | + implementations, |
| 24 | + target_compatible_with, |
| 25 | + target_settings): |
| 26 | + """Define the toolchains so that the lexicographical order registration is deterministic. |
| 27 | +
|
| 28 | + TODO @aignas 2025-03-09: see if this can be reused in the python toolchains. |
| 29 | +
|
| 30 | + Args: |
| 31 | + name: The prefix to all of the targets, which goes after a numeric prefix. |
| 32 | + toolchains: The toolchain names for the targets defined by this macro. |
| 33 | + The earlier occurring items take precedence over the later items if |
| 34 | + they match the target platform and target settings. |
| 35 | + implementations: The name to label mapping. |
| 36 | + target_compatible_with: The name to target_compatible_with list mapping. |
| 37 | + target_settings: The name to target_settings list mapping. |
| 38 | + """ |
| 39 | + if len(toolchains) != len(implementations): |
| 40 | + fail("Each name must have an implementation") |
| 41 | + |
| 42 | + # We are defining the toolchains so that the order of toolchain matching is |
| 43 | + # the same as the order of the toolchains, because: |
| 44 | + # * the toolchains are matched by target settings and target_compatible_with |
| 45 | + # * the first toolchain satisfying the above wins |
| 46 | + # |
| 47 | + # this means we need to register the toolchains prefixed with a number of |
| 48 | + # format 00xy, where x and y are some digits and the leading zeros to |
| 49 | + # ensure lexicographical sorting. |
| 50 | + # |
| 51 | + # Add 1 so that there is always a leading zero |
| 52 | + prefix_len = len(str(len(toolchains))) + 1 |
| 53 | + prefix = "0" * (prefix_len - 1) |
| 54 | + |
| 55 | + for i, toolchain in enumerate(toolchains): |
| 56 | + # prefix with a prefix and then truncate the string. |
| 57 | + number_prefix = "{}{}".format(prefix, i)[-prefix_len:] |
| 58 | + |
| 59 | + native.toolchain( |
| 60 | + name = "{}_{}_{}".format(number_prefix, name, toolchain), |
| 61 | + target_compatible_with = target_compatible_with.get(toolchain, []), |
| 62 | + target_settings = target_settings.get(toolchain, []), |
| 63 | + toolchain = implementations[toolchain], |
| 64 | + toolchain_type = UV_TOOLCHAIN_TYPE, |
| 65 | + ) |
0 commit comments