|  | 
|  | 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 a repository for a locally installed Python runtime.""" | 
|  | 16 | + | 
|  | 17 | +load("//python/private:enum.bzl", "enum") | 
|  | 18 | +load(":repo_utils.bzl", "REPO_DEBUG_ENV_VAR", "repo_utils") | 
|  | 19 | + | 
|  | 20 | +# buildifier: disable=name-conventions | 
|  | 21 | +_OnFailure = enum( | 
|  | 22 | +    SKIP = "skip", | 
|  | 23 | +    WARN = "warn", | 
|  | 24 | +    FAIL = "fail", | 
|  | 25 | +) | 
|  | 26 | + | 
|  | 27 | +_TOOLCHAIN_IMPL_TEMPLATE = """\ | 
|  | 28 | +# Generated by python/private/local_runtime_repo.bzl | 
|  | 29 | +
 | 
|  | 30 | +load("@rules_python//python/private:local_runtime_repo_setup.bzl", "define_local_runtime_toolchain_impl") | 
|  | 31 | +
 | 
|  | 32 | +define_local_runtime_toolchain_impl( | 
|  | 33 | +    name = "local_runtime", | 
|  | 34 | +    lib_ext = "{lib_ext}", | 
|  | 35 | +    major = "{major}", | 
|  | 36 | +    minor = "{minor}", | 
|  | 37 | +    micro = "{micro}", | 
|  | 38 | +    interpreter_path = "{interpreter_path}", | 
|  | 39 | +    implementation_name = "{implementation_name}", | 
|  | 40 | +    os = "{os}", | 
|  | 41 | +) | 
|  | 42 | +""" | 
|  | 43 | + | 
|  | 44 | +def _local_runtime_repo_impl(rctx): | 
|  | 45 | +    logger = repo_utils.logger(rctx) | 
|  | 46 | +    on_failure = rctx.attr.on_failure | 
|  | 47 | + | 
|  | 48 | +    platforms_os_name = repo_utils.get_platforms_os_name(rctx) | 
|  | 49 | +    if not platforms_os_name: | 
|  | 50 | +        if on_failure == "fail": | 
|  | 51 | +            fail("Unrecognized host platform '{}': cannot determine OS constraint".format( | 
|  | 52 | +                rctx.os.name, | 
|  | 53 | +            )) | 
|  | 54 | + | 
|  | 55 | +        if on_failure == "warn": | 
|  | 56 | +            logger.warn(lambda: "Unrecognized host platform '{}': cannot determine OS constraint".format( | 
|  | 57 | +                rctx.os.name, | 
|  | 58 | +            )) | 
|  | 59 | + | 
|  | 60 | +        # else, on_failure must be skip | 
|  | 61 | +        rctx.file("BUILD.bazel", _expand_incompatible_template()) | 
|  | 62 | +        return | 
|  | 63 | + | 
|  | 64 | +    result = _resolve_interpreter_path(rctx) | 
|  | 65 | +    if not result.resolved_path: | 
|  | 66 | +        if on_failure == "fail": | 
|  | 67 | +            fail("interpreter not found: {}".format(result.describe_failure())) | 
|  | 68 | + | 
|  | 69 | +        if on_failure == "warn": | 
|  | 70 | +            logger.warn(lambda: "interpreter not found: {}".format(result.describe_failure())) | 
|  | 71 | + | 
|  | 72 | +        # else, on_failure must be skip | 
|  | 73 | +        rctx.file("BUILD.bazel", _expand_incompatible_template()) | 
|  | 74 | +        return | 
|  | 75 | +    else: | 
|  | 76 | +        interpreter_path = result.resolved_path | 
|  | 77 | + | 
|  | 78 | +    logger.info(lambda: "resolved interpreter {} to {}".format(rctx.attr.interpreter_path, interpreter_path)) | 
|  | 79 | + | 
|  | 80 | +    exec_result = repo_utils.execute_unchecked( | 
|  | 81 | +        rctx, | 
|  | 82 | +        op = "local_runtime_repo.GetPythonInfo({})".format(rctx.name), | 
|  | 83 | +        arguments = [ | 
|  | 84 | +            interpreter_path, | 
|  | 85 | +            rctx.path(rctx.attr._get_local_runtime_info), | 
|  | 86 | +        ], | 
|  | 87 | +        quiet = True, | 
|  | 88 | +    ) | 
|  | 89 | +    if exec_result.return_code != 0: | 
|  | 90 | +        if on_failure == "fail": | 
|  | 91 | +            fail("GetPythonInfo failed: {}".format(exec_result.describe_failure())) | 
|  | 92 | +        if on_failure == "warn": | 
|  | 93 | +            logger.warn(lambda: "GetPythonInfo failed: {}".format(exec_result.describe_failure())) | 
|  | 94 | + | 
|  | 95 | +        # else, on_failure must be skip | 
|  | 96 | +        rctx.file("BUILD.bazel", _expand_incompatible_template()) | 
|  | 97 | +        return | 
|  | 98 | + | 
|  | 99 | +    info = json.decode(exec_result.stdout) | 
|  | 100 | +    logger.info(lambda: _format_get_info_result(info)) | 
|  | 101 | + | 
|  | 102 | +    # NOTE: Keep in sync with recursive glob in define_local_runtime_toolchain_impl | 
|  | 103 | +    repo_utils.watch_tree(rctx, rctx.path(info["include"])) | 
|  | 104 | + | 
|  | 105 | +    # The cc_library.includes values have to be non-absolute paths, otherwise | 
|  | 106 | +    # the toolchain will give an error. Work around this error by making them | 
|  | 107 | +    # appear as part of this repo. | 
|  | 108 | +    rctx.symlink(info["include"], "include") | 
|  | 109 | + | 
|  | 110 | +    # NOTE: For some reason (unknown why), the values found may refer to | 
|  | 111 | +    # .a files (static libraries) instead of .so (shared libraries) files. | 
|  | 112 | +    shared_lib_names = [ | 
|  | 113 | +        info["PY3LIBRARY"],  # libpython3.so | 
|  | 114 | +        info["LDLIBRARY"],  # libpython3.11.so | 
|  | 115 | +        info["INSTSONAME"],  # libpython3.11.so.1.0 | 
|  | 116 | +    ] | 
|  | 117 | + | 
|  | 118 | +    # In some cases, the value may be empty. Not clear why. | 
|  | 119 | +    shared_lib_names = [v for v in shared_lib_names if v] | 
|  | 120 | + | 
|  | 121 | +    # In some cases, the same value is returned or multiple keys. Not clear why. | 
|  | 122 | +    shared_lib_names = {v: None for v in shared_lib_names}.keys() | 
|  | 123 | + | 
|  | 124 | +    # It's not entirely clear how to get the directory with libraries. | 
|  | 125 | +    # There's several types of libraries with different names and a plethora | 
|  | 126 | +    # of settings. | 
|  | 127 | +    # https://stackoverflow.com/questions/47423246/get-pythons-lib-path | 
|  | 128 | +    # For now, it seems LIBDIR has what is needed, so just use that. | 
|  | 129 | +    shared_lib_dir = info["LIBDIR"] | 
|  | 130 | + | 
|  | 131 | +    # The specific files are symlinked instead of the whole directory | 
|  | 132 | +    # because it can point to a directory that has more than just | 
|  | 133 | +    # the Python runtime shared libraries, e.g. /usr/lib, or a Python | 
|  | 134 | +    # specific directory with pip-installed shared libraries. | 
|  | 135 | +    rctx.report_progress("Symlinking external Python shared libraries") | 
|  | 136 | +    for name in shared_lib_names: | 
|  | 137 | +        origin = rctx.path("{}/{}".format(shared_lib_dir, name)) | 
|  | 138 | + | 
|  | 139 | +        # The reported names don't always exist; it depends on the particulars | 
|  | 140 | +        # of the runtime installation. | 
|  | 141 | +        if origin.exists: | 
|  | 142 | +            repo_utils.watch(rctx, origin) | 
|  | 143 | +            rctx.symlink(origin, "lib/" + name) | 
|  | 144 | + | 
|  | 145 | +    rctx.file("WORKSPACE", "") | 
|  | 146 | +    rctx.file("MODULE.bazel", "") | 
|  | 147 | +    rctx.file("REPO.bazel", "") | 
|  | 148 | +    rctx.file("BUILD.bazel", _TOOLCHAIN_IMPL_TEMPLATE.format( | 
|  | 149 | +        major = info["major"], | 
|  | 150 | +        minor = info["minor"], | 
|  | 151 | +        micro = info["micro"], | 
|  | 152 | +        interpreter_path = interpreter_path, | 
|  | 153 | +        lib_ext = info["SHLIB_SUFFIX"], | 
|  | 154 | +        implementation_name = info["implementation_name"], | 
|  | 155 | +        os = "@platforms//os:{}".format(repo_utils.get_platforms_os_name(rctx)), | 
|  | 156 | +    )) | 
|  | 157 | + | 
|  | 158 | +local_runtime_repo = repository_rule( | 
|  | 159 | +    implementation = _local_runtime_repo_impl, | 
|  | 160 | +    doc = """ | 
|  | 161 | +Use a locally installed Python runtime as a toolchain implementation. | 
|  | 162 | +
 | 
