1
+ load ("@rules_proto//proto:defs.bzl" , "ProtoInfo" )
2
+
1
3
# Borrowed from https://github.com/grpc/grpc-java/blob/v1.24.1/java_grpc_library.bzl#L61
2
4
def _path_ignoring_repository (f ):
3
5
# Bazel creates a _virtual_imports directory in case the .proto source files
@@ -43,16 +45,24 @@ def api_proto_plugin_impl(target, ctx, output_group, mnemonic, output_suffixes):
43
45
output_suffix ) for f in proto_sources ]
44
46
45
47
# Create the protoc command-line args.
48
+ inputs = target [ProtoInfo ].transitive_sources
46
49
ctx_path = ctx .label .package + "/" + ctx .label .name
47
50
output_path = outputs [0 ].root .path + "/" + outputs [0 ].owner .workspace_root + "/" + ctx_path
48
51
args = ["-I./" + ctx .label .workspace_root ]
49
52
args += ["-I" + import_path for import_path in import_paths ]
50
53
args += ["--plugin=protoc-gen-api_proto_plugin=" + ctx .executable ._api_proto_plugin .path , "--api_proto_plugin_out=" + output_path ]
54
+ if hasattr (ctx .attr , "_type_db" ):
55
+ inputs = depset (transitive = [inputs ] + [ctx .attr ._type_db .files ])
56
+ if len (ctx .attr ._type_db .files .to_list ()) != 1 :
57
+ fail ("{} must have one type database file" .format (ctx .attr ._type_db ))
58
+ args += ["--api_proto_plugin_opt=type_db_path=" + ctx .attr ._type_db .files .to_list ()[0 ].path ]
51
59
args += [src .path for src in target [ProtoInfo ].direct_sources ]
60
+ env = {}
61
+
52
62
ctx .actions .run (
53
63
executable = ctx .executable ._protoc ,
54
64
arguments = args ,
55
- inputs = target [ ProtoInfo ]. transitive_sources ,
65
+ inputs = inputs ,
56
66
tools = [ctx .executable ._api_proto_plugin ],
57
67
outputs = outputs ,
58
68
mnemonic = mnemonic ,
@@ -62,20 +72,25 @@ def api_proto_plugin_impl(target, ctx, output_group, mnemonic, output_suffixes):
62
72
transitive_outputs = depset (outputs , transitive = [transitive_outputs ])
63
73
return [OutputGroupInfo (** {output_group : transitive_outputs })]
64
74
65
- def api_proto_plugin_aspect (tool_label , aspect_impl ):
75
+ def api_proto_plugin_aspect (tool_label , aspect_impl , use_type_db = False ):
76
+ _attrs = {
77
+ "_protoc" : attr .label (
78
+ default = Label ("@com_google_protobuf//:protoc" ),
79
+ executable = True ,
80
+ cfg = "exec" ,
81
+ ),
82
+ "_api_proto_plugin" : attr .label (
83
+ default = Label (tool_label ),
84
+ executable = True ,
85
+ cfg = "exec" ,
86
+ ),
87
+ }
88
+ if use_type_db :
89
+ _attrs ["_type_db" ] = attr .label (
90
+ default = Label ("@envoy//tools/api_proto_plugin:default_type_db" ),
91
+ )
66
92
return aspect (
67
93
attr_aspects = ["deps" ],
68
- attrs = {
69
- "_protoc" : attr .label (
70
- default = Label ("@com_google_protobuf//:protoc" ),
71
- executable = True ,
72
- cfg = "exec" ,
73
- ),
74
- "_api_proto_plugin" : attr .label (
75
- default = Label (tool_label ),
76
- executable = True ,
77
- cfg = "exec" ,
78
- ),
79
- },
94
+ attrs = _attrs ,
80
95
implementation = aspect_impl ,
81
96
)
0 commit comments