Use coreutils toolchain for copy_file action (#622)

refactor: use coreutils toolchain for copy_file action
---------

Co-authored-by: Greg Magolan <greg@aspect.dev>
Co-authored-by: Derek Cormier <derek@aspect.dev>
This commit is contained in:
David Zbarsky 2023-11-14 14:03:51 -05:00 committed by GitHub
parent b74a45456d
commit 01ca8f9432
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 64 deletions

View File

@ -26,63 +26,6 @@ cmd.exe (on Windows). `_copy_xfile` marks the resulting file executable,
load(":copy_common.bzl", _COPY_EXECUTION_REQUIREMENTS = "COPY_EXECUTION_REQUIREMENTS", _progress_path = "progress_path") load(":copy_common.bzl", _COPY_EXECUTION_REQUIREMENTS = "COPY_EXECUTION_REQUIREMENTS", _progress_path = "progress_path")
load(":directory_path.bzl", "DirectoryPathInfo") load(":directory_path.bzl", "DirectoryPathInfo")
load(":platform_utils.bzl", _platform_utils = "platform_utils")
def _copy_cmd(ctx, src, src_path, dst):
# 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.
# The label of the target is intentionally not included so that two different targets
# can copy the same file to the output tree.
bat = ctx.actions.declare_file("%s-cmd.bat" % hash(src_path + dst.short_path))
# Flags are documented at
# https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/copy
cmd_tmpl = "@copy /Y \"{src}\" \"{dst}\" >NUL"
mnemonic = "CopyFile"
progress_message = "Copying file %s" % _progress_path(src)
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(
src = src_path.replace("/", "\\"),
dst = dst.path.replace("/", "\\"),
),
is_executable = True,
)
ctx.actions.run(
inputs = [src],
tools = [bat],
outputs = [dst],
executable = "cmd.exe",
arguments = ["/C", bat.path.replace("/", "\\")],
mnemonic = mnemonic,
progress_message = progress_message,
use_default_shell_env = True,
execution_requirements = _COPY_EXECUTION_REQUIREMENTS,
)
def _copy_bash(ctx, src, src_path, dst):
cmd_tmpl = "cp -f \"$1\" \"$2\""
mnemonic = "CopyFile"
progress_message = "Copying file %s" % _progress_path(src)
ctx.actions.run_shell(
tools = [src],
outputs = [dst],
command = cmd_tmpl,
arguments = [src_path, dst.path],
mnemonic = mnemonic,
progress_message = progress_message,
use_default_shell_env = True,
execution_requirements = _COPY_EXECUTION_REQUIREMENTS,
)
def copy_file_action(ctx, src, dst, dir_path = None): def copy_file_action(ctx, src, dst, dir_path = None):
"""Factory function that creates an action to copy a file from src to dst. """Factory function that creates an action to copy a file from src to dst.
@ -109,13 +52,17 @@ def copy_file_action(ctx, src, dst, dir_path = None):
else: else:
src_path = src.path src_path = src.path
# Because copy actions have "local" execution requirements, we can safely assume coreutils = ctx.toolchains["@aspect_bazel_lib//lib:coreutils_toolchain_type"].coreutils_info
# the execution is the same as the host platform and generate different actions for Windows
# and non-Windows host platforms ctx.actions.run(
if _platform_utils.host_platform_is_windows(): executable = coreutils.bin,
_copy_cmd(ctx, src, src_path, dst) arguments = ["cp", src_path, dst.path],
else: inputs = [src],
_copy_bash(ctx, src, src_path, dst) outputs = [dst],
mnemonic = "CopyFile",
progress_message = "Copying file %s" % _progress_path(src),
execution_requirements = _COPY_EXECUTION_REQUIREMENTS,
)
def _copy_file_impl(ctx): def _copy_file_impl(ctx):
if ctx.attr.allow_symlink: if ctx.attr.allow_symlink:
@ -160,6 +107,7 @@ _copy_file = rule(
implementation = _copy_file_impl, implementation = _copy_file_impl,
provides = [DefaultInfo], provides = [DefaultInfo],
attrs = _ATTRS, attrs = _ATTRS,
toolchains = ["@aspect_bazel_lib//lib:coreutils_toolchain_type"],
) )
_copy_xfile = rule( _copy_xfile = rule(
@ -167,6 +115,7 @@ _copy_xfile = rule(
executable = True, executable = True,
provides = [DefaultInfo], provides = [DefaultInfo],
attrs = _ATTRS, attrs = _ATTRS,
toolchains = ["@aspect_bazel_lib//lib:coreutils_toolchain_type"],
) )
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, **kwargs):

View File

@ -121,6 +121,7 @@ _copy_to_bin = rule(
attrs = { attrs = {
"srcs": attr.label_list(mandatory = True, allow_files = True), "srcs": attr.label_list(mandatory = True, allow_files = True),
}, },
toolchains = ["@aspect_bazel_lib//lib:coreutils_toolchain_type"],
) )
def copy_to_bin(name, srcs, **kwargs): def copy_to_bin(name, srcs, **kwargs):