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 // http://code.google.com/p/protobuf/ |
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. |
11 // * Redistributions in binary form must reproduce the above | 11 // * Redistributions in binary form must reproduce the above |
12 // copyright notice, this list of conditions and the following disclaimer | 12 // copyright notice, this list of conditions and the following disclaimer |
13 // in the documentation and/or other materials provided with the | 13 // in the documentation and/or other materials provided with the |
(...skipping 18 matching lines...) Expand all Loading... |
32 // Based on original Protocol Buffers design by | 32 // Based on original Protocol Buffers design by |
33 // Sanjay Ghemawat, Jeff Dean, and others. | 33 // Sanjay Ghemawat, Jeff Dean, and others. |
34 // | 34 // |
35 // Implements the Protocol Compiler front-end such that it may be reused by | 35 // Implements the Protocol Compiler front-end such that it may be reused by |
36 // custom compilers written to support other languages. | 36 // custom compilers written to support other languages. |
37 | 37 |
38 #ifndef GOOGLE_PROTOBUF_COMPILER_COMMAND_LINE_INTERFACE_H__ | 38 #ifndef GOOGLE_PROTOBUF_COMPILER_COMMAND_LINE_INTERFACE_H__ |
39 #define GOOGLE_PROTOBUF_COMPILER_COMMAND_LINE_INTERFACE_H__ | 39 #define GOOGLE_PROTOBUF_COMPILER_COMMAND_LINE_INTERFACE_H__ |
40 | 40 |
41 #include <google/protobuf/stubs/common.h> | 41 #include <google/protobuf/stubs/common.h> |
42 #include <google/protobuf/stubs/hash.h> | |
43 #include <string> | 42 #include <string> |
44 #include <vector> | 43 #include <vector> |
45 #include <map> | 44 #include <map> |
46 #include <set> | 45 #include <set> |
47 #include <utility> | 46 #include <utility> |
48 | 47 |
49 namespace google { | 48 namespace google { |
50 namespace protobuf { | 49 namespace protobuf { |
51 | 50 |
52 class Descriptor; // descriptor.h | 51 class FileDescriptor; // descriptor.h |
53 class DescriptorPool; // descriptor.h | 52 class DescriptorPool; // descriptor.h |
54 class FileDescriptor; // descriptor.h | |
55 class FileDescriptorProto; // descriptor.pb.h | 53 class FileDescriptorProto; // descriptor.pb.h |
56 template<typename T> class RepeatedPtrField; // repeated_field.h | 54 template<typename T> class RepeatedPtrField; // repeated_field.h |
57 | 55 |
58 namespace compiler { | 56 namespace compiler { |
59 | 57 |
60 class CodeGenerator; // code_generator.h | 58 class CodeGenerator; // code_generator.h |
61 class GeneratorContext; // code_generator.h | 59 class GeneratorContext; // code_generator.h |
62 class DiskSourceTree; // importer.h | 60 class DiskSourceTree; // importer.h |
63 | 61 |
64 // This class implements the command-line interface to the protocol compiler. | 62 // This class implements the command-line interface to the protocol compiler. |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
184 version_info_ = text; | 182 version_info_ = text; |
185 } | 183 } |
186 | 184 |
187 | 185 |
188 private: | 186 private: |
189 // ----------------------------------------------------------------- | 187 // ----------------------------------------------------------------- |
190 | 188 |
191 class ErrorPrinter; | 189 class ErrorPrinter; |
192 class GeneratorContextImpl; | 190 class GeneratorContextImpl; |
193 class MemoryOutputStream; | 191 class MemoryOutputStream; |
194 typedef hash_map<string, GeneratorContextImpl*> GeneratorContextMap; | |
195 | 192 |
196 // Clear state from previous Run(). | 193 // Clear state from previous Run(). |
197 void Clear(); | 194 void Clear(); |
198 | 195 |
199 // Remaps each file in input_files_ so that it is relative to one of the | 196 // Remaps each file in input_files_ so that it is relative to one of the |
200 // directories in proto_path_. Returns false if an error occurred. This | 197 // directories in proto_path_. Returns false if an error occurred. This |
201 // is only used if inputs_are_proto_path_relative_ is false. | 198 // is only used if inputs_are_proto_path_relative_ is false. |
202 bool MakeInputsBeProtoPathRelative( | 199 bool MakeInputsBeProtoPathRelative( |
203 DiskSourceTree* source_tree); | 200 DiskSourceTree* source_tree); |
204 | 201 |
205 // Return status for ParseArguments() and InterpretArgument(). | 202 // Return status for ParseArguments() and InterpretArgument(). |
206 enum ParseArgumentStatus { | 203 enum ParseArgumentStatus { |
207 PARSE_ARGUMENT_DONE_AND_CONTINUE, | 204 PARSE_ARGUMENT_DONE_AND_CONTINUE, |
208 PARSE_ARGUMENT_DONE_AND_EXIT, | 205 PARSE_ARGUMENT_DONE_AND_EXIT, |
209 PARSE_ARGUMENT_FAIL | 206 PARSE_ARGUMENT_FAIL |
210 }; | 207 }; |
211 | 208 |
212 // Parse all command-line arguments. | 209 // Parse all command-line arguments. |
213 ParseArgumentStatus ParseArguments(int argc, const char* const argv[]); | 210 ParseArgumentStatus ParseArguments(int argc, const char* const argv[]); |
214 | 211 |
215 // Parses a command-line argument into a name/value pair. Returns | 212 // Parses a command-line argument into a name/value pair. Returns |
216 // true if the next argument in the argv should be used as the value, | 213 // true if the next argument in the argv should be used as the value, |
217 // false otherwise. | 214 // false otherwise. |
218 // | 215 // |
219 // Examples: | 216 // Exmaples: |
220 // "-Isrc/protos" -> | 217 // "-Isrc/protos" -> |
221 // name = "-I", value = "src/protos" | 218 // name = "-I", value = "src/protos" |
222 // "--cpp_out=src/foo.pb2.cc" -> | 219 // "--cpp_out=src/foo.pb2.cc" -> |
223 // name = "--cpp_out", value = "src/foo.pb2.cc" | 220 // name = "--cpp_out", value = "src/foo.pb2.cc" |
224 // "foo.proto" -> | 221 // "foo.proto" -> |
225 // name = "", value = "foo.proto" | 222 // name = "", value = "foo.proto" |
226 bool ParseArgument(const char* arg, string* name, string* value); | 223 bool ParseArgument(const char* arg, string* name, string* value); |
227 | 224 |
228 // Interprets arguments parsed with ParseArgument. | 225 // Interprets arguments parsed with ParseArgument. |
229 ParseArgumentStatus InterpretArgument(const string& name, | 226 ParseArgumentStatus InterpretArgument(const string& name, |
(...skipping 12 matching lines...) Expand all Loading... |
242 const string& parameter, | 239 const string& parameter, |
243 GeneratorContext* generator_context, | 240 GeneratorContext* generator_context, |
244 string* error); | 241 string* error); |
245 | 242 |
246 // Implements --encode and --decode. | 243 // Implements --encode and --decode. |
247 bool EncodeOrDecode(const DescriptorPool* pool); | 244 bool EncodeOrDecode(const DescriptorPool* pool); |
248 | 245 |
249 // Implements the --descriptor_set_out option. | 246 // Implements the --descriptor_set_out option. |
250 bool WriteDescriptorSet(const vector<const FileDescriptor*> parsed_files); | 247 bool WriteDescriptorSet(const vector<const FileDescriptor*> parsed_files); |
251 | 248 |
252 // Implements the --dependency_out option | |
253 bool GenerateDependencyManifestFile( | |
254 const vector<const FileDescriptor*>& parsed_files, | |
255 const GeneratorContextMap& output_directories, | |
256 DiskSourceTree* source_tree); | |
257 | |
258 // Get all transitive dependencies of the given file (including the file | 249 // Get all transitive dependencies of the given file (including the file |
259 // itself), adding them to the given list of FileDescriptorProtos. The | 250 // itself), adding them to the given list of FileDescriptorProtos. The |
260 // protos will be ordered such that every file is listed before any file that | 251 // protos will be ordered such that every file is listed before any file that |
261 // depends on it, so that you can call DescriptorPool::BuildFile() on them | 252 // depends on it, so that you can call DescriptorPool::BuildFile() on them |
262 // in order. Any files in *already_seen will not be added, and each file | 253 // in order. Any files in *already_seen will not be added, and each file |
263 // added will be inserted into *already_seen. If include_source_code_info is | 254 // added will be inserted into *already_seen. If include_source_code_info is |
264 // true then include the source code information in the FileDescriptorProtos. | 255 // true then include the source code information in the FileDescriptorProtos. |
265 static void GetTransitiveDependencies( | 256 static void GetTransitiveDependencies( |
266 const FileDescriptor* file, | 257 const FileDescriptor* file, |
267 bool include_source_code_info, | 258 bool include_source_code_info, |
268 set<const FileDescriptor*>* already_seen, | 259 set<const FileDescriptor*>* already_seen, |
269 RepeatedPtrField<FileDescriptorProto>* output); | 260 RepeatedPtrField<FileDescriptorProto>* output); |
270 | 261 |
271 // Implements the --print_free_field_numbers. This function prints free field | |
272 // numbers into stdout for the message and it's nested message types in | |
273 // post-order, i.e. nested types first. Printed range are left-right | |
274 // inclusive, i.e. [a, b]. | |
275 // | |
276 // Groups: | |
277 // For historical reasons, groups are considered to share the same | |
278 // field number space with the parent message, thus it will not print free | |
279 // field numbers for groups. The field numbers used in the groups are | |
280 // excluded in the free field numbers of the parent message. | |
281 // | |
282 // Extension Ranges: | |
283 // Extension ranges are considered ocuppied field numbers and they will not be | |
284 // listed as free numbers in the output. | |
285 void PrintFreeFieldNumbers(const Descriptor* descriptor); | |
286 | |
287 // ----------------------------------------------------------------- | 262 // ----------------------------------------------------------------- |
288 | 263 |
289 // The name of the executable as invoked (i.e. argv[0]). | 264 // The name of the executable as invoked (i.e. argv[0]). |
290 string executable_name_; | 265 string executable_name_; |
291 | 266 |
292 // Version info set with SetVersionInfo(). | 267 // Version info set with SetVersionInfo(). |
293 string version_info_; | 268 string version_info_; |
294 | 269 |
295 // Registered generators. | 270 // Registered generators. |
296 struct GeneratorInfo { | 271 struct GeneratorInfo { |
(...skipping 16 matching lines...) Expand all Loading... |
313 | 288 |
314 // Maps specific plugin names to files. When executing a plugin, this map | 289 // Maps specific plugin names to files. When executing a plugin, this map |
315 // is searched first to find the plugin executable. If not found here, the | 290 // is searched first to find the plugin executable. If not found here, the |
316 // PATH (or other OS-specific search strategy) is searched. | 291 // PATH (or other OS-specific search strategy) is searched. |
317 map<string, string> plugins_; | 292 map<string, string> plugins_; |
318 | 293 |
319 // Stuff parsed from command line. | 294 // Stuff parsed from command line. |
320 enum Mode { | 295 enum Mode { |
321 MODE_COMPILE, // Normal mode: parse .proto files and compile them. | 296 MODE_COMPILE, // Normal mode: parse .proto files and compile them. |
322 MODE_ENCODE, // --encode: read text from stdin, write binary to stdout. | 297 MODE_ENCODE, // --encode: read text from stdin, write binary to stdout. |
323 MODE_DECODE, // --decode: read binary from stdin, write text to stdout. | 298 MODE_DECODE // --decode: read binary from stdin, write text to stdout. |
324 MODE_PRINT, // Print mode: print info of the given .proto files and exit. | |
325 }; | 299 }; |
326 | 300 |
327 Mode mode_; | 301 Mode mode_; |
328 | 302 |
329 enum PrintMode { | |
330 PRINT_NONE, // Not in MODE_PRINT | |
331 PRINT_FREE_FIELDS, // --print_free_fields | |
332 }; | |
333 | |
334 PrintMode print_mode_; | |
335 | |
336 enum ErrorFormat { | 303 enum ErrorFormat { |
337 ERROR_FORMAT_GCC, // GCC error output format (default). | 304 ERROR_FORMAT_GCC, // GCC error output format (default). |
338 ERROR_FORMAT_MSVS // Visual Studio output (--error_format=msvs). | 305 ERROR_FORMAT_MSVS // Visual Studio output (--error_format=msvs). |
339 }; | 306 }; |
340 | 307 |
341 ErrorFormat error_format_; | 308 ErrorFormat error_format_; |
342 | 309 |
343 vector<pair<string, string> > proto_path_; // Search path for proto files. | 310 vector<pair<string, string> > proto_path_; // Search path for proto files. |
344 vector<string> input_files_; // Names of the input proto files. | 311 vector<string> input_files_; // Names of the input proto files. |
345 | 312 |
346 // output_directives_ lists all the files we are supposed to output and what | 313 // output_directives_ lists all the files we are supposed to output and what |
347 // generator to use for each. | 314 // generator to use for each. |
348 struct OutputDirective { | 315 struct OutputDirective { |
349 string name; // E.g. "--foo_out" | 316 string name; // E.g. "--foo_out" |
350 CodeGenerator* generator; // NULL for plugins | 317 CodeGenerator* generator; // NULL for plugins |
351 string parameter; | 318 string parameter; |
352 string output_location; | 319 string output_location; |
353 }; | 320 }; |
354 vector<OutputDirective> output_directives_; | 321 vector<OutputDirective> output_directives_; |
355 | 322 |
356 // When using --encode or --decode, this names the type we are encoding or | 323 // When using --encode or --decode, this names the type we are encoding or |
357 // decoding. (Empty string indicates --decode_raw.) | 324 // decoding. (Empty string indicates --decode_raw.) |
358 string codec_type_; | 325 string codec_type_; |
359 | 326 |
360 // If --descriptor_set_out was given, this is the filename to which the | 327 // If --descriptor_set_out was given, this is the filename to which the |
361 // FileDescriptorSet should be written. Otherwise, empty. | 328 // FileDescriptorSet should be written. Otherwise, empty. |
362 string descriptor_set_name_; | 329 string descriptor_set_name_; |
363 | 330 |
364 // If --dependency_out was given, this is the path to the file where the | |
365 // dependency file will be written. Otherwise, empty. | |
366 string dependency_out_name_; | |
367 | |
368 // True if --include_imports was given, meaning that we should | 331 // True if --include_imports was given, meaning that we should |
369 // write all transitive dependencies to the DescriptorSet. Otherwise, only | 332 // write all transitive dependencies to the DescriptorSet. Otherwise, only |
370 // the .proto files listed on the command-line are added. | 333 // the .proto files listed on the command-line are added. |
371 bool imports_in_descriptor_set_; | 334 bool imports_in_descriptor_set_; |
372 | 335 |
373 // True if --include_source_info was given, meaning that we should not strip | 336 // True if --include_source_info was given, meaning that we should not strip |
374 // SourceCodeInfo from the DescriptorSet. | 337 // SourceCodeInfo from the DescriptorSet. |
375 bool source_info_in_descriptor_set_; | 338 bool source_info_in_descriptor_set_; |
376 | 339 |
377 // Was the --disallow_services flag used? | 340 // Was the --disallow_services flag used? |
378 bool disallow_services_; | 341 bool disallow_services_; |
379 | 342 |
380 // See SetInputsAreProtoPathRelative(). | 343 // See SetInputsAreProtoPathRelative(). |
381 bool inputs_are_proto_path_relative_; | 344 bool inputs_are_proto_path_relative_; |
382 | 345 |
383 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CommandLineInterface); | 346 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CommandLineInterface); |
384 }; | 347 }; |
385 | 348 |
386 } // namespace compiler | 349 } // namespace compiler |
387 } // namespace protobuf | 350 } // namespace protobuf |
388 | 351 |
389 } // namespace google | 352 } // namespace google |
390 #endif // GOOGLE_PROTOBUF_COMPILER_COMMAND_LINE_INTERFACE_H__ | 353 #endif // GOOGLE_PROTOBUF_COMPILER_COMMAND_LINE_INTERFACE_H__ |
OLD | NEW |