blob: 2150e962b7f75efd6aa43156e4ccfe9ed894d28f [file] [log] [blame]
Lorenz Brunefb028f2020-07-28 17:04:49 +02001Copyright 2020 The Monogon Project Authors.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14
15
Serge Bazanskif12bedf2021-01-15 16:58:50 +010016From 2cb763552e2e386928e6c2211c4fb056b9ca7971 Mon Sep 17 00:00:00 2001
17From: Serge Bazanski <serge@nexantic.com>
18Date: Thu, 14 Jan 2021 16:30:32 +0100
Lorenz Brunefb028f2020-07-28 17:04:49 +020019Subject: [PATCH] Add support for prepatching
Serge Bazanskif12bedf2021-01-15 16:58:50 +010020Company: nexantic GmbH
Lorenz Brunefb028f2020-07-28 17:04:49 +020021
22---
Serge Bazanskif12bedf2021-01-15 16:58:50 +010023 internal/go_repository.bzl | 14 +++++++++++---
24 1 file changed, 11 insertions(+), 3 deletions(-)
Lorenz Brunefb028f2020-07-28 17:04:49 +020025
26diff --git a/internal/go_repository.bzl b/internal/go_repository.bzl
Serge Bazanskif12bedf2021-01-15 16:58:50 +010027index 7f87ed1..a0ef36d 100644
Lorenz Brunefb028f2020-07-28 17:04:49 +020028--- a/internal/go_repository.bzl
29+++ b/internal/go_repository.bzl
Serge Bazanskif12bedf2021-01-15 16:58:50 +010030@@ -130,6 +130,8 @@ def _go_repository_impl(ctx):
Lorenz Brunefb028f2020-07-28 17:04:49 +020031 if result.stderr:
32 print("fetch_repo: " + result.stderr)
33
34+ patch(ctx, True)
35+
36 # Repositories are fetched. Determine if build file generation is needed.
37 build_file_names = ctx.attr.build_file_name.split(",")
38 existing_build_file = ""
Serge Bazanskif12bedf2021-01-15 16:58:50 +010039@@ -263,6 +265,10 @@ go_repository = repository_rule(
40 "build_config": attr.label(default = "@bazel_gazelle_go_repository_config//:WORKSPACE"),
Lorenz Brunefb028f2020-07-28 17:04:49 +020041 "build_directives": attr.string_list(default = []),
42
43+ # Patches to apply before running gazelle.
44+ "pre_patches": attr.label_list(),
45+ "pre_patch_cmds": attr.string_list(default = []),
46+
47 # Patches to apply after running gazelle.
48 "patches": attr.label_list(),
49 "patch_tool": attr.string(default = "patch"),
Serge Bazanskif12bedf2021-01-15 16:58:50 +010050@@ -273,10 +279,11 @@ go_repository = repository_rule(
Lorenz Brunefb028f2020-07-28 17:04:49 +020051 """See repository.rst#go-repository for full documentation."""
52
53 # Copied from @bazel_tools//tools/build_defs/repo:utils.bzl
54-def patch(ctx):
55+def patch(ctx, pre_mode = False):
56 """Implementation of patching an already extracted repository"""
57 bash_exe = ctx.os.environ["BAZEL_SH"] if "BAZEL_SH" in ctx.os.environ else "bash"
58- for patchfile in ctx.attr.patches:
59+ patches = ctx.attr.patches if not pre_mode else ctx.attr.pre_patches
60+ for patchfile in patches:
61 command = "{patchtool} {patch_args} < {patchfile}".format(
62 patchtool = ctx.attr.patch_tool,
63 patchfile = ctx.path(patchfile),
Serge Bazanskif12bedf2021-01-15 16:58:50 +010064@@ -289,7 +296,8 @@ def patch(ctx):
Lorenz Brunefb028f2020-07-28 17:04:49 +020065 if st.return_code:
66 fail("Error applying patch %s:\n%s%s" %
67 (str(patchfile), st.stderr, st.stdout))
68- for cmd in ctx.attr.patch_cmds:
69+ patch_cmds = ctx.attr.patch_cmds if not pre_mode else ctx.attr.pre_patch_cmds
70+ for cmd in patch_cmds:
71 st = ctx.execute([bash_exe, "-c", cmd])
72 if st.return_code:
73 fail("Error applying patch command %s:\n%s%s" %
74--
Serge Bazanskif12bedf2021-01-15 16:58:50 +0100752.26.2
Lorenz Brunefb028f2020-07-28 17:04:49 +020076