Skip to content

Commit 862b5bf

Browse files
adincebicalexeagle
authored andcommitted
Fix java example
Signed-off-by: Adin Cebic <[email protected]>
1 parent 5628966 commit 862b5bf

File tree

4 files changed

+222
-0
lines changed

4 files changed

+222
-0
lines changed

examples/MODULE.bazel

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ bazel_dep(name = "aspect_rules_py", version = "1.3.2")
55
bazel_dep(name = "aspect_rules_ts", version = "3.5.1")
66
bazel_dep(name = "platforms", version = "0.0.11")
77
bazel_dep(name = "protobuf", version = "29.3")
8+
single_version_override(
9+
module_name = "protobuf",
10+
patch_strip = 1,
11+
patches = [
12+
"//third_party:protobuf/0001-bazel-Remove-hardcoded-dependency-on-protoc-from-lan.patch",
13+
"//third_party:protobuf/0002-Switch-to-toolchains.patch",
14+
],
15+
version = "29.3",
16+
)
17+
818
bazel_dep(name = "rules_java", version = "8.6.3")
919
bazel_dep(name = "rules_proto", version = "7.1.0")
1020
bazel_dep(name = "rules_python", version = "1.2.0")

examples/third_party/BUILD.bazel

Whitespace-only changes.
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
From 755aafd6fa3de6ea507be6a7e5790c6b53e0e5f9 Mon Sep 17 00:00:00 2001
2+
From: Fabian Meumertzheim <[email protected]>
3+
Date: Mon, 16 Dec 2024 15:55:24 +0100
4+
Subject: [PATCH 1/2] bazel: Remove hardcoded dependency on `//:protoc` from
5+
language runtimes
6+
7+
Without this change, language runtimes still result in a build of `//:protoc` even with a prebuilt `proto_toolchain` registered or `--proto_compiler` set to a precompiled protoc.
8+
---
9+
bazel/private/BUILD | 6 ++++++
10+
protobuf.bzl | 13 +++++--------
11+
2 files changed, 11 insertions(+), 8 deletions(-)
12+
13+
diff --git a/bazel/private/BUILD b/bazel/private/BUILD
14+
index 8c1c94ac8..a5b3abeda 100644
15+
--- a/bazel/private/BUILD
16+
+++ b/bazel/private/BUILD
17+
@@ -1,4 +1,5 @@
18+
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
19+
+load(":current_protoc.bzl", "current_protoc")
20+
load(":native_bool_flag.bzl", "native_bool_flag")
21+
22+
package(default_applicable_licenses = ["//:license"])
23+
@@ -28,6 +29,11 @@ toolchain_type(
24+
visibility = ["//visibility:public"],
25+
)
26+
27+
+current_protoc(
28+
+ name = "current_protoc",
29+
+ visibility = ["//:__subpackages__"],
30+
+)
31+
+
32+
bzl_library(
33+
name = "upb_proto_library_internal_bzl",
34+
srcs = [
35+
diff --git a/protobuf.bzl b/protobuf.bzl
36+
index fdf09bd6b..736cc19cf 100644
37+
--- a/protobuf.bzl
38+
+++ b/protobuf.bzl
39+
@@ -2,6 +2,7 @@ load("@bazel_skylib//lib:versions.bzl", "versions")
40+
load("@rules_cc//cc:defs.bzl", "objc_library")
41+
load("@rules_python//python:defs.bzl", "py_library")
42+
load("//bazel/common:proto_info.bzl", "ProtoInfo")
43+
+load("//bazel/private:current_protoc.bzl", "ProtocFilesToRun")
44+
45+
def _GetPath(ctx, path):
46+
if ctx.label.workspace_root:
47+
@@ -310,7 +311,7 @@ def _internal_gen_well_known_protos_java_impl(ctx):
48+
args.add_all([src.path[offset:] for src in dep.direct_sources])
49+
50+
ctx.actions.run(
51+
- executable = ctx.executable._protoc,
52+
+ executable = ctx.attr._protoc[ProtocFilesToRun].files_to_run,
53+
inputs = descriptors,
54+
outputs = [srcjar],
55+
arguments = [args],
56+
@@ -335,9 +336,7 @@ internal_gen_well_known_protos_java = rule(
57+
default = False,
58+
),
59+
"_protoc": attr.label(
60+
- executable = True,
61+
- cfg = "exec",
62+
- default = "//:protoc",
63+
+ default = "//bazel/private:current_protoc",
64+
),
65+
},
66+
)
67+
@@ -373,7 +372,7 @@ def _internal_gen_kt_protos(ctx):
68+
args.add_all([src.path[offset:] for src in dep.direct_sources])
69+
70+
ctx.actions.run(
71+
- executable = ctx.executable._protoc,
72+
+ executable = ctx.attr._protoc[ProtocFilesToRun].files_to_run,
73+
inputs = descriptors,
74+
outputs = [srcjar],
75+
arguments = [args],
76+
@@ -398,9 +397,7 @@ internal_gen_kt_protos = rule(
77+
default = False,
78+
),
79+
"_protoc": attr.label(
80+
- executable = True,
81+
- cfg = "exec",
82+
- default = "//:protoc",
83+
+ default = "//bazel/private:current_protoc",
84+
),
85+
},
86+
)
87+
--
88+
2.49.0
89+
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
From d2e4671e04bd72211d15183f891707c4ff2f11cd Mon Sep 17 00:00:00 2001
2+
From: Fabian Meumertzheim <[email protected]>
3+
Date: Mon, 16 Dec 2024 22:20:13 +0100
4+
Subject: [PATCH 2/2] Switch to toolchains
5+
6+
---
7+
bazel/private/BUILD | 6 ------
8+
protobuf.bzl | 41 ++++++++++++++++++++++++++++++-----------
9+
2 files changed, 30 insertions(+), 17 deletions(-)
10+
11+
diff --git a/bazel/private/BUILD b/bazel/private/BUILD
12+
index a5b3abeda..8c1c94ac8 100644
13+
--- a/bazel/private/BUILD
14+
+++ b/bazel/private/BUILD
15+
@@ -1,5 +1,4 @@
16+
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
17+
-load(":current_protoc.bzl", "current_protoc")
18+
load(":native_bool_flag.bzl", "native_bool_flag")
19+
20+
package(default_applicable_licenses = ["//:license"])
21+
@@ -29,11 +28,6 @@ toolchain_type(
22+
visibility = ["//visibility:public"],
23+
)
24+
25+
-current_protoc(
26+
- name = "current_protoc",
27+
- visibility = ["//:__subpackages__"],
28+
-)
29+
-
30+
bzl_library(
31+
name = "upb_proto_library_internal_bzl",
32+
srcs = [
33+
diff --git a/protobuf.bzl b/protobuf.bzl
34+
index 736cc19cf..acb190942 100644
35+
--- a/protobuf.bzl
36+
+++ b/protobuf.bzl
37+
@@ -1,8 +1,9 @@
38+
load("@bazel_skylib//lib:versions.bzl", "versions")
39+
load("@rules_cc//cc:defs.bzl", "objc_library")
40+
load("@rules_python//python:defs.bzl", "py_library")
41+
+load("//bazel/common:proto_common.bzl", "proto_common")
42+
load("//bazel/common:proto_info.bzl", "ProtoInfo")
43+
-load("//bazel/private:current_protoc.bzl", "ProtocFilesToRun")
44+
+load("//bazel/private:toolchain_helpers.bzl", "toolchains")
45+
46+
def _GetPath(ctx, path):
47+
if ctx.label.workspace_root:
48+
@@ -72,6 +73,26 @@ def _CsharpOuts(srcs):
49+
for src in srcs
50+
]
51+
52+
+_PROTOC_ATTRS = toolchains.if_legacy_toolchain({
53+
+ "_proto_compiler": attr.label(
54+
+ cfg = "exec",
55+
+ executable = True,
56+
+ allow_files = True,
57+
+ default = configuration_field("proto", "proto_compiler"),
58+
+ ),
59+
+})
60+
+_PROTOC_FRAGMENTS = ["proto"]
61+
+_PROTOC_TOOLCHAINS = toolchains.use_toolchain(toolchains.PROTO_TOOLCHAIN)
62+
+
63+
+def _protoc_files_to_run(ctx):
64+
+ if proto_common.INCOMPATIBLE_ENABLE_PROTO_TOOLCHAIN_RESOLUTION:
65+
+ toolchain = ctx.toolchains[toolchains.PROTO_TOOLCHAIN]
66+
+ if not toolchain:
67+
+ fail("Protocol compiler toolchain could not be resolved.")
68+
+ return toolchain.proto.proto_compiler
69+
+ else:
70+
+ return ctx.attr._proto_compiler[DefaultInfo].files_to_run
71+
+
72+
ProtoGenInfo = provider(
73+
fields = ["srcs", "import_flags", "deps"],
74+
)
75+
@@ -311,7 +332,7 @@ def _internal_gen_well_known_protos_java_impl(ctx):
76+
args.add_all([src.path[offset:] for src in dep.direct_sources])
77+
78+
ctx.actions.run(
79+
- executable = ctx.attr._protoc[ProtocFilesToRun].files_to_run,
80+
+ executable = _protoc_files_to_run(ctx),
81+
inputs = descriptors,
82+
outputs = [srcjar],
83+
arguments = [args],
84+
@@ -335,10 +356,9 @@ internal_gen_well_known_protos_java = rule(
85+
"javalite": attr.bool(
86+
default = False,
87+
),
88+
- "_protoc": attr.label(
89+
- default = "//bazel/private:current_protoc",
90+
- ),
91+
- },
92+
+ } | _PROTOC_ATTRS,
93+
+ fragments = _PROTOC_FRAGMENTS,
94+
+ toolchains = _PROTOC_TOOLCHAINS,
95+
)
96+
97+
def _internal_gen_kt_protos(ctx):
98+
@@ -372,7 +392,7 @@ def _internal_gen_kt_protos(ctx):
99+
args.add_all([src.path[offset:] for src in dep.direct_sources])
100+
101+
ctx.actions.run(
102+
- executable = ctx.attr._protoc[ProtocFilesToRun].files_to_run,
103+
+ executable = _protoc_files_to_run(ctx),
104+
inputs = descriptors,
105+
outputs = [srcjar],
106+
arguments = [args],
107+
@@ -396,10 +416,9 @@ internal_gen_kt_protos = rule(
108+
"lite": attr.bool(
109+
default = False,
110+
),
111+
- "_protoc": attr.label(
112+
- default = "//bazel/private:current_protoc",
113+
- ),
114+
- },
115+
+ } | _PROTOC_ATTRS,
116+
+ fragments = _PROTOC_FRAGMENTS,
117+
+ toolchains = _PROTOC_TOOLCHAINS,
118+
)
119+
120+
def internal_objc_proto_library(
121+
--
122+
2.49.0
123+

0 commit comments

Comments
 (0)