OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 | 48 |
49 namespace google { | 49 namespace google { |
50 namespace protobuf { | 50 namespace protobuf { |
51 | 51 |
52 class Descriptor; // descriptor.h | 52 class Descriptor; // descriptor.h |
53 class DescriptorPool; // descriptor.h | 53 class DescriptorPool; // descriptor.h |
54 class FileDescriptor; // descriptor.h | 54 class FileDescriptor; // descriptor.h |
55 class FileDescriptorProto; // descriptor.pb.h | 55 class FileDescriptorProto; // descriptor.pb.h |
56 template<typename T> class RepeatedPtrField; // repeated_field.h | 56 template<typename T> class RepeatedPtrField; // repeated_field.h |
57 | 57 |
58 } // namespace protobuf | |
59 | |
60 namespace protobuf { | |
61 namespace compiler { | 58 namespace compiler { |
62 | 59 |
63 class CodeGenerator; // code_generator.h | 60 class CodeGenerator; // code_generator.h |
64 class GeneratorContext; // code_generator.h | 61 class GeneratorContext; // code_generator.h |
65 class DiskSourceTree; // importer.h | 62 class DiskSourceTree; // importer.h |
66 | 63 |
67 // This class implements the command-line interface to the protocol compiler. | 64 // This class implements the command-line interface to the protocol compiler. |
68 // It is designed to make it very easy to create a custom protocol compiler | 65 // It is designed to make it very easy to create a custom protocol compiler |
69 // supporting the languages of your choice. For example, if you wanted to | 66 // supporting the languages of your choice. For example, if you wanted to |
70 // create a custom protocol compiler binary which includes both the regular | 67 // create a custom protocol compiler binary which includes both the regular |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 // | 134 // |
138 // The compiler determines the executable name to search for by concatenating | 135 // The compiler determines the executable name to search for by concatenating |
139 // exe_name_prefix with the unrecognized flag name, removing "_out". So, for | 136 // exe_name_prefix with the unrecognized flag name, removing "_out". So, for |
140 // example, if exe_name_prefix is "protoc-" and you pass the flag --foo_out, | 137 // example, if exe_name_prefix is "protoc-" and you pass the flag --foo_out, |
141 // the compiler will try to run the program "protoc-foo". | 138 // the compiler will try to run the program "protoc-foo". |
142 // | 139 // |
143 // The plugin program should implement the following usage: | 140 // The plugin program should implement the following usage: |
144 // plugin [--out=OUTDIR] [--parameter=PARAMETER] PROTO_FILES < DESCRIPTORS | 141 // plugin [--out=OUTDIR] [--parameter=PARAMETER] PROTO_FILES < DESCRIPTORS |
145 // --out indicates the output directory (as passed to the --foo_out | 142 // --out indicates the output directory (as passed to the --foo_out |
146 // parameter); if omitted, the current directory should be used. --parameter | 143 // parameter); if omitted, the current directory should be used. --parameter |
147 // gives the generator parameter, if any was provided (see below). The | 144 // gives the generator parameter, if any was provided. The PROTO_FILES list |
148 // PROTO_FILES list the .proto files which were given on the compiler | 145 // the .proto files which were given on the compiler command-line; these are |
149 // command-line; these are the files for which the plugin is expected to | 146 // the files for which the plugin is expected to generate output code. |
150 // generate output code. Finally, DESCRIPTORS is an encoded FileDescriptorSet | 147 // Finally, DESCRIPTORS is an encoded FileDescriptorSet (as defined in |
151 // (as defined in descriptor.proto). This is piped to the plugin's stdin. | 148 // descriptor.proto). This is piped to the plugin's stdin. The set will |
152 // The set will include descriptors for all the files listed in PROTO_FILES as | 149 // include descriptors for all the files listed in PROTO_FILES as well as |
153 // well as all files that they import. The plugin MUST NOT attempt to read | 150 // all files that they import. The plugin MUST NOT attempt to read the |
154 // the PROTO_FILES directly -- it must use the FileDescriptorSet. | 151 // PROTO_FILES directly -- it must use the FileDescriptorSet. |
155 // | 152 // |
156 // The plugin should generate whatever files are necessary, as code generators | 153 // The plugin should generate whatever files are necessary, as code generators |
157 // normally do. It should write the names of all files it generates to | 154 // normally do. It should write the names of all files it generates to |
158 // stdout. The names should be relative to the output directory, NOT absolute | 155 // stdout. The names should be relative to the output directory, NOT absolute |
159 // names or relative to the current directory. If any errors occur, error | 156 // names or relative to the current directory. If any errors occur, error |
160 // messages should be written to stderr. If an error is fatal, the plugin | 157 // messages should be written to stderr. If an error is fatal, the plugin |
161 // should exit with a non-zero exit code. | 158 // should exit with a non-zero exit code. |
162 // | |
163 // Plugins can have generator parameters similar to normal built-in | |
164 // generators. Extra generator parameters can be passed in via a matching | |
165 // "_opt" parameter. For example: | |
166 // protoc --plug_out=enable_bar:outdir --plug_opt=enable_baz | |
167 // This will pass "enable_bar,enable_baz" as the parameter to the plugin. | |
168 // | |
169 void AllowPlugins(const string& exe_name_prefix); | 159 void AllowPlugins(const string& exe_name_prefix); |
170 | 160 |
171 // Run the Protocol Compiler with the given command-line parameters. | 161 // Run the Protocol Compiler with the given command-line parameters. |
172 // Returns the error code which should be returned by main(). | 162 // Returns the error code which should be returned by main(). |
173 // | 163 // |
174 // It may not be safe to call Run() in a multi-threaded environment because | 164 // It may not be safe to call Run() in a multi-threaded environment because |
175 // it calls strerror(). I'm not sure why you'd want to do this anyway. | 165 // it calls strerror(). I'm not sure why you'd want to do this anyway. |
176 int Run(int argc, const char* const argv[]); | 166 int Run(int argc, const char* const argv[]); |
177 | 167 |
178 // Call SetInputsAreCwdRelative(true) if the input files given on the command | 168 // Call SetInputsAreCwdRelative(true) if the input files given on the command |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 | 228 |
239 // Interprets arguments parsed with ParseArgument. | 229 // Interprets arguments parsed with ParseArgument. |
240 ParseArgumentStatus InterpretArgument(const string& name, | 230 ParseArgumentStatus InterpretArgument(const string& name, |
241 const string& value); | 231 const string& value); |
242 | 232 |
243 // Print the --help text to stderr. | 233 // Print the --help text to stderr. |
244 void PrintHelpText(); | 234 void PrintHelpText(); |
245 | 235 |
246 // Generate the given output file from the given input. | 236 // Generate the given output file from the given input. |
247 struct OutputDirective; // see below | 237 struct OutputDirective; // see below |
248 bool GenerateOutput(const std::vector<const FileDescriptor*>& parsed_files, | 238 bool GenerateOutput(const vector<const FileDescriptor*>& parsed_files, |
249 const OutputDirective& output_directive, | 239 const OutputDirective& output_directive, |
250 GeneratorContext* generator_context); | 240 GeneratorContext* generator_context); |
251 bool GeneratePluginOutput( | 241 bool GeneratePluginOutput(const vector<const FileDescriptor*>& parsed_files, |
252 const std::vector<const FileDescriptor*>& parsed_files, | 242 const string& plugin_name, |
253 const string& plugin_name, const string& parameter, | 243 const string& parameter, |
254 GeneratorContext* generator_context, string* error); | 244 GeneratorContext* generator_context, |
| 245 string* error); |
255 | 246 |
256 // Implements --encode and --decode. | 247 // Implements --encode and --decode. |
257 bool EncodeOrDecode(const DescriptorPool* pool); | 248 bool EncodeOrDecode(const DescriptorPool* pool); |
258 | 249 |
259 // Implements the --descriptor_set_out option. | 250 // Implements the --descriptor_set_out option. |
260 bool WriteDescriptorSet( | 251 bool WriteDescriptorSet(const vector<const FileDescriptor*> parsed_files); |
261 const std::vector<const FileDescriptor*> parsed_files); | |
262 | 252 |
263 // Implements the --dependency_out option | 253 // Implements the --dependency_out option |
264 bool GenerateDependencyManifestFile( | 254 bool GenerateDependencyManifestFile( |
265 const std::vector<const FileDescriptor*>& parsed_files, | 255 const vector<const FileDescriptor*>& parsed_files, |
266 const GeneratorContextMap& output_directories, | 256 const GeneratorContextMap& output_directories, |
267 DiskSourceTree* source_tree); | 257 DiskSourceTree* source_tree); |
268 | 258 |
269 // Get all transitive dependencies of the given file (including the file | 259 // Get all transitive dependencies of the given file (including the file |
270 // itself), adding them to the given list of FileDescriptorProtos. The | 260 // itself), adding them to the given list of FileDescriptorProtos. The |
271 // protos will be ordered such that every file is listed before any file that | 261 // protos will be ordered such that every file is listed before any file that |
272 // depends on it, so that you can call DescriptorPool::BuildFile() on them | 262 // depends on it, so that you can call DescriptorPool::BuildFile() on them |
273 // in order. Any files in *already_seen will not be added, and each file | 263 // in order. Any files in *already_seen will not be added, and each file |
274 // added will be inserted into *already_seen. If include_source_code_info is | 264 // added will be inserted into *already_seen. If include_source_code_info is |
275 // true then include the source code information in the FileDescriptorProtos. | 265 // true then include the source code information in the FileDescriptorProtos. |
276 // If include_json_name is true, populate the json_name field of | 266 // If include_json_name is true, populate the json_name field of |
277 // FieldDescriptorProto for all fields. | 267 // FieldDescriptorProto for all fields. |
278 static void GetTransitiveDependencies( | 268 static void GetTransitiveDependencies( |
279 const FileDescriptor* file, | 269 const FileDescriptor* file, |
280 bool include_json_name, | 270 bool include_json_name, |
281 bool include_source_code_info, | 271 bool include_source_code_info, |
282 std::set<const FileDescriptor*>* already_seen, | 272 set<const FileDescriptor*>* already_seen, |
283 RepeatedPtrField<FileDescriptorProto>* output); | 273 RepeatedPtrField<FileDescriptorProto>* output); |
284 | 274 |
285 // Implements the --print_free_field_numbers. This function prints free field | 275 // Implements the --print_free_field_numbers. This function prints free field |
286 // numbers into stdout for the message and it's nested message types in | 276 // numbers into stdout for the message and it's nested message types in |
287 // post-order, i.e. nested types first. Printed range are left-right | 277 // post-order, i.e. nested types first. Printed range are left-right |
288 // inclusive, i.e. [a, b]. | 278 // inclusive, i.e. [a, b]. |
289 // | 279 // |
290 // Groups: | 280 // Groups: |
291 // For historical reasons, groups are considered to share the same | 281 // For historical reasons, groups are considered to share the same |
292 // field number space with the parent message, thus it will not print free | 282 // field number space with the parent message, thus it will not print free |
(...skipping 13 matching lines...) Expand all Loading... |
306 // Version info set with SetVersionInfo(). | 296 // Version info set with SetVersionInfo(). |
307 string version_info_; | 297 string version_info_; |
308 | 298 |
309 // Registered generators. | 299 // Registered generators. |
310 struct GeneratorInfo { | 300 struct GeneratorInfo { |
311 string flag_name; | 301 string flag_name; |
312 string option_flag_name; | 302 string option_flag_name; |
313 CodeGenerator* generator; | 303 CodeGenerator* generator; |
314 string help_text; | 304 string help_text; |
315 }; | 305 }; |
316 typedef std::map<string, GeneratorInfo> GeneratorMap; | 306 typedef map<string, GeneratorInfo> GeneratorMap; |
317 GeneratorMap generators_by_flag_name_; | 307 GeneratorMap generators_by_flag_name_; |
318 GeneratorMap generators_by_option_name_; | 308 GeneratorMap generators_by_option_name_; |
319 // A map from generator names to the parameters specified using the option | 309 // A map from generator names to the parameters specified using the option |
320 // flag. For example, if the user invokes the compiler with: | 310 // flag. For example, if the user invokes the compiler with: |
321 // protoc --foo_out=outputdir --foo_opt=enable_bar ... | 311 // protoc --foo_out=outputdir --foo_opt=enable_bar ... |
322 // Then there will be an entry ("--foo_out", "enable_bar") in this map. | 312 // Then there will be an entry ("--foo_out", "enable_bar") in this map. |
323 std::map<string, string> generator_parameters_; | 313 map<string, string> generator_parameters_; |
324 // Similar to generator_parameters_, but stores the parameters for plugins. | |
325 std::map<string, string> plugin_parameters_; | |
326 | 314 |
327 // See AllowPlugins(). If this is empty, plugins aren't allowed. | 315 // See AllowPlugins(). If this is empty, plugins aren't allowed. |
328 string plugin_prefix_; | 316 string plugin_prefix_; |
329 | 317 |
330 // Maps specific plugin names to files. When executing a plugin, this map | 318 // Maps specific plugin names to files. When executing a plugin, this map |
331 // is searched first to find the plugin executable. If not found here, the | 319 // is searched first to find the plugin executable. If not found here, the |
332 // PATH (or other OS-specific search strategy) is searched. | 320 // PATH (or other OS-specific search strategy) is searched. |
333 std::map<string, string> plugins_; | 321 map<string, string> plugins_; |
334 | 322 |
335 // Stuff parsed from command line. | 323 // Stuff parsed from command line. |
336 enum Mode { | 324 enum Mode { |
337 MODE_COMPILE, // Normal mode: parse .proto files and compile them. | 325 MODE_COMPILE, // Normal mode: parse .proto files and compile them. |
338 MODE_ENCODE, // --encode: read text from stdin, write binary to stdout. | 326 MODE_ENCODE, // --encode: read text from stdin, write binary to stdout. |
339 MODE_DECODE, // --decode: read binary from stdin, write text to stdout. | 327 MODE_DECODE, // --decode: read binary from stdin, write text to stdout. |
340 MODE_PRINT, // Print mode: print info of the given .proto files and exit. | 328 MODE_PRINT, // Print mode: print info of the given .proto files and exit. |
341 }; | 329 }; |
342 | 330 |
343 Mode mode_; | 331 Mode mode_; |
344 | 332 |
345 enum PrintMode { | 333 enum PrintMode { |
346 PRINT_NONE, // Not in MODE_PRINT | 334 PRINT_NONE, // Not in MODE_PRINT |
347 PRINT_FREE_FIELDS, // --print_free_fields | 335 PRINT_FREE_FIELDS, // --print_free_fields |
348 }; | 336 }; |
349 | 337 |
350 PrintMode print_mode_; | 338 PrintMode print_mode_; |
351 | 339 |
352 enum ErrorFormat { | 340 enum ErrorFormat { |
353 ERROR_FORMAT_GCC, // GCC error output format (default). | 341 ERROR_FORMAT_GCC, // GCC error output format (default). |
354 ERROR_FORMAT_MSVS // Visual Studio output (--error_format=msvs). | 342 ERROR_FORMAT_MSVS // Visual Studio output (--error_format=msvs). |
355 }; | 343 }; |
356 | 344 |
357 ErrorFormat error_format_; | 345 ErrorFormat error_format_; |
358 | 346 |
359 std::vector<std::pair<string, string> > | 347 vector<pair<string, string> > proto_path_; // Search path for proto files. |
360 proto_path_; // Search path for proto files. | 348 vector<string> input_files_; // Names of the input proto files. |
361 std::vector<string> input_files_; // Names of the input proto files. | |
362 | |
363 // Names of proto files which are allowed to be imported. Used by build | |
364 // systems to enforce depend-on-what-you-import. | |
365 std::set<string> direct_dependencies_; | |
366 bool direct_dependencies_explicitly_set_; | |
367 | 349 |
368 // output_directives_ lists all the files we are supposed to output and what | 350 // output_directives_ lists all the files we are supposed to output and what |
369 // generator to use for each. | 351 // generator to use for each. |
370 struct OutputDirective { | 352 struct OutputDirective { |
371 string name; // E.g. "--foo_out" | 353 string name; // E.g. "--foo_out" |
372 CodeGenerator* generator; // NULL for plugins | 354 CodeGenerator* generator; // NULL for plugins |
373 string parameter; | 355 string parameter; |
374 string output_location; | 356 string output_location; |
375 }; | 357 }; |
376 std::vector<OutputDirective> output_directives_; | 358 vector<OutputDirective> output_directives_; |
377 | 359 |
378 // When using --encode or --decode, this names the type we are encoding or | 360 // When using --encode or --decode, this names the type we are encoding or |
379 // decoding. (Empty string indicates --decode_raw.) | 361 // decoding. (Empty string indicates --decode_raw.) |
380 string codec_type_; | 362 string codec_type_; |
381 | 363 |
382 // If --descriptor_set_out was given, this is the filename to which the | 364 // If --descriptor_set_out was given, this is the filename to which the |
383 // FileDescriptorSet should be written. Otherwise, empty. | 365 // FileDescriptorSet should be written. Otherwise, empty. |
384 string descriptor_set_name_; | 366 string descriptor_set_name_; |
385 | 367 |
386 // If --dependency_out was given, this is the path to the file where the | 368 // If --dependency_out was given, this is the path to the file where the |
(...skipping 16 matching lines...) Expand all Loading... |
403 bool inputs_are_proto_path_relative_; | 385 bool inputs_are_proto_path_relative_; |
404 | 386 |
405 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CommandLineInterface); | 387 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CommandLineInterface); |
406 }; | 388 }; |
407 | 389 |
408 } // namespace compiler | 390 } // namespace compiler |
409 } // namespace protobuf | 391 } // namespace protobuf |
410 | 392 |
411 } // namespace google | 393 } // namespace google |
412 #endif // GOOGLE_PROTOBUF_COMPILER_COMMAND_LINE_INTERFACE_H__ | 394 #endif // GOOGLE_PROTOBUF_COMPILER_COMMAND_LINE_INTERFACE_H__ |
OLD | NEW |