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 17These rules copy a file to another location using Bash (on Linux/macOS) or 18cmd.exe (on Windows). '_copy_xfile' marks the resulting file executable, 19'_copy_file' does not. 20""" 21 22def copy_cmd(ctx, src, dst): 23 # Most Windows binaries built with MSVC use a certain argument quoting 24 # scheme. Bazel uses that scheme too to quote arguments. However, 25 # cmd.exe uses different semantics, so Bazel's quoting is wrong here. 26 # To fix that we write the command to a .bat file so no command line 27 # quoting or escaping is required. 28 bat = ctx.actions.declare_file(ctx.label.name + "-cmd.bat") 29 ctx.actions.write( 30 output = bat, 31 # Do not use lib/shell.bzl's shell.quote() method, because that uses 32 # Bash quoting syntax, which is different from cmd.exe's syntax. 33 content = "@copy /Y \"%s\" \"%s\" >NUL" % ( 34 src.path.replace("/", "\\"), 35 dst.path.replace("/", "\\"), 36 ), 37 is_executable = True, 38 ) 39 ctx.actions.run( 40 inputs = [src], 41 tools = [bat], 42 outputs = [dst], 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 50def copy_bash(ctx, src, dst): 51 ctx.actions.run_shell( 52 tools = [src], 53 outputs = [dst], 54 command = "cp -f \"$1\" \"$2\"", 55 arguments = [src.path, dst.path], 56 mnemonic = "CopyFile", 57 progress_message = "Copying files", 58 use_default_shell_env = True, 59 ) 60 61def _copy_file_impl(ctx): 62 if ctx.attr.allow_symlink: 63 ctx.actions.symlink( 64 output = ctx.outputs.out, 65 target_file = ctx.file.src, 66 is_executable = ctx.attr.is_executable, 67 ) 68 else: 69 if ctx.attr.is_windows: 70 copy_cmd(ctx, ctx.file.src, ctx.outputs.out) 71 else: 72 copy_bash(ctx, ctx.file.src, ctx.outputs.out) 73 74 files = depset(direct = [ctx.outputs.out]) 75 runfiles = ctx.runfiles(files = [ctx.outputs.out]) 76 if ctx.attr.is_executable: 77 return [DefaultInfo(files = files, runfiles = runfiles, executable = ctx.outputs.out)] 78 else: 79 return [DefaultInfo(files = files, runfiles = runfiles)] 80 81_ATTRS = { 82 "src": attr.label(mandatory = True, allow_single_file = True), 83 "out": attr.output(mandatory = True), 84 "is_windows": attr.bool(mandatory = True), 85 "is_executable": attr.bool(mandatory = True), 86 "allow_symlink": attr.bool(mandatory = True), 87} 88 89_copy_file = rule( 90 implementation = _copy_file_impl, 91 provides = [DefaultInfo], 92 attrs = _ATTRS, 93) 94 95_copy_xfile = rule( 96 implementation = _copy_file_impl, 97 executable = True, 98 provides = [DefaultInfo], 99 attrs = _ATTRS, 100) 101 102def copy_file(name, src, out, is_executable = False, allow_symlink = False, **kwargs): 103 """Copies a file to another location. 104 105 `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. 106 107 This rule uses a Bash command on Linux/macOS/non-Windows, and a cmd.exe command on Windows (no Bash is required). 108 109 Args: 110 name: Name of the rule. 111 src: A Label. The file to make a copy of. (Can also be the label of a rule 112 that generates a file.) 113 out: Path of the output file, relative to this package. 114 is_executable: A boolean. Whether to make the output file executable. When 115 True, the rule's output can be executed using `bazel run` and can be 116 in the srcs of binary and test rules that require executable sources. 117 WARNING: If `allow_symlink` is True, `src` must also be executable. 118 allow_symlink: A boolean. Whether to allow symlinking instead of copying. 119 When False, the output is always a hard copy. When True, the output 120 *can* be a symlink, but there is no guarantee that a symlink is 121 created (i.e., at the time of writing, we don't create symlinks on 122 Windows). Set this to True if you need fast copying and your tools can 123 handle symlinks (which most UNIX tools can). 124 **kwargs: further keyword arguments, e.g. `visibility` 125 """ 126 127 copy_file_impl = _copy_file 128 if is_executable: 129 copy_file_impl = _copy_xfile 130 131 copy_file_impl( 132 name = name, 133 src = src, 134 out = out, 135 is_windows = select({ 136 "@bazel_tools//src/conditions:host_windows": True, 137 "//conditions:default": False, 138 }), 139 is_executable = is_executable, 140 allow_symlink = allow_symlink, 141 **kwargs 142 ) 143