11load ("@bazel_skylib//lib:versions.bzl" , "versions" )
22load ("@rules_cc//cc:defs.bzl" , "objc_library" )
33load ("@rules_python//python:defs.bzl" , "py_library" )
4+ load ("//bazel/common:proto_common.bzl" , "proto_common" )
45load ("//bazel/common:proto_info.bzl" , "ProtoInfo" )
6+ load ("//bazel/private:toolchain_helpers.bzl" , "toolchains" )
57
68def _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+
7496ProtoGenInfo = 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
345364def _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
408424def internal_objc_proto_library (
0 commit comments