Skip to content

Commit c3c626a

Browse files
committed
Split rust_library and add //rust:defs.bzl
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
1 parent 14eb6f3 commit c3c626a

File tree

9 files changed

+518
-114
lines changed

9 files changed

+518
-114
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: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,17 @@ load(
3737
_rust_toolchain_repository_proxy = "rust_toolchain_repository_proxy",
3838
)
3939
load(
40-
"@rules_rust//rust:rust.bzl",
40+
"@rules_rust//rust:defs.bzl",
4141
_rust_analyzer = "rust_analyzer",
4242
_rust_benchmark = "rust_benchmark",
4343
_rust_binary = "rust_binary",
4444
_rust_clippy = "rust_clippy",
4545
_rust_doc = "rust_doc",
4646
_rust_doc_test = "rust_doc_test",
4747
_rust_library = "rust_library",
48+
_rust_static_library = "rust_static_library",
49+
_rust_shared_library = "rust_shared_library",
50+
_rust_proc_macro = "rust_proc_macro",
4851
_rust_test = "rust_test",
4952
)
5053
load(
@@ -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: 128 additions & 11 deletions
Large diffs are not rendered by default.

docs/flatten.md

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

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.

rust/private/rust.bzl

Lines changed: 113 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def _determine_lib_name(name, crate_type, toolchain, lib_hash = ""):
3636
3737
Args:
3838
name (str): The name of the current target
39-
crate_type (str): The `crate_type` attribute from a `rust_library`
39+
crate_type (str): The `crate_type`
4040
toolchain (rust_toolchain): The current `rust_toolchain`
4141
lib_hash (str, optional): The hashed crate root path. Defaults to "".
4242
@@ -136,11 +136,56 @@ def _shortest_src_with_basename(srcs, basename):
136136
return shortest
137137

138138
def _rust_library_impl(ctx):
139-
"""The implementation of the `rust_library` rule
139+
"""The implementation of the `rust_library` rule.
140140
141141
Args:
142142
ctx (ctx): The rule's context object
143143
144+
Returns:
145+
list: A list of providers.
146+
"""
147+
return _rust_library_common(ctx, "rlib")
148+
149+
def _rust_static_library_impl(ctx):
150+
"""The implementation of the `rust_static_library` rule.
151+
152+
Args:
153+
ctx (ctx): The rule's context object
154+
155+
Returns:
156+
list: A list of providers.
157+
"""
158+
return _rust_library_common(ctx, "staticlib")
159+
160+
def _rust_shared_library_impl(ctx):
161+
"""The implementation of the `rust_shared_library` rule.
162+
163+
Args:
164+
ctx (ctx): The rule's context object
165+
166+
Returns:
167+
list: A list of providers.
168+
"""
169+
return _rust_library_common(ctx, "cdylib")
170+
171+
def _rust_proc_macro_impl(ctx):
172+
"""The implementation of the `rust_proc_macro` rule.
173+
174+
Args:
175+
ctx (ctx): The rule's context object
176+
177+
Returns:
178+
list: A list of providers.
179+
"""
180+
return _rust_library_common(ctx, "proc-macro")
181+
182+
def _rust_library_common(ctx, crate_type):
183+
"""The common implementation of the library-like rules.
184+
185+
Args:
186+
ctx (ctx): The rule's context object
187+
crate_type (String): one of lib|rlib|dylib|staticlib|cdylib|proc-macro
188+
144189
Returns:
145190
list: A list of providers. See `rustc_compile_action`
146191
"""
@@ -155,7 +200,6 @@ def _rust_library_impl(ctx):
155200
output_hash = determine_output_hash(crate_root)
156201

157202
crate_name = name_to_crate_name(ctx.label.name)
158-
crate_type = getattr(ctx.attr, "crate_type")
159203
rust_lib_name = _determine_lib_name(
160204
crate_name,
161205
crate_type,
@@ -198,16 +242,14 @@ def _rust_binary_impl(ctx):
198242

199243
output = ctx.actions.declare_file(ctx.label.name + toolchain.binary_ext)
200244

201-
crate_type = getattr(ctx.attr, "crate_type")
202-
203245
return rustc_compile_action(
204246
ctx = ctx,
205247
toolchain = toolchain,
206-
crate_type = crate_type,
248+
crate_type = "bin",
207249
crate_info = rust_common.crate_info(
208250
name = crate_name,
209-
type = crate_type,
210-
root = crate_root_src(ctx.attr, ctx.files.srcs, crate_type),
251+
type = "bin",
252+
root = crate_root_src(ctx.attr, ctx.files.srcs, crate_type="bin"),
211253
srcs = ctx.files.srcs,
212254
deps = ctx.attr.deps,
213255
proc_macro_deps = ctx.attr.proc_macro_deps,
@@ -575,18 +617,6 @@ _common_attrs = {
575617
),
576618
}
577619

578-
_rust_library_attrs = {
579-
"crate_type": attr.string(
580-
doc = _tidy("""
581-
The type of linkage to use for building this library.
582-
Options include `"lib"`, `"rlib"`, `"dylib"`, `"cdylib"`, `"staticlib"`, and `"proc-macro"`.
583-
584-
The exact output file will depend on the toolchain used.
585-
"""),
586-
default = "rlib",
587-
),
588-
}
589-
590620
_rust_test_attrs = {
591621
"crate": attr.label(
592622
mandatory = False,
@@ -626,8 +656,7 @@ _rust_test_attrs = {
626656

627657
rust_library = rule(
628658
implementation = _rust_library_impl,
629-
attrs = dict(_common_attrs.items() +
630-
_rust_library_attrs.items()),
659+
attrs = dict(_common_attrs.items()),
631660
fragments = ["cpp"],
632661
host_fragments = ["cpp"],
633662
toolchains = [
@@ -700,10 +729,66 @@ INFO: Elapsed time: 1.245s, Critical Path: 1.01s
700729
""",
701730
)
702731

