Skip to content

Commit 3b2f83c

Browse files
committed
Apply protobuf#19679, register toolchain early
This is an experiment to see if applying protocolbuffers/protobuf#19679 via a patch and registering `@rules_scala_toolchains//protoc/...:all` will fix Windows MSVC builds. Local experiments suggest that this does indeed enable the toolchainized `protoc` to take precedence and avoid compiling protobuf.
1 parent 7b7ab2a commit 3b2f83c

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed

WORKSPACE

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ load("//scala:deps.bzl", "rules_scala_dependencies")
55

66
rules_scala_dependencies()
77

8+
register_toolchains("@rules_scala_toolchains//protoc/...:all")
9+
810
# The next two calls instantiate the `@host_platform` repo to work around:
911
# - https://github.com/bazelbuild/bazel/issues/22558
1012
# Only required if using `--incompatible_enable_proto_toolchain_resolution`.
+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
diff --git a/protobuf.bzl b/protobuf.bzl
2+
index 283c85850..ad91faba6 100644
3+
--- a/protobuf.bzl
4+
+++ b/protobuf.bzl
5+
@@ -1,7 +1,9 @@
6+
load("@bazel_skylib//lib:versions.bzl", "versions")
7+
load("@rules_cc//cc:defs.bzl", "objc_library")
8+
load("@rules_python//python:defs.bzl", "py_library")
9+
+load("//bazel/common:proto_common.bzl", "proto_common")
10+
load("//bazel/common:proto_info.bzl", "ProtoInfo")
11+
+load("//bazel/private:toolchain_helpers.bzl", "toolchains")
12+
13+
def _GetPath(ctx, path):
14+
if ctx.label.workspace_root:
15+
@@ -71,6 +73,26 @@ def _CsharpOuts(srcs):
16+
for src in srcs
17+
]
18+
19+
+_PROTOC_ATTRS = toolchains.if_legacy_toolchain({
20+
+ "_proto_compiler": attr.label(
21+
+ cfg = "exec",
22+
+ executable = True,
23+
+ allow_files = True,
24+
+ default = configuration_field("proto", "proto_compiler"),
25+
+ ),
26+
+})
27+
+_PROTOC_FRAGMENTS = ["proto"]
28+
+_PROTOC_TOOLCHAINS = toolchains.use_toolchain(toolchains.PROTO_TOOLCHAIN)
29+
+
30+
+def _protoc_files_to_run(ctx):
31+
+ if proto_common.INCOMPATIBLE_ENABLE_PROTO_TOOLCHAIN_RESOLUTION:
32+
+ toolchain = ctx.toolchains[toolchains.PROTO_TOOLCHAIN]
33+
+ if not toolchain:
34+
+ fail("Protocol compiler toolchain could not be resolved.")
35+
+ return toolchain.proto.proto_compiler
36+
+ else:
37+
+ return ctx.attr._proto_compiler[DefaultInfo].files_to_run
38+
+
39+
ProtoGenInfo = provider(
40+
fields = ["srcs", "import_flags", "deps"],
41+
)
42+
@@ -310,7 +332,7 @@ def _internal_gen_well_known_protos_java_impl(ctx):
43+
args.add_all([src.path[offset:] for src in dep.direct_sources])
44+
45+
ctx.actions.run(
46+
- executable = ctx.executable._protoc,
47+
+ executable = _protoc_files_to_run(ctx),
48+
inputs = descriptors,
49+
outputs = [srcjar],
50+
arguments = [args],
51+
@@ -334,12 +356,9 @@ internal_gen_well_known_protos_java = rule(
52+
"javalite": attr.bool(
53+
default = False,
54+
),
55+
- "_protoc": attr.label(
56+
- executable = True,
57+
- cfg = "exec",
58+
- default = "//:protoc",
59+
- ),
60+
- },
61+
+ } | _PROTOC_ATTRS,
62+
+ fragments = _PROTOC_FRAGMENTS,
63+
+ toolchains = _PROTOC_TOOLCHAINS,
64+
)
65+
66+
def _internal_gen_kt_protos(ctx):
67+
@@ -373,7 +392,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 = _protoc_files_to_run(ctx),
73+
inputs = descriptors,
74+
outputs = [srcjar],
75+
arguments = [args],
76+
@@ -397,12 +416,9 @@ internal_gen_kt_protos = rule(
77+
"lite": attr.bool(
78+
default = False,
79+
),
80+
- "_protoc": attr.label(
81+
- executable = True,
82+
- cfg = "exec",
83+
- default = "//:protoc",
84+
- ),
85+
- },
86+
+ } | _PROTOC_ATTRS,
87+
+ fragments = _PROTOC_FRAGMENTS,
88+
+ toolchains = _PROTOC_TOOLCHAINS,
89+
)
90+
91+
def internal_objc_proto_library(

patches/BUILD

Whitespace-only changes.

scala/deps.bzl

+2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ def rules_scala_dependencies():
5757
strip_prefix = "protobuf-29.3",
5858
url = "https://github.com/protocolbuffers/protobuf/archive/refs/tags/v29.3.tar.gz",
5959
repo_mapping = {"@com_google_absl": "@abseil-cpp"},
60+
patches = ["//patches:0001-protobuf-rm-protoc-dep.patch"],
61+
patch_args = ["-p1"],
6062
)
6163

6264
maybe(

0 commit comments

Comments
 (0)