/thirdparty/breakpad/third_party/protobuf/protobuf/src/google/protobuf/compiler/command_line_interface.h

http://github.com/tomahawk-player/tomahawk · C++ Header · 318 lines · 100 code · 46 blank · 172 comment · 0 complexity · 025a14a72030dcac6bfb8ac546a4b0e0 MD5 · raw file

  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // http://code.google.com/p/protobuf/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // Author: kenton@google.com (Kenton Varda)
  31. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. //
  34. // Implements the Protocol Compiler front-end such that it may be reused by
  35. // custom compilers written to support other languages.
  36. #ifndef GOOGLE_PROTOBUF_COMPILER_COMMAND_LINE_INTERFACE_H__
  37. #define GOOGLE_PROTOBUF_COMPILER_COMMAND_LINE_INTERFACE_H__
  38. #include <google/protobuf/stubs/common.h>
  39. #include <string>
  40. #include <vector>
  41. #include <map>
  42. #include <set>
  43. #include <utility>
  44. namespace google {
  45. namespace protobuf {
  46. class FileDescriptor; // descriptor.h
  47. class DescriptorPool; // descriptor.h
  48. class FileDescriptorProto; // descriptor.pb.h
  49. template<typename T> class RepeatedPtrField; // repeated_field.h
  50. namespace compiler {
  51. class CodeGenerator; // code_generator.h
  52. class GeneratorContext; // code_generator.h
  53. class DiskSourceTree; // importer.h
  54. // This class implements the command-line interface to the protocol compiler.
  55. // It is designed to make it very easy to create a custom protocol compiler
  56. // supporting the languages of your choice. For example, if you wanted to
  57. // create a custom protocol compiler binary which includes both the regular
  58. // C++ support plus support for your own custom output "Foo", you would
  59. // write a class "FooGenerator" which implements the CodeGenerator interface,
  60. // then write a main() procedure like this:
  61. //
  62. // int main(int argc, char* argv[]) {
  63. // google::protobuf::compiler::CommandLineInterface cli;
  64. //
  65. // // Support generation of C++ source and headers.
  66. // google::protobuf::compiler::cpp::CppGenerator cpp_generator;
  67. // cli.RegisterGenerator("--cpp_out", &cpp_generator,
  68. // "Generate C++ source and header.");
  69. //
  70. // // Support generation of Foo code.
  71. // FooGenerator foo_generator;
  72. // cli.RegisterGenerator("--foo_out", &foo_generator,
  73. // "Generate Foo file.");
  74. //
  75. // return cli.Run(argc, argv);
  76. // }
  77. //
  78. // The compiler is invoked with syntax like:
  79. // protoc --cpp_out=outdir --foo_out=outdir --proto_path=src src/foo.proto
  80. //
  81. // For a full description of the command-line syntax, invoke it with --help.
  82. class LIBPROTOC_EXPORT CommandLineInterface {
  83. public:
  84. CommandLineInterface();
  85. ~CommandLineInterface();
  86. // Register a code generator for a language.
  87. //
  88. // Parameters:
  89. // * flag_name: The command-line flag used to specify an output file of
  90. // this type. The name must start with a '-'. If the name is longer
  91. // than one letter, it must start with two '-'s.
  92. // * generator: The CodeGenerator which will be called to generate files
  93. // of this type.
  94. // * help_text: Text describing this flag in the --help output.
  95. //
  96. // Some generators accept extra parameters. You can specify this parameter
  97. // on the command-line by placing it before the output directory, separated
  98. // by a colon:
  99. // protoc --foo_out=enable_bar:outdir
  100. // The text before the colon is passed to CodeGenerator::Generate() as the
  101. // "parameter".
  102. void RegisterGenerator(const string& flag_name,
  103. CodeGenerator* generator,
  104. const string& help_text);
  105. // Enables "plugins". In this mode, if a command-line flag ends with "_out"
  106. // but does not match any registered generator, the compiler will attempt to
  107. // find a "plugin" to implement the generator. Plugins are just executables.
  108. // They should live somewhere in the PATH.
  109. //
  110. // The compiler determines the executable name to search for by concatenating
  111. // exe_name_prefix with the unrecognized flag name, removing "_out". So, for
  112. // example, if exe_name_prefix is "protoc-" and you pass the flag --foo_out,
  113. // the compiler will try to run the program "protoc-foo".
  114. //
  115. // The plugin program should implement the following usage:
  116. // plugin [--out=OUTDIR] [--parameter=PARAMETER] PROTO_FILES < DESCRIPTORS
  117. // --out indicates the output directory (as passed to the --foo_out
  118. // parameter); if omitted, the current directory should be used. --parameter
  119. // gives the generator parameter, if any was provided. The PROTO_FILES list
  120. // the .proto files which were given on the compiler command-line; these are
  121. // the files for which the plugin is expected to generate output code.
  122. // Finally, DESCRIPTORS is an encoded FileDescriptorSet (as defined in
  123. // descriptor.proto). This is piped to the plugin's stdin. The set will
  124. // include descriptors for all the files listed in PROTO_FILES as well as
  125. // all files that they import. The plugin MUST NOT attempt to read the
  126. // PROTO_FILES directly -- it must use the FileDescriptorSet.
  127. //
  128. // The plugin should generate whatever files are necessary, as code generators
  129. // normally do. It should write the names of all files it generates to
  130. // stdout. The names should be relative to the output directory, NOT absolute
  131. // names or relative to the current directory. If any errors occur, error
  132. // messages should be written to stderr. If an error is fatal, the plugin
  133. // should exit with a non-zero exit code.
  134. void AllowPlugins(const string& exe_name_prefix);
  135. // Run the Protocol Compiler with the given command-line parameters.
  136. // Returns the error code which should be returned by main().
  137. //
  138. // It may not be safe to call Run() in a multi-threaded environment because
  139. // it calls strerror(). I'm not sure why you'd want to do this anyway.
  140. int Run(int argc, const char* const argv[]);
  141. // Call SetInputsAreCwdRelative(true) if the input files given on the command
  142. // line should be interpreted relative to the proto import path specified
  143. // using --proto_path or -I flags. Otherwise, input file names will be
  144. // interpreted relative to the current working directory (or as absolute
  145. // paths if they start with '/'), though they must still reside inside
  146. // a directory given by --proto_path or the compiler will fail. The latter
  147. // mode is generally more intuitive and easier to use, especially e.g. when
  148. // defining implicit rules in Makefiles.
  149. void SetInputsAreProtoPathRelative(bool enable) {
  150. inputs_are_proto_path_relative_ = enable;
  151. }
  152. // Provides some text which will be printed when the --version flag is
  153. // used. The version of libprotoc will also be printed on the next line
  154. // after this text.
  155. void SetVersionInfo(const string& text) {
  156. version_info_ = text;
  157. }
  158. private:
  159. // -----------------------------------------------------------------
  160. class ErrorPrinter;
  161. class GeneratorContextImpl;
  162. class MemoryOutputStream;
  163. // Clear state from previous Run().
  164. void Clear();
  165. // Remaps each file in input_files_ so that it is relative to one of the
  166. // directories in proto_path_. Returns false if an error occurred. This
  167. // is only used if inputs_are_proto_path_relative_ is false.
  168. bool MakeInputsBeProtoPathRelative(
  169. DiskSourceTree* source_tree);
  170. // Parse all command-line arguments.
  171. bool ParseArguments(int argc, const char* const argv[]);
  172. // Parses a command-line argument into a name/value pair. Returns
  173. // true if the next argument in the argv should be used as the value,
  174. // false otherwise.
  175. //
  176. // Exmaples:
  177. // "-Isrc/protos" ->
  178. // name = "-I", value = "src/protos"
  179. // "--cpp_out=src/foo.pb2.cc" ->
  180. // name = "--cpp_out", value = "src/foo.pb2.cc"
  181. // "foo.proto" ->
  182. // name = "", value = "foo.proto"
  183. bool ParseArgument(const char* arg, string* name, string* value);
  184. // Interprets arguments parsed with ParseArgument.
  185. bool InterpretArgument(const string& name, const string& value);
  186. // Print the --help text to stderr.
  187. void PrintHelpText();
  188. // Generate the given output file from the given input.
  189. struct OutputDirective; // see below
  190. bool GenerateOutput(const vector<const FileDescriptor*>& parsed_files,
  191. const OutputDirective& output_directive,
  192. GeneratorContext* generator_context);
  193. bool GeneratePluginOutput(const vector<const FileDescriptor*>& parsed_files,
  194. const string& plugin_name,
  195. const string& parameter,
  196. GeneratorContext* generator_context,
  197. string* error);
  198. // Implements --encode and --decode.
  199. bool EncodeOrDecode(const DescriptorPool* pool);
  200. // Implements the --descriptor_set_out option.
  201. bool WriteDescriptorSet(const vector<const FileDescriptor*> parsed_files);
  202. // Get all transitive dependencies of the given file (including the file
  203. // itself), adding them to the given list of FileDescriptorProtos. The
  204. // protos will be ordered such that every file is listed before any file that
  205. // depends on it, so that you can call DescriptorPool::BuildFile() on them
  206. // in order. Any files in *already_seen will not be added, and each file
  207. // added will be inserted into *already_seen.
  208. static void GetTransitiveDependencies(
  209. const FileDescriptor* file,
  210. set<const FileDescriptor*>* already_seen,
  211. RepeatedPtrField<FileDescriptorProto>* output);
  212. // -----------------------------------------------------------------
  213. // The name of the executable as invoked (i.e. argv[0]).
  214. string executable_name_;
  215. // Version info set with SetVersionInfo().
  216. string version_info_;
  217. // Map from flag names to registered generators.
  218. struct GeneratorInfo {
  219. CodeGenerator* generator;
  220. string help_text;
  221. };
  222. typedef map<string, GeneratorInfo> GeneratorMap;
  223. GeneratorMap generators_;
  224. // See AllowPlugins(). If this is empty, plugins aren't allowed.
  225. string plugin_prefix_;
  226. // Maps specific plugin names to files. When executing a plugin, this map
  227. // is searched first to find the plugin executable. If not found here, the
  228. // PATH (or other OS-specific search strategy) is searched.
  229. map<string, string> plugins_;
  230. // Stuff parsed from command line.
  231. enum Mode {
  232. MODE_COMPILE, // Normal mode: parse .proto files and compile them.
  233. MODE_ENCODE, // --encode: read text from stdin, write binary to stdout.
  234. MODE_DECODE // --decode: read binary from stdin, write text to stdout.
  235. };
  236. Mode mode_;
  237. enum ErrorFormat {
  238. ERROR_FORMAT_GCC, // GCC error output format (default).
  239. ERROR_FORMAT_MSVS // Visual Studio output (--error_format=msvs).
  240. };
  241. ErrorFormat error_format_;
  242. vector<pair<string, string> > proto_path_; // Search path for proto files.
  243. vector<string> input_files_; // Names of the input proto files.
  244. // output_directives_ lists all the files we are supposed to output and what
  245. // generator to use for each.
  246. struct OutputDirective {
  247. string name; // E.g. "--foo_out"
  248. CodeGenerator* generator; // NULL for plugins
  249. string parameter;
  250. string output_location;
  251. };
  252. vector<OutputDirective> output_directives_;
  253. // When using --encode or --decode, this names the type we are encoding or
  254. // decoding. (Empty string indicates --decode_raw.)
  255. string codec_type_;
  256. // If --descriptor_set_out was given, this is the filename to which the
  257. // FileDescriptorSet should be written. Otherwise, empty.
  258. string descriptor_set_name_;
  259. // True if --include_imports was given, meaning that we should
  260. // write all transitive dependencies to the DescriptorSet. Otherwise, only
  261. // the .proto files listed on the command-line are added.
  262. bool imports_in_descriptor_set_;
  263. // Was the --disallow_services flag used?
  264. bool disallow_services_;
  265. // See SetInputsAreProtoPathRelative().
  266. bool inputs_are_proto_path_relative_;
  267. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CommandLineInterface);
  268. };
  269. } // namespace compiler
  270. } // namespace protobuf
  271. } // namespace google
  272. #endif // GOOGLE_PROTOBUF_COMPILER_COMMAND_LINE_INTERFACE_H__