Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom mnemonic and progress_message #491

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/copy_directory_doc.md
Expand Up @@ -11,7 +11,7 @@ on Windows (no Bash is required).
## copy_directory

<pre>
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>)
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-mnemonic">mnemonic</a>, <a href="#copy_directory-progress_message">progress_message</a>, <a href="#copy_directory-kwargs">kwargs</a>)
</pre>

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


Expand Down
4 changes: 3 additions & 1 deletion docs/copy_file_doc.md
Expand Up @@ -14,7 +14,7 @@ on Windows (no Bash is required).
## copy_file

<pre>
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>)
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-mnemonic">mnemonic</a>, <a href="#copy_file-progress_message">progress_message</a>, <a href="#copy_file-kwargs">kwargs</a>)
</pre>

Copies a file to another location.
Expand All @@ -34,6 +34,8 @@ This rule uses a Bash command on Linux/macOS/non-Windows, and a cmd.exe command
| <a id="copy_file-out"></a>out | Path of the output file, relative to this package. | none |
| <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> |
| <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> |
| <a id="copy_file-mnemonic"></a>mnemonic | A custom action mnemonic. | <code>"CopyFile"</code> |
| <a id="copy_file-progress_message"></a>progress_message | A custom action progress message. | <code>"Copying files"</code> |
| <a id="copy_file-kwargs"></a>kwargs | further keyword arguments, e.g. <code>visibility</code> | none |


4 changes: 3 additions & 1 deletion docs/run_binary_doc.md
Expand Up @@ -11,7 +11,7 @@ Runs a binary as a build action. This rule does not require Bash (unlike native.
## run_binary

<pre>
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>)
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>)
</pre>

Runs a binary as a build action.
Expand All @@ -26,7 +26,9 @@ This rule does not require Bash (unlike `native.genrule`).
| <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 | |
| <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> |
| <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> |
| <a id="run_binary-mnemonic"></a>mnemonic | Custom action mnemonic. | String | optional | <code>"RunBinary"</code> |
| <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 | |
| <a id="run_binary-progress_message"></a>progress_message | Custom action progress message. | String | optional | <code>"Running binary"</code> |
| <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> |
| <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 | |

Expand Down
22 changes: 11 additions & 11 deletions rules/private/copy_directory_private.bzl
Expand Up @@ -40,9 +40,6 @@ if not exist \"{src}\\\" (
)
@robocopy \"{src}\" \"{dst}\" /E /MIR >NUL & @exit 0
"""
mnemonic = "CopyDirectory"
progress_message = "Copying directory %{input}"

ctx.actions.write(
output = bat,
# Do not use lib/shell.bzl's shell.quote() method, because that uses
Expand All @@ -58,8 +55,8 @@ if not exist \"{src}\\\" (
outputs = [dst],
executable = "cmd.exe",
arguments = ["/C", bat.path.replace("/", "\\")],
mnemonic = mnemonic,
progress_message = progress_message,
mnemonic = ctx.attr.mnemonic,
progress_message = ctx.attr.progress_message,
use_default_shell_env = True,
execution_requirements = COPY_EXECUTION_REQUIREMENTS,
)
Expand All @@ -73,16 +70,13 @@ fi

rm -rf \"$2\" && cp -fR \"$1/\" \"$2\"
"""
mnemonic = "CopyDirectory"
progress_message = "Copying directory %s" % src.path

ctx.actions.run_shell(
inputs = [src],
outputs = [dst],
command = cmd,
arguments = [src.path, dst.path],
mnemonic = mnemonic,
progress_message = progress_message,
mnemonic = ctx.attr.mnemonic,
progress_message = ctx.attr.progress_message,
use_default_shell_env = True,
execution_requirements = COPY_EXECUTION_REQUIREMENTS,
)
Expand Down Expand Up @@ -124,10 +118,12 @@ _copy_directory = rule(
# Cannot declare out as an output here, because there's no API for declaring
# TreeArtifact outputs.
"out": attr.string(mandatory = True),
"mnemonic": attr.string(),
"progress_message": attr.string(),
},
)

