Skip to content

Commit 0f69967

Browse files
psigenprasbg
authored andcommitted
native_binary: use target name as filename by default
This changes the `out` attribute in native binary to default to the target name if unspecified, which is convenient because it can be omitted when renaming targets that are linux executables and frequently share the target name.
1 parent 207acb3 commit 0f69967

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

docs/native_binary_doc.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ in genrule.tools for example. You can also augment the binary with runfiles.
3030
| :------------- | :------------- | :------------- | :------------- | :------------- |
3131
| <a id="native_binary-name"></a>name | A unique name for this target. | <a href="https://bazel.build/docs/build-ref.html#name">Name</a> | required | |
3232
| <a id="native_binary-data"></a>data | data dependencies. See https://docs.bazel.build/versions/main/be/common-definitions.html#typical.data | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [] |
33-
| <a id="native_binary-out"></a>out | An output name for the copy of the binary | String | required | |
33+
| <a id="native_binary-out"></a>out | An output name for the copy of the binary (defaults to target name) | String | optional | "" |
3434
| <a id="native_binary-src"></a>src | path of the pre-built executable | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | required | |
3535

3636

@@ -56,7 +56,7 @@ the binary with runfiles.
5656
| :------------- | :------------- | :------------- | :------------- | :------------- |
5757
| <a id="native_test-name"></a>name | A unique name for this target. | <a href="https://bazel.build/docs/build-ref.html#name">Name</a> | required | |
5858
| <a id="native_test-data"></a>data | data dependencies. See https://docs.bazel.build/versions/main/be/common-definitions.html#typical.data | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [] |
59-
| <a id="native_test-out"></a>out | An output name for the copy of the binary | String | required | |
59+
| <a id="native_test-out"></a>out | An output name for the copy of the binary (defaults to target name) | String | optional | "" |
6060
| <a id="native_test-src"></a>src | path of the pre-built executable | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | required | |
6161

6262

rules/native_binary.bzl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@ don't depend on Bash and work with --shell_executable="".
2121
"""
2222

2323
def _impl_rule(ctx):
24-
out = ctx.actions.declare_file(ctx.attr.out)
24+
if ctx.attr.out:
25+
out_file = ctx.attr.out
26+
else:
27+
out_file = ctx.name
28+
out = ctx.actions.declare_file(out_file)
29+
2530
ctx.actions.symlink(
2631
target_file = ctx.executable.src,
2732
output = out,
@@ -64,7 +69,7 @@ _ATTRS = {
6469
" https://docs.bazel.build/versions/main/be/common-definitions.html#typical.data",
6570
),
6671
# "out" is attr.string instead of attr.output, so that it is select()'able.
67-
"out": attr.string(mandatory = True, doc = "An output name for the copy of the binary"),
72+
"out": attr.string(doc = "An output name for the copy of the binary (defaults to target name)"),
6873
}
6974

7075
native_binary = rule(

0 commit comments

Comments
 (0)