-
Notifications
You must be signed in to change notification settings - Fork 442
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 bazel run //:hello_world ``` A "hub" repository for toolchains is defined, and can be registered using: ``` register_toolchains("@rust_toolchains//:all") ```
- Loading branch information
1 parent
20ce44e
commit a3d7796
Showing
10 changed files
with
280 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
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", | ||
sha256 = "1a8304da9f9370f6a6f9020b7903b044aa9ce3470f300a1fba5bc77c78145a16", | ||
version = "2.3.0", | ||
) | ||
|
||
use_repo( | ||
raw_cargo, | ||
rules_rust_tinyjson = "raw_cargo_tinyjson_2_3_0", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
build --experimental_enable_bzlmod |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/bazel-* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module( | ||
name = "hello_world", | ||
version = "1.0", | ||
) | ||
|
||
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_toolchains", | ||
) | ||
|
||
register_toolchains("@rust_toolchains//:all") |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
"Module extensions for using rules_rust with bzlmod" | ||
|
||
load("//rust:repositories.bzl", "rust_register_toolchains") | ||
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") | ||
|
||
def _toolchains_impl(ctx): | ||
mod = ctx.modules[0] | ||
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}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.