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