def copy_directory(name, src, out, **kwargs):
def copy_directory(name, src, out, mnemonic = "CopyDirectory", progress_message = "Copying directory %{input}", **kwargs):
"""Copies a directory to another location.

This rule uses a Bash command on Linux/macOS/non-Windows, and a cmd.exe command on Windows (no Bash is required).
Expand All @@ -142,6 +138,8 @@ def copy_directory(name, src, out, **kwargs):
name: Name of the rule.
src: The directory to make a copy of. Can be a source directory or TreeArtifact.
out: Path of the output directory, relative to this package.
progress_message: A custom action progress message.
mnemonic: A custom action mnemonic.
**kwargs: further keyword arguments, e.g. `visibility`
"""
_copy_directory(
Expand All @@ -152,5 +150,7 @@ def copy_directory(name, src, out, **kwargs):
"//conditions:default": False,
}),
out = out,
mnemonic = mnemonic,
progress_message = progress_message,
**kwargs
)
16 changes: 11 additions & 5 deletions rules/private/copy_file_private.bzl
Expand Up @@ -43,8 +43,8 @@ def copy_cmd(ctx, src, dst):
outputs = [dst],
executable = "cmd.exe",
arguments = ["/C", bat.path.replace("/", "\\")],
mnemonic = "CopyFile",
progress_message = "Copying files",
mnemonic = ctx.attr.mnemonic,
progress_message = ctx.attr.progress_message,
use_default_shell_env = True,
execution_requirements = COPY_EXECUTION_REQUIREMENTS,
)
Expand All @@ -55,8 +55,8 @@ def copy_bash(ctx, src, dst):
outputs = [dst],
command = "cp -f \"$1\" \"$2\"",
arguments = [src.path, dst.path],
mnemonic = "CopyFile",
progress_message = "Copying files",
mnemonic = ctx.attr.mnemonic,
progress_message = ctx.attr.progress_message,
use_default_shell_env = True,
execution_requirements = COPY_EXECUTION_REQUIREMENTS,
)
Expand Down Expand Up @@ -89,6 +89,8 @@ _ATTRS = {
"is_windows": attr.bool(mandatory = True),
"is_executable": attr.bool(mandatory = True),
"allow_symlink": attr.bool(mandatory = True),
"mnemonic": attr.string(),
"progress_message": attr.string(),
}

_copy_file = rule(
Expand All @@ -104,7 +106,7 @@ _copy_xfile = rule(
attrs = _ATTRS,
)

def copy_file(name, src, out, is_executable = False, allow_symlink = False, **kwargs):
def copy_file(name, src, out, is_executable = False, allow_symlink = False, mnemonic = "CopyFile", progress_message = "Copying files", **kwargs):
"""Copies a file to another location.

`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.
Expand All @@ -126,6 +128,8 @@ def copy_file(name, src, out, is_executable = False, allow_symlink = False, **kw
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).
progress_message: A custom action progress message.
mnemonic: A custom action mnemonic.
**kwargs: further keyword arguments, e.g. `visibility`
"""

Expand All @@ -143,5 +147,7 @@ def copy_file(name, src, out, is_executable = False, allow_symlink = False, **kw
}),
is_executable = is_executable,
allow_symlink = allow_symlink,
mnemonic = mnemonic,
progress_message = progress_message,
**kwargs
)
11 changes: 10 additions & 1 deletion rules/run_binary.bzl
Expand Up @@ -47,7 +47,8 @@ def _impl(ctx):
tools = [ctx.executable.tool],
executable = ctx.executable.tool,
arguments = args,
mnemonic = "RunBinary",
mnemonic = ctx.attr.mnemonic,
progress_message = ctx.attr.progress_message,
use_default_shell_env = False,
env = dicts.add(ctx.configuration.default_shell_env, envs),
)
Expand Down Expand Up @@ -92,5 +93,13 @@ run_binary = rule(
" [`$(location)`](https://bazel.build/reference/be/make-variables#predefined_label_variables)" +
" expansion.",
),
"mnemonic": attr.string(
default = "RunBinary",
doc = "Custom action mnemonic.",
),
"progress_message": attr.string(
default = "Running binary",
doc = "Custom action progress message.",
),
},
)
14 changes: 14 additions & 0 deletions tests/copy_directory/BUILD.bazel
@@ -1,6 +1,7 @@
# This package aids testing the 'copy_directory' rule.

