Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow using cargo_bazel_bootstrap from module extensions #2385

Merged
merged 1 commit into from
Jan 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions crate_universe/private/module_extensions/cargo_bazel_bootstrap.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading