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 elif ctx.attr.is_windows: 69 copy_cmd(ctx, ctx.file.src, ctx.outputs.out) 70 else: 71 copy_bash(ctx, ctx.file.src, ctx.outputs.out) 72 73 files = depset(direct = [ctx.outputs.out]) 74 runfiles = ctx.runfiles(files = [ctx.outputs.out]) 75 if ctx.attr.is_executable: 76 return [DefaultInfo(files = files, runfiles = runfiles, executable = ctx.outputs.out)] 77 else: 78 return [DefaultInfo(files = files, runfiles = runfiles)] 79 80_ATTRS = { 81 "src": attr.label(mandatory = True, allow_single_file = True), 82 "out": attr.output(mandatory = True), 83 "is_windows": attr.bool(mandatory = True), 84 "is_executable": attr.bool(mandatory = True), 85 "allow_symlink": attr.bool(mandatory = True), 86} 87 88_copy_file = rule( 89 implementation = _copy_file_impl, 90 provides = [DefaultInfo], 91 attrs = _ATTRS, 92) 93 94_copy_xfile = rule( 95 implementation = _copy_file_impl, 96 executable = True, 97 provides = [DefaultInfo], 98 attrs = _ATTRS, 99) 100 101def copy_file(name, src, out, is_executable = False, allow_symlink = False, **kwargs): 102 """Copies a file to another location. 103 104 `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. 105 106 This rule uses a Bash command on Linux/macOS/non-Windows, and a cmd.exe command on Windows (no Bash is required). 107 108 Args: 109 name: Name of the rule. 110 src: A Label. The file to make a copy of. (Can also be the label of a rule 111 that generates a file.) 112 out: Path of the output file, relative to this package. 113 is_executable: A boolean. Whether to make the output file executable. When 114 True, the rule's output can be executed using `bazel run` and can be 115 in the srcs of binary and test rules that require executable sources. 116 WARNING: If `allow_symlink` is True, `src` must also be executable. 117 allow_symlink: A boolean. Whether to allow symlinking instead of copying. 118 When False, the output is always a hard copy. When True, the output 119 *can* be a symlink, but there is no guarantee that a symlink is 120 created (i.e., at the time of writing, we don't create symlinks on 121 Windows). Set this to True if you need fast copying and your tools can 122 handle symlinks (which most UNIX tools can). 123 **kwargs: further keyword arguments, e.g. `visibility` 124 """ 125 126 copy_file_impl = _copy_file 127 if is_executable: 128 copy_file_impl = _copy_xfile 129 130 copy_file_impl( 131 name = name, 132 src = src, 133 out = out, 134 is_windows = select({ 135 "@bazel_tools//src/conditions:host_windows": True, 136 "//conditions:default": False, 137 }), 138 is_executable = is_executable, 139 allow_symlink = allow_symlink, 140 **kwargs 141 ) 142