732+
rust_static_library = rule(
733+
implementation = _rust_static_library_impl,
734+
attrs = dict(_common_attrs.items()),
735+
fragments = ["cpp"],
736+
host_fragments = ["cpp"],
737+
toolchains = [
738+
str(Label("//rust:toolchain")),
739+
"@bazel_tools//tools/cpp:toolchain_type",
740+
],
741+
doc = """\
742+
Builds a Rust static library.
743+
744+
This static library will contain all transitively reachable crates and native objects.
745+
It is meant to be used when producing an artifact that is then consumed by some other build system
746+
(for example to produce an archive that Python program links against).
747+
748+
This rule provides CcInfo, so it can be used everywhere Bazel expects `rules_cc`.
749+
750+
When building the whole binary in Bazel, use `rust_library` instead.
751+
```
752+
""",
753+
)
754+
755+
rust_shared_library = rule(
756+
implementation = _rust_shared_library_impl,
757+
attrs = dict(_common_attrs.items()),
758+
fragments = ["cpp"],
759+
host_fragments = ["cpp"],
760+
toolchains = [
761+
str(Label("//rust:toolchain")),
762+
"@bazel_tools//tools/cpp:toolchain_type",
763+
],
764+
doc = """\
765+
Builds a Rust shared library.
766+
767+
This shared library will contain all transitively reachable crates and native objects.
768+
It is meant to be used when producing an artifact that is then consumed by some other build system
769+
(for example to produce a shared library that Python program links against).
770+
771+
This rule provides CcInfo, so it can be used everywhere Bazel expects `rules_cc`.
772+
773+
When building the whole binary in Bazel, use `rust_library` instead.
774+
""",
775+
)
776+
777+
rust_proc_macro = rule(
778+
implementation = _rust_proc_macro_impl,
779+
attrs = dict(_common_attrs.items()),
780+
fragments = ["cpp"],
781+
host_fragments = ["cpp"],
782+
toolchains = [
783+
str(Label("//rust:toolchain")),
784+
"@bazel_tools//tools/cpp:toolchain_type",
785+
],
786+
doc = """\
787+
Builds a Rust proc-macro crate.
788+
""",
789+
)
790+
703791
_rust_binary_attrs = {
704-
"crate_type": attr.string(
705-
default = "bin",
706-
),
707792
"linker_script": attr.label(
708793
doc = _tidy("""
709794
Link script to forward into linker via rustc options.
@@ -874,7 +959,7 @@ only depends on the `hello_lib` `rust_library` target:
874959
```python
875960
package(default_visibility = ["//visibility:public"])
876961
877-
load("@rules_rust//rust:rust.bzl", "rust_library", "rust_test")
962+
load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test")
878963
879964
rust_library(
880965
name = "hello_lib",
@@ -941,7 +1026,7 @@ with `greeting.rs` in `srcs` and a dependency on the `hello_lib` target:
9411026
```python
9421027
package(default_visibility = ["//visibility:public"])
9431028
944-
load("@rules_rust//rust:rust.bzl", "rust_library", "rust_test")
1029+
load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test")
9451030
9461031
rust_library(
9471032
name = "hello_lib",
@@ -1057,7 +1142,7 @@ To build the benchmark test, add a `rust_benchmark` target:
10571142
```python
10581143
package(default_visibility = ["//visibility:public"])
10591144
1060-
load("@rules_rust//rust:rust.bzl", "rust_library", "rust_benchmark")
1145+
load("@rules_rust//rust:defs.bzl", "rust_library", "rust_benchmark")
10611146
10621147
rust_library(
10631148
name = "fibonacci",

0 commit comments

Comments
 (0)