Skip to content

Commit

Permalink
Split rust_library and add //rust:defs.bzl
Browse files Browse the repository at this point in the history
This PR splits rust_library into multiple smaller rules:

* rust_library: rust_library will represent a non-transitive library
  similar to how cc_library, java_library and others behave. It will
  always provide CrateInfo, and depending on it will always mean you can
  access the crate from source. Once we support dynamic linking we can
  consider producing both rlib and dylib from rust_library, but let's
  leave that for another discussion.
* rust_static_library: this will only provide CcInfo and it will
  represent an archive of all transitive objects, both Rustc-made and
  native.
* rust_shared_library: this will only provide CcInfo and it will
  represent an shared library of all transitive objects, both Rustc-made
  and native.
* rust_proc_macro: similar to rust_library, but with different
  crate_type passed to rustc and different validation logic for its
  attributes.

I this this makes sense based on these observations:
* Right now rust_library covers all possible crate types except `bin`.
* rust_library always provides CrateInfo, rust_libraries can depend on
  other rust_libraries.
* When the crate type is `cdylib` or `staticlib`, rust_library provides
  CcInfo.
* When the crate type is `cdylib` or `staticlib`, Rust code will not be
  able to access the crate by `extern crate Foo` in the source; the
  behavior will be similar to depending on a CcInfo providing rule.

I believe smaller rules will make them less confusing and
will allow us to have more focused implementations.

This PR is mostly backwards compatible. //rust:rust.bzl#rust_library is
a macro. If the crate_type attribute is present, macro dispatches to
the right new rule. If it's not present, macro will choose the new
rust_library. New rules are added, so people can migrate at
their own pace.

