Skip to content

Commit 2c79484

Browse files
Support custom 'mnemonic' and 'progress_message'
Adds support for custom 'mnemonic' and 'progress_message' attributes for 'copy_file', 'copy_directory' and 'run_binary' rules.
1 parent e60cf00 commit 2c79484

6 files changed

+41
-20
lines changed

docs/copy_directory_doc.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ on Windows (no Bash is required).
1111
## copy_directory
1212

1313
<pre>
14-
copy_directory(<a href="#copy_directory-name">name</a>, <a href="#copy_directory-src">src</a>, <a href="#copy_directory-out">out</a>, <a href="#copy_directory-kwargs">kwargs</a>)
14+
copy_directory(<a href="#copy_directory-name">name</a>, <a href="#copy_directory-src">src</a>, <a href="#copy_directory-out">out</a>, <a href="#copy_directory-progress_message">progress_message</a>, <a href="#copy_directory-mnemonic">mnemonic</a>, <a href="#copy_directory-kwargs">kwargs</a>)
1515
</pre>
1616

1717
Copies a directory to another location.
@@ -33,6 +33,8 @@ for more context.
3333
| <a id="copy_directory-name"></a>name | Name of the rule. | none |
3434
| <a id="copy_directory-src"></a>src | The directory to make a copy of. Can be a source directory or TreeArtifact. | none |
3535
| <a id="copy_directory-out"></a>out | Path of the output directory, relative to this package. | none |
36+
| <a id="copy_directory-progress_message"></a>progress_message | A custom action progress message. | <code>"Copying directory %{input}"</code> |
37+
| <a id="copy_directory-mnemonic"></a>mnemonic | A custom action mnemonic. | <code>"CopyDirectory"</code> |
3638
| <a id="copy_directory-kwargs"></a>kwargs | further keyword arguments, e.g. <code>visibility</code> | none |
3739

3840

docs/copy_file_doc.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ on Windows (no Bash is required).
1414
## copy_file
1515

1616
<pre>
17-
copy_file(<a href="#copy_file-name">name</a>, <a href="#copy_file-src">src</a>, <a href="#copy_file-out">out</a>, <a href="#copy_file-is_executable">is_executable</a>, <a href="#copy_file-allow_symlink">allow_symlink</a>, <a href="#copy_file-kwargs">kwargs</a>)
17+
copy_file(<a href="#copy_file-name">name</a>, <a href="#copy_file-src">src</a>, <a href="#copy_file-out">out</a>, <a href="#copy_file-is_executable">is_executable</a>, <a href="#copy_file-allow_symlink">allow_symlink</a>, <a href="#copy_file-progress_message">progress_message</a>, <a href="#copy_file-mnemonic">mnemonic</a>, <a href="#copy_file-kwargs">kwargs</a>)
1818
</pre>
1919

