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