|
| 1 | +# Copyright 2018 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 | +"""Fork of @bazel_tools//tools/build_defs/repo:utils.bzl patch function with |
| 16 | +working_directory argument added. |
| 17 | +""" |
| 18 | + |
| 19 | +load(":repo_utils.bzl", "repo_utils") |
| 20 | + |
| 21 | +# Temporary directory for downloading remote patch files. |
| 22 | +_REMOTE_PATCH_DIR = ".tmp_remote_patches" |
| 23 | + |
| 24 | +def _use_native_patch(patch_args): |
| 25 | + """If patch_args only contains -p<NUM> options, we can use the native patch implementation.""" |
| 26 | + for arg in patch_args: |
| 27 | + if not arg.startswith("-p"): |
| 28 | + return False |
| 29 | + return True |
| 30 | + |
| 31 | +def _download_patch(ctx, patch_url, integrity, auth): |
| 32 | + name = patch_url.split("/")[-1] |
| 33 | + patch_path = ctx.path(_REMOTE_PATCH_DIR).get_child(name) |
| 34 | + ctx.download( |
| 35 | + patch_url, |
| 36 | + patch_path, |
| 37 | + canonical_id = ctx.attr.canonical_id, |
| 38 | + auth = auth, |
| 39 | + integrity = integrity, |
| 40 | + ) |
| 41 | + return patch_path |
| 42 | + |
| 43 | +def patch(ctx, patches = None, patch_cmds = None, patch_cmds_win = None, patch_tool = None, patch_args = None, auth = None, working_directory = None): |
| 44 | + """Implementation of patching an already extracted repository. |
| 45 | +
|
| 46 | + This rule is intended to be used in the implementation function of |
| 47 | + a repository rule. If the parameters `patches`, `patch_tool`, |
| 48 | + `patch_args`, `patch_cmds` and `patch_cmds_win` are not specified |
| 49 | + then they are taken from `ctx.attr`. |
| 50 | +
|
| 51 | + Args: |
| 52 | + ctx: The repository context of the repository rule calling this utility |
| 53 | + function. |
| 54 | + patches: The patch files to apply. List of strings, Labels, or paths. |
| 55 | + patch_cmds: Bash commands to run for patching, passed one at a |
| 56 | + time to bash -c. List of strings |
| 57 | + patch_cmds_win: Powershell commands to run for patching, passed |
| 58 | + one at a time to powershell /c. List of strings. If the |
| 59 | + boolean value of this parameter is false, patch_cmds will be |
| 60 | + used and this parameter will be ignored. |
| 61 | + patch_tool: Path of the patch tool to execute for applying |
| 62 | + patches. String. |
| 63 | + patch_args: Arguments to pass to the patch tool. List of strings. |
| 64 | + auth: An optional dict specifying authentication information for some of the URLs. |
| 65 | + working_directory: Working directory to apply the patches in |
| 66 | +
|
| 67 | + """ |
| 68 | + bash_exe = ctx.os.environ["BAZEL_SH"] if "BAZEL_SH" in ctx.os.environ else "bash" |
| 69 | + powershell_exe = ctx.os.environ["BAZEL_POWERSHELL"] if "BAZEL_POWERSHELL" in ctx.os.environ else "powershell.exe" |
| 70 | + |
| 71 | + if patches == None: |
| 72 | + patches = [] |
| 73 | + if hasattr(ctx.attr, "patches") and ctx.attr.patches: |
| 74 | + patches += ctx.attr.patches |
| 75 | + |
| 76 | + remote_patches = {} |
| 77 | + remote_patch_strip = 0 |
| 78 | + if hasattr(ctx.attr, "remote_patches") and ctx.attr.remote_patches: |
| 79 | + if hasattr(ctx.attr, "remote_patch_strip"): |
| 80 | + remote_patch_strip = ctx.attr.remote_patch_strip |
| 81 | + remote_patches = ctx.attr.remote_patches |
| 82 | + |
| 83 | + if patch_cmds == None and hasattr(ctx.attr, "patch_cmds"): |
| 84 | + patch_cmds = ctx.attr.patch_cmds |
| 85 | + if patch_cmds == None: |
| 86 | + patch_cmds = [] |
| 87 | + |
| 88 | + if patch_cmds_win == None and hasattr(ctx.attr, "patch_cmds_win"): |
| 89 | + patch_cmds_win = ctx.attr.patch_cmds_win |
| 90 | + if patch_cmds_win == None: |
| 91 | + patch_cmds_win = [] |
| 92 | + |
| 93 | + if patch_tool == None and hasattr(ctx.attr, "patch_tool"): |
| 94 | + patch_tool = ctx.attr.patch_tool |
| 95 | + if not patch_tool: |
| 96 | + patch_tool = "patch" |
| 97 | + native_patch = True |
| 98 | + else: |
| 99 | + native_patch = False |
| 100 | + |
| 101 | + if patch_args == None and hasattr(ctx.attr, "patch_args"): |
| 102 | + patch_args = ctx.attr.patch_args |
| 103 | + if patch_args == None: |
| 104 | + patch_args = [] |
| 105 | + |
| 106 | + if len(remote_patches) > 0 or len(patches) > 0 or len(patch_cmds) > 0: |
| 107 | + ctx.report_progress("Patching repository") |
| 108 | + |
| 109 | + # Apply remote patches |
| 110 | + for patch_url in remote_patches: |
| 111 | + integrity = remote_patches[patch_url] |
| 112 | + patchfile = _download_patch(ctx, patch_url, integrity, auth) |
| 113 | + ctx.patch(patchfile, remote_patch_strip) |
| 114 | + ctx.delete(patchfile) |
| 115 | + ctx.delete(ctx.path(_REMOTE_PATCH_DIR)) |
| 116 | + |
| 117 | + # Apply local patches |
| 118 | + if native_patch and _use_native_patch(patch_args) and not working_directory: |
| 119 | + if patch_args: |
| 120 | + strip = int(patch_args[-1][2:]) |
| 121 | + else: |
| 122 | + strip = 0 |
| 123 | + for patchfile in patches: |
| 124 | + ctx.patch(patchfile, strip) |
| 125 | + else: |
| 126 | + for patchfile in patches: |
| 127 | + command = "{patchtool} {patch_args} < {patchfile}".format( |
| 128 | + patchtool = patch_tool, |
| 129 | + patchfile = ctx.path(patchfile), |
| 130 | + patch_args = " ".join([ |
| 131 | + "'%s'" % arg |
| 132 | + for arg in patch_args |
| 133 | + ]), |
| 134 | + ) |
| 135 | + st = ctx.execute([bash_exe, "-c", command], working_directory = working_directory) |
| 136 | + if st.return_code: |
| 137 | + fail("Error applying patch %s:\n%s%s" % |
| 138 | + (str(patchfile), st.stderr, st.stdout)) |
| 139 | + |
| 140 | + if repo_utils.is_windows_os(ctx) and patch_cmds_win: |
| 141 | + for cmd in patch_cmds_win: |
| 142 | + st = ctx.execute([powershell_exe, "/c", cmd], working_directory = working_directory) |
| 143 | + if st.return_code: |
| 144 | + fail("Error applying patch command %s:\n%s%s" % |
| 145 | + (cmd, st.stdout, st.stderr)) |
| 146 | + else: |
| 147 | + for cmd in patch_cmds: |
| 148 | + st = ctx.execute([bash_exe, "-c", cmd], working_directory = working_directory) |
| 149 | + if st.return_code: |
| 150 | + fail("Error applying patch command %s:\n%s%s" % |
| 151 | + (cmd, st.stdout, st.stderr)) |
0 commit comments