2020
Copies a file to another location.
@@ -34,6 +34,8 @@ This rule uses a Bash command on Linux/macOS/non-Windows, and a cmd.exe command
3434
| <a id="copy_file-out"></a>out | Path of the output file, relative to this package. | none |
3535
| <a id="copy_file-is_executable"></a>is_executable | A boolean. Whether to make the output file executable. When True, the rule's output can be executed using <code>bazel run</code> and can be in the srcs of binary and test rules that require executable sources. WARNING: If <code>allow_symlink</code> is True, <code>src</code> must also be executable. | <code>False</code> |
3636
| <a id="copy_file-allow_symlink"></a>allow_symlink | A boolean. Whether to allow symlinking instead of copying. When False, the output is always a hard copy. When True, the output *can* be a symlink, but there is no guarantee that a symlink is created (i.e., at the time of writing, we don't create symlinks on Windows). Set this to True if you need fast copying and your tools can handle symlinks (which most UNIX tools can). | <code>False</code> |
37+
| <a id="copy_file-progress_message"></a>progress_message | A custom action progress message. | <code>"Copying files"</code> |
38+
| <a id="copy_file-mnemonic"></a>mnemonic | A custom action mnemonic. | <code>"CopyFile"</code> |
3739
| <a id="copy_file-kwargs"></a>kwargs | further keyword arguments, e.g. <code>visibility</code> | none |
3840

3941

docs/run_binary_doc.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Runs a binary as a build action. This rule does not require Bash (unlike native.
1111
## run_binary
1212

1313
<pre>
14-
run_binary(<a href="#run_binary-name">name</a>, <a href="#run_binary-args">args</a>, <a href="#run_binary-env">env</a>, <a href="#run_binary-outs">outs</a>, <a href="#run_binary-srcs">srcs</a>, <a href="#run_binary-tool">tool</a>)
14+
run_binary(<a href="#run_binary-name">name</a>, <a href="#run_binary-args">args</a>, <a href="#run_binary-env">env</a>, <a href="#run_binary-mnemonic">mnemonic</a>, <a href="#run_binary-outs">outs</a>, <a href="#run_binary-progress_message">progress_message</a>, <a href="#run_binary-srcs">srcs</a>, <a href="#run_binary-tool">tool</a>)
1515
</pre>
1616

1717
Runs a binary as a build action.
@@ -26,7 +26,9 @@ This rule does not require Bash (unlike `native.genrule`).
2626
| <a id="run_binary-name"></a>name | A unique name for this target. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | |
2727
| <a id="run_binary-args"></a>args | Command line arguments of the binary.<br><br>Subject to [<code>$(location)</code>](https://bazel.build/reference/be/make-variables#predefined_label_variables) expansion. | List of strings | optional | <code>[]</code> |
2828
| <a id="run_binary-env"></a>env | Environment variables of the action.<br><br>Subject to [<code>$(location)</code>](https://bazel.build/reference/be/make-variables#predefined_label_variables) expansion. | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional | <code>{}</code> |
29+
| <a id="run_binary-mnemonic"></a>mnemonic | Custom action mnemonic. | String | optional | <code>"RunBinary"</code> |
2930
| <a id="run_binary-outs"></a>outs | Output files generated by the action.<br><br>These labels are available for <code>$(location)</code> expansion in <code>args</code> and <code>env</code>. | List of labels | required | |
31+
| <a id="run_binary-progress_message"></a>progress_message | Custom action progress message. | String | optional | <code>"Running binary"</code> |
3032
| <a id="run_binary-srcs"></a>srcs | Additional inputs of the action.<br><br>These labels are available for <code>$(location)</code> expansion in <code>args</code> and <code>env</code>. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | <code>[]</code> |
3133
| <a id="run_binary-tool"></a>tool | The tool to run in the action.<br><br>Must be the label of a *_binary rule, of a rule that generates an executable file, or of a file that can be executed as a subprocess (e.g. an .exe or .bat file on Windows or a binary with executable permission on Linux). This label is available for <code>$(location)</code> expansion in <code>args</code> and <code>env</code>. | <a href="https://bazel.build/concepts/labels">Label</a> | required | |
3234

rules/private/copy_directory_private.bzl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,6 @@ if not exist \"{src}\\\" (
4040
)
4141
@robocopy \"{src}\" \"{dst}\" /E /MIR >NUL & @exit 0
4242
"""
43-
mnemonic = "CopyDirectory"
44-
progress_message = "Copying directory %{input}"
45-
4643
ctx.actions.write(
4744
output = bat,
4845
# Do not use lib/shell.bzl's shell.quote() method, because that uses
@@ -58,8 +55,8 @@ if not exist \"{src}\\\" (
5855
outputs = [dst],
5956
executable = "cmd.exe",
6057
arguments = ["/C", bat.path.replace("/", "\\")],
61-
mnemonic = mnemonic,
62-
progress_message = progress_message,
58+
mnemonic = ctx.attr.mnemonic,
59+
progress_message = ctx.attr.progress_message,
6360
use_default_shell_env = True,
6461
execution_requirements = COPY_EXECUTION_REQUIREMENTS,
6562
)
@@ -73,16 +70,13 @@ fi
7370
7471
rm -rf \"$2\" && cp -fR \"$1/\" \"$2\"
7572
"""
76-
mnemonic = "CopyDirectory"
77-
progress_message = "Copying directory %s" % src.path
78-
7973
ctx.actions.run_shell(
8074
inputs = [src],
8175
outputs = [dst],
8276
command = cmd,
8377
arguments = [src.path, dst.path],
84-
mnemonic = mnemonic,
85-
progress_message = progress_message,
78+
mnemonic = ctx.attr.mnemonic,
79+
progress_message = ctx.attr.progress_message,
8680
use_default_shell_env = True,
8781
execution_requirements = COPY_EXECUTION_REQUIREMENTS,
8882
)
@@ -124,10 +118,12 @@ _copy_directory = rule(
124118
# Cannot declare out as an output here, because there's no API for declaring
125119
# TreeArtifact outputs.
126120
"out": attr.string(mandatory = True),
121+
"mnemonic": attr.string(),
122+
"progress_message": attr.string(),
127123
},
128124
)
129125

130-
def copy_directory(name, src, out, **kwargs):
126+
def copy_directory(name, src, out, progress_message = "Copying directory %{input}", mnemonic = "CopyDirectory", **kwargs):
131127
"""Copies a directory to another location.
132128
133129
This rule uses a Bash command on Linux/macOS/non-Windows, and a cmd.exe command on Windows (no Bash is required).
@@ -142,6 +138,8 @@ def copy_directory(name, src, out, **kwargs):
142138
name: Name of the rule.
143139
src: The directory to make a copy of. Can be a source directory or TreeArtifact.
144140
out: Path of the output directory, relative to this package.
141+
progress_message: A custom action progress message.
142+
mnemonic: A custom action mnemonic.
145143
**kwargs: further keyword arguments, e.g. `visibility`
146144
"""
147145
_copy_directory(
@@ -152,5 +150,7 @@ def copy_directory(name, src, out, **kwargs):
152150
"//conditions:default": False,
153151
}),
154152
out = out,
153+
mnemonic = mnemonic,
154+
progress_message = progress_message,
155155
**kwargs
156156
)

