Skip to content

Commit

Permalink
Add example usage of third party crates.
Browse files Browse the repository at this point in the history
  • Loading branch information
matts1 committed Nov 13, 2023
1 parent 1522a3d commit 22841ef
Show file tree
Hide file tree
Showing 7 changed files with 220 additions and 1 deletion.
11 changes: 10 additions & 1 deletion examples/bzlmod/hello_world/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,18 @@ load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_doc")

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

rust_binary(
name = "hello_log",
srcs = ["hello_log.rs"],
deps = [
"@crates//:env_logger",
"@crates//:log",
],
)

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

rust_doc(
Expand Down
146 changes: 146 additions & 0 deletions examples/bzlmod/hello_world/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions examples/bzlmod/hello_world/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# This package is not used. It's a dummy package that declares dependencies that
# you can then access from @crates//:<dep>.
[package]
name = "dummy"
version = "0.1.0"
edition = "2021"

[[bin]]
name = "dummy"
# Some arbitrary .rs file that exists.
path = "hello_world.rs"

[dependencies]
env_logger = "0.9.2"
log = "0.4.17"
14 changes: 14 additions & 0 deletions examples/bzlmod/hello_world/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,17 @@ use_repo(
)

register_toolchains("@rust_toolchains//:all")

# Use a toml file to generate your dependencies like so:
crate = use_extension(
"@rules_rust//crate_universe:extension.bzl",
"crate",
)
crate.from_cargo(
# This is optional, but if you need crate.annotation, then this is the
# bzlmod equivalent.
annotation_files = ["//:annotations.json"],
cargo_lockfile = "//:Cargo.lock",
manifests = ["//:Cargo.toml"],
)
use_repo(crate, "crates")
13 changes: 13 additions & 0 deletions examples/bzlmod/hello_world/annotations.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"env_logger": [
{
"version": "0.9.2",
"additive_build_file_content": "\n# Extra stuff here"
}
],
"log": [
{
"additive_build_file_content": "\n# Extra stuff here"
}
]
}
22 changes: 22 additions & 0 deletions examples/bzlmod/hello_world/hello_log.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2023 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() {
env_logger::init();
if log::log_enabled!(log::Level::Info) {
log::info!("Hello log!");
} else {
println!("Logging disabled");
}
}
File renamed without changes.

0 comments on commit 22841ef

Please sign in to comment.