load("//rules:copy_directory.bzl", "copy_directory")
load("//tests/copy_directory:mnemonic_test.bzl", "copy_directory_mnemonic_test")
load(":empty_directory.bzl", "empty_directory")

licenses(["notice"])
Expand Down Expand Up @@ -41,3 +42,16 @@ sh_test(
],
deps = ["@bazel_tools//tools/bash/runfiles"],
)

copy_directory(
name = "copy_directory_mnemonic_test_target",
src = "dir_with_subdir",
out = "copied_dir_with_subdir",
mnemonic = "FooBar",
tags = ["manual"],
)

copy_directory_mnemonic_test(
name = "copy_directory_mnemonic_test",
target_under_test = ":copy_directory_mnemonic_test_target",
)
31 changes: 31 additions & 0 deletions tests/copy_directory/mnemonic_test.bzl
@@ -0,0 +1,31 @@
# Copyright 2019 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Custom mnemonic tests for copy_directory.bzl"""

load("//lib:unittest.bzl", "analysistest", "asserts")

def _copy_directory_mnemonic_test_impl(ctx):
env = analysistest.begin(ctx)

expected_mmemonic = "FooBar"

mnemonics = [target_action.mnemonic for target_action in analysistest.target_actions(env)]
contains_expected_mnemonic = expected_mmemonic in mnemonics

asserts.true(env, contains_expected_mnemonic)

return analysistest.end(env)

copy_directory_mnemonic_test = analysistest.make(_copy_directory_mnemonic_test_impl)
14 changes: 14 additions & 0 deletions tests/copy_file/BUILD
Expand Up @@ -32,6 +32,7 @@
# of it, so we assert that that field contains the output file of the rule

load("//rules:copy_file.bzl", "copy_file")
load("//tests/copy_file:mnemonic_test.bzl", "copy_file_mnemonic_test")

licenses(["notice"])

Expand Down Expand Up @@ -171,3 +172,16 @@ genrule(
outs = ["b.txt"],
cmd = "echo -e '#!/usr/bin/env bash\necho potato' > $@",
)

copy_file(
name = "copy_file_mnemonic_test_target",
src = "foo.txt",
out = "bar.txt",
mnemonic = "FooBar",
tags = ["manual"],
)

copy_file_mnemonic_test(
name = "copy_file_mnemonic_test",
target_under_test = ":copy_file_mnemonic_test_target",
)
31 changes: 31 additions & 0 deletions tests/copy_file/mnemonic_test.bzl
@@ -0,0 +1,31 @@
# Copyright 2019 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Custom mnemonic tests for copy_file.bzl"""

load("//lib:unittest.bzl", "analysistest", "asserts")

def _copy_file_mnemonic_test_impl(ctx):
env = analysistest.begin(ctx)

expected_mmemonic = "FooBar"

mnemonics = [target_action.mnemonic for target_action in analysistest.target_actions(env)]
contains_expected_mnemonic = expected_mmemonic in mnemonics

asserts.true(env, contains_expected_mnemonic)

return analysistest.end(env)

copy_file_mnemonic_test = analysistest.make(_copy_file_mnemonic_test_impl)
19 changes: 19 additions & 0 deletions tests/run_binary/BUILD
Expand Up @@ -2,6 +2,7 @@ load("@rules_cc//cc:defs.bzl", "cc_binary")
load("//rules:diff_test.bzl", "diff_test")
load("//rules:run_binary.bzl", "run_binary")
load("//rules:write_file.bzl", "write_file")
load("//tests/run_binary:mnemonic_test.bzl", "run_binary_mnemonic_test")

package(
default_testonly = 1,
Expand Down Expand Up @@ -165,3 +166,21 @@ cc_binary(
name = "printargs",
srcs = ["printargs.cc"],
)

cc_binary(
name = "hello",
srcs = ["hello.cc"],
)

run_binary(
name = "run_binary_test_target",
outs = ["hello.out"],
mnemonic = "FooBar",
tags = ["manual"],
tool = ":hello",
)

run_binary_mnemonic_test(
name = "run_binary_mnemonic_test",
target_under_test = ":run_binary_test_target",
)
20 changes: 20 additions & 0 deletions tests/run_binary/hello.cc
@@ -0,0 +1,20 @@
// Copyright 2019 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <stdio.h>

int main() {
printf("Hello, world!");
return 0;
}