1 // Protocol Buffers - Google's data interchange format 2 // Copyright 2008 Google Inc. All rights reserved. 3 // https://developers.google.com/protocol-buffers/ 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 31 // Author: kenton@google.com (Kenton Varda) 32 // Based on original Protocol Buffers design by 33 // Sanjay Ghemawat, Jeff Dean, and others. 34 // 35 // Implements the Protocol Compiler front-end such that it may be reused by 36 // custom compilers written to support other languages. 37 38 #ifndef GOOGLE_PROTOBUF_COMPILER_COMMAND_LINE_INTERFACE_H__ 39 #define GOOGLE_PROTOBUF_COMPILER_COMMAND_LINE_INTERFACE_H__ 40 41 #include <map> 42 #include <memory> 43 #include <set> 44 #include <string> 45 #include <unordered_map> 46 #include <unordered_set> 47 #include <utility> 48 #include <vector> 49 50 #include <google/protobuf/stubs/common.h> 51 #include <google/protobuf/port_def.inc> 52 53 namespace google { 54 namespace protobuf { 55 56 class Descriptor; // descriptor.h 57 class DescriptorDatabase; // descriptor_database.h 58 class DescriptorPool; // descriptor.h 59 class FileDescriptor; // descriptor.h 60 class FileDescriptorSet; // descriptor.h 61 class FileDescriptorProto; // descriptor.pb.h 62 template <typename T> 63 class RepeatedPtrField; // repeated_field.h 64 class SimpleDescriptorDatabase; // descriptor_database.h 65 66 namespace compiler { 67 68 class CodeGenerator; // code_generator.h 69 class GeneratorContext; // code_generator.h 70 class DiskSourceTree; // importer.h 71 72 // This class implements the command-line interface to the protocol compiler. 73 // It is designed to make it very easy to create a custom protocol compiler 74 // supporting the languages of your choice. For example, if you wanted to 75 // create a custom protocol compiler binary which includes both the regular 76 // C++ support plus support for your own custom output "Foo", you would 77 // write a class "FooGenerator" which implements the CodeGenerator interface, 78 // then write a main() procedure like this: 79 // 80 // int main(int argc, char* argv[]) { 81 // google::protobuf::compiler::CommandLineInterface cli; 82 // 83 // // Support generation of C++ source and headers. 84 // google::protobuf::compiler::cpp::CppGenerator cpp_generator; 85 // cli.RegisterGenerator("--cpp_out", &cpp_generator, 86 // "Generate C++ source and header."); 87 // 88 // // Support generation of Foo code. 89 // FooGenerator foo_generator; 90 // cli.RegisterGenerator("--foo_out", &foo_generator, 91 // "Generate Foo file."); 92 // 93 // return cli.Run(argc, argv); 94 // } 95 // 96 // The compiler is invoked with syntax like: 97 // protoc --cpp_out=outdir --foo_out=outdir --proto_path=src src/foo.proto 98 // 99 // The .proto file to compile can be specified on the command line using either 100 // its physical file path, or a virtual path relative to a directory specified 101 // in --proto_path. For example, for src/foo.proto, the following two protoc 102 // invocations work the same way: 103 // 1. protoc --proto_path=src src/foo.proto (physical file path) 104 // 2. protoc --proto_path=src foo.proto (virtual path relative to src) 105 // 106 // If a file path can be interpreted both as a physical file path and as a 107 // relative virtual path, the physical file path takes precendence. 108 // 109 // For a full description of the command-line syntax, invoke it with --help. 110 class PROTOC_EXPORT CommandLineInterface { 111 public: 112 static const char* const kPathSeparator; 113 114 CommandLineInterface(); 115 ~CommandLineInterface(); 116 117 // Register a code generator for a language. 118 // 119 // Parameters: 120 // * flag_name: The command-line flag used to specify an output file of 121 // this type. The name must start with a '-'. If the name is longer 122 // than one letter, it must start with two '-'s. 123 // * generator: The CodeGenerator which will be called to generate files 124 // of this type. 125 // * help_text: Text describing this flag in the --help output. 126 // 127 // Some generators accept extra parameters. You can specify this parameter 128 // on the command-line by placing it before the output directory, separated 129 // by a colon: 130 // protoc --foo_out=enable_bar:outdir 131 // The text before the colon is passed to CodeGenerator::Generate() as the 132 // "parameter". 133 void RegisterGenerator(const std::string& flag_name, CodeGenerator* generator, 134 const std::string& help_text); 135 136 // Register a code generator for a language. 137 // Besides flag_name you can specify another option_flag_name that could be 138 // used to pass extra parameters to the registered code generator. 139 // Suppose you have registered a generator by calling: 140 // command_line_interface.RegisterGenerator("--foo_out", "--foo_opt", ...) 141 // Then you could invoke the compiler with a command like: 142 // protoc --foo_out=enable_bar:outdir --foo_opt=enable_baz 143 // This will pass "enable_bar,enable_baz" as the parameter to the generator. 144 void RegisterGenerator(const std::string& flag_name, 145 const std::string& option_flag_name, 146 CodeGenerator* generator, 147 const std::string& help_text); 148 149 // Enables "plugins". In this mode, if a command-line flag ends with "_out" 150 // but does not match any registered generator, the compiler will attempt to 151 // find a "plugin" to implement the generator. Plugins are just executables. 152 // They should live somewhere in the PATH. 153 // 154 // The compiler determines the executable name to search for by concatenating 155 // exe_name_prefix with the unrecognized flag name, removing "_out". So, for 156 // example, if exe_name_prefix is "protoc-" and you pass the flag --foo_out, 157 // the compiler will try to run the program "protoc-gen-foo". 158 // 159 // The plugin program should implement the following usage: 160 // plugin [--out=OUTDIR] [--parameter=PARAMETER] PROTO_FILES < DESCRIPTORS 161 // --out indicates the output directory (as passed to the --foo_out 162 // parameter); if omitted, the current directory should be used. --parameter 163 // gives the generator parameter, if any was provided (see below). The 164 // PROTO_FILES list the .proto files which were given on the compiler 165 // command-line; these are the files for which the plugin is expected to 166 // generate output code. Finally, DESCRIPTORS is an encoded FileDescriptorSet 167 // (as defined in descriptor.proto). This is piped to the plugin's stdin. 168 // The set will include descriptors for all the files listed in PROTO_FILES as 169 // well as all files that they import. The plugin MUST NOT attempt to read 170 // the PROTO_FILES directly -- it must use the FileDescriptorSet. 171 // 172 // The plugin should generate whatever files are necessary, as code generators 173 // normally do. It should write the names of all files it generates to 174 // stdout. The names should be relative to the output directory, NOT absolute 175 // names or relative to the current directory. If any errors occur, error 176 // messages should be written to stderr. If an error is fatal, the plugin 177 // should exit with a non-zero exit code. 178 // 179 // Plugins can have generator parameters similar to normal built-in 180 // generators. Extra generator parameters can be passed in via a matching 181 // "_opt" parameter. For example: 182 // protoc --plug_out=enable_bar:outdir --plug_opt=enable_baz 183 // This will pass "enable_bar,enable_baz" as the parameter to the plugin. 184 // 185 void AllowPlugins(const std::string& exe_name_prefix); 186 187 // Run the Protocol Compiler with the given command-line parameters. 188 // Returns the error code which should be returned by main(). 189 // 190 // It may not be safe to call Run() in a multi-threaded environment because 191 // it calls strerror(). I'm not sure why you'd want to do this anyway. 192 int Run(int argc, const char* const argv[]); 193 194 // DEPRECATED. Calling this method has no effect. Protocol compiler now 195 // always try to find the .proto file relative to the current directory 196 // first and if the file is not found, it will then treat the input path 197 // as a virtual path. SetInputsAreProtoPathRelative(bool)198 void SetInputsAreProtoPathRelative(bool /* enable */) {} 199 200 // Provides some text which will be printed when the --version flag is 201 // used. The version of libprotoc will also be printed on the next line 202 // after this text. SetVersionInfo(const std::string & text)203 void SetVersionInfo(const std::string& text) { version_info_ = text; } 204 205 206 private: 207 // ----------------------------------------------------------------- 208 209 class ErrorPrinter; 210 class GeneratorContextImpl; 211 class MemoryOutputStream; 212 typedef std::unordered_map<std::string, std::unique_ptr<GeneratorContextImpl>> 213 GeneratorContextMap; 214 215 // Clear state from previous Run(). 216 void Clear(); 217 218 // Remaps the proto file so that it is relative to one of the directories 219 // in proto_path_. Returns false if an error occurred. 220 bool MakeProtoProtoPathRelative(DiskSourceTree* source_tree, 221 std::string* proto, 222 DescriptorDatabase* fallback_database); 223 224 // Remaps each file in input_files_ so that it is relative to one of the 225 // directories in proto_path_. Returns false if an error occurred. 226 bool MakeInputsBeProtoPathRelative(DiskSourceTree* source_tree, 227 DescriptorDatabase* fallback_database); 228 229 // Is this .proto file whitelisted, or do we have a command-line flag allowing 230 // us to use proto3 optional? This is a temporary control to avoid people from 231 // using proto3 optional until code generators have implemented it. 232 bool AllowProto3Optional(const FileDescriptor& file) const; 233 234 // Fails if these files use proto3 optional and the code generator doesn't 235 // support it. This is a permanent check. 236 bool EnforceProto3OptionalSupport( 237 const std::string& codegen_name, uint64 supported_features, 238 const std::vector<const FileDescriptor*>& parsed_files) const; 239 240 241 // Return status for ParseArguments() and InterpretArgument(). 242 enum ParseArgumentStatus { 243 PARSE_ARGUMENT_DONE_AND_CONTINUE, 244 PARSE_ARGUMENT_DONE_AND_EXIT, 245 PARSE_ARGUMENT_FAIL 246 }; 247 248 // Parse all command-line arguments. 249 ParseArgumentStatus ParseArguments(int argc, const char* const argv[]); 250 251 // Read an argument file and append the file's content to the list of 252 // arguments. Return false if the file cannot be read. 253 bool ExpandArgumentFile(const std::string& file, 254 std::vector<std::string>* arguments); 255 256 // Parses a command-line argument into a name/value pair. Returns 257 // true if the next argument in the argv should be used as the value, 258 // false otherwise. 259 // 260 // Examples: 261 // "-Isrc/protos" -> 262 // name = "-I", value = "src/protos" 263 // "--cpp_out=src/foo.pb2.cc" -> 264 // name = "--cpp_out", value = "src/foo.pb2.cc" 265 // "foo.proto" -> 266 // name = "", value = "foo.proto" 267 bool ParseArgument(const char* arg, std::string* name, std::string* value); 268 269 // Interprets arguments parsed with ParseArgument. 270 ParseArgumentStatus InterpretArgument(const std::string& name, 271 const std::string& value); 272 273 // Print the --help text to stderr. 274 void PrintHelpText(); 275 276 // Loads proto_path_ into the provided source_tree. 277 bool InitializeDiskSourceTree(DiskSourceTree* source_tree, 278 DescriptorDatabase* fallback_database); 279 280 // Verify that all the input files exist in the given database. 281 bool VerifyInputFilesInDescriptors(DescriptorDatabase* fallback_database); 282 283 // Parses input_files_ into parsed_files 284 bool ParseInputFiles(DescriptorPool* descriptor_pool, 285 DiskSourceTree* source_tree, 286 std::vector<const FileDescriptor*>* parsed_files); 287 288 // Generate the given output file from the given input. 289 struct OutputDirective; // see below 290 bool GenerateOutput(const std::vector<const FileDescriptor*>& parsed_files, 291 const OutputDirective& output_directive, 292 GeneratorContext* generator_context); 293 bool GeneratePluginOutput( 294 const std::vector<const FileDescriptor*>& parsed_files, 295 const std::string& plugin_name, const std::string& parameter, 296 GeneratorContext* generator_context, std::string* error); 297 298 // Implements --encode and --decode. 299 bool EncodeOrDecode(const DescriptorPool* pool); 300 301 // Implements the --descriptor_set_out option. 302 bool WriteDescriptorSet( 303 const std::vector<const FileDescriptor*>& parsed_files); 304 305 // Implements the --dependency_out option 306 bool GenerateDependencyManifestFile( 307 const std::vector<const FileDescriptor*>& parsed_files, 308 const GeneratorContextMap& output_directories, 309 DiskSourceTree* source_tree); 310 311 // Get all transitive dependencies of the given file (including the file 312 // itself), adding them to the given list of FileDescriptorProtos. The 313 // protos will be ordered such that every file is listed before any file that 314 // depends on it, so that you can call DescriptorPool::BuildFile() on them 315 // in order. Any files in *already_seen will not be added, and each file 316 // added will be inserted into *already_seen. If include_source_code_info is 317 // true then include the source code information in the FileDescriptorProtos. 318 // If include_json_name is true, populate the json_name field of 319 // FieldDescriptorProto for all fields. 320 static void GetTransitiveDependencies( 321 const FileDescriptor* file, bool include_json_name, 322 bool include_source_code_info, 323 std::set<const FileDescriptor*>* already_seen, 324 RepeatedPtrField<FileDescriptorProto>* output); 325 326 // Implements the --print_free_field_numbers. This function prints free field 327 // numbers into stdout for the message and it's nested message types in 328 // post-order, i.e. nested types first. Printed range are left-right 329 // inclusive, i.e. [a, b]. 330 // 331 // Groups: 332 // For historical reasons, groups are considered to share the same 333 // field number space with the parent message, thus it will not print free 334 // field numbers for groups. The field numbers used in the groups are 335 // excluded in the free field numbers of the parent message. 336 // 337 // Extension Ranges: 338 // Extension ranges are considered ocuppied field numbers and they will not be 339 // listed as free numbers in the output. 340 void PrintFreeFieldNumbers(const Descriptor* descriptor); 341 342 // ----------------------------------------------------------------- 343 344 // The name of the executable as invoked (i.e. argv[0]). 345 std::string executable_name_; 346 347 // Version info set with SetVersionInfo(). 348 std::string version_info_; 349 350 // Registered generators. 351 struct GeneratorInfo { 352 std::string flag_name; 353 std::string option_flag_name; 354 CodeGenerator* generator; 355 std::string help_text; 356 }; 357 typedef std::map<std::string, GeneratorInfo> GeneratorMap; 358 GeneratorMap generators_by_flag_name_; 359 GeneratorMap generators_by_option_name_; 360 // A map from generator names to the parameters specified using the option 361 // flag. For example, if the user invokes the compiler with: 362 // protoc --foo_out=outputdir --foo_opt=enable_bar ... 363 // Then there will be an entry ("--foo_out", "enable_bar") in this map. 364 std::map<std::string, std::string> generator_parameters_; 365 // Similar to generator_parameters_, but stores the parameters for plugins. 366 std::map<std::string, std::string> plugin_parameters_; 367 368 // See AllowPlugins(). If this is empty, plugins aren't allowed. 369 std::string plugin_prefix_; 370 371 // Maps specific plugin names to files. When executing a plugin, this map 372 // is searched first to find the plugin executable. If not found here, the 373 // PATH (or other OS-specific search strategy) is searched. 374 std::map<std::string, std::string> plugins_; 375 376 // Stuff parsed from command line. 377 enum Mode { 378 MODE_COMPILE, // Normal mode: parse .proto files and compile them. 379 MODE_ENCODE, // --encode: read text from stdin, write binary to stdout. 380 MODE_DECODE, // --decode: read binary from stdin, write text to stdout. 381 MODE_PRINT, // Print mode: print info of the given .proto files and exit. 382 }; 383 384 Mode mode_ = MODE_COMPILE; 385 386 enum PrintMode { 387 PRINT_NONE, // Not in MODE_PRINT 388 PRINT_FREE_FIELDS, // --print_free_fields 389 }; 390 391 PrintMode print_mode_ = PRINT_NONE; 392 393 enum ErrorFormat { 394 ERROR_FORMAT_GCC, // GCC error output format (default). 395 ERROR_FORMAT_MSVS // Visual Studio output (--error_format=msvs). 396 }; 397 398 ErrorFormat error_format_ = ERROR_FORMAT_GCC; 399 400 std::vector<std::pair<std::string, std::string> > 401 proto_path_; // Search path for proto files. 402 std::vector<std::string> input_files_; // Names of the input proto files. 403 404 // Names of proto files which are allowed to be imported. Used by build 405 // systems to enforce depend-on-what-you-import. 406 std::set<std::string> direct_dependencies_; 407 bool direct_dependencies_explicitly_set_ = false; 408 409 // If there's a violation of depend-on-what-you-import, this string will be 410 // presented to the user. "%s" will be replaced with the violating import. 411 std::string direct_dependencies_violation_msg_; 412 413 // output_directives_ lists all the files we are supposed to output and what 414 // generator to use for each. 415 struct OutputDirective { 416 std::string name; // E.g. "--foo_out" 417 CodeGenerator* generator; // NULL for plugins 418 std::string parameter; 419 std::string output_location; 420 }; 421 std::vector<OutputDirective> output_directives_; 422 423 // When using --encode or --decode, this names the type we are encoding or 424 // decoding. (Empty string indicates --decode_raw.) 425 std::string codec_type_; 426 427 // If --descriptor_set_in was given, these are filenames containing 428 // parsed FileDescriptorSets to be used for loading protos. Otherwise, empty. 429 std::vector<std::string> descriptor_set_in_names_; 430 431 // If --descriptor_set_out was given, this is the filename to which the 432 // FileDescriptorSet should be written. Otherwise, empty. 433 std::string descriptor_set_out_name_; 434 435 // If --dependency_out was given, this is the path to the file where the 436 // dependency file will be written. Otherwise, empty. 437 std::string dependency_out_name_; 438 439 // True if --include_imports was given, meaning that we should 440 // write all transitive dependencies to the DescriptorSet. Otherwise, only 441 // the .proto files listed on the command-line are added. 442 bool imports_in_descriptor_set_; 443 444 // True if --include_source_info was given, meaning that we should not strip 445 // SourceCodeInfo from the DescriptorSet. 446 bool source_info_in_descriptor_set_ = false; 447 448 // Was the --disallow_services flag used? 449 bool disallow_services_ = false; 450 451 // Was the --experimental_allow_proto3_optional flag used? 452 bool allow_proto3_optional_ = false; 453 454 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CommandLineInterface); 455 }; 456 457 } // namespace compiler 458 } // namespace protobuf 459 } // namespace google 460 461 #include <google/protobuf/port_undef.inc> 462 463 #endif // GOOGLE_PROTOBUF_COMPILER_COMMAND_LINE_INTERFACE_H__ 464