Allow applying patches before BUILD file generation
This adds support for patching Go dependencies before BUILD file generation and
also plumbs that support into fietsje. No actual prepatching is done in this revision.
Test Plan: This has been used successfully in code built on top of it.
X-Origin-Diff: phab/D612
GitOrigin-RevId: 7013e5f98feb57ac64ff3dc79d1a9bb94e4152a8
diff --git a/third_party/gazelle/BUILD b/third_party/gazelle/BUILD
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/third_party/gazelle/BUILD
diff --git a/third_party/gazelle/add-prepatching.patch b/third_party/gazelle/add-prepatching.patch
new file mode 100644
index 0000000..edbc94c
--- /dev/null
+++ b/third_party/gazelle/add-prepatching.patch
@@ -0,0 +1,88 @@
+Copyright 2020 The Monogon Project Authors.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+
+
+From db55421f284253c06561a4df18bca262999962ce Mon Sep 17 00:00:00 2001
+From: Lorenz Brun <lorenz@nexantic.com>
+Date: Wed, 22 Jul 2020 09:46:56 +0200
+Subject: [PATCH] Add support for prepatching
+
+---
+ internal/go_repository.bzl | 18 +++++++++++++-----
+ 1 file changed, 13 insertions(+), 5 deletions(-)
+
+diff --git a/internal/go_repository.bzl b/internal/go_repository.bzl
+index 51a7317..86899bd 100644
+--- a/internal/go_repository.bzl
++++ b/internal/go_repository.bzl
+@@ -127,6 +127,8 @@ def _go_repository_impl(ctx):
+ if result.stderr:
+ print("fetch_repo: " + result.stderr)
+
++ patch(ctx, True)
++
+ # Repositories are fetched. Determine if build file generation is needed.
+ build_file_names = ctx.attr.build_file_name.split(",")
+ existing_build_file = ""
+@@ -160,7 +162,7 @@ def _go_repository_impl(ctx):
+ "-repo_root",
+ ctx.path(""),
+ "-repo_config",
+- ctx.path(ctx.attr.build_config)
++ ctx.path(ctx.attr.build_config),
+ ]
+ if ctx.attr.version:
+ cmd.append("-go_repository_module_mode")
+@@ -247,9 +249,13 @@ go_repository = repository_rule(
+ ],
+ ),
+ "build_extra_args": attr.string_list(),
+- "build_config": attr.label(default= "@bazel_gazelle_go_repository_config//:WORKSPACE"),
++ "build_config": attr.label(default = "@bazel_gazelle_go_repository_config//:WORKSPACE"),
+ "build_directives": attr.string_list(default = []),
+
++ # Patches to apply before running gazelle.
++ "pre_patches": attr.label_list(),
++ "pre_patch_cmds": attr.string_list(default = []),
++
+ # Patches to apply after running gazelle.
+ "patches": attr.label_list(),
+ "patch_tool": attr.string(default = "patch"),
+@@ -260,10 +266,11 @@ go_repository = repository_rule(
+ """See repository.rst#go-repository for full documentation."""
+
+ # Copied from @bazel_tools//tools/build_defs/repo:utils.bzl
+-def patch(ctx):
++def patch(ctx, pre_mode = False):
+ """Implementation of patching an already extracted repository"""
+ bash_exe = ctx.os.environ["BAZEL_SH"] if "BAZEL_SH" in ctx.os.environ else "bash"
+- for patchfile in ctx.attr.patches:
++ patches = ctx.attr.patches if not pre_mode else ctx.attr.pre_patches
++ for patchfile in patches:
+ command = "{patchtool} {patch_args} < {patchfile}".format(
+ patchtool = ctx.attr.patch_tool,
+ patchfile = ctx.path(patchfile),
+@@ -276,7 +283,8 @@ def patch(ctx):
+ if st.return_code:
+ fail("Error applying patch %s:\n%s%s" %
+ (str(patchfile), st.stderr, st.stdout))
+- for cmd in ctx.attr.patch_cmds:
++ patch_cmds = ctx.attr.patch_cmds if not pre_mode else ctx.attr.pre_patch_cmds
++ for cmd in patch_cmds:
+ st = ctx.execute([bash_exe, "-c", cmd])
+ if st.return_code:
+ fail("Error applying patch command %s:\n%s%s" %
+--
+2.25.1
+