From a729221baa1d3de306d9ff493bdb43e161c2e481 Mon Sep 17 00:00:00 2001 From: Matt Stark Date: Mon, 30 Oct 2023 22:16:01 +1100 Subject: [PATCH] Add helper function to allow for use of cargo_bazel from module extensions. --- .../cargo_bazel_bootstrap.bzl | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/crate_universe/private/module_extensions/cargo_bazel_bootstrap.bzl b/crate_universe/private/module_extensions/cargo_bazel_bootstrap.bzl index 18a46eff25..28b6e12176 100644 --- a/crate_universe/private/module_extensions/cargo_bazel_bootstrap.bzl +++ b/crate_universe/private/module_extensions/cargo_bazel_bootstrap.bzl @@ -12,3 +12,43 @@ 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