Skip to content

Commit 9aa4956

Browse files
authored
Split rust_library and add //rust:defs.bzl (#592)
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 Regenerate documentation
1 parent fee3b3c commit 9aa4956

File tree

10 files changed

+1104
-698
lines changed

10 files changed

+1104
-698
lines changed

docs/BUILD

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,12 @@ PAGES = {
3131
"cargo_build_script": [
3232
"cargo_build_script",
3333
],
34-
"rust": [
35-
"rust_library",
34+
"defs": [
3635
"rust_binary",
36+
"rust_library",
37+
"rust_static_library",
38+
"rust_shared_library",
39+
"rust_proc_macro",
3740
"rust_benchmark",
3841
"rust_test",
3942
],

docs/all.bzl

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,26 @@ load(
3030
_rust_proto_toolchain = "rust_proto_toolchain",
3131
)
3232
load(
33-
"@rules_rust//rust:repositories.bzl",
34-
_rust_repositories = "rust_repositories",
35-
_rust_repository_set = "rust_repository_set",
36-
_rust_toolchain_repository = "rust_toolchain_repository",
37-
_rust_toolchain_repository_proxy = "rust_toolchain_repository_proxy",
38-
)
39-
load(
40-
"@rules_rust//rust:rust.bzl",
33+
"@rules_rust//rust:defs.bzl",
4134
_rust_analyzer = "rust_analyzer",
4235
_rust_benchmark = "rust_benchmark",
4336
_rust_binary = "rust_binary",
4437
_rust_clippy = "rust_clippy",
4538
_rust_doc = "rust_doc",
4639
_rust_doc_test = "rust_doc_test",
4740
_rust_library = "rust_library",
41+
_rust_proc_macro = "rust_proc_macro",
42+
_rust_shared_library = "rust_shared_library",
43+
_rust_static_library = "rust_static_library",
4844
_rust_test = "rust_test",
4945
)
46+
load(
47+
"@rules_rust//rust:repositories.bzl",
48+
_rust_repositories = "rust_repositories",
49+
_rust_repository_set = "rust_repository_set",
50+
_rust_toolchain_repository = "rust_toolchain_repository",
51+
_rust_toolchain_repository_proxy = "rust_toolchain_repository_proxy",
52+
)
5053
load(
5154
"@rules_rust//rust:toolchain.bzl",
5255
_rust_toolchain = "rust_toolchain",
@@ -61,8 +64,11 @@ load(
6164
_rust_wasm_bindgen_toolchain = "rust_wasm_bindgen_toolchain",
6265
)
6366

64-
rust_library = _rust_library
6567
rust_binary = _rust_binary
68+
rust_library = _rust_library
69+
rust_static_library = _rust_static_library
70+
rust_shared_library = _rust_shared_library
71+
rust_proc_macro = _rust_proc_macro
6672
rust_test = _rust_test
6773
rust_doc = _rust_doc
6874
rust_doc_test = _rust_doc_test

docs/rust.md renamed to docs/defs.md

Lines changed: 245 additions & 127 deletions
Large diffs are not rendered by default.

docs/flatten.md

Lines changed: 244 additions & 126 deletions
Large diffs are not rendered by default.

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ supported in certain environments.
3939

4040
## Rules
4141

42-
- [rust](rust.md): standard rust rules for building and testing libraries and binaries.
42+
- [defs](defs.md): standard rust rules for building and testing libraries and binaries.
4343
- [rust_doc](rust_doc.md): rules for generating and testing rust documentation.
4444
- [rust_proto](rust_proto.md): rules for generating [protobuf](https://developers.google.com/protocol-buffers).
4545
and [gRPC](https://grpc.io) stubs.

rust/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ exports_files([
66
"known_shas.bzl",
77
"repositories.bzl",
88
"rust.bzl",
9+
"defs.bzl",
910
"toolchain.bzl",
1011
])
1112

rust/defs.bzl

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# Copyright 2021 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Public entry point to all Rust rules and supported APIs."""
16+
17+
load(
18+
"//rust/private:clippy.bzl",
19+
_rust_clippy = "rust_clippy",
20+
_rust_clippy_aspect = "rust_clippy_aspect",
21+
)
22+
load("//rust/private:common.bzl", _rust_common = "rust_common")
23+
load(
24+
"//rust/private:rust.bzl",
25+
_rust_benchmark = "rust_benchmark",
26+
_rust_binary = "rust_binary",
27+
_rust_library = "rust_library",
28+
_rust_proc_macro = "rust_proc_macro",
29+
_rust_shared_library = "rust_shared_library",
30+
_rust_static_library = "rust_static_library",
31+
_rust_test = "rust_test",
32+
_rust_test_binary = "rust_test_binary",
33+
)
34+
load(
35+
"//rust/private:rust_analyzer.bzl",
36+
_rust_analyzer = "rust_analyzer",
37+
_rust_analyzer_aspect = "rust_analyzer_aspect",
38+
)
39+
load(
40+
"//rust/private:rustc.bzl",
41+
_error_format = "error_format",
42+
)
43+
load(
44+
"//rust/private:rustdoc.bzl",
45+
_rust_doc = "rust_doc",
46+
)
47+
load(
48+
"//rust/private:rustdoc_test.bzl",
49+
_rust_doc_test = "rust_doc_test",
50+
)
51+
52+
rust_library = _rust_library
53+
# See @rules_rust//rust/private:rust.bzl for a complete description.
54+
55+
rust_static_library = _rust_static_library
56+
# See @rules_rust//rust/private:rust.bzl for a complete description.
57+
58+
rust_shared_library = _rust_shared_library
59+
# See @rules_rust//rust/private:rust.bzl for a complete description.
60+
61+
rust_proc_macro = _rust_proc_macro
62+
# See @rules_rust//rust/private:rust.bzl for a complete description.
63+
64+
rust_binary = _rust_binary
65+
# See @rules_rust//rust/private:rust.bzl for a complete description.
66+
67+
rust_test = _rust_test
68+
# See @rules_rust//rust/private:rust.bzl for a complete description.
69+
70+
rust_test_binary = _rust_test_binary
71+
# See @rules_rust//rust/private:rust.bzl for a complete description.
72+
73+
rust_benchmark = _rust_benchmark
74+
# See @rules_rust//rust/private:rust.bzl for a complete description.
75+
76+
rust_doc = _rust_doc
77+
# See @rules_rust//rust/private:rustdoc.bzl for a complete description.
78+
79+
rust_doc_test = _rust_doc_test
80+
# See @rules_rust//rust/private:rustdoc_test.bzl for a complete description.
81+
82+
rust_clippy_aspect = _rust_clippy_aspect
83+
# See @rules_rust//rust/private:clippy.bzl for a complete description.
84+
85+
rust_clippy = _rust_clippy
86+
# See @rules_rust//rust/private:clippy.bzl for a complete description.
87+
88+
error_format = _error_format
89+
# See @rules_rust//rust/private:rustc.bzl for a complete description.
90+
91+
rust_common = _rust_common
92+
# See @rules_rust//rust/private:common.bzl for a complete description.
93+
94+
rust_analyzer_aspect = _rust_analyzer_aspect
95+
# See @rules_rust//rust:private/rust_analyzer.bzl for a complete description.
96+
97+
rust_analyzer = _rust_analyzer
98+
# See @rules_rust//rust:private/rust_analyzer.bzl for a complete description.

0 commit comments

Comments
 (0)