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 committed 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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"lockFileVersion": 6,
"moduleFileHash": "97fc4632c12fa1c1dc76d29301d11258c4d118d7080c0caf767b093b05361be0",
"moduleFileHash": "626730f6bfed207274c0f53502d56fd955e96d92ecd46b177e51164bd7c88cca",
"flags": {
"cmdRegistries": [
"https://bcr.bazel.build/"
Expand All @@ -13,7 +13,7 @@
"compatibilityMode": "ERROR"
},
"localOverrideHashes": {
"rules_rust": "b01d8fa2d0cadc2b4b427e5008dfef525aa9e17ddb781848b17f0af39e09c076",
"rules_rust": "87746193d9d8812a75923cc19772c2bb61b1ff870adfe6a70863a2df47188864",
"bazel_tools": "1ae69322ac3823527337acf02016e8ee95813d8d356f47060255b8956fa642f0"
},
"moduleDepGraph": {
Expand All @@ -23,47 +23,15 @@
"key": "<root>",
"repoName": "hello_world_example",
"executionPlatformsToRegister": [],
"toolchainsToRegister": [
"@rust_toolchains//:all"
],
"toolchainsToRegister": [],
"extensionUsages": [
{
"extensionBzlFile": "@rules_rust//rust:extensions.bzl",
"extensionName": "rust",
"usingModule": "<root>",
"location": {
"file": "@@//:MODULE.bazel",
"line": 22,
"column": 21
},
"imports": {
"rust_toolchains": "rust_toolchains"
},
"devImports": [],
"tags": [
{
"tagName": "toolchain",
"attributeValues": {
"edition": "2021"
},
"devDependency": false,
"location": {
"file": "@@//:MODULE.bazel",
"line": 23,
"column": 15
}
}
],
"hasDevUseExtension": false,
"hasNonDevUseExtension": true
},
{
"extensionBzlFile": "@rules_rust//crate_universe:extension.bzl",
"extensionName": "crate",
"usingModule": "<root>",
"location": {
"file": "@@//:MODULE.bazel",
"line": 34,
"line": 25,
"column": 22
},
"imports": {
Expand All @@ -83,7 +51,7 @@
"devDependency": false,
"location": {
"file": "@@//:MODULE.bazel",
"line": 38,
"line": 29,
"column": 17
}
},
Expand All @@ -103,7 +71,7 @@
"devDependency": false,
"location": {
"file": "@@//:MODULE.bazel",
"line": 44,
"line": 35,
"column": 17
}
}
Expand All @@ -117,7 +85,7 @@
"usingModule": "<root>",
"location": {
"file": "@@//:MODULE.bazel",
"line": 57,
"line": 48,
"column": 35
},
"imports": {
Expand Down Expand Up @@ -196,12 +164,15 @@
},
"rules_rust@_": {
"name": "rules_rust",
"version": "0.42.0",
"version": "0.42.1",
"key": "rules_rust@_",
"repoName": "rules_rust",
"executionPlatformsToRegister": [],
"toolchainsToRegister": [
"@rust_toolchains//:all"
"@rust_toolchains//:all",
"//proto/protobuf:default-proto-toolchain",
"//proto/prost:default_prost_toolchain",
"//bindgen:default_bindgen_toolchain"
],
"extensionUsages": [
{
Expand Down Expand Up @@ -350,7 +321,7 @@
"usingModule": "rules_rust@_",
"location": {
"file": "@@rules_rust~//:MODULE.bazel",
"line": 176,
"line": 173,
"column": 32
},
"imports": {
Expand All @@ -367,7 +338,7 @@
"usingModule": "rules_rust@_",
"location": {
"file": "@@rules_rust~//:MODULE.bazel",
"line": 179,
"line": 176,
"column": 38
},
"imports": {
Expand Down Expand Up @@ -2159,7 +2130,7 @@
},
"@@rules_rust~//rust:extensions.bzl%rust": {
"general": {
"bzlTransitiveDigest": "fc5384f0cxBOMkpjo3iXF+BV2bz6qjpFTzUqS8we/JM=",
"bzlTransitiveDigest": "kaZnVIQi/tGg5Je230Rp7u/785j3/IFhhQPVChEJd6A=",
"recordedFileInputs": {},
"recordedDirentsInputs": {},
"envVariables": {},
Expand Down
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.