blob: fc409a1450908717704c7d41768c3ff7210e32b8 [file] [log] [blame]
Tim Windelschmidt3325b4b2024-07-15 19:19:49 +02001From f784d39b50b36ca06fbe844e2b09cbd996747577 Mon Sep 17 00:00:00 2001
Lorenz Brun65702192023-08-31 16:27:38 +02002From: Lorenz Brun <lorenz@monogon.tech>
3Date: Thu, 31 Aug 2023 15:52:06 +0200
Lorenz Brunefb028f2020-07-28 17:04:49 +02004Subject: [PATCH] Add support for prepatching
5
6---
Tim Windelschmidt3325b4b2024-07-15 19:19:49 +02007 internal/bzlmod/go_deps.bzl | 8 ++++++++
8 internal/go_repository.bzl | 10 ++++++++++
9 2 files changed, 18 insertions(+)
Lorenz Brunefb028f2020-07-28 17:04:49 +020010
Tim Windelschmidt3325b4b2024-07-15 19:19:49 +020011diff --git a/internal/bzlmod/go_deps.bzl b/internal/bzlmod/go_deps.bzl
12index e304ec5..7684243 100644
13--- a/internal/bzlmod/go_deps.bzl
14+++ b/internal/bzlmod/go_deps.bzl
15@@ -156,6 +156,9 @@ def _get_build_extra_args(path, gazelle_overrides, gazelle_default_attributes):
16 def _get_patches(path, module_overrides):
17 return _get_override_or_default(module_overrides, struct(), {}, path, [], "patches")
18
19+def _get_pre_patches(path, module_overrides):
20+ return _get_override_or_default(module_overrides, struct(), {}, path, [], "pre_patches")
21+
22 def _get_patch_args(path, module_overrides):
23 override = _get_override_or_default(module_overrides, struct(), {}, path, None, "patch_strip")
24 return ["-p{}".format(override)] if override else []
25@@ -605,6 +608,7 @@ def _go_deps_impl(module_ctx):
26 "build_directives": _get_directives(path, gazelle_overrides, gazelle_default_attributes),
27 "build_file_generation": _get_build_file_generation(path, gazelle_overrides, gazelle_default_attributes),
28 "build_extra_args": _get_build_extra_args(path, gazelle_overrides, gazelle_default_attributes),
29+ "pre_patches": _get_pre_patches(path, module_overrides),
30 "patches": _get_patches(path, module_overrides),
31 "patch_args": _get_patch_args(path, module_overrides),
32 "debug_mode": debug_mode,
33@@ -616,6 +620,7 @@ def _go_deps_impl(module_ctx):
34 "urls": archive_override.urls,
35 "strip_prefix": archive_override.strip_prefix,
36 "sha256": archive_override.sha256,
37+ "pre_patches": _get_pre_patches(path, archive_overrides),
38 "patches": _get_patches(path, archive_overrides),
39 "patch_args": _get_patch_args(path, archive_overrides),
40 })
41@@ -799,6 +804,9 @@ _module_override_tag = tag_class(
42 extension within this Bazel module.""",
43 mandatory = True,
44 ),
45+ "pre_patches": attr.label_list(
46+ doc = "A list of patches to apply to the repository before gazelle runs.",
47+ ),
48 "patches": attr.label_list(
49 doc = "A list of patches to apply to the repository *after* gazelle runs.",
50 ),
Lorenz Brunefb028f2020-07-28 17:04:49 +020051diff --git a/internal/go_repository.bzl b/internal/go_repository.bzl
Tim Windelschmidt3325b4b2024-07-15 19:19:49 +020052index 627a1f9..4da270f 100644
Lorenz Brunefb028f2020-07-28 17:04:49 +020053--- a/internal/go_repository.bzl
54+++ b/internal/go_repository.bzl
Tim Windelschmidt3325b4b2024-07-15 19:19:49 +020055@@ -296,6 +296,11 @@ def _go_repository_impl(ctx):
56 if result.return_code:
57 fail("%s: %s" % (ctx.name, result.stderr))
58
Lorenz Brun65702192023-08-31 16:27:38 +020059+ # TODO(lorenz): Replace this with patch() once the patches argument no longer gets merged with
60+ # the attribute pulled from ctx.
61+ for p in ctx.attr.pre_patches:
62+ ctx.patch(p, 1)
Lorenz Brunefb028f2020-07-28 17:04:49 +020063+
64 # Repositories are fetched. Determine if build file generation is needed.
65 build_file_names = ctx.attr.build_file_name.split(",")
66 existing_build_file = ""
Tim Windelschmidt3325b4b2024-07-15 19:19:49 +020067@@ -583,6 +588,11 @@ go_repository = repository_rule(
Lorenz Brund13c1c62022-03-30 19:58:58 +020068 Gazelle directives.""",
69 ),
Tim Windelschmidt3325b4b2024-07-15 19:19:49 +020070
Lorenz Brunefb028f2020-07-28 17:04:49 +020071+ # Patches to apply before running gazelle.
Lorenz Brun65702192023-08-31 16:27:38 +020072+ "pre_patches": attr.label_list(
73+ doc = "A list of patches to apply to the repository before gazelle runs.",
74+ ),
Lorenz Brunefb028f2020-07-28 17:04:49 +020075+
76 # Patches to apply after running gazelle.
Lorenz Brund13c1c62022-03-30 19:58:58 +020077 "patches": attr.label_list(
78 doc = "A list of patches to apply to the repository after gazelle runs.",
Tim Windelschmidt3325b4b2024-07-15 19:19:49 +020079--
802.44.1
Lorenz Brunefb028f2020-07-28 17:04:49 +020081