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