Skip to content

Commit 0d1f750

Browse files
committed
Move to a prebuilt protoc
1 parent 8c169bd commit 0d1f750

File tree

6 files changed

+122
-2
lines changed

6 files changed

+122
-2
lines changed

.bazelrc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,17 @@ query --@aspect_rules_ts//ts:default_to_tsc_transpiler
5757

5858
build --incompatible_strict_action_env
5959

60+
# Required for prebuilt protoc to be used
61+
common --incompatible_enable_proto_toolchain_resolution
62+
63+
# Ensure that we don't accidentally build protobuf or gRPC
64+
common --per_file_copt=external/.*protobuf.*@--PROTOBUF_WAS_NOT_SUPPOSED_TO_BE_BUILT
65+
common --host_per_file_copt=external/.*protobuf.*@--PROTOBUF_WAS_NOT_SUPPOSED_TO_BE_BUILT
66+
common --per_file_copt=external/.*grpc.*@--GRPC_WAS_NOT_SUPPOSED_TO_BE_BUILT
67+
common --host_per_file_copt=external/.*grpc.*@--GRPC_WAS_NOT_SUPPOSED_TO_BE_BUILT
68+
6069
# Required to get `protobuf` compiling, which is required for `rules_closure`
70+
common --incompatible_enable_proto_toolchain_resolution
6171
build --incompatible_enable_cc_toolchain_resolution
6272
build --cxxopt=-std=c++17
6373
build --host_cxxopt=-std=c++17

MODULE.bazel

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ bazel_dep(name = "buildifier_prebuilt", version = "8.2.1.1")
1212
bazel_dep(name = "contrib_rules_jvm", version = "0.31.1")
1313
bazel_dep(name = "platforms", version = "1.0.0")
1414

15+
# If you update this, also update the prebuilt version of `protoc` we use below
16+
bazel_dep(name = "protobuf", version = "32.1")
17+
1518
# Required for rules_rust to import the crates properly
1619
bazel_dep(name = "rules_cc", version = "0.2.16", dev_dependency = True)
1720

@@ -27,20 +30,29 @@ bazel_dep(name = "rules_python", version = "1.7.0")
2730
bazel_dep(name = "rules_proto", version = "7.1.0")
2831
bazel_dep(name = "rules_ruby", version = "0.21.1")
2932
bazel_dep(name = "rules_rust", version = "0.68.1")
33+
bazel_dep(name = "toolchains_protoc", version = "0.6.0")
3034

3135
# Provide mechanisms to group javadocs together.
3236
single_version_override(
3337
module_name = "rules_jvm_external",
3438
patch_strip = 1,
35-
patches = ["//java:rules_jvm_external_javadoc.patch"],
39+
patches = ["//third_party/bazel:rules_jvm_external_javadoc.patch"],
3640
)
3741

3842
# Work around `contrib_rules_jvm` not properly handling non-iterable
3943
# items in lists.
4044
single_version_override(
4145
module_name = "contrib_rules_jvm",
4246
patch_strip = 1,
43-
patches = ["//java:contrib_rules_jvm_select_fix.patch"],
47+
patches = ["//third_party/bazel:contrib_rules_jvm_select_fix.patch"],
48+
)
49+
50+
# Patch protobuf to use toolchain resolution for internal_gen_well_known_protos_java
51+
# instead of hardcoding //:protoc dependency, enabling prebuilt protoc usage.
52+
single_version_override(
53+
module_name = "protobuf",
54+
patch_strip = 1,
55+
patches = ["//third_party/bazel:protobuf_toolchain_resolution.patch"],
4456
)
4557

4658
multitool = use_extension("@rules_multitool//multitool:extension.bzl", "multitool")
@@ -396,6 +408,13 @@ crate.from_cargo(
396408
)
397409
use_repo(crate, "crates")
398410

411+
# Override the toolchain registration from the protobuf module
412+
protoc = use_extension("@toolchains_protoc//protoc:extensions.bzl", "protoc")
413+
protoc.toolchain(version = "v32.1")
414+
use_repo(protoc, "toolchains_protoc_hub")
415+
416+
register_toolchains("@toolchains_protoc_hub//:all")
417+
399418
selenium_manager_artifacts = use_extension("//common:selenium_manager.bzl", "selenium_manager_artifacts")
400419
use_repo(
401420
selenium_manager_artifacts,

third_party/bazel/BUILD.bazel

Whitespace-only changes.
File renamed without changes.
Lines changed: 91 additions & 0 deletions
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(
File renamed without changes.

0 commit comments

Comments
 (0)