|  | 163 | +Note this uses the runtime as a *platform runtime*. A platform runtime means | 
|  | 164 | +means targets don't include the runtime itself as part of their runfiles or | 
|  | 165 | +inputs. Instead, users must assure that where the targets run have the runtime | 
|  | 166 | +pre-installed or otherwise available. | 
|  | 167 | +
 | 
|  | 168 | +This results in lighter weight binaries (in particular, Bazel doesn't have to | 
|  | 169 | +create thousands of files for every `py_test`), at the risk of having to rely on | 
|  | 170 | +a system having the necessary Python installed. | 
|  | 171 | +""", | 
|  | 172 | +    attrs = { | 
|  | 173 | +        "interpreter_path": attr.string( | 
|  | 174 | +            doc = """ | 
|  | 175 | +An absolute path or program name on the `PATH` env var. | 
|  | 176 | +
 | 
|  | 177 | +Values with slashes are assumed to be the path to a program. Otherwise, it is | 
|  | 178 | +treated as something to search for on `PATH` | 
|  | 179 | +
 | 
|  | 180 | +Note that, when a plain program name is used, the path to the interpreter is | 
|  | 181 | +resolved at repository evalution time, not runtime of any resulting binaries. | 
|  | 182 | +""", | 
|  | 183 | +            default = "python3", | 
|  | 184 | +        ), | 
|  | 185 | +        "on_failure": attr.string( | 
|  | 186 | +            default = _OnFailure.SKIP, | 
|  | 187 | +            values = sorted(_OnFailure.__members__.values()), | 
|  | 188 | +            doc = """ | 
|  | 189 | +How to handle errors when trying to automatically determine settings. | 
|  | 190 | +
 | 
