diff --git a/docs/copy_directory_doc.md b/docs/copy_directory_doc.md index 1177d20e..8c678e0e 100755 --- a/docs/copy_directory_doc.md +++ b/docs/copy_directory_doc.md @@ -11,7 +11,7 @@ on Windows (no Bash is required). ## copy_directory
-copy_directory(name, src, out, kwargs)
+copy_directory(name, src, out, mnemonic, progress_message, kwargs)
 
Copies a directory to another location. @@ -33,6 +33,8 @@ for more context. | name | Name of the rule. | none | | src | The directory to make a copy of. Can be a source directory or TreeArtifact. | none | | out | Path of the output directory, relative to this package. | none | +| mnemonic | A custom action mnemonic. | "CopyDirectory" | +| progress_message | A custom action progress message. | "Copying directory %{input}" | | kwargs | further keyword arguments, e.g. visibility | none | diff --git a/docs/copy_file_doc.md b/docs/copy_file_doc.md index ed297a66..1156383b 100755 --- a/docs/copy_file_doc.md +++ b/docs/copy_file_doc.md @@ -14,7 +14,7 @@ on Windows (no Bash is required). ## copy_file
-copy_file(name, src, out, is_executable, allow_symlink, kwargs)
+copy_file(name, src, out, is_executable, allow_symlink, mnemonic, progress_message, kwargs)
 
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 | out | Path of the output file, relative to this package. | none | | is_executable | A boolean. Whether to make the output file executable. When True, the rule's output can be executed using bazel run and can be in the srcs of binary and test rules that require executable sources. WARNING: If allow_symlink is True, src must also be executable. | False | | 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). | False | +| mnemonic | A custom action mnemonic. | "CopyFile" | +| progress_message | A custom action progress message. | "Copying files" | | kwargs | further keyword arguments, e.g. visibility | none | diff --git a/docs/run_binary_doc.md b/docs/run_binary_doc.md index 50b62e40..10371636 100755 --- a/docs/run_binary_doc.md +++ b/docs/run_binary_doc.md @@ -11,7 +11,7 @@ Runs a binary as a build action. This rule does not require Bash (unlike native. ## run_binary
-run_binary(name, args, env, outs, srcs, tool)
+run_binary(name, args, env, mnemonic, outs, progress_message, srcs, tool)
 
Runs a binary as a build action. @@ -26,7 +26,9 @@ This rule does not require Bash (unlike `native.genrule`). | name | A unique name for this target. | Name | required | | | args | Command line arguments of the binary.

Subject to [$(location)](https://bazel.build/reference/be/make-variables#predefined_label_variables) expansion. | List of strings | optional | [] | | env | Environment variables of the action.

Subject to [$(location)](https://bazel.build/reference/be/make-variables#predefined_label_variables) expansion. | Dictionary: String -> String | optional | {} | +| mnemonic | Custom action mnemonic. | String | optional | "RunBinary" | | outs | Output files generated by the action.

These labels are available for $(location) expansion in args and env. | List of labels | required | | +| progress_message | Custom action progress message. | String | optional | "Running binary" | | srcs | Additional inputs of the action.

These labels are available for $(location) expansion in args and env. | List of labels | optional | [] | | tool | The tool to run in the action.

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 $(location) expansion in args and env. | Label | required | | diff --git a/rules/private/copy_directory_private.bzl b/rules/private/copy_directory_private.bzl index 650e17eb..e56b1b11 100644 --- a/rules/private/copy_directory_private.bzl +++ b/rules/private/copy_directory_private.bzl @@ -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 @@ -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, ) @@ -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, ) @@ -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). @@ -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( @@ -152,5 +150,7 @@ def copy_directory(name, src, out, **kwargs): "//conditions:default": False, }), out = out, + mnemonic = mnemonic, + progress_message = progress_message, **kwargs ) diff --git a/rules/private/copy_file_private.bzl b/rules/private/copy_file_private.bzl index 15e1d4b8..e5bccc74 100644 --- a/rules/private/copy_file_private.bzl +++ b/rules/private/copy_file_private.bzl @@ -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, ) @@ -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, ) @@ -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( @@ -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. @@ -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` """ @@ -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 ) diff --git a/rules/run_binary.bzl b/rules/run_binary.bzl index 7701fa0d..f0b37d5e 100644 --- a/rules/run_binary.bzl +++ b/rules/run_binary.bzl @@ -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), ) @@ -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.", + ), }, ) diff --git a/tests/copy_directory/BUILD.bazel b/tests/copy_directory/BUILD.bazel index f766e99f..b229eea3 100644 --- a/tests/copy_directory/BUILD.bazel +++ b/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"]) @@ -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", +) diff --git a/tests/copy_directory/mnemonic_test.bzl b/tests/copy_directory/mnemonic_test.bzl new file mode 100644 index 00000000..64d16e87 --- /dev/null +++ b/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) diff --git a/tests/copy_file/BUILD b/tests/copy_file/BUILD index 2e6914ce..768e7c7e 100644 --- a/tests/copy_file/BUILD +++ b/tests/copy_file/BUILD @@ -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"]) @@ -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", +) diff --git a/tests/copy_file/mnemonic_test.bzl b/tests/copy_file/mnemonic_test.bzl new file mode 100644 index 00000000..3b5433b6 --- /dev/null +++ b/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) diff --git a/tests/run_binary/BUILD b/tests/run_binary/BUILD index 259c84a3..8d3a398f 100644 --- a/tests/run_binary/BUILD +++ b/tests/run_binary/BUILD @@ -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, @@ -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", +) diff --git a/tests/run_binary/hello.cc b/tests/run_binary/hello.cc new file mode 100644 index 00000000..21dd492f --- /dev/null +++ b/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 + +int main() { + printf("Hello, world!"); + return 0; +} \ No newline at end of file diff --git a/tests/run_binary/mnemonic_test.bzl b/tests/run_binary/mnemonic_test.bzl new file mode 100644 index 00000000..d8fdd75e --- /dev/null +++ b/tests/run_binary/mnemonic_test.bzl @@ -0,0 +1,29 @@ +# 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 run_binary.bzl""" + +load("//lib:unittest.bzl", "analysistest", "asserts") + +def _run_binary_mnemonic_test_impl(ctx): + env = analysistest.begin(ctx) + + expected_mmemonic = "FooBar" + actual_mnemonic = analysistest.target_actions(env)[0].mnemonic + + asserts.equals(env, expected_mmemonic, actual_mnemonic) + + return analysistest.end(env) + +run_binary_mnemonic_test = analysistest.make(_run_binary_mnemonic_test_impl)