diff --git a/examples/bzlmod/hello_world/BUILD.bazel b/examples/bzlmod/hello_world/BUILD.bazel index 3e2ec1932e..95fe9bfbc8 100644 --- a/examples/bzlmod/hello_world/BUILD.bazel +++ b/examples/bzlmod/hello_world/BUILD.bazel @@ -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( diff --git a/examples/bzlmod/hello_world/Cargo.lock b/examples/bzlmod/hello_world/Cargo.lock new file mode 100644 index 0000000000..26c8f6d7a7 --- /dev/null +++ b/examples/bzlmod/hello_world/Cargo.lock @@ -0,0 +1,146 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aho-corasick" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0" +dependencies = [ + "memchr", +] + +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + +[[package]] +name = "dummy" +version = "0.1.0" +dependencies = [ + "env_logger", + "log", +] + +[[package]] +name = "env_logger" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a12e6657c4c97ebab115a42dcee77225f7f482cdd841cf7088c657a42e9e00e7" +dependencies = [ + "atty", + "humantime", + "log", + "regex", + "termcolor", +] + +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + +[[package]] +name = "libc" +version = "0.2.149" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a08173bc88b7955d1b3145aa561539096c421ac8debde8cbc3612ec635fee29b" + +[[package]] +name = "log" +version = "0.4.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" + +[[package]] +name = "memchr" +version = "2.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167" + +[[package]] +name = "regex" +version = "1.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "380b951a9c5e80ddfd6136919eef32310721aa4aacd4889a8d39124b026ab343" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f804c7828047e88b2d32e2d7fe5a105da8ee3264f01902f796c8e067dc2483f" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" + +[[package]] +name = "termcolor" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6093bad37da69aab9d123a8091e4be0aa4a03e4d601ec641c327398315f62b64" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-util" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596" +dependencies = [ + "winapi", +] + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/examples/bzlmod/hello_world/Cargo.toml b/examples/bzlmod/hello_world/Cargo.toml new file mode 100644 index 0000000000..5e6c2f1db5 --- /dev/null +++ b/examples/bzlmod/hello_world/Cargo.toml @@ -0,0 +1,15 @@ +# This package is not used. It's a dummy package that declares dependencies that +# you can then access from @crates//:. +[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" \ No newline at end of file diff --git a/examples/bzlmod/hello_world/MODULE.bazel b/examples/bzlmod/hello_world/MODULE.bazel index dca22cebb4..fb3ec48361 100644 --- a/examples/bzlmod/hello_world/MODULE.bazel +++ b/examples/bzlmod/hello_world/MODULE.bazel @@ -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") diff --git a/examples/bzlmod/hello_world/annotations.json b/examples/bzlmod/hello_world/annotations.json new file mode 100644 index 0000000000..3d5102e9fa --- /dev/null +++ b/examples/bzlmod/hello_world/annotations.json @@ -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" + } + ] +} \ No newline at end of file diff --git a/examples/bzlmod/hello_world/hello_log.rs b/examples/bzlmod/hello_world/hello_log.rs new file mode 100644 index 0000000000..5e8a7f2869 --- /dev/null +++ b/examples/bzlmod/hello_world/hello_log.rs @@ -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"); + } +} diff --git a/examples/bzlmod/hello_world/src/main.rs b/examples/bzlmod/hello_world/hello_world.rs similarity index 100% rename from examples/bzlmod/hello_world/src/main.rs rename to examples/bzlmod/hello_world/hello_world.rs