Skip to content

Commit

Permalink
Support bzlmod
Browse files Browse the repository at this point in the history
This is a rough proof of concept of making rules_rust load dependencies via bzlmod. Currently it can only compile and run rust crates, and cannot load crates from Cargo.toml.

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
USE_BAZEL_VERSION=last_green bazel run //:hello_world
```

Currently toolchain registration has quite bad user experience, because all the toolchains to register have to be listed by the user in the MODULE.bazel file. This is because module extensions cannot directly register toolchains and instead must only expose repositories with toolchains, that are then registered using `toolchains_to_register`. It would be nice if all the toolchains could be aliased to one label and them all registered together. However, I can't see how this would be possible.
  • Loading branch information
cameron-martin committed Sep 3, 2022
1 parent 51c0658 commit c0fad5e
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 27 deletions.
14 changes: 14 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module(
name = "rules_rust",
version = "0.9.0",
)

bazel_dep(name = "platforms", version = "0.0.5")
bazel_dep(name = "rules_cc", version = "0.0.1")
bazel_dep(name = "bazel_skylib", version = "1.2.0")
bazel_dep(name = "apple_support", version = "0.13.0")

# Load tinyjson without using the process wrapper
raw_cargo = use_extension("//:extensions.bzl", "raw_cargo")
raw_cargo.crate(name = "tinyjson", version = "2.3.0", sha256 = "1a8304da9f9370f6a6f9020b7903b044aa9ce3470f300a1fba5bc77c78145a16")
use_repo(raw_cargo, rules_rust_tinyjson = "raw_cargo_tinyjson_2_3_0")
1 change: 1 addition & 0 deletions examples/bzlmod/hello_world/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build --experimental_enable_bzlmod
1 change: 1 addition & 0 deletions examples/bzlmod/hello_world/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bazel-*
13 changes: 13 additions & 0 deletions examples/bzlmod/hello_world/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_doc")

package(default_visibility = ["//visibility:public"])

rust_binary(
name = "hello_world",
srcs = ["src/main.rs"],
)

rust_doc(
name = "hello_world_doc",
crate = ":hello_world",
)
13 changes: 13 additions & 0 deletions examples/bzlmod/hello_world/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module(
name = "hello_world",
version = "1.0",
toolchains_to_register = ["@rust_linux_x86_64__x86_64-unknown-linux-gnu//:toolchain"],
)

bazel_dep(name = "rules_rust", version = "0.9.0")

local_path_override(module_name = "rules_rust", path = "../../..")

rust_toolchains = use_extension("@rules_rust//:extensions.bzl", "toolchains")
rust_toolchains.toolchain(edition = "2021")
use_repo(rust_toolchains, "rust_linux_x86_64__x86_64-unknown-linux-gnu")
Empty file.
17 changes: 17 additions & 0 deletions examples/bzlmod/hello_world/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2015 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

fn main() {
println!("Hello, world!");
}
45 changes: 45 additions & 0 deletions extensions.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
load("//rust:repositories.bzl", "rust_register_toolchains")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

def _toolchains_impl(ctx):
for mod in ctx.modules:
for toolchain in mod.tags.toolchain:
rust_register_toolchains(edition = toolchain.edition, register_toolchains = False)

toolchains_toolchain = tag_class(attrs = {"edition": attr.string()})
toolchains = module_extension(
implementation=_toolchains_impl,
tag_classes={"toolchain": toolchains_toolchain},
)

def _create_build_file_content(name):
return """
load("@rules_rust//rust/private:rust.bzl", "rust_library_without_process_wrapper")
rust_library_without_process_wrapper(
name = "{}",
srcs = glob(["src/*.rs"]),
edition = "2018",
visibility = ["@rules_rust//util/process_wrapper:__pkg__"],
)
""".format(name)

def _raw_cargo_impl(ctx):
for mod in ctx.modules:
for crate in mod.tags.crate:
http_archive(
name = "raw_cargo_{}_{}".format(crate.name, crate.version.replace(".", "_")),
sha256 = crate.sha256,
url = "https://crates.io/api/v1/crates/{}/{}/download".format(crate.name, crate.version),
strip_prefix = "{}-{}".format(crate.name, crate.version),
type = "tar.gz",
build_file_content = _create_build_file_content(crate.name),
)

raw_cargo_crate = tag_class(attrs = {"name": attr.string(), "version": attr.string(), "sha256": attr.string()})
raw_cargo = module_extension(
implementation=_raw_cargo_impl,
tag_classes={"crate": raw_cargo_crate},
)


20 changes: 10 additions & 10 deletions rust/private/repository_utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -205,15 +205,15 @@ load("@rules_rust//rust:toolchain.bzl", "rust_toolchain")
rust_toolchain(
name = "{toolchain_name}",
rust_doc = "@{workspace_name}//:rustdoc",
rust_std = "@{workspace_name}//:rust_std-{target_triple}",
rustc = "@{workspace_name}//:rustc",
rust_doc = "//:rustdoc",
rust_std = "//:rust_std-{target_triple}",
rustc = "//:rustc",
rustfmt = {rustfmt_label},
cargo = "@{workspace_name}//:cargo",
clippy_driver = "@{workspace_name}//:clippy_driver_bin",
cargo = "//:cargo",
clippy_driver = "//:clippy_driver_bin",
llvm_cov = {llvm_cov_label},
llvm_profdata = {llvm_profdata_label},
rustc_lib = "@{workspace_name}//:rustc_lib",
rustc_lib = "//:rustc_lib",
rustc_srcs = {rustc_srcs},
allocator_library = {allocator_library},
binary_ext = "{binary_ext}",
Expand Down Expand Up @@ -265,15 +265,15 @@ def BUILD_for_rust_toolchain(

rustc_srcs = "None"
if include_rustc_srcs:
rustc_srcs = "\"@{workspace_name}//lib/rustlib/src:rustc_srcs\"".format(workspace_name = workspace_name)
rustc_srcs = "\"//lib/rustlib/src:rustc_srcs\"".format(workspace_name = workspace_name)
rustfmt_label = "None"
if include_rustfmt:
rustfmt_label = "\"@{workspace_name}//:rustfmt_bin\"".format(workspace_name = workspace_name)
rustfmt_label = "\"//:rustfmt_bin\"".format(workspace_name = workspace_name)
llvm_cov_label = "None"
llvm_profdata_label = "None"
if include_llvm_tools:
llvm_cov_label = "\"@{workspace_name}//:llvm_cov_bin\"".format(workspace_name = workspace_name)
llvm_profdata_label = "\"@{workspace_name}//:llvm_profdata_bin\"".format(workspace_name = workspace_name)
llvm_cov_label = "\"//:llvm_cov_bin\"".format(workspace_name = workspace_name)
llvm_profdata_label = "\"//:llvm_profdata_bin\"".format(workspace_name = workspace_name)
allocator_library_label = "None"
if allocator_library:
allocator_library_label = "\"{allocator_library}\"".format(allocator_library = allocator_library)
Expand Down
34 changes: 17 additions & 17 deletions rust/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -610,20 +610,20 @@ def rust_repository_set(
native.register_toolchains(*all_toolchain_names)
native.register_toolchains(str(Label("//rust/private/dummy_cc_toolchain:dummy_cc_wasm32_toolchain")))

# Inform users that they should be using the canonical name if it's not detected
if "rules_rust" not in native.existing_rules():
message = "\n" + ("=" * 79) + "\n"
message += (
"It appears that you are trying to import rules_rust without using its\n" +
"canonical name, \"@rules_rust\" Please change your WORKSPACE file to\n" +
"import this repo with `name = \"rules_rust\"` instead."
)

if "io_bazel_rules_rust" in native.existing_rules():
message += "\n\n" + (
"Note that the previous name of \"@io_bazel_rules_rust\" is deprecated.\n" +
"See https://github.com/bazelbuild/rules_rust/issues/499 for context."
)

message += "\n" + ("=" * 79)
fail(message)
# # Inform users that they should be using the canonical name if it's not detected
# if "rules_rust" not in native.existing_rules():
# message = "\n" + ("=" * 79) + "\n"
# message += (
# "It appears that you are trying to import rules_rust without using its\n" +
# "canonical name, \"@rules_rust\" Please change your WORKSPACE file to\n" +
# "import this repo with `name = \"rules_rust\"` instead."
# )

# if "io_bazel_rules_rust" in native.existing_rules():
# message += "\n\n" + (
# "Note that the previous name of \"@io_bazel_rules_rust\" is deprecated.\n" +
# "See https://github.com/bazelbuild/rules_rust/issues/499 for context."
# )

# message += "\n" + ("=" * 79)
# fail(message)

0 comments on commit c0fad5e

Please sign in to comment.