fork/rules_rust

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

David TolnayMake get_host_triple public to get a triple from Bazel's repository_ctx (#1289)58627f59

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