-
Notifications
You must be signed in to change notification settings - Fork 442
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support cargo_bazel_bootstrap with bzlmod.
This is required for crate_universe.
- Loading branch information
Showing
9 changed files
with
149 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
49 changes: 49 additions & 0 deletions
49
crate_universe/private/module_extensions/cargo_bazel_bootstrap.bzl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
"""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, | ||
) | ||
|
||
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")) | ||
|
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,5 @@ | ||
"Module extensions for using rules_rust with bzlmod" | ||
|
||
load( | ||
"//rust/private:repository_utils.bzl", | ||
"DEFAULT_EXTRA_TARGET_TRIPLES", | ||
"DEFAULT_NIGHTLY_VERSION", | ||
"DEFAULT_STATIC_RUST_URL_TEMPLATES", | ||
) | ||
load(":repositories.bzl", "rust_register_toolchains") | ||
load("//rust/private/module_extensions:toolchain.bzl", _rust = "rust") | ||
|
||
def _rust_impl(ctx): | ||
mod = ctx.modules[0] | ||
for toolchain in mod.tags.toolchain: | ||
rust_register_toolchains( | ||
dev_components = toolchain.dev_components, | ||
edition = toolchain.edition, | ||
allocator_library = toolchain.allocator_library, | ||
rustfmt_version = toolchain.rustfmt_version, | ||
rust_analyzer_version = toolchain.rust_analyzer_version, | ||
sha256s = toolchain.sha256s, | ||
extra_target_triples = toolchain.extra_target_triples, | ||
urls = toolchain.urls, | ||
versions = toolchain.versions, | ||
register_toolchains = False, | ||
) | ||
|
||
rust_toolchain = tag_class(attrs = { | ||
"allocator_library": attr.string(), | ||
"dev_components": attr.bool(default = False), | ||
"edition": attr.string(), | ||
"extra_target_triples": attr.string_list(default = DEFAULT_EXTRA_TARGET_TRIPLES), | ||
"rust_analyzer_version": attr.string(), | ||
"rustfmt_version": attr.string(default = DEFAULT_NIGHTLY_VERSION), | ||
"sha256s": attr.string_dict(), | ||
"urls": attr.string_list(default = DEFAULT_STATIC_RUST_URL_TEMPLATES), | ||
"versions": attr.string_list(default = []), | ||
}) | ||
|
||
rust = module_extension( | ||
implementation = _rust_impl, | ||
tag_classes = {"toolchain": rust_toolchain}, | ||
) | ||
rust = _rust |
Empty file.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
"""Module extension for rust toolchain.""" | ||
|
||
load("//rust:defs.bzl", "rust_common") | ||
load("//rust:repositories.bzl", "rust_register_toolchains", "rust_toolchain_tools_repository") | ||
load("//rust/platform:triple.bzl", "get_host_triple") | ||
load( | ||
"//rust/private:repository_utils.bzl", | ||
"DEFAULT_EXTRA_TARGET_TRIPLES", | ||
"DEFAULT_NIGHTLY_VERSION", | ||
"DEFAULT_STATIC_RUST_URL_TEMPLATES", | ||
) | ||
|
||
def _rust_impl(module_ctx): | ||
# Allow the root module to define host tools. Otherwise, we'll fall back to | ||
# the one defined in rules_rust. | ||
for mod in module_ctx.modules: | ||
if mod.tags.host_tools: | ||
host_tools = mod.tags.host_tools[0] | ||
host_triple = get_host_triple(module_ctx) | ||
|
||
rust_toolchain_tools_repository( | ||
name = "rust_host_tools", | ||
exec_triple = host_triple.str, | ||
target_triple = host_triple.str, | ||
allocator_library = host_tools.allocator_library, | ||
dev_components = host_tools.dev_components, | ||
edition = host_tools.edition, | ||
rustfmt_version = host_tools.rustfmt_version, | ||
sha256s = host_tools.sha256s, | ||
urls = host_tools.urls, | ||
version = host_tools.version, | ||
) | ||
break | ||
|
||
mod = module_ctx.modules[0] | ||
for toolchain in mod.tags.toolchain: | ||
rust_register_toolchains( | ||
dev_components = toolchain.dev_components, | ||
edition = toolchain.edition, | ||
allocator_library = toolchain.allocator_library, | ||
rustfmt_version = toolchain.rustfmt_version, | ||
rust_analyzer_version = toolchain.rust_analyzer_version, | ||
sha256s = toolchain.sha256s, | ||
extra_target_triples = toolchain.extra_target_triples, | ||
urls = toolchain.urls, | ||
versions = toolchain.versions, | ||
register_toolchains = False, | ||
) | ||
|
||
common_kwargs = dict( | ||
allocator_library = attr.string(), | ||
dev_components = attr.bool(default = False), | ||
edition = attr.string(), | ||
rustfmt_version = attr.string(default = DEFAULT_NIGHTLY_VERSION), | ||
sha256s = attr.string_dict(), | ||
urls = attr.string_list(default = DEFAULT_STATIC_RUST_URL_TEMPLATES), | ||
) | ||
|
||
rust_toolchain = tag_class(attrs = dict( | ||
extra_target_triples = attr.string_list(default = DEFAULT_EXTRA_TARGET_TRIPLES), | ||
rust_analyzer_version = attr.string(), | ||
versions = attr.string_list(default = []), | ||
**common_kwargs | ||
)) | ||
|
||
rust_host_tools = tag_class(attrs = dict( | ||
version = attr.string(default = rust_common.default_version), | ||
**common_kwargs | ||
)) | ||
|
||
rust = module_extension( | ||
implementation = _rust_impl, | ||
tag_classes = { | ||
"host_tools": rust_host_tools, | ||
"toolchain": rust_toolchain, | ||
}, | ||
) |