Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-31 10:12:00

0001 // Protocol Buffers - Google's data interchange format
0002 // Copyright 2008 Google Inc.  All rights reserved.
0003 //
0004 // Use of this source code is governed by a BSD-style
0005 // license that can be found in the LICENSE file or at
0006 // https://developers.google.com/open-source/licenses/bsd
0007 
0008 // Author: kenton@google.com (Kenton Varda)
0009 //  Based on original Protocol Buffers design by
0010 //  Sanjay Ghemawat, Jeff Dean, and others.
0011 //
0012 // Implements the Protocol Compiler front-end such that it may be reused by
0013 // custom compilers written to support other languages.
0014 
0015 #ifndef GOOGLE_PROTOBUF_COMPILER_COMMAND_LINE_INTERFACE_H__
0016 #define GOOGLE_PROTOBUF_COMPILER_COMMAND_LINE_INTERFACE_H__
0017 
0018 #include <cstdint>
0019 #include <functional>
0020 #include <memory>
0021 #include <string>
0022 #include <utility>
0023 #include <vector>
0024 
0025 #include "absl/container/btree_map.h"
0026 #include "absl/container/flat_hash_map.h"
0027 #include "absl/container/flat_hash_set.h"
0028 #include "absl/strings/string_view.h"
0029 #include "google/protobuf/descriptor.pb.h"
0030 #include "google/protobuf/descriptor_database.h"
0031 #include "google/protobuf/port.h"
0032 
0033 // Must be included last.
0034 #include "google/protobuf/port_def.inc"
0035 
0036 namespace google {
0037 namespace protobuf {
0038 
0039 class Descriptor;           // descriptor.h
0040 class DescriptorDatabase;   // descriptor_database.h
0041 class DescriptorPool;       // descriptor.h
0042 class FileDescriptor;       // descriptor.h
0043 class FileDescriptorSet;    // descriptor.h
0044 class FileDescriptorProto;  // descriptor.pb.h
0045 template <typename T>
0046 class RepeatedPtrField;          // repeated_field.h
0047 class SimpleDescriptorDatabase;  // descriptor_database.h
0048 
0049 namespace compiler {
0050 
0051 class CodeGenerator;     // code_generator.h
0052 class GeneratorContext;  // code_generator.h
0053 class DiskSourceTree;    // importer.h
0054 
0055 struct TransitiveDependencyOptions {
0056   bool include_json_name = false;
0057   bool include_source_code_info = false;
0058   bool retain_options = false;
0059 };
0060 
0061 // This class implements the command-line interface to the protocol compiler.
0062 // It is designed to make it very easy to create a custom protocol compiler
0063 // supporting the languages of your choice.  For example, if you wanted to
0064 // create a custom protocol compiler binary which includes both the regular
0065 // C++ support plus support for your own custom output "Foo", you would
0066 // write a class "FooGenerator" which implements the CodeGenerator interface,
0067 // then write a main() procedure like this:
0068 //
0069 //   int main(int argc, char* argv[]) {
0070 //     google::protobuf::compiler::CommandLineInterface cli;
0071 //
0072 //     // Support generation of C++ source and headers.
0073 //     google::protobuf::compiler::cpp::CppGenerator cpp_generator;
0074 //     cli.RegisterGenerator("--cpp_out", &cpp_generator,
0075 //       "Generate C++ source and header.");
0076 //
0077 //     // Support generation of Foo code.
0078 //     FooGenerator foo_generator;
0079 //     cli.RegisterGenerator("--foo_out", &foo_generator,
0080 //       "Generate Foo file.");
0081 //
0082 //     return cli.Run(argc, argv);
0083 //   }
0084 //
0085 // The compiler is invoked with syntax like:
0086 //   protoc --cpp_out=outdir --foo_out=outdir --proto_path=src src/foo.proto
0087 //
0088 // The .proto file to compile can be specified on the command line using either
0089 // its physical file path, or a virtual path relative to a directory specified
0090 // in --proto_path. For example, for src/foo.proto, the following two protoc
0091 // invocations work the same way:
0092 //   1. protoc --proto_path=src src/foo.proto (physical file path)
0093 //   2. protoc --proto_path=src foo.proto (virtual path relative to src)
0094 //
0095 // If a file path can be interpreted both as a physical file path and as a
0096 // relative virtual path, the physical file path takes precedence.
0097 //
0098 // For a full description of the command-line syntax, invoke it with --help.
0099 class PROTOC_EXPORT CommandLineInterface {
0100  public:
0101   static const char* const kPathSeparator;
0102 
0103   CommandLineInterface();
0104   CommandLineInterface(const CommandLineInterface&) = delete;
0105   CommandLineInterface& operator=(const CommandLineInterface&) = delete;
0106   ~CommandLineInterface();
0107 
0108   // Register a code generator for a language.
0109   //
0110   // Parameters:
0111   // * flag_name: The command-line flag used to specify an output file of
0112   //   this type.  The name must start with a '-'.  If the name is longer
0113   //   than one letter, it must start with two '-'s.
0114   // * generator: The CodeGenerator which will be called to generate files
0115   //   of this type.
0116   // * help_text: Text describing this flag in the --help output.
0117   //
0118   // Some generators accept extra parameters.  You can specify this parameter
0119   // on the command-line by placing it before the output directory, separated
0120   // by a colon:
0121   //   protoc --foo_out=enable_bar:outdir
0122   // The text before the colon is passed to CodeGenerator::Generate() as the
0123   // "parameter".
0124   void RegisterGenerator(const std::string& flag_name, CodeGenerator* generator,
0125                          const std::string& help_text);
0126 
0127   // Register a code generator for a language.
0128   // Besides flag_name you can specify another option_flag_name that could be
0129   // used to pass extra parameters to the registered code generator.
0130   // Suppose you have registered a generator by calling:
0131   //   command_line_interface.RegisterGenerator("--foo_out", "--foo_opt", ...)
0132   // Then you could invoke the compiler with a command like:
0133   //   protoc --foo_out=enable_bar:outdir --foo_opt=enable_baz
0134   // This will pass "enable_bar,enable_baz" as the parameter to the generator.
0135   void RegisterGenerator(const std::string& flag_name,
0136                          const std::string& option_flag_name,
0137                          CodeGenerator* generator,
0138                          const std::string& help_text);
0139 
0140   // Enables "plugins".  In this mode, if a command-line flag ends with "_out"
0141   // but does not match any registered generator, the compiler will attempt to
0142   // find a "plugin" to implement the generator.  Plugins are just executables.
0143   // They should live somewhere in the PATH.
0144   //
0145   // The compiler determines the executable name to search for by concatenating
0146   // exe_name_prefix with the unrecognized flag name, removing "_out".  So, for
0147   // example, if exe_name_prefix is "protoc-" and you pass the flag --foo_out,
0148   // the compiler will try to run the program "protoc-gen-foo".
0149   //
0150   // The plugin program should implement the following usage:
0151   //   plugin [--out=OUTDIR] [--parameter=PARAMETER] PROTO_FILES < DESCRIPTORS
0152   // --out indicates the output directory (as passed to the --foo_out
0153   // parameter); if omitted, the current directory should be used.  --parameter
0154   // gives the generator parameter, if any was provided (see below).  The
0155   // PROTO_FILES list the .proto files which were given on the compiler
0156   // command-line; these are the files for which the plugin is expected to
0157   // generate output code.  Finally, DESCRIPTORS is an encoded FileDescriptorSet
0158   // (as defined in descriptor.proto).  This is piped to the plugin's stdin.
0159   // The set will include descriptors for all the files listed in PROTO_FILES as
0160   // well as all files that they import.  The plugin MUST NOT attempt to read
0161   // the PROTO_FILES directly -- it must use the FileDescriptorSet.
0162   //
0163   // The plugin should generate whatever files are necessary, as code generators
0164   // normally do.  It should write the names of all files it generates to
0165   // stdout.  The names should be relative to the output directory, NOT absolute
0166   // names or relative to the current directory.  If any errors occur, error
0167   // messages should be written to stderr.  If an error is fatal, the plugin
0168   // should exit with a non-zero exit code.
0169   //
0170   // Plugins can have generator parameters similar to normal built-in
0171   // generators. Extra generator parameters can be passed in via a matching
0172   // "_opt" parameter. For example:
0173   //   protoc --plug_out=enable_bar:outdir --plug_opt=enable_baz
0174   // This will pass "enable_bar,enable_baz" as the parameter to the plugin.
0175   //
0176   void AllowPlugins(const std::string& exe_name_prefix);
0177 
0178   // Run the Protocol Compiler with the given command-line parameters.
0179   // Returns the error code which should be returned by main().
0180   //
0181   // It may not be safe to call Run() in a multi-threaded environment because
0182   // it calls strerror().  I'm not sure why you'd want to do this anyway.
0183   int Run(int argc, const char* const argv[]);
0184 
0185   // DEPRECATED. Calling this method has no effect. Protocol compiler now
0186   // always try to find the .proto file relative to the current directory
0187   // first and if the file is not found, it will then treat the input path
0188   // as a virtual path.
0189   void SetInputsAreProtoPathRelative(bool /* enable */) {}
0190 
0191   // Provides some text which will be printed when the --version flag is
0192   // used.  The version of libprotoc will also be printed on the next line
0193   // after this text.
0194   void SetVersionInfo(const std::string& text) { version_info_ = text; }
0195 
0196 
0197   // Configure protoc to act as if we're in opensource.
0198   void set_opensource_runtime(bool opensource) {
0199     opensource_runtime_ = opensource;
0200   }
0201 
0202  private:
0203   // -----------------------------------------------------------------
0204 
0205   class ErrorPrinter;
0206   class GeneratorContextImpl;
0207   class MemoryOutputStream;
0208   using GeneratorContextMap =
0209       absl::flat_hash_map<std::string, std::unique_ptr<GeneratorContextImpl>>;
0210 
0211   // Clear state from previous Run().
0212   void Clear();
0213 
0214   // Remaps the proto file so that it is relative to one of the directories
0215   // in proto_path_.  Returns false if an error occurred.
0216   bool MakeProtoProtoPathRelative(DiskSourceTree* source_tree,
0217                                   std::string* proto,
0218                                   DescriptorDatabase* fallback_database);
0219 
0220   // Remaps each file in input_files_ so that it is relative to one of the
0221   // directories in proto_path_.  Returns false if an error occurred.
0222   bool MakeInputsBeProtoPathRelative(DiskSourceTree* source_tree,
0223                                      DescriptorDatabase* fallback_database);
0224 
0225   // Fails if these files use proto3 optional and the code generator doesn't
0226   // support it. This is a permanent check.
0227   bool EnforceProto3OptionalSupport(
0228       const std::string& codegen_name, uint64_t supported_features,
0229       const std::vector<const FileDescriptor*>& parsed_files) const;
0230 
0231   bool EnforceEditionsSupport(
0232       const std::string& codegen_name, uint64_t supported_features,
0233       Edition minimum_edition, Edition maximum_edition,
0234       const std::vector<const FileDescriptor*>& parsed_files) const;
0235 
0236 
0237   // Return status for ParseArguments() and InterpretArgument().
0238   enum ParseArgumentStatus {
0239     PARSE_ARGUMENT_DONE_AND_CONTINUE,
0240     PARSE_ARGUMENT_DONE_AND_EXIT,
0241     PARSE_ARGUMENT_FAIL
0242   };
0243 
0244   // Parse all command-line arguments.
0245   ParseArgumentStatus ParseArguments(int argc, const char* const argv[]);
0246 
0247   // Read an argument file and append the file's content to the list of
0248   // arguments. Return false if the file cannot be read.
0249   bool ExpandArgumentFile(const char* file,
0250                           std::vector<std::string>* arguments);
0251 
0252   // Parses a command-line argument into a name/value pair.  Returns
0253   // true if the next argument in the argv should be used as the value,
0254   // false otherwise.
0255   //
0256   // Examples:
0257   //   "-Isrc/protos" ->
0258   //     name = "-I", value = "src/protos"
0259   //   "--cpp_out=src/foo.pb2.cc" ->
0260   //     name = "--cpp_out", value = "src/foo.pb2.cc"
0261   //   "foo.proto" ->
0262   //     name = "", value = "foo.proto"
0263   bool ParseArgument(const char* arg, std::string* name, std::string* value);
0264 
0265   // Interprets arguments parsed with ParseArgument.
0266   ParseArgumentStatus InterpretArgument(const std::string& name,
0267                                         const std::string& value);
0268 
0269   // Print the --help text to stderr.
0270   void PrintHelpText();
0271 
0272   // Loads proto_path_ into the provided source_tree.
0273   bool InitializeDiskSourceTree(DiskSourceTree* source_tree,
0274                                 DescriptorDatabase* fallback_database);
0275 
0276   // Verify that all the input files exist in the given database.
0277   bool VerifyInputFilesInDescriptors(DescriptorDatabase* fallback_database);
0278 
0279   // Parses input_files_ into parsed_files
0280   bool ParseInputFiles(DescriptorPool* descriptor_pool,
0281                        DiskSourceTree* source_tree,
0282                        std::vector<const FileDescriptor*>* parsed_files);
0283 
0284   bool SetupFeatureResolution(DescriptorPool& pool);
0285 
0286   // Generate the given output file from the given input.
0287   struct OutputDirective;  // see below
0288   bool GenerateOutput(const std::vector<const FileDescriptor*>& parsed_files,
0289                       const OutputDirective& output_directive,
0290                       GeneratorContext* generator_context);
0291   bool GeneratePluginOutput(
0292       const std::vector<const FileDescriptor*>& parsed_files,
0293       const std::string& plugin_name, const std::string& parameter,
0294       GeneratorContext* generator_context, std::string* error);
0295 
0296   // Implements --encode and --decode.
0297   bool EncodeOrDecode(const DescriptorPool* pool);
0298 
0299   // Implements the --descriptor_set_out option.
0300   bool WriteDescriptorSet(
0301       const std::vector<const FileDescriptor*>& parsed_files);
0302 
0303   // Implements the --edition_defaults_out option.
0304   bool WriteEditionDefaults(const DescriptorPool& pool);
0305 
0306   // Implements the --dependency_out option
0307   bool GenerateDependencyManifestFile(
0308       const std::vector<const FileDescriptor*>& parsed_files,
0309       const GeneratorContextMap& output_directories,
0310       DiskSourceTree* source_tree);
0311 
0312   // Implements the --print_free_field_numbers. This function prints free field
0313   // numbers into stdout for the message and it's nested message types in
0314   // post-order, i.e. nested types first. Printed range are left-right
0315   // inclusive, i.e. [a, b].
0316   //
0317   // Groups:
0318   // For historical reasons, groups are considered to share the same
0319   // field number space with the parent message, thus it will not print free
0320   // field numbers for groups. The field numbers used in the groups are
0321   // excluded in the free field numbers of the parent message.
0322   //
0323   // Extension Ranges:
0324   // Extension ranges are considered ocuppied field numbers and they will not be
0325   // listed as free numbers in the output.
0326   void PrintFreeFieldNumbers(const Descriptor* descriptor);
0327 
0328   // Get all transitive dependencies of the given file (including the file
0329   // itself), adding them to the given list of FileDescriptorProtos.  The
0330   // protos will be ordered such that every file is listed before any file that
0331   // depends on it, so that you can call DescriptorPool::BuildFile() on them
0332   // in order.  Any files in *already_seen will not be added, and each file
0333   // added will be inserted into *already_seen.  If include_source_code_info
0334   // (from TransitiveDependencyOptions) is true then include the source code
0335   // information in the FileDescriptorProtos. If include_json_name is true,
0336   // populate the json_name field of FieldDescriptorProto for all fields.
0337   void GetTransitiveDependencies(
0338       const FileDescriptor* file,
0339       absl::flat_hash_set<const FileDescriptor*>* already_seen,
0340       RepeatedPtrField<FileDescriptorProto>* output,
0341       const TransitiveDependencyOptions& options =
0342           TransitiveDependencyOptions());
0343 
0344 
0345   // -----------------------------------------------------------------
0346 
0347   // The name of the executable as invoked (i.e. argv[0]).
0348   std::string executable_name_;
0349 
0350   // Version info set with SetVersionInfo().
0351   std::string version_info_;
0352 
0353   // Registered generators.
0354   struct GeneratorInfo {
0355     std::string flag_name;
0356     std::string option_flag_name;
0357     CodeGenerator* generator;
0358     std::string help_text;
0359   };
0360 
0361   const GeneratorInfo* FindGeneratorByFlag(const std::string& name) const;
0362   const GeneratorInfo* FindGeneratorByOption(const std::string& option) const;
0363 
0364   absl::btree_map<std::string, GeneratorInfo> generators_by_flag_name_;
0365   absl::flat_hash_map<std::string, GeneratorInfo> generators_by_option_name_;
0366   // A map from generator names to the parameters specified using the option
0367   // flag. For example, if the user invokes the compiler with:
0368   //   protoc --foo_out=outputdir --foo_opt=enable_bar ...
0369   // Then there will be an entry ("--foo_out", "enable_bar") in this map.
0370   absl::flat_hash_map<std::string, std::string> generator_parameters_;
0371   // Similar to generator_parameters_, stores the parameters for plugins but the
0372   // key is the actual plugin name e.g. "protoc-gen-foo".
0373   absl::flat_hash_map<std::string, std::string> plugin_parameters_;
0374 
0375   // See AllowPlugins().  If this is empty, plugins aren't allowed.
0376   std::string plugin_prefix_;
0377 
0378   // Maps specific plugin names to files.  When executing a plugin, this map
0379   // is searched first to find the plugin executable.  If not found here, the
0380   // PATH (or other OS-specific search strategy) is searched.
0381   absl::flat_hash_map<std::string, std::string> plugins_;
0382 
0383   // Stuff parsed from command line.
0384   enum Mode {
0385     MODE_COMPILE,  // Normal mode:  parse .proto files and compile them.
0386     MODE_ENCODE,   // --encode:  read text from stdin, write binary to stdout.
0387     MODE_DECODE,   // --decode:  read binary from stdin, write text to stdout.
0388     MODE_PRINT,    // Print mode: print info of the given .proto files and exit.
0389   };
0390 
0391   Mode mode_ = MODE_COMPILE;
0392 
0393   enum PrintMode {
0394     PRINT_NONE,         // Not in MODE_PRINT
0395     PRINT_FREE_FIELDS,  // --print_free_fields
0396   };
0397 
0398   PrintMode print_mode_ = PRINT_NONE;
0399 
0400   enum ErrorFormat {
0401     ERROR_FORMAT_GCC,  // GCC error output format (default).
0402     ERROR_FORMAT_MSVS  // Visual Studio output (--error_format=msvs).
0403   };
0404 
0405   ErrorFormat error_format_ = ERROR_FORMAT_GCC;
0406 
0407   // True if we should treat warnings as errors that fail the compilation.
0408   bool fatal_warnings_ = false;
0409 
0410   std::vector<std::pair<std::string, std::string>>
0411       proto_path_;                        // Search path for proto files.
0412   std::vector<std::string> input_files_;  // Names of the input proto files.
0413 
0414   // Names of proto files which are allowed to be imported. Used by build
0415   // systems to enforce depend-on-what-you-import.
0416   absl::flat_hash_set<std::string> direct_dependencies_;
0417   bool direct_dependencies_explicitly_set_ = false;
0418 
0419   // If there's a violation of depend-on-what-you-import, this string will be
0420   // presented to the user. "%s" will be replaced with the violating import.
0421   std::string direct_dependencies_violation_msg_;
0422 
0423   // output_directives_ lists all the files we are supposed to output and what
0424   // generator to use for each.
0425   struct OutputDirective {
0426     std::string name;          // E.g. "--foo_out"
0427     CodeGenerator* generator;  // NULL for plugins
0428     std::string parameter;
0429     std::string output_location;
0430   };
0431   std::vector<OutputDirective> output_directives_;
0432 
0433   // When using --encode or --decode, this names the type we are encoding or
0434   // decoding.  (Empty string indicates --decode_raw.)
0435   std::string codec_type_;
0436 
0437   // If --descriptor_set_in was given, these are filenames containing
0438   // parsed FileDescriptorSets to be used for loading protos.  Otherwise, empty.
0439   std::vector<std::string> descriptor_set_in_names_;
0440 
0441   // If --descriptor_set_out was given, this is the filename to which the
0442   // FileDescriptorSet should be written.  Otherwise, empty.
0443   std::string descriptor_set_out_name_;
0444 
0445   std::string edition_defaults_out_name_;
0446   Edition edition_defaults_minimum_;
0447   Edition edition_defaults_maximum_;
0448 
0449   // If --dependency_out was given, this is the path to the file where the
0450   // dependency file will be written. Otherwise, empty.
0451   std::string dependency_out_name_;
0452 
0453   bool experimental_editions_ = false;
0454 
0455   // True if --include_imports was given, meaning that we should
0456   // write all transitive dependencies to the DescriptorSet.  Otherwise, only
0457   // the .proto files listed on the command-line are added.
0458   bool imports_in_descriptor_set_;
0459 
0460   // True if --include_source_info was given, meaning that we should not strip
0461   // SourceCodeInfo from the DescriptorSet.
0462   bool source_info_in_descriptor_set_ = false;
0463 
0464   // True if --retain_options was given, meaning that we shouldn't strip any
0465   // options from the DescriptorSet, even if they have RETENTION_SOURCE
0466   // specified.
0467   bool retain_options_in_descriptor_set_ = false;
0468 
0469   // Was the --disallow_services flag used?
0470   bool disallow_services_ = false;
0471 
0472   // When using --encode, this will be passed to SetSerializationDeterministic.
0473   bool deterministic_output_ = false;
0474 
0475   bool opensource_runtime_ = google::protobuf::internal::IsOss();
0476 
0477 };
0478 
0479 }  // namespace compiler
0480 }  // namespace protobuf
0481 }  // namespace google
0482 
0483 #include "google/protobuf/port_undef.inc"
0484 
0485 #endif  // GOOGLE_PROTOBUF_COMPILER_COMMAND_LINE_INTERFACE_H__