@@ -88,6 +88,87 @@ def _proto_path_flag(path):
8888def _Iimport_path_equals_fullpath (proto_source ):
8989 return "-I%s=%s" % (proto_source .import_path (), proto_source .source_file ().path )
9090
91+ def _compile (
92+ actions ,
93+ proto_library_target ,
94+ proto_lang_toolchain_info ,
95+ generated_files ,
96+ plugin_output = None ,
97+ additional_args = None ,
98+ additional_tools = [],
99+ additional_inputs = depset (),
100+ resource_set = None ):
101+ """Creates proto compile action for compiling *.proto files to language specific sources.
102+
103+ Args:
104+ actions: (ActionFactory) Obtained by ctx.actions, used to register the actions.
105+ proto_library_target: (Target) The proto_library to generate the sources for.
106+ Obtained as the `target` parameter from an aspect's implementation.
107+ proto_lang_toolchain_info: (ProtoLangToolchainInfo) The proto lang toolchain info.
108+ Obtained from a `proto_lang_toolchain` target or constructed ad-hoc..
109+ generated_files: (list[File]) The output files generated by the proto compiler.
110+ Callee needs to declare files using `ctx.actions.declare_file`.
111+ See also: `proto_common.declare_generated_files`.
112+ plugin_output: (File|str) The file or directory passed to the plugin.
113+ Formatted with `proto_lang_toolchain.out_replacement_format_flag`
114+ additional_args: (Args) Additional arguments to add to the action.
115+ Accepts an ctx.actions.args() object that is added at the beginning
116+ of the command line.
117+ additional_tools: (list[File]) Additional tools to add to the action.
118+ additional_inputs: (Depset[File]) Additional input files to add to the action.
119+ resource_set:
120+ (func) A callback function that is passed to the created action.
121+ See `ctx.actions.run`, `resource_set` parameter for full definition of
122+ the callback.
123+ """
124+ proto_info = proto_library_target [_builtins .toplevel .ProtoInfo ]
125+
126+ args = actions .args ()
127+ args .use_param_file (param_file_arg = "@%s" )
128+ args .set_param_file_format ("multiline" )
129+ tools = list (additional_tools )
130+
131+ if plugin_output :
132+ args .add (plugin_output , format = proto_lang_toolchain_info .out_replacement_format_flag )
133+ if proto_lang_toolchain_info .plugin :
134+ tools .append (proto_lang_toolchain_info .plugin )
135+ args .add (proto_lang_toolchain_info .plugin .executable , format = proto_lang_toolchain_info .plugin_format_flag )
136+
137+ args .add_all (proto_info .transitive_proto_path , map_each = _proto_path_flag )
138+ # Example: `--proto_path=--proto_path=bazel-bin/target/third_party/pkg/_virtual_imports/subpkg`
139+
140+ args .add_all (proto_lang_toolchain_info .protoc_opts )
141+
142+ # Include maps
143+ # For each import, include both the import as well as the import relativized against its
144+ # protoSourceRoot. This ensures that protos can reference either the full path or the short
145+ # path when including other protos.
146+ args .add_all (proto_info .transitive_proto_sources (), map_each = _Iimport_path_equals_fullpath )
147+ # Example: `-Ia.proto=bazel-bin/target/third_party/pkg/_virtual_imports/subpkg/a.proto`
148+
149+ args .add_all (proto_info .direct_sources )
150+
151+ if additional_args :
152+ additional_args .use_param_file (param_file_arg = "@%s" )
153+ additional_args .set_param_file_format ("multiline" )
154+
155+ actions .run (
156+ mnemonic = proto_lang_toolchain_info .mnemonic ,
157+ progress_message = proto_lang_toolchain_info .progress_message ,
158+ executable = proto_lang_toolchain_info .proto_compiler ,
159+ arguments = [additional_args , args ] if additional_args else [args ],
160+ inputs = depset (transitive = [proto_info .transitive_sources , additional_inputs ]),
161+ outputs = generated_files ,
162+ tools = tools ,
163+ use_default_shell_env = True ,
164+ resource_set = resource_set ,
165+ )
166+
91167proto_common = struct (
92168 create_proto_compile_action = _create_proto_compile_action ,
93169)
170+
171+ proto_common_do_not_use = struct (
172+ compile = _compile ,
173+ ProtoLangToolchainInfo = _builtins .internal .ProtoLangToolchainInfo ,
174+ )
0 commit comments