Skip to content

Commit

Permalink
Add example usage of third party crates.
Browse files Browse the repository at this point in the history
Change-Id: I5dee8352f9a014b51cd36937475a5cc4765c4f66
  • Loading branch information
matts1 committed Jan 3, 2024
1 parent d34eaed commit b76566f
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 9 deletions.
16 changes: 13 additions & 3 deletions examples/bzlmod/hello_world/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@ load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_doc")
package(default_visibility = ["//visibility:public"])

rust_binary(
name = "hello_world",
name = "hello_world_transient",
srcs = ["src/main.rs"],
deps = [
"@crates//:anyhow",
],
)

rust_binary(
name = "hello_world_vendored",
srcs = ["src/main.rs"],
deps = [
"//third-party/crates:anyhow",
Expand All @@ -19,10 +27,12 @@ sh_test(
name = "hello_world_test",
srcs = ["hello_world_test.sh"],
args = [
"$(rlocationpath :hello_world)",
"$(rlocationpath :hello_world_transient)",
"$(rlocationpath :hello_world_vendored)",
],
data = [
":hello_world",
":hello_world_transient",
":hello_world_vendored",
],
deps = [
"@bazel_tools//tools/bash/runfiles",
Expand Down
32 changes: 30 additions & 2 deletions examples/bzlmod/hello_world/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,49 @@ module(
version = "0.0.0",
)

bazel_dep(name = "bazel_skylib", version = "1.5.0")
bazel_dep(name = "rules_rust", version = "0.0.0")
bazel_dep(
name = "bazel_skylib",
version = "1.5.0",
)

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

local_path_override(
module_name = "rules_rust",
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).
crate = use_extension(
"@rules_rust//crate_universe:extension.bzl",
"crate",
)

crate.from_cargo(
name = "crates",
cargo_lockfile = "//third-party:Cargo.lock",
manifests = ["//third-party:Cargo.toml"],
)

use_repo(crate, "crates")

# Option 2: Vendored crates
crate_repositories = use_extension("//third-party:extension.bzl", "crate_repositories")

use_repo(crate_repositories, "vendor__anyhow-1.0.77")
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"
}
]
}
13 changes: 9 additions & 4 deletions examples/bzlmod/hello_world/hello_world_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,18 @@ fail() {

# MARK - Args

if [[ "$#" -ne 1 ]]; then
fail "Usage: $0 /path/to/hello_world"
if [[ "$#" -ne 2 ]]; then
fail "Usage: $0 /path/to/hello_world_transient /path/to/hello_world_vendored"
fi
HELLO_WORLD="$(rlocation "$1")"
HELLO_WORLD_TRANSIENT="$(rlocation "$1")"
HELLO_WORLD_VENDORED="$(rlocation "$2")"

# MARK - Test

OUTPUT="$("${HELLO_WORLD}")"
OUTPUT="$("${HELLO_WORLD_TRANSIENT}")"
[[ "${OUTPUT}" == "Hello, world!" ]] ||
fail 'Expected "Hello, world!", but was' "${OUTPUT}"

OUTPUT="$("${HELLO_WORLD_VENDORED}")"
[[ "${OUTPUT}" == "Hello, world!" ]] ||
fail 'Expected "Hello, world!", but was' "${OUTPUT}"

0 comments on commit b76566f

Please sign in to comment.