aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGeoffrey Martin-Noble <gcmn@google.com>2022-05-17 13:26:16 +0200
committerGitHub <noreply@github.com>2022-05-17 07:26:16 -0400
commita6f17ab1fec39a2d65d13bf3c6c79079237a53f7 (patch)
tree5423ae8a03abeab0b11daba85a880b1f23093dd7
parenta832b8d717c01e76ecfc28322873a44e12aa23bd (diff)
downloadbazel-skylib-a6f17ab1fec39a2d65d13bf3c6c79079237a53f7.tar.gz
Switch native_binary/test to use symlinks (#340)
A copy should not be necessary here. The symlink action falls back to copying when symlinks are not enabled on windows, which I think would be the only problem with using symlinks: https://docs.bazel.build/versions/4.2.2/command-line-reference.html#flag--windows_enable_symlinks
-rwxr-xr-xdocs/native_binary_doc.md45
-rw-r--r--rules/BUILD1
-rw-r--r--rules/native_binary.bzl97
3 files changed, 53 insertions, 90 deletions
diff --git a/docs/native_binary_doc.md b/docs/native_binary_doc.md
index 1a330ca..a9c476d 100755
--- a/docs/native_binary_doc.md
+++ b/docs/native_binary_doc.md
@@ -5,7 +5,7 @@ native_binary() and native_test() rule implementations.
These rules let you wrap a pre-built binary or script in a conventional binary
and test rule respectively. They fulfill the same goal as sh_binary and sh_test
do, but they run the wrapped binary directly, instead of through Bash, so they
-don't depend on Bash and work with --shell_exectuable="".
+don't depend on Bash and work with --shell_executable="".
<a id="#native_binary"></a>
@@ -13,24 +13,25 @@ don't depend on Bash and work with --shell_exectuable="".
## native_binary
<pre>
-native_binary(<a href="#native_binary-name">name</a>, <a href="#native_binary-src">src</a>, <a href="#native_binary-out">out</a>, <a href="#native_binary-data">data</a>, <a href="#native_binary-kwargs">kwargs</a>)
+native_binary(<a href="#native_binary-name">name</a>, <a href="#native_binary-data">data</a>, <a href="#native_binary-out">out</a>, <a href="#native_binary-src">src</a>)
</pre>
+
Wraps a pre-built binary or script with a binary rule.
-You can "bazel run" this rule like any other binary rule, and use it as a tool in genrule.tools for example. You can also augment the binary with runfiles.
+You can "bazel run" this rule like any other binary rule, and use it as a tool
+in genrule.tools for example. You can also augment the binary with runfiles.
-**PARAMETERS**
+**ATTRIBUTES**
-| Name | Description | Default Value |
-| :------------- | :------------- | :------------- |
-| <a id="native_binary-name"></a>name | The name of the rule. | none |
-| <a id="native_binary-src"></a>src | label; path of the pre-built executable | none |
-| <a id="native_binary-out"></a>out | output; an output name for the copy of the binary. (Bazel requires that this rule make a copy of 'src'.) | none |
-| <a id="native_binary-data"></a>data | list of labels; data dependencies | <code>None</code> |
-| <a id="native_binary-kwargs"></a>kwargs | The &lt;a href="https://docs.bazel.build/versions/main/be/common-definitions.html#common-attributes-binaries"&gt;common attributes for binaries&lt;/a&gt;. | none |
+| Name | Description | Type | Mandatory | Default |
+| :------------- | :------------- | :------------- | :------------- | :------------- |
+| <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 | |
+| <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 | [] |
+| <a id="native_binary-out"></a>out | An output name for the copy of the binary | String | required | |
+| <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 | |
<a id="#native_test"></a>
@@ -38,24 +39,24 @@ You can "bazel run" this rule like any other binary rule, and use it as a tool i
## native_test
<pre>
-native_test(<a href="#native_test-name">name</a>, <a href="#native_test-src">src</a>, <a href="#native_test-out">out</a>, <a href="#native_test-data">data</a>, <a href="#native_test-kwargs">kwargs</a>)
+native_test(<a href="#native_test-name">name</a>, <a href="#native_test-data">data</a>, <a href="#native_test-out">out</a>, <a href="#native_test-src">src</a>)
</pre>
+
Wraps a pre-built binary or script with a test rule.
-You can "bazel test" this rule like any other test rule. You can also augment the binary with
-runfiles.
+You can "bazel test" this rule like any other test rule. You can also augment
+the binary with runfiles.
-**PARAMETERS**
+**ATTRIBUTES**
-| Name | Description | Default Value |
-| :------------- | :------------- | :------------- |
-| <a id="native_test-name"></a>name | The name of the test rule. | none |
-| <a id="native_test-src"></a>src | label; path of the pre-built executable | none |
-| <a id="native_test-out"></a>out | output; an output name for the copy of the binary. (Bazel requires that this rule make a copy of 'src'.) | none |
-| <a id="native_test-data"></a>data | list of labels; data dependencies | <code>None</code> |
-| <a id="native_test-kwargs"></a>kwargs | The &lt;a href="https://docs.bazel.build/versions/main/be/common-definitions.html#common-attributes-tests"&gt;common attributes for tests&lt;/a&gt;. | none |
+| Name | Description | Type | Mandatory | Default |
+| :------------- | :------------- | :------------- | :------------- | :------------- |
+| <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 | |
+| <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 | [] |
+| <a id="native_test-out"></a>out | An output name for the copy of the binary | String | required | |
+| <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 | |
diff --git a/rules/BUILD b/rules/BUILD
index c1ffbf2..e2709d6 100644
--- a/rules/BUILD
+++ b/rules/BUILD
@@ -41,7 +41,6 @@ bzl_library(
bzl_library(
name = "native_binary",
srcs = ["native_binary.bzl"],
- deps = ["//rules/private:copy_file_private"],
)
bzl_library(
diff --git a/rules/native_binary.bzl b/rules/native_binary.bzl
index a3f0d73..a027fc9 100644
--- a/rules/native_binary.bzl
+++ b/rules/native_binary.bzl
@@ -17,17 +17,16 @@
These rules let you wrap a pre-built binary or script in a conventional binary
and test rule respectively. They fulfill the same goal as sh_binary and sh_test
do, but they run the wrapped binary directly, instead of through Bash, so they
-don't depend on Bash and work with --shell_exectuable="".
+don't depend on Bash and work with --shell_executable="".
"""
-load("//rules/private:copy_file_private.bzl", "copy_bash", "copy_cmd")
-
-def _impl_rule(ctx, is_windows):
+def _impl_rule(ctx):
out = ctx.actions.declare_file(ctx.attr.out)
- if is_windows:
- copy_cmd(ctx, ctx.executable.src, out)
- else:
- copy_bash(ctx, ctx.executable.src, out)
+ ctx.actions.symlink(
+ target_file = ctx.executable.src,
+ output = out,
+ is_executable = True,
+ )
runfiles = ctx.runfiles(files = ctx.files.data)
# Bazel 4.x LTS does not support `merge_all`.
@@ -48,9 +47,6 @@ def _impl_rule(ctx, is_windows):
runfiles = runfiles,
)
-def _impl(ctx):
- return _impl_rule(ctx, ctx.attr.is_windows)
-
_ATTRS = {
"src": attr.label(
executable = True,
@@ -60,70 +56,37 @@ _ATTRS = {
allow_files = True,
mandatory = True,
cfg = "target",
+ doc = "path of the pre-built executable",
+ ),
+ "data": attr.label_list(
+ allow_files = True,
+ doc = "data dependencies. See" +
+ " https://docs.bazel.build/versions/main/be/common-definitions.html#typical.data",
),
- "data": attr.label_list(allow_files = True),
# "out" is attr.string instead of attr.output, so that it is select()'able.
- "out": attr.string(mandatory = True),
- "is_windows": attr.bool(mandatory = True),
+ "out": attr.string(mandatory = True, doc = "An output name for the copy of the binary"),
}
-_native_binary = rule(
- implementation = _impl,
+native_binary = rule(
+ implementation = _impl_rule,
attrs = _ATTRS,
executable = True,
+ doc = """
+Wraps a pre-built binary or script with a binary rule.
+
+You can "bazel run" this rule like any other binary rule, and use it as a tool
+in genrule.tools for example. You can also augment the binary with runfiles.
+""",
)
-_native_test = rule(
- implementation = _impl,
+native_test = rule(
+ implementation = _impl_rule,
attrs = _ATTRS,
test = True,
-)
+ doc = """
+Wraps a pre-built binary or script with a test rule.
-def native_binary(name, src, out, data = None, **kwargs):
- """Wraps a pre-built binary or script with a binary rule.
-
- You can "bazel run" this rule like any other binary rule, and use it as a tool in genrule.tools for example. You can also augment the binary with runfiles.
-
- Args:
- name: The name of the rule.
- src: label; path of the pre-built executable
- out: output; an output name for the copy of the binary. (Bazel requires that this rule make a copy of 'src'.)
- data: list of labels; data dependencies
- **kwargs: The <a href="https://docs.bazel.build/versions/main/be/common-definitions.html#common-attributes-binaries">common attributes for binaries</a>.
- """
- _native_binary(
- name = name,
- src = src,
- out = out,
- data = data,
- is_windows = select({
- "@bazel_tools//src/conditions:host_windows": True,
- "//conditions:default": False,
- }),
- **kwargs
- )
-
-def native_test(name, src, out, data = None, **kwargs):
- """Wraps a pre-built binary or script with a test rule.
-
- You can "bazel test" this rule like any other test rule. You can also augment the binary with
- runfiles.
-
- Args:
- name: The name of the test rule.
- src: label; path of the pre-built executable
- out: output; an output name for the copy of the binary. (Bazel requires that this rule make a copy of 'src'.)
- data: list of labels; data dependencies
- **kwargs: The <a href="https://docs.bazel.build/versions/main/be/common-definitions.html#common-attributes-tests">common attributes for tests</a>.
- """
- _native_test(
- name = name,
- src = src,
- out = out,
- data = data,
- is_windows = select({
- "@bazel_tools//src/conditions:host_windows": True,
- "//conditions:default": False,
- }),
- **kwargs
- )
+You can "bazel test" this rule like any other test rule. You can also augment
+the binary with runfiles.
+""",
+)