| Tim Windelschmidt | 3325b4b | 2024-07-15 19:19:49 +0200 | [diff] [blame^] | 1 | From f784d39b50b36ca06fbe844e2b09cbd996747577 Mon Sep 17 00:00:00 2001 |
| Lorenz Brun | 6570219 | 2023-08-31 16:27:38 +0200 | [diff] [blame] | 2 | From: Lorenz Brun <lorenz@monogon.tech> |
| 3 | Date: Thu, 31 Aug 2023 15:52:06 +0200 |
| Lorenz Brun | efb028f | 2020-07-28 17:04:49 +0200 | [diff] [blame] | 4 | Subject: [PATCH] Add support for prepatching |
| 5 | |
| 6 | --- |
| Tim Windelschmidt | 3325b4b | 2024-07-15 19:19:49 +0200 | [diff] [blame^] | 7 | internal/bzlmod/go_deps.bzl | 8 ++++++++ |
| 8 | internal/go_repository.bzl | 10 ++++++++++ |
| 9 | 2 files changed, 18 insertions(+) |
| Lorenz Brun | efb028f | 2020-07-28 17:04:49 +0200 | [diff] [blame] | 10 | |
| Tim Windelschmidt | 3325b4b | 2024-07-15 19:19:49 +0200 | [diff] [blame^] | 11 | diff --git a/internal/bzlmod/go_deps.bzl b/internal/bzlmod/go_deps.bzl |
| 12 | index 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 Brun | efb028f | 2020-07-28 17:04:49 +0200 | [diff] [blame] | 51 | diff --git a/internal/go_repository.bzl b/internal/go_repository.bzl |
| Tim Windelschmidt | 3325b4b | 2024-07-15 19:19:49 +0200 | [diff] [blame^] | 52 | index 627a1f9..4da270f 100644 |
| Lorenz Brun | efb028f | 2020-07-28 17:04:49 +0200 | [diff] [blame] | 53 | --- a/internal/go_repository.bzl |
| 54 | +++ b/internal/go_repository.bzl |
| Tim Windelschmidt | 3325b4b | 2024-07-15 19:19:49 +0200 | [diff] [blame^] | 55 | @@ -296,6 +296,11 @@ def _go_repository_impl(ctx): |
| 56 | if result.return_code: |
| 57 | fail("%s: %s" % (ctx.name, result.stderr)) |
| 58 | |
| Lorenz Brun | 6570219 | 2023-08-31 16:27:38 +0200 | [diff] [blame] | 59 | + # 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 Brun | efb028f | 2020-07-28 17:04:49 +0200 | [diff] [blame] | 63 | + |
| 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 Windelschmidt | 3325b4b | 2024-07-15 19:19:49 +0200 | [diff] [blame^] | 67 | @@ -583,6 +588,11 @@ go_repository = repository_rule( |
| Lorenz Brun | d13c1c6 | 2022-03-30 19:58:58 +0200 | [diff] [blame] | 68 | Gazelle directives.""", |
| 69 | ), |
| Tim Windelschmidt | 3325b4b | 2024-07-15 19:19:49 +0200 | [diff] [blame^] | 70 | |
| Lorenz Brun | efb028f | 2020-07-28 17:04:49 +0200 | [diff] [blame] | 71 | + # Patches to apply before running gazelle. |
| Lorenz Brun | 6570219 | 2023-08-31 16:27:38 +0200 | [diff] [blame] | 72 | + "pre_patches": attr.label_list( |
| 73 | + doc = "A list of patches to apply to the repository before gazelle runs.", |
| 74 | + ), |
| Lorenz Brun | efb028f | 2020-07-28 17:04:49 +0200 | [diff] [blame] | 75 | + |
| 76 | # Patches to apply after running gazelle. |
| Lorenz Brun | d13c1c6 | 2022-03-30 19:58:58 +0200 | [diff] [blame] | 77 | "patches": attr.label_list( |
| 78 | doc = "A list of patches to apply to the repository after gazelle runs.", |
| Tim Windelschmidt | 3325b4b | 2024-07-15 19:19:49 +0200 | [diff] [blame^] | 79 | -- |
| 80 | 2.44.1 |
| Lorenz Brun | efb028f | 2020-07-28 17:04:49 +0200 | [diff] [blame] | 81 | |