aboutsummaryrefslogtreecommitdiff
path: root/crate_universe/private/module_extensions/cargo_bazel_bootstrap.bzl
blob: 28b6e121767005343f0ad5ed114290c1a247b18f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""Module extension for bootstrapping cargo-bazel."""

load("//crate_universe:deps_bootstrap.bzl", _cargo_bazel_bootstrap_repo_rule = "cargo_bazel_bootstrap")

def _cargo_bazel_bootstrap_impl(_):
    _cargo_bazel_bootstrap_repo_rule(
        rust_toolchain_cargo_template = "@rust_host_tools//:bin/{tool}",
        rust_toolchain_rustc_template = "@rust_host_tools//:bin/{tool}",
    )

cargo_bazel_bootstrap = module_extension(
    implementation = _cargo_bazel_bootstrap_impl,
    doc = """Module extension to generate the cargo_bazel binary.""",
)

def get_cargo_bazel_runner(module_ctx):
    """A helper function to allow executing cargo_bazel in module extensions.

    Args:
        module_ctx: The module extension's context.

    Returns:
        A function that can be called to execute cargo_bazel.
    """

    cargo_path = str(module_ctx.path(Label("@rust_host_tools//:bin/cargo")))
    rustc_path = str(module_ctx.path(Label("@rust_host_tools//:bin/rustc")))
    cargo_bazel = module_ctx.path(Label("@cargo_bazel_bootstrap//:cargo-bazel"))

    # Placing this as a nested function allows users to call this right at the
    # start of a module extension, thus triggering any restarts as early as
    # possible (since module_ctx.path triggers restarts).
    def run(args, env = {}, timeout = 600):
        final_args = [cargo_bazel]
        final_args.extend(args)
        final_args.extend([
            "--cargo",
            cargo_path,
            "--rustc",
            rustc_path,
        ])
        result = module_ctx.execute(
            final_args,
            environment = dict(CARGO = cargo_path, RUSTC = rustc_path, **env),
            timeout = timeout,
        )
        if result.return_code != 0:
            if result.stdout:
                print("Stdout:", result.stdout)  # buildifier: disable=print
            pretty_args = " ".join([str(arg) for arg in final_args])
            fail("%s returned with exit code %d:\n%s" % (pretty_args, result.return_code, result.stderr))
        return result

    return run