From 0f69967615a7c671f2f058e6f951ae3a9549b1ab Mon Sep 17 00:00:00 2001 From: Pras Velagapudi Date: Fri, 10 Jun 2022 08:43:33 -0400 Subject: [PATCH] `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. --- docs/native_binary_doc.md | 4 ++-- rules/native_binary.bzl | 9 +++++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/docs/native_binary_doc.md b/docs/native_binary_doc.md index a9c476d..0dcee4a 100755 --- a/docs/native_binary_doc.md +++ b/docs/native_binary_doc.md @@ -30,7 +30,7 @@ in genrule.tools for example. You can also augment the binary with runfiles. | :------------- | :------------- | :------------- | :------------- | :------------- | | name | A unique name for this target. | Name | required | | | data | data dependencies. See https://docs.bazel.build/versions/main/be/common-definitions.html#typical.data | List of labels | optional | [] | -| out | An output name for the copy of the binary | String | required | | +| out | An output name for the copy of the binary (defaults to target name) | String | optional | "" | | src | path of the pre-built executable | Label | required | | @@ -56,7 +56,7 @@ the binary with runfiles. | :------------- | :------------- | :------------- | :------------- | :------------- | | name | A unique name for this target. | Name | required | | | data | data dependencies. See https://docs.bazel.build/versions/main/be/common-definitions.html#typical.data | List of labels | optional | [] | -| out | An output name for the copy of the binary | String | required | | +| out | An output name for the copy of the binary (defaults to target name) | String | optional | "" | | src | path of the pre-built executable | Label | required | | diff --git a/rules/native_binary.bzl b/rules/native_binary.bzl index a027fc9..25f6a25 100644 --- a/rules/native_binary.bzl +++ b/rules/native_binary.bzl @@ -21,7 +21,12 @@ don't depend on Bash and work with --shell_executable="". """ def _impl_rule(ctx): - out = ctx.actions.declare_file(ctx.attr.out) + if ctx.attr.out: + out_file = ctx.attr.out + else: + out_file = ctx.name + out = ctx.actions.declare_file(out_file) + ctx.actions.symlink( target_file = ctx.executable.src, output = out, @@ -64,7 +69,7 @@ _ATTRS = { " https://docs.bazel.build/versions/main/be/common-definitions.html#typical.data", ), # "out" is attr.string instead of attr.output, so that it is select()'able. - "out": attr.string(mandatory = True, doc = "An output name for the copy of the binary"), + "out": attr.string(doc = "An output name for the copy of the binary (defaults to target name)"), } native_binary = rule(