blob: fe2d31a547bfbabad32ccb1df15b71ca136ba33f [file] [log] [blame]
Tim Windelschmidte5e90a82024-07-17 23:46:22 +02001From 914eedf51a7d636caa14c09554d1cf26fd87cb05 Mon Sep 17 00:00:00 2001
2From: Tim Windelschmidt <tim@monogon.tech>
3Date: Wed, 17 Jul 2024 18:27:41 +0200
Lorenz Brunefb028f2020-07-28 17:04:49 +02004Subject: [PATCH] Add support for prepatching
5
6---
Tim Windelschmidte5e90a82024-07-17 23:46:22 +02007 internal/bzlmod/go_deps.bzl | 13 +++++++++++++
8 internal/go_repository.bzl | 10 +++++++++-
9 2 files changed, 22 insertions(+), 1 deletion(-)
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
Tim Windelschmidte5e90a82024-07-17 23:46:22 +020012index e304ec5..778f3c3 100644
Tim Windelschmidt3325b4b2024-07-15 19:19:49 +020013--- 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 []
Tim Windelschmidte5e90a82024-07-17 23:46:22 +020025@@ -232,6 +235,7 @@ def _process_gazelle_override(gazelle_override_tag):
26 def _process_module_override(module_override_tag):
27 return struct(
28 patches = module_override_tag.patches,
29+ pre_patches = module_override_tag.pre_patches,
30 patch_strip = module_override_tag.patch_strip,
31 )
32
33@@ -240,6 +244,7 @@ def _process_archive_override(archive_override_tag):
34 urls = archive_override_tag.urls,
35 sha256 = archive_override_tag.sha256,
36 strip_prefix = archive_override_tag.strip_prefix,
37+ pre_patches = archive_override_tag.pre_patches,
38 patches = archive_override_tag.patches,
39 patch_strip = archive_override_tag.patch_strip,
40 )
41@@ -605,6 +610,7 @@ def _go_deps_impl(module_ctx):
Tim Windelschmidt3325b4b2024-07-15 19:19:49 +020042 "build_directives": _get_directives(path, gazelle_overrides, gazelle_default_attributes),
43 "build_file_generation": _get_build_file_generation(path, gazelle_overrides, gazelle_default_attributes),
44 "build_extra_args": _get_build_extra_args(path, gazelle_overrides, gazelle_default_attributes),
45+ "pre_patches": _get_pre_patches(path, module_overrides),
46 "patches": _get_patches(path, module_overrides),
47 "patch_args": _get_patch_args(path, module_overrides),
48 "debug_mode": debug_mode,
Tim Windelschmidte5e90a82024-07-17 23:46:22 +020049@@ -616,6 +622,7 @@ def _go_deps_impl(module_ctx):
Tim Windelschmidt3325b4b2024-07-15 19:19:49 +020050 "urls": archive_override.urls,
51 "strip_prefix": archive_override.strip_prefix,
52 "sha256": archive_override.sha256,
53+ "pre_patches": _get_pre_patches(path, archive_overrides),
54 "patches": _get_patches(path, archive_overrides),
55 "patch_args": _get_patch_args(path, archive_overrides),
56 })
Tim Windelschmidte5e90a82024-07-17 23:46:22 +020057@@ -761,6 +768,9 @@ _archive_override_tag = tag_class(
58 SHA-256 sum of the downloaded archive. When set, Bazel will verify the archive
59 against this sum before extracting it.""",
60 ),
61+ "pre_patches": attr.label_list(
62+ doc = "A list of patches to apply to the repository before gazelle runs.",
63+ ),
64 "patches": attr.label_list(
65 doc = "A list of patches to apply to the repository *after* gazelle runs.",
66 ),
67@@ -799,6 +809,9 @@ _module_override_tag = tag_class(
Tim Windelschmidt3325b4b2024-07-15 19:19:49 +020068 extension within this Bazel module.""",
69 mandatory = True,
70 ),
71+ "pre_patches": attr.label_list(
72+ doc = "A list of patches to apply to the repository before gazelle runs.",
73+ ),
74 "patches": attr.label_list(
75 doc = "A list of patches to apply to the repository *after* gazelle runs.",
76 ),
Lorenz Brunefb028f2020-07-28 17:04:49 +020077diff --git a/internal/go_repository.bzl b/internal/go_repository.bzl
Tim Windelschmidte5e90a82024-07-17 23:46:22 +020078index 627a1f9..b12adf4 100644
Lorenz Brunefb028f2020-07-28 17:04:49 +020079--- a/internal/go_repository.bzl
80+++ b/internal/go_repository.bzl
Tim Windelschmidt3325b4b2024-07-15 19:19:49 +020081@@ -296,6 +296,11 @@ def _go_repository_impl(ctx):
82 if result.return_code:
83 fail("%s: %s" % (ctx.name, result.stderr))
84
Lorenz Brun65702192023-08-31 16:27:38 +020085+ # TODO(lorenz): Replace this with patch() once the patches argument no longer gets merged with
86+ # the attribute pulled from ctx.
87+ for p in ctx.attr.pre_patches:
88+ ctx.patch(p, 1)
Lorenz Brunefb028f2020-07-28 17:04:49 +020089+
90 # Repositories are fetched. Determine if build file generation is needed.
91 build_file_names = ctx.attr.build_file_name.split(",")
92 existing_build_file = ""
Tim Windelschmidte5e90a82024-07-17 23:46:22 +020093@@ -582,7 +587,10 @@ go_repository = repository_rule(
94 prefixed with `#` automatically. A common use case is to pass a list of
Lorenz Brund13c1c62022-03-30 19:58:58 +020095 Gazelle directives.""",
96 ),
Tim Windelschmidte5e90a82024-07-17 23:46:22 +020097-
Lorenz Brunefb028f2020-07-28 17:04:49 +020098+ # Patches to apply before running gazelle.
Lorenz Brun65702192023-08-31 16:27:38 +020099+ "pre_patches": attr.label_list(
100+ doc = "A list of patches to apply to the repository before gazelle runs.",
101+ ),
Lorenz Brunefb028f2020-07-28 17:04:49 +0200102 # Patches to apply after running gazelle.
Lorenz Brund13c1c62022-03-30 19:58:58 +0200103 "patches": attr.label_list(
104 doc = "A list of patches to apply to the repository after gazelle runs.",
Tim Windelschmidt3325b4b2024-07-15 19:19:49 +0200105--
1062.44.1
Lorenz Brunefb028f2020-07-28 17:04:49 +0200107