xref: /aosp_15_r20/external/skia/bazel/go_googleapis_compatibility_hack.bzl (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1"""This module defines the go_googleapis_compatibility_hack repository rule.
2
3This file is copied almost verbatim from the Skia Infrastructure repository:
4https://skia.googlesource.com/buildbot/+/d32a5c6f592dfd0668001e57b0f4bb55523629f5/bazel/external/go_googleapis_compatibility_hack.bzl
5"""
6
7def _go_googleapis_compatibility_hack_impl(ctx):
8    if ctx.name != "go_googleapis":
9        fail("This rule should be named \"go_googleapis\" for this compatibility hack to work.")
10
11    # The visibility of the below aliases should be limited to @com_github_bazelbuild_remote_apis
12    # to prevent accidental usages from other dependencies or from our own code.
13
14    # Aliases @go_googleapis//google/api:annotations_go_proto.
15    ctx.file("google/api/BUILD.bazel", """
16# GENERATED FILE. See //bazel/external/go_googleapis_compatibility_hack.bzl in the Skia Buildbot
17# repository.
18
19alias(
20  name = "annotations_go_proto",
21  actual = "@org_golang_google_genproto_googleapis_api//annotations",
22  visibility = ["@com_github_bazelbuild_remote_apis//:__subpackages__"],
23)
24""")
25
26    # Aliases @go_googleapis//google/longrunning:longrunning_go_proto".
27    ctx.file("google/longrunning/BUILD.bazel", """
28# GENERATED FILE. See //bazel/external/go_googleapis_compatibility_hack.bzl in the Skia Buildbot
29# repository.
30
31alias(
32  name = "longrunning_go_proto",
33  actual = "@org_golang_google_genproto//googleapis/longrunning",
34  visibility = ["@com_github_bazelbuild_remote_apis//:__subpackages__"],
35)
36""")
37
38    # Aliases @go_googleapis//google/rpc:status_go_pro
39    ctx.file("google/rpc/BUILD.bazel", """
40# GENERATED FILE. See //bazel/external/go_googleapis_compatibility_hack.bzl in the Skia Buildbot
41# repository.
42
43alias(
44  name = "status_go_proto",
45  actual = "@org_golang_google_genproto_googleapis_rpc//status",
46  visibility = ["@com_github_bazelbuild_remote_apis//:__subpackages__"],
47)
48""")
49
50go_googleapis_compatibility_hack = repository_rule(
51    doc = """Hack to make github.com/bazelbuild/remote-apis work with rules_go v0.41.0.
52
53Starting with version v0.41.0, rules_go no longer ships with the @go_googleapis external
54repository: https://github.com/bazelbuild/rules_go/releases/tag/v0.41.0. As per the release notes,
55Go code imported generated Google API .pb.go files should use the @org_golang_google_genproto
56repository instead (https://github.com/googleapis/go-genproto).
57
58As of 2023-05-26, the github.com/googleapis/go-genproto Go module has been broken into smaller
59submodules as a usability improvements for customers that do not depend on Google Cloud:
60https://github.com/googleapis/go-genproto/issues/1015. This means some former @go_googleapis
61dependencies are now found in Go modules such as google.golang.org/genproto/googleapis/api and
62google.golang.org/genproto/googleapis/rpc. In Bazel terms, the corresponding Gazelle-generated
63external repositories will be @org_golang_google_genproto_googleapis_api and
64@com_github_bazelbuild_remote_apis, respectively.
65
66Unfortunately, as of 2023-10-30 the https://github.com/bazelbuild/remote-apis Go module is still
67distributed with BUILD files that reference the @go_googleapis repository:
68https://github.com/bazelbuild/remote-apis/blob/6c32c3b917cc5d3cfee680c03179d7552832bb3f/build/bazel/remote/execution/v2/go/BUILD#L13.
69
70As a compatibility hack, this repository rule provides a fake @go_googleapis repository that
71aliases the Bazel labels required by https://github.com/bazelbuild/remote-apis to point to the
72correct targets within @org_golang_google_genproto, @org_golang_google_genproto_googleapis_api,
73etc.
74
75We should delete this rule if/when https://github.com/bazelbuild/remote-apis is updated to work
76with more recent versions of rules_go.
77""",
78    implementation = _go_googleapis_compatibility_hack_impl,
79)
80