Skip to content

Commit

Permalink
Add crate_name attribute (#2118)
Browse files Browse the repository at this point in the history
This PR adds `crate_name` attribute to `rust_proto_library` and
`rust_grpc_library` rules. This aligns with `crate_name` attribute
provided in other rules to allow custom crate name different from label
name.

Setting `--crate-name` flag to `rustc_flags` attribute is not an option
because `rustc` does not allow `--crate-name` flag to be set twice.

@UebelAndre Would you mind reviewing this PR when you have a chance?

---------
  • Loading branch information
Vinh Tran authored Aug 23, 2023
1 parent 3e8f19e commit d5505c8
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 10 deletions.
6 changes: 4 additions & 2 deletions docs/flatten.md
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ Running `bazel test //hello_lib:hello_lib_doc_test` will run all documentation t
## rust_grpc_library

<pre>
rust_grpc_library(<a href="#rust_grpc_library-name">name</a>, <a href="#rust_grpc_library-deps">deps</a>, <a href="#rust_grpc_library-rust_deps">rust_deps</a>, <a href="#rust_grpc_library-rustc_flags">rustc_flags</a>)
rust_grpc_library(<a href="#rust_grpc_library-name">name</a>, <a href="#rust_grpc_library-crate_name">crate_name</a>, <a href="#rust_grpc_library-deps">deps</a>, <a href="#rust_grpc_library-rust_deps">rust_deps</a>, <a href="#rust_grpc_library-rustc_flags">rustc_flags</a>)
</pre>

Builds a Rust library crate from a set of `proto_library`s suitable for gRPC.
Expand Down Expand Up @@ -632,6 +632,7 @@ rust_binary(
| Name | Description | Type | Mandatory | Default |
| :------------- | :------------- | :------------- | :------------- | :------------- |
| <a id="rust_grpc_library-name"></a>name | A unique name for this target. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | |
| <a id="rust_grpc_library-crate_name"></a>crate_name | Crate name to use for this target.<br><br> This must be a valid Rust identifier, i.e. it may contain only alphanumeric characters and underscores. Defaults to the target name, with any hyphens replaced by underscores. | String | optional | <code>""</code> |
| <a id="rust_grpc_library-deps"></a>deps | List of proto_library dependencies that will be built. One crate for each proto_library will be created with the corresponding gRPC stubs. | <a href="https://bazel.build/concepts/labels">List of labels</a> | required | |
| <a id="rust_grpc_library-rust_deps"></a>rust_deps | The crates the generated library depends on. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | <code>[]</code> |
| <a id="rust_grpc_library-rustc_flags"></a>rustc_flags | List of compiler flags passed to <code>rustc</code>.<br><br> These strings are subject to Make variable expansion for predefined source/output path variables like <code>$location</code>, <code>$execpath</code>, and <code>$rootpath</code>. This expansion is useful if you wish to pass a generated file of arguments to rustc: <code>@$(location //package:target)</code>. | List of strings | optional | <code>[]</code> |
Expand Down Expand Up @@ -856,7 +857,7 @@ Rust Prost toolchain rule.
## rust_proto_library

<pre>
rust_proto_library(<a href="#rust_proto_library-name">name</a>, <a href="#rust_proto_library-deps">deps</a>, <a href="#rust_proto_library-rust_deps">rust_deps</a>, <a href="#rust_proto_library-rustc_flags">rustc_flags</a>)
rust_proto_library(<a href="#rust_proto_library-name">name</a>, <a href="#rust_proto_library-crate_name">crate_name</a>, <a href="#rust_proto_library-deps">deps</a>, <a href="#rust_proto_library-rust_deps">rust_deps</a>, <a href="#rust_proto_library-rustc_flags">rustc_flags</a>)
</pre>

Builds a Rust library crate from a set of `proto_library`s.
Expand Down Expand Up @@ -890,6 +891,7 @@ rust_binary(
| Name | Description | Type | Mandatory | Default |
| :------------- | :------------- | :------------- | :------------- | :------------- |
| <a id="rust_proto_library-name"></a>name | A unique name for this target. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | |
| <a id="rust_proto_library-crate_name"></a>crate_name | Crate name to use for this target.<br><br> This must be a valid Rust identifier, i.e. it may contain only alphanumeric characters and underscores. Defaults to the target name, with any hyphens replaced by underscores. | String | optional | <code>""</code> |
| <a id="rust_proto_library-deps"></a>deps | List of proto_library dependencies that will be built. One crate for each proto_library will be created with the corresponding stubs. | <a href="https://bazel.build/concepts/labels">List of labels</a> | required | |
| <a id="rust_proto_library-rust_deps"></a>rust_deps | The crates the generated library depends on. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | <code>[]</code> |
| <a id="rust_proto_library-rustc_flags"></a>rustc_flags | List of compiler flags passed to <code>rustc</code>.<br><br> These strings are subject to Make variable expansion for predefined source/output path variables like <code>$location</code>, <code>$execpath</code>, and <code>$rootpath</code>. This expansion is useful if you wish to pass a generated file of arguments to rustc: <code>@$(location //package:target)</code>. | List of strings | optional | <code>[]</code> |
Expand Down
6 changes: 4 additions & 2 deletions docs/rust_proto.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ configuration.
## rust_grpc_library

<pre>
rust_grpc_library(<a href="#rust_grpc_library-name">name</a>, <a href="#rust_grpc_library-deps">deps</a>, <a href="#rust_grpc_library-rust_deps">rust_deps</a>, <a href="#rust_grpc_library-rustc_flags">rustc_flags</a>)
rust_grpc_library(<a href="#rust_grpc_library-name">name</a>, <a href="#rust_grpc_library-crate_name">crate_name</a>, <a href="#rust_grpc_library-deps">deps</a>, <a href="#rust_grpc_library-rust_deps">rust_deps</a>, <a href="#rust_grpc_library-rustc_flags">rustc_flags</a>)
</pre>

Builds a Rust library crate from a set of `proto_library`s suitable for gRPC.
Expand Down Expand Up @@ -292,6 +292,7 @@ rust_binary(
| Name | Description | Type | Mandatory | Default |
| :------------- | :------------- | :------------- | :------------- | :------------- |
| <a id="rust_grpc_library-name"></a>name | A unique name for this target. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | |
| <a id="rust_grpc_library-crate_name"></a>crate_name | Crate name to use for this target.<br><br> This must be a valid Rust identifier, i.e. it may contain only alphanumeric characters and underscores. Defaults to the target name, with any hyphens replaced by underscores. | String | optional | <code>""</code> |
| <a id="rust_grpc_library-deps"></a>deps | List of proto_library dependencies that will be built. One crate for each proto_library will be created with the corresponding gRPC stubs. | <a href="https://bazel.build/concepts/labels">List of labels</a> | required | |
| <a id="rust_grpc_library-rust_deps"></a>rust_deps | The crates the generated library depends on. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | <code>[]</code> |
| <a id="rust_grpc_library-rustc_flags"></a>rustc_flags | List of compiler flags passed to <code>rustc</code>.<br><br> These strings are subject to Make variable expansion for predefined source/output path variables like <code>$location</code>, <code>$execpath</code>, and <code>$rootpath</code>. This expansion is useful if you wish to pass a generated file of arguments to rustc: <code>@$(location //package:target)</code>. | List of strings | optional | <code>[]</code> |
Expand Down Expand Up @@ -331,7 +332,7 @@ Rust Prost toolchain rule.
## rust_proto_library

<pre>
rust_proto_library(<a href="#rust_proto_library-name">name</a>, <a href="#rust_proto_library-deps">deps</a>, <a href="#rust_proto_library-rust_deps">rust_deps</a>, <a href="#rust_proto_library-rustc_flags">rustc_flags</a>)
rust_proto_library(<a href="#rust_proto_library-name">name</a>, <a href="#rust_proto_library-crate_name">crate_name</a>, <a href="#rust_proto_library-deps">deps</a>, <a href="#rust_proto_library-rust_deps">rust_deps</a>, <a href="#rust_proto_library-rustc_flags">rustc_flags</a>)
</pre>

Builds a Rust library crate from a set of `proto_library`s.
Expand Down Expand Up @@ -365,6 +366,7 @@ rust_binary(
| Name | Description | Type | Mandatory | Default |
| :------------- | :------------- | :------------- | :------------- | :------------- |
| <a id="rust_proto_library-name"></a>name | A unique name for this target. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | |
| <a id="rust_proto_library-crate_name"></a>crate_name | Crate name to use for this target.<br><br> This must be a valid Rust identifier, i.e. it may contain only alphanumeric characters and underscores. Defaults to the target name, with any hyphens replaced by underscores. | String | optional | <code>""</code> |
| <a id="rust_proto_library-deps"></a>deps | List of proto_library dependencies that will be built. One crate for each proto_library will be created with the corresponding stubs. | <a href="https://bazel.build/concepts/labels">List of labels</a> | required | |
| <a id="rust_proto_library-rust_deps"></a>rust_deps | The crates the generated library depends on. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | <code>[]</code> |
| <a id="rust_proto_library-rustc_flags"></a>rustc_flags | List of compiler flags passed to <code>rustc</code>.<br><br> These strings are subject to Make variable expansion for predefined source/output path variables like <code>$location</code>, <code>$execpath</code>, and <code>$rootpath</code>. This expansion is useful if you wish to pass a generated file of arguments to rustc: <code>@$(location //package:target)</code>. | List of strings | optional | <code>[]</code> |
Expand Down
7 changes: 4 additions & 3 deletions examples/proto/basic/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library")
package(default_visibility = ["//proto:__subpackages__"])

rust_proto_library(
name = "common_proto_rust",
name = "libcommon_proto_rust",
crate_name = "common_proto_rust",
tags = ["manual"],
deps = ["//proto:common"],
)
Expand All @@ -13,7 +14,7 @@ rust_library(
name = "common_lib",
srcs = ["lib.rs"],
tags = ["manual"],
deps = [":common_proto_rust"],
deps = [":libcommon_proto_rust"],
)

rust_binary(
Expand All @@ -22,6 +23,6 @@ rust_binary(
tags = ["manual"],
deps = [
":common_lib",
":common_proto_rust",
":libcommon_proto_rust",
],
)
3 changes: 2 additions & 1 deletion examples/proto/helloworld/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ proto_library(
)

rust_grpc_library(
name = "helloworld_proto",
name = "libhelloworld_proto",
crate_name = "helloworld_proto",
tags = ["manual"],
visibility = ["//proto/helloworld:__subpackages__"],
deps = [":helloworld"],
Expand Down
2 changes: 1 addition & 1 deletion examples/proto/helloworld/greeter_client/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ rust_binary(
tags = ["manual"],
visibility = ["//proto/helloworld:__subpackages__"],
deps = [
"//proto/helloworld:helloworld_proto",
"//proto/helloworld:libhelloworld_proto",
] + GRPC_COMPILE_DEPS,
)
18 changes: 17 additions & 1 deletion proto/protobuf/proto.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ def _rust_protogrpc_library_impl(ctx, is_grpc):
]

toolchain = find_toolchain(ctx)
crate_name = compute_crate_name(ctx.workspace_name, ctx.label, toolchain)
crate_name = compute_crate_name(ctx.workspace_name, ctx.label, toolchain, ctx.attr.crate_name)

return _rust_proto_compile(
protos = depset(transitive = transitive_sources),
Expand All @@ -280,6 +280,14 @@ def _rust_proto_library_impl(ctx):
rust_proto_library = rule(
implementation = _rust_proto_library_impl,
attrs = {
"crate_name": attr.string(
doc = """\
Crate name to use for this target.
This must be a valid Rust identifier, i.e. it may contain only alphanumeric characters and underscores.
Defaults to the target name, with any hyphens replaced by underscores.
""",
),
"deps": attr.label_list(
doc = (
"List of proto_library dependencies that will be built. " +
Expand Down Expand Up @@ -368,6 +376,14 @@ def _rust_grpc_library_impl(ctx):
rust_grpc_library = rule(
implementation = _rust_grpc_library_impl,
attrs = {
"crate_name": attr.string(
doc = """\
Crate name to use for this target.
This must be a valid Rust identifier, i.e. it may contain only alphanumeric characters and underscores.
Defaults to the target name, with any hyphens replaced by underscores.
""",
),
"deps": attr.label_list(
doc = (
"List of proto_library dependencies that will be built. " +
Expand Down

0 comments on commit d5505c8

Please sign in to comment.