2022-04-04 00:52:03 +00:00
|
|
|
"""Implementation of copy_directory macro and underlying rules.
|
|
|
|
|
|
|
|
This rule copies a directory to another location using Bash (on Linux/macOS) or
|
|
|
|
cmd.exe (on Windows).
|
|
|
|
"""
|
|
|
|
|
2022-09-13 03:45:48 +00:00
|
|
|
load(":copy_common.bzl", _COPY_EXECUTION_REQUIREMENTS = "COPY_EXECUTION_REQUIREMENTS", _is_windows_host = "is_windows_host")
|
2022-04-04 00:52:03 +00:00
|
|
|
|
2022-04-12 10:34:51 +00:00
|
|
|
def _copy_cmd(ctx, src, dst):
|
2022-04-04 00:52:03 +00:00
|
|
|
# Most Windows binaries built with MSVC use a certain argument quoting
|
|
|
|
# scheme. Bazel uses that scheme too to quote arguments. However,
|
|
|
|
# cmd.exe uses different semantics, so Bazel's quoting is wrong here.
|
|
|
|
# To fix that we write the command to a .bat file so no command line
|
|
|
|
# quoting or escaping is required.
|
|
|
|
# Put a hash of the file name into the name of the generated batch file to
|
|
|
|
# make it unique within the package, so that users can define multiple copy_file's.
|
2022-04-12 10:34:51 +00:00
|
|
|
bat = ctx.actions.declare_file("%s-%s-cmd.bat" % (ctx.label.name, hash(src.path)))
|
2022-04-04 00:52:03 +00:00
|
|
|
|
|
|
|
# Flags are documented at
|
|
|
|
# https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/robocopy
|
|
|
|
# NB: robocopy return non-zero exit codes on success so we must exit 0 after calling it
|
|
|
|
cmd_tmpl = "@robocopy \"{src}\" \"{dst}\" /E >NUL & @exit 0"
|
|
|
|
mnemonic = "CopyDirectory"
|
2022-04-12 10:34:51 +00:00
|
|
|
progress_message = "Copying directory %s" % src.path
|
2022-04-04 00:52:03 +00:00
|
|
|
|
|
|
|
ctx.actions.write(
|
|
|
|
output = bat,
|
|
|
|
# Do not use lib/shell.bzl's shell.quote() method, because that uses
|
|
|
|
# Bash quoting syntax, which is different from cmd.exe's syntax.
|
|
|
|
content = cmd_tmpl.format(
|
2022-04-12 10:34:51 +00:00
|
|
|
src = src.path.replace("/", "\\"),
|
2022-04-04 00:52:03 +00:00
|
|
|
dst = dst.path.replace("/", "\\"),
|
|
|
|
),
|
|
|
|
is_executable = True,
|
|
|
|
)
|
|
|
|
ctx.actions.run(
|
2022-04-12 10:34:51 +00:00
|
|
|
inputs = [src],
|
2022-04-04 00:52:03 +00:00
|
|
|
tools = [bat],
|
|
|
|
outputs = [dst],
|
|
|
|
executable = "cmd.exe",
|
|
|
|
arguments = ["/C", bat.path.replace("/", "\\")],
|
|
|
|
mnemonic = mnemonic,
|
|
|
|
progress_message = progress_message,
|
|
|
|
use_default_shell_env = True,
|
2022-04-14 17:48:44 +00:00
|
|
|
execution_requirements = _COPY_EXECUTION_REQUIREMENTS,
|
2022-04-04 00:52:03 +00:00
|
|
|
)
|
|
|
|
|
2022-04-12 10:34:51 +00:00
|
|
|
def _copy_bash(ctx, src, dst):
|
|
|
|
cmd = "rm -rf \"$2\" && cp -fR \"$1/\" \"$2\""
|
2022-04-04 00:52:03 +00:00
|
|
|
mnemonic = "CopyDirectory"
|
2022-04-12 10:34:51 +00:00
|
|
|
progress_message = "Copying directory %s" % src.path
|
2022-04-04 00:52:03 +00:00
|
|
|
|
|
|
|
ctx.actions.run_shell(
|
2022-04-12 10:34:51 +00:00
|
|
|
tools = [src],
|
2022-04-04 00:52:03 +00:00
|
|
|
outputs = [dst],
|
2022-04-12 10:34:51 +00:00
|
|
|
command = cmd,
|
|
|
|
arguments = [src.path, dst.path],
|
2022-04-04 00:52:03 +00:00
|
|
|
mnemonic = mnemonic,
|
|
|
|
progress_message = progress_message,
|
|
|
|
use_default_shell_env = True,
|
2022-04-14 17:48:44 +00:00
|
|
|
execution_requirements = _COPY_EXECUTION_REQUIREMENTS,
|
2022-04-04 00:52:03 +00:00
|
|
|
)
|
|
|
|
|
2022-09-13 03:45:48 +00:00
|
|
|
def copy_directory_action(ctx, src, dst, is_windows = None):
|
2022-04-12 10:34:51 +00:00
|
|
|
"""Helper function that creates an action to copy a directory from src to dst.
|
|
|
|
|
|
|
|
This helper is used by copy_directory. It is exposed as a public API so it can be used within
|
|
|
|
other rule implementations.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
ctx: The rule context.
|
|
|
|
src: The directory to make a copy of. Can be a source directory or TreeArtifact.
|
|
|
|
dst: The directory to copy to. Must be a TreeArtifact.
|
2022-09-13 03:45:48 +00:00
|
|
|
is_windows: Deprecated and unused
|
2022-04-12 10:34:51 +00:00
|
|
|
"""
|
2022-09-13 03:45:48 +00:00
|
|
|
|
|
|
|
# TODO(2.0): remove depcreated & unused is_windows parameter
|
2022-04-12 10:34:51 +00:00
|
|
|
if not src.is_source and not dst.is_directory:
|
|
|
|
fail("src must be a source directory or TreeArtifact")
|
|
|
|
if dst.is_source or not dst.is_directory:
|
|
|
|
fail("dst must be a TreeArtifact")
|
2022-09-13 03:45:48 +00:00
|
|
|
|
|
|
|
# Because copy actions have "local" execution requirements, we can safely assume
|
|
|
|
# the execution is the same as the host platform and generate different actions for Windows
|
|
|
|
# and non-Windows host platforms
|
|
|
|
is_windows = _is_windows_host()
|
2022-04-12 10:34:51 +00:00
|
|
|
if is_windows:
|
|
|
|
_copy_cmd(ctx, src, dst)
|
2022-04-04 00:52:03 +00:00
|
|
|
else:
|
2022-04-12 10:34:51 +00:00
|
|
|
_copy_bash(ctx, src, dst)
|
|
|
|
|
|
|
|
def _copy_directory_impl(ctx):
|
|
|
|
dst = ctx.actions.declare_directory(ctx.attr.out)
|
2022-09-13 03:45:48 +00:00
|
|
|
copy_directory_action(ctx, ctx.file.src, dst)
|
2022-04-04 00:52:03 +00:00
|
|
|
|
2022-04-12 10:34:51 +00:00
|
|
|
files = depset(direct = [dst])
|
|
|
|
runfiles = ctx.runfiles(files = [dst])
|
2022-04-04 00:52:03 +00:00
|
|
|
|
|
|
|
return [DefaultInfo(files = files, runfiles = runfiles)]
|
|
|
|
|
|
|
|
_copy_directory = rule(
|
|
|
|
implementation = _copy_directory_impl,
|
|
|
|
provides = [DefaultInfo],
|
|
|
|
attrs = {
|
|
|
|
"src": attr.label(mandatory = True, allow_single_file = True),
|
|
|
|
# Cannot declare out as an output here, because there's no API for declaring
|
|
|
|
# TreeArtifact outputs.
|
|
|
|
"out": attr.string(mandatory = True),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
def copy_directory(name, src, out, **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).
|
|
|
|
|
|
|
|
If using this rule with source directories, it is recommended that you use the
|
|
|
|
`--host_jvm_args=-DBAZEL_TRACK_SOURCE_DIRECTORIES=1` startup option so that changes
|
|
|
|
to files within source directories are detected. See
|
|
|
|
https://github.com/bazelbuild/bazel/commit/c64421bc35214f0414e4f4226cc953e8c55fa0d2
|
|
|
|
for more context.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
name: Name of the rule.
|
2022-04-12 10:34:51 +00:00
|
|
|
src: The directory to make a copy of. Can be a source directory or TreeArtifact.
|
2022-04-04 00:52:03 +00:00
|
|
|
out: Path of the output directory, relative to this package.
|
|
|
|
**kwargs: further keyword arguments, e.g. `visibility`
|
|
|
|
"""
|
|
|
|
_copy_directory(
|
|
|
|
name = name,
|
|
|
|
src = src,
|
|
|
|
out = out,
|
|
|
|
**kwargs
|
|
|
|
)
|