From 1e4150eb96f8560be7d393609b9394c0f4f1dccb Mon Sep 17 00:00:00 2001 From: Cameron Martin Date: Thu, 25 Aug 2022 18:54:51 +0100 Subject: [PATCH] Support bzlmod This is the very start of making rules_rust being able to load dependencies via bzlmod. Currently the aim is to replace `rules_rust_dependencies` only. There is one new module, `examples/bzlmod/hello_world`, that depends on the root `rules_rust` module. This can be built by: ``` cd examples/bzlmod/hello_world bazel build //:hello_world ``` There is currently some strange stuff going on with visibility because I get the following error: ``` While resolving toolchains for target //:hello_world: com.google.devtools.build.lib.packages.BuildFileNotFoundException: no such package '@rules_rust.override//rust': The repository '@rules_rust.override' could not be resolved: Repository '@rules_rust.override' is not visible from repository '@rules_rust.override' ``` A package is not visible to its self? --- MODULE.bazel | 22 ++++++++++++++++++++++ examples/bzlmod/hello_world/.bazelrc | 1 + examples/bzlmod/hello_world/BUILD.bazel | 14 ++++++++++++++ examples/bzlmod/hello_world/MODULE.bazel | 8 ++++++++ examples/bzlmod/hello_world/WORKSPACE | 0 examples/bzlmod/hello_world/src/main.rs | 22 ++++++++++++++++++++++ 6 files changed, 67 insertions(+) create mode 100644 MODULE.bazel create mode 100644 examples/bzlmod/hello_world/.bazelrc create mode 100644 examples/bzlmod/hello_world/BUILD.bazel create mode 100644 examples/bzlmod/hello_world/MODULE.bazel create mode 100644 examples/bzlmod/hello_world/WORKSPACE create mode 100644 examples/bzlmod/hello_world/src/main.rs diff --git a/MODULE.bazel b/MODULE.bazel new file mode 100644 index 0000000000..d2d6c37205 --- /dev/null +++ b/MODULE.bazel @@ -0,0 +1,22 @@ +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") + +# TODO: Implement the equivalent of this: + +# # process_wrapper needs a low-dependency way to process json. +# maybe( +# http_archive, +# name = "rules_rust_tinyjson", +# sha256 = "1a8304da9f9370f6a6f9020b7903b044aa9ce3470f300a1fba5bc77c78145a16", +# url = "https://crates.io/api/v1/crates/tinyjson/2.3.0/download", +# strip_prefix = "tinyjson-2.3.0", +# type = "tar.gz", +# build_file = "@rules_rust//util/process_wrapper:BUILD.tinyjson.bazel", +# ) \ No newline at end of file diff --git a/examples/bzlmod/hello_world/.bazelrc b/examples/bzlmod/hello_world/.bazelrc new file mode 100644 index 0000000000..c8428d9570 --- /dev/null +++ b/examples/bzlmod/hello_world/.bazelrc @@ -0,0 +1 @@ +build --experimental_enable_bzlmod \ No newline at end of file diff --git a/examples/bzlmod/hello_world/BUILD.bazel b/examples/bzlmod/hello_world/BUILD.bazel new file mode 100644 index 0000000000..8de82781ff --- /dev/null +++ b/examples/bzlmod/hello_world/BUILD.bazel @@ -0,0 +1,14 @@ +load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_doc") + +package(default_visibility = ["//visibility:public"]) + +rust_binary( + name = "hello_world", + srcs = ["src/main.rs"], + deps = ["@examples//hello_lib"], +) + +rust_doc( + name = "hello_world_doc", + crate = ":hello_world", +) diff --git a/examples/bzlmod/hello_world/MODULE.bazel b/examples/bzlmod/hello_world/MODULE.bazel new file mode 100644 index 0000000000..698569fef2 --- /dev/null +++ b/examples/bzlmod/hello_world/MODULE.bazel @@ -0,0 +1,8 @@ +module( + name = "hello_world", + version = "1.0", +) + +bazel_dep(name = "rules_rust", version = "0.9.0") + +local_path_override(module_name = "rules_rust", path = "../../..") \ No newline at end of file diff --git a/examples/bzlmod/hello_world/WORKSPACE b/examples/bzlmod/hello_world/WORKSPACE new file mode 100644 index 0000000000..e69de29bb2 diff --git a/examples/bzlmod/hello_world/src/main.rs b/examples/bzlmod/hello_world/src/main.rs new file mode 100644 index 0000000000..03cf43fcfe --- /dev/null +++ b/examples/bzlmod/hello_world/src/main.rs @@ -0,0 +1,22 @@ +// 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. + +extern crate hello_lib; + +use hello_lib::greeter; + +fn main() { + let hello = greeter::Greeter::new("Hello"); + hello.greet("world"); +}