|  | 191 | +* `skip` will silently skip creating a runtime. Instead, a non-functional | 
|  | 192 | +  runtime will be generated and marked as incompatible so it cannot be used. | 
|  | 193 | +  This is best if a local runtime is known not to work or be available | 
|  | 194 | +  in certain cases and that's OK. e.g., one use windows paths when there | 
|  | 195 | +  are people running on linux. | 
|  | 196 | +* `warn` will print a warning message. This is useful when you expect | 
|  | 197 | +  a runtime to be available, but are OK with it missing and falling back | 
|  | 198 | +  to some other runtime. | 
|  | 199 | +* `fail` will result in a failure. This is only recommended if you must | 
|  | 200 | +  ensure the runtime is available. | 
|  | 201 | +""", | 
|  | 202 | +        ), | 
|  | 203 | +        "_get_local_runtime_info": attr.label( | 
|  | 204 | +            allow_single_file = True, | 
|  | 205 | +            default = "//python/private:get_local_runtime_info.py", | 
|  | 206 | +        ), | 
|  | 207 | +        "_rule_name": attr.string(default = "local_runtime_repo"), | 
|  | 208 | +    }, | 
|  | 209 | +    environ = ["PATH", REPO_DEBUG_ENV_VAR], | 
|  | 210 | +) | 
|  | 211 | + | 
|  | 212 | +def _expand_incompatible_template(): | 
|  | 213 | +    return _TOOLCHAIN_IMPL_TEMPLATE.format( | 
|  | 214 | +        interpreter_path = "/incompatible", | 
|  | 215 | +        implementation_name = "incompatible", | 
|  | 216 | +        lib_ext = "incompatible", | 
|  | 217 | +        major = "0", | 
|  | 218 | +        minor = "0", | 
|  | 219 | +        micro = "0", | 
|  | 220 | +        os = "@platforms//:incompatible", | 
|  | 221 | +    ) | 
|  | 222 | + | 
|  | 223 | +def _resolve_interpreter_path(rctx): | 
|  | 224 | +    """Find the absolute path for an interpreter. | 
|  | 225 | +
 | 
