fork/rules_rust

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

Maximilian GoisserAdd `CARGO_BAZEL_REPIN_ONLY` repinning allowlist (#1798)f651cd18

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