| Tim Windelschmidt | 5d0f634 | 2024-09-25 03:35:00 +0200 | [diff] [blame^] | 1 | From 6d876e488124d7f0f6d660164c112a1a5d375218 Mon Sep 17 00:00:00 2001 |
| Tim Windelschmidt | e5e90a8 | 2024-07-17 23:46:22 +0200 | [diff] [blame] | 2 | From: Tim Windelschmidt <tim@monogon.tech> |
| 3 | Date: Wed, 17 Jul 2024 18:27:41 +0200 |
| Lorenz Brun | efb028f | 2020-07-28 17:04:49 +0200 | [diff] [blame] | 4 | Subject: [PATCH] Add support for prepatching |
| 5 | |
| 6 | --- |
| Tim Windelschmidt | e5e90a8 | 2024-07-17 23:46:22 +0200 | [diff] [blame] | 7 | internal/bzlmod/go_deps.bzl | 13 +++++++++++++ |
| 8 | internal/go_repository.bzl | 10 +++++++++- |
| 9 | 2 files changed, 22 insertions(+), 1 deletion(-) |
| 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 |
| Tim Windelschmidt | 5d0f634 | 2024-09-25 03:35:00 +0200 | [diff] [blame^] | 12 | index dcd0db3..7170506 100644 |
| Tim Windelschmidt | 3325b4b | 2024-07-15 19:19:49 +0200 | [diff] [blame] | 13 | --- a/internal/bzlmod/go_deps.bzl |
| 14 | +++ b/internal/bzlmod/go_deps.bzl |
| Tim Windelschmidt | 5d0f634 | 2024-09-25 03:35:00 +0200 | [diff] [blame^] | 15 | @@ -159,6 +159,9 @@ def _get_build_extra_args(path, gazelle_overrides, gazelle_default_attributes): |
| Tim Windelschmidt | 3325b4b | 2024-07-15 19:19:49 +0200 | [diff] [blame] | 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 Windelschmidt | 5d0f634 | 2024-09-25 03:35:00 +0200 | [diff] [blame^] | 25 | @@ -235,6 +238,7 @@ def _process_gazelle_override(gazelle_override_tag): |
| Tim Windelschmidt | e5e90a8 | 2024-07-17 23:46:22 +0200 | [diff] [blame] | 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 | |
| Tim Windelschmidt | 5d0f634 | 2024-09-25 03:35:00 +0200 | [diff] [blame^] | 33 | @@ -243,6 +247,7 @@ def _process_archive_override(archive_override_tag): |
| Tim Windelschmidt | e5e90a8 | 2024-07-17 23:46:22 +0200 | [diff] [blame] | 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 | ) |
| Tim Windelschmidt | 5d0f634 | 2024-09-25 03:35:00 +0200 | [diff] [blame^] | 41 | @@ -611,6 +616,7 @@ def _go_deps_impl(module_ctx): |
| Tim Windelschmidt | 3325b4b | 2024-07-15 19:19:49 +0200 | [diff] [blame] | 42 | "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 Windelschmidt | 5d0f634 | 2024-09-25 03:35:00 +0200 | [diff] [blame^] | 49 | @@ -622,6 +628,7 @@ def _go_deps_impl(module_ctx): |
| Tim Windelschmidt | 3325b4b | 2024-07-15 19:19:49 +0200 | [diff] [blame] | 50 | "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 Windelschmidt | 5d0f634 | 2024-09-25 03:35:00 +0200 | [diff] [blame^] | 57 | @@ -775,6 +782,9 @@ _archive_override_tag = tag_class( |
| Tim Windelschmidt | e5e90a8 | 2024-07-17 23:46:22 +0200 | [diff] [blame] | 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 | ), |
| Tim Windelschmidt | 5d0f634 | 2024-09-25 03:35:00 +0200 | [diff] [blame^] | 67 | @@ -813,6 +823,9 @@ _module_override_tag = tag_class( |
| Tim Windelschmidt | 3325b4b | 2024-07-15 19:19:49 +0200 | [diff] [blame] | 68 | 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 Brun | efb028f | 2020-07-28 17:04:49 +0200 | [diff] [blame] | 77 | diff --git a/internal/go_repository.bzl b/internal/go_repository.bzl |
| Tim Windelschmidt | 5d0f634 | 2024-09-25 03:35:00 +0200 | [diff] [blame^] | 78 | index 48a9d14..e3efa5b 100644 |
| Lorenz Brun | efb028f | 2020-07-28 17:04:49 +0200 | [diff] [blame] | 79 | --- a/internal/go_repository.bzl |
| 80 | +++ b/internal/go_repository.bzl |
| Tim Windelschmidt | 5d0f634 | 2024-09-25 03:35:00 +0200 | [diff] [blame^] | 81 | @@ -286,6 +286,11 @@ def _go_repository_impl(ctx): |
| 82 | if result.return_code: |
| 83 | fail("%s: %s" % (ctx.name, result.stderr)) |
| Tim Windelschmidt | 3325b4b | 2024-07-15 19:19:49 +0200 | [diff] [blame] | 84 | |
| Lorenz Brun | 6570219 | 2023-08-31 16:27:38 +0200 | [diff] [blame] | 85 | + # 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 Brun | efb028f | 2020-07-28 17:04:49 +0200 | [diff] [blame] | 89 | + |
| 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 Windelschmidt | 5d0f634 | 2024-09-25 03:35:00 +0200 | [diff] [blame^] | 93 | @@ -623,7 +628,10 @@ go_repository = repository_rule( |
| Tim Windelschmidt | e5e90a8 | 2024-07-17 23:46:22 +0200 | [diff] [blame] | 94 | prefixed with `#` automatically. A common use case is to pass a list of |
| Lorenz Brun | d13c1c6 | 2022-03-30 19:58:58 +0200 | [diff] [blame] | 95 | Gazelle directives.""", |
| 96 | ), |
| Tim Windelschmidt | e5e90a8 | 2024-07-17 23:46:22 +0200 | [diff] [blame] | 97 | - |
| Lorenz Brun | efb028f | 2020-07-28 17:04:49 +0200 | [diff] [blame] | 98 | + # Patches to apply before running gazelle. |
| Lorenz Brun | 6570219 | 2023-08-31 16:27:38 +0200 | [diff] [blame] | 99 | + "pre_patches": attr.label_list( |
| 100 | + doc = "A list of patches to apply to the repository before gazelle runs.", |
| 101 | + ), |
| Lorenz Brun | efb028f | 2020-07-28 17:04:49 +0200 | [diff] [blame] | 102 | # Patches to apply after running gazelle. |
| Lorenz Brun | d13c1c6 | 2022-03-30 19:58:58 +0200 | [diff] [blame] | 103 | "patches": attr.label_list( |
| 104 | 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] | 105 | -- |
| 106 | 2.44.1 |
| Lorenz Brun | efb028f | 2020-07-28 17:04:49 +0200 | [diff] [blame] | 107 | |