|  | 226 | +    Args: | 
|  | 227 | +        rctx: A repository_ctx object | 
|  | 228 | +
 | 
|  | 229 | +    Returns: | 
|  | 230 | +        `struct` with the following fields: | 
|  | 231 | +        * `resolved_path`: `path` object of a path that exists | 
|  | 232 | +        * `describe_failure`: `Callable | None`. If a path that doesn't exist, | 
|  | 233 | +          returns a description of why it couldn't be resolved | 
|  | 234 | +        A path object or None. The path may not exist. | 
|  | 235 | +    """ | 
|  | 236 | +    if "/" not in rctx.attr.interpreter_path and "\\" not in rctx.attr.interpreter_path: | 
|  | 237 | +        # Provide a bit nicer integration with pyenv: recalculate the runtime if the | 
|  | 238 | +        # user changes the python version using e.g. `pyenv shell` | 
|  | 239 | +        repo_utils.getenv(rctx, "PYENV_VERSION") | 
|  | 240 | +        result = repo_utils.which_unchecked(rctx, rctx.attr.interpreter_path) | 
|  | 241 | +        resolved_path = result.binary | 
|  | 242 | +        describe_failure = result.describe_failure | 
|  | 243 | +    else: | 
|  | 244 | +        repo_utils.watch(rctx, rctx.attr.interpreter_path) | 
|  | 245 | +        resolved_path = rctx.path(rctx.attr.interpreter_path) | 
|  | 246 | +        if not resolved_path.exists: | 
|  | 247 | +            describe_failure = lambda: "Path not found: {}".format(repr(rctx.attr.interpreter_path)) | 
|  | 248 | +        else: | 
|  | 249 | +            describe_failure = None | 
|  | 250 | + | 
|  | 251 | +    return struct( | 
|  | 252 | +        resolved_path = resolved_path, | 
|  | 253 | +        describe_failure = describe_failure, | 
|  | 254 | +    ) | 
|  | 255 | + | 
|  | 256 | +def _format_get_info_result(info): | 
|  | 257 | +    lines = ["GetPythonInfo result:"] | 
|  | 258 | +    for key, value in sorted(info.items()): | 
|  | 259 | +        lines.append("  {}: {}".format(key, value if value != "" else "<empty string>")) | 
|  | 260 | +    return "\n".join(lines) | 
0 commit comments