Skip to content

Commit 2d78b09

Browse files
fmeumcopybara-github
authored andcommitted
bazel: Remove hardcoded dependency on //:protoc from language runtimes (#19679)
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. Removing this hardcoded dependency allows a (fast) build of `java_proto_library` targets without a C++ toolchain assuming a prebuilt protoc. Work towards #19558 Closes #19679 COPYBARA_INTEGRATE_REVIEW=#19679 from fmeum:19558-dont-hardcode-protoc 3bd939d FUTURE_COPYBARA_INTEGRATE_REVIEW=#19679 from fmeum:19558-dont-hardcode-protoc 3bd939d PiperOrigin-RevId: 739382145
1 parent e8edc5d commit 2d78b09

File tree

1 file changed

+30
-14
lines changed

1 file changed

+30
-14
lines changed

protobuf.bzl

+30-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
load("@bazel_skylib//lib:versions.bzl", "versions")
22
load("@rules_cc//cc:defs.bzl", "objc_library")
33
load("@rules_python//python:defs.bzl", "py_library")
4+
load("//bazel/common:proto_common.bzl", "proto_common")
45
load("//bazel/common:proto_info.bzl", "ProtoInfo")
6+
load("//bazel/private:toolchain_helpers.bzl", "toolchains")
57

68
def _GetPath(ctx, path):
79
if ctx.label.workspace_root:
@@ -71,6 +73,26 @@ def _CsharpOuts(srcs):
7173
for src in srcs
7274
]
7375

76+
_PROTOC_ATTRS = toolchains.if_legacy_toolchain({
77+
"_proto_compiler": attr.label(
78+
cfg = "exec",
79+
executable = True,
80+
allow_files = True,
81+
default = configuration_field("proto", "proto_compiler"),
82+
),
83+
})
84+
_PROTOC_FRAGMENTS = ["proto"]
85+
_PROTOC_TOOLCHAINS = toolchains.use_toolchain(toolchains.PROTO_TOOLCHAIN)
86+
87+
def _protoc_files_to_run(ctx):
88+
if proto_common.INCOMPATIBLE_ENABLE_PROTO_TOOLCHAIN_RESOLUTION:
89+
toolchain = ctx.toolchains[toolchains.PROTO_TOOLCHAIN]
90+
if not toolchain:
91+
fail("Protocol compiler toolchain could not be resolved.")
92+
return toolchain.proto.proto_compiler
93+
else:
94+
return ctx.attr._proto_compiler[DefaultInfo].files_to_run
95+
7496
ProtoGenInfo = provider(
7597
fields = ["srcs", "import_flags", "deps"],
7698
)
@@ -310,7 +332,7 @@ def _internal_gen_well_known_protos_java_impl(ctx):
310332
args.add_all([src.path[offset:] for src in dep.direct_sources])
311333

312334
ctx.actions.run(
313-
executable = ctx.executable._protoc,
335+
executable = _protoc_files_to_run(ctx),
314336
inputs = descriptors,
315337
outputs = [srcjar],
316338
arguments = [args],
@@ -334,12 +356,9 @@ internal_gen_well_known_protos_java = rule(
334356
"javalite": attr.bool(
335357
default = False,
336358
),
337-
"_protoc": attr.label(
338-
executable = True,
339-
cfg = "exec",
340-
default = "//:protoc",
341-
),
342-
},
359+
} | _PROTOC_ATTRS,
360+
fragments = _PROTOC_FRAGMENTS,
361+
toolchains = _PROTOC_TOOLCHAINS,
343362
)
344363

345364
def _internal_gen_kt_protos(ctx):
@@ -373,7 +392,7 @@ def _internal_gen_kt_protos(ctx):
373392
args.add_all([src.path[offset:] for src in dep.direct_sources])
374393

375394
ctx.actions.run(
376-
executable = ctx.executable._protoc,
395+
executable = _protoc_files_to_run(ctx),
377396
inputs = descriptors,
378397
outputs = [srcjar],
379398
arguments = [args],
@@ -397,12 +416,9 @@ internal_gen_kt_protos = rule(
397416
"lite": attr.bool(
398417
default = False,
399418
),
400-
"_protoc": attr.label(
401-
executable = True,
402-
cfg = "exec",
403-
default = "//:protoc",
404-
),
405-
},
419+
} | _PROTOC_ATTRS,
420+
fragments = _PROTOC_FRAGMENTS,
421+
toolchains = _PROTOC_TOOLCHAINS,
406422
)
407423

408424
def internal_objc_proto_library(

0 commit comments

Comments
 (0)