@@ -18,18 +18,39 @@ load("//protoc/private:prebuilt_protoc_toolchain.bzl", "prebuilt_protoc_repo")
1818load ("//protoc/private:protoc_toolchains.bzl" , "protoc_toolchains_repo" )
1919load ("//protoc/private:versions.bzl" , "PROTOC_PLATFORMS" )
2020
21- def protoc_toolchains (name , version , register = True ):
21+ def _google_protobuf_alias_repo_impl (rctx ):
22+ rctx .file ("BUILD" , """package(default_visibility=["//visibility:public"])
23+ alias(name = "any_proto", actual = "@{0}//:any_proto")
24+ """ .format (rctx .attr .alias_to ))
25+
26+ _google_protobuf_alias_repo = repository_rule (_google_protobuf_alias_repo_impl , attrs = {"alias_to" : attr .string ()})
27+
28+ def protoc_toolchains (name , version , google_protobuf = None , alias_to = "osx-aarch_64" , register = True ):
2229 """A utility method to load all Protobuf toolchains.
2330
2431 Args:
2532 name: base name for generated repositories, allowing multiple protoc versions.
2633 version: a release tag from protocolbuffers/protobuf, e.g. 'v25.3'
34+ google_protobuf: a repository to expose the google.protobuf package, providing the well-known-types.
35+ alias_to: a platform whose download of protoc.zip will become eager, as it will be used to back aliases for the
36+ google_protobuf repo. We cannot rely on toolchain resolution because that doesn't give a way for a label
37+ to reference one of the concrete toolchain targets. We don't want to use select() because that makes
38+ cquery eager-fetch ALL platforms.
2739 register: whether to register the resulting toolchains.
2840 Should be True for WORKSPACE and False under bzlmod.
2941 """
3042
3143 for platform in PROTOC_PLATFORMS .keys ():
32- prebuilt_protoc_repo (name = "." .join ([name , platform ]), platform = platform , version = version )
44+ prebuilt_protoc_repo (
45+ # We must replace hyphen with underscore to workaround rules_python
46+ # File "/output-base/external/rules_python~override/python/private/proto/py_proto_library.bzl", line 62, column 17, in _py_proto_aspect_impl
47+ # Error in fail: Cannot generate Python code for a .proto whose path contains '-'
48+ # (external/_main~protoc~toolchains_protoc_hub.osx-aarch_64/include/google/protobuf/any.proto).
49+ name = "." .join ([name , platform .replace ("-" , "_" )]),
50+ platform = platform ,
51+ version = version )
3352 protoc_toolchains_repo (name = name , user_repository_name = name )
3453 if register :
3554 native .register_toolchains ("@{}//:all" .format (name ))
55+ if google_protobuf :
56+ _google_protobuf_alias_repo (name = google_protobuf , alias_to = "." .join ([name , alias_to .replace ("-" , "_" )]))
0 commit comments