Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add an attr to rust_binary for customization of the binary name #2970

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions docs/src/flatten.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,10 @@ A toolchain for [rust-analyzer](https://rust-analyzer.github.io/).
## rust_binary

<pre>
rust_binary(<a href="#rust_binary-name">name</a>, <a href="#rust_binary-deps">deps</a>, <a href="#rust_binary-srcs">srcs</a>, <a href="#rust_binary-data">data</a>, <a href="#rust_binary-aliases">aliases</a>, <a href="#rust_binary-alwayslink">alwayslink</a>, <a href="#rust_binary-compile_data">compile_data</a>, <a href="#rust_binary-crate_features">crate_features</a>, <a href="#rust_binary-crate_name">crate_name</a>,
<a href="#rust_binary-crate_root">crate_root</a>, <a href="#rust_binary-crate_type">crate_type</a>, <a href="#rust_binary-edition">edition</a>, <a href="#rust_binary-env">env</a>, <a href="#rust_binary-experimental_use_cc_common_link">experimental_use_cc_common_link</a>, <a href="#rust_binary-linker_script">linker_script</a>,
<a href="#rust_binary-malloc">malloc</a>, <a href="#rust_binary-out_binary">out_binary</a>, <a href="#rust_binary-platform">platform</a>, <a href="#rust_binary-proc_macro_deps">proc_macro_deps</a>, <a href="#rust_binary-rustc_env">rustc_env</a>, <a href="#rust_binary-rustc_env_files">rustc_env_files</a>, <a href="#rust_binary-rustc_flags">rustc_flags</a>,
<a href="#rust_binary-stamp">stamp</a>, <a href="#rust_binary-version">version</a>)
rust_binary(<a href="#rust_binary-name">name</a>, <a href="#rust_binary-deps">deps</a>, <a href="#rust_binary-srcs">srcs</a>, <a href="#rust_binary-data">data</a>, <a href="#rust_binary-aliases">aliases</a>, <a href="#rust_binary-alwayslink">alwayslink</a>, <a href="#rust_binary-binary_name">binary_name</a>, <a href="#rust_binary-compile_data">compile_data</a>, <a href="#rust_binary-crate_features">crate_features</a>,
<a href="#rust_binary-crate_name">crate_name</a>, <a href="#rust_binary-crate_root">crate_root</a>, <a href="#rust_binary-crate_type">crate_type</a>, <a href="#rust_binary-edition">edition</a>, <a href="#rust_binary-env">env</a>, <a href="#rust_binary-experimental_use_cc_common_link">experimental_use_cc_common_link</a>,
<a href="#rust_binary-linker_script">linker_script</a>, <a href="#rust_binary-malloc">malloc</a>, <a href="#rust_binary-out_binary">out_binary</a>, <a href="#rust_binary-platform">platform</a>, <a href="#rust_binary-proc_macro_deps">proc_macro_deps</a>, <a href="#rust_binary-rustc_env">rustc_env</a>, <a href="#rust_binary-rustc_env_files">rustc_env_files</a>,
<a href="#rust_binary-rustc_flags">rustc_flags</a>, <a href="#rust_binary-stamp">stamp</a>, <a href="#rust_binary-version">version</a>)
</pre>

Builds a Rust binary crate.
Expand Down Expand Up @@ -303,6 +303,7 @@ is available under the key `dsym_folder` in `OutputGroupInfo`.
| <a id="rust_binary-data"></a>data | List of files used by this rule at compile time and runtime.<br><br>If including data at compile time with include_str!() and similar, prefer `compile_data` over `data`, to prevent the data also being included in the runfiles. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
| <a id="rust_binary-aliases"></a>aliases | Remap crates to a new name or moniker for linkage to this target<br><br>These are other `rust_library` targets and will be presented as the new name given. | <a href="https://bazel.build/rules/lib/dict">Dictionary: Label -> String</a> | optional | `{}` |
| <a id="rust_binary-alwayslink"></a>alwayslink | If 1, any binary that depends (directly or indirectly) on this library will link in all the object files even if some contain no symbols referenced by the binary.<br><br>This attribute is used by the C++ Starlark API when passing CcInfo providers. | Boolean | optional | `False` |
| <a id="rust_binary-binary_name"></a>binary_name | Override the resulting binary file name. By default, the binary file will be named using the `name` attribute on this rule, however sometimes that is not deseriable. | String | optional | `""` |
| <a id="rust_binary-compile_data"></a>compile_data | List of files used by this rule at compile time.<br><br>This attribute can be used to specify any data files that are embedded into the library, such as via the [`include_str!`](https://doc.rust-lang.org/std/macro.include_str!.html) macro. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
| <a id="rust_binary-crate_features"></a>crate_features | List of features to enable for this crate.<br><br>Features are defined in the code using the `#[cfg(feature = "foo")]` configuration option. The features listed here will be passed to `rustc` with `--cfg feature="${feature_name}"` flags. | List of strings | optional | `[]` |
| <a id="rust_binary-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 | `""` |
Expand Down
9 changes: 5 additions & 4 deletions docs/src/rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ Add additional rustc_flags from the command line with `--@rules_rust//:extra_rus
## rust_binary

<pre>
rust_binary(<a href="#rust_binary-name">name</a>, <a href="#rust_binary-deps">deps</a>, <a href="#rust_binary-srcs">srcs</a>, <a href="#rust_binary-data">data</a>, <a href="#rust_binary-aliases">aliases</a>, <a href="#rust_binary-alwayslink">alwayslink</a>, <a href="#rust_binary-compile_data">compile_data</a>, <a href="#rust_binary-crate_features">crate_features</a>, <a href="#rust_binary-crate_name">crate_name</a>,
<a href="#rust_binary-crate_root">crate_root</a>, <a href="#rust_binary-crate_type">crate_type</a>, <a href="#rust_binary-edition">edition</a>, <a href="#rust_binary-env">env</a>, <a href="#rust_binary-experimental_use_cc_common_link">experimental_use_cc_common_link</a>, <a href="#rust_binary-linker_script">linker_script</a>,
<a href="#rust_binary-malloc">malloc</a>, <a href="#rust_binary-out_binary">out_binary</a>, <a href="#rust_binary-platform">platform</a>, <a href="#rust_binary-proc_macro_deps">proc_macro_deps</a>, <a href="#rust_binary-rustc_env">rustc_env</a>, <a href="#rust_binary-rustc_env_files">rustc_env_files</a>, <a href="#rust_binary-rustc_flags">rustc_flags</a>,
<a href="#rust_binary-stamp">stamp</a>, <a href="#rust_binary-version">version</a>)
rust_binary(<a href="#rust_binary-name">name</a>, <a href="#rust_binary-deps">deps</a>, <a href="#rust_binary-srcs">srcs</a>, <a href="#rust_binary-data">data</a>, <a href="#rust_binary-aliases">aliases</a>, <a href="#rust_binary-alwayslink">alwayslink</a>, <a href="#rust_binary-binary_name">binary_name</a>, <a href="#rust_binary-compile_data">compile_data</a>, <a href="#rust_binary-crate_features">crate_features</a>,
<a href="#rust_binary-crate_name">crate_name</a>, <a href="#rust_binary-crate_root">crate_root</a>, <a href="#rust_binary-crate_type">crate_type</a>, <a href="#rust_binary-edition">edition</a>, <a href="#rust_binary-env">env</a>, <a href="#rust_binary-experimental_use_cc_common_link">experimental_use_cc_common_link</a>,
<a href="#rust_binary-linker_script">linker_script</a>, <a href="#rust_binary-malloc">malloc</a>, <a href="#rust_binary-out_binary">out_binary</a>, <a href="#rust_binary-platform">platform</a>, <a href="#rust_binary-proc_macro_deps">proc_macro_deps</a>, <a href="#rust_binary-rustc_env">rustc_env</a>, <a href="#rust_binary-rustc_env_files">rustc_env_files</a>,
<a href="#rust_binary-rustc_flags">rustc_flags</a>, <a href="#rust_binary-stamp">stamp</a>, <a href="#rust_binary-version">version</a>)
</pre>

Builds a Rust binary crate.
Expand Down Expand Up @@ -195,6 +195,7 @@ is available under the key `dsym_folder` in `OutputGroupInfo`.
| <a id="rust_binary-data"></a>data | List of files used by this rule at compile time and runtime.<br><br>If including data at compile time with include_str!() and similar, prefer `compile_data` over `data`, to prevent the data also being included in the runfiles. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
| <a id="rust_binary-aliases"></a>aliases | Remap crates to a new name or moniker for linkage to this target<br><br>These are other `rust_library` targets and will be presented as the new name given. | <a href="https://bazel.build/rules/lib/dict">Dictionary: Label -> String</a> | optional | `{}` |
| <a id="rust_binary-alwayslink"></a>alwayslink | If 1, any binary that depends (directly or indirectly) on this library will link in all the object files even if some contain no symbols referenced by the binary.<br><br>This attribute is used by the C++ Starlark API when passing CcInfo providers. | Boolean | optional | `False` |
| <a id="rust_binary-binary_name"></a>binary_name | Override the resulting binary file name. By default, the binary file will be named using the `name` attribute on this rule, however sometimes that is not deseriable. | String | optional | `""` |
| <a id="rust_binary-compile_data"></a>compile_data | List of files used by this rule at compile time.<br><br>This attribute can be used to specify any data files that are embedded into the library, such as via the [`include_str!`](https://doc.rust-lang.org/std/macro.include_str!.html) macro. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
| <a id="rust_binary-crate_features"></a>crate_features | List of features to enable for this crate.<br><br>Features are defined in the code using the `#[cfg(feature = "foo")]` configuration option. The features listed here will be passed to `rustc` with `--cfg feature="${feature_name}"` flags. | List of strings | optional | `[]` |
| <a id="rust_binary-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 | `""` |
Expand Down
12 changes: 11 additions & 1 deletion rust/private/rust.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,11 @@ def _rust_binary_impl(ctx):
crate_name = compute_crate_name(ctx.workspace_name, ctx.label, toolchain, ctx.attr.crate_name)
_assert_correct_dep_mapping(ctx)

output = ctx.actions.declare_file(ctx.label.name + toolchain.binary_ext)
if ctx.attr.binary_name:
output_filename = ctx.attr.binary_name
else:
output_filename = ctx.label.name
output = ctx.actions.declare_file(output_filename + toolchain.binary_ext)

deps = transform_deps(ctx.attr.deps)
proc_macro_deps = transform_deps(ctx.attr.proc_macro_deps + get_import_macro_deps(ctx))
Expand Down Expand Up @@ -1033,6 +1037,12 @@ rust_proc_macro = rule(
)

_rust_binary_attrs = dict({
"binary_name": attr.string(
doc = dedent("""\
Override the resulting binary file name. By default, the binary file will be named using the `name` attribute on this rule,
however sometimes that is not deseriable.
"""),
),
"crate_type": attr.string(
doc = dedent("""\
Crate type that will be passed to `rustc` to be used for building this crate.
Expand Down
5 changes: 5 additions & 0 deletions test/unit/rust_binary_name/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
load(":rust_binary_name_suite.bzl", "binary_name_test_suite")

binary_name_test_suite(
name = "binary_name_test_suite",
)
75 changes: 75 additions & 0 deletions test/unit/rust_binary_name/rust_binary_name_suite.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
"""Starlark tests for `rust_binary.binary_name`"""

load("@bazel_skylib//lib:paths.bzl", "paths")
load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
load("@bazel_skylib//rules:write_file.bzl", "write_file")
load("//rust:defs.bzl", "rust_binary")

def _rust_binary_binary_name_test_impl(ctx):
expected_basename = ctx.attr.expected_binary_name

env = analysistest.begin(ctx)
target = analysistest.target_under_test(env)

action = target.actions[0]
output = action.outputs.to_list()[0]

filename = paths.split_extension(output.basename)[0]
asserts.equals(env, filename, expected_basename)

return analysistest.end(env)

_binary_name_test = analysistest.make(
_rust_binary_binary_name_test_impl,
attrs = {
"expected_binary_name": attr.string(),
},
)

def binary_name_test_suite(name):
"""Entry-point macro called from the BUILD file.

Args:
name (str): The name of the test suite.
"""
write_file(
name = "main",
out = "main.rs",
content = [
"fn main() {}",
"",
],
)

rust_binary(
name = "bin_unset",
srcs = [":main.rs"],
edition = "2021",
)

_binary_name_test(
name = "unset_binary_name_test",
target_under_test = ":bin_unset",
expected_binary_name = "bin_unset",
)

rust_binary(
name = "bin",
binary_name = "some-binary",
srcs = [":main.rs"],
edition = "2021",
)

_binary_name_test(
name = "set_binary_name_test",
target_under_test = ":bin",
expected_binary_name = "some-binary",
)

native.test_suite(
name = name,
tests = [
":unset_binary_name_test",
":set_binary_name_test",
],
)
Loading