fork/rules_rust

git clone https://git.julia.blue/fork/rules_rust

UebelAndrecrate_universe support for individually updating packages. (#1426)3d65214d

0.7.0
3.8 KiB113 linesraw
1"""Common utilities useful for unifying the behavior of different parts of `cargo-bazel`."""
2
3# buildifier: disable=bzl-visibility
4load("//cargo/private:cargo_utils.bzl", _rust_get_rust_tools = "get_rust_tools")
5load("//rust/platform:triple.bzl", _get_host_triple = "get_host_triple")
6
7get_host_triple = _get_host_triple
8
9CARGO_BAZEL_ISOLATED = "CARGO_BAZEL_ISOLATED"
10CARGO_BAZEL_REPIN = "CARGO_BAZEL_REPIN"
11REPIN = "REPIN"
12
13REPIN_ENV_VARS = [
14    CARGO_BAZEL_REPIN,
15    REPIN,
16]
17
18_EXECUTE_ERROR_MESSAGE = """\
19Command {args} failed with exit code {exit_code}.
20STDOUT ------------------------------------------------------------------------
21{stdout}
22STDERR ------------------------------------------------------------------------
23{stderr}
24"""
25
26def execute(repository_ctx, args, env = {}):
27    """A heler macro for executing some arguments and displaying nicely formatted errors
28
29    Args:
30        repository_ctx (repository_ctx): The rule's context object.
31        args (list): A list of strings which act as `argv` for execution.
32        env (dict, optional): Environment variables to set in the execution environment.
33
34    Returns:
35        struct: The results of `repository_ctx.execute`
36    """
37    result = repository_ctx.execute(
38        args,
39        environment = env,
40        quiet = repository_ctx.attr.quiet,
41    )
42
43    if result.return_code:
44        fail(_EXECUTE_ERROR_MESSAGE.format(
45            args = args,
46            exit_code = result.return_code,
47            stdout = result.stdout,
48            stderr = result.stderr,
49        ))
50
51    return result
52
53def get_rust_tools(repository_ctx, host_triple):
54    """Retrieve a cargo and rustc binary based on the host triple.
55
56    Args:
57        repository_ctx (repository_ctx): The rule's context object.
58        host_triple (struct): A `@rules_rust//rust:triple.bzl%triple` object.
59
60    Returns:
61        struct: A struct containing the expected rust tools
62    """
63
64    # This is a bit hidden but to ensure Cargo behaves consistently based
65    # on the user provided config file, the config file is installed into
66    # the `CARGO_HOME` path. This is done so here since fetching tools
67    # is expected to always occur before any subcommands are run.
68    if repository_ctx.attr.isolated and repository_ctx.attr.cargo_config:
69        cargo_home = _cargo_home_path(repository_ctx)
70        cargo_home_config = repository_ctx.path("{}/config.toml".format(cargo_home))
71        cargo_config = repository_ctx.path(repository_ctx.attr.cargo_config)
72        repository_ctx.symlink(cargo_config, cargo_home_config)
73
74    return _rust_get_rust_tools(
75        cargo_template = repository_ctx.attr.rust_toolchain_cargo_template,
76        rustc_template = repository_ctx.attr.rust_toolchain_rustc_template,
77        host_triple = host_triple,
78        version = repository_ctx.attr.rust_version,
79    )
80
81def _cargo_home_path(repository_ctx):
82    """Define a path within the repository to use in place of `CARGO_HOME`
83
84    Args:
85        repository_ctx (repository_ctx): The rules context object
86
87    Returns:
88        path: The path to a directory to use as `CARGO_HOME`
89    """
90    return repository_ctx.path(".cargo_home")
91
92def cargo_environ(repository_ctx):
93    """Define Cargo environment varables for use with `cargo-bazel`
94
95    Args:
96        repository_ctx (repository_ctx): The rules context object
97
98    Returns:
99        dict: A set of environment variables for `cargo-bazel` executions
100    """
101    env = dict()
102
103    if CARGO_BAZEL_ISOLATED in repository_ctx.os.environ:
104        if repository_ctx.os.environ[CARGO_BAZEL_ISOLATED].lower() in ["true", "1", "yes", "on"]:
105            env.update({
106                "CARGO_HOME": str(_cargo_home_path(repository_ctx)),
107            })
108    elif repository_ctx.attr.isolated:
109        env.update({
110            "CARGO_HOME": str(_cargo_home_path(repository_ctx)),
111        })
112
113    return env