rules/private/copy_file_private.bzl

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ def copy_cmd(ctx, src, dst):
4343
outputs = [dst],
4444
executable = "cmd.exe",
4545
arguments = ["/C", bat.path.replace("/", "\\")],
46-
mnemonic = "CopyFile",
47-
progress_message = "Copying files",
46+
mnemonic = ctx.attr.mnemonic,
47+
progress_message = ctx.attr.progress_message,
4848
use_default_shell_env = True,
4949
execution_requirements = COPY_EXECUTION_REQUIREMENTS,
5050
)
@@ -55,8 +55,8 @@ def copy_bash(ctx, src, dst):
5555
outputs = [dst],
5656
command = "cp -f \"$1\" \"$2\"",
5757
arguments = [src.path, dst.path],
58-
mnemonic = "CopyFile",
59-
progress_message = "Copying files",
58+
mnemonic = ctx.attr.mnemonic,
59+
progress_message = ctx.attr.progress_message,
6060
use_default_shell_env = True,
6161
execution_requirements = COPY_EXECUTION_REQUIREMENTS,
6262
)
@@ -89,6 +89,8 @@ _ATTRS = {
8989
"is_windows": attr.bool(mandatory = True),
9090
"is_executable": attr.bool(mandatory = True),
9191
"allow_symlink": attr.bool(mandatory = True),
92+
"mnemonic": attr.string(),
93+
"progress_message": attr.string(),
9294
}
9395

9496
_copy_file = rule(
@@ -104,7 +106,7 @@ _copy_xfile = rule(
104106
attrs = _ATTRS,
105107
)
106108

107-
def copy_file(name, src, out, is_executable = False, allow_symlink = False, **kwargs):
109+
def copy_file(name, src, out, is_executable = False, allow_symlink = False, progress_message = "Copying files", mnemonic = "CopyFile", **kwargs):
108110
"""Copies a file to another location.
109111
110112
`native.genrule()` is sometimes used to copy files (often wishing to rename them). The 'copy_file' rule does this with a simpler interface than genrule.
@@ -126,6 +128,8 @@ def copy_file(name, src, out, is_executable = False, allow_symlink = False, **kw
126128
created (i.e., at the time of writing, we don't create symlinks on
127129
Windows). Set this to True if you need fast copying and your tools can
128130
handle symlinks (which most UNIX tools can).
131+
progress_message: A custom action progress message.
132+
mnemonic: A custom action mnemonic.
129133
**kwargs: further keyword arguments, e.g. `visibility`
130134
"""
131135

@@ -143,5 +147,7 @@ def copy_file(name, src, out, is_executable = False, allow_symlink = False, **kw
143147
}),
144148
is_executable = is_executable,
145149
allow_symlink = allow_symlink,
150+
mnemonic = mnemonic,
151+
progress_message = progress_message,
146152
**kwargs
147153
)

rules/run_binary.bzl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ def _impl(ctx):
4747
tools = [ctx.executable.tool],
4848
executable = ctx.executable.tool,
4949
arguments = args,
50-
mnemonic = "RunBinary",
50+
mnemonic = ctx.attr.mnemonic,
51+
progress_message = ctx.attr.progress_message,
5152
use_default_shell_env = False,
5253
env = dicts.add(ctx.configuration.default_shell_env, envs),
5354
)
@@ -92,5 +93,13 @@ run_binary = rule(
9293
" [`$(location)`](https://bazel.build/reference/be/make-variables#predefined_label_variables)" +
9394
" expansion.",
9495
),
96+
"mnemonic": attr.string(
97+
default = "RunBinary",
98+
doc = "Custom action mnemonic.",
99+
),
100+
"progress_message": attr.string(
101+
default = "Running binary",
102+
doc = "Custom action progress message.",
103+
),
95104
},
96105
)

0 commit comments

Comments
 (0)