Skip to content

Commit

Permalink
copy_file: Add parameter to allow symlinks
Browse files Browse the repository at this point in the history
This change adds a new parameter `allow_symlinks` to `copy_file` that
allows the action to create a symlink instead of doing an expensive
copy if the execution platform (host) allows it.

Updates #248
  • Loading branch information
Yannic committed Jun 3, 2020
1 parent 560d7b2 commit 89daac6
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 6 deletions.
25 changes: 21 additions & 4 deletions rules/private/copy_file_private.bzl
Expand Up @@ -59,10 +59,17 @@ def copy_bash(ctx, src, dst):
)

def _common_impl(ctx, is_executable):
if ctx.attr.is_windows:
copy_cmd(ctx, ctx.file.src, ctx.outputs.out)
if ctx.attr.allow_symlink:
ctx.actions.symlink(
output = ctx.outputs.out,
target_file = ctx.file.src,
is_executable = is_executable,
)
else:
copy_bash(ctx, ctx.file.src, ctx.outputs.out)
if ctx.attr.is_windows:
copy_cmd(ctx, ctx.file.src, ctx.outputs.out)
else:
copy_bash(ctx, ctx.file.src, ctx.outputs.out)

files = depset(direct = [ctx.outputs.out])
runfiles = ctx.runfiles(files = [ctx.outputs.out])
Expand All @@ -81,6 +88,7 @@ _ATTRS = {
"src": attr.label(mandatory = True, allow_single_file = True),
"out": attr.output(mandatory = True),
"is_windows": attr.bool(mandatory = True),
"allow_symlink": attr.bool(mandatory = True),
}

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

def copy_file(name, src, out, is_executable = False, **kwargs):
def copy_file(name, src, out, is_executable = False, allow_symlink = False, **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 @@ -111,6 +119,13 @@ def copy_file(name, src, out, is_executable = False, **kwargs):
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.
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).
**kwargs: further keyword arguments, e.g. `visibility`
"""
if is_executable:
Expand All @@ -122,6 +137,7 @@ def copy_file(name, src, out, is_executable = False, **kwargs):
"@bazel_tools//src/conditions:host_windows": True,
"//conditions:default": False,
}),
allow_symlink = allow_symlink,
**kwargs
)
else:
Expand All @@ -133,5 +149,6 @@ def copy_file(name, src, out, is_executable = False, **kwargs):
"@bazel_tools//src/conditions:host_windows": True,
"//conditions:default": False,
}),
allow_symlink = allow_symlink,
**kwargs
)
56 changes: 54 additions & 2 deletions tests/copy_file/BUILD
Expand Up @@ -46,6 +46,7 @@ sh_test(
":file_deps",
# Use DefaultInfo.runfiles from 'copy_gen'.
":copy_gen",
":copy_gen_symlink",
"//tests:unittest.bash",
],
deps = ["@bazel_tools//tools/bash/runfiles"],
Expand All @@ -56,6 +57,7 @@ filegroup(
# Use DefaultInfo.files from 'copy_src'.
srcs = [
":copy_src",
":copy_src_symlink",
],
)

Expand All @@ -64,15 +66,23 @@ filegroup(
genrule(
name = "run_executables",
outs = [
"xsrc-out-symlink.txt",
"xgen-out-symlink.txt",
"xsrc-out.txt",
"xgen-out.txt",
],
cmd = ("$(location :bin_src) > $(location xsrc-out.txt) && " +
"$(location :bin_gen) > $(location xgen-out.txt)"),
cmd = " && ".join([
"$(location :bin_src_symlink) > $(location xsrc-out-symlink.txt)",
"$(location :bin_gen_symlink) > $(location xgen-out-symlink.txt)",
"$(location :bin_src) > $(location xsrc-out.txt)",
"$(location :bin_gen) > $(location xgen-out.txt)",
]),
output_to_bindir = 1,
tools = [
":bin_gen",
":bin_src",
":bin_gen_symlink",
":bin_src_symlink",
],
)

Expand All @@ -82,22 +92,48 @@ sh_binary(
srcs = [":copy_xsrc"],
)

# If 'bin_src' is built, then 'copy_xsrc' made its output executable.
sh_binary(
name = "bin_src_symlink",
srcs = [":copy_xsrc_symlink"],
)

# If 'bin_gen' is built, then 'copy_xgen' made its output executable.
sh_binary(
name = "bin_gen",
srcs = [":copy_xgen"],
)

# If 'bin_gen' is built, then 'copy_xgen' made its output executable.
sh_binary(
name = "bin_gen_symlink",
srcs = [":copy_xgen_symlink"],
)

copy_file(
name = "copy_src",
src = "a.txt",
out = "out/a-out.txt",
)

copy_file(
name = "copy_src_symlink",
src = "a.txt",
out = "out/a-out-symlink.txt",
allow_symlink = True,
)

copy_file(
name = "copy_gen",
src = ":gen",
out = "out/gen-out.txt",
allow_symlink = True,
)

copy_file(
name = "copy_gen_symlink",
src = ":gen",
out = "out/gen-out-symlink.txt",
)

copy_file(
Expand All @@ -107,13 +143,29 @@ copy_file(
is_executable = True,
)

copy_file(
name = "copy_xsrc_symlink",
src = "a_with_exec_bit.txt",
out = "xout/a-out-symlink.sh",
is_executable = True,
allow_symlink = True,
)

copy_file(
name = "copy_xgen",
src = ":gen",
out = "xout/gen-out.sh",
is_executable = True,
)

copy_file(
name = "copy_xgen_symlink",
src = ":gen",
out = "xout/gen-out-symlink.sh",
is_executable = True,
allow_symlink = True,
)

genrule(
name = "gen",
outs = ["b.txt"],
Expand Down
2 changes: 2 additions & 0 deletions tests/copy_file/a_with_exec_bit.txt
@@ -0,0 +1,2 @@
#!/bin/bash
echo aaa
22 changes: 22 additions & 0 deletions tests/copy_file/copy_file_tests.sh
Expand Up @@ -44,20 +44,42 @@ function test_copy_src() {
expect_log '^echo aaa$'
}

function test_copy_src_symlink() {
cat "$(rlocation bazel_skylib/tests/copy_file/out/a-out-symlink.txt)" >"$TEST_log"
expect_log '^#!/bin/bash$'
expect_log '^echo aaa$'
}

function test_copy_gen() {
cat "$(rlocation bazel_skylib/tests/copy_file/out/gen-out.txt)" >"$TEST_log"
expect_log '^#!/bin/bash$'
expect_log '^echo potato$'
}

function test_copy_gen_symlink() {
cat "$(rlocation bazel_skylib/tests/copy_file/out/gen-out-symlink.txt)" >"$TEST_log"
expect_log '^#!/bin/bash$'
expect_log '^echo potato$'
}

function test_copy_xsrc() {
cat "$(rlocation bazel_skylib/tests/copy_file/xsrc-out.txt)" >"$TEST_log"
expect_log '^aaa$'
}

function test_copy_xsrc_symlink() {
cat "$(rlocation bazel_skylib/tests/copy_file/xsrc-out-symlink.txt)" >"$TEST_log"
expect_log '^aaa$'
}

function test_copy_xgen() {
cat "$(rlocation bazel_skylib/tests/copy_file/xgen-out.txt)" >"$TEST_log"
expect_log '^potato$'
}

function test_copy_xgen_symlink() {
cat "$(rlocation bazel_skylib/tests/copy_file/xgen-out-symlink.txt)" >"$TEST_log"
expect_log '^potato$'
}

run_suite "copy_file_tests test suite"

0 comments on commit 89daac6

Please sign in to comment.