defs.bzl is the bzl file that we now expect people to load. Bazel docs
recommend to store public rules in defs.bzl
(https://docs.bazel.build/versions/master/skylark/deploying.html#repository-content).

This change was first socialized at
https://groups.google.com/g/rules_rust/c/kGMg6haEF44.

Regenerate documentation

Regenerate documentation

Regenerate documentation

Regenerate documentation
  • Loading branch information
hlopko committed Feb 18, 2021
1 parent 14eb6f3 commit df503d2
Show file tree
Hide file tree
Showing 10 changed files with 1,094 additions and 696 deletions.
7 changes: 5 additions & 2 deletions docs/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@ PAGES = {
"cargo_build_script": [
"cargo_build_script",
],
"rust": [
"rust_library",
"defs": [
"rust_binary",
"rust_library",
"rust_static_library",
"rust_shared_library",
"rust_proc_macro",
"rust_benchmark",
"rust_test",
],
Expand Down
24 changes: 15 additions & 9 deletions docs/all.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,26 @@ load(
_rust_proto_toolchain = "rust_proto_toolchain",
)
load(
"@rules_rust//rust:repositories.bzl",
_rust_repositories = "rust_repositories",
_rust_repository_set = "rust_repository_set",
_rust_toolchain_repository = "rust_toolchain_repository",
_rust_toolchain_repository_proxy = "rust_toolchain_repository_proxy",
)
load(
"@rules_rust//rust:rust.bzl",
"@rules_rust//rust:defs.bzl",
_rust_analyzer = "rust_analyzer",
_rust_benchmark = "rust_benchmark",
_rust_binary = "rust_binary",
_rust_clippy = "rust_clippy",
_rust_doc = "rust_doc",
_rust_doc_test = "rust_doc_test",
_rust_library = "rust_library",
_rust_proc_macro = "rust_proc_macro",
_rust_shared_library = "rust_shared_library",
_rust_static_library = "rust_static_library",
_rust_test = "rust_test",
)
load(
"@rules_rust//rust:repositories.bzl",
_rust_repositories = "rust_repositories",
_rust_repository_set = "rust_repository_set",
_rust_toolchain_repository = "rust_toolchain_repository",
_rust_toolchain_repository_proxy = "rust_toolchain_repository_proxy",
)
load(
"@rules_rust//rust:toolchain.bzl",
_rust_toolchain = "rust_toolchain",
Expand All @@ -61,8 +64,11 @@ load(
_rust_wasm_bindgen_toolchain = "rust_wasm_bindgen_toolchain",
)

rust_library = _rust_library
rust_binary = _rust_binary
rust_library = _rust_library
rust_static_library = _rust_static_library
rust_shared_library = _rust_shared_library
rust_proc_macro = _rust_proc_macro
rust_test = _rust_test
rust_doc = _rust_doc
rust_doc_test = _rust_doc_test
Expand Down
366 changes: 240 additions & 126 deletions docs/rust.md → docs/defs.md

Large diffs are not rendered by default.

364 changes: 239 additions & 125 deletions docs/flatten.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ The rules are under active development, as such the lastest commit on the master

## Rules

- [rust](rust.md): standard rust rules for building and testing libraries and binaries.
- [defs](defs.md): standard rust rules for building and testing libraries and binaries.
- [rust_doc](rust_doc.md): rules for generating and testing rust documentation.
- [rust_proto](rust_proto.md): rules for generating [protobuf](https://developers.google.com/protocol-buffers).
and [gRPC](https://grpc.io) stubs.
Expand Down
1 change: 1 addition & 0 deletions rust/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ exports_files([
"known_shas.bzl",
"repositories.bzl",
"rust.bzl",
"defs.bzl",
"toolchain.bzl",
])

Expand Down
98 changes: 98 additions & 0 deletions rust/defs.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# Copyright 2021 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.

"""Public entry point to all Rust rules and supported APIs."""

load(
"//rust/private:clippy.bzl",
_rust_clippy = "rust_clippy",
_rust_clippy_aspect = "rust_clippy_aspect",
)
load("//rust/private:common.bzl", _rust_common = "rust_common")
load(
"//rust/private:rust.bzl",
_rust_benchmark = "rust_benchmark",
_rust_binary = "rust_binary",
_rust_library = "rust_library",
_rust_proc_macro = "rust_proc_macro",
_rust_shared_library = "rust_shared_library",
_rust_static_library = "rust_static_library",
_rust_test = "rust_test",
_rust_test_binary = "rust_test_binary",
)
load(
"//rust/private:rust_analyzer.bzl",
_rust_analyzer = "rust_analyzer",
_rust_analyzer_aspect = "rust_analyzer_aspect",
)
load(
"//rust/private:rustc.bzl",
_error_format = "error_format",
)
load(
"//rust/private:rustdoc.bzl",
_rust_doc = "rust_doc",
)
load(
"//rust/private:rustdoc_test.bzl",
_rust_doc_test = "rust_doc_test",
)

rust_library = _rust_library
# See @rules_rust//rust/private:rust.bzl for a complete description.

rust_static_library = _rust_static_library
# See @rules_rust//rust/private:rust.bzl for a complete description.

rust_shared_library = _rust_shared_library
# See @rules_rust//rust/private:rust.bzl for a complete description.

rust_proc_macro = _rust_proc_macro
# See @rules_rust//rust/private:rust.bzl for a complete description.

rust_binary = _rust_binary
# See @rules_rust//rust/private:rust.bzl for a complete description.

rust_test = _rust_test
# See @rules_rust//rust/private:rust.bzl for a complete description.

rust_test_binary = _rust_test_binary
# See @rules_rust//rust/private:rust.bzl for a complete description.

rust_benchmark = _rust_benchmark
# See @rules_rust//rust/private:rust.bzl for a complete description.

rust_doc = _rust_doc
# See @rules_rust//rust/private:rustdoc.bzl for a complete description.

rust_doc_test = _rust_doc_test
# See @rules_rust//rust/private:rustdoc_test.bzl for a complete description.

rust_clippy_aspect = _rust_clippy_aspect
# See @rules_rust//rust/private:clippy.bzl for a complete description.

rust_clippy = _rust_clippy
# See @rules_rust//rust/private:clippy.bzl for a complete description.

error_format = _error_format
# See @rules_rust//rust/private:rustc.bzl for a complete description.

rust_common = _rust_common
# See @rules_rust//rust/private:common.bzl for a complete description.

rust_analyzer_aspect = _rust_analyzer_aspect
# See @rules_rust//rust:private/rust_analyzer.bzl for a complete description.

rust_analyzer = _rust_analyzer
# See @rules_rust//rust:private/rust_analyzer.bzl for a complete description.
Loading

0 comments on commit df503d2

Please sign in to comment.