Skip to content

Commit 41eb75c

Browse files
ngiloq6laurentlb
authored andcommitted
copy_file: add rule and tests (#123)
This PR adds two new rules: copy_file and copy_xfile. Both rules solve a common problem: to copy one file to another location. The problem is routinely solved using a genrule. That however requires Bash, since genrules execute Bash commands. Requiring Bash is a problem on Windows. The new rules do not require Bash on Windows (only on other platforms). The only difference between the rules is that copy_xfile creates an executable file while copy_file doesn't. See bazelbuild/bazel#4319
1 parent 492fa32 commit 41eb75c

File tree

8 files changed

+367
-0
lines changed

8 files changed

+367
-0
lines changed

docs/BUILD

+7
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,13 @@ stardoc(
9292
deps = ["//lib:versions"],
9393
)
9494

95+
stardoc(
96+
name = "copy_file_docs",
97+
out = "copy_file_doc_gen.md",
98+
input = "//rules:copy_file.bzl",
99+
deps = ["//rules:copy_file"],
100+
)
101+
95102
stardoc(
96103
name = "maprule_docs",
97104
out = "maprule_doc_gen.md",

rules/BUILD

+12
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@ bzl_library(
1010
deps = ["//lib:new_sets"],
1111
)
1212

13+
bzl_library(
14+
name = "copy_file",
15+
srcs = ["copy_file.bzl"],
16+
deps = [":copy_file_private"],
17+
)
18+
19+
bzl_library(
20+
name = "copy_file_private",
21+
srcs = ["copy_file_private.bzl"],
22+
visibility = ["//visibility:private"],
23+
)
24+
1325
bzl_library(
1426
name = "maprule",
1527
srcs = ["maprule.bzl"],

rules/copy_file.bzl

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright 2019 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""A rule that copies a file to another place.
16+
17+
native.genrule() is sometimes used to copy files (often wishing to rename them).
18+
The 'copy_file' rule does this with a simpler interface than genrule.
19+
20+
The rule uses a Bash command on Linux/macOS/non-Windows, and a cmd.exe command
21+
on Windows (no Bash is required).
22+
"""
23+
24+
load(
25+
":copy_file_private.bzl",
26+
_copy_file = "copy_file",
27+
)
28+
29+
copy_file = _copy_file

rules/copy_file_private.bzl

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# Copyright 2019 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Implementation of copy_file macro and underlying rules.
16+
17+
These rules copy a file to another location using Bash (on Linux/macOS) or
18+
cmd.exe (on Windows). '_copy_xfile' marks the resulting file executable,
19+
'_copy_file' does not.
20+
"""
21+
22+
def _common_impl(ctx, is_executable):
23+
if ctx.attr.is_windows:
24+
# Most Windows binaries built with MSVC use a certain argument quoting
25+
# scheme. Bazel uses that scheme too to quote arguments. However,
26+
# cmd.exe uses different semantics, so Bazel's quoting is wrong here.
27+
# To fix that we write the command to a .bat file so no command line
28+
# quoting or escaping is required.
29+
bat = ctx.actions.declare_file(ctx.label.name + "-cmd.bat")
30+
ctx.actions.write(
31+
output = bat,
32+
# Do not use lib/shell.bzl's shell.quote() method, because that uses
33+
# Bash quoting syntax, which is different from cmd.exe's syntax.
34+
content = "@copy /Y \"%s\" \"%s\" >NUL" % (
35+
ctx.file.src.path.replace("/", "\\"),
36+
ctx.outputs.out.path.replace("/", "\\"),
37+
),
38+
is_executable = True,
39+
)
40+
ctx.actions.run(
41+
inputs = [ctx.file.src, bat],
42+
outputs = [ctx.outputs.out],
43+
executable = "cmd.exe",
44+
arguments = ["/C", bat.path.replace("/", "\\")],
45+
mnemonic = "CopyFile",
46+
progress_message = "Copying files",
47+
use_default_shell_env = True,
48+
)
49+
else:
50+
ctx.actions.run_shell(
51+
inputs = [ctx.file.src],
52+
outputs = [ctx.outputs.out],
53+
command = "cp -f \"$1\" \"$2\"",
54+
arguments = [ctx.file.src.path, ctx.outputs.out.path],
55+
mnemonic = "CopyFile",
56+
progress_message = "Copying files",
57+
use_default_shell_env = True,
58+
)
59+
60+
files = depset(direct = [ctx.outputs.out])
61+
runfiles = ctx.runfiles(files = [ctx.outputs.out])
62+
if is_executable:
63+
return [DefaultInfo(files = files, runfiles = runfiles, executable = ctx.outputs.out)]
64+
else:
65+
return [DefaultInfo(files = files, runfiles = runfiles)]
66+
67+
def _impl(ctx):
68+
return _common_impl(ctx, False)
69+
70+
def _ximpl(ctx):
71+
return _common_impl(ctx, True)
72+
73+
_ATTRS = {
74+
"src": attr.label(mandatory = True, allow_single_file = True),
75+
"out": attr.output(mandatory = True),
76+
"is_windows": attr.bool(mandatory = True),
77+
}
78+
79+
_copy_file = rule(
80+
implementation = _impl,
81+
provides = [DefaultInfo],
82+
attrs = _ATTRS,
83+
)
84+
85+
_copy_xfile = rule(
86+
implementation = _ximpl,
87+
executable = True,
88+
provides = [DefaultInfo],
89+
attrs = _ATTRS,
90+
)
91+
92+
def copy_file(name, src, out, is_executable = False, **kwargs):
93+
"""Copies a file to another location.
94+
95+
`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.
96+
97+
This rule uses a Bash command on Linux/macOS/non-Windows, and a cmd.exe command on Windows (no Bash is required).
98+
99+
Args:
100+
name: Name of the rule.
101+
src: A Label. The file to make a copy of. (Can also be the label of a rule
102+
that generates a file.)
103+
out: Path of the output file, relative to this package.
104+
is_executable: A boolean. Whether to make the output file executable. When
105+
True, the rule's output can be executed using `bazel run` and can be
106+
in the srcs of binary and test rules that require executable sources.
107+
**kwargs: further keyword arguments, e.g. `visibility`
108+
"""
109+
if is_executable:
110+
_copy_xfile(
111+
name = name,
112+
src = src,
113+
out = out,
114+
is_windows = select({
115+
"@bazel_tools//src/conditions:host_windows": True,
116+
"//conditions:default": False,
117+
}),
118+
**kwargs
119+
)
120+
else:
121+
_copy_file(
122+
name = name,
123+
src = src,
124+
out = out,
125+
is_windows = select({
126+
"@bazel_tools//src/conditions:host_windows": True,
127+
"//conditions:default": False,
128+
}),
129+
**kwargs
130+
)

tests/BUILD

+5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ load(":versions_tests.bzl", "versions_test_suite")
1616

1717
licenses(["notice"])
1818

19+
exports_files(
20+
["unittest.bash"],
21+
visibility = ["//tests:__subpackages__"],
22+
)
23+
1924
build_test_test_suite()
2025

2126
collections_test_suite()

tests/copy_file/BUILD

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# This package aids testing the 'copy_file' rule.
2+
#
3+
# The package contains 4 copy_file rules:
4+
# - 'copy_src' and 'copy_gen' copy a source file and a generated file
5+
# respectively
6+
# - 'copy_xsrc' and 'copy_xgen' copy a source file and a generated file
7+
# respectively (both are shell scripts), and mark their output as executable
8+
#
9+
# The generated file is the output of the 'gen' genrule.
10+
#
11+
# The 'bin_src' and 'bin_gen' rules are sh_binary rules. They use the
12+
# 'copy_xsrc' and 'copy_xgen' rules respectively. The sh_binary rule requires
13+
# its source to be executable, so building these two rules successfully means
14+
# that 'copy_file' managed to make its output executable.
15+
#
16+
# The 'run_executables' genrule runs the 'bin_src' and 'bin_gen' binaries,
17+
# partly to ensure they can be run, and partly so we can observe their output
18+
# and assert the contents in the 'copy_file_tests' test.
19+
#
20+
# The 'file_deps' filegroup depends on 'copy_src'. The filegroup rule uses the
21+
# DefaultInfo.files field from its dependencies. When we data-depend on the
22+
# filegroup from 'copy_file_tests', we transitively data-depend on the
23+
# DefaultInfo.files of the 'copy_src' rule.
24+
#
25+
# The 'copy_file_tests' test is the actual integration test. It data-depends
26+
# on:
27+
# - the 'run_executables' rule, to get the outputs of 'bin_src' and 'bin_gen'
28+
# - the 'file_deps' rule, and by nature of using a filegroup, we get the files
29+
# from the DefaultInfo.files of the 'copy_file' rule, and thereby assert that
30+
# that field contains the output file of the rule
31+
# - the 'copy_nonempty_text' rule, and thereby on the DefaultInfo.runfiles field
32+
# of it, so we assert that that field contains the output file of the rule
33+
34+
load("//rules:copy_file.bzl", "copy_file")
35+
36+
package(default_testonly = 1)
37+
38+
sh_test(
39+
name = "copy_file_tests",
40+
srcs = ["copy_file_tests.sh"],
41+
data = [
42+
":run_executables",
43+
# Use DefaultInfo.files from 'copy_src' (via 'file_deps').
44+
":file_deps",
45+
# Use DefaultInfo.runfiles from 'copy_gen'.
46+
":copy_gen",
47+
"//tests:unittest.bash",
48+
],
49+
deps = ["@bazel_tools//tools/bash/runfiles"],
50+
)
51+
52+
filegroup(
53+
name = "file_deps",
54+
# Use DefaultInfo.files from 'copy_src'.
55+
srcs = [
56+
":copy_src",
57+
],
58+
)
59+
60+
# If 'run_executables' is built, then 'bin_gen' and 'bin_src' are
61+
# executable, asserting that copy_file makes the output executable.
62+
genrule(
63+
name = "run_executables",
64+
outs = [
65+
"xsrc-out.txt",
66+
"xgen-out.txt",
67+
],
68+
cmd = ("$(location :bin_src) > $(location xsrc-out.txt) && " +
69+
"$(location :bin_gen) > $(location xgen-out.txt)"),
70+
output_to_bindir = 1,
71+
tools = [
72+
":bin_gen",
73+
":bin_src",
74+
],
75+
)
76+
77+
# If 'bin_src' is built, then 'copy_xsrc' made its output executable.
78+
sh_binary(
79+
name = "bin_src",
80+
srcs = [":copy_xsrc"],
81+
)
82+
83+
# If 'bin_gen' is built, then 'copy_xgen' made its output executable.
84+
sh_binary(
85+
name = "bin_gen",
86+
srcs = [":copy_xgen"],
87+
)
88+
89+
copy_file(
90+
name = "copy_src",
91+
src = "a.txt",
92+
out = "out/a-out.txt",
93+
)
94+
95+
copy_file(
96+
name = "copy_gen",
97+
src = ":gen",
98+
out = "out/gen-out.txt",
99+
)
100+
101+
copy_file(
102+
name = "copy_xsrc",
103+
src = "a.txt",
104+
out = "xout/a-out.sh",
105+
is_executable = True,
106+
)
107+
108+
copy_file(
109+
name = "copy_xgen",
110+
src = ":gen",
111+
out = "xout/gen-out.sh",
112+
is_executable = True,
113+
)
114+
115+
genrule(
116+
name = "gen",
117+
outs = ["b.txt"],
118+
cmd = "echo -e '#!/bin/bash\necho potato' > $@",
119+
)

tests/copy_file/a.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
echo aaa

tests/copy_file/copy_file_tests.sh

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Copyright 2019 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# --- begin runfiles.bash initialization ---
16+
# Copy-pasted from Bazel's Bash runfiles library (tools/bash/runfiles/runfiles.bash).
17+
set -euo pipefail
18+
if [[ ! -d "${RUNFILES_DIR:-/dev/null}" && ! -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
19+
if [[ -f "$0.runfiles_manifest" ]]; then
20+
export RUNFILES_MANIFEST_FILE="$0.runfiles_manifest"
21+
elif [[ -f "$0.runfiles/MANIFEST" ]]; then
22+
export RUNFILES_MANIFEST_FILE="$0.runfiles/MANIFEST"
23+
elif [[ -f "$0.runfiles/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
24+
export RUNFILES_DIR="$0.runfiles"
25+
fi
26+
fi
27+
if [[ -f "${RUNFILES_DIR:-/dev/null}/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
28+
source "${RUNFILES_DIR}/bazel_tools/tools/bash/runfiles/runfiles.bash"
29+
elif [[ -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
30+
source "$(grep -m1 "^bazel_tools/tools/bash/runfiles/runfiles.bash " \
31+
"$RUNFILES_MANIFEST_FILE" | cut -d ' ' -f 2-)"
32+
else
33+
echo >&2 "ERROR: cannot find @bazel_tools//tools/bash/runfiles:runfiles.bash"
34+
exit 1
35+
fi
36+
# --- end runfiles.bash initialization ---
37+
38+
source "$(rlocation bazel_skylib/tests/unittest.bash)" \
39+
|| { echo "Could not source bazel_skylib/tests/unittest.bash" >&2; exit 1; }
40+
41+
function test_copy_src() {
42+
cat "$(rlocation bazel_skylib/tests/copy_file/out/a-out.txt)" >"$TEST_log"
43+
expect_log '^#!/bin/bash$'
44+
expect_log '^echo aaa$'
45+
}
46+
47+
function test_copy_gen() {
48+
cat "$(rlocation bazel_skylib/tests/copy_file/out/gen-out.txt)" >"$TEST_log"
49+
expect_log '^#!/bin/bash$'
50+
expect_log '^echo potato$'
51+
}
52+
53+
function test_copy_xsrc() {
54+
cat "$(rlocation bazel_skylib/tests/copy_file/xsrc-out.txt)" >"$TEST_log"
55+
expect_log '^aaa$'
56+
}
57+
58+
function test_copy_xgen() {
59+
cat "$(rlocation bazel_skylib/tests/copy_file/xgen-out.txt)" >"$TEST_log"
60+
expect_log '^potato$'
61+
}
62+
63+
run_suite "copy_file_tests test suite"

0 commit comments

Comments
 (0)