Skip to content

Commit eaf5138

Browse files
Support bzlmod (#1528)
This makes rules_rust load dependencies via bzlmod. Currently only basic functionality is completed, such as registering rustc toolchains and compiling crates. Note that it cannot interact with cargo, wasm or load any other rust toolchains such as rustfmt. There is one new module, `examples/bzlmod/hello_world`, that depends on the root `rules_rust` module. This example can be built and run using: ``` cd examples/bzlmod/hello_world bazel run //:hello_world ``` To register toolchains in an ergonomic way, it defines a new "hub" repository that contains all the toolchain proxies, so they can all be registered like so: ``` register_toolchains("@rust_toolchains//:all") ``` closes #1493
1 parent 1074ecb commit eaf5138

File tree

12 files changed

+303
-64
lines changed

12 files changed

+303
-64
lines changed

.bazelci/presubmit.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,12 @@ tasks:
342342
- "//..."
343343
build_flags: *aspects_flags
344344
soft_fail: yes
345+
ubuntu2004_examples_bzlmod:
346+
name: Bzlmod Examples
347+
platform: ubuntu2004
348+
working_directory: examples/bzlmod/hello_world
349+
build_targets:
350+
- "//..."
345351
rbe_ubuntu1604_examples:
346352
name: Examples
347353
platform: rbe_ubuntu1604

MODULE.bazel

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module(
2+
name = "rules_rust",
3+
version = "0.20.0",
4+
)
5+
6+
print("WARNING: The rules_rust Bazel module is still highly experimental and subject to change at any time. Only use it to try out bzlmod for now.") # buildifier: disable=print
7+
8+
bazel_dep(name = "platforms", version = "0.0.5")
9+
bazel_dep(name = "rules_cc", version = "0.0.1")
10+
bazel_dep(name = "bazel_skylib", version = "1.2.0")
11+
bazel_dep(name = "apple_support", version = "1.3.1")
12+
13+
internal_deps = use_extension("//rust/private:extensions.bzl", "internal_deps")
14+
use_repo(
15+
internal_deps,
16+
"rules_rust_tinyjson",
17+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
build --experimental_enable_bzlmod
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/bazel-*
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_doc")
2+
3+
package(default_visibility = ["//visibility:public"])
4+
5+
rust_binary(
6+
name = "hello_world",
7+
srcs = ["src/main.rs"],
8+
)
9+
10+
rust_doc(
11+
name = "hello_world_doc",
12+
crate = ":hello_world",
13+
)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module(
2+
name = "hello_world",
3+
version = "1.0",
4+
)
5+
6+
bazel_dep(name = "rules_rust", version = "0.9.0")
7+
local_path_override(
8+
module_name = "rules_rust",
9+
path = "../../..",
10+
)
11+
12+
rust = use_extension("@rules_rust//rust:extensions.bzl", "rust")
13+
rust.toolchain(edition = "2021")
14+
use_repo(
15+
rust,
16+
"rust_toolchains",
17+
)
18+
19+
register_toolchains("@rust_toolchains//:all")

examples/bzlmod/hello_world/WORKSPACE

Whitespace-only changes.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2015 The Bazel Authors. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
fn main() {
16+
println!("Hello, world!");
17+
}

rust/extensions.bzl

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"Module extensions for using rules_rust with bzlmod"
2+
3+
load(
4+
"//rust/private:repository_utils.bzl",
5+
"DEFAULT_EXTRA_TARGET_TRIPLES",
6+
"DEFAULT_NIGHTLY_VERSION",
7+
"DEFAULT_STATIC_RUST_URL_TEMPLATES",
8+
)
9+
load(":repositories.bzl", "rust_register_toolchains")
10+
11+
def _rust_impl(ctx):
12+
mod = ctx.modules[0]
13+
for toolchain in mod.tags.toolchain:
14+
rust_register_toolchains(
15+
dev_components = toolchain.dev_components,
16+
edition = toolchain.edition,
17+
allocator_library = toolchain.allocator_library,
18+
rustfmt_version = toolchain.rustfmt_version,
19+
rust_analyzer_version = toolchain.rust_analyzer_version,
20+
sha256s = toolchain.sha256s,
21+
extra_target_triples = toolchain.extra_target_triples,
22+
urls = toolchain.urls,
23+
versions = toolchain.versions,
24+
register_toolchains = False,
25+
)
26+
27+
rust_toolchain = tag_class(attrs = {
28+
"allocator_library": attr.string(),
29+
"dev_components": attr.bool(default = False),
30+
"edition": attr.string(),
31+
"extra_target_triples": attr.string_list(default = DEFAULT_EXTRA_TARGET_TRIPLES),
32+
"rust_analyzer_version": attr.string(),
33+
"rustfmt_version": attr.string(default = DEFAULT_NIGHTLY_VERSION),
34+
"sha256s": attr.string_dict(),
35+
"urls": attr.string_list(default = DEFAULT_STATIC_RUST_URL_TEMPLATES),
36+
"versions": attr.string_list(default = []),
37+
})
38+
39+
rust = module_extension(
40+
implementation = _rust_impl,
41+
tag_classes = {"toolchain": rust_toolchain},
42+
)

rust/private/extensions.bzl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""Bzlmod module extensions that are only used internally"""
2+
3+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
4+
load("//rust/private:repository_utils.bzl", "TINYJSON_KWARGS")
5+
6+
def _internal_deps_impl(_module_ctx):
7+
http_archive(**TINYJSON_KWARGS)
8+
9+
internal_deps = module_extension(
10+
doc = "Dependencies for rules_rust",
11+
implementation = _internal_deps_impl,
12+
)

0 commit comments

Comments
 (0)