-
Notifications
You must be signed in to change notification settings - Fork 449
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add an attr to rust_binary for customization of the binary name (…
…#2970) It's often desirable to set the name of the resulting binary to something else other than the target name (`attr.name`), perhaps when the `rust_binary` target is wrapped in a macro, and not considered the "main" target of that macro. This diff adds a new `binary_name` attribute to `rust_binary`, which when set will be used as the resulting binaries prefix instead of the targets name. The default is to still use the target name.
- Loading branch information
Matt Mackay
authored
Oct 30, 2024
1 parent
223d1e7
commit 9d799b2
Showing
5 changed files
with
101 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
], | ||
) |