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