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

Register a default rust toolchain. #2624

Merged
merged 1 commit into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
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
3 changes: 0 additions & 3 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -160,17 +160,14 @@ register_toolchains(

register_toolchains(
"//proto/protobuf:default-proto-toolchain",
dev_dependency = True,
)

register_toolchains(
"//proto/prost:default_prost_toolchain",
dev_dependency = True,
)

register_toolchains(
"//bindgen:default_bindgen_toolchain",
dev_dependency = True,
)

rust_host_tools = use_extension("//rust:extensions.bzl", "rust_host_tools")
Expand Down
9 changes: 0 additions & 9 deletions examples/bzlmod/hello_world/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,6 @@ local_path_override(
path = "../../..",
)

rust = use_extension("@rules_rust//rust:extensions.bzl", "rust")
rust.toolchain(edition = "2021")
use_repo(
rust,
"rust_toolchains",
)

register_toolchains("@rust_toolchains//:all")

# To do third party dependencies, you have multiple options:

# Option 1: Fully transient (Cargo.toml / Cargo.lock as source of truth).
Expand Down
59 changes: 15 additions & 44 deletions examples/bzlmod/hello_world/MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 17 additions & 36 deletions rust/extensions.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -11,47 +11,28 @@ load(
"DEFAULT_STATIC_RUST_URL_TEMPLATES",
)

_EXAMPLE_TOOLCHAIN = """
rust = use_extension("@rules_rust//rust:extensions.bzl", "rust")
rust.toolchain(
edition = "2021",
versions = ["1.70.2"],
)
use_repo(rust, "rust_toolchains")
register_toolchains("@rust_toolchains//:all")"""

_TRANSITIVE_DEP_ERR = """
Your transitive dependency %s is using rules_rust, so you need to define a rust toolchain.
To do so, you will need to add the following to your root MODULE.bazel. For example:

bazel_dep(name = "rules_rust", version = "<rules_rust version>")
""" + _EXAMPLE_TOOLCHAIN

_TOOLCHAIN_ERR = """
Please add at least one toolchain to your root MODULE.bazel. For example:
""" + _EXAMPLE_TOOLCHAIN

def _find_root(module_ctx):
def _find_modules(module_ctx):
root = None
our_module = None
for mod in module_ctx.modules:
if mod.is_root:
root = mod
if mod.name == "rules_rust":
our_module = mod
if root == None:
root = our_module
if our_module == None:
fail("Unable to find rules_rust module")

return root
return root, our_module

def _rust_impl(module_ctx):
# Toolchain configuration is only allowed in the root module.
# It would be very confusing (and a security concern) if I was using the
# default rust toolchains, then when I added a module built on rust, I was
# suddenly using a custom rustc.
root = _find_root(module_ctx)

if not root:
fail(_TRANSITIVE_DEP_ERR % module_ctx.modules[0].name)
# Toolchain configuration is only allowed in the root module, or in
# rules_rust.
# See https://github.com/bazelbuild/bazel/discussions/22024 for discussion.
root, rules_rust = _find_modules(module_ctx)

toolchains = root.tags.toolchain
if not toolchains:
fail(_TOOLCHAIN_ERR)
toolchains = root.tags.toolchain or rules_rust.tags.toolchain

for toolchain in toolchains:
rust_register_toolchains(
Expand Down Expand Up @@ -134,9 +115,9 @@ rust = module_extension(
# This is a separate module extension so that only the host tools are
# marked as reproducible and os and arch dependent
def _rust_host_tools_impl(module_ctx):
root = _find_root(module_ctx)
root, _ = _find_modules(module_ctx)

if root != None and len(root.tags.host_tools) == 1:
if len(root.tags.host_tools) == 1:
attrs = root.tags.host_tools[0]

iso_date = None
Expand All @@ -156,7 +137,7 @@ def _rust_host_tools_impl(module_ctx):
"urls": attrs.urls,
"version": version,
}
elif root == None or not root.tags.host_tools:
elif not root.tags.host_tools:
host_tools = {
"version": rust_common.default_version,
}
Expand Down
Loading