|
| 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 | +"""Create toolchain alias targets.""" |
| 16 | + |
| 17 | +load("@rules_python//python:versions.bzl", "PLATFORMS") |
| 18 | + |
| 19 | +def toolchain_aliases(*, name, platforms, visibility = None, native = native): |
| 20 | + """Cretae toolchain aliases for the python toolchains. |
| 21 | +
|
| 22 | + Args: |
| 23 | + name: {type}`str` The name of the current repository. |
| 24 | + platforms: {type}`platforms` The list of platforms that are supported |
| 25 | + for the current toolchain repository. |
| 26 | + visibility: {type}`list[Target] | None` The visibility of the aliases. |
| 27 | + native: The native struct used in the macro, useful for testing. |
| 28 | + """ |
| 29 | + for platform in PLATFORMS.keys(): |
| 30 | + if platform not in platforms: |
| 31 | + continue |
| 32 | + |
| 33 | + native.config_setting( |
| 34 | + name = platform, |
| 35 | + flag_values = PLATFORMS[platform].flag_values, |
| 36 | + constraint_values = PLATFORMS[platform].compatible_with, |
| 37 | + visibility = ["//visibility:private"], |
| 38 | + ) |
| 39 | + |
| 40 | + prefix = name |
| 41 | + for name in [ |
| 42 | + "files", |
| 43 | + "includes", |
| 44 | + "libpython", |
| 45 | + "py3_runtime", |
| 46 | + "python_headers", |
| 47 | + "python_runtimes", |
| 48 | + ]: |
| 49 | + native.alias( |
| 50 | + name = name, |
| 51 | + actual = select({ |
| 52 | + ":" + platform: "@{}_{}//:{}".format(prefix, platform, name) |
| 53 | + for platform in platforms |
| 54 | + }), |
| 55 | + visibility = visibility, |
| 56 | + ) |
| 57 | + |
| 58 | + native.alias( |
| 59 | + name = "python3", |
| 60 | + actual = select({ |
| 61 | + ":" + platform: "@{}_{}//:{}".format(prefix, platform, "python.exe" if "windows" in platform else "bin/python3") |
| 62 | + for platform in platforms |
| 63 | + }), |
| 64 | + visibility = visibility, |
| 65 | + ) |
| 66 | + native.alias( |
| 67 | + name = "pip", |
| 68 | + actual = select({ |
| 69 | + ":" + platform: "@{}_{}//:python_runtimes".format(prefix, platform) |
| 70 | + for platform in platforms |
| 71 | + if "windows" not in platform |
| 72 | + }), |
| 73 | + visibility = visibility, |
| 74 | + ) |
0 commit comments