1414
1515"""Starlark implementation of cc_proto_library"""
1616
17- load (":common/proto/proto_info .bzl" , "ProtoInfo " )
17+ load (":common/cc/cc_common .bzl" , "cc_common " )
1818load (":common/cc/cc_helper.bzl" , "cc_helper" )
19- load (":common/proto/proto_common.bzl" , "ProtoLangToolchainInfo" , proto_common = "proto_common_do_not_use" )
2019load (":common/cc/cc_info.bzl" , "CcInfo" )
21- load (":common/cc/cc_common.bzl" , "cc_common" )
20+ load (":common/cc/semantics.bzl" , "semantics" )
21+ load (":common/proto/proto_common.bzl" , "toolchains" , proto_common = "proto_common_do_not_use" )
22+ load (":common/proto/proto_info.bzl" , "ProtoInfo" )
2223
2324ProtoCcFilesInfo = provider (fields = ["files" ], doc = "Provide cc proto files." )
2425ProtoCcHeaderInfo = provider (fields = ["headers" ], doc = "Provide cc proto headers." )
2526
26- def _are_srcs_excluded (ctx , target ):
27- return not proto_common .experimental_should_generate_code (target [ProtoInfo ], ctx . attr . _aspect_cc_proto_toolchain [ ProtoLangToolchainInfo ] , "cc_proto_library" , target .label )
27+ def _are_srcs_excluded (proto_toolchain , target ):
28+ return not proto_common .experimental_should_generate_code (target [ProtoInfo ], proto_toolchain , "cc_proto_library" , target .label )
2829
29- def _get_feature_configuration (ctx , target , cc_toolchain , proto_info ):
30+ def _get_feature_configuration (ctx , target , cc_toolchain , proto_info , proto_toolchain ):
3031 requested_features = list (ctx .features )
3132 unsupported_features = list (ctx .disabled_features )
3233 unsupported_features .append ("parse_headers" )
3334 unsupported_features .append ("layering_check" )
34- if not _are_srcs_excluded (ctx , target ) and len (proto_info .direct_sources ) != 0 :
35+ if not _are_srcs_excluded (proto_toolchain , target ) and len (proto_info .direct_sources ) != 0 :
3536 requested_features .append ("header_modules" )
3637 else :
3738 unsupported_features .append ("header_modules" )
@@ -77,12 +78,13 @@ def _get_strip_include_prefix(ctx, proto_info):
7778
7879def _aspect_impl (target , ctx ):
7980 cc_toolchain = cc_helper .find_cpp_toolchain (ctx )
81+ proto_toolchain = toolchains .find_toolchain (ctx , "_aspect_cc_proto_toolchain" , semantics .CC_PROTO_TOOLCHAIN )
8082 proto_info = target [ProtoInfo ]
81- feature_configuration = _get_feature_configuration (ctx , target , cc_toolchain , proto_info )
83+ feature_configuration = _get_feature_configuration (ctx , target , cc_toolchain , proto_info , proto_toolchain )
8284 proto_configuration = ctx .fragments .proto
8385
8486 deps = []
85- runtime = ctx . attr . _aspect_cc_proto_toolchain [ ProtoLangToolchainInfo ] .runtime
87+ runtime = proto_toolchain .runtime
8688 if runtime != None :
8789 deps .append (runtime )
8890 deps .extend (getattr (ctx .rule .attr , "deps" , []))
@@ -98,7 +100,7 @@ def _aspect_impl(target, ctx):
98100 textual_hdrs = []
99101 additional_exported_hdrs = []
100102
101- if _are_srcs_excluded (ctx , target ):
103+ if _are_srcs_excluded (proto_toolchain , target ):
102104 header_provider = None
103105
104106 # Hack: This is a proto_library for descriptor.proto or similar.
@@ -153,7 +155,7 @@ def _aspect_impl(target, ctx):
153155 proto_common .compile (
154156 actions = ctx .actions ,
155157 proto_info = proto_info ,
156- proto_lang_toolchain_info = ctx . attr . _aspect_cc_proto_toolchain [ ProtoLangToolchainInfo ] ,
158+ proto_lang_toolchain_info = proto_toolchain ,
157159 generated_files = outputs ,
158160 experimental_output_files = "multiple" ,
159161 )
@@ -248,12 +250,11 @@ cc_proto_aspect = aspect(
248250 required_providers = [ProtoInfo ],
249251 provides = [CcInfo ],
250252 attrs = {
251- "_aspect_cc_proto_toolchain" : attr .label (
252- default = configuration_field (fragment = "proto" , name = "proto_toolchain_for_cc" ),
253- ),
254253 "_cc_toolchain" : attr .label (default = "@bazel_tools//tools/cpp:current_cc_toolchain" ),
255- },
256- toolchains = cc_helper .use_cpp_toolchain (),
254+ } | toolchains .if_legacy_toolchain ({"_aspect_cc_proto_toolchain" : attr .label (
255+ default = configuration_field (fragment = "proto" , name = "proto_toolchain_for_cc" ),
256+ )}),
257+ toolchains = cc_helper .use_cpp_toolchain () + toolchains .use_toolchain (semantics .CC_PROTO_TOOLCHAIN ),
257258)
258259
259260def _impl (ctx ):
@@ -267,7 +268,8 @@ def _impl(ctx):
267268 attr = "deps" ,
268269 )
269270 dep = ctx .attr .deps [0 ]
270- proto_common .check_collocated (ctx .label , dep [ProtoInfo ], ctx .attr ._aspect_cc_proto_toolchain [ProtoLangToolchainInfo ])
271+ proto_toolchain = toolchains .find_toolchain (ctx , "_aspect_cc_proto_toolchain" , semantics .CC_PROTO_TOOLCHAIN )
272+ proto_common .check_collocated (ctx .label , dep [ProtoInfo ], proto_toolchain )
271273 cc_info = dep [CcInfo ]
272274 output_groups = dep [OutputGroupInfo ]
273275 return [cc_info , DefaultInfo (files = dep [ProtoCcFilesInfo ].files ), output_groups ]
@@ -280,9 +282,11 @@ cc_proto_library = rule(
280282 allow_rules = ["proto_library" ],
281283 allow_files = False ,
282284 ),
285+ } | toolchains .if_legacy_toolchain ({
283286 "_aspect_cc_proto_toolchain" : attr .label (
284287 default = configuration_field (fragment = "proto" , name = "proto_toolchain_for_cc" ),
285288 ),
286- },
289+ }) ,
287290 provides = [CcInfo ],
291+ toolchains = toolchains .use_toolchain (semantics .CC_PROTO_TOOLCHAIN ),
288292)
0 commit comments