Skip to content

Commit

Permalink
Register a default rust toolchain. (#2624)
Browse files Browse the repository at this point in the history
See bazelbuild/bazel#22024 for details.
  • Loading branch information
matts1 authored Apr 24, 2024
1 parent df7a655 commit 29ec07a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 92 deletions.
3 changes: 0 additions & 3 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -164,17 +164,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

0 comments on commit 29ec07a

Please sign in to comment.