|
| 1 | +# Copyright 2022 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 a repository to hold the toolchains. |
| 16 | +
|
| 17 | +This follows guidance here: |
| 18 | +https://docs.bazel.build/versions/main/skylark/deploying.html#registering-toolchains |
| 19 | +
|
| 20 | +The "complex computation" in our case is simply downloading large artifacts. |
| 21 | +This guidance tells us how to avoid that: we put the toolchain targets in the |
| 22 | +alias repository with only the toolchain attribute pointing into the |
| 23 | +platform-specific repositories. |
| 24 | +""" |
| 25 | + |
| 26 | +load( |
| 27 | + "//python:versions.bzl", |
| 28 | + "LINUX_NAME", |
| 29 | + "MACOS_NAME", |
| 30 | + "PLATFORMS", |
| 31 | + "WINDOWS_NAME", |
| 32 | +) |
| 33 | + |
| 34 | +def _toolchains_repo_impl(rctx): |
| 35 | + build_content = """\ |
| 36 | +# Generated by toolchains_repo.bzl |
| 37 | +# |
| 38 | +# These can be registered in the workspace file or passed to --extra_toolchains |
| 39 | +# flag. By default all these toolchains are registered by the |
| 40 | +# python_register_toolchains macro so you don't normally need to interact with |
| 41 | +# these targets. |
| 42 | +
|
| 43 | +""" |
| 44 | + |
| 45 | + for [platform, meta] in PLATFORMS.items(): |
| 46 | + build_content += """\ |
| 47 | +# Bazel selects this toolchain to get a Python interpreter |
| 48 | +# for executing build actions. |
| 49 | +toolchain( |
| 50 | + name = "{platform}_toolchain", |
| 51 | + exec_compatible_with = {compatible_with}, |
| 52 | + toolchain = "@{user_repository_name}_{platform}//:python_runtimes", |
| 53 | + toolchain_type = "@bazel_tools//tools/python:toolchain_type", |
| 54 | +) |
| 55 | +""".format( |
| 56 | + platform = platform, |
| 57 | + name = rctx.attr.name, |
| 58 | + user_repository_name = rctx.attr.user_repository_name, |
| 59 | + compatible_with = meta.compatible_with, |
| 60 | + ) |
| 61 | + |
| 62 | + rctx.file("BUILD.bazel", build_content) |
| 63 | + |
| 64 | +toolchains_repo = repository_rule( |
| 65 | + _toolchains_repo_impl, |
| 66 | + doc = "Creates a repository with toolchain definitions for all known platforms " + |
| 67 | + "which can be registered or selected.", |
| 68 | + attrs = { |
| 69 | + "user_repository_name": attr.string(doc = "what the user chose for the base name"), |
| 70 | + }, |
| 71 | +) |
| 72 | + |
| 73 | +def _resolved_interpreter_os_alias_impl(rctx): |
| 74 | + (os_name, arch) = _host_os_arch(rctx) |
| 75 | + |
| 76 | + host_platform = None |
| 77 | + for platform, meta in PLATFORMS.items(): |
| 78 | + if meta.os_name == os_name and meta.arch == arch: |
| 79 | + host_platform = platform |
| 80 | + if not host_platform: |
| 81 | + fail("No platform declared for host OS {} on arch {}".format(os_name, arch)) |
| 82 | + |
| 83 | + is_windows = (os_name == WINDOWS_NAME) |
| 84 | + python3_binary_path = "python.exe" if is_windows else "bin/python3" |
| 85 | + |
| 86 | + # Base BUILD file for this repository. |
| 87 | + build_contents = """\ |
| 88 | +# Generated by python/repositories.bzl |
| 89 | +package(default_visibility = ["//visibility:public"]) |
| 90 | +alias(name = "files", actual = "@{py_repository}_{host_platform}//:files") |
| 91 | +alias(name = "py3_runtime", actual = "@{py_repository}_{host_platform}//:py3_runtime") |
| 92 | +alias(name = "python_runtimes", actual = "@{py_repository}_{host_platform}//:python_runtimes") |
| 93 | +alias(name = "python3", actual = "@{py_repository}_{host_platform}//:{python3_binary_path}") |
| 94 | +""".format( |
| 95 | + py_repository = rctx.attr.user_repository_name, |
| 96 | + host_platform = host_platform, |
| 97 | + python3_binary_path = python3_binary_path, |
| 98 | + ) |
| 99 | + if not is_windows: |
| 100 | + build_contents += """\ |
| 101 | +alias(name = "pip", actual = "@{py_repository}_{host_platform}//:bin/pip") |
| 102 | +""".format( |
| 103 | + py_repository = rctx.attr.user_repository_name, |
| 104 | + host_platform = host_platform, |
| 105 | + ) |
| 106 | + rctx.file("BUILD.bazel", build_contents) |
| 107 | + |
| 108 | + # Expose a Starlark file so rules can know what host platform we used and where to find an interpreter |
| 109 | + # when using repository_ctx.path, which doesn't understand aliases. |
| 110 | + rctx.file("defs.bzl", content = """\ |
| 111 | +# Generated by python/repositories.bzl |
| 112 | +host_platform = "{host_platform}" |
| 113 | +interpreter = "@{py_repository}_{host_platform}//:{python3_binary_path}" |
| 114 | +""".format( |
| 115 | + py_repository = rctx.attr.user_repository_name, |
| 116 | + host_platform = host_platform, |
| 117 | + python3_binary_path = python3_binary_path, |
| 118 | + )) |
| 119 | + |
| 120 | +resolved_interpreter_os_alias = repository_rule( |
| 121 | + _resolved_interpreter_os_alias_impl, |
| 122 | + doc = """Creates a repository with a shorter name meant for the host platform, which contains |
| 123 | + a BUILD.bazel file declaring aliases to the host platform's targets. |
| 124 | + """, |
| 125 | + attrs = { |
| 126 | + "user_repository_name": attr.string( |
| 127 | + mandatory = True, |
| 128 | + doc = "The base name for all created repositories, like 'python38'.", |
| 129 | + ), |
| 130 | + }, |
| 131 | +) |
| 132 | + |
| 133 | +def _host_os_arch(rctx): |
| 134 | + """Infer the host OS name and arch from a repository context. |
| 135 | +
|
| 136 | + Args: |
| 137 | + rctx: Bazel's repository_ctx. |
| 138 | + Returns: |
| 139 | + A tuple with the host OS name and arch. |
| 140 | + """ |
| 141 | + os_name = rctx.os.name |
| 142 | + |
| 143 | + # We assume the arch for Windows is always x86_64. |
| 144 | + if "windows" in os_name.lower(): |
| 145 | + arch = "x86_64" |
| 146 | + |
| 147 | + # Normalize the os_name. E.g. os_name could be "OS windows server 2019". |
| 148 | + os_name = WINDOWS_NAME |
| 149 | + else: |
| 150 | + # This is not ideal, but bazel doesn't directly expose arch. |
| 151 | + arch = rctx.execute(["uname", "-m"]).stdout.strip() |
| 152 | + |
| 153 | + # Normalize the os_name. |
| 154 | + if "mac" in os_name.lower(): |
| 155 | + os_name = MACOS_NAME |
| 156 | + elif "linux" in os_name.lower(): |
| 157 | + os_name = LINUX_NAME |
| 158 | + |
| 159 | + return (os_name, arch